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