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