blob: 9d6ca7fe9539c695f9885be8bfae8aa9a016905d [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
Holger Schurig4c555232008-06-05 13:08:35 +0200271/*
272 * Used to detect ancient chips:
273 */
274#define IF_CS_PRODUCT_ID 0x0000001C
275#define IF_CS_CF8385_B1_REV 0x12
276
Holger Schurig27590d02007-10-03 17:25:41 -0700277
278/********************************************************************/
Holger Schurig43d01c52008-05-26 12:50:23 +0200279/* I/O and interrupt handling */
Holger Schurig27590d02007-10-03 17:25:41 -0700280/********************************************************************/
281
Holger Schurig43d01c52008-05-26 12:50:23 +0200282static inline void if_cs_enable_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, 0);
Holger Schurig43d01c52008-05-26 12:50:23 +0200286}
287
288static inline void if_cs_disable_ints(struct if_cs_card *card)
289{
290 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200291 if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
Holger Schurig43d01c52008-05-26 12:50:23 +0200292}
293
Holger Schurig27590d02007-10-03 17:25:41 -0700294/*
295 * Called from if_cs_host_to_card to send a command to the hardware
296 */
Holger Schurig69f90322007-11-23 15:43:44 +0100297static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700298{
299 struct if_cs_card *card = (struct if_cs_card *)priv->card;
300 int ret = -1;
301 int loops = 0;
302
303 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200304 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700305
306 /* Is hardware ready? */
307 while (1) {
Holger Schurig78cf0742008-06-02 09:25:05 +0200308 u16 val = if_cs_read16(card, IF_CS_CARD_STATUS);
309 if (val & IF_CS_BIT_COMMAND)
Holger Schurig27590d02007-10-03 17:25:41 -0700310 break;
311 if (++loops > 100) {
312 lbs_pr_err("card not ready for commands\n");
313 goto done;
314 }
315 mdelay(1);
316 }
317
Holger Schurig78cf0742008-06-02 09:25:05 +0200318 if_cs_write16(card, IF_CS_HOST_CMD_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700319
Holger Schurig78cf0742008-06-02 09:25:05 +0200320 if_cs_write16_rep(card, IF_CS_HOST_CMD, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700321 /* Are we supposed to transfer an odd amount of bytes? */
322 if (nb & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200323 if_cs_write8(card, IF_CS_HOST_CMD, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700324
325 /* "Assert the download over interrupt command in the Host
326 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200327 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700328
329 /* "Assert the download over interrupt command in the Card
330 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200331 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700332 ret = 0;
333
334done:
Holger Schurig43d01c52008-05-26 12:50:23 +0200335 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700336 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
337 return ret;
338}
339
Holger Schurig27590d02007-10-03 17:25:41 -0700340/*
341 * Called from if_cs_host_to_card to send a data to the hardware
342 */
Holger Schurig69f90322007-11-23 15:43:44 +0100343static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700344{
345 struct if_cs_card *card = (struct if_cs_card *)priv->card;
Holger Schurig43d01c52008-05-26 12:50:23 +0200346 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700347
348 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200349 if_cs_disable_ints(card);
350
Holger Schurig78cf0742008-06-02 09:25:05 +0200351 status = if_cs_read16(card, IF_CS_CARD_STATUS);
352 BUG_ON((status & IF_CS_BIT_TX) == 0);
Holger Schurig27590d02007-10-03 17:25:41 -0700353
Holger Schurig78cf0742008-06-02 09:25:05 +0200354 if_cs_write16(card, IF_CS_HOST_WRITE_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700355
356 /* write even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200357 if_cs_write16_rep(card, IF_CS_HOST_WRITE, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700358 if (nb & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200359 if_cs_write8(card, IF_CS_HOST_WRITE, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700360
Holger Schurig78cf0742008-06-02 09:25:05 +0200361 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
362 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
Holger Schurig43d01c52008-05-26 12:50:23 +0200363 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700364
365 lbs_deb_leave(LBS_DEB_CS);
366}
367
Holger Schurig27590d02007-10-03 17:25:41 -0700368/*
369 * Get the command result out of the card.
370 */
Holger Schurig69f90322007-11-23 15:43:44 +0100371static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
Holger Schurig27590d02007-10-03 17:25:41 -0700372{
Holger Schurig7919b892008-04-01 14:50:43 +0200373 unsigned long flags;
Holger Schurig27590d02007-10-03 17:25:41 -0700374 int ret = -1;
Holger Schurig78cf0742008-06-02 09:25:05 +0200375 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700376
377 lbs_deb_enter(LBS_DEB_CS);
378
379 /* is hardware ready? */
Holger Schurig78cf0742008-06-02 09:25:05 +0200380 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
381 if ((status & IF_CS_BIT_RESP) == 0) {
382 lbs_pr_err("no cmd response in card\n");
383 *len = 0;
Holger Schurig27590d02007-10-03 17:25:41 -0700384 goto out;
385 }
386
Holger Schurig78cf0742008-06-02 09:25:05 +0200387 *len = if_cs_read16(priv->card, IF_CS_CARD_CMD_LEN);
Dan Williamsddac4522007-12-11 13:49:39 -0500388 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
Holger Schurig27590d02007-10-03 17:25:41 -0700389 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
390 goto out;
391 }
392
393 /* read even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200394 if_cs_read16_rep(priv->card, IF_CS_CARD_CMD, data, *len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700395 if (*len & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200396 data[*len-1] = if_cs_read8(priv->card, IF_CS_CARD_CMD);
Holger Schurig27590d02007-10-03 17:25:41 -0700397
Holger Schurig09d4fad2007-12-06 13:50:30 +0100398 /* This is a workaround for a firmware that reports too much
399 * bytes */
400 *len -= 8;
Holger Schurig27590d02007-10-03 17:25:41 -0700401 ret = 0;
Holger Schurig7919b892008-04-01 14:50:43 +0200402
403 /* Clear this flag again */
404 spin_lock_irqsave(&priv->driver_lock, flags);
405 priv->dnld_sent = DNLD_RES_RECEIVED;
406 spin_unlock_irqrestore(&priv->driver_lock, flags);
407
Holger Schurig27590d02007-10-03 17:25:41 -0700408out:
409 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
410 return ret;
411}
412
Holger Schurig69f90322007-11-23 15:43:44 +0100413static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
Holger Schurig27590d02007-10-03 17:25:41 -0700414{
415 struct sk_buff *skb = NULL;
416 u16 len;
417 u8 *data;
418
419 lbs_deb_enter(LBS_DEB_CS);
420
Holger Schurig78cf0742008-06-02 09:25:05 +0200421 len = if_cs_read16(priv->card, IF_CS_READ_LEN);
Holger Schurig27590d02007-10-03 17:25:41 -0700422 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
423 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
424 priv->stats.rx_dropped++;
Holger Schurig27590d02007-10-03 17:25:41 -0700425 goto dat_err;
426 }
427
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400428 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700429 if (!skb)
430 goto out;
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400431 skb_put(skb, len);
432 skb_reserve(skb, 2);/* 16 byte align */
433 data = skb->data;
Holger Schurig27590d02007-10-03 17:25:41 -0700434
435 /* read even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200436 if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700437 if (len & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200438 data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
Holger Schurig27590d02007-10-03 17:25:41 -0700439
440dat_err:
Holger Schurig78cf0742008-06-02 09:25:05 +0200441 if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
442 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
Holger Schurig27590d02007-10-03 17:25:41 -0700443
444out:
445 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
446 return skb;
447}
448
Holger Schurig7919b892008-04-01 14:50:43 +0200449static irqreturn_t if_cs_interrupt(int irq, void *data)
450{
451 struct if_cs_card *card = data;
452 struct lbs_private *priv = card->priv;
453 u16 cause;
454
455 lbs_deb_enter(LBS_DEB_CS);
456
Holger Schurig43d01c52008-05-26 12:50:23 +0200457 /* Ask card interrupt cause register if there is something for us */
Holger Schurig78cf0742008-06-02 09:25:05 +0200458 cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
Holger Schurig30735562008-06-05 13:06:15 +0200459 lbs_deb_cs("cause 0x%04x\n", cause);
460
Holger Schurig7919b892008-04-01 14:50:43 +0200461 if (cause == 0) {
462 /* Not for us */
463 return IRQ_NONE;
464 }
465
466 if (cause == 0xffff) {
467 /* Read in junk, the card has probably been removed */
468 card->priv->surpriseremoved = 1;
469 return IRQ_HANDLED;
470 }
471
Holger Schurig78cf0742008-06-02 09:25:05 +0200472 if (cause & IF_CS_BIT_RX) {
Holger Schurig7919b892008-04-01 14:50:43 +0200473 struct sk_buff *skb;
474 lbs_deb_cs("rx packet\n");
475 skb = if_cs_receive_data(priv);
476 if (skb)
477 lbs_process_rxed_packet(priv, skb);
478 }
479
Holger Schurig78cf0742008-06-02 09:25:05 +0200480 if (cause & IF_CS_BIT_TX) {
Holger Schurig43d01c52008-05-26 12:50:23 +0200481 lbs_deb_cs("tx done\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200482 lbs_host_to_card_done(priv);
483 }
484
Holger Schurig78cf0742008-06-02 09:25:05 +0200485 if (cause & IF_CS_BIT_RESP) {
Holger Schurig7919b892008-04-01 14:50:43 +0200486 unsigned long flags;
487 u8 i;
488
Holger Schurig43d01c52008-05-26 12:50:23 +0200489 lbs_deb_cs("cmd resp\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200490 spin_lock_irqsave(&priv->driver_lock, flags);
491 i = (priv->resp_idx == 0) ? 1 : 0;
492 spin_unlock_irqrestore(&priv->driver_lock, flags);
493
494 BUG_ON(priv->resp_len[i]);
495 if_cs_receive_cmdres(priv, priv->resp_buf[i],
496 &priv->resp_len[i]);
497
498 spin_lock_irqsave(&priv->driver_lock, flags);
499 lbs_notify_command_response(priv, i);
500 spin_unlock_irqrestore(&priv->driver_lock, flags);
501 }
502
Holger Schurig78cf0742008-06-02 09:25:05 +0200503 if (cause & IF_CS_BIT_EVENT) {
504 u16 event = if_cs_read16(priv->card, IF_CS_CARD_STATUS)
505 & IF_CS_CARD_STATUS_MASK;
506 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
507 IF_CS_BIT_EVENT);
Holger Schurig43d01c52008-05-26 12:50:23 +0200508 lbs_deb_cs("host event 0x%04x\n", event);
Holger Schurig7919b892008-04-01 14:50:43 +0200509 lbs_queue_event(priv, event >> 8 & 0xff);
510 }
511
Holger Schurig30735562008-06-05 13:06:15 +0200512 /* Clear interrupt cause */
513 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
514
Holger Schurig43d01c52008-05-26 12:50:23 +0200515 lbs_deb_leave(LBS_DEB_CS);
Holger Schurig7919b892008-04-01 14:50:43 +0200516 return IRQ_HANDLED;
517}
518
519
520
521
522/********************************************************************/
Holger Schurig27590d02007-10-03 17:25:41 -0700523/* Firmware */
524/********************************************************************/
525
526/*
527 * Tries to program the helper firmware.
528 *
529 * Return 0 on success
530 */
531static int if_cs_prog_helper(struct if_cs_card *card)
532{
533 int ret = 0;
534 int sent = 0;
535 u8 scratch;
536 const struct firmware *fw;
537
538 lbs_deb_enter(LBS_DEB_CS);
539
540 scratch = if_cs_read8(card, IF_CS_SCRATCH);
541
542 /* "If the value is 0x5a, the firmware is already
543 * downloaded successfully"
544 */
Holger Schurig53143252008-06-05 13:07:04 +0200545 if (scratch == IF_CS_SCRATCH_HELPER_OK)
Holger Schurig27590d02007-10-03 17:25:41 -0700546 goto done;
547
548 /* "If the value is != 00, it is invalid value of register */
Holger Schurig53143252008-06-05 13:07:04 +0200549 if (scratch != IF_CS_SCRATCH_BOOT_OK) {
Holger Schurig27590d02007-10-03 17:25:41 -0700550 ret = -ENODEV;
551 goto done;
552 }
553
554 /* TODO: make firmware file configurable */
555 ret = request_firmware(&fw, "libertas_cs_helper.fw",
556 &handle_to_dev(card->p_dev));
557 if (ret) {
558 lbs_pr_err("can't load helper firmware\n");
559 ret = -ENODEV;
560 goto done;
561 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700562 lbs_deb_cs("helper size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700563
564 /* "Set the 5 bytes of the helper image to 0" */
565 /* Not needed, this contains an ARM branch instruction */
566
567 for (;;) {
568 /* "the number of bytes to send is 256" */
569 int count = 256;
570 int remain = fw->size - sent;
571
572 if (remain < count)
573 count = remain;
Holger Schurig27590d02007-10-03 17:25:41 -0700574
575 /* "write the number of bytes to be sent to the I/O Command
576 * write length register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200577 if_cs_write16(card, IF_CS_HOST_CMD_LEN, count);
Holger Schurig27590d02007-10-03 17:25:41 -0700578
579 /* "write this to I/O Command port register as 16 bit writes */
580 if (count)
Holger Schurig78cf0742008-06-02 09:25:05 +0200581 if_cs_write16_rep(card, IF_CS_HOST_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700582 &fw->data[sent],
583 count >> 1);
584
585 /* "Assert the download over interrupt command in the Host
586 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200587 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700588
589 /* "Assert the download over interrupt command in the Card
590 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200591 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700592
593 /* "The host polls the Card Status register ... for 50 ms before
594 declaring a failure */
Holger Schurig78cf0742008-06-02 09:25:05 +0200595 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
596 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700597 if (ret < 0) {
598 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
599 sent, ret);
600 goto done;
601 }
602
603 if (count == 0)
604 break;
605
606 sent += count;
607 }
608
609 release_firmware(fw);
610 ret = 0;
611
612done:
613 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
614 return ret;
615}
616
617
618static int if_cs_prog_real(struct if_cs_card *card)
619{
620 const struct firmware *fw;
621 int ret = 0;
622 int retry = 0;
623 int len = 0;
624 int sent;
625
626 lbs_deb_enter(LBS_DEB_CS);
627
628 /* TODO: make firmware file configurable */
629 ret = request_firmware(&fw, "libertas_cs.fw",
630 &handle_to_dev(card->p_dev));
631 if (ret) {
632 lbs_pr_err("can't load firmware\n");
633 ret = -ENODEV;
634 goto done;
635 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700636 lbs_deb_cs("fw size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700637
Holger Schurig78cf0742008-06-02 09:25:05 +0200638 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_SQ_READ_LOW,
639 IF_CS_CARD_SQ_HELPER_OK);
Holger Schurig27590d02007-10-03 17:25:41 -0700640 if (ret < 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700641 lbs_pr_err("helper firmware doesn't answer\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700642 goto err_release;
643 }
644
645 for (sent = 0; sent < fw->size; sent += len) {
Holger Schurig78cf0742008-06-02 09:25:05 +0200646 len = if_cs_read16(card, IF_CS_CARD_SQ_READ_LOW);
Holger Schurig27590d02007-10-03 17:25:41 -0700647 if (len & 1) {
648 retry++;
649 lbs_pr_info("odd, need to retry this firmware block\n");
650 } else {
651 retry = 0;
652 }
653
654 if (retry > 20) {
655 lbs_pr_err("could not download firmware\n");
656 ret = -ENODEV;
657 goto err_release;
658 }
659 if (retry) {
660 sent -= len;
661 }
662
663
Holger Schurig78cf0742008-06-02 09:25:05 +0200664 if_cs_write16(card, IF_CS_HOST_CMD_LEN, len);
Holger Schurig27590d02007-10-03 17:25:41 -0700665
Holger Schurig78cf0742008-06-02 09:25:05 +0200666 if_cs_write16_rep(card, IF_CS_HOST_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700667 &fw->data[sent],
668 (len+1) >> 1);
Holger Schurig78cf0742008-06-02 09:25:05 +0200669 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
670 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700671
Holger Schurig78cf0742008-06-02 09:25:05 +0200672 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
673 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700674 if (ret < 0) {
675 lbs_pr_err("can't download firmware at 0x%x\n", sent);
676 goto err_release;
677 }
678 }
679
680 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
681 if (ret < 0) {
682 lbs_pr_err("firmware download failed\n");
683 goto err_release;
684 }
685
686 ret = 0;
687 goto done;
688
689
690err_release:
691 release_firmware(fw);
692
693done:
694 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
695 return ret;
696}
697
698
699
700/********************************************************************/
701/* Callback functions for libertas.ko */
702/********************************************************************/
703
Holger Schurig27590d02007-10-03 17:25:41 -0700704/* Send commands or data packets to the card */
Holger Schurig69f90322007-11-23 15:43:44 +0100705static int if_cs_host_to_card(struct lbs_private *priv,
706 u8 type,
707 u8 *buf,
708 u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700709{
710 int ret = -1;
711
712 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
713
714 switch (type) {
715 case MVMS_DAT:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400716 priv->dnld_sent = DNLD_DATA_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700717 if_cs_send_data(priv, buf, nb);
718 ret = 0;
719 break;
720 case MVMS_CMD:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400721 priv->dnld_sent = DNLD_CMD_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700722 ret = if_cs_send_cmd(priv, buf, nb);
723 break;
724 default:
725 lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
726 }
727
728 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
729 return ret;
730}
731
732
Holger Schurig27590d02007-10-03 17:25:41 -0700733/********************************************************************/
734/* Card Services */
735/********************************************************************/
736
737/*
738 * After a card is removed, if_cs_release() will unregister the
739 * device, and release the PCMCIA configuration. If the device is
740 * still open, this will be postponed until it is closed.
741 */
742static void if_cs_release(struct pcmcia_device *p_dev)
743{
744 struct if_cs_card *card = p_dev->priv;
745
746 lbs_deb_enter(LBS_DEB_CS);
747
Holger Schurig27590d02007-10-03 17:25:41 -0700748 free_irq(p_dev->irq.AssignedIRQ, card);
Holger Schurigfdfb92e2008-01-25 14:15:48 +0100749 pcmcia_disable_device(p_dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700750 if (card->iobase)
751 ioport_unmap(card->iobase);
752
753 lbs_deb_leave(LBS_DEB_CS);
754}
755
756
757/*
758 * This creates an "instance" of the driver, allocating local data
759 * structures for one device. The device is registered with Card
760 * Services.
761 *
762 * The dev_link structure is initialized, but we don't actually
763 * configure the card at this point -- we wait until we receive a card
764 * insertion event.
765 */
766static int if_cs_probe(struct pcmcia_device *p_dev)
767{
768 int ret = -ENOMEM;
Holger Schurig69f90322007-11-23 15:43:44 +0100769 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700770 struct if_cs_card *card;
771 /* CIS parsing */
772 tuple_t tuple;
773 cisparse_t parse;
774 cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
775 cistpl_io_t *io = &cfg->io;
776 u_char buf[64];
777
778 lbs_deb_enter(LBS_DEB_CS);
779
780 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
781 if (!card) {
782 lbs_pr_err("error in kzalloc\n");
783 goto out;
784 }
785 card->p_dev = p_dev;
786 p_dev->priv = card;
787
788 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
789 p_dev->irq.Handler = NULL;
790 p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
791
792 p_dev->conf.Attributes = 0;
793 p_dev->conf.IntType = INT_MEMORY_AND_IO;
794
795 tuple.Attributes = 0;
796 tuple.TupleData = buf;
797 tuple.TupleDataMax = sizeof(buf);
798 tuple.TupleOffset = 0;
799
800 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
801 if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
802 (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
803 (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
804 {
805 lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
806 goto out1;
807 }
808
809 p_dev->conf.ConfigIndex = cfg->index;
810
811 /* Do we need to allocate an interrupt? */
812 if (cfg->irq.IRQInfo1) {
813 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
814 }
815
816 /* IO window settings */
817 if (cfg->io.nwin != 1) {
818 lbs_pr_err("wrong CIS (check number of IO windows)\n");
819 ret = -ENODEV;
820 goto out1;
821 }
822 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
823 p_dev->io.BasePort1 = io->win[0].base;
824 p_dev->io.NumPorts1 = io->win[0].len;
825
826 /* This reserves IO space but doesn't actually enable it */
827 ret = pcmcia_request_io(p_dev, &p_dev->io);
828 if (ret) {
829 lbs_pr_err("error in pcmcia_request_io\n");
830 goto out1;
831 }
832
833 /*
834 * Allocate an interrupt line. Note that this does not assign
835 * a handler to the interrupt, unless the 'Handler' member of
836 * the irq structure is initialized.
837 */
838 if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
839 ret = pcmcia_request_irq(p_dev, &p_dev->irq);
840 if (ret) {
841 lbs_pr_err("error in pcmcia_request_irq\n");
842 goto out1;
843 }
844 }
845
846 /* Initialize io access */
847 card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
848 if (!card->iobase) {
849 lbs_pr_err("error in ioport_map\n");
850 ret = -EIO;
851 goto out1;
852 }
853
854 /*
855 * This actually configures the PCMCIA socket -- setting up
856 * the I/O windows and the interrupt mapping, and putting the
857 * card and host interface into "Memory and IO" mode.
858 */
859 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
860 if (ret) {
861 lbs_pr_err("error in pcmcia_request_configuration\n");
862 goto out2;
863 }
864
865 /* Finally, report what we've done */
866 lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
867 p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
868 p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
869
Holger Schurig4c555232008-06-05 13:08:35 +0200870 /* Check if we have a current silicon */
871 if (if_cs_read8(card, IF_CS_PRODUCT_ID) < IF_CS_CF8385_B1_REV) {
872 lbs_pr_err("old chips like 8385 rev B1 aren't supported\n");
873 ret = -ENODEV;
874 goto out2;
875 }
Holger Schurig27590d02007-10-03 17:25:41 -0700876
877 /* Load the firmware early, before calling into libertas.ko */
878 ret = if_cs_prog_helper(card);
879 if (ret == 0)
880 ret = if_cs_prog_real(card);
881 if (ret)
882 goto out2;
883
884 /* Make this card known to the libertas driver */
Holger Schurig10078322007-11-15 18:05:47 -0500885 priv = lbs_add_card(card, &p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700886 if (!priv) {
887 ret = -ENOMEM;
888 goto out2;
889 }
890
Holger Schurig7919b892008-04-01 14:50:43 +0200891 /* Finish setting up fields in lbs_private */
Dan Williams954ee162007-08-20 11:43:25 -0400892 card->priv = priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700893 priv->card = card;
Holger Schurig7919b892008-04-01 14:50:43 +0200894 priv->hw_host_to_card = if_cs_host_to_card;
David Woodhouseaa21c002007-12-08 20:04:36 +0000895 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400896
Holger Schurig27590d02007-10-03 17:25:41 -0700897 /* Now actually get the IRQ */
898 ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
899 IRQF_SHARED, DRV_NAME, card);
900 if (ret) {
901 lbs_pr_err("error in request_irq\n");
902 goto out3;
903 }
904
Holger Schurig4ef31702007-10-09 10:41:57 +0200905 /* Clear any interrupt cause that happend while sending
906 * firmware/initializing card */
Holger Schurig78cf0742008-06-02 09:25:05 +0200907 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400908 if_cs_enable_ints(card);
909
Holger Schurig27590d02007-10-03 17:25:41 -0700910 /* And finally bring the card up */
Holger Schurig10078322007-11-15 18:05:47 -0500911 if (lbs_start_card(priv) != 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700912 lbs_pr_err("could not activate card\n");
913 goto out3;
914 }
915
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100916 /* The firmware for the CF card supports powersave */
917 priv->ps_supported = 1;
918
Holger Schurig27590d02007-10-03 17:25:41 -0700919 ret = 0;
920 goto out;
921
922out3:
Holger Schurig10078322007-11-15 18:05:47 -0500923 lbs_remove_card(priv);
Holger Schurig27590d02007-10-03 17:25:41 -0700924out2:
925 ioport_unmap(card->iobase);
926out1:
927 pcmcia_disable_device(p_dev);
928out:
929 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
930 return ret;
931}
932
933
934/*
935 * This deletes a driver "instance". The device is de-registered with
936 * Card Services. If it has been released, all local data structures
937 * are freed. Otherwise, the structures will be freed when the device
938 * is released.
939 */
940static void if_cs_detach(struct pcmcia_device *p_dev)
941{
942 struct if_cs_card *card = p_dev->priv;
943
944 lbs_deb_enter(LBS_DEB_CS);
945
Holger Schurig10078322007-11-15 18:05:47 -0500946 lbs_stop_card(card->priv);
947 lbs_remove_card(card->priv);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400948 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700949 if_cs_release(p_dev);
950 kfree(card);
951
952 lbs_deb_leave(LBS_DEB_CS);
953}
954
955
956
957/********************************************************************/
958/* Module initialization */
959/********************************************************************/
960
961static struct pcmcia_device_id if_cs_ids[] = {
962 PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
963 PCMCIA_DEVICE_NULL,
964};
965MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
966
967
Holger Schurig10078322007-11-15 18:05:47 -0500968static struct pcmcia_driver lbs_driver = {
Holger Schurig27590d02007-10-03 17:25:41 -0700969 .owner = THIS_MODULE,
970 .drv = {
971 .name = DRV_NAME,
972 },
973 .probe = if_cs_probe,
974 .remove = if_cs_detach,
975 .id_table = if_cs_ids,
976};
977
978
979static int __init if_cs_init(void)
980{
981 int ret;
982
983 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500984 ret = pcmcia_register_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700985 lbs_deb_leave(LBS_DEB_CS);
986 return ret;
987}
988
989
990static void __exit if_cs_exit(void)
991{
992 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500993 pcmcia_unregister_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700994 lbs_deb_leave(LBS_DEB_CS);
995}
996
997
998module_init(if_cs_init);
999module_exit(if_cs_exit);