blob: 6870276259e941bb26b2146b0a5e7f5a642b5f3b [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
Holger Schurig53143252008-06-05 13:07:04 +0200162/*
163 * First the bitmasks for the host/card interrupt/status registers:
164 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200165#define IF_CS_BIT_TX 0x0001
166#define IF_CS_BIT_RX 0x0002
167#define IF_CS_BIT_COMMAND 0x0004
168#define IF_CS_BIT_RESP 0x0008
169#define IF_CS_BIT_EVENT 0x0010
170#define IF_CS_BIT_MASK 0x001f
Holger Schurig27590d02007-10-03 17:25:41 -0700171
Holger Schurig53143252008-06-05 13:07:04 +0200172
173
174/*
175 * It's not really clear to me what the host status register is for. It
176 * needs to be set almost in union with "host int cause". The following
177 * bits from above are used:
178 *
179 * IF_CS_BIT_TX driver downloaded a data packet
180 * IF_CS_BIT_RX driver got a data packet
181 * IF_CS_BIT_COMMAND driver downloaded a command
182 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
183 * IF_CS_BIT_EVENT driver read a host event
184 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200185#define IF_CS_HOST_STATUS 0x00000000
Holger Schurig27590d02007-10-03 17:25:41 -0700186
Holger Schurig53143252008-06-05 13:07:04 +0200187/*
188 * With the host int cause register can the host (that is, Linux) cause
189 * an interrupt in the firmware, to tell the firmware about those events:
190 *
191 * IF_CS_BIT_TX a data packet has been downloaded
192 * IF_CS_BIT_RX a received data packet has retrieved
193 * IF_CS_BIT_COMMAND a firmware block or a command has been downloaded
194 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
195 * IF_CS_BIT_EVENT a host event (link lost etc) has been retrieved
196 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200197#define IF_CS_HOST_INT_CAUSE 0x00000002
Holger Schurig27590d02007-10-03 17:25:41 -0700198
Holger Schurig53143252008-06-05 13:07:04 +0200199/*
200 * The host int mask register is used to enable/disable interrupt. However,
201 * I have the suspicion that disabled interrupts are lost.
202 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200203#define IF_CS_HOST_INT_MASK 0x00000004
Holger Schurig27590d02007-10-03 17:25:41 -0700204
Holger Schurig53143252008-06-05 13:07:04 +0200205/*
206 * Used to send or receive data packets:
207 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200208#define IF_CS_HOST_WRITE 0x00000016
209#define IF_CS_HOST_WRITE_LEN 0x00000014
Holger Schurig78cf0742008-06-02 09:25:05 +0200210#define IF_CS_READ 0x00000010
211#define IF_CS_READ_LEN 0x00000024
Holger Schurig27590d02007-10-03 17:25:41 -0700212
Holger Schurig53143252008-06-05 13:07:04 +0200213/*
214 * Used to send commands (and to send firmware block) and to
215 * receive command responses:
216 */
217#define IF_CS_HOST_CMD 0x0000001A
218#define IF_CS_HOST_CMD_LEN 0x00000018
Holger Schurig78cf0742008-06-02 09:25:05 +0200219#define IF_CS_CARD_CMD 0x00000012
220#define IF_CS_CARD_CMD_LEN 0x00000030
Holger Schurig27590d02007-10-03 17:25:41 -0700221
Holger Schurig53143252008-06-05 13:07:04 +0200222/*
223 * The card status registers shows what the card/firmware actually
224 * accepts:
225 *
226 * IF_CS_BIT_TX you may send a data packet
227 * IF_CS_BIT_RX you may retrieve a data packet
228 * IF_CS_BIT_COMMAND you may send a command
229 * IF_CS_BIT_RESP you may retrieve a command response
230 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
231 *
232 * When reading this register several times, you will get back the same
233 * results --- with one exception: the IF_CS_BIT_EVENT clear itself
234 * automatically.
235 *
236 * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
237 * we handle this via the card int cause register.
238 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200239#define IF_CS_CARD_STATUS 0x00000020
240#define IF_CS_CARD_STATUS_MASK 0x7f00
Holger Schurig27590d02007-10-03 17:25:41 -0700241
Holger Schurig53143252008-06-05 13:07:04 +0200242/*
243 * The card int cause register is used by the card/firmware to notify us
244 * about the following events:
245 *
246 * IF_CS_BIT_TX a data packet has successfully been sentx
247 * IF_CS_BIT_RX a data packet has been received and can be retrieved
248 * IF_CS_BIT_COMMAND not used
249 * IF_CS_BIT_RESP the firmware has a command response for us
250 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
251 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200252#define IF_CS_CARD_INT_CAUSE 0x00000022
Holger Schurig27590d02007-10-03 17:25:41 -0700253
Holger Schurig53143252008-06-05 13:07:04 +0200254/*
255 * This is used to for handshaking with the card's bootloader/helper image
256 * to synchronize downloading of firmware blocks.
257 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200258#define IF_CS_CARD_SQ_READ_LOW 0x00000028
259#define IF_CS_CARD_SQ_HELPER_OK 0x10
Holger Schurig27590d02007-10-03 17:25:41 -0700260
Holger Schurig53143252008-06-05 13:07:04 +0200261/*
262 * The scratch register tells us ...
263 *
264 * IF_CS_SCRATCH_BOOT_OK the bootloader runs
265 * IF_CS_SCRATCH_HELPER_OK the helper firmware already runs
266 */
Holger Schurig27590d02007-10-03 17:25:41 -0700267#define IF_CS_SCRATCH 0x0000003F
Holger Schurig53143252008-06-05 13:07:04 +0200268#define IF_CS_SCRATCH_BOOT_OK 0x00
269#define IF_CS_SCRATCH_HELPER_OK 0x5a
Holger Schurig27590d02007-10-03 17:25:41 -0700270
271
272/********************************************************************/
Holger Schurig43d01c52008-05-26 12:50:23 +0200273/* I/O and interrupt handling */
Holger Schurig27590d02007-10-03 17:25:41 -0700274/********************************************************************/
275
Holger Schurig43d01c52008-05-26 12:50:23 +0200276static inline void if_cs_enable_ints(struct if_cs_card *card)
277{
278 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200279 if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
Holger Schurig43d01c52008-05-26 12:50:23 +0200280}
281
282static inline void if_cs_disable_ints(struct if_cs_card *card)
283{
284 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200285 if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
Holger Schurig43d01c52008-05-26 12:50:23 +0200286}
287
Holger Schurig27590d02007-10-03 17:25:41 -0700288/*
289 * Called from if_cs_host_to_card to send a command to the hardware
290 */
Holger Schurig69f90322007-11-23 15:43:44 +0100291static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700292{
293 struct if_cs_card *card = (struct if_cs_card *)priv->card;
294 int ret = -1;
295 int loops = 0;
296
297 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200298 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700299
300 /* Is hardware ready? */
301 while (1) {
Holger Schurig78cf0742008-06-02 09:25:05 +0200302 u16 val = if_cs_read16(card, IF_CS_CARD_STATUS);
303 if (val & IF_CS_BIT_COMMAND)
Holger Schurig27590d02007-10-03 17:25:41 -0700304 break;
305 if (++loops > 100) {
306 lbs_pr_err("card not ready for commands\n");
307 goto done;
308 }
309 mdelay(1);
310 }
311
Holger Schurig78cf0742008-06-02 09:25:05 +0200312 if_cs_write16(card, IF_CS_HOST_CMD_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700313
Holger Schurig78cf0742008-06-02 09:25:05 +0200314 if_cs_write16_rep(card, IF_CS_HOST_CMD, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700315 /* Are we supposed to transfer an odd amount of bytes? */
316 if (nb & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200317 if_cs_write8(card, IF_CS_HOST_CMD, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700318
319 /* "Assert the download over interrupt command in the Host
320 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200321 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700322
323 /* "Assert the download over interrupt command in the Card
324 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200325 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700326 ret = 0;
327
328done:
Holger Schurig43d01c52008-05-26 12:50:23 +0200329 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700330 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
331 return ret;
332}
333
Holger Schurig27590d02007-10-03 17:25:41 -0700334/*
335 * Called from if_cs_host_to_card to send a data to the hardware
336 */
Holger Schurig69f90322007-11-23 15:43:44 +0100337static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700338{
339 struct if_cs_card *card = (struct if_cs_card *)priv->card;
Holger Schurig43d01c52008-05-26 12:50:23 +0200340 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700341
342 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200343 if_cs_disable_ints(card);
344
Holger Schurig78cf0742008-06-02 09:25:05 +0200345 status = if_cs_read16(card, IF_CS_CARD_STATUS);
346 BUG_ON((status & IF_CS_BIT_TX) == 0);
Holger Schurig27590d02007-10-03 17:25:41 -0700347
Holger Schurig78cf0742008-06-02 09:25:05 +0200348 if_cs_write16(card, IF_CS_HOST_WRITE_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700349
350 /* write even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200351 if_cs_write16_rep(card, IF_CS_HOST_WRITE, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700352 if (nb & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200353 if_cs_write8(card, IF_CS_HOST_WRITE, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700354
Holger Schurig78cf0742008-06-02 09:25:05 +0200355 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
356 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
Holger Schurig43d01c52008-05-26 12:50:23 +0200357 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700358
359 lbs_deb_leave(LBS_DEB_CS);
360}
361
Holger Schurig27590d02007-10-03 17:25:41 -0700362/*
363 * Get the command result out of the card.
364 */
Holger Schurig69f90322007-11-23 15:43:44 +0100365static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
Holger Schurig27590d02007-10-03 17:25:41 -0700366{
Holger Schurig7919b892008-04-01 14:50:43 +0200367 unsigned long flags;
Holger Schurig27590d02007-10-03 17:25:41 -0700368 int ret = -1;
Holger Schurig78cf0742008-06-02 09:25:05 +0200369 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700370
371 lbs_deb_enter(LBS_DEB_CS);
372
373 /* is hardware ready? */
Holger Schurig78cf0742008-06-02 09:25:05 +0200374 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
375 if ((status & IF_CS_BIT_RESP) == 0) {
376 lbs_pr_err("no cmd response in card\n");
377 *len = 0;
Holger Schurig27590d02007-10-03 17:25:41 -0700378 goto out;
379 }
380
Holger Schurig78cf0742008-06-02 09:25:05 +0200381 *len = if_cs_read16(priv->card, IF_CS_CARD_CMD_LEN);
Dan Williamsddac4522007-12-11 13:49:39 -0500382 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
Holger Schurig27590d02007-10-03 17:25:41 -0700383 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
384 goto out;
385 }
386
387 /* read even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200388 if_cs_read16_rep(priv->card, IF_CS_CARD_CMD, data, *len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700389 if (*len & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200390 data[*len-1] = if_cs_read8(priv->card, IF_CS_CARD_CMD);
Holger Schurig27590d02007-10-03 17:25:41 -0700391
Holger Schurig09d4fad2007-12-06 13:50:30 +0100392 /* This is a workaround for a firmware that reports too much
393 * bytes */
394 *len -= 8;
Holger Schurig27590d02007-10-03 17:25:41 -0700395 ret = 0;
Holger Schurig7919b892008-04-01 14:50:43 +0200396
397 /* Clear this flag again */
398 spin_lock_irqsave(&priv->driver_lock, flags);
399 priv->dnld_sent = DNLD_RES_RECEIVED;
400 spin_unlock_irqrestore(&priv->driver_lock, flags);
401
Holger Schurig27590d02007-10-03 17:25:41 -0700402out:
403 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
404 return ret;
405}
406
Holger Schurig69f90322007-11-23 15:43:44 +0100407static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
Holger Schurig27590d02007-10-03 17:25:41 -0700408{
409 struct sk_buff *skb = NULL;
410 u16 len;
411 u8 *data;
412
413 lbs_deb_enter(LBS_DEB_CS);
414
Holger Schurig78cf0742008-06-02 09:25:05 +0200415 len = if_cs_read16(priv->card, IF_CS_READ_LEN);
Holger Schurig27590d02007-10-03 17:25:41 -0700416 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
417 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
418 priv->stats.rx_dropped++;
Holger Schurig27590d02007-10-03 17:25:41 -0700419 goto dat_err;
420 }
421
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400422 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700423 if (!skb)
424 goto out;
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400425 skb_put(skb, len);
426 skb_reserve(skb, 2);/* 16 byte align */
427 data = skb->data;
Holger Schurig27590d02007-10-03 17:25:41 -0700428
429 /* read even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200430 if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700431 if (len & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200432 data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
Holger Schurig27590d02007-10-03 17:25:41 -0700433
434dat_err:
Holger Schurig78cf0742008-06-02 09:25:05 +0200435 if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
436 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
Holger Schurig27590d02007-10-03 17:25:41 -0700437
438out:
439 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
440 return skb;
441}
442
Holger Schurig7919b892008-04-01 14:50:43 +0200443static irqreturn_t if_cs_interrupt(int irq, void *data)
444{
445 struct if_cs_card *card = data;
446 struct lbs_private *priv = card->priv;
447 u16 cause;
448
449 lbs_deb_enter(LBS_DEB_CS);
450
Holger Schurig43d01c52008-05-26 12:50:23 +0200451 /* Ask card interrupt cause register if there is something for us */
Holger Schurig78cf0742008-06-02 09:25:05 +0200452 cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
Holger Schurig30735562008-06-05 13:06:15 +0200453 lbs_deb_cs("cause 0x%04x\n", cause);
454
Holger Schurig7919b892008-04-01 14:50:43 +0200455 if (cause == 0) {
456 /* Not for us */
457 return IRQ_NONE;
458 }
459
460 if (cause == 0xffff) {
461 /* Read in junk, the card has probably been removed */
462 card->priv->surpriseremoved = 1;
463 return IRQ_HANDLED;
464 }
465
Holger Schurig78cf0742008-06-02 09:25:05 +0200466 if (cause & IF_CS_BIT_RX) {
Holger Schurig7919b892008-04-01 14:50:43 +0200467 struct sk_buff *skb;
468 lbs_deb_cs("rx packet\n");
469 skb = if_cs_receive_data(priv);
470 if (skb)
471 lbs_process_rxed_packet(priv, skb);
472 }
473
Holger Schurig78cf0742008-06-02 09:25:05 +0200474 if (cause & IF_CS_BIT_TX) {
Holger Schurig43d01c52008-05-26 12:50:23 +0200475 lbs_deb_cs("tx done\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200476 lbs_host_to_card_done(priv);
477 }
478
Holger Schurig78cf0742008-06-02 09:25:05 +0200479 if (cause & IF_CS_BIT_RESP) {
Holger Schurig7919b892008-04-01 14:50:43 +0200480 unsigned long flags;
481 u8 i;
482
Holger Schurig43d01c52008-05-26 12:50:23 +0200483 lbs_deb_cs("cmd resp\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200484 spin_lock_irqsave(&priv->driver_lock, flags);
485 i = (priv->resp_idx == 0) ? 1 : 0;
486 spin_unlock_irqrestore(&priv->driver_lock, flags);
487
488 BUG_ON(priv->resp_len[i]);
489 if_cs_receive_cmdres(priv, priv->resp_buf[i],
490 &priv->resp_len[i]);
491
492 spin_lock_irqsave(&priv->driver_lock, flags);
493 lbs_notify_command_response(priv, i);
494 spin_unlock_irqrestore(&priv->driver_lock, flags);
495 }
496
Holger Schurig78cf0742008-06-02 09:25:05 +0200497 if (cause & IF_CS_BIT_EVENT) {
498 u16 event = if_cs_read16(priv->card, IF_CS_CARD_STATUS)
499 & IF_CS_CARD_STATUS_MASK;
500 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
501 IF_CS_BIT_EVENT);
Holger Schurig43d01c52008-05-26 12:50:23 +0200502 lbs_deb_cs("host event 0x%04x\n", event);
Holger Schurig7919b892008-04-01 14:50:43 +0200503 lbs_queue_event(priv, event >> 8 & 0xff);
504 }
505
Holger Schurig30735562008-06-05 13:06:15 +0200506 /* Clear interrupt cause */
507 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
508
Holger Schurig43d01c52008-05-26 12:50:23 +0200509 lbs_deb_leave(LBS_DEB_CS);
Holger Schurig7919b892008-04-01 14:50:43 +0200510 return IRQ_HANDLED;
511}
512
513
514
515
516/********************************************************************/
Holger Schurig27590d02007-10-03 17:25:41 -0700517/* Firmware */
518/********************************************************************/
519
520/*
521 * Tries to program the helper firmware.
522 *
523 * Return 0 on success
524 */
525static int if_cs_prog_helper(struct if_cs_card *card)
526{
527 int ret = 0;
528 int sent = 0;
529 u8 scratch;
530 const struct firmware *fw;
531
532 lbs_deb_enter(LBS_DEB_CS);
533
534 scratch = if_cs_read8(card, IF_CS_SCRATCH);
535
536 /* "If the value is 0x5a, the firmware is already
537 * downloaded successfully"
538 */
Holger Schurig53143252008-06-05 13:07:04 +0200539 if (scratch == IF_CS_SCRATCH_HELPER_OK)
Holger Schurig27590d02007-10-03 17:25:41 -0700540 goto done;
541
542 /* "If the value is != 00, it is invalid value of register */
Holger Schurig53143252008-06-05 13:07:04 +0200543 if (scratch != IF_CS_SCRATCH_BOOT_OK) {
Holger Schurig27590d02007-10-03 17:25:41 -0700544 ret = -ENODEV;
545 goto done;
546 }
547
548 /* TODO: make firmware file configurable */
549 ret = request_firmware(&fw, "libertas_cs_helper.fw",
550 &handle_to_dev(card->p_dev));
551 if (ret) {
552 lbs_pr_err("can't load helper firmware\n");
553 ret = -ENODEV;
554 goto done;
555 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700556 lbs_deb_cs("helper size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700557
558 /* "Set the 5 bytes of the helper image to 0" */
559 /* Not needed, this contains an ARM branch instruction */
560
561 for (;;) {
562 /* "the number of bytes to send is 256" */
563 int count = 256;
564 int remain = fw->size - sent;
565
566 if (remain < count)
567 count = remain;
Holger Schurig27590d02007-10-03 17:25:41 -0700568
569 /* "write the number of bytes to be sent to the I/O Command
570 * write length register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200571 if_cs_write16(card, IF_CS_HOST_CMD_LEN, count);
Holger Schurig27590d02007-10-03 17:25:41 -0700572
573 /* "write this to I/O Command port register as 16 bit writes */
574 if (count)
Holger Schurig78cf0742008-06-02 09:25:05 +0200575 if_cs_write16_rep(card, IF_CS_HOST_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700576 &fw->data[sent],
577 count >> 1);
578
579 /* "Assert the download over interrupt command in the Host
580 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200581 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700582
583 /* "Assert the download over interrupt command in the Card
584 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200585 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700586
587 /* "The host polls the Card Status register ... for 50 ms before
588 declaring a failure */
Holger Schurig78cf0742008-06-02 09:25:05 +0200589 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
590 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700591 if (ret < 0) {
592 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
593 sent, ret);
594 goto done;
595 }
596
597 if (count == 0)
598 break;
599
600 sent += count;
601 }
602
603 release_firmware(fw);
604 ret = 0;
605
606done:
607 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
608 return ret;
609}
610
611
612static int if_cs_prog_real(struct if_cs_card *card)
613{
614 const struct firmware *fw;
615 int ret = 0;
616 int retry = 0;
617 int len = 0;
618 int sent;
619
620 lbs_deb_enter(LBS_DEB_CS);
621
622 /* TODO: make firmware file configurable */
623 ret = request_firmware(&fw, "libertas_cs.fw",
624 &handle_to_dev(card->p_dev));
625 if (ret) {
626 lbs_pr_err("can't load firmware\n");
627 ret = -ENODEV;
628 goto done;
629 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700630 lbs_deb_cs("fw size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700631
Holger Schurig78cf0742008-06-02 09:25:05 +0200632 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_SQ_READ_LOW,
633 IF_CS_CARD_SQ_HELPER_OK);
Holger Schurig27590d02007-10-03 17:25:41 -0700634 if (ret < 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700635 lbs_pr_err("helper firmware doesn't answer\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700636 goto err_release;
637 }
638
639 for (sent = 0; sent < fw->size; sent += len) {
Holger Schurig78cf0742008-06-02 09:25:05 +0200640 len = if_cs_read16(card, IF_CS_CARD_SQ_READ_LOW);
Holger Schurig27590d02007-10-03 17:25:41 -0700641 if (len & 1) {
642 retry++;
643 lbs_pr_info("odd, need to retry this firmware block\n");
644 } else {
645 retry = 0;
646 }
647
648 if (retry > 20) {
649 lbs_pr_err("could not download firmware\n");
650 ret = -ENODEV;
651 goto err_release;
652 }
653 if (retry) {
654 sent -= len;
655 }
656
657
Holger Schurig78cf0742008-06-02 09:25:05 +0200658 if_cs_write16(card, IF_CS_HOST_CMD_LEN, len);
Holger Schurig27590d02007-10-03 17:25:41 -0700659
Holger Schurig78cf0742008-06-02 09:25:05 +0200660 if_cs_write16_rep(card, IF_CS_HOST_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700661 &fw->data[sent],
662 (len+1) >> 1);
Holger Schurig78cf0742008-06-02 09:25:05 +0200663 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
664 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700665
Holger Schurig78cf0742008-06-02 09:25:05 +0200666 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
667 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700668 if (ret < 0) {
669 lbs_pr_err("can't download firmware at 0x%x\n", sent);
670 goto err_release;
671 }
672 }
673
674 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
675 if (ret < 0) {
676 lbs_pr_err("firmware download failed\n");
677 goto err_release;
678 }
679
680 ret = 0;
681 goto done;
682
683
684err_release:
685 release_firmware(fw);
686
687done:
688 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
689 return ret;
690}
691
692
693
694/********************************************************************/
695/* Callback functions for libertas.ko */
696/********************************************************************/
697
Holger Schurig27590d02007-10-03 17:25:41 -0700698/* Send commands or data packets to the card */
Holger Schurig69f90322007-11-23 15:43:44 +0100699static int if_cs_host_to_card(struct lbs_private *priv,
700 u8 type,
701 u8 *buf,
702 u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700703{
704 int ret = -1;
705
706 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
707
708 switch (type) {
709 case MVMS_DAT:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400710 priv->dnld_sent = DNLD_DATA_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700711 if_cs_send_data(priv, buf, nb);
712 ret = 0;
713 break;
714 case MVMS_CMD:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400715 priv->dnld_sent = DNLD_CMD_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700716 ret = if_cs_send_cmd(priv, buf, nb);
717 break;
718 default:
719 lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
720 }
721
722 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
723 return ret;
724}
725
726
Holger Schurig27590d02007-10-03 17:25:41 -0700727/********************************************************************/
728/* Card Services */
729/********************************************************************/
730
731/*
732 * After a card is removed, if_cs_release() will unregister the
733 * device, and release the PCMCIA configuration. If the device is
734 * still open, this will be postponed until it is closed.
735 */
736static void if_cs_release(struct pcmcia_device *p_dev)
737{
738 struct if_cs_card *card = p_dev->priv;
739
740 lbs_deb_enter(LBS_DEB_CS);
741
Holger Schurig27590d02007-10-03 17:25:41 -0700742 free_irq(p_dev->irq.AssignedIRQ, card);
Holger Schurigfdfb92e2008-01-25 14:15:48 +0100743 pcmcia_disable_device(p_dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700744 if (card->iobase)
745 ioport_unmap(card->iobase);
746
747 lbs_deb_leave(LBS_DEB_CS);
748}
749
750
751/*
752 * This creates an "instance" of the driver, allocating local data
753 * structures for one device. The device is registered with Card
754 * Services.
755 *
756 * The dev_link structure is initialized, but we don't actually
757 * configure the card at this point -- we wait until we receive a card
758 * insertion event.
759 */
760static int if_cs_probe(struct pcmcia_device *p_dev)
761{
762 int ret = -ENOMEM;
Holger Schurig69f90322007-11-23 15:43:44 +0100763 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700764 struct if_cs_card *card;
765 /* CIS parsing */
766 tuple_t tuple;
767 cisparse_t parse;
768 cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
769 cistpl_io_t *io = &cfg->io;
770 u_char buf[64];
771
772 lbs_deb_enter(LBS_DEB_CS);
773
774 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
775 if (!card) {
776 lbs_pr_err("error in kzalloc\n");
777 goto out;
778 }
779 card->p_dev = p_dev;
780 p_dev->priv = card;
781
782 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
783 p_dev->irq.Handler = NULL;
784 p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
785
786 p_dev->conf.Attributes = 0;
787 p_dev->conf.IntType = INT_MEMORY_AND_IO;
788
789 tuple.Attributes = 0;
790 tuple.TupleData = buf;
791 tuple.TupleDataMax = sizeof(buf);
792 tuple.TupleOffset = 0;
793
794 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
795 if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
796 (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
797 (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
798 {
799 lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
800 goto out1;
801 }
802
803 p_dev->conf.ConfigIndex = cfg->index;
804
805 /* Do we need to allocate an interrupt? */
806 if (cfg->irq.IRQInfo1) {
807 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
808 }
809
810 /* IO window settings */
811 if (cfg->io.nwin != 1) {
812 lbs_pr_err("wrong CIS (check number of IO windows)\n");
813 ret = -ENODEV;
814 goto out1;
815 }
816 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
817 p_dev->io.BasePort1 = io->win[0].base;
818 p_dev->io.NumPorts1 = io->win[0].len;
819
820 /* This reserves IO space but doesn't actually enable it */
821 ret = pcmcia_request_io(p_dev, &p_dev->io);
822 if (ret) {
823 lbs_pr_err("error in pcmcia_request_io\n");
824 goto out1;
825 }
826
827 /*
828 * Allocate an interrupt line. Note that this does not assign
829 * a handler to the interrupt, unless the 'Handler' member of
830 * the irq structure is initialized.
831 */
832 if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
833 ret = pcmcia_request_irq(p_dev, &p_dev->irq);
834 if (ret) {
835 lbs_pr_err("error in pcmcia_request_irq\n");
836 goto out1;
837 }
838 }
839
840 /* Initialize io access */
841 card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
842 if (!card->iobase) {
843 lbs_pr_err("error in ioport_map\n");
844 ret = -EIO;
845 goto out1;
846 }
847
848 /*
849 * This actually configures the PCMCIA socket -- setting up
850 * the I/O windows and the interrupt mapping, and putting the
851 * card and host interface into "Memory and IO" mode.
852 */
853 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
854 if (ret) {
855 lbs_pr_err("error in pcmcia_request_configuration\n");
856 goto out2;
857 }
858
859 /* Finally, report what we've done */
860 lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
861 p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
862 p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
863
Holger Schurig27590d02007-10-03 17:25:41 -0700864
865 /* Load the firmware early, before calling into libertas.ko */
866 ret = if_cs_prog_helper(card);
867 if (ret == 0)
868 ret = if_cs_prog_real(card);
869 if (ret)
870 goto out2;
871
872 /* Make this card known to the libertas driver */
Holger Schurig10078322007-11-15 18:05:47 -0500873 priv = lbs_add_card(card, &p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700874 if (!priv) {
875 ret = -ENOMEM;
876 goto out2;
877 }
878
Holger Schurig7919b892008-04-01 14:50:43 +0200879 /* Finish setting up fields in lbs_private */
Dan Williams954ee162007-08-20 11:43:25 -0400880 card->priv = priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700881 priv->card = card;
Holger Schurig7919b892008-04-01 14:50:43 +0200882 priv->hw_host_to_card = if_cs_host_to_card;
David Woodhouseaa21c002007-12-08 20:04:36 +0000883 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400884
Holger Schurig27590d02007-10-03 17:25:41 -0700885 /* Now actually get the IRQ */
886 ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
887 IRQF_SHARED, DRV_NAME, card);
888 if (ret) {
889 lbs_pr_err("error in request_irq\n");
890 goto out3;
891 }
892
Holger Schurig4ef31702007-10-09 10:41:57 +0200893 /* Clear any interrupt cause that happend while sending
894 * firmware/initializing card */
Holger Schurig78cf0742008-06-02 09:25:05 +0200895 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400896 if_cs_enable_ints(card);
897
Holger Schurig27590d02007-10-03 17:25:41 -0700898 /* And finally bring the card up */
Holger Schurig10078322007-11-15 18:05:47 -0500899 if (lbs_start_card(priv) != 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700900 lbs_pr_err("could not activate card\n");
901 goto out3;
902 }
903
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100904 /* The firmware for the CF card supports powersave */
905 priv->ps_supported = 1;
906
Holger Schurig27590d02007-10-03 17:25:41 -0700907 ret = 0;
908 goto out;
909
910out3:
Holger Schurig10078322007-11-15 18:05:47 -0500911 lbs_remove_card(priv);
Holger Schurig27590d02007-10-03 17:25:41 -0700912out2:
913 ioport_unmap(card->iobase);
914out1:
915 pcmcia_disable_device(p_dev);
916out:
917 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
918 return ret;
919}
920
921
922/*
923 * This deletes a driver "instance". The device is de-registered with
924 * Card Services. If it has been released, all local data structures
925 * are freed. Otherwise, the structures will be freed when the device
926 * is released.
927 */
928static void if_cs_detach(struct pcmcia_device *p_dev)
929{
930 struct if_cs_card *card = p_dev->priv;
931
932 lbs_deb_enter(LBS_DEB_CS);
933
Holger Schurig10078322007-11-15 18:05:47 -0500934 lbs_stop_card(card->priv);
935 lbs_remove_card(card->priv);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400936 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700937 if_cs_release(p_dev);
938 kfree(card);
939
940 lbs_deb_leave(LBS_DEB_CS);
941}
942
943
944
945/********************************************************************/
946/* Module initialization */
947/********************************************************************/
948
949static struct pcmcia_device_id if_cs_ids[] = {
950 PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
951 PCMCIA_DEVICE_NULL,
952};
953MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
954
955
Holger Schurig10078322007-11-15 18:05:47 -0500956static struct pcmcia_driver lbs_driver = {
Holger Schurig27590d02007-10-03 17:25:41 -0700957 .owner = THIS_MODULE,
958 .drv = {
959 .name = DRV_NAME,
960 },
961 .probe = if_cs_probe,
962 .remove = if_cs_detach,
963 .id_table = if_cs_ids,
964};
965
966
967static int __init if_cs_init(void)
968{
969 int ret;
970
971 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500972 ret = pcmcia_register_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700973 lbs_deb_leave(LBS_DEB_CS);
974 return ret;
975}
976
977
978static void __exit if_cs_exit(void)
979{
980 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500981 pcmcia_unregister_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700982 lbs_deb_leave(LBS_DEB_CS);
983}
984
985
986module_init(if_cs_init);
987module_exit(if_cs_exit);