Linux-libre 3.4.28-gnu1
[librecmc/linux-libre.git] / drivers / net / wireless / 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         lbs_deb_enter(LBS_DEB_CS);
334         if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
335 }
336
337 static inline void if_cs_disable_ints(struct if_cs_card *card)
338 {
339         lbs_deb_enter(LBS_DEB_CS);
340         if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
341 }
342
343 /*
344  * Called from if_cs_host_to_card to send a command to the hardware
345  */
346 static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
347 {
348         struct if_cs_card *card = (struct if_cs_card *)priv->card;
349         int ret = -1;
350         int loops = 0;
351
352         lbs_deb_enter(LBS_DEB_CS);
353         if_cs_disable_ints(card);
354
355         /* Is hardware ready? */
356         while (1) {
357                 u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
358                 if (status & IF_CS_BIT_COMMAND)
359                         break;
360                 if (++loops > 100) {
361                         netdev_err(priv->dev, "card not ready for commands\n");
362                         goto done;
363                 }
364                 mdelay(1);
365         }
366
367         if_cs_write16(card, IF_CS_CMD_LEN, nb);
368
369         if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
370         /* Are we supposed to transfer an odd amount of bytes? */
371         if (nb & 1)
372                 if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
373
374         /* "Assert the download over interrupt command in the Host
375          * status register" */
376         if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
377
378         /* "Assert the download over interrupt command in the Card
379          * interrupt case register" */
380         if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
381         ret = 0;
382
383 done:
384         if_cs_enable_ints(card);
385         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
386         return ret;
387 }
388
389 /*
390  * Called from if_cs_host_to_card to send a data to the hardware
391  */
392 static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
393 {
394         struct if_cs_card *card = (struct if_cs_card *)priv->card;
395         u16 status;
396
397         lbs_deb_enter(LBS_DEB_CS);
398         if_cs_disable_ints(card);
399
400         status = if_cs_read16(card, IF_CS_CARD_STATUS);
401         BUG_ON((status & IF_CS_BIT_TX) == 0);
402
403         if_cs_write16(card, IF_CS_WRITE_LEN, nb);
404
405         /* write even number of bytes, then odd byte if necessary */
406         if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
407         if (nb & 1)
408                 if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
409
410         if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
411         if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
412         if_cs_enable_ints(card);
413
414         lbs_deb_leave(LBS_DEB_CS);
415 }
416
417 /*
418  * Get the command result out of the card.
419  */
420 static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
421 {
422         unsigned long flags;
423         int ret = -1;
424         u16 status;
425
426         lbs_deb_enter(LBS_DEB_CS);
427
428         /* is hardware ready? */
429         status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
430         if ((status & IF_CS_BIT_RESP) == 0) {
431                 netdev_err(priv->dev, "no cmd response in card\n");
432                 *len = 0;
433                 goto out;
434         }
435
436         *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
437         if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
438                 netdev_err(priv->dev,
439                            "card cmd buffer has invalid # of bytes (%d)\n",
440                            *len);
441                 goto out;
442         }
443
444         /* read even number of bytes, then odd byte if necessary */
445         if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
446         if (*len & 1)
447                 data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
448
449         /* This is a workaround for a firmware that reports too much
450          * bytes */
451         *len -= 8;
452         ret = 0;
453
454         /* Clear this flag again */
455         spin_lock_irqsave(&priv->driver_lock, flags);
456         priv->dnld_sent = DNLD_RES_RECEIVED;
457         spin_unlock_irqrestore(&priv->driver_lock, flags);
458
459 out:
460         lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
461         return ret;
462 }
463
464 static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
465 {
466         struct sk_buff *skb = NULL;
467         u16 len;
468         u8 *data;
469
470         lbs_deb_enter(LBS_DEB_CS);
471
472         len = if_cs_read16(priv->card, IF_CS_READ_LEN);
473         if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
474                 netdev_err(priv->dev,
475                            "card data buffer has invalid # of bytes (%d)\n",
476                            len);
477                 priv->dev->stats.rx_dropped++;
478                 goto dat_err;
479         }
480
481         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
482         if (!skb)
483                 goto out;
484         skb_put(skb, len);
485         skb_reserve(skb, 2);/* 16 byte align */
486         data = skb->data;
487
488         /* read even number of bytes, then odd byte if necessary */
489         if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
490         if (len & 1)
491                 data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
492
493 dat_err:
494         if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
495         if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
496
497 out:
498         lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
499         return skb;
500 }
501
502 static irqreturn_t if_cs_interrupt(int irq, void *data)
503 {
504         struct if_cs_card *card = data;
505         struct lbs_private *priv = card->priv;
506         u16 cause;
507
508         lbs_deb_enter(LBS_DEB_CS);
509
510         /* Ask card interrupt cause register if there is something for us */
511         cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
512         lbs_deb_cs("cause 0x%04x\n", cause);
513
514         if (cause == 0) {
515                 /* Not for us */
516                 return IRQ_NONE;
517         }
518
519         if (cause == 0xffff) {
520                 /* Read in junk, the card has probably been removed */
521                 card->priv->surpriseremoved = 1;
522                 return IRQ_HANDLED;
523         }
524
525         if (cause & IF_CS_BIT_RX) {
526                 struct sk_buff *skb;
527                 lbs_deb_cs("rx packet\n");
528                 skb = if_cs_receive_data(priv);
529                 if (skb)
530                         lbs_process_rxed_packet(priv, skb);
531         }
532
533         if (cause & IF_CS_BIT_TX) {
534                 lbs_deb_cs("tx done\n");
535                 lbs_host_to_card_done(priv);
536         }
537
538         if (cause & IF_CS_BIT_RESP) {
539                 unsigned long flags;
540                 u8 i;
541
542                 lbs_deb_cs("cmd resp\n");
543                 spin_lock_irqsave(&priv->driver_lock, flags);
544                 i = (priv->resp_idx == 0) ? 1 : 0;
545                 spin_unlock_irqrestore(&priv->driver_lock, flags);
546
547                 BUG_ON(priv->resp_len[i]);
548                 if_cs_receive_cmdres(priv, priv->resp_buf[i],
549                         &priv->resp_len[i]);
550
551                 spin_lock_irqsave(&priv->driver_lock, flags);
552                 lbs_notify_command_response(priv, i);
553                 spin_unlock_irqrestore(&priv->driver_lock, flags);
554         }
555
556         if (cause & IF_CS_BIT_EVENT) {
557                 u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
558                 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
559                         IF_CS_BIT_EVENT);
560                 lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
561         }
562
563         /* Clear interrupt cause */
564         if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
565
566         lbs_deb_leave(LBS_DEB_CS);
567         return IRQ_HANDLED;
568 }
569
570
571
572
573 /********************************************************************/
574 /* Firmware                                                         */
575 /********************************************************************/
576
577 /*
578  * Tries to program the helper firmware.
579  *
580  * Return 0 on success
581  */
582 static int if_cs_prog_helper(struct if_cs_card *card, const struct firmware *fw)
583 {
584         int ret = 0;
585         int sent = 0;
586         u8  scratch;
587
588         lbs_deb_enter(LBS_DEB_CS);
589
590         /*
591          * This is the only place where an unaligned register access happens on
592          * the CF8305 card, therefore for the sake of speed of the driver, we do
593          * the alignment correction here.
594          */
595         if (card->align_regs)
596                 scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
597         else
598                 scratch = if_cs_read8(card, IF_CS_SCRATCH);
599
600         /* "If the value is 0x5a, the firmware is already
601          * downloaded successfully"
602          */
603         if (scratch == IF_CS_SCRATCH_HELPER_OK)
604                 goto done;
605
606         /* "If the value is != 00, it is invalid value of register */
607         if (scratch != IF_CS_SCRATCH_BOOT_OK) {
608                 ret = -ENODEV;
609                 goto done;
610         }
611
612         lbs_deb_cs("helper size %td\n", fw->size);
613
614         /* "Set the 5 bytes of the helper image to 0" */
615         /* Not needed, this contains an ARM branch instruction */
616
617         for (;;) {
618                 /* "the number of bytes to send is 256" */
619                 int count = 256;
620                 int remain = fw->size - sent;
621
622                 if (remain < count)
623                         count = remain;
624
625                 /*
626                  * "write the number of bytes to be sent to the I/O Command
627                  * write length register"
628                  */
629                 if_cs_write16(card, IF_CS_CMD_LEN, count);
630
631                 /* "write this to I/O Command port register as 16 bit writes */
632                 if (count)
633                         if_cs_write16_rep(card, IF_CS_CMD,
634                                 &fw->data[sent],
635                                 count >> 1);
636
637                 /*
638                  * "Assert the download over interrupt command in the Host
639                  * status register"
640                  */
641                 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
642
643                 /*
644                  * "Assert the download over interrupt command in the Card
645                  * interrupt case register"
646                  */
647                 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
648
649                 /*
650                  * "The host polls the Card Status register ... for 50 ms before
651                  * declaring a failure"
652                  */
653                 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
654                         IF_CS_BIT_COMMAND);
655                 if (ret < 0) {
656                         pr_err("can't download helper at 0x%x, ret %d\n",
657                                sent, ret);
658                         goto done;
659                 }
660
661                 if (count == 0)
662                         break;
663
664                 sent += count;
665         }
666
667 done:
668         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
669         return ret;
670 }
671
672
673 static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
674 {
675         int ret = 0;
676         int retry = 0;
677         int len = 0;
678         int sent;
679
680         lbs_deb_enter(LBS_DEB_CS);
681
682         lbs_deb_cs("fw size %td\n", fw->size);
683
684         ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
685                 IF_CS_SQ_HELPER_OK);
686         if (ret < 0) {
687                 pr_err("helper firmware doesn't answer\n");
688                 goto done;
689         }
690
691         for (sent = 0; sent < fw->size; sent += len) {
692                 len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
693                 if (len & 1) {
694                         retry++;
695                         pr_info("odd, need to retry this firmware block\n");
696                 } else {
697                         retry = 0;
698                 }
699
700                 if (retry > 20) {
701                         pr_err("could not download firmware\n");
702                         ret = -ENODEV;
703                         goto done;
704                 }
705                 if (retry) {
706                         sent -= len;
707                 }
708
709
710                 if_cs_write16(card, IF_CS_CMD_LEN, len);
711
712                 if_cs_write16_rep(card, IF_CS_CMD,
713                         &fw->data[sent],
714                         (len+1) >> 1);
715                 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
716                 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
717
718                 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
719                         IF_CS_BIT_COMMAND);
720                 if (ret < 0) {
721                         pr_err("can't download firmware at 0x%x\n", sent);
722                         goto done;
723                 }
724         }
725
726         ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
727         if (ret < 0)
728                 pr_err("firmware download failed\n");
729
730 done:
731         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
732         return ret;
733 }
734
735
736
737 /********************************************************************/
738 /* Callback functions for libertas.ko                               */
739 /********************************************************************/
740
741 /* Send commands or data packets to the card */
742 static int if_cs_host_to_card(struct lbs_private *priv,
743         u8 type,
744         u8 *buf,
745         u16 nb)
746 {
747         int ret = -1;
748
749         lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
750
751         switch (type) {
752         case MVMS_DAT:
753                 priv->dnld_sent = DNLD_DATA_SENT;
754                 if_cs_send_data(priv, buf, nb);
755                 ret = 0;
756                 break;
757         case MVMS_CMD:
758                 priv->dnld_sent = DNLD_CMD_SENT;
759                 ret = if_cs_send_cmd(priv, buf, nb);
760                 break;
761         default:
762                 netdev_err(priv->dev, "%s: unsupported type %d\n",
763                            __func__, type);
764         }
765
766         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
767         return ret;
768 }
769
770
771 static void if_cs_release(struct pcmcia_device *p_dev)
772 {
773         struct if_cs_card *card = p_dev->priv;
774
775         lbs_deb_enter(LBS_DEB_CS);
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         lbs_deb_leave(LBS_DEB_CS);
783 }
784
785
786 static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
787 {
788         p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
789         p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
790
791         if (p_dev->resource[1]->end) {
792                 pr_err("wrong CIS (check number of IO windows)\n");
793                 return -ENODEV;
794         }
795
796         /* This reserves IO space but doesn't actually enable it */
797         return pcmcia_request_io(p_dev);
798 }
799
800 static int if_cs_probe(struct pcmcia_device *p_dev)
801 {
802         int ret = -ENOMEM;
803         unsigned int prod_id;
804         struct lbs_private *priv;
805         struct if_cs_card *card;
806         const struct firmware *helper = NULL;
807         const struct firmware *mainfw = NULL;
808
809         lbs_deb_enter(LBS_DEB_CS);
810
811         card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
812         if (!card)
813                 goto out;
814
815         card->p_dev = p_dev;
816         p_dev->priv = card;
817
818         p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
819
820         if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
821                 pr_err("error in pcmcia_loop_config\n");
822                 goto out1;
823         }
824
825         /*
826          * Allocate an interrupt line.  Note that this does not assign
827          * a handler to the interrupt, unless the 'Handler' member of
828          * the irq structure is initialized.
829          */
830         if (!p_dev->irq)
831                 goto out1;
832
833         /* Initialize io access */
834         card->iobase = ioport_map(p_dev->resource[0]->start,
835                                 resource_size(p_dev->resource[0]));
836         if (!card->iobase) {
837                 pr_err("error in ioport_map\n");
838                 ret = -EIO;
839                 goto out1;
840         }
841
842         ret = pcmcia_enable_device(p_dev);
843         if (ret) {
844                 pr_err("error in pcmcia_enable_device\n");
845                 goto out2;
846         }
847
848         /* Finally, report what we've done */
849         lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
850
851         /*
852          * Most of the libertas cards can do unaligned register access, but some
853          * weird ones cannot. That's especially true for the CF8305 card.
854          */
855         card->align_regs = false;
856
857         card->model = get_model(p_dev->manf_id, p_dev->card_id);
858         if (card->model == MODEL_UNKNOWN) {
859                 pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
860                        p_dev->manf_id, p_dev->card_id);
861                 goto out2;
862         }
863
864         /* Check if we have a current silicon */
865         prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
866         if (card->model == MODEL_8305) {
867                 card->align_regs = true;
868                 if (prod_id < IF_CS_CF8305_B1_REV) {
869                         pr_err("8305 rev B0 and older are not supported\n");
870                         ret = -ENODEV;
871                         goto out2;
872                 }
873         }
874
875         if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
876                 pr_err("8381 rev B2 and older are not supported\n");
877                 ret = -ENODEV;
878                 goto out2;
879         }
880
881         if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
882                 pr_err("8385 rev B0 and older are not supported\n");
883                 ret = -ENODEV;
884                 goto out2;
885         }
886
887         ret = lbs_get_firmware(&p_dev->dev, NULL, NULL, card->model,
888                                 &fw_table[0], &helper, &mainfw);
889         if (ret) {
890                 pr_err("failed to find firmware (%d)\n", ret);
891                 goto out2;
892         }
893
894         /* Load the firmware early, before calling into libertas.ko */
895         ret = if_cs_prog_helper(card, helper);
896         if (ret == 0 && (card->model != MODEL_8305))
897                 ret = if_cs_prog_real(card, mainfw);
898         if (ret)
899                 goto out2;
900
901         /* Make this card known to the libertas driver */
902         priv = lbs_add_card(card, &p_dev->dev);
903         if (!priv) {
904                 ret = -ENOMEM;
905                 goto out2;
906         }
907
908         /* Finish setting up fields in lbs_private */
909         card->priv = priv;
910         priv->card = card;
911         priv->hw_host_to_card = if_cs_host_to_card;
912         priv->enter_deep_sleep = NULL;
913         priv->exit_deep_sleep = NULL;
914         priv->reset_deep_sleep_wakeup = NULL;
915         priv->fw_ready = 1;
916
917         /* Now actually get the IRQ */
918         ret = request_irq(p_dev->irq, if_cs_interrupt,
919                 IRQF_SHARED, DRV_NAME, card);
920         if (ret) {
921                 pr_err("error in request_irq\n");
922                 goto out3;
923         }
924
925         /*
926          * Clear any interrupt cause that happened while sending
927          * firmware/initializing card
928          */
929         if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
930         if_cs_enable_ints(card);
931
932         /* And finally bring the card up */
933         if (lbs_start_card(priv) != 0) {
934                 pr_err("could not activate card\n");
935                 goto out3;
936         }
937
938         ret = 0;
939         goto out;
940
941 out3:
942         lbs_remove_card(priv);
943 out2:
944         ioport_unmap(card->iobase);
945 out1:
946         pcmcia_disable_device(p_dev);
947 out:
948         if (helper)
949                 release_firmware(helper);
950         if (mainfw)
951                 release_firmware(mainfw);
952
953         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
954         return ret;
955 }
956
957
958 static void if_cs_detach(struct pcmcia_device *p_dev)
959 {
960         struct if_cs_card *card = p_dev->priv;
961
962         lbs_deb_enter(LBS_DEB_CS);
963
964         lbs_stop_card(card->priv);
965         lbs_remove_card(card->priv);
966         if_cs_disable_ints(card);
967         if_cs_release(p_dev);
968         kfree(card);
969
970         lbs_deb_leave(LBS_DEB_CS);
971 }
972
973
974
975 /********************************************************************/
976 /* Module initialization                                            */
977 /********************************************************************/
978
979 static const struct pcmcia_device_id if_cs_ids[] = {
980         PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
981         PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
982         PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
983         /* NOTE: keep in sync with get_model() */
984         PCMCIA_DEVICE_NULL,
985 };
986 MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
987
988
989 static struct pcmcia_driver lbs_driver = {
990         .owner          = THIS_MODULE,
991         .name           = DRV_NAME,
992         .probe          = if_cs_probe,
993         .remove         = if_cs_detach,
994         .id_table       = if_cs_ids,
995 };
996
997
998 static int __init if_cs_init(void)
999 {
1000         int ret;
1001
1002         lbs_deb_enter(LBS_DEB_CS);
1003         ret = pcmcia_register_driver(&lbs_driver);
1004         lbs_deb_leave(LBS_DEB_CS);
1005         return ret;
1006 }
1007
1008
1009 static void __exit if_cs_exit(void)
1010 {
1011         lbs_deb_enter(LBS_DEB_CS);
1012         pcmcia_unregister_driver(&lbs_driver);
1013         lbs_deb_leave(LBS_DEB_CS);
1014 }
1015
1016
1017 module_init(if_cs_init);
1018 module_exit(if_cs_exit);