blob: 49a01780dd03296b036086e09c20fe251bb368a4 [file] [log] [blame]
Holger Schurig27590d02007-10-03 17:25:41 -07001/*
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;
Holger Schurig69f90322007-11-23 15:43:44 +010060 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -070061 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)
86 printk(KERN_INFO "##inb %08x<%02x\n", reg, val);
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)
93 printk(KERN_INFO "##inw %08x<%04x\n", reg, val);
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)
103 printk(KERN_INFO "##insw %08x<(0x%lx words)\n",
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)
111 printk(KERN_INFO "##outb %08x>%02x\n", reg, val);
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)
118 printk(KERN_INFO "##outw %08x>%04x\n", reg, val);
119 iowrite16(val, card->iobase + reg);
120}
121
122static inline void if_cs_write16_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 "##outsw %08x>(0x%lx words)\n",
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
Holger Schurig4ef31702007-10-09 10:41:57 +0200151 for (i = 0; i < 1000; i++) {
Holger Schurig27590d02007-10-03 17:25:41 -0700152 u8 val = if_cs_read8(card, addr);
153 if (val == reg)
154 return i;
Holger Schurig4ef31702007-10-09 10:41:57 +0200155 udelay(500);
Holger Schurig27590d02007-10-03 17:25:41 -0700156 }
157 return -ETIME;
158}
159
160
161
162/* Host control registers and their bit definitions */
163
164#define IF_CS_H_STATUS 0x00000000
165#define IF_CS_H_STATUS_TX_OVER 0x0001
166#define IF_CS_H_STATUS_RX_OVER 0x0002
167#define IF_CS_H_STATUS_DNLD_OVER 0x0004
168
169#define IF_CS_H_INT_CAUSE 0x00000002
170#define IF_CS_H_IC_TX_OVER 0x0001
171#define IF_CS_H_IC_RX_OVER 0x0002
172#define IF_CS_H_IC_DNLD_OVER 0x0004
Holger Schurig6591e362007-11-26 09:35:44 +0100173#define IF_CS_H_IC_POWER_DOWN 0x0008
174#define IF_CS_H_IC_HOST_EVENT 0x0010
Holger Schurig27590d02007-10-03 17:25:41 -0700175#define IF_CS_H_IC_MASK 0x001f
176
177#define IF_CS_H_INT_MASK 0x00000004
178#define IF_CS_H_IM_MASK 0x001f
179
180#define IF_CS_H_WRITE_LEN 0x00000014
181
182#define IF_CS_H_WRITE 0x00000016
183
184#define IF_CS_H_CMD_LEN 0x00000018
185
186#define IF_CS_H_CMD 0x0000001A
187
188#define IF_CS_C_READ_LEN 0x00000024
189
190#define IF_CS_H_READ 0x00000010
191
192/* Card control registers and their bit definitions */
193
194#define IF_CS_C_STATUS 0x00000020
195#define IF_CS_C_S_TX_DNLD_RDY 0x0001
196#define IF_CS_C_S_RX_UPLD_RDY 0x0002
197#define IF_CS_C_S_CMD_DNLD_RDY 0x0004
198#define IF_CS_C_S_CMD_UPLD_RDY 0x0008
199#define IF_CS_C_S_CARDEVENT 0x0010
200#define IF_CS_C_S_MASK 0x001f
201#define IF_CS_C_S_STATUS_MASK 0x7f00
202/* The following definitions should be the same as the MRVDRV_ ones */
203
204#if MRVDRV_CMD_DNLD_RDY != IF_CS_C_S_CMD_DNLD_RDY
205#error MRVDRV_CMD_DNLD_RDY and IF_CS_C_S_CMD_DNLD_RDY not in sync
206#endif
207#if MRVDRV_CMD_UPLD_RDY != IF_CS_C_S_CMD_UPLD_RDY
208#error MRVDRV_CMD_UPLD_RDY and IF_CS_C_S_CMD_UPLD_RDY not in sync
209#endif
210#if MRVDRV_CARDEVENT != IF_CS_C_S_CARDEVENT
211#error MRVDRV_CARDEVENT and IF_CS_C_S_CARDEVENT not in sync
212#endif
213
214#define IF_CS_C_INT_CAUSE 0x00000022
215#define IF_CS_C_IC_MASK 0x001f
216
217#define IF_CS_C_SQ_READ_LOW 0x00000028
218#define IF_CS_C_SQ_HELPER_OK 0x10
219
220#define IF_CS_C_CMD_LEN 0x00000030
221
222#define IF_CS_C_CMD 0x00000012
223
224#define IF_CS_SCRATCH 0x0000003F
225
226
227
228/********************************************************************/
229/* Interrupts */
230/********************************************************************/
231
232static inline void if_cs_enable_ints(struct if_cs_card *card)
233{
234 lbs_deb_enter(LBS_DEB_CS);
235 if_cs_write16(card, IF_CS_H_INT_MASK, 0);
236}
237
238static inline void if_cs_disable_ints(struct if_cs_card *card)
239{
240 lbs_deb_enter(LBS_DEB_CS);
241 if_cs_write16(card, IF_CS_H_INT_MASK, IF_CS_H_IM_MASK);
242}
243
244static irqreturn_t if_cs_interrupt(int irq, void *data)
245{
Jeff Garzik28fc1f52007-10-29 05:46:16 -0400246 struct if_cs_card *card = data;
Holger Schurig27590d02007-10-03 17:25:41 -0700247 u16 int_cause;
248
249 lbs_deb_enter(LBS_DEB_CS);
250
251 int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400252 if(int_cause == 0x0) {
253 /* Not for us */
Holger Schurig27590d02007-10-03 17:25:41 -0700254 return IRQ_NONE;
Ryan Mallon28de0b32007-09-06 21:32:42 -0400255
David Woodhousee775ed72007-12-06 14:36:11 +0000256 } else if (int_cause == 0xffff) {
Ryan Mallon28de0b32007-09-06 21:32:42 -0400257 /* Read in junk, the card has probably been removed */
David Woodhouseaa21c002007-12-08 20:04:36 +0000258 card->priv->surpriseremoved = 1;
Ryan Mallon28de0b32007-09-06 21:32:42 -0400259
260 } else {
David Woodhousee775ed72007-12-06 14:36:11 +0000261 if (int_cause & IF_CS_H_IC_TX_OVER)
262 lbs_host_to_card_done(card->priv);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400263
Holger Schurig27590d02007-10-03 17:25:41 -0700264 /* clear interrupt */
265 if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause & IF_CS_C_IC_MASK);
Holger Schurig27590d02007-10-03 17:25:41 -0700266 }
267
Holger Schurig10078322007-11-15 18:05:47 -0500268 lbs_interrupt(card->priv->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700269
270 return IRQ_HANDLED;
271}
272
273
274
275
276/********************************************************************/
277/* I/O */
278/********************************************************************/
279
280/*
281 * Called from if_cs_host_to_card to send a command to the hardware
282 */
Holger Schurig69f90322007-11-23 15:43:44 +0100283static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700284{
285 struct if_cs_card *card = (struct if_cs_card *)priv->card;
286 int ret = -1;
287 int loops = 0;
288
289 lbs_deb_enter(LBS_DEB_CS);
290
291 /* Is hardware ready? */
292 while (1) {
293 u16 val = if_cs_read16(card, IF_CS_C_STATUS);
294 if (val & IF_CS_C_S_CMD_DNLD_RDY)
295 break;
296 if (++loops > 100) {
297 lbs_pr_err("card not ready for commands\n");
298 goto done;
299 }
300 mdelay(1);
301 }
302
303 if_cs_write16(card, IF_CS_H_CMD_LEN, nb);
304
305 if_cs_write16_rep(card, IF_CS_H_CMD, buf, nb / 2);
306 /* Are we supposed to transfer an odd amount of bytes? */
307 if (nb & 1)
308 if_cs_write8(card, IF_CS_H_CMD, buf[nb-1]);
309
310 /* "Assert the download over interrupt command in the Host
311 * status register" */
312 if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
313
314 /* "Assert the download over interrupt command in the Card
315 * interrupt case register" */
316 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
317 ret = 0;
318
319done:
320 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
321 return ret;
322}
323
324
325/*
326 * Called from if_cs_host_to_card to send a data to the hardware
327 */
Holger Schurig69f90322007-11-23 15:43:44 +0100328static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700329{
330 struct if_cs_card *card = (struct if_cs_card *)priv->card;
331
332 lbs_deb_enter(LBS_DEB_CS);
333
334 if_cs_write16(card, IF_CS_H_WRITE_LEN, nb);
335
336 /* write even number of bytes, then odd byte if necessary */
337 if_cs_write16_rep(card, IF_CS_H_WRITE, buf, nb / 2);
338 if (nb & 1)
339 if_cs_write8(card, IF_CS_H_WRITE, buf[nb-1]);
340
341 if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_TX_OVER);
342 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_STATUS_TX_OVER);
343
344 lbs_deb_leave(LBS_DEB_CS);
345}
346
347
348/*
349 * Get the command result out of the card.
350 */
Holger Schurig69f90322007-11-23 15:43:44 +0100351static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
Holger Schurig27590d02007-10-03 17:25:41 -0700352{
353 int ret = -1;
354 u16 val;
355
356 lbs_deb_enter(LBS_DEB_CS);
357
358 /* is hardware ready? */
359 val = if_cs_read16(priv->card, IF_CS_C_STATUS);
360 if ((val & IF_CS_C_S_CMD_UPLD_RDY) == 0) {
361 lbs_pr_err("card not ready for CMD\n");
362 goto out;
363 }
364
365 *len = if_cs_read16(priv->card, IF_CS_C_CMD_LEN);
366 if ((*len == 0) || (*len > MRVDRV_SIZE_OF_CMD_BUFFER)) {
367 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
368 goto out;
369 }
370
371 /* read even number of bytes, then odd byte if necessary */
372 if_cs_read16_rep(priv->card, IF_CS_C_CMD, data, *len/sizeof(u16));
373 if (*len & 1)
374 data[*len-1] = if_cs_read8(priv->card, IF_CS_C_CMD);
375
Holger Schurig09d4fad2007-12-06 13:50:30 +0100376 /* This is a workaround for a firmware that reports too much
377 * bytes */
378 *len -= 8;
Holger Schurig27590d02007-10-03 17:25:41 -0700379 ret = 0;
380out:
381 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
382 return ret;
383}
384
385
Holger Schurig69f90322007-11-23 15:43:44 +0100386static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
Holger Schurig27590d02007-10-03 17:25:41 -0700387{
388 struct sk_buff *skb = NULL;
389 u16 len;
390 u8 *data;
391
392 lbs_deb_enter(LBS_DEB_CS);
393
394 len = if_cs_read16(priv->card, IF_CS_C_READ_LEN);
395 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
396 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
397 priv->stats.rx_dropped++;
398 printk(KERN_INFO "##HS %s:%d TODO\n", __FUNCTION__, __LINE__);
399 goto dat_err;
400 }
401
402 //TODO: skb = dev_alloc_skb(len+ETH_FRAME_LEN+MRVDRV_SNAP_HEADER_LEN+EXTRA_LEN);
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400403 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700404 if (!skb)
405 goto out;
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400406 skb_put(skb, len);
407 skb_reserve(skb, 2);/* 16 byte align */
408 data = skb->data;
Holger Schurig27590d02007-10-03 17:25:41 -0700409
410 /* read even number of bytes, then odd byte if necessary */
411 if_cs_read16_rep(priv->card, IF_CS_H_READ, data, len/sizeof(u16));
412 if (len & 1)
413 data[len-1] = if_cs_read8(priv->card, IF_CS_H_READ);
414
415dat_err:
416 if_cs_write16(priv->card, IF_CS_H_STATUS, IF_CS_H_STATUS_RX_OVER);
417 if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_RX_OVER);
418
419out:
420 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
421 return skb;
422}
423
424
425
426/********************************************************************/
427/* Firmware */
428/********************************************************************/
429
430/*
431 * Tries to program the helper firmware.
432 *
433 * Return 0 on success
434 */
435static int if_cs_prog_helper(struct if_cs_card *card)
436{
437 int ret = 0;
438 int sent = 0;
439 u8 scratch;
440 const struct firmware *fw;
441
442 lbs_deb_enter(LBS_DEB_CS);
443
444 scratch = if_cs_read8(card, IF_CS_SCRATCH);
445
446 /* "If the value is 0x5a, the firmware is already
447 * downloaded successfully"
448 */
449 if (scratch == 0x5a)
450 goto done;
451
452 /* "If the value is != 00, it is invalid value of register */
453 if (scratch != 0x00) {
454 ret = -ENODEV;
455 goto done;
456 }
457
458 /* TODO: make firmware file configurable */
459 ret = request_firmware(&fw, "libertas_cs_helper.fw",
460 &handle_to_dev(card->p_dev));
461 if (ret) {
462 lbs_pr_err("can't load helper firmware\n");
463 ret = -ENODEV;
464 goto done;
465 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700466 lbs_deb_cs("helper size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700467
468 /* "Set the 5 bytes of the helper image to 0" */
469 /* Not needed, this contains an ARM branch instruction */
470
471 for (;;) {
472 /* "the number of bytes to send is 256" */
473 int count = 256;
474 int remain = fw->size - sent;
475
476 if (remain < count)
477 count = remain;
478 /* printk(KERN_INFO "//HS %d loading %d of %d bytes\n",
479 __LINE__, sent, fw->size); */
480
481 /* "write the number of bytes to be sent to the I/O Command
482 * write length register" */
483 if_cs_write16(card, IF_CS_H_CMD_LEN, count);
484
485 /* "write this to I/O Command port register as 16 bit writes */
486 if (count)
487 if_cs_write16_rep(card, IF_CS_H_CMD,
488 &fw->data[sent],
489 count >> 1);
490
491 /* "Assert the download over interrupt command in the Host
492 * status register" */
493 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
494
495 /* "Assert the download over interrupt command in the Card
496 * interrupt case register" */
497 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
498
499 /* "The host polls the Card Status register ... for 50 ms before
500 declaring a failure */
501 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
502 IF_CS_C_S_CMD_DNLD_RDY);
503 if (ret < 0) {
504 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
505 sent, ret);
506 goto done;
507 }
508
509 if (count == 0)
510 break;
511
512 sent += count;
513 }
514
515 release_firmware(fw);
516 ret = 0;
517
518done:
519 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
520 return ret;
521}
522
523
524static int if_cs_prog_real(struct if_cs_card *card)
525{
526 const struct firmware *fw;
527 int ret = 0;
528 int retry = 0;
529 int len = 0;
530 int sent;
531
532 lbs_deb_enter(LBS_DEB_CS);
533
534 /* TODO: make firmware file configurable */
535 ret = request_firmware(&fw, "libertas_cs.fw",
536 &handle_to_dev(card->p_dev));
537 if (ret) {
538 lbs_pr_err("can't load firmware\n");
539 ret = -ENODEV;
540 goto done;
541 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700542 lbs_deb_cs("fw size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700543
544 ret = if_cs_poll_while_fw_download(card, IF_CS_C_SQ_READ_LOW, IF_CS_C_SQ_HELPER_OK);
545 if (ret < 0) {
546 int i;
547 lbs_pr_err("helper firmware doesn't answer\n");
548 for (i = 0; i < 0x50; i += 2)
549 printk(KERN_INFO "## HS %02x: %04x\n",
550 i, if_cs_read16(card, i));
551 goto err_release;
552 }
553
554 for (sent = 0; sent < fw->size; sent += len) {
555 len = if_cs_read16(card, IF_CS_C_SQ_READ_LOW);
556 /* printk(KERN_INFO "//HS %d loading %d of %d bytes\n",
557 __LINE__, sent, fw->size); */
558 if (len & 1) {
559 retry++;
560 lbs_pr_info("odd, need to retry this firmware block\n");
561 } else {
562 retry = 0;
563 }
564
565 if (retry > 20) {
566 lbs_pr_err("could not download firmware\n");
567 ret = -ENODEV;
568 goto err_release;
569 }
570 if (retry) {
571 sent -= len;
572 }
573
574
575 if_cs_write16(card, IF_CS_H_CMD_LEN, len);
576
577 if_cs_write16_rep(card, IF_CS_H_CMD,
578 &fw->data[sent],
579 (len+1) >> 1);
580 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
581 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
582
583 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
584 IF_CS_C_S_CMD_DNLD_RDY);
585 if (ret < 0) {
586 lbs_pr_err("can't download firmware at 0x%x\n", sent);
587 goto err_release;
588 }
589 }
590
591 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
592 if (ret < 0) {
593 lbs_pr_err("firmware download failed\n");
594 goto err_release;
595 }
596
597 ret = 0;
598 goto done;
599
600
601err_release:
602 release_firmware(fw);
603
604done:
605 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
606 return ret;
607}
608
609
610
611/********************************************************************/
612/* Callback functions for libertas.ko */
613/********************************************************************/
614
Holger Schurig27590d02007-10-03 17:25:41 -0700615/* Send commands or data packets to the card */
Holger Schurig69f90322007-11-23 15:43:44 +0100616static int if_cs_host_to_card(struct lbs_private *priv,
617 u8 type,
618 u8 *buf,
619 u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700620{
621 int ret = -1;
622
623 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
624
625 switch (type) {
626 case MVMS_DAT:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400627 priv->dnld_sent = DNLD_DATA_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700628 if_cs_send_data(priv, buf, nb);
629 ret = 0;
630 break;
631 case MVMS_CMD:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400632 priv->dnld_sent = DNLD_CMD_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700633 ret = if_cs_send_cmd(priv, buf, nb);
634 break;
635 default:
636 lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
637 }
638
639 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
640 return ret;
641}
642
643
Holger Schurig69f90322007-11-23 15:43:44 +0100644static int if_cs_get_int_status(struct lbs_private *priv, u8 *ireg)
Holger Schurig27590d02007-10-03 17:25:41 -0700645{
646 struct if_cs_card *card = (struct if_cs_card *)priv->card;
Holger Schurig27590d02007-10-03 17:25:41 -0700647 int ret = 0;
648 u16 int_cause;
649 u8 *cmdbuf;
650 *ireg = 0;
651
652 lbs_deb_enter(LBS_DEB_CS);
653
David Woodhouseaa21c002007-12-08 20:04:36 +0000654 if (priv->surpriseremoved)
Holger Schurig27590d02007-10-03 17:25:41 -0700655 goto out;
656
657 int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE) & IF_CS_C_IC_MASK;
658 if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause);
659
660 *ireg = if_cs_read16(card, IF_CS_C_STATUS) & IF_CS_C_S_MASK;
Holger Schurig27590d02007-10-03 17:25:41 -0700661
662 if (!*ireg)
663 goto sbi_get_int_status_exit;
664
665sbi_get_int_status_exit:
666
667 /* is there a data packet for us? */
668 if (*ireg & IF_CS_C_S_RX_UPLD_RDY) {
669 struct sk_buff *skb = if_cs_receive_data(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500670 lbs_process_rxed_packet(priv, skb);
Holger Schurig27590d02007-10-03 17:25:41 -0700671 *ireg &= ~IF_CS_C_S_RX_UPLD_RDY;
672 }
673
674 if (*ireg & IF_CS_C_S_TX_DNLD_RDY) {
675 priv->dnld_sent = DNLD_RES_RECEIVED;
676 }
677
678 /* Card has a command result for us */
679 if (*ireg & IF_CS_C_S_CMD_UPLD_RDY) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000680 spin_lock(&priv->driver_lock);
681 if (!priv->cur_cmd) {
Holger Schurig27590d02007-10-03 17:25:41 -0700682 cmdbuf = priv->upld_buf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000683 priv->hisregcpy &= ~IF_CS_C_S_RX_UPLD_RDY;
Holger Schurig27590d02007-10-03 17:25:41 -0700684 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000685 cmdbuf = priv->cur_cmd->bufvirtualaddr;
Holger Schurig27590d02007-10-03 17:25:41 -0700686 }
687
688 ret = if_cs_receive_cmdres(priv, cmdbuf, &priv->upld_len);
David Woodhouseaa21c002007-12-08 20:04:36 +0000689 spin_unlock(&priv->driver_lock);
Holger Schurig27590d02007-10-03 17:25:41 -0700690 if (ret < 0)
691 lbs_pr_err("could not receive cmd from card\n");
692 }
693
694out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000695 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, ireg 0x%x, hisregcpy 0x%x", ret, *ireg, priv->hisregcpy);
Holger Schurig27590d02007-10-03 17:25:41 -0700696 return ret;
697}
698
699
Holger Schurig69f90322007-11-23 15:43:44 +0100700static int if_cs_read_event_cause(struct lbs_private *priv)
Holger Schurig27590d02007-10-03 17:25:41 -0700701{
702 lbs_deb_enter(LBS_DEB_CS);
703
David Woodhouseaa21c002007-12-08 20:04:36 +0000704 priv->eventcause = (if_cs_read16(priv->card, IF_CS_C_STATUS) & IF_CS_C_S_STATUS_MASK) >> 5;
Holger Schurig27590d02007-10-03 17:25:41 -0700705 if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_HOST_EVENT);
706
707 return 0;
708}
709
710
711
712/********************************************************************/
713/* Card Services */
714/********************************************************************/
715
716/*
717 * After a card is removed, if_cs_release() will unregister the
718 * device, and release the PCMCIA configuration. If the device is
719 * still open, this will be postponed until it is closed.
720 */
721static void if_cs_release(struct pcmcia_device *p_dev)
722{
723 struct if_cs_card *card = p_dev->priv;
724
725 lbs_deb_enter(LBS_DEB_CS);
726
727 pcmcia_disable_device(p_dev);
728 free_irq(p_dev->irq.AssignedIRQ, card);
729 if (card->iobase)
730 ioport_unmap(card->iobase);
731
732 lbs_deb_leave(LBS_DEB_CS);
733}
734
735
736/*
737 * This creates an "instance" of the driver, allocating local data
738 * structures for one device. The device is registered with Card
739 * Services.
740 *
741 * The dev_link structure is initialized, but we don't actually
742 * configure the card at this point -- we wait until we receive a card
743 * insertion event.
744 */
745static int if_cs_probe(struct pcmcia_device *p_dev)
746{
747 int ret = -ENOMEM;
Holger Schurig69f90322007-11-23 15:43:44 +0100748 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700749 struct if_cs_card *card;
750 /* CIS parsing */
751 tuple_t tuple;
752 cisparse_t parse;
753 cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
754 cistpl_io_t *io = &cfg->io;
755 u_char buf[64];
756
757 lbs_deb_enter(LBS_DEB_CS);
758
759 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
760 if (!card) {
761 lbs_pr_err("error in kzalloc\n");
762 goto out;
763 }
764 card->p_dev = p_dev;
765 p_dev->priv = card;
766
767 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
768 p_dev->irq.Handler = NULL;
769 p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
770
771 p_dev->conf.Attributes = 0;
772 p_dev->conf.IntType = INT_MEMORY_AND_IO;
773
774 tuple.Attributes = 0;
775 tuple.TupleData = buf;
776 tuple.TupleDataMax = sizeof(buf);
777 tuple.TupleOffset = 0;
778
779 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
780 if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
781 (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
782 (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
783 {
784 lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
785 goto out1;
786 }
787
788 p_dev->conf.ConfigIndex = cfg->index;
789
790 /* Do we need to allocate an interrupt? */
791 if (cfg->irq.IRQInfo1) {
792 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
793 }
794
795 /* IO window settings */
796 if (cfg->io.nwin != 1) {
797 lbs_pr_err("wrong CIS (check number of IO windows)\n");
798 ret = -ENODEV;
799 goto out1;
800 }
801 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
802 p_dev->io.BasePort1 = io->win[0].base;
803 p_dev->io.NumPorts1 = io->win[0].len;
804
805 /* This reserves IO space but doesn't actually enable it */
806 ret = pcmcia_request_io(p_dev, &p_dev->io);
807 if (ret) {
808 lbs_pr_err("error in pcmcia_request_io\n");
809 goto out1;
810 }
811
812 /*
813 * Allocate an interrupt line. Note that this does not assign
814 * a handler to the interrupt, unless the 'Handler' member of
815 * the irq structure is initialized.
816 */
817 if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
818 ret = pcmcia_request_irq(p_dev, &p_dev->irq);
819 if (ret) {
820 lbs_pr_err("error in pcmcia_request_irq\n");
821 goto out1;
822 }
823 }
824
825 /* Initialize io access */
826 card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
827 if (!card->iobase) {
828 lbs_pr_err("error in ioport_map\n");
829 ret = -EIO;
830 goto out1;
831 }
832
833 /*
834 * This actually configures the PCMCIA socket -- setting up
835 * the I/O windows and the interrupt mapping, and putting the
836 * card and host interface into "Memory and IO" mode.
837 */
838 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
839 if (ret) {
840 lbs_pr_err("error in pcmcia_request_configuration\n");
841 goto out2;
842 }
843
844 /* Finally, report what we've done */
845 lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
846 p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
847 p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
848
Holger Schurig27590d02007-10-03 17:25:41 -0700849
850 /* Load the firmware early, before calling into libertas.ko */
851 ret = if_cs_prog_helper(card);
852 if (ret == 0)
853 ret = if_cs_prog_real(card);
854 if (ret)
855 goto out2;
856
857 /* Make this card known to the libertas driver */
Holger Schurig10078322007-11-15 18:05:47 -0500858 priv = lbs_add_card(card, &p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700859 if (!priv) {
860 ret = -ENOMEM;
861 goto out2;
862 }
863
864 /* Store pointers to our call-back functions */
Dan Williams954ee162007-08-20 11:43:25 -0400865 card->priv = priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700866 priv->card = card;
Holger Schurig27590d02007-10-03 17:25:41 -0700867 priv->hw_host_to_card = if_cs_host_to_card;
868 priv->hw_get_int_status = if_cs_get_int_status;
869 priv->hw_read_event_cause = if_cs_read_event_cause;
870
David Woodhouseaa21c002007-12-08 20:04:36 +0000871 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400872
Holger Schurig27590d02007-10-03 17:25:41 -0700873 /* Now actually get the IRQ */
874 ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
875 IRQF_SHARED, DRV_NAME, card);
876 if (ret) {
877 lbs_pr_err("error in request_irq\n");
878 goto out3;
879 }
880
Holger Schurig4ef31702007-10-09 10:41:57 +0200881 /* Clear any interrupt cause that happend while sending
882 * firmware/initializing card */
883 if_cs_write16(card, IF_CS_C_INT_CAUSE, IF_CS_C_IC_MASK);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400884 if_cs_enable_ints(card);
885
Holger Schurig27590d02007-10-03 17:25:41 -0700886 /* And finally bring the card up */
Holger Schurig10078322007-11-15 18:05:47 -0500887 if (lbs_start_card(priv) != 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700888 lbs_pr_err("could not activate card\n");
889 goto out3;
890 }
891
892 ret = 0;
893 goto out;
894
895out3:
Holger Schurig10078322007-11-15 18:05:47 -0500896 lbs_remove_card(priv);
Holger Schurig27590d02007-10-03 17:25:41 -0700897out2:
898 ioport_unmap(card->iobase);
899out1:
900 pcmcia_disable_device(p_dev);
901out:
902 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
903 return ret;
904}
905
906
907/*
908 * This deletes a driver "instance". The device is de-registered with
909 * Card Services. If it has been released, all local data structures
910 * are freed. Otherwise, the structures will be freed when the device
911 * is released.
912 */
913static void if_cs_detach(struct pcmcia_device *p_dev)
914{
915 struct if_cs_card *card = p_dev->priv;
916
917 lbs_deb_enter(LBS_DEB_CS);
918
Holger Schurig10078322007-11-15 18:05:47 -0500919 lbs_stop_card(card->priv);
920 lbs_remove_card(card->priv);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400921 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700922 if_cs_release(p_dev);
923 kfree(card);
924
925 lbs_deb_leave(LBS_DEB_CS);
926}
927
928
929
930/********************************************************************/
931/* Module initialization */
932/********************************************************************/
933
934static struct pcmcia_device_id if_cs_ids[] = {
935 PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
936 PCMCIA_DEVICE_NULL,
937};
938MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
939
940
Holger Schurig10078322007-11-15 18:05:47 -0500941static struct pcmcia_driver lbs_driver = {
Holger Schurig27590d02007-10-03 17:25:41 -0700942 .owner = THIS_MODULE,
943 .drv = {
944 .name = DRV_NAME,
945 },
946 .probe = if_cs_probe,
947 .remove = if_cs_detach,
948 .id_table = if_cs_ids,
949};
950
951
952static int __init if_cs_init(void)
953{
954 int ret;
955
956 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500957 ret = pcmcia_register_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700958 lbs_deb_leave(LBS_DEB_CS);
959 return ret;
960}
961
962
963static void __exit if_cs_exit(void)
964{
965 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500966 pcmcia_unregister_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700967 lbs_deb_leave(LBS_DEB_CS);
968}
969
970
971module_init(if_cs_init);
972module_exit(if_cs_exit);