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