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