blob: 5963d92cc94d59462c8aef4b8dcea78799a6aff9 [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)
Holger Schurig7919b892008-04-01 14:50:43 +020086 printk(KERN_INFO "inb %08x<%02x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -070087 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)
Holger Schurig7919b892008-04-01 14:50:43 +020093 printk(KERN_INFO "inw %08x<%04x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -070094 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)
Holger Schurig7919b892008-04-01 14:50:43 +0200103 printk(KERN_INFO "insw %08x<(0x%lx words)\n",
Holger Schurig27590d02007-10-03 17:25:41 -0700104 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)
Holger Schurig7919b892008-04-01 14:50:43 +0200111 printk(KERN_INFO "outb %08x>%02x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700112 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)
Holger Schurig7919b892008-04-01 14:50:43 +0200118 printk(KERN_INFO "outw %08x>%04x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700119 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)
Holger Schurig7919b892008-04-01 14:50:43 +0200129 printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
Holger Schurig27590d02007-10-03 17:25:41 -0700130 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 Schurig93416c82008-05-23 10:18:26 +0200151 for (i = 0; i < 100000; i++) {
Holger Schurig27590d02007-10-03 17:25:41 -0700152 u8 val = if_cs_read8(card, addr);
153 if (val == reg)
154 return i;
Holger Schurig93416c82008-05-23 10:18:26 +0200155 udelay(5);
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
Holger Schurig27590d02007-10-03 17:25:41 -0700202
203#define IF_CS_C_INT_CAUSE 0x00000022
204#define IF_CS_C_IC_MASK 0x001f
205
206#define IF_CS_C_SQ_READ_LOW 0x00000028
207#define IF_CS_C_SQ_HELPER_OK 0x10
208
209#define IF_CS_C_CMD_LEN 0x00000030
210
211#define IF_CS_C_CMD 0x00000012
212
213#define IF_CS_SCRATCH 0x0000003F
214
215
216
217/********************************************************************/
Holger Schurig43d01c52008-05-26 12:50:23 +0200218/* I/O and interrupt handling */
Holger Schurig27590d02007-10-03 17:25:41 -0700219/********************************************************************/
220
Holger Schurig43d01c52008-05-26 12:50:23 +0200221static inline void if_cs_enable_ints(struct if_cs_card *card)
222{
223 lbs_deb_enter(LBS_DEB_CS);
224 if_cs_write16(card, IF_CS_H_INT_MASK, 0);
225}
226
227static inline void if_cs_disable_ints(struct if_cs_card *card)
228{
229 lbs_deb_enter(LBS_DEB_CS);
230 if_cs_write16(card, IF_CS_H_INT_MASK, IF_CS_H_IM_MASK);
231}
232
Holger Schurig27590d02007-10-03 17:25:41 -0700233/*
234 * Called from if_cs_host_to_card to send a command to the hardware
235 */
Holger Schurig69f90322007-11-23 15:43:44 +0100236static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700237{
238 struct if_cs_card *card = (struct if_cs_card *)priv->card;
239 int ret = -1;
240 int loops = 0;
241
242 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200243 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700244
245 /* Is hardware ready? */
246 while (1) {
247 u16 val = if_cs_read16(card, IF_CS_C_STATUS);
248 if (val & IF_CS_C_S_CMD_DNLD_RDY)
249 break;
250 if (++loops > 100) {
251 lbs_pr_err("card not ready for commands\n");
252 goto done;
253 }
254 mdelay(1);
255 }
256
257 if_cs_write16(card, IF_CS_H_CMD_LEN, nb);
258
259 if_cs_write16_rep(card, IF_CS_H_CMD, buf, nb / 2);
260 /* Are we supposed to transfer an odd amount of bytes? */
261 if (nb & 1)
262 if_cs_write8(card, IF_CS_H_CMD, buf[nb-1]);
263
264 /* "Assert the download over interrupt command in the Host
265 * status register" */
266 if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
267
268 /* "Assert the download over interrupt command in the Card
269 * interrupt case register" */
270 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
271 ret = 0;
272
273done:
Holger Schurig43d01c52008-05-26 12:50:23 +0200274 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700275 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
276 return ret;
277}
278
Holger Schurig27590d02007-10-03 17:25:41 -0700279/*
280 * Called from if_cs_host_to_card to send a data to the hardware
281 */
Holger Schurig69f90322007-11-23 15:43:44 +0100282static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700283{
284 struct if_cs_card *card = (struct if_cs_card *)priv->card;
Holger Schurig43d01c52008-05-26 12:50:23 +0200285 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700286
287 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200288 if_cs_disable_ints(card);
289
290 status = if_cs_read16(card, IF_CS_C_STATUS);
291 BUG_ON((status & IF_CS_C_S_TX_DNLD_RDY) == 0);
Holger Schurig27590d02007-10-03 17:25:41 -0700292
293 if_cs_write16(card, IF_CS_H_WRITE_LEN, nb);
294
295 /* write even number of bytes, then odd byte if necessary */
296 if_cs_write16_rep(card, IF_CS_H_WRITE, buf, nb / 2);
297 if (nb & 1)
298 if_cs_write8(card, IF_CS_H_WRITE, buf[nb-1]);
299
300 if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_TX_OVER);
301 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_STATUS_TX_OVER);
Holger Schurig43d01c52008-05-26 12:50:23 +0200302 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700303
304 lbs_deb_leave(LBS_DEB_CS);
305}
306
Holger Schurig27590d02007-10-03 17:25:41 -0700307/*
308 * Get the command result out of the card.
309 */
Holger Schurig69f90322007-11-23 15:43:44 +0100310static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
Holger Schurig27590d02007-10-03 17:25:41 -0700311{
Holger Schurig7919b892008-04-01 14:50:43 +0200312 unsigned long flags;
Holger Schurig27590d02007-10-03 17:25:41 -0700313 int ret = -1;
314 u16 val;
315
316 lbs_deb_enter(LBS_DEB_CS);
317
318 /* is hardware ready? */
319 val = if_cs_read16(priv->card, IF_CS_C_STATUS);
320 if ((val & IF_CS_C_S_CMD_UPLD_RDY) == 0) {
321 lbs_pr_err("card not ready for CMD\n");
322 goto out;
323 }
324
325 *len = if_cs_read16(priv->card, IF_CS_C_CMD_LEN);
Dan Williamsddac4522007-12-11 13:49:39 -0500326 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
Holger Schurig27590d02007-10-03 17:25:41 -0700327 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
328 goto out;
329 }
330
331 /* read even number of bytes, then odd byte if necessary */
332 if_cs_read16_rep(priv->card, IF_CS_C_CMD, data, *len/sizeof(u16));
333 if (*len & 1)
334 data[*len-1] = if_cs_read8(priv->card, IF_CS_C_CMD);
335
Holger Schurig09d4fad2007-12-06 13:50:30 +0100336 /* This is a workaround for a firmware that reports too much
337 * bytes */
338 *len -= 8;
Holger Schurig27590d02007-10-03 17:25:41 -0700339 ret = 0;
Holger Schurig7919b892008-04-01 14:50:43 +0200340
341 /* Clear this flag again */
342 spin_lock_irqsave(&priv->driver_lock, flags);
343 priv->dnld_sent = DNLD_RES_RECEIVED;
344 spin_unlock_irqrestore(&priv->driver_lock, flags);
345
Holger Schurig27590d02007-10-03 17:25:41 -0700346out:
347 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
348 return ret;
349}
350
Holger Schurig69f90322007-11-23 15:43:44 +0100351static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
Holger Schurig27590d02007-10-03 17:25:41 -0700352{
353 struct sk_buff *skb = NULL;
354 u16 len;
355 u8 *data;
356
357 lbs_deb_enter(LBS_DEB_CS);
358
359 len = if_cs_read16(priv->card, IF_CS_C_READ_LEN);
360 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
361 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
362 priv->stats.rx_dropped++;
Holger Schurig27590d02007-10-03 17:25:41 -0700363 goto dat_err;
364 }
365
Vladimir Davydovf31ce762007-09-06 21:41:02 -0400366 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700367 if (!skb)
368 goto out;
Vladimir Davydovf31ce762007-09-06 21:41:02 -0400369 skb_put(skb, len);
370 skb_reserve(skb, 2);/* 16 byte align */
371 data = skb->data;
Holger Schurig27590d02007-10-03 17:25:41 -0700372
373 /* read even number of bytes, then odd byte if necessary */
374 if_cs_read16_rep(priv->card, IF_CS_H_READ, data, len/sizeof(u16));
375 if (len & 1)
376 data[len-1] = if_cs_read8(priv->card, IF_CS_H_READ);
377
378dat_err:
379 if_cs_write16(priv->card, IF_CS_H_STATUS, IF_CS_H_STATUS_RX_OVER);
380 if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_RX_OVER);
381
382out:
383 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
384 return skb;
385}
386
Holger Schurig7919b892008-04-01 14:50:43 +0200387static irqreturn_t if_cs_interrupt(int irq, void *data)
388{
389 struct if_cs_card *card = data;
390 struct lbs_private *priv = card->priv;
391 u16 cause;
392
393 lbs_deb_enter(LBS_DEB_CS);
394
Holger Schurig43d01c52008-05-26 12:50:23 +0200395 /* Ask card interrupt cause register if there is something for us */
Holger Schurig7919b892008-04-01 14:50:43 +0200396 cause = if_cs_read16(card, IF_CS_C_INT_CAUSE);
Holger Schurig7919b892008-04-01 14:50:43 +0200397 if (cause == 0) {
398 /* Not for us */
399 return IRQ_NONE;
400 }
401
402 if (cause == 0xffff) {
403 /* Read in junk, the card has probably been removed */
404 card->priv->surpriseremoved = 1;
405 return IRQ_HANDLED;
406 }
407
Holger Schurig43d01c52008-05-26 12:50:23 +0200408 /* Clear interrupt cause */
409 if_cs_write16(card, IF_CS_C_INT_CAUSE, cause & IF_CS_C_IC_MASK);
410 lbs_deb_cs("cause 0x%04x\n", cause);
Holger Schurig7919b892008-04-01 14:50:43 +0200411
412 if (cause & IF_CS_C_S_RX_UPLD_RDY) {
413 struct sk_buff *skb;
414 lbs_deb_cs("rx packet\n");
415 skb = if_cs_receive_data(priv);
416 if (skb)
417 lbs_process_rxed_packet(priv, skb);
418 }
419
420 if (cause & IF_CS_H_IC_TX_OVER) {
Holger Schurig43d01c52008-05-26 12:50:23 +0200421 lbs_deb_cs("tx done\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200422 lbs_host_to_card_done(priv);
423 }
424
425 if (cause & IF_CS_C_S_CMD_UPLD_RDY) {
426 unsigned long flags;
427 u8 i;
428
Holger Schurig43d01c52008-05-26 12:50:23 +0200429 lbs_deb_cs("cmd resp\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200430 spin_lock_irqsave(&priv->driver_lock, flags);
431 i = (priv->resp_idx == 0) ? 1 : 0;
432 spin_unlock_irqrestore(&priv->driver_lock, flags);
433
434 BUG_ON(priv->resp_len[i]);
435 if_cs_receive_cmdres(priv, priv->resp_buf[i],
436 &priv->resp_len[i]);
437
438 spin_lock_irqsave(&priv->driver_lock, flags);
439 lbs_notify_command_response(priv, i);
440 spin_unlock_irqrestore(&priv->driver_lock, flags);
441 }
442
443 if (cause & IF_CS_H_IC_HOST_EVENT) {
444 u16 event = if_cs_read16(priv->card, IF_CS_C_STATUS)
445 & IF_CS_C_S_STATUS_MASK;
446 if_cs_write16(priv->card, IF_CS_H_INT_CAUSE,
447 IF_CS_H_IC_HOST_EVENT);
Holger Schurig43d01c52008-05-26 12:50:23 +0200448 lbs_deb_cs("host event 0x%04x\n", event);
Holger Schurig7919b892008-04-01 14:50:43 +0200449 lbs_queue_event(priv, event >> 8 & 0xff);
450 }
451
Holger Schurig43d01c52008-05-26 12:50:23 +0200452 lbs_deb_leave(LBS_DEB_CS);
Holger Schurig7919b892008-04-01 14:50:43 +0200453 return IRQ_HANDLED;
454}
455
456
457
458
459/********************************************************************/
Holger Schurig27590d02007-10-03 17:25:41 -0700460/* Firmware */
461/********************************************************************/
462
463/*
464 * Tries to program the helper firmware.
465 *
466 * Return 0 on success
467 */
468static int if_cs_prog_helper(struct if_cs_card *card)
469{
470 int ret = 0;
471 int sent = 0;
472 u8 scratch;
473 const struct firmware *fw;
474
475 lbs_deb_enter(LBS_DEB_CS);
476
477 scratch = if_cs_read8(card, IF_CS_SCRATCH);
478
479 /* "If the value is 0x5a, the firmware is already
480 * downloaded successfully"
481 */
482 if (scratch == 0x5a)
483 goto done;
484
485 /* "If the value is != 00, it is invalid value of register */
486 if (scratch != 0x00) {
487 ret = -ENODEV;
488 goto done;
489 }
490
491 /* TODO: make firmware file configurable */
492 ret = request_firmware(&fw, "libertas_cs_helper.fw",
493 &handle_to_dev(card->p_dev));
494 if (ret) {
495 lbs_pr_err("can't load helper firmware\n");
496 ret = -ENODEV;
497 goto done;
498 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700499 lbs_deb_cs("helper size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700500
501 /* "Set the 5 bytes of the helper image to 0" */
502 /* Not needed, this contains an ARM branch instruction */
503
504 for (;;) {
505 /* "the number of bytes to send is 256" */
506 int count = 256;
507 int remain = fw->size - sent;
508
509 if (remain < count)
510 count = remain;
Holger Schurig27590d02007-10-03 17:25:41 -0700511
512 /* "write the number of bytes to be sent to the I/O Command
513 * write length register" */
514 if_cs_write16(card, IF_CS_H_CMD_LEN, count);
515
516 /* "write this to I/O Command port register as 16 bit writes */
517 if (count)
518 if_cs_write16_rep(card, IF_CS_H_CMD,
519 &fw->data[sent],
520 count >> 1);
521
522 /* "Assert the download over interrupt command in the Host
523 * status register" */
524 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
525
526 /* "Assert the download over interrupt command in the Card
527 * interrupt case register" */
528 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
529
530 /* "The host polls the Card Status register ... for 50 ms before
531 declaring a failure */
532 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
533 IF_CS_C_S_CMD_DNLD_RDY);
534 if (ret < 0) {
535 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
536 sent, ret);
537 goto done;
538 }
539
540 if (count == 0)
541 break;
542
543 sent += count;
544 }
545
546 release_firmware(fw);
547 ret = 0;
548
549done:
550 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
551 return ret;
552}
553
554
555static int if_cs_prog_real(struct if_cs_card *card)
556{
557 const struct firmware *fw;
558 int ret = 0;
559 int retry = 0;
560 int len = 0;
561 int sent;
562
563 lbs_deb_enter(LBS_DEB_CS);
564
565 /* TODO: make firmware file configurable */
566 ret = request_firmware(&fw, "libertas_cs.fw",
567 &handle_to_dev(card->p_dev));
568 if (ret) {
569 lbs_pr_err("can't load firmware\n");
570 ret = -ENODEV;
571 goto done;
572 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700573 lbs_deb_cs("fw size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700574
575 ret = if_cs_poll_while_fw_download(card, IF_CS_C_SQ_READ_LOW, IF_CS_C_SQ_HELPER_OK);
576 if (ret < 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700577 lbs_pr_err("helper firmware doesn't answer\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700578 goto err_release;
579 }
580
581 for (sent = 0; sent < fw->size; sent += len) {
582 len = if_cs_read16(card, IF_CS_C_SQ_READ_LOW);
Holger Schurig27590d02007-10-03 17:25:41 -0700583 if (len & 1) {
584 retry++;
585 lbs_pr_info("odd, need to retry this firmware block\n");
586 } else {
587 retry = 0;
588 }
589
590 if (retry > 20) {
591 lbs_pr_err("could not download firmware\n");
592 ret = -ENODEV;
593 goto err_release;
594 }
595 if (retry) {
596 sent -= len;
597 }
598
599
600 if_cs_write16(card, IF_CS_H_CMD_LEN, len);
601
602 if_cs_write16_rep(card, IF_CS_H_CMD,
603 &fw->data[sent],
604 (len+1) >> 1);
605 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
606 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
607
608 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
609 IF_CS_C_S_CMD_DNLD_RDY);
610 if (ret < 0) {
611 lbs_pr_err("can't download firmware at 0x%x\n", sent);
612 goto err_release;
613 }
614 }
615
616 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
617 if (ret < 0) {
618 lbs_pr_err("firmware download failed\n");
619 goto err_release;
620 }
621
622 ret = 0;
623 goto done;
624
625
626err_release:
627 release_firmware(fw);
628
629done:
630 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
631 return ret;
632}
633
634
635
636/********************************************************************/
637/* Callback functions for libertas.ko */
638/********************************************************************/
639
Holger Schurig27590d02007-10-03 17:25:41 -0700640/* Send commands or data packets to the card */
Holger Schurig69f90322007-11-23 15:43:44 +0100641static int if_cs_host_to_card(struct lbs_private *priv,
642 u8 type,
643 u8 *buf,
644 u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700645{
646 int ret = -1;
647
648 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
649
650 switch (type) {
651 case MVMS_DAT:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400652 priv->dnld_sent = DNLD_DATA_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700653 if_cs_send_data(priv, buf, nb);
654 ret = 0;
655 break;
656 case MVMS_CMD:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400657 priv->dnld_sent = DNLD_CMD_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700658 ret = if_cs_send_cmd(priv, buf, nb);
659 break;
660 default:
661 lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
662 }
663
664 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
665 return ret;
666}
667
668
Holger Schurig27590d02007-10-03 17:25:41 -0700669/********************************************************************/
670/* Card Services */
671/********************************************************************/
672
673/*
674 * After a card is removed, if_cs_release() will unregister the
675 * device, and release the PCMCIA configuration. If the device is
676 * still open, this will be postponed until it is closed.
677 */
678static void if_cs_release(struct pcmcia_device *p_dev)
679{
680 struct if_cs_card *card = p_dev->priv;
681
682 lbs_deb_enter(LBS_DEB_CS);
683
Holger Schurig27590d02007-10-03 17:25:41 -0700684 free_irq(p_dev->irq.AssignedIRQ, card);
Holger Schurigfdfb92e2008-01-25 14:15:48 +0100685 pcmcia_disable_device(p_dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700686 if (card->iobase)
687 ioport_unmap(card->iobase);
688
689 lbs_deb_leave(LBS_DEB_CS);
690}
691
692
693/*
694 * This creates an "instance" of the driver, allocating local data
695 * structures for one device. The device is registered with Card
696 * Services.
697 *
698 * The dev_link structure is initialized, but we don't actually
699 * configure the card at this point -- we wait until we receive a card
700 * insertion event.
701 */
702static int if_cs_probe(struct pcmcia_device *p_dev)
703{
704 int ret = -ENOMEM;
Holger Schurig69f90322007-11-23 15:43:44 +0100705 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700706 struct if_cs_card *card;
707 /* CIS parsing */
708 tuple_t tuple;
709 cisparse_t parse;
710 cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
711 cistpl_io_t *io = &cfg->io;
712 u_char buf[64];
713
714 lbs_deb_enter(LBS_DEB_CS);
715
716 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
717 if (!card) {
718 lbs_pr_err("error in kzalloc\n");
719 goto out;
720 }
721 card->p_dev = p_dev;
722 p_dev->priv = card;
723
724 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
725 p_dev->irq.Handler = NULL;
726 p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
727
728 p_dev->conf.Attributes = 0;
729 p_dev->conf.IntType = INT_MEMORY_AND_IO;
730
731 tuple.Attributes = 0;
732 tuple.TupleData = buf;
733 tuple.TupleDataMax = sizeof(buf);
734 tuple.TupleOffset = 0;
735
736 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
737 if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
738 (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
739 (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
740 {
741 lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
742 goto out1;
743 }
744
745 p_dev->conf.ConfigIndex = cfg->index;
746
747 /* Do we need to allocate an interrupt? */
748 if (cfg->irq.IRQInfo1) {
749 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
750 }
751
752 /* IO window settings */
753 if (cfg->io.nwin != 1) {
754 lbs_pr_err("wrong CIS (check number of IO windows)\n");
755 ret = -ENODEV;
756 goto out1;
757 }
758 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
759 p_dev->io.BasePort1 = io->win[0].base;
760 p_dev->io.NumPorts1 = io->win[0].len;
761
762 /* This reserves IO space but doesn't actually enable it */
763 ret = pcmcia_request_io(p_dev, &p_dev->io);
764 if (ret) {
765 lbs_pr_err("error in pcmcia_request_io\n");
766 goto out1;
767 }
768
769 /*
770 * Allocate an interrupt line. Note that this does not assign
771 * a handler to the interrupt, unless the 'Handler' member of
772 * the irq structure is initialized.
773 */
774 if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
775 ret = pcmcia_request_irq(p_dev, &p_dev->irq);
776 if (ret) {
777 lbs_pr_err("error in pcmcia_request_irq\n");
778 goto out1;
779 }
780 }
781
782 /* Initialize io access */
783 card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
784 if (!card->iobase) {
785 lbs_pr_err("error in ioport_map\n");
786 ret = -EIO;
787 goto out1;
788 }
789
790 /*
791 * This actually configures the PCMCIA socket -- setting up
792 * the I/O windows and the interrupt mapping, and putting the
793 * card and host interface into "Memory and IO" mode.
794 */
795 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
796 if (ret) {
797 lbs_pr_err("error in pcmcia_request_configuration\n");
798 goto out2;
799 }
800
801 /* Finally, report what we've done */
802 lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
803 p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
804 p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
805
Holger Schurig27590d02007-10-03 17:25:41 -0700806
807 /* Load the firmware early, before calling into libertas.ko */
808 ret = if_cs_prog_helper(card);
809 if (ret == 0)
810 ret = if_cs_prog_real(card);
811 if (ret)
812 goto out2;
813
814 /* Make this card known to the libertas driver */
Holger Schurig10078322007-11-15 18:05:47 -0500815 priv = lbs_add_card(card, &p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700816 if (!priv) {
817 ret = -ENOMEM;
818 goto out2;
819 }
820
Holger Schurig7919b892008-04-01 14:50:43 +0200821 /* Finish setting up fields in lbs_private */
Dan Williams954ee162007-08-20 11:43:25 -0400822 card->priv = priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700823 priv->card = card;
Holger Schurig7919b892008-04-01 14:50:43 +0200824 priv->hw_host_to_card = if_cs_host_to_card;
David Woodhouseaa21c002007-12-08 20:04:36 +0000825 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400826
Holger Schurig27590d02007-10-03 17:25:41 -0700827 /* Now actually get the IRQ */
828 ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
829 IRQF_SHARED, DRV_NAME, card);
830 if (ret) {
831 lbs_pr_err("error in request_irq\n");
832 goto out3;
833 }
834
Holger Schurig4ef31702007-10-09 10:41:57 +0200835 /* Clear any interrupt cause that happend while sending
836 * firmware/initializing card */
837 if_cs_write16(card, IF_CS_C_INT_CAUSE, IF_CS_C_IC_MASK);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400838 if_cs_enable_ints(card);
839
Holger Schurig27590d02007-10-03 17:25:41 -0700840 /* And finally bring the card up */
Holger Schurig10078322007-11-15 18:05:47 -0500841 if (lbs_start_card(priv) != 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700842 lbs_pr_err("could not activate card\n");
843 goto out3;
844 }
845
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100846 /* The firmware for the CF card supports powersave */
847 priv->ps_supported = 1;
848
Holger Schurig27590d02007-10-03 17:25:41 -0700849 ret = 0;
850 goto out;
851
852out3:
Holger Schurig10078322007-11-15 18:05:47 -0500853 lbs_remove_card(priv);
Holger Schurig27590d02007-10-03 17:25:41 -0700854out2:
855 ioport_unmap(card->iobase);
856out1:
857 pcmcia_disable_device(p_dev);
858out:
859 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
860 return ret;
861}
862
863
864/*
865 * This deletes a driver "instance". The device is de-registered with
866 * Card Services. If it has been released, all local data structures
867 * are freed. Otherwise, the structures will be freed when the device
868 * is released.
869 */
870static void if_cs_detach(struct pcmcia_device *p_dev)
871{
872 struct if_cs_card *card = p_dev->priv;
873
874 lbs_deb_enter(LBS_DEB_CS);
875
Holger Schurig10078322007-11-15 18:05:47 -0500876 lbs_stop_card(card->priv);
877 lbs_remove_card(card->priv);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400878 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700879 if_cs_release(p_dev);
880 kfree(card);
881
882 lbs_deb_leave(LBS_DEB_CS);
883}
884
885
886
887/********************************************************************/
888/* Module initialization */
889/********************************************************************/
890
891static struct pcmcia_device_id if_cs_ids[] = {
892 PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
893 PCMCIA_DEVICE_NULL,
894};
895MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
896
897
Holger Schurig10078322007-11-15 18:05:47 -0500898static struct pcmcia_driver lbs_driver = {
Holger Schurig27590d02007-10-03 17:25:41 -0700899 .owner = THIS_MODULE,
900 .drv = {
901 .name = DRV_NAME,
902 },
903 .probe = if_cs_probe,
904 .remove = if_cs_detach,
905 .id_table = if_cs_ids,
906};
907
908
909static int __init if_cs_init(void)
910{
911 int ret;
912
913 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500914 ret = pcmcia_register_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700915 lbs_deb_leave(LBS_DEB_CS);
916 return ret;
917}
918
919
920static void __exit if_cs_exit(void)
921{
922 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500923 pcmcia_unregister_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700924 lbs_deb_leave(LBS_DEB_CS);
925}
926
927
928module_init(if_cs_init);
929module_exit(if_cs_exit);