~ubuntu-branches/ubuntu/natty/linux-backports-modules-2.6.38/natty-proposed

« back to all changes in this revision

Viewing changes to updates/compat-wireless-2.6.37/drivers/net/wireless/libertas/if_cs.c

  • Committer: Bazaar Package Importer
  • Author(s): Tim Gardner, Tim Gardner
  • Date: 2011-06-08 10:44:09 UTC
  • Revision ID: james.westby@ubuntu.com-20110608104409-fnl8carkdo15bwsz
Tags: 2.6.38-10.6
[ Tim Gardner ]

Shorten compat-wireless package name to cw to accomodate
CDROM file name length restrictions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
  Driver for the Marvell 8385 based compact flash WLAN cards.
4
 
 
5
 
  (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de>
6
 
 
7
 
  This program is free software; you can redistribute it and/or modify
8
 
  it under the terms of the GNU General Public License as published by
9
 
  the Free Software Foundation; either version 2 of the License, or
10
 
  (at your option) any later version.
11
 
 
12
 
  This program is distributed in the hope that it will be useful,
13
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
  GNU General Public License for more details.
16
 
 
17
 
  You should have received a copy of the GNU General Public License
18
 
  along with this program; see the file COPYING.  If not, write to
19
 
  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
20
 
  Boston, MA 02110-1301, USA.
21
 
 
22
 
*/
23
 
 
24
 
#include <linux/module.h>
25
 
#include <linux/slab.h>
26
 
#include <linux/delay.h>
27
 
#include <linux/moduleparam.h>
28
 
#include <linux/firmware.h>
29
 
#include <linux/netdevice.h>
30
 
 
31
 
#include <pcmcia/cistpl.h>
32
 
#include <pcmcia/ds.h>
33
 
 
34
 
#include <linux/io.h>
35
 
 
36
 
#define DRV_NAME "libertas_cs"
37
 
 
38
 
#include "decl.h"
39
 
#include "defs.h"
40
 
#include "dev.h"
41
 
 
42
 
 
43
 
/********************************************************************/
44
 
/* Module stuff                                                     */
45
 
/********************************************************************/
46
 
 
47
 
MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
48
 
MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
49
 
MODULE_LICENSE("GPL");
50
 
 
51
 
 
52
 
 
53
 
/********************************************************************/
54
 
/* Data structures                                                  */
55
 
/********************************************************************/
56
 
 
57
 
struct if_cs_card {
58
 
        struct pcmcia_device *p_dev;
59
 
        struct lbs_private *priv;
60
 
        void __iomem *iobase;
61
 
        bool align_regs;
62
 
        u32 model;
63
 
};
64
 
 
65
 
 
66
 
enum {
67
 
        MODEL_UNKNOWN = 0x00,
68
 
        MODEL_8305 = 0x01,
69
 
        MODEL_8381 = 0x02,
70
 
        MODEL_8385 = 0x03
71
 
};
72
 
 
73
 
static const struct lbs_fw_table fw_table[] = {
74
 
        { MODEL_8305, "libertas/cf8305.bin", NULL },
75
 
        { MODEL_8305, "libertas_cs_helper.fw", NULL },
76
 
        { MODEL_8381, "libertas/cf8381_helper.bin", "libertas/cf8381.bin" },
77
 
        { MODEL_8381, "libertas_cs_helper.fw", "libertas_cs.fw" },
78
 
        { MODEL_8385, "libertas/cf8385_helper.bin", "libertas/cf8385.bin" },
79
 
        { MODEL_8385, "libertas_cs_helper.fw", "libertas_cs.fw" },
80
 
        { 0, NULL, NULL }
81
 
};
82
 
MODULE_FIRMWARE("libertas/cf8305.bin");
83
 
MODULE_FIRMWARE("libertas/cf8381_helper.bin");
84
 
MODULE_FIRMWARE("libertas/cf8381.bin");
85
 
MODULE_FIRMWARE("libertas/cf8385_helper.bin");
86
 
MODULE_FIRMWARE("libertas/cf8385.bin");
87
 
MODULE_FIRMWARE("libertas_cs_helper.fw");
88
 
MODULE_FIRMWARE("libertas_cs.fw");
89
 
 
90
 
 
91
 
/********************************************************************/
92
 
/* Hardware access                                                  */
93
 
/********************************************************************/
94
 
 
95
 
/* This define enables wrapper functions which allow you
96
 
   to dump all register accesses. You normally won't this,
97
 
   except for development */
98
 
/* #define DEBUG_IO */
99
 
 
100
 
#ifdef DEBUG_IO
101
 
static int debug_output = 0;
102
 
#else
103
 
/* This way the compiler optimizes the printk's away */
104
 
#define debug_output 0
105
 
#endif
106
 
 
107
 
static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
108
 
{
109
 
        unsigned int val = ioread8(card->iobase + reg);
110
 
        if (debug_output)
111
 
                printk(KERN_INFO "inb %08x<%02x\n", reg, val);
112
 
        return val;
113
 
}
114
 
static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
115
 
{
116
 
        unsigned int val = ioread16(card->iobase + reg);
117
 
        if (debug_output)
118
 
                printk(KERN_INFO "inw %08x<%04x\n", reg, val);
119
 
        return val;
120
 
}
121
 
static inline void if_cs_read16_rep(
122
 
        struct if_cs_card *card,
123
 
        uint reg,
124
 
        void *buf,
125
 
        unsigned long count)
126
 
{
127
 
        if (debug_output)
128
 
                printk(KERN_INFO "insw %08x<(0x%lx words)\n",
129
 
                        reg, count);
130
 
        ioread16_rep(card->iobase + reg, buf, count);
131
 
}
132
 
 
133
 
static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
134
 
{
135
 
        if (debug_output)
136
 
                printk(KERN_INFO "outb %08x>%02x\n", reg, val);
137
 
        iowrite8(val, card->iobase + reg);
138
 
}
139
 
 
140
 
static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
141
 
{
142
 
        if (debug_output)
143
 
                printk(KERN_INFO "outw %08x>%04x\n", reg, val);
144
 
        iowrite16(val, card->iobase + reg);
145
 
}
146
 
 
147
 
static inline void if_cs_write16_rep(
148
 
        struct if_cs_card *card,
149
 
        uint reg,
150
 
        const void *buf,
151
 
        unsigned long count)
152
 
{
153
 
        if (debug_output)
154
 
                printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
155
 
                        reg, count);
156
 
        iowrite16_rep(card->iobase + reg, buf, count);
157
 
}
158
 
 
159
 
 
160
 
/*
161
 
 * I know that polling/delaying is frowned upon. However, this procedure
162
 
 * with polling is needed while downloading the firmware. At this stage,
163
 
 * the hardware does unfortunately not create any interrupts.
164
 
 *
165
 
 * Fortunately, this function is never used once the firmware is in
166
 
 * the card. :-)
167
 
 *
168
 
 * As a reference, see the "Firmware Specification v5.1", page 18
169
 
 * and 19. I did not follow their suggested timing to the word,
170
 
 * but this works nice & fast anyway.
171
 
 */
172
 
static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
173
 
{
174
 
        int i;
175
 
 
176
 
        for (i = 0; i < 100000; i++) {
177
 
                u8 val = if_cs_read8(card, addr);
178
 
                if (val == reg)
179
 
                        return 0;
180
 
                udelay(5);
181
 
        }
182
 
        return -ETIME;
183
 
}
184
 
 
185
 
 
186
 
 
187
 
/*
188
 
 * First the bitmasks for the host/card interrupt/status registers:
189
 
 */
190
 
#define IF_CS_BIT_TX                    0x0001
191
 
#define IF_CS_BIT_RX                    0x0002
192
 
#define IF_CS_BIT_COMMAND               0x0004
193
 
#define IF_CS_BIT_RESP                  0x0008
194
 
#define IF_CS_BIT_EVENT                 0x0010
195
 
#define IF_CS_BIT_MASK                  0x001f
196
 
 
197
 
 
198
 
 
199
 
/*
200
 
 * It's not really clear to me what the host status register is for. It
201
 
 * needs to be set almost in union with "host int cause". The following
202
 
 * bits from above are used:
203
 
 *
204
 
 *   IF_CS_BIT_TX         driver downloaded a data packet
205
 
 *   IF_CS_BIT_RX         driver got a data packet
206
 
 *   IF_CS_BIT_COMMAND    driver downloaded a command
207
 
 *   IF_CS_BIT_RESP       not used (has some meaning with powerdown)
208
 
 *   IF_CS_BIT_EVENT      driver read a host event
209
 
 */
210
 
#define IF_CS_HOST_STATUS               0x00000000
211
 
 
212
 
/*
213
 
 * With the host int cause register can the host (that is, Linux) cause
214
 
 * an interrupt in the firmware, to tell the firmware about those events:
215
 
 *
216
 
 *   IF_CS_BIT_TX         a data packet has been downloaded
217
 
 *   IF_CS_BIT_RX         a received data packet has retrieved
218
 
 *   IF_CS_BIT_COMMAND    a firmware block or a command has been downloaded
219
 
 *   IF_CS_BIT_RESP       not used (has some meaning with powerdown)
220
 
 *   IF_CS_BIT_EVENT      a host event (link lost etc) has been retrieved
221
 
 */
222
 
#define IF_CS_HOST_INT_CAUSE            0x00000002
223
 
 
224
 
/*
225
 
 * The host int mask register is used to enable/disable interrupt.  However,
226
 
 * I have the suspicion that disabled interrupts are lost.
227
 
 */
228
 
#define IF_CS_HOST_INT_MASK             0x00000004
229
 
 
230
 
/*
231
 
 * Used to send or receive data packets:
232
 
 */
233
 
#define IF_CS_WRITE                     0x00000016
234
 
#define IF_CS_WRITE_LEN                 0x00000014
235
 
#define IF_CS_READ                      0x00000010
236
 
#define IF_CS_READ_LEN                  0x00000024
237
 
 
238
 
/*
239
 
 * Used to send commands (and to send firmware block) and to
240
 
 * receive command responses:
241
 
 */
242
 
#define IF_CS_CMD                       0x0000001A
243
 
#define IF_CS_CMD_LEN                   0x00000018
244
 
#define IF_CS_RESP                      0x00000012
245
 
#define IF_CS_RESP_LEN                  0x00000030
246
 
 
247
 
/*
248
 
 * The card status registers shows what the card/firmware actually
249
 
 * accepts:
250
 
 *
251
 
 *   IF_CS_BIT_TX        you may send a data packet
252
 
 *   IF_CS_BIT_RX        you may retrieve a data packet
253
 
 *   IF_CS_BIT_COMMAND   you may send a command
254
 
 *   IF_CS_BIT_RESP      you may retrieve a command response
255
 
 *   IF_CS_BIT_EVENT     the card has a event for use (link lost, snr low etc)
256
 
 *
257
 
 * When reading this register several times, you will get back the same
258
 
 * results --- with one exception: the IF_CS_BIT_EVENT clear itself
259
 
 * automatically.
260
 
 *
261
 
 * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
262
 
 * we handle this via the card int cause register.
263
 
 */
264
 
#define IF_CS_CARD_STATUS               0x00000020
265
 
#define IF_CS_CARD_STATUS_MASK          0x7f00
266
 
 
267
 
/*
268
 
 * The card int cause register is used by the card/firmware to notify us
269
 
 * about the following events:
270
 
 *
271
 
 *   IF_CS_BIT_TX        a data packet has successfully been sentx
272
 
 *   IF_CS_BIT_RX        a data packet has been received and can be retrieved
273
 
 *   IF_CS_BIT_COMMAND   not used
274
 
 *   IF_CS_BIT_RESP      the firmware has a command response for us
275
 
 *   IF_CS_BIT_EVENT     the card has a event for use (link lost, snr low etc)
276
 
 */
277
 
#define IF_CS_CARD_INT_CAUSE            0x00000022
278
 
 
279
 
/*
280
 
 * This is used to for handshaking with the card's bootloader/helper image
281
 
 * to synchronize downloading of firmware blocks.
282
 
 */
283
 
#define IF_CS_SQ_READ_LOW               0x00000028
284
 
#define IF_CS_SQ_HELPER_OK              0x10
285
 
 
286
 
/*
287
 
 * The scratch register tells us ...
288
 
 *
289
 
 * IF_CS_SCRATCH_BOOT_OK     the bootloader runs
290
 
 * IF_CS_SCRATCH_HELPER_OK   the helper firmware already runs
291
 
 */
292
 
#define IF_CS_SCRATCH                   0x0000003F
293
 
#define IF_CS_SCRATCH_BOOT_OK           0x00
294
 
#define IF_CS_SCRATCH_HELPER_OK         0x5a
295
 
 
296
 
/*
297
 
 * Used to detect ancient chips:
298
 
 */
299
 
#define IF_CS_PRODUCT_ID                0x0000001C
300
 
#define IF_CS_CF8385_B1_REV             0x12
301
 
#define IF_CS_CF8381_B3_REV             0x04
302
 
#define IF_CS_CF8305_B1_REV             0x03
303
 
 
304
 
/*
305
 
 * Used to detect other cards than CF8385 since their revisions of silicon
306
 
 * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
307
 
 */
308
 
#define CF8305_MANFID           0x02db
309
 
#define CF8305_CARDID           0x8103
310
 
#define CF8381_MANFID           0x02db
311
 
#define CF8381_CARDID           0x6064
312
 
#define CF8385_MANFID           0x02df
313
 
#define CF8385_CARDID           0x8103
314
 
 
315
 
/* FIXME: just use the 'driver_info' field of 'struct pcmcia_device_id' when
316
 
 * that gets fixed.  Currently there's no way to access it from the probe hook.
317
 
 */
318
 
static inline u32 get_model(u16 manf_id, u16 card_id)
319
 
{
320
 
        /* NOTE: keep in sync with if_cs_ids */
321
 
        if (manf_id == CF8305_MANFID && card_id == CF8305_CARDID)
322
 
                return MODEL_8305;
323
 
        else if (manf_id == CF8381_MANFID && card_id == CF8381_CARDID)
324
 
                return MODEL_8381;
325
 
        else if (manf_id == CF8385_MANFID && card_id == CF8385_CARDID)
326
 
                return MODEL_8385;
327
 
        return MODEL_UNKNOWN;
328
 
}
329
 
 
330
 
/********************************************************************/
331
 
/* I/O and interrupt handling                                       */
332
 
/********************************************************************/
333
 
 
334
 
static inline void if_cs_enable_ints(struct if_cs_card *card)
335
 
{
336
 
        lbs_deb_enter(LBS_DEB_CS);
337
 
        if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
338
 
}
339
 
 
340
 
static inline void if_cs_disable_ints(struct if_cs_card *card)
341
 
{
342
 
        lbs_deb_enter(LBS_DEB_CS);
343
 
        if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
344
 
}
345
 
 
346
 
/*
347
 
 * Called from if_cs_host_to_card to send a command to the hardware
348
 
 */
349
 
static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
350
 
{
351
 
        struct if_cs_card *card = (struct if_cs_card *)priv->card;
352
 
        int ret = -1;
353
 
        int loops = 0;
354
 
 
355
 
        lbs_deb_enter(LBS_DEB_CS);
356
 
        if_cs_disable_ints(card);
357
 
 
358
 
        /* Is hardware ready? */
359
 
        while (1) {
360
 
                u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
361
 
                if (status & IF_CS_BIT_COMMAND)
362
 
                        break;
363
 
                if (++loops > 100) {
364
 
                        lbs_pr_err("card not ready for commands\n");
365
 
                        goto done;
366
 
                }
367
 
                mdelay(1);
368
 
        }
369
 
 
370
 
        if_cs_write16(card, IF_CS_CMD_LEN, nb);
371
 
 
372
 
        if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
373
 
        /* Are we supposed to transfer an odd amount of bytes? */
374
 
        if (nb & 1)
375
 
                if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
376
 
 
377
 
        /* "Assert the download over interrupt command in the Host
378
 
         * status register" */
379
 
        if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
380
 
 
381
 
        /* "Assert the download over interrupt command in the Card
382
 
         * interrupt case register" */
383
 
        if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
384
 
        ret = 0;
385
 
 
386
 
done:
387
 
        if_cs_enable_ints(card);
388
 
        lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
389
 
        return ret;
390
 
}
391
 
 
392
 
/*
393
 
 * Called from if_cs_host_to_card to send a data to the hardware
394
 
 */
395
 
static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
396
 
{
397
 
        struct if_cs_card *card = (struct if_cs_card *)priv->card;
398
 
        u16 status;
399
 
 
400
 
        lbs_deb_enter(LBS_DEB_CS);
401
 
        if_cs_disable_ints(card);
402
 
 
403
 
        status = if_cs_read16(card, IF_CS_CARD_STATUS);
404
 
        BUG_ON((status & IF_CS_BIT_TX) == 0);
405
 
 
406
 
        if_cs_write16(card, IF_CS_WRITE_LEN, nb);
407
 
 
408
 
        /* write even number of bytes, then odd byte if necessary */
409
 
        if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
410
 
        if (nb & 1)
411
 
                if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
412
 
 
413
 
        if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
414
 
        if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
415
 
        if_cs_enable_ints(card);
416
 
 
417
 
        lbs_deb_leave(LBS_DEB_CS);
418
 
}
419
 
 
420
 
/*
421
 
 * Get the command result out of the card.
422
 
 */
423
 
static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
424
 
{
425
 
        unsigned long flags;
426
 
        int ret = -1;
427
 
        u16 status;
428
 
 
429
 
        lbs_deb_enter(LBS_DEB_CS);
430
 
 
431
 
        /* is hardware ready? */
432
 
        status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
433
 
        if ((status & IF_CS_BIT_RESP) == 0) {
434
 
                lbs_pr_err("no cmd response in card\n");
435
 
                *len = 0;
436
 
                goto out;
437
 
        }
438
 
 
439
 
        *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
440
 
        if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
441
 
                lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
442
 
                goto out;
443
 
        }
444
 
 
445
 
        /* read even number of bytes, then odd byte if necessary */
446
 
        if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
447
 
        if (*len & 1)
448
 
                data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
449
 
 
450
 
        /* This is a workaround for a firmware that reports too much
451
 
         * bytes */
452
 
        *len -= 8;
453
 
        ret = 0;
454
 
 
455
 
        /* Clear this flag again */
456
 
        spin_lock_irqsave(&priv->driver_lock, flags);
457
 
        priv->dnld_sent = DNLD_RES_RECEIVED;
458
 
        spin_unlock_irqrestore(&priv->driver_lock, flags);
459
 
 
460
 
out:
461
 
        lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
462
 
        return ret;
463
 
}
464
 
 
465
 
static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
466
 
{
467
 
        struct sk_buff *skb = NULL;
468
 
        u16 len;
469
 
        u8 *data;
470
 
 
471
 
        lbs_deb_enter(LBS_DEB_CS);
472
 
 
473
 
        len = if_cs_read16(priv->card, IF_CS_READ_LEN);
474
 
        if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
475
 
                lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
476
 
                priv->dev->stats.rx_dropped++;
477
 
                goto dat_err;
478
 
        }
479
 
 
480
 
        skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
481
 
        if (!skb)
482
 
                goto out;
483
 
        skb_put(skb, len);
484
 
        skb_reserve(skb, 2);/* 16 byte align */
485
 
        data = skb->data;
486
 
 
487
 
        /* read even number of bytes, then odd byte if necessary */
488
 
        if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
489
 
        if (len & 1)
490
 
                data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
491
 
 
492
 
dat_err:
493
 
        if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
494
 
        if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
495
 
 
496
 
out:
497
 
        lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
498
 
        return skb;
499
 
}
500
 
 
501
 
static irqreturn_t if_cs_interrupt(int irq, void *data)
502
 
{
503
 
        struct if_cs_card *card = data;
504
 
        struct lbs_private *priv = card->priv;
505
 
        u16 cause;
506
 
 
507
 
        lbs_deb_enter(LBS_DEB_CS);
508
 
 
509
 
        /* Ask card interrupt cause register if there is something for us */
510
 
        cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
511
 
        lbs_deb_cs("cause 0x%04x\n", cause);
512
 
 
513
 
        if (cause == 0) {
514
 
                /* Not for us */
515
 
                return IRQ_NONE;
516
 
        }
517
 
 
518
 
        if (cause == 0xffff) {
519
 
                /* Read in junk, the card has probably been removed */
520
 
                card->priv->surpriseremoved = 1;
521
 
                return IRQ_HANDLED;
522
 
        }
523
 
 
524
 
        if (cause & IF_CS_BIT_RX) {
525
 
                struct sk_buff *skb;
526
 
                lbs_deb_cs("rx packet\n");
527
 
                skb = if_cs_receive_data(priv);
528
 
                if (skb)
529
 
                        lbs_process_rxed_packet(priv, skb);
530
 
        }
531
 
 
532
 
        if (cause & IF_CS_BIT_TX) {
533
 
                lbs_deb_cs("tx done\n");
534
 
                lbs_host_to_card_done(priv);
535
 
        }
536
 
 
537
 
        if (cause & IF_CS_BIT_RESP) {
538
 
                unsigned long flags;
539
 
                u8 i;
540
 
 
541
 
                lbs_deb_cs("cmd resp\n");
542
 
                spin_lock_irqsave(&priv->driver_lock, flags);
543
 
                i = (priv->resp_idx == 0) ? 1 : 0;
544
 
                spin_unlock_irqrestore(&priv->driver_lock, flags);
545
 
 
546
 
                BUG_ON(priv->resp_len[i]);
547
 
                if_cs_receive_cmdres(priv, priv->resp_buf[i],
548
 
                        &priv->resp_len[i]);
549
 
 
550
 
                spin_lock_irqsave(&priv->driver_lock, flags);
551
 
                lbs_notify_command_response(priv, i);
552
 
                spin_unlock_irqrestore(&priv->driver_lock, flags);
553
 
        }
554
 
 
555
 
        if (cause & IF_CS_BIT_EVENT) {
556
 
                u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
557
 
                if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
558
 
                        IF_CS_BIT_EVENT);
559
 
                lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
560
 
        }
561
 
 
562
 
        /* Clear interrupt cause */
563
 
        if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
564
 
 
565
 
        lbs_deb_leave(LBS_DEB_CS);
566
 
        return IRQ_HANDLED;
567
 
}
568
 
 
569
 
 
570
 
 
571
 
 
572
 
/********************************************************************/
573
 
/* Firmware                                                         */
574
 
/********************************************************************/
575
 
 
576
 
/*
577
 
 * Tries to program the helper firmware.
578
 
 *
579
 
 * Return 0 on success
580
 
 */
581
 
static int if_cs_prog_helper(struct if_cs_card *card, const struct firmware *fw)
582
 
{
583
 
        int ret = 0;
584
 
        int sent = 0;
585
 
        u8  scratch;
586
 
 
587
 
        lbs_deb_enter(LBS_DEB_CS);
588
 
 
589
 
        /*
590
 
         * This is the only place where an unaligned register access happens on
591
 
         * the CF8305 card, therefore for the sake of speed of the driver, we do
592
 
         * the alignment correction here.
593
 
         */
594
 
        if (card->align_regs)
595
 
                scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
596
 
        else
597
 
                scratch = if_cs_read8(card, IF_CS_SCRATCH);
598
 
 
599
 
        /* "If the value is 0x5a, the firmware is already
600
 
         * downloaded successfully"
601
 
         */
602
 
        if (scratch == IF_CS_SCRATCH_HELPER_OK)
603
 
                goto done;
604
 
 
605
 
        /* "If the value is != 00, it is invalid value of register */
606
 
        if (scratch != IF_CS_SCRATCH_BOOT_OK) {
607
 
                ret = -ENODEV;
608
 
                goto done;
609
 
        }
610
 
 
611
 
        lbs_deb_cs("helper size %td\n", fw->size);
612
 
 
613
 
        /* "Set the 5 bytes of the helper image to 0" */
614
 
        /* Not needed, this contains an ARM branch instruction */
615
 
 
616
 
        for (;;) {
617
 
                /* "the number of bytes to send is 256" */
618
 
                int count = 256;
619
 
                int remain = fw->size - sent;
620
 
 
621
 
                if (remain < count)
622
 
                        count = remain;
623
 
 
624
 
                /* "write the number of bytes to be sent to the I/O Command
625
 
                 * write length register" */
626
 
                if_cs_write16(card, IF_CS_CMD_LEN, count);
627
 
 
628
 
                /* "write this to I/O Command port register as 16 bit writes */
629
 
                if (count)
630
 
                        if_cs_write16_rep(card, IF_CS_CMD,
631
 
                                &fw->data[sent],
632
 
                                count >> 1);
633
 
 
634
 
                /* "Assert the download over interrupt command in the Host
635
 
                 * status register" */
636
 
                if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
637
 
 
638
 
                /* "Assert the download over interrupt command in the Card
639
 
                 * interrupt case register" */
640
 
                if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
641
 
 
642
 
                /* "The host polls the Card Status register ... for 50 ms before
643
 
                   declaring a failure */
644
 
                ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
645
 
                        IF_CS_BIT_COMMAND);
646
 
                if (ret < 0) {
647
 
                        lbs_pr_err("can't download helper at 0x%x, ret %d\n",
648
 
                                sent, ret);
649
 
                        goto done;
650
 
                }
651
 
 
652
 
                if (count == 0)
653
 
                        break;
654
 
 
655
 
                sent += count;
656
 
        }
657
 
 
658
 
done:
659
 
        lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
660
 
        return ret;
661
 
}
662
 
 
663
 
 
664
 
static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
665
 
{
666
 
        int ret = 0;
667
 
        int retry = 0;
668
 
        int len = 0;
669
 
        int sent;
670
 
 
671
 
        lbs_deb_enter(LBS_DEB_CS);
672
 
 
673
 
        lbs_deb_cs("fw size %td\n", fw->size);
674
 
 
675
 
        ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
676
 
                IF_CS_SQ_HELPER_OK);
677
 
        if (ret < 0) {
678
 
                lbs_pr_err("helper firmware doesn't answer\n");
679
 
                goto done;
680
 
        }
681
 
 
682
 
        for (sent = 0; sent < fw->size; sent += len) {
683
 
                len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
684
 
                if (len & 1) {
685
 
                        retry++;
686
 
                        lbs_pr_info("odd, need to retry this firmware block\n");
687
 
                } else {
688
 
                        retry = 0;
689
 
                }
690
 
 
691
 
                if (retry > 20) {
692
 
                        lbs_pr_err("could not download firmware\n");
693
 
                        ret = -ENODEV;
694
 
                        goto done;
695
 
                }
696
 
                if (retry) {
697
 
                        sent -= len;
698
 
                }
699
 
 
700
 
 
701
 
                if_cs_write16(card, IF_CS_CMD_LEN, len);
702
 
 
703
 
                if_cs_write16_rep(card, IF_CS_CMD,
704
 
                        &fw->data[sent],
705
 
                        (len+1) >> 1);
706
 
                if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
707
 
                if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
708
 
 
709
 
                ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
710
 
                        IF_CS_BIT_COMMAND);
711
 
                if (ret < 0) {
712
 
                        lbs_pr_err("can't download firmware at 0x%x\n", sent);
713
 
                        goto done;
714
 
                }
715
 
        }
716
 
 
717
 
        ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
718
 
        if (ret < 0)
719
 
                lbs_pr_err("firmware download failed\n");
720
 
 
721
 
done:
722
 
        lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
723
 
        return ret;
724
 
}
725
 
 
726
 
 
727
 
 
728
 
/********************************************************************/
729
 
/* Callback functions for libertas.ko                               */
730
 
/********************************************************************/
731
 
 
732
 
/* Send commands or data packets to the card */
733
 
static int if_cs_host_to_card(struct lbs_private *priv,
734
 
        u8 type,
735
 
        u8 *buf,
736
 
        u16 nb)
737
 
{
738
 
        int ret = -1;
739
 
 
740
 
        lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
741
 
 
742
 
        switch (type) {
743
 
        case MVMS_DAT:
744
 
                priv->dnld_sent = DNLD_DATA_SENT;
745
 
                if_cs_send_data(priv, buf, nb);
746
 
                ret = 0;
747
 
                break;
748
 
        case MVMS_CMD:
749
 
                priv->dnld_sent = DNLD_CMD_SENT;
750
 
                ret = if_cs_send_cmd(priv, buf, nb);
751
 
                break;
752
 
        default:
753
 
                lbs_pr_err("%s: unsupported type %d\n", __func__, type);
754
 
        }
755
 
 
756
 
        lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
757
 
        return ret;
758
 
}
759
 
 
760
 
 
761
 
static void if_cs_release(struct pcmcia_device *p_dev)
762
 
{
763
 
        struct if_cs_card *card = p_dev->priv;
764
 
 
765
 
        lbs_deb_enter(LBS_DEB_CS);
766
 
 
767
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
768
 
        free_irq(p_dev->irq, card);
769
 
#else
770
 
        free_irq(p_dev->irq.AssignedIRQ, card);
771
 
#endif
772
 
        pcmcia_disable_device(p_dev);
773
 
        if (card->iobase)
774
 
                ioport_unmap(card->iobase);
775
 
 
776
 
        lbs_deb_leave(LBS_DEB_CS);
777
 
}
778
 
 
779
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
780
 
static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
781
 
{
782
 
        p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
783
 
        p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
784
 
 
785
 
        if (p_dev->resource[1]->end) {
786
 
                lbs_pr_err("wrong CIS (check number of IO windows)\n");
787
 
                return -ENODEV;
788
 
        }
789
 
#else
790
 
static int if_cs_ioprobe(struct pcmcia_device *p_dev,
791
 
                         cistpl_cftable_entry_t *cfg,
792
 
                         cistpl_cftable_entry_t *dflt,
793
 
                         unsigned int vcc,
794
 
                         void *priv_data)
795
 
{
796
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
797
 
        p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
798
 
        p_dev->resource[0]->start = cfg->io.win[0].base;
799
 
        p_dev->resource[0]->end = cfg->io.win[0].len;
800
 
#else
801
 
        p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
802
 
        p_dev->io.BasePort1 = cfg->io.win[0].base;
803
 
        p_dev->io.NumPorts1 = cfg->io.win[0].len;
804
 
#endif
805
 
 
806
 
        /* Do we need to allocate an interrupt? */
807
 
        p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
808
 
 
809
 
        /* IO window settings */
810
 
        if (cfg->io.nwin != 1) {
811
 
                lbs_pr_err("wrong CIS (check number of IO windows)\n");
812
 
                return -ENODEV;
813
 
        }
814
 
#endif
815
 
 
816
 
        /* This reserves IO space but doesn't actually enable it */
817
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
818
 
        return pcmcia_request_io(p_dev);
819
 
#else
820
 
        return pcmcia_request_io(p_dev, &p_dev->io);
821
 
#endif
822
 
}
823
 
 
824
 
static int if_cs_probe(struct pcmcia_device *p_dev)
825
 
{
826
 
        int ret = -ENOMEM;
827
 
        unsigned int prod_id;
828
 
        struct lbs_private *priv;
829
 
        struct if_cs_card *card;
830
 
        const struct firmware *helper = NULL;
831
 
        const struct firmware *mainfw = NULL;
832
 
 
833
 
        lbs_deb_enter(LBS_DEB_CS);
834
 
 
835
 
        card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
836
 
        if (!card) {
837
 
                lbs_pr_err("error in kzalloc\n");
838
 
                goto out;
839
 
        }
840
 
        card->p_dev = p_dev;
841
 
        p_dev->priv = card;
842
 
 
843
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
844
 
        p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
845
 
#else
846
 
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35))
847
 
        p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
848
 
        p_dev->irq.Handler = NULL;
849
 
#endif
850
 
        p_dev->conf.Attributes = 0;
851
 
        p_dev->conf.IntType = INT_MEMORY_AND_IO;
852
 
#endif
853
 
 
854
 
        if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
855
 
                lbs_pr_err("error in pcmcia_loop_config\n");
856
 
                goto out1;
857
 
        }
858
 
 
859
 
        /*
860
 
         * Allocate an interrupt line.  Note that this does not assign
861
 
         * a handler to the interrupt, unless the 'Handler' member of
862
 
         * the irq structure is initialized.
863
 
         */
864
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
865
 
        if (!p_dev->irq)
866
 
                goto out1;
867
 
#else
868
 
        if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
869
 
                ret = pcmcia_request_irq(p_dev, &p_dev->irq);
870
 
                if (ret) {
871
 
                        lbs_pr_err("error in pcmcia_request_irq\n");
872
 
                        goto out1;
873
 
                }
874
 
        }
875
 
#endif
876
 
 
877
 
        /* Initialize io access */
878
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
879
 
        card->iobase = ioport_map(p_dev->resource[0]->start,
880
 
                                resource_size(p_dev->resource[0]));
881
 
#else
882
 
        card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
883
 
#endif
884
 
        if (!card->iobase) {
885
 
                lbs_pr_err("error in ioport_map\n");
886
 
                ret = -EIO;
887
 
                goto out1;
888
 
        }
889
 
 
890
 
        ret = pcmcia_enable_device(p_dev);
891
 
        if (ret) {
892
 
                lbs_pr_err("error in pcmcia_enable_device\n");
893
 
                goto out2;
894
 
        }
895
 
 
896
 
        /* Finally, report what we've done */
897
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
898
 
        lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
899
 
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
900
 
        lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
901
 
                  p_dev->irq, p_dev->io.BasePort1,
902
 
                  p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
903
 
#else
904
 
        lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
905
 
                  p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
906
 
                  p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
907
 
#endif
908
 
 
909
 
        /*
910
 
         * Most of the libertas cards can do unaligned register access, but some
911
 
         * weird ones can not. That's especially true for the CF8305 card.
912
 
         */
913
 
        card->align_regs = 0;
914
 
 
915
 
        card->model = get_model(p_dev->manf_id, p_dev->card_id);
916
 
        if (card->model == MODEL_UNKNOWN) {
917
 
                lbs_pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
918
 
                           p_dev->manf_id, p_dev->card_id);
919
 
                goto out2;
920
 
        }
921
 
 
922
 
        /* Check if we have a current silicon */
923
 
        prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
924
 
        if (card->model == MODEL_8305) {
925
 
                card->align_regs = 1;
926
 
                if (prod_id < IF_CS_CF8305_B1_REV) {
927
 
                        lbs_pr_err("8305 rev B0 and older are not supported\n");
928
 
                        ret = -ENODEV;
929
 
                        goto out2;
930
 
                }
931
 
        }
932
 
 
933
 
        if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
934
 
                lbs_pr_err("8381 rev B2 and older are not supported\n");
935
 
                ret = -ENODEV;
936
 
                goto out2;
937
 
        }
938
 
 
939
 
        if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
940
 
                lbs_pr_err("8385 rev B0 and older are not supported\n");
941
 
                ret = -ENODEV;
942
 
                goto out2;
943
 
        }
944
 
 
945
 
        ret = lbs_get_firmware(&p_dev->dev, NULL, NULL, card->model,
946
 
                                &fw_table[0], &helper, &mainfw);
947
 
        if (ret) {
948
 
                lbs_pr_err("failed to find firmware (%d)\n", ret);
949
 
                goto out2;
950
 
        }
951
 
 
952
 
        /* Load the firmware early, before calling into libertas.ko */
953
 
        ret = if_cs_prog_helper(card, helper);
954
 
        if (ret == 0 && (card->model != MODEL_8305))
955
 
                ret = if_cs_prog_real(card, mainfw);
956
 
        if (ret)
957
 
                goto out2;
958
 
 
959
 
        /* Make this card known to the libertas driver */
960
 
        priv = lbs_add_card(card, &p_dev->dev);
961
 
        if (!priv) {
962
 
                ret = -ENOMEM;
963
 
                goto out2;
964
 
        }
965
 
 
966
 
        /* Finish setting up fields in lbs_private */
967
 
        card->priv = priv;
968
 
        priv->card = card;
969
 
        priv->hw_host_to_card = if_cs_host_to_card;
970
 
        priv->enter_deep_sleep = NULL;
971
 
        priv->exit_deep_sleep = NULL;
972
 
        priv->reset_deep_sleep_wakeup = NULL;
973
 
        priv->fw_ready = 1;
974
 
 
975
 
        /* Now actually get the IRQ */
976
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
977
 
        ret = request_irq(p_dev->irq, if_cs_interrupt,
978
 
#else
979
 
        ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
980
 
#endif
981
 
                IRQF_SHARED, DRV_NAME, card);
982
 
        if (ret) {
983
 
                lbs_pr_err("error in request_irq\n");
984
 
                goto out3;
985
 
        }
986
 
 
987
 
        /* Clear any interrupt cause that happend while sending
988
 
         * firmware/initializing card */
989
 
        if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
990
 
        if_cs_enable_ints(card);
991
 
 
992
 
        /* And finally bring the card up */
993
 
        if (lbs_start_card(priv) != 0) {
994
 
                lbs_pr_err("could not activate card\n");
995
 
                goto out3;
996
 
        }
997
 
 
998
 
        ret = 0;
999
 
        goto out;
1000
 
 
1001
 
out3:
1002
 
        lbs_remove_card(priv);
1003
 
out2:
1004
 
        ioport_unmap(card->iobase);
1005
 
out1:
1006
 
        pcmcia_disable_device(p_dev);
1007
 
out:
1008
 
        if (helper)
1009
 
                release_firmware(helper);
1010
 
        if (mainfw)
1011
 
                release_firmware(mainfw);
1012
 
 
1013
 
        lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
1014
 
        return ret;
1015
 
}
1016
 
 
1017
 
 
1018
 
static void if_cs_detach(struct pcmcia_device *p_dev)
1019
 
{
1020
 
        struct if_cs_card *card = p_dev->priv;
1021
 
 
1022
 
        lbs_deb_enter(LBS_DEB_CS);
1023
 
 
1024
 
        lbs_stop_card(card->priv);
1025
 
        lbs_remove_card(card->priv);
1026
 
        if_cs_disable_ints(card);
1027
 
        if_cs_release(p_dev);
1028
 
        kfree(card);
1029
 
 
1030
 
        lbs_deb_leave(LBS_DEB_CS);
1031
 
}
1032
 
 
1033
 
 
1034
 
 
1035
 
/********************************************************************/
1036
 
/* Module initialization                                            */
1037
 
/********************************************************************/
1038
 
 
1039
 
static struct pcmcia_device_id if_cs_ids[] = {
1040
 
        PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
1041
 
        PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
1042
 
        PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
1043
 
        /* NOTE: keep in sync with get_model() */
1044
 
        PCMCIA_DEVICE_NULL,
1045
 
};
1046
 
MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
1047
 
 
1048
 
 
1049
 
static struct pcmcia_driver lbs_driver = {
1050
 
        .owner          = THIS_MODULE,
1051
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
1052
 
        .name           = DRV_NAME,
1053
 
#else
1054
 
        .drv            = {
1055
 
                .name   = DRV_NAME,
1056
 
        },
1057
 
#endif
1058
 
        .probe          = if_cs_probe,
1059
 
        .remove         = if_cs_detach,
1060
 
        .id_table       = if_cs_ids,
1061
 
};
1062
 
 
1063
 
 
1064
 
static int __init if_cs_init(void)
1065
 
{
1066
 
        int ret;
1067
 
 
1068
 
        lbs_deb_enter(LBS_DEB_CS);
1069
 
        ret = pcmcia_register_driver(&lbs_driver);
1070
 
        lbs_deb_leave(LBS_DEB_CS);
1071
 
        return ret;
1072
 
}
1073
 
 
1074
 
 
1075
 
static void __exit if_cs_exit(void)
1076
 
{
1077
 
        lbs_deb_enter(LBS_DEB_CS);
1078
 
        pcmcia_unregister_driver(&lbs_driver);
1079
 
        lbs_deb_leave(LBS_DEB_CS);
1080
 
}
1081
 
 
1082
 
 
1083
 
module_init(if_cs_init);
1084
 
module_exit(if_cs_exit);