Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[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/cs.h>
32 #include <pcmcia/cistpl.h>
33 #include <pcmcia/ds.h>
34
35 #include <linux/io.h>
36
37 #define DRV_NAME "libertas_cs"
38
39 #include "decl.h"
40 #include "defs.h"
41 #include "dev.h"
42
43
44 /********************************************************************/
45 /* Module stuff                                                     */
46 /********************************************************************/
47
48 MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
49 MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
50 MODULE_LICENSE("GPL");
51
52
53
54 /********************************************************************/
55 /* Data structures                                                  */
56 /********************************************************************/
57
58 struct if_cs_card {
59         struct pcmcia_device *p_dev;
60         struct lbs_private *priv;
61         void __iomem *iobase;
62         bool align_regs;
63         u32 model;
64 };
65
66
67 enum {
68         MODEL_UNKNOWN = 0x00,
69         MODEL_8305 = 0x01,
70         MODEL_8381 = 0x02,
71         MODEL_8385 = 0x03
72 };
73
74 static const struct lbs_fw_table fw_table[] = {
75         { MODEL_8305, "libertas/cf8305.bin", NULL },
76         { MODEL_8305, "libertas_cs_helper.fw", NULL },
77         { MODEL_8381, "libertas/cf8381_helper.bin", "libertas/cf8381.bin" },
78         { MODEL_8381, "libertas_cs_helper.fw", "libertas_cs.fw" },
79         { MODEL_8385, "libertas/cf8385_helper.bin", "libertas/cf8385.bin" },
80         { MODEL_8385, "libertas_cs_helper.fw", "libertas_cs.fw" },
81         { 0, NULL, NULL }
82 };
83 MODULE_FIRMWARE("libertas/cf8305.bin");
84 MODULE_FIRMWARE("libertas/cf8381_helper.bin");
85 MODULE_FIRMWARE("libertas/cf8381.bin");
86 MODULE_FIRMWARE("libertas/cf8385_helper.bin");
87 MODULE_FIRMWARE("libertas/cf8385.bin");
88 MODULE_FIRMWARE("libertas_cs_helper.fw");
89 MODULE_FIRMWARE("libertas_cs.fw");
90
91
92 /********************************************************************/
93 /* Hardware access                                                  */
94 /********************************************************************/
95
96 /* This define enables wrapper functions which allow you
97    to dump all register accesses. You normally won't this,
98    except for development */
99 /* #define DEBUG_IO */
100
101 #ifdef DEBUG_IO
102 static int debug_output = 0;
103 #else
104 /* This way the compiler optimizes the printk's away */
105 #define debug_output 0
106 #endif
107
108 static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
109 {
110         unsigned int val = ioread8(card->iobase + reg);
111         if (debug_output)
112                 printk(KERN_INFO "inb %08x<%02x\n", reg, val);
113         return val;
114 }
115 static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
116 {
117         unsigned int val = ioread16(card->iobase + reg);
118         if (debug_output)
119                 printk(KERN_INFO "inw %08x<%04x\n", reg, val);
120         return val;
121 }
122 static inline void if_cs_read16_rep(
123         struct if_cs_card *card,
124         uint reg,
125         void *buf,
126         unsigned long count)
127 {
128         if (debug_output)
129                 printk(KERN_INFO "insw %08x<(0x%lx words)\n",
130                         reg, count);
131         ioread16_rep(card->iobase + reg, buf, count);
132 }
133
134 static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
135 {
136         if (debug_output)
137                 printk(KERN_INFO "outb %08x>%02x\n", reg, val);
138         iowrite8(val, card->iobase + reg);
139 }
140
141 static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
142 {
143         if (debug_output)
144                 printk(KERN_INFO "outw %08x>%04x\n", reg, val);
145         iowrite16(val, card->iobase + reg);
146 }
147
148 static inline void if_cs_write16_rep(
149         struct if_cs_card *card,
150         uint reg,
151         const void *buf,
152         unsigned long count)
153 {
154         if (debug_output)
155                 printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
156                         reg, count);
157         iowrite16_rep(card->iobase + reg, buf, count);
158 }
159
160
161 /*
162  * I know that polling/delaying is frowned upon. However, this procedure
163  * with polling is needed while downloading the firmware. At this stage,
164  * the hardware does unfortunately not create any interrupts.
165  *
166  * Fortunately, this function is never used once the firmware is in
167  * the card. :-)
168  *
169  * As a reference, see the "Firmware Specification v5.1", page 18
170  * and 19. I did not follow their suggested timing to the word,
171  * but this works nice & fast anyway.
172  */
173 static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
174 {
175         int i;
176
177         for (i = 0; i < 100000; i++) {
178                 u8 val = if_cs_read8(card, addr);
179                 if (val == reg)
180                         return 0;
181                 udelay(5);
182         }
183         return -ETIME;
184 }
185
186
187
188 /*
189  * First the bitmasks for the host/card interrupt/status registers:
190  */
191 #define IF_CS_BIT_TX                    0x0001
192 #define IF_CS_BIT_RX                    0x0002
193 #define IF_CS_BIT_COMMAND               0x0004
194 #define IF_CS_BIT_RESP                  0x0008
195 #define IF_CS_BIT_EVENT                 0x0010
196 #define IF_CS_BIT_MASK                  0x001f
197
198
199
200 /*
201  * It's not really clear to me what the host status register is for. It
202  * needs to be set almost in union with "host int cause". The following
203  * bits from above are used:
204  *
205  *   IF_CS_BIT_TX         driver downloaded a data packet
206  *   IF_CS_BIT_RX         driver got a data packet
207  *   IF_CS_BIT_COMMAND    driver downloaded a command
208  *   IF_CS_BIT_RESP       not used (has some meaning with powerdown)
209  *   IF_CS_BIT_EVENT      driver read a host event
210  */
211 #define IF_CS_HOST_STATUS               0x00000000
212
213 /*
214  * With the host int cause register can the host (that is, Linux) cause
215  * an interrupt in the firmware, to tell the firmware about those events:
216  *
217  *   IF_CS_BIT_TX         a data packet has been downloaded
218  *   IF_CS_BIT_RX         a received data packet has retrieved
219  *   IF_CS_BIT_COMMAND    a firmware block or a command has been downloaded
220  *   IF_CS_BIT_RESP       not used (has some meaning with powerdown)
221  *   IF_CS_BIT_EVENT      a host event (link lost etc) has been retrieved
222  */
223 #define IF_CS_HOST_INT_CAUSE            0x00000002
224
225 /*
226  * The host int mask register is used to enable/disable interrupt.  However,
227  * I have the suspicion that disabled interrupts are lost.
228  */
229 #define IF_CS_HOST_INT_MASK             0x00000004
230
231 /*
232  * Used to send or receive data packets:
233  */
234 #define IF_CS_WRITE                     0x00000016
235 #define IF_CS_WRITE_LEN                 0x00000014
236 #define IF_CS_READ                      0x00000010
237 #define IF_CS_READ_LEN                  0x00000024
238
239 /*
240  * Used to send commands (and to send firmware block) and to
241  * receive command responses:
242  */
243 #define IF_CS_CMD                       0x0000001A
244 #define IF_CS_CMD_LEN                   0x00000018
245 #define IF_CS_RESP                      0x00000012
246 #define IF_CS_RESP_LEN                  0x00000030
247
248 /*
249  * The card status registers shows what the card/firmware actually
250  * accepts:
251  *
252  *   IF_CS_BIT_TX        you may send a data packet
253  *   IF_CS_BIT_RX        you may retrieve a data packet
254  *   IF_CS_BIT_COMMAND   you may send a command
255  *   IF_CS_BIT_RESP      you may retrieve a command response
256  *   IF_CS_BIT_EVENT     the card has a event for use (link lost, snr low etc)
257  *
258  * When reading this register several times, you will get back the same
259  * results --- with one exception: the IF_CS_BIT_EVENT clear itself
260  * automatically.
261  *
262  * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
263  * we handle this via the card int cause register.
264  */
265 #define IF_CS_CARD_STATUS               0x00000020
266 #define IF_CS_CARD_STATUS_MASK          0x7f00
267
268 /*
269  * The card int cause register is used by the card/firmware to notify us
270  * about the following events:
271  *
272  *   IF_CS_BIT_TX        a data packet has successfully been sentx
273  *   IF_CS_BIT_RX        a data packet has been received and can be retrieved
274  *   IF_CS_BIT_COMMAND   not used
275  *   IF_CS_BIT_RESP      the firmware has a command response for us
276  *   IF_CS_BIT_EVENT     the card has a event for use (link lost, snr low etc)
277  */
278 #define IF_CS_CARD_INT_CAUSE            0x00000022
279
280 /*
281  * This is used to for handshaking with the card's bootloader/helper image
282  * to synchronize downloading of firmware blocks.
283  */
284 #define IF_CS_SQ_READ_LOW               0x00000028
285 #define IF_CS_SQ_HELPER_OK              0x10
286
287 /*
288  * The scratch register tells us ...
289  *
290  * IF_CS_SCRATCH_BOOT_OK     the bootloader runs
291  * IF_CS_SCRATCH_HELPER_OK   the helper firmware already runs
292  */
293 #define IF_CS_SCRATCH                   0x0000003F
294 #define IF_CS_SCRATCH_BOOT_OK           0x00
295 #define IF_CS_SCRATCH_HELPER_OK         0x5a
296
297 /*
298  * Used to detect ancient chips:
299  */
300 #define IF_CS_PRODUCT_ID                0x0000001C
301 #define IF_CS_CF8385_B1_REV             0x12
302 #define IF_CS_CF8381_B3_REV             0x04
303 #define IF_CS_CF8305_B1_REV             0x03
304
305 /*
306  * Used to detect other cards than CF8385 since their revisions of silicon
307  * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
308  */
309 #define CF8305_MANFID           0x02db
310 #define CF8305_CARDID           0x8103
311 #define CF8381_MANFID           0x02db
312 #define CF8381_CARDID           0x6064
313 #define CF8385_MANFID           0x02df
314 #define CF8385_CARDID           0x8103
315
316 /* FIXME: just use the 'driver_info' field of 'struct pcmcia_device_id' when
317  * that gets fixed.  Currently there's no way to access it from the probe hook.
318  */
319 static inline u32 get_model(u16 manf_id, u16 card_id)
320 {
321         /* NOTE: keep in sync with if_cs_ids */
322         if (manf_id == CF8305_MANFID && card_id == CF8305_CARDID)
323                 return MODEL_8305;
324         else if (manf_id == CF8381_MANFID && card_id == CF8381_CARDID)
325                 return MODEL_8381;
326         else if (manf_id == CF8385_MANFID && card_id == CF8385_CARDID)
327                 return MODEL_8385;
328         return MODEL_UNKNOWN;
329 }
330
331 /********************************************************************/
332 /* I/O and interrupt handling                                       */
333 /********************************************************************/
334
335 static inline void if_cs_enable_ints(struct if_cs_card *card)
336 {
337         lbs_deb_enter(LBS_DEB_CS);
338         if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
339 }
340
341 static inline void if_cs_disable_ints(struct if_cs_card *card)
342 {
343         lbs_deb_enter(LBS_DEB_CS);
344         if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
345 }
346
347 /*
348  * Called from if_cs_host_to_card to send a command to the hardware
349  */
350 static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
351 {
352         struct if_cs_card *card = (struct if_cs_card *)priv->card;
353         int ret = -1;
354         int loops = 0;
355
356         lbs_deb_enter(LBS_DEB_CS);
357         if_cs_disable_ints(card);
358
359         /* Is hardware ready? */
360         while (1) {
361                 u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
362                 if (status & IF_CS_BIT_COMMAND)
363                         break;
364                 if (++loops > 100) {
365                         lbs_pr_err("card not ready for commands\n");
366                         goto done;
367                 }
368                 mdelay(1);
369         }
370
371         if_cs_write16(card, IF_CS_CMD_LEN, nb);
372
373         if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
374         /* Are we supposed to transfer an odd amount of bytes? */
375         if (nb & 1)
376                 if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
377
378         /* "Assert the download over interrupt command in the Host
379          * status register" */
380         if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
381
382         /* "Assert the download over interrupt command in the Card
383          * interrupt case register" */
384         if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
385         ret = 0;
386
387 done:
388         if_cs_enable_ints(card);
389         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
390         return ret;
391 }
392
393 /*
394  * Called from if_cs_host_to_card to send a data to the hardware
395  */
396 static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
397 {
398         struct if_cs_card *card = (struct if_cs_card *)priv->card;
399         u16 status;
400
401         lbs_deb_enter(LBS_DEB_CS);
402         if_cs_disable_ints(card);
403
404         status = if_cs_read16(card, IF_CS_CARD_STATUS);
405         BUG_ON((status & IF_CS_BIT_TX) == 0);
406
407         if_cs_write16(card, IF_CS_WRITE_LEN, nb);
408
409         /* write even number of bytes, then odd byte if necessary */
410         if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
411         if (nb & 1)
412                 if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
413
414         if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
415         if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
416         if_cs_enable_ints(card);
417
418         lbs_deb_leave(LBS_DEB_CS);
419 }
420
421 /*
422  * Get the command result out of the card.
423  */
424 static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
425 {
426         unsigned long flags;
427         int ret = -1;
428         u16 status;
429
430         lbs_deb_enter(LBS_DEB_CS);
431
432         /* is hardware ready? */
433         status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
434         if ((status & IF_CS_BIT_RESP) == 0) {
435                 lbs_pr_err("no cmd response in card\n");
436                 *len = 0;
437                 goto out;
438         }
439
440         *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
441         if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
442                 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
443                 goto out;
444         }
445
446         /* read even number of bytes, then odd byte if necessary */
447         if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
448         if (*len & 1)
449                 data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
450
451         /* This is a workaround for a firmware that reports too much
452          * bytes */
453         *len -= 8;
454         ret = 0;
455
456         /* Clear this flag again */
457         spin_lock_irqsave(&priv->driver_lock, flags);
458         priv->dnld_sent = DNLD_RES_RECEIVED;
459         spin_unlock_irqrestore(&priv->driver_lock, flags);
460
461 out:
462         lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
463         return ret;
464 }
465
466 static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
467 {
468         struct sk_buff *skb = NULL;
469         u16 len;
470         u8 *data;
471
472         lbs_deb_enter(LBS_DEB_CS);
473
474         len = if_cs_read16(priv->card, IF_CS_READ_LEN);
475         if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
476                 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", 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                 /* "write the number of bytes to be sent to the I/O Command
626                  * write length register" */
627                 if_cs_write16(card, IF_CS_CMD_LEN, count);
628
629                 /* "write this to I/O Command port register as 16 bit writes */
630                 if (count)
631                         if_cs_write16_rep(card, IF_CS_CMD,
632                                 &fw->data[sent],
633                                 count >> 1);
634
635                 /* "Assert the download over interrupt command in the Host
636                  * status register" */
637                 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
638
639                 /* "Assert the download over interrupt command in the Card
640                  * interrupt case register" */
641                 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
642
643                 /* "The host polls the Card Status register ... for 50 ms before
644                    declaring a failure */
645                 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
646                         IF_CS_BIT_COMMAND);
647                 if (ret < 0) {
648                         lbs_pr_err("can't download helper at 0x%x, ret %d\n",
649                                 sent, ret);
650                         goto done;
651                 }
652
653                 if (count == 0)
654                         break;
655
656                 sent += count;
657         }
658
659 done:
660         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
661         return ret;
662 }
663
664
665 static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
666 {
667         int ret = 0;
668         int retry = 0;
669         int len = 0;
670         int sent;
671
672         lbs_deb_enter(LBS_DEB_CS);
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 done;
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 done;
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 done;
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 done:
723         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
724         return ret;
725 }
726
727
728
729 /********************************************************************/
730 /* Callback functions for libertas.ko                               */
731 /********************************************************************/
732
733 /* Send commands or data packets to the card */
734 static int if_cs_host_to_card(struct lbs_private *priv,
735         u8 type,
736         u8 *buf,
737         u16 nb)
738 {
739         int ret = -1;
740
741         lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
742
743         switch (type) {
744         case MVMS_DAT:
745                 priv->dnld_sent = DNLD_DATA_SENT;
746                 if_cs_send_data(priv, buf, nb);
747                 ret = 0;
748                 break;
749         case MVMS_CMD:
750                 priv->dnld_sent = DNLD_CMD_SENT;
751                 ret = if_cs_send_cmd(priv, buf, nb);
752                 break;
753         default:
754                 lbs_pr_err("%s: unsupported type %d\n", __func__, type);
755         }
756
757         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
758         return ret;
759 }
760
761
762 /********************************************************************/
763 /* Card Services                                                    */
764 /********************************************************************/
765
766 /*
767  * After a card is removed, if_cs_release() will unregister the
768  * device, and release the PCMCIA configuration.  If the device is
769  * still open, this will be postponed until it is closed.
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 /*
787  * This creates an "instance" of the driver, allocating local data
788  * structures for one device.  The device is registered with Card
789  * Services.
790  *
791  * The dev_link structure is initialized, but we don't actually
792  * configure the card at this point -- we wait until we receive a card
793  * insertion event.
794  */
795
796 static int if_cs_ioprobe(struct pcmcia_device *p_dev,
797                          cistpl_cftable_entry_t *cfg,
798                          cistpl_cftable_entry_t *dflt,
799                          unsigned int vcc,
800                          void *priv_data)
801 {
802         p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
803         p_dev->resource[0]->start = cfg->io.win[0].base;
804         p_dev->resource[0]->end = cfg->io.win[0].len;
805
806         /* Do we need to allocate an interrupt? */
807         p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
808
809         /* IO window settings */
810         if (cfg->io.nwin != 1) {
811                 lbs_pr_err("wrong CIS (check number of IO windows)\n");
812                 return -ENODEV;
813         }
814
815         /* This reserves IO space but doesn't actually enable it */
816         return pcmcia_request_io(p_dev);
817 }
818
819 static int if_cs_probe(struct pcmcia_device *p_dev)
820 {
821         int ret = -ENOMEM;
822         unsigned int prod_id;
823         struct lbs_private *priv;
824         struct if_cs_card *card;
825         const struct firmware *helper = NULL;
826         const struct firmware *mainfw = NULL;
827
828         lbs_deb_enter(LBS_DEB_CS);
829
830         card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
831         if (!card) {
832                 lbs_pr_err("error in kzalloc\n");
833                 goto out;
834         }
835         card->p_dev = p_dev;
836         p_dev->priv = card;
837
838         p_dev->conf.Attributes = 0;
839         p_dev->conf.IntType = INT_MEMORY_AND_IO;
840
841         if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
842                 lbs_pr_err("error in pcmcia_loop_config\n");
843                 goto out1;
844         }
845
846         /*
847          * Allocate an interrupt line.  Note that this does not assign
848          * a handler to the interrupt, unless the 'Handler' member of
849          * the irq structure is initialized.
850          */
851         if (!p_dev->irq)
852                 goto out1;
853
854         /* Initialize io access */
855         card->iobase = ioport_map(p_dev->resource[0]->start,
856                                 resource_size(p_dev->resource[0]));
857         if (!card->iobase) {
858                 lbs_pr_err("error in ioport_map\n");
859                 ret = -EIO;
860                 goto out1;
861         }
862
863         /*
864          * This actually configures the PCMCIA socket -- setting up
865          * the I/O windows and the interrupt mapping, and putting the
866          * card and host interface into "Memory and IO" mode.
867          */
868         ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
869         if (ret) {
870                 lbs_pr_err("error in pcmcia_request_configuration\n");
871                 goto out2;
872         }
873
874         /* Finally, report what we've done */
875         lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
876
877         /*
878          * Most of the libertas cards can do unaligned register access, but some
879          * weird ones can not. That's especially true for the CF8305 card.
880          */
881         card->align_regs = 0;
882
883         card->model = get_model(p_dev->manf_id, p_dev->card_id);
884         if (card->model == MODEL_UNKNOWN) {
885                 lbs_pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
886                            p_dev->manf_id, p_dev->card_id);
887                 goto out2;
888         }
889
890         /* Check if we have a current silicon */
891         prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
892         if (card->model == MODEL_8305) {
893                 card->align_regs = 1;
894                 if (prod_id < IF_CS_CF8305_B1_REV) {
895                         lbs_pr_err("8305 rev B0 and older are not supported\n");
896                         ret = -ENODEV;
897                         goto out2;
898                 }
899         }
900
901         if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
902                 lbs_pr_err("8381 rev B2 and older are not supported\n");
903                 ret = -ENODEV;
904                 goto out2;
905         }
906
907         if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
908                 lbs_pr_err("8385 rev B0 and older are not supported\n");
909                 ret = -ENODEV;
910                 goto out2;
911         }
912
913         ret = lbs_get_firmware(&p_dev->dev, NULL, NULL, card->model,
914                                 &fw_table[0], &helper, &mainfw);
915         if (ret) {
916                 lbs_pr_err("failed to find firmware (%d)\n", ret);
917                 goto out2;
918         }
919
920         /* Load the firmware early, before calling into libertas.ko */
921         ret = if_cs_prog_helper(card, helper);
922         if (ret == 0 && (card->model != MODEL_8305))
923                 ret = if_cs_prog_real(card, mainfw);
924         if (ret)
925                 goto out2;
926
927         /* Make this card known to the libertas driver */
928         priv = lbs_add_card(card, &p_dev->dev);
929         if (!priv) {
930                 ret = -ENOMEM;
931                 goto out2;
932         }
933
934         /* Finish setting up fields in lbs_private */
935         card->priv = priv;
936         priv->card = card;
937         priv->hw_host_to_card = if_cs_host_to_card;
938         priv->enter_deep_sleep = NULL;
939         priv->exit_deep_sleep = NULL;
940         priv->reset_deep_sleep_wakeup = NULL;
941         priv->fw_ready = 1;
942
943         /* Now actually get the IRQ */
944         ret = request_irq(p_dev->irq, if_cs_interrupt,
945                 IRQF_SHARED, DRV_NAME, card);
946         if (ret) {
947                 lbs_pr_err("error in request_irq\n");
948                 goto out3;
949         }
950
951         /* Clear any interrupt cause that happend while sending
952          * firmware/initializing card */
953         if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
954         if_cs_enable_ints(card);
955
956         /* And finally bring the card up */
957         if (lbs_start_card(priv) != 0) {
958                 lbs_pr_err("could not activate card\n");
959                 goto out3;
960         }
961
962         ret = 0;
963         goto out;
964
965 out3:
966         lbs_remove_card(priv);
967 out2:
968         ioport_unmap(card->iobase);
969 out1:
970         pcmcia_disable_device(p_dev);
971 out:
972         if (helper)
973                 release_firmware(helper);
974         if (mainfw)
975                 release_firmware(mainfw);
976
977         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
978         return ret;
979 }
980
981
982 /*
983  * This deletes a driver "instance".  The device is de-registered with
984  * Card Services.  If it has been released, all local data structures
985  * are freed.  Otherwise, the structures will be freed when the device
986  * is released.
987  */
988 static void if_cs_detach(struct pcmcia_device *p_dev)
989 {
990         struct if_cs_card *card = p_dev->priv;
991
992         lbs_deb_enter(LBS_DEB_CS);
993
994         lbs_stop_card(card->priv);
995         lbs_remove_card(card->priv);
996         if_cs_disable_ints(card);
997         if_cs_release(p_dev);
998         kfree(card);
999
1000         lbs_deb_leave(LBS_DEB_CS);
1001 }
1002
1003
1004
1005 /********************************************************************/
1006 /* Module initialization                                            */
1007 /********************************************************************/
1008
1009 static struct pcmcia_device_id if_cs_ids[] = {
1010         PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
1011         PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
1012         PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
1013         /* NOTE: keep in sync with get_model() */
1014         PCMCIA_DEVICE_NULL,
1015 };
1016 MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
1017
1018
1019 static struct pcmcia_driver lbs_driver = {
1020         .owner          = THIS_MODULE,
1021         .drv            = {
1022                 .name   = DRV_NAME,
1023         },
1024         .probe          = if_cs_probe,
1025         .remove         = if_cs_detach,
1026         .id_table       = if_cs_ids,
1027 };
1028
1029
1030 static int __init if_cs_init(void)
1031 {
1032         int ret;
1033
1034         lbs_deb_enter(LBS_DEB_CS);
1035         ret = pcmcia_register_driver(&lbs_driver);
1036         lbs_deb_leave(LBS_DEB_CS);
1037         return ret;
1038 }
1039
1040
1041 static void __exit if_cs_exit(void)
1042 {
1043         lbs_deb_enter(LBS_DEB_CS);
1044         pcmcia_unregister_driver(&lbs_driver);
1045         lbs_deb_leave(LBS_DEB_CS);
1046 }
1047
1048
1049 module_init(if_cs_init);
1050 module_exit(if_cs_exit);