Fix common misspellings
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wireless / libertas / if_cs.c
CommitLineData
27590d06
HS
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>
5a0e3ad6 25#include <linux/slab.h>
27590d06
HS
26#include <linux/delay.h>
27#include <linux/moduleparam.h>
28#include <linux/firmware.h>
29#include <linux/netdevice.h>
30
27590d06
HS
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
47MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
48MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
49MODULE_LICENSE("GPL");
50
51
52
53/********************************************************************/
54/* Data structures */
55/********************************************************************/
56
57struct if_cs_card {
58 struct pcmcia_device *p_dev;
69f9032d 59 struct lbs_private *priv;
27590d06 60 void __iomem *iobase;
9d45368a 61 bool align_regs;
82222e9b 62 u32 model;
27590d06
HS
63};
64
65
82222e9b
DW
66enum {
67 MODEL_UNKNOWN = 0x00,
68 MODEL_8305 = 0x01,
69 MODEL_8381 = 0x02,
70 MODEL_8385 = 0x03
71};
72
73static 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};
82MODULE_FIRMWARE("libertas/cf8305.bin");
83MODULE_FIRMWARE("libertas/cf8381_helper.bin");
84MODULE_FIRMWARE("libertas/cf8381.bin");
85MODULE_FIRMWARE("libertas/cf8385_helper.bin");
86MODULE_FIRMWARE("libertas/cf8385.bin");
87MODULE_FIRMWARE("libertas_cs_helper.fw");
88MODULE_FIRMWARE("libertas_cs.fw");
89
27590d06
HS
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
101static int debug_output = 0;
102#else
103/* This way the compiler optimizes the printk's away */
104#define debug_output 0
105#endif
106
107static 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)
7919b89c 111 printk(KERN_INFO "inb %08x<%02x\n", reg, val);
27590d06
HS
112 return val;
113}
114static 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)
7919b89c 118 printk(KERN_INFO "inw %08x<%04x\n", reg, val);
27590d06
HS
119 return val;
120}
121static 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)
7919b89c 128 printk(KERN_INFO "insw %08x<(0x%lx words)\n",
27590d06
HS
129 reg, count);
130 ioread16_rep(card->iobase + reg, buf, count);
131}
132
133static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
134{
135 if (debug_output)
7919b89c 136 printk(KERN_INFO "outb %08x>%02x\n", reg, val);
27590d06
HS
137 iowrite8(val, card->iobase + reg);
138}
139
140static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
141{
142 if (debug_output)
7919b89c 143 printk(KERN_INFO "outw %08x>%04x\n", reg, val);
27590d06
HS
144 iowrite16(val, card->iobase + reg);
145}
146
147static inline void if_cs_write16_rep(
148 struct if_cs_card *card,
149 uint reg,
6dfff895 150 const void *buf,
27590d06
HS
151 unsigned long count)
152{
153 if (debug_output)
7919b89c 154 printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
27590d06
HS
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 */
172static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
173{
174 int i;
175
93416c87 176 for (i = 0; i < 100000; i++) {
27590d06
HS
177 u8 val = if_cs_read8(card, addr);
178 if (val == reg)
1fac36ee 179 return 0;
93416c87 180 udelay(5);
27590d06
HS
181 }
182 return -ETIME;
183}
184
185
186
53143256
HS
187/*
188 * First the bitmasks for the host/card interrupt/status registers:
189 */
78cf0747
HS
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
27590d06 196
53143256
HS
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 */
78cf0747 210#define IF_CS_HOST_STATUS 0x00000000
27590d06 211
53143256
HS
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 */
78cf0747 222#define IF_CS_HOST_INT_CAUSE 0x00000002
27590d06 223
53143256
HS
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 */
78cf0747 228#define IF_CS_HOST_INT_MASK 0x00000004
27590d06 229
53143256
HS
230/*
231 * Used to send or receive data packets:
232 */
a78a8325
HS
233#define IF_CS_WRITE 0x00000016
234#define IF_CS_WRITE_LEN 0x00000014
78cf0747
HS
235#define IF_CS_READ 0x00000010
236#define IF_CS_READ_LEN 0x00000024
27590d06 237
53143256
HS
238/*
239 * Used to send commands (and to send firmware block) and to
240 * receive command responses:
241 */
a78a8325
HS
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
27590d06 246
53143256
HS
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 */
78cf0747
HS
264#define IF_CS_CARD_STATUS 0x00000020
265#define IF_CS_CARD_STATUS_MASK 0x7f00
27590d06 266
53143256
HS
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 */
78cf0747 277#define IF_CS_CARD_INT_CAUSE 0x00000022
27590d06 278
53143256
HS
279/*
280 * This is used to for handshaking with the card's bootloader/helper image
281 * to synchronize downloading of firmware blocks.
282 */
a78a8325
HS
283#define IF_CS_SQ_READ_LOW 0x00000028
284#define IF_CS_SQ_HELPER_OK 0x10
27590d06 285
53143256
HS
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 */
27590d06 292#define IF_CS_SCRATCH 0x0000003F
53143256
HS
293#define IF_CS_SCRATCH_BOOT_OK 0x00
294#define IF_CS_SCRATCH_HELPER_OK 0x5a
27590d06 295
4c55523e
HS
296/*
297 * Used to detect ancient chips:
298 */
299#define IF_CS_PRODUCT_ID 0x0000001C
300#define IF_CS_CF8385_B1_REV 0x12
dc7d243d 301#define IF_CS_CF8381_B3_REV 0x04
9d45368a 302#define IF_CS_CF8305_B1_REV 0x03
4c55523e 303
dc7d243d
MV
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 */
9d45368a
MV
308#define CF8305_MANFID 0x02db
309#define CF8305_CARDID 0x8103
dc7d243d
MV
310#define CF8381_MANFID 0x02db
311#define CF8381_CARDID 0x6064
312#define CF8385_MANFID 0x02df
313#define CF8385_CARDID 0x8103
314
82222e9b
DW
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 */
318static inline u32 get_model(u16 manf_id, u16 card_id)
dc7d243d 319{
82222e9b
DW
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;
dc7d243d 328}
27590d06 329
27590d06 330/********************************************************************/
43d01c56 331/* I/O and interrupt handling */
27590d06
HS
332/********************************************************************/
333
43d01c56
HS
334static inline void if_cs_enable_ints(struct if_cs_card *card)
335{
336 lbs_deb_enter(LBS_DEB_CS);
78cf0747 337 if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
43d01c56
HS
338}
339
340static inline void if_cs_disable_ints(struct if_cs_card *card)
341{
342 lbs_deb_enter(LBS_DEB_CS);
78cf0747 343 if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
43d01c56
HS
344}
345
27590d06
HS
346/*
347 * Called from if_cs_host_to_card to send a command to the hardware
348 */
69f9032d 349static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
27590d06
HS
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);
43d01c56 356 if_cs_disable_ints(card);
27590d06
HS
357
358 /* Is hardware ready? */
359 while (1) {
a78a8325
HS
360 u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
361 if (status & IF_CS_BIT_COMMAND)
27590d06
HS
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
a78a8325 370 if_cs_write16(card, IF_CS_CMD_LEN, nb);
27590d06 371
a78a8325 372 if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
27590d06
HS
373 /* Are we supposed to transfer an odd amount of bytes? */
374 if (nb & 1)
a78a8325 375 if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
27590d06
HS
376
377 /* "Assert the download over interrupt command in the Host
378 * status register" */
78cf0747 379 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
27590d06
HS
380
381 /* "Assert the download over interrupt command in the Card
382 * interrupt case register" */
78cf0747 383 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
27590d06
HS
384 ret = 0;
385
386done:
43d01c56 387 if_cs_enable_ints(card);
27590d06
HS
388 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
389 return ret;
390}
391
27590d06
HS
392/*
393 * Called from if_cs_host_to_card to send a data to the hardware
394 */
69f9032d 395static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
27590d06
HS
396{
397 struct if_cs_card *card = (struct if_cs_card *)priv->card;
43d01c56 398 u16 status;
27590d06
HS
399
400 lbs_deb_enter(LBS_DEB_CS);
43d01c56
HS
401 if_cs_disable_ints(card);
402
78cf0747
HS
403 status = if_cs_read16(card, IF_CS_CARD_STATUS);
404 BUG_ON((status & IF_CS_BIT_TX) == 0);
27590d06 405
a78a8325 406 if_cs_write16(card, IF_CS_WRITE_LEN, nb);
27590d06
HS
407
408 /* write even number of bytes, then odd byte if necessary */
a78a8325 409 if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
27590d06 410 if (nb & 1)
a78a8325 411 if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
27590d06 412
78cf0747
HS
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);
43d01c56 415 if_cs_enable_ints(card);
27590d06
HS
416
417 lbs_deb_leave(LBS_DEB_CS);
418}
419
27590d06
HS
420/*
421 * Get the command result out of the card.
422 */
69f9032d 423static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
27590d06 424{
7919b89c 425 unsigned long flags;
27590d06 426 int ret = -1;
78cf0747 427 u16 status;
27590d06
HS
428
429 lbs_deb_enter(LBS_DEB_CS);
430
431 /* is hardware ready? */
78cf0747
HS
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;
27590d06
HS
436 goto out;
437 }
438
a78a8325 439 *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
ddac4526 440 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
27590d06
HS
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 */
a78a8325 446 if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
27590d06 447 if (*len & 1)
a78a8325 448 data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
27590d06 449
09d4fad6
HS
450 /* This is a workaround for a firmware that reports too much
451 * bytes */
452 *len -= 8;
27590d06 453 ret = 0;
7919b89c
HS
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
27590d06
HS
460out:
461 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
462 return ret;
463}
464
69f9032d 465static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
27590d06
HS
466{
467 struct sk_buff *skb = NULL;
468 u16 len;
469 u8 *data;
470
471 lbs_deb_enter(LBS_DEB_CS);
472
78cf0747 473 len = if_cs_read16(priv->card, IF_CS_READ_LEN);
27590d06
HS
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);
bbfc6b78 476 priv->dev->stats.rx_dropped++;
27590d06
HS
477 goto dat_err;
478 }
479
f31ce76b 480 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
27590d06
HS
481 if (!skb)
482 goto out;
f31ce76b
VD
483 skb_put(skb, len);
484 skb_reserve(skb, 2);/* 16 byte align */
485 data = skb->data;
27590d06
HS
486
487 /* read even number of bytes, then odd byte if necessary */
78cf0747 488 if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
27590d06 489 if (len & 1)
78cf0747 490 data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
27590d06
HS
491
492dat_err:
78cf0747
HS
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);
27590d06
HS
495
496out:
497 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
498 return skb;
499}
500
7919b89c
HS
501static 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
43d01c56 509 /* Ask card interrupt cause register if there is something for us */
78cf0747 510 cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
30735561
HS
511 lbs_deb_cs("cause 0x%04x\n", cause);
512
7919b89c
HS
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
78cf0747 524 if (cause & IF_CS_BIT_RX) {
7919b89c
HS
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
78cf0747 532 if (cause & IF_CS_BIT_TX) {
43d01c56 533 lbs_deb_cs("tx done\n");
7919b89c
HS
534 lbs_host_to_card_done(priv);
535 }
536
78cf0747 537 if (cause & IF_CS_BIT_RESP) {
7919b89c
HS
538 unsigned long flags;
539 u8 i;
540
43d01c56 541 lbs_deb_cs("cmd resp\n");
7919b89c
HS
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
78cf0747 555 if (cause & IF_CS_BIT_EVENT) {
a78a8325 556 u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
78cf0747
HS
557 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
558 IF_CS_BIT_EVENT);
a78a8325 559 lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
7919b89c
HS
560 }
561
30735561
HS
562 /* Clear interrupt cause */
563 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
564
43d01c56 565 lbs_deb_leave(LBS_DEB_CS);
7919b89c
HS
566 return IRQ_HANDLED;
567}
568
569
570
571
27590d06
HS
572/********************************************************************/
573/* Firmware */
574/********************************************************************/
575
576/*
577 * Tries to program the helper firmware.
578 *
579 * Return 0 on success
580 */
82222e9b 581static int if_cs_prog_helper(struct if_cs_card *card, const struct firmware *fw)
27590d06
HS
582{
583 int ret = 0;
584 int sent = 0;
585 u8 scratch;
27590d06
HS
586
587 lbs_deb_enter(LBS_DEB_CS);
588
9d45368a
MV
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);
27590d06
HS
598
599 /* "If the value is 0x5a, the firmware is already
600 * downloaded successfully"
601 */
53143256 602 if (scratch == IF_CS_SCRATCH_HELPER_OK)
27590d06
HS
603 goto done;
604
605 /* "If the value is != 00, it is invalid value of register */
53143256 606 if (scratch != IF_CS_SCRATCH_BOOT_OK) {
27590d06
HS
607 ret = -ENODEV;
608 goto done;
609 }
610
4ecd41bd 611 lbs_deb_cs("helper size %td\n", fw->size);
27590d06
HS
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;
27590d06
HS
623
624 /* "write the number of bytes to be sent to the I/O Command
625 * write length register" */
a78a8325 626 if_cs_write16(card, IF_CS_CMD_LEN, count);
27590d06
HS
627
628 /* "write this to I/O Command port register as 16 bit writes */
629 if (count)
a78a8325 630 if_cs_write16_rep(card, IF_CS_CMD,
27590d06
HS
631 &fw->data[sent],
632 count >> 1);
633
634 /* "Assert the download over interrupt command in the Host
635 * status register" */
78cf0747 636 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
27590d06
HS
637
638 /* "Assert the download over interrupt command in the Card
639 * interrupt case register" */
78cf0747 640 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
27590d06
HS
641
642 /* "The host polls the Card Status register ... for 50 ms before
643 declaring a failure */
78cf0747
HS
644 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
645 IF_CS_BIT_COMMAND);
27590d06
HS
646 if (ret < 0) {
647 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
648 sent, ret);
82222e9b 649 goto done;
27590d06
HS
650 }
651
652 if (count == 0)
653 break;
654
655 sent += count;
656 }
657
27590d06
HS
658done:
659 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
660 return ret;
661}
662
663
82222e9b 664static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
27590d06 665{
27590d06
HS
666 int ret = 0;
667 int retry = 0;
668 int len = 0;
669 int sent;
670
671 lbs_deb_enter(LBS_DEB_CS);
672
4ecd41bd 673 lbs_deb_cs("fw size %td\n", fw->size);
27590d06 674
a78a8325
HS
675 ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
676 IF_CS_SQ_HELPER_OK);
27590d06 677 if (ret < 0) {
27590d06 678 lbs_pr_err("helper firmware doesn't answer\n");
82222e9b 679 goto done;
27590d06
HS
680 }
681
682 for (sent = 0; sent < fw->size; sent += len) {
a78a8325 683 len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
27590d06
HS
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;
82222e9b 694 goto done;
27590d06
HS
695 }
696 if (retry) {
697 sent -= len;
698 }
699
700
a78a8325 701 if_cs_write16(card, IF_CS_CMD_LEN, len);
27590d06 702
a78a8325 703 if_cs_write16_rep(card, IF_CS_CMD,
27590d06
HS
704 &fw->data[sent],
705 (len+1) >> 1);
78cf0747
HS
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);
27590d06 708
78cf0747
HS
709 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
710 IF_CS_BIT_COMMAND);
27590d06
HS
711 if (ret < 0) {
712 lbs_pr_err("can't download firmware at 0x%x\n", sent);
82222e9b 713 goto done;
27590d06
HS
714 }
715 }
716
717 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
9a52028e 718 if (ret < 0)
27590d06 719 lbs_pr_err("firmware download failed\n");
27590d06 720
27590d06
HS
721done:
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
27590d06 732/* Send commands or data packets to the card */
69f9032d
HS
733static int if_cs_host_to_card(struct lbs_private *priv,
734 u8 type,
735 u8 *buf,
736 u16 nb)
27590d06
HS
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:
6f05cbe5 744 priv->dnld_sent = DNLD_DATA_SENT;
27590d06
HS
745 if_cs_send_data(priv, buf, nb);
746 ret = 0;
747 break;
748 case MVMS_CMD:
6f05cbe5 749 priv->dnld_sent = DNLD_CMD_SENT;
27590d06
HS
750 ret = if_cs_send_cmd(priv, buf, nb);
751 break;
752 default:
c94c93da 753 lbs_pr_err("%s: unsupported type %d\n", __func__, type);
27590d06
HS
754 }
755
756 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
757 return ret;
758}
759
760
27590d06
HS
761static 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
eb14120f 767 free_irq(p_dev->irq, card);
fdfb92ea 768 pcmcia_disable_device(p_dev);
27590d06
HS
769 if (card->iobase)
770 ioport_unmap(card->iobase);
771
772 lbs_deb_leave(LBS_DEB_CS);
773}
774
775
00990e7c 776static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
aaa8cfda 777{
00990e7c 778 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
90abdc3b 779 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
aaa8cfda 780
00990e7c 781 if (p_dev->resource[1]->end) {
aaa8cfda
DB
782 lbs_pr_err("wrong CIS (check number of IO windows)\n");
783 return -ENODEV;
784 }
785
786 /* This reserves IO space but doesn't actually enable it */
90abdc3b 787 return pcmcia_request_io(p_dev);
aaa8cfda
DB
788}
789
27590d06
HS
790static int if_cs_probe(struct pcmcia_device *p_dev)
791{
792 int ret = -ENOMEM;
dc7d243d 793 unsigned int prod_id;
69f9032d 794 struct lbs_private *priv;
27590d06 795 struct if_cs_card *card;
82222e9b
DW
796 const struct firmware *helper = NULL;
797 const struct firmware *mainfw = NULL;
27590d06
HS
798
799 lbs_deb_enter(LBS_DEB_CS);
800
801 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
802 if (!card) {
803 lbs_pr_err("error in kzalloc\n");
804 goto out;
805 }
806 card->p_dev = p_dev;
807 p_dev->priv = card;
808
00990e7c
DB
809 p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
810
aaa8cfda
DB
811 if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
812 lbs_pr_err("error in pcmcia_loop_config\n");
27590d06
HS
813 goto out1;
814 }
27590d06 815
27590d06
HS
816 /*
817 * Allocate an interrupt line. Note that this does not assign
818 * a handler to the interrupt, unless the 'Handler' member of
819 * the irq structure is initialized.
820 */
eb14120f
DB
821 if (!p_dev->irq)
822 goto out1;
27590d06
HS
823
824 /* Initialize io access */
9a017a91
DB
825 card->iobase = ioport_map(p_dev->resource[0]->start,
826 resource_size(p_dev->resource[0]));
27590d06
HS
827 if (!card->iobase) {
828 lbs_pr_err("error in ioport_map\n");
829 ret = -EIO;
830 goto out1;
831 }
832
1ac71e5a 833 ret = pcmcia_enable_device(p_dev);
27590d06 834 if (ret) {
1ac71e5a 835 lbs_pr_err("error in pcmcia_enable_device\n");
27590d06
HS
836 goto out2;
837 }
838
839 /* Finally, report what we've done */
9a017a91 840 lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
27590d06 841
9d45368a
MV
842 /*
843 * Most of the libertas cards can do unaligned register access, but some
844 * weird ones can not. That's especially true for the CF8305 card.
845 */
846 card->align_regs = 0;
847
82222e9b
DW
848 card->model = get_model(p_dev->manf_id, p_dev->card_id);
849 if (card->model == MODEL_UNKNOWN) {
850 lbs_pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
851 p_dev->manf_id, p_dev->card_id);
852 goto out2;
853 }
854
4c55523e 855 /* Check if we have a current silicon */
dc7d243d 856 prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
82222e9b 857 if (card->model == MODEL_8305) {
9d45368a
MV
858 card->align_regs = 1;
859 if (prod_id < IF_CS_CF8305_B1_REV) {
82222e9b 860 lbs_pr_err("8305 rev B0 and older are not supported\n");
9d45368a
MV
861 ret = -ENODEV;
862 goto out2;
863 }
864 }
865
82222e9b
DW
866 if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
867 lbs_pr_err("8381 rev B2 and older are not supported\n");
dc7d243d
MV
868 ret = -ENODEV;
869 goto out2;
870 }
871
82222e9b
DW
872 if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
873 lbs_pr_err("8385 rev B0 and older are not supported\n");
4c55523e
HS
874 ret = -ENODEV;
875 goto out2;
876 }
27590d06 877
82222e9b
DW
878 ret = lbs_get_firmware(&p_dev->dev, NULL, NULL, card->model,
879 &fw_table[0], &helper, &mainfw);
880 if (ret) {
881 lbs_pr_err("failed to find firmware (%d)\n", ret);
882 goto out2;
883 }
884
27590d06 885 /* Load the firmware early, before calling into libertas.ko */
82222e9b
DW
886 ret = if_cs_prog_helper(card, helper);
887 if (ret == 0 && (card->model != MODEL_8305))
888 ret = if_cs_prog_real(card, mainfw);
27590d06
HS
889 if (ret)
890 goto out2;
891
892 /* Make this card known to the libertas driver */
10078321 893 priv = lbs_add_card(card, &p_dev->dev);
27590d06
HS
894 if (!priv) {
895 ret = -ENOMEM;
896 goto out2;
897 }
898
7919b89c 899 /* Finish setting up fields in lbs_private */
954ee164 900 card->priv = priv;
27590d06 901 priv->card = card;
7919b89c 902 priv->hw_host_to_card = if_cs_host_to_card;
49125454
AK
903 priv->enter_deep_sleep = NULL;
904 priv->exit_deep_sleep = NULL;
905 priv->reset_deep_sleep_wakeup = NULL;
aa21c004 906 priv->fw_ready = 1;
954ee164 907
27590d06 908 /* Now actually get the IRQ */
eb14120f 909 ret = request_irq(p_dev->irq, if_cs_interrupt,
27590d06
HS
910 IRQF_SHARED, DRV_NAME, card);
911 if (ret) {
912 lbs_pr_err("error in request_irq\n");
913 goto out3;
914 }
915
25985edc 916 /* Clear any interrupt cause that happened while sending
4ef31702 917 * firmware/initializing card */
78cf0747 918 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
28de0b36
RM
919 if_cs_enable_ints(card);
920
27590d06 921 /* And finally bring the card up */
10078321 922 if (lbs_start_card(priv) != 0) {
27590d06
HS
923 lbs_pr_err("could not activate card\n");
924 goto out3;
925 }
926
927 ret = 0;
928 goto out;
929
930out3:
10078321 931 lbs_remove_card(priv);
27590d06
HS
932out2:
933 ioport_unmap(card->iobase);
934out1:
935 pcmcia_disable_device(p_dev);
936out:
82222e9b
DW
937 if (helper)
938 release_firmware(helper);
939 if (mainfw)
940 release_firmware(mainfw);
941
27590d06
HS
942 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
943 return ret;
944}
945
946
27590d06
HS
947static void if_cs_detach(struct pcmcia_device *p_dev)
948{
949 struct if_cs_card *card = p_dev->priv;
950
951 lbs_deb_enter(LBS_DEB_CS);
952
10078321
HS
953 lbs_stop_card(card->priv);
954 lbs_remove_card(card->priv);
28de0b36 955 if_cs_disable_ints(card);
27590d06
HS
956 if_cs_release(p_dev);
957 kfree(card);
958
959 lbs_deb_leave(LBS_DEB_CS);
960}
961
962
963
964/********************************************************************/
965/* Module initialization */
966/********************************************************************/
967
968static struct pcmcia_device_id if_cs_ids[] = {
9d45368a 969 PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
dc7d243d
MV
970 PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
971 PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
82222e9b 972 /* NOTE: keep in sync with get_model() */
27590d06
HS
973 PCMCIA_DEVICE_NULL,
974};
975MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
976
977
10078321 978static struct pcmcia_driver lbs_driver = {
27590d06 979 .owner = THIS_MODULE,
2e9b981a 980 .name = DRV_NAME,
27590d06
HS
981 .probe = if_cs_probe,
982 .remove = if_cs_detach,
983 .id_table = if_cs_ids,
984};
985
986
987static int __init if_cs_init(void)
988{
989 int ret;
990
991 lbs_deb_enter(LBS_DEB_CS);
10078321 992 ret = pcmcia_register_driver(&lbs_driver);
27590d06
HS
993 lbs_deb_leave(LBS_DEB_CS);
994 return ret;
995}
996
997
998static void __exit if_cs_exit(void)
999{
1000 lbs_deb_enter(LBS_DEB_CS);
10078321 1001 pcmcia_unregister_driver(&lbs_driver);
27590d06
HS
1002 lbs_deb_leave(LBS_DEB_CS);
1003}
1004
1005
1006module_init(if_cs_init);
1007module_exit(if_cs_exit);