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