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