blob: a6fd70404c3d31793a2f6b27f05dd4151de04f25 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Holger Schurig27590d02007-10-03 17:25:41 -070026#include <linux/delay.h>
27#include <linux/moduleparam.h>
28#include <linux/firmware.h>
29#include <linux/netdevice.h>
30
31#include <pcmcia/cs_types.h>
32#include <pcmcia/cs.h>
33#include <pcmcia/cistpl.h>
34#include <pcmcia/ds.h>
35
36#include <linux/io.h>
37
38#define DRV_NAME "libertas_cs"
39
40#include "decl.h"
41#include "defs.h"
42#include "dev.h"
43
44
45/********************************************************************/
46/* Module stuff */
47/********************************************************************/
48
49MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
50MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
51MODULE_LICENSE("GPL");
52
53
54
55/********************************************************************/
56/* Data structures */
57/********************************************************************/
58
59struct if_cs_card {
60 struct pcmcia_device *p_dev;
Holger Schurig69f90322007-11-23 15:43:44 +010061 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -070062 void __iomem *iobase;
Marek Vasut9d453682009-08-21 04:08:16 +020063 bool align_regs;
Dan Williams82222e92010-08-07 21:15:19 -050064 u32 model;
Holger Schurig27590d02007-10-03 17:25:41 -070065};
66
67
Dan Williams82222e92010-08-07 21:15:19 -050068enum {
69 MODEL_UNKNOWN = 0x00,
70 MODEL_8305 = 0x01,
71 MODEL_8381 = 0x02,
72 MODEL_8385 = 0x03
73};
74
75static const struct lbs_fw_table fw_table[] = {
76 { MODEL_8305, "libertas/cf8305.bin", NULL },
77 { MODEL_8305, "libertas_cs_helper.fw", NULL },
78 { MODEL_8381, "libertas/cf8381_helper.bin", "libertas/cf8381.bin" },
79 { MODEL_8381, "libertas_cs_helper.fw", "libertas_cs.fw" },
80 { MODEL_8385, "libertas/cf8385_helper.bin", "libertas/cf8385.bin" },
81 { MODEL_8385, "libertas_cs_helper.fw", "libertas_cs.fw" },
82 { 0, NULL, NULL }
83};
84MODULE_FIRMWARE("libertas/cf8305.bin");
85MODULE_FIRMWARE("libertas/cf8381_helper.bin");
86MODULE_FIRMWARE("libertas/cf8381.bin");
87MODULE_FIRMWARE("libertas/cf8385_helper.bin");
88MODULE_FIRMWARE("libertas/cf8385.bin");
89MODULE_FIRMWARE("libertas_cs_helper.fw");
90MODULE_FIRMWARE("libertas_cs.fw");
91
Holger Schurig27590d02007-10-03 17:25:41 -070092
93/********************************************************************/
94/* Hardware access */
95/********************************************************************/
96
97/* This define enables wrapper functions which allow you
98 to dump all register accesses. You normally won't this,
99 except for development */
100/* #define DEBUG_IO */
101
102#ifdef DEBUG_IO
103static int debug_output = 0;
104#else
105/* This way the compiler optimizes the printk's away */
106#define debug_output 0
107#endif
108
109static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
110{
111 unsigned int val = ioread8(card->iobase + reg);
112 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200113 printk(KERN_INFO "inb %08x<%02x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700114 return val;
115}
116static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
117{
118 unsigned int val = ioread16(card->iobase + reg);
119 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200120 printk(KERN_INFO "inw %08x<%04x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700121 return val;
122}
123static inline void if_cs_read16_rep(
124 struct if_cs_card *card,
125 uint reg,
126 void *buf,
127 unsigned long count)
128{
129 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200130 printk(KERN_INFO "insw %08x<(0x%lx words)\n",
Holger Schurig27590d02007-10-03 17:25:41 -0700131 reg, count);
132 ioread16_rep(card->iobase + reg, buf, count);
133}
134
135static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
136{
137 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200138 printk(KERN_INFO "outb %08x>%02x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700139 iowrite8(val, card->iobase + reg);
140}
141
142static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
143{
144 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200145 printk(KERN_INFO "outw %08x>%04x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700146 iowrite16(val, card->iobase + reg);
147}
148
149static inline void if_cs_write16_rep(
150 struct if_cs_card *card,
151 uint reg,
David Woodhouse6dfff892008-05-23 18:37:51 +0100152 const void *buf,
Holger Schurig27590d02007-10-03 17:25:41 -0700153 unsigned long count)
154{
155 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200156 printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
Holger Schurig27590d02007-10-03 17:25:41 -0700157 reg, count);
158 iowrite16_rep(card->iobase + reg, buf, count);
159}
160
161
162/*
163 * I know that polling/delaying is frowned upon. However, this procedure
164 * with polling is needed while downloading the firmware. At this stage,
165 * the hardware does unfortunately not create any interrupts.
166 *
167 * Fortunately, this function is never used once the firmware is in
168 * the card. :-)
169 *
170 * As a reference, see the "Firmware Specification v5.1", page 18
171 * and 19. I did not follow their suggested timing to the word,
172 * but this works nice & fast anyway.
173 */
174static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
175{
176 int i;
177
Holger Schurig93416c872008-05-23 10:18:26 +0200178 for (i = 0; i < 100000; i++) {
Holger Schurig27590d02007-10-03 17:25:41 -0700179 u8 val = if_cs_read8(card, addr);
180 if (val == reg)
Dan Williams1fac36e2009-01-23 11:55:33 -0500181 return 0;
Holger Schurig93416c872008-05-23 10:18:26 +0200182 udelay(5);
Holger Schurig27590d02007-10-03 17:25:41 -0700183 }
184 return -ETIME;
185}
186
187
188
Holger Schurig53143252008-06-05 13:07:04 +0200189/*
190 * First the bitmasks for the host/card interrupt/status registers:
191 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200192#define IF_CS_BIT_TX 0x0001
193#define IF_CS_BIT_RX 0x0002
194#define IF_CS_BIT_COMMAND 0x0004
195#define IF_CS_BIT_RESP 0x0008
196#define IF_CS_BIT_EVENT 0x0010
197#define IF_CS_BIT_MASK 0x001f
Holger Schurig27590d02007-10-03 17:25:41 -0700198
Holger Schurig53143252008-06-05 13:07:04 +0200199
200
201/*
202 * It's not really clear to me what the host status register is for. It
203 * needs to be set almost in union with "host int cause". The following
204 * bits from above are used:
205 *
206 * IF_CS_BIT_TX driver downloaded a data packet
207 * IF_CS_BIT_RX driver got a data packet
208 * IF_CS_BIT_COMMAND driver downloaded a command
209 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
210 * IF_CS_BIT_EVENT driver read a host event
211 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200212#define IF_CS_HOST_STATUS 0x00000000
Holger Schurig27590d02007-10-03 17:25:41 -0700213
Holger Schurig53143252008-06-05 13:07:04 +0200214/*
215 * With the host int cause register can the host (that is, Linux) cause
216 * an interrupt in the firmware, to tell the firmware about those events:
217 *
218 * IF_CS_BIT_TX a data packet has been downloaded
219 * IF_CS_BIT_RX a received data packet has retrieved
220 * IF_CS_BIT_COMMAND a firmware block or a command has been downloaded
221 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
222 * IF_CS_BIT_EVENT a host event (link lost etc) has been retrieved
223 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200224#define IF_CS_HOST_INT_CAUSE 0x00000002
Holger Schurig27590d02007-10-03 17:25:41 -0700225
Holger Schurig53143252008-06-05 13:07:04 +0200226/*
227 * The host int mask register is used to enable/disable interrupt. However,
228 * I have the suspicion that disabled interrupts are lost.
229 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200230#define IF_CS_HOST_INT_MASK 0x00000004
Holger Schurig27590d02007-10-03 17:25:41 -0700231
Holger Schurig53143252008-06-05 13:07:04 +0200232/*
233 * Used to send or receive data packets:
234 */
Holger Schuriga78a83252008-06-05 13:09:50 +0200235#define IF_CS_WRITE 0x00000016
236#define IF_CS_WRITE_LEN 0x00000014
Holger Schurig78cf0742008-06-02 09:25:05 +0200237#define IF_CS_READ 0x00000010
238#define IF_CS_READ_LEN 0x00000024
Holger Schurig27590d02007-10-03 17:25:41 -0700239
Holger Schurig53143252008-06-05 13:07:04 +0200240/*
241 * Used to send commands (and to send firmware block) and to
242 * receive command responses:
243 */
Holger Schuriga78a83252008-06-05 13:09:50 +0200244#define IF_CS_CMD 0x0000001A
245#define IF_CS_CMD_LEN 0x00000018
246#define IF_CS_RESP 0x00000012
247#define IF_CS_RESP_LEN 0x00000030
Holger Schurig27590d02007-10-03 17:25:41 -0700248
Holger Schurig53143252008-06-05 13:07:04 +0200249/*
250 * The card status registers shows what the card/firmware actually
251 * accepts:
252 *
253 * IF_CS_BIT_TX you may send a data packet
254 * IF_CS_BIT_RX you may retrieve a data packet
255 * IF_CS_BIT_COMMAND you may send a command
256 * IF_CS_BIT_RESP you may retrieve a command response
257 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
258 *
259 * When reading this register several times, you will get back the same
260 * results --- with one exception: the IF_CS_BIT_EVENT clear itself
261 * automatically.
262 *
263 * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
264 * we handle this via the card int cause register.
265 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200266#define IF_CS_CARD_STATUS 0x00000020
267#define IF_CS_CARD_STATUS_MASK 0x7f00
Holger Schurig27590d02007-10-03 17:25:41 -0700268
Holger Schurig53143252008-06-05 13:07:04 +0200269/*
270 * The card int cause register is used by the card/firmware to notify us
271 * about the following events:
272 *
273 * IF_CS_BIT_TX a data packet has successfully been sentx
274 * IF_CS_BIT_RX a data packet has been received and can be retrieved
275 * IF_CS_BIT_COMMAND not used
276 * IF_CS_BIT_RESP the firmware has a command response for us
277 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
278 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200279#define IF_CS_CARD_INT_CAUSE 0x00000022
Holger Schurig27590d02007-10-03 17:25:41 -0700280
Holger Schurig53143252008-06-05 13:07:04 +0200281/*
282 * This is used to for handshaking with the card's bootloader/helper image
283 * to synchronize downloading of firmware blocks.
284 */
Holger Schuriga78a83252008-06-05 13:09:50 +0200285#define IF_CS_SQ_READ_LOW 0x00000028
286#define IF_CS_SQ_HELPER_OK 0x10
Holger Schurig27590d02007-10-03 17:25:41 -0700287
Holger Schurig53143252008-06-05 13:07:04 +0200288/*
289 * The scratch register tells us ...
290 *
291 * IF_CS_SCRATCH_BOOT_OK the bootloader runs
292 * IF_CS_SCRATCH_HELPER_OK the helper firmware already runs
293 */
Holger Schurig27590d02007-10-03 17:25:41 -0700294#define IF_CS_SCRATCH 0x0000003F
Holger Schurig53143252008-06-05 13:07:04 +0200295#define IF_CS_SCRATCH_BOOT_OK 0x00
296#define IF_CS_SCRATCH_HELPER_OK 0x5a
Holger Schurig27590d02007-10-03 17:25:41 -0700297
Holger Schurig4c555232008-06-05 13:08:35 +0200298/*
299 * Used to detect ancient chips:
300 */
301#define IF_CS_PRODUCT_ID 0x0000001C
302#define IF_CS_CF8385_B1_REV 0x12
Marek Vasutdc7d2432009-03-24 21:33:43 +0100303#define IF_CS_CF8381_B3_REV 0x04
Marek Vasut9d453682009-08-21 04:08:16 +0200304#define IF_CS_CF8305_B1_REV 0x03
Holger Schurig4c555232008-06-05 13:08:35 +0200305
Marek Vasutdc7d2432009-03-24 21:33:43 +0100306/*
307 * Used to detect other cards than CF8385 since their revisions of silicon
308 * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
309 */
Marek Vasut9d453682009-08-21 04:08:16 +0200310#define CF8305_MANFID 0x02db
311#define CF8305_CARDID 0x8103
Marek Vasutdc7d2432009-03-24 21:33:43 +0100312#define CF8381_MANFID 0x02db
313#define CF8381_CARDID 0x6064
314#define CF8385_MANFID 0x02df
315#define CF8385_CARDID 0x8103
316
Dan Williams82222e92010-08-07 21:15:19 -0500317/* FIXME: just use the 'driver_info' field of 'struct pcmcia_device_id' when
318 * that gets fixed. Currently there's no way to access it from the probe hook.
319 */
320static inline u32 get_model(u16 manf_id, u16 card_id)
Marek Vasut9d453682009-08-21 04:08:16 +0200321{
Dan Williams82222e92010-08-07 21:15:19 -0500322 /* NOTE: keep in sync with if_cs_ids */
323 if (manf_id == CF8305_MANFID && card_id == CF8305_CARDID)
324 return MODEL_8305;
325 else if (manf_id == CF8381_MANFID && card_id == CF8381_CARDID)
326 return MODEL_8381;
327 else if (manf_id == CF8385_MANFID && card_id == CF8385_CARDID)
328 return MODEL_8385;
329 return MODEL_UNKNOWN;
Marek Vasutdc7d2432009-03-24 21:33:43 +0100330}
Holger Schurig27590d02007-10-03 17:25:41 -0700331
332/********************************************************************/
Holger Schurig43d01c52008-05-26 12:50:23 +0200333/* I/O and interrupt handling */
Holger Schurig27590d02007-10-03 17:25:41 -0700334/********************************************************************/
335
Holger Schurig43d01c52008-05-26 12:50:23 +0200336static inline void if_cs_enable_ints(struct if_cs_card *card)
337{
338 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200339 if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
Holger Schurig43d01c52008-05-26 12:50:23 +0200340}
341
342static inline void if_cs_disable_ints(struct if_cs_card *card)
343{
344 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200345 if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
Holger Schurig43d01c52008-05-26 12:50:23 +0200346}
347
Holger Schurig27590d02007-10-03 17:25:41 -0700348/*
349 * Called from if_cs_host_to_card to send a command to the hardware
350 */
Holger Schurig69f90322007-11-23 15:43:44 +0100351static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700352{
353 struct if_cs_card *card = (struct if_cs_card *)priv->card;
354 int ret = -1;
355 int loops = 0;
356
357 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200358 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700359
360 /* Is hardware ready? */
361 while (1) {
Holger Schuriga78a83252008-06-05 13:09:50 +0200362 u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
363 if (status & IF_CS_BIT_COMMAND)
Holger Schurig27590d02007-10-03 17:25:41 -0700364 break;
365 if (++loops > 100) {
366 lbs_pr_err("card not ready for commands\n");
367 goto done;
368 }
369 mdelay(1);
370 }
371
Holger Schuriga78a83252008-06-05 13:09:50 +0200372 if_cs_write16(card, IF_CS_CMD_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700373
Holger Schuriga78a83252008-06-05 13:09:50 +0200374 if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700375 /* Are we supposed to transfer an odd amount of bytes? */
376 if (nb & 1)
Holger Schuriga78a83252008-06-05 13:09:50 +0200377 if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700378
379 /* "Assert the download over interrupt command in the Host
380 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200381 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700382
383 /* "Assert the download over interrupt command in the Card
384 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200385 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700386 ret = 0;
387
388done:
Holger Schurig43d01c52008-05-26 12:50:23 +0200389 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700390 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
391 return ret;
392}
393
Holger Schurig27590d02007-10-03 17:25:41 -0700394/*
395 * Called from if_cs_host_to_card to send a data to the hardware
396 */
Holger Schurig69f90322007-11-23 15:43:44 +0100397static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700398{
399 struct if_cs_card *card = (struct if_cs_card *)priv->card;
Holger Schurig43d01c52008-05-26 12:50:23 +0200400 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700401
402 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200403 if_cs_disable_ints(card);
404
Holger Schurig78cf0742008-06-02 09:25:05 +0200405 status = if_cs_read16(card, IF_CS_CARD_STATUS);
406 BUG_ON((status & IF_CS_BIT_TX) == 0);
Holger Schurig27590d02007-10-03 17:25:41 -0700407
Holger Schuriga78a83252008-06-05 13:09:50 +0200408 if_cs_write16(card, IF_CS_WRITE_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700409
410 /* write even number of bytes, then odd byte if necessary */
Holger Schuriga78a83252008-06-05 13:09:50 +0200411 if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700412 if (nb & 1)
Holger Schuriga78a83252008-06-05 13:09:50 +0200413 if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700414
Holger Schurig78cf0742008-06-02 09:25:05 +0200415 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
416 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
Holger Schurig43d01c52008-05-26 12:50:23 +0200417 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700418
419 lbs_deb_leave(LBS_DEB_CS);
420}
421
Holger Schurig27590d02007-10-03 17:25:41 -0700422/*
423 * Get the command result out of the card.
424 */
Holger Schurig69f90322007-11-23 15:43:44 +0100425static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
Holger Schurig27590d02007-10-03 17:25:41 -0700426{
Holger Schurig7919b892008-04-01 14:50:43 +0200427 unsigned long flags;
Holger Schurig27590d02007-10-03 17:25:41 -0700428 int ret = -1;
Holger Schurig78cf0742008-06-02 09:25:05 +0200429 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700430
431 lbs_deb_enter(LBS_DEB_CS);
432
433 /* is hardware ready? */
Holger Schurig78cf0742008-06-02 09:25:05 +0200434 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
435 if ((status & IF_CS_BIT_RESP) == 0) {
436 lbs_pr_err("no cmd response in card\n");
437 *len = 0;
Holger Schurig27590d02007-10-03 17:25:41 -0700438 goto out;
439 }
440
Holger Schuriga78a83252008-06-05 13:09:50 +0200441 *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
Dan Williamsddac4522007-12-11 13:49:39 -0500442 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
Holger Schurig27590d02007-10-03 17:25:41 -0700443 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
444 goto out;
445 }
446
447 /* read even number of bytes, then odd byte if necessary */
Holger Schuriga78a83252008-06-05 13:09:50 +0200448 if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700449 if (*len & 1)
Holger Schuriga78a83252008-06-05 13:09:50 +0200450 data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
Holger Schurig27590d02007-10-03 17:25:41 -0700451
Holger Schurig09d4fad2007-12-06 13:50:30 +0100452 /* This is a workaround for a firmware that reports too much
453 * bytes */
454 *len -= 8;
Holger Schurig27590d02007-10-03 17:25:41 -0700455 ret = 0;
Holger Schurig7919b892008-04-01 14:50:43 +0200456
457 /* Clear this flag again */
458 spin_lock_irqsave(&priv->driver_lock, flags);
459 priv->dnld_sent = DNLD_RES_RECEIVED;
460 spin_unlock_irqrestore(&priv->driver_lock, flags);
461
Holger Schurig27590d02007-10-03 17:25:41 -0700462out:
463 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
464 return ret;
465}
466
Holger Schurig69f90322007-11-23 15:43:44 +0100467static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
Holger Schurig27590d02007-10-03 17:25:41 -0700468{
469 struct sk_buff *skb = NULL;
470 u16 len;
471 u8 *data;
472
473 lbs_deb_enter(LBS_DEB_CS);
474
Holger Schurig78cf0742008-06-02 09:25:05 +0200475 len = if_cs_read16(priv->card, IF_CS_READ_LEN);
Holger Schurig27590d02007-10-03 17:25:41 -0700476 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
477 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000478 priv->dev->stats.rx_dropped++;
Holger Schurig27590d02007-10-03 17:25:41 -0700479 goto dat_err;
480 }
481
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400482 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700483 if (!skb)
484 goto out;
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400485 skb_put(skb, len);
486 skb_reserve(skb, 2);/* 16 byte align */
487 data = skb->data;
Holger Schurig27590d02007-10-03 17:25:41 -0700488
489 /* read even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200490 if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700491 if (len & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200492 data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
Holger Schurig27590d02007-10-03 17:25:41 -0700493
494dat_err:
Holger Schurig78cf0742008-06-02 09:25:05 +0200495 if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
496 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
Holger Schurig27590d02007-10-03 17:25:41 -0700497
498out:
499 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
500 return skb;
501}
502
Holger Schurig7919b892008-04-01 14:50:43 +0200503static irqreturn_t if_cs_interrupt(int irq, void *data)
504{
505 struct if_cs_card *card = data;
506 struct lbs_private *priv = card->priv;
507 u16 cause;
508
509 lbs_deb_enter(LBS_DEB_CS);
510
Holger Schurig43d01c52008-05-26 12:50:23 +0200511 /* Ask card interrupt cause register if there is something for us */
Holger Schurig78cf0742008-06-02 09:25:05 +0200512 cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
Holger Schurig30735562008-06-05 13:06:15 +0200513 lbs_deb_cs("cause 0x%04x\n", cause);
514
Holger Schurig7919b892008-04-01 14:50:43 +0200515 if (cause == 0) {
516 /* Not for us */
517 return IRQ_NONE;
518 }
519
520 if (cause == 0xffff) {
521 /* Read in junk, the card has probably been removed */
522 card->priv->surpriseremoved = 1;
523 return IRQ_HANDLED;
524 }
525
Holger Schurig78cf0742008-06-02 09:25:05 +0200526 if (cause & IF_CS_BIT_RX) {
Holger Schurig7919b892008-04-01 14:50:43 +0200527 struct sk_buff *skb;
528 lbs_deb_cs("rx packet\n");
529 skb = if_cs_receive_data(priv);
530 if (skb)
531 lbs_process_rxed_packet(priv, skb);
532 }
533
Holger Schurig78cf0742008-06-02 09:25:05 +0200534 if (cause & IF_CS_BIT_TX) {
Holger Schurig43d01c52008-05-26 12:50:23 +0200535 lbs_deb_cs("tx done\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200536 lbs_host_to_card_done(priv);
537 }
538
Holger Schurig78cf0742008-06-02 09:25:05 +0200539 if (cause & IF_CS_BIT_RESP) {
Holger Schurig7919b892008-04-01 14:50:43 +0200540 unsigned long flags;
541 u8 i;
542
Holger Schurig43d01c52008-05-26 12:50:23 +0200543 lbs_deb_cs("cmd resp\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200544 spin_lock_irqsave(&priv->driver_lock, flags);
545 i = (priv->resp_idx == 0) ? 1 : 0;
546 spin_unlock_irqrestore(&priv->driver_lock, flags);
547
548 BUG_ON(priv->resp_len[i]);
549 if_cs_receive_cmdres(priv, priv->resp_buf[i],
550 &priv->resp_len[i]);
551
552 spin_lock_irqsave(&priv->driver_lock, flags);
553 lbs_notify_command_response(priv, i);
554 spin_unlock_irqrestore(&priv->driver_lock, flags);
555 }
556
Holger Schurig78cf0742008-06-02 09:25:05 +0200557 if (cause & IF_CS_BIT_EVENT) {
Holger Schuriga78a83252008-06-05 13:09:50 +0200558 u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200559 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
560 IF_CS_BIT_EVENT);
Holger Schuriga78a83252008-06-05 13:09:50 +0200561 lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
Holger Schurig7919b892008-04-01 14:50:43 +0200562 }
563
Holger Schurig30735562008-06-05 13:06:15 +0200564 /* Clear interrupt cause */
565 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
566
Holger Schurig43d01c52008-05-26 12:50:23 +0200567 lbs_deb_leave(LBS_DEB_CS);
Holger Schurig7919b892008-04-01 14:50:43 +0200568 return IRQ_HANDLED;
569}
570
571
572
573
574/********************************************************************/
Holger Schurig27590d02007-10-03 17:25:41 -0700575/* Firmware */
576/********************************************************************/
577
578/*
579 * Tries to program the helper firmware.
580 *
581 * Return 0 on success
582 */
Dan Williams82222e92010-08-07 21:15:19 -0500583static int if_cs_prog_helper(struct if_cs_card *card, const struct firmware *fw)
Holger Schurig27590d02007-10-03 17:25:41 -0700584{
585 int ret = 0;
586 int sent = 0;
587 u8 scratch;
Holger Schurig27590d02007-10-03 17:25:41 -0700588
589 lbs_deb_enter(LBS_DEB_CS);
590
Marek Vasut9d453682009-08-21 04:08:16 +0200591 /*
592 * This is the only place where an unaligned register access happens on
593 * the CF8305 card, therefore for the sake of speed of the driver, we do
594 * the alignment correction here.
595 */
596 if (card->align_regs)
597 scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
598 else
599 scratch = if_cs_read8(card, IF_CS_SCRATCH);
Holger Schurig27590d02007-10-03 17:25:41 -0700600
601 /* "If the value is 0x5a, the firmware is already
602 * downloaded successfully"
603 */
Holger Schurig53143252008-06-05 13:07:04 +0200604 if (scratch == IF_CS_SCRATCH_HELPER_OK)
Holger Schurig27590d02007-10-03 17:25:41 -0700605 goto done;
606
607 /* "If the value is != 00, it is invalid value of register */
Holger Schurig53143252008-06-05 13:07:04 +0200608 if (scratch != IF_CS_SCRATCH_BOOT_OK) {
Holger Schurig27590d02007-10-03 17:25:41 -0700609 ret = -ENODEV;
610 goto done;
611 }
612
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700613 lbs_deb_cs("helper size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700614
615 /* "Set the 5 bytes of the helper image to 0" */
616 /* Not needed, this contains an ARM branch instruction */
617
618 for (;;) {
619 /* "the number of bytes to send is 256" */
620 int count = 256;
621 int remain = fw->size - sent;
622
623 if (remain < count)
624 count = remain;
Holger Schurig27590d02007-10-03 17:25:41 -0700625
626 /* "write the number of bytes to be sent to the I/O Command
627 * write length register" */
Holger Schuriga78a83252008-06-05 13:09:50 +0200628 if_cs_write16(card, IF_CS_CMD_LEN, count);
Holger Schurig27590d02007-10-03 17:25:41 -0700629
630 /* "write this to I/O Command port register as 16 bit writes */
631 if (count)
Holger Schuriga78a83252008-06-05 13:09:50 +0200632 if_cs_write16_rep(card, IF_CS_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700633 &fw->data[sent],
634 count >> 1);
635
636 /* "Assert the download over interrupt command in the Host
637 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200638 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700639
640 /* "Assert the download over interrupt command in the Card
641 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200642 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700643
644 /* "The host polls the Card Status register ... for 50 ms before
645 declaring a failure */
Holger Schurig78cf0742008-06-02 09:25:05 +0200646 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
647 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700648 if (ret < 0) {
649 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
650 sent, ret);
Dan Williams82222e92010-08-07 21:15:19 -0500651 goto done;
Holger Schurig27590d02007-10-03 17:25:41 -0700652 }
653
654 if (count == 0)
655 break;
656
657 sent += count;
658 }
659
Holger Schurig27590d02007-10-03 17:25:41 -0700660done:
661 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
662 return ret;
663}
664
665
Dan Williams82222e92010-08-07 21:15:19 -0500666static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
Holger Schurig27590d02007-10-03 17:25:41 -0700667{
Holger Schurig27590d02007-10-03 17:25:41 -0700668 int ret = 0;
669 int retry = 0;
670 int len = 0;
671 int sent;
672
673 lbs_deb_enter(LBS_DEB_CS);
674
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700675 lbs_deb_cs("fw size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700676
Holger Schuriga78a83252008-06-05 13:09:50 +0200677 ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
678 IF_CS_SQ_HELPER_OK);
Holger Schurig27590d02007-10-03 17:25:41 -0700679 if (ret < 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700680 lbs_pr_err("helper firmware doesn't answer\n");
Dan Williams82222e92010-08-07 21:15:19 -0500681 goto done;
Holger Schurig27590d02007-10-03 17:25:41 -0700682 }
683
684 for (sent = 0; sent < fw->size; sent += len) {
Holger Schuriga78a83252008-06-05 13:09:50 +0200685 len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
Holger Schurig27590d02007-10-03 17:25:41 -0700686 if (len & 1) {
687 retry++;
688 lbs_pr_info("odd, need to retry this firmware block\n");
689 } else {
690 retry = 0;
691 }
692
693 if (retry > 20) {
694 lbs_pr_err("could not download firmware\n");
695 ret = -ENODEV;
Dan Williams82222e92010-08-07 21:15:19 -0500696 goto done;
Holger Schurig27590d02007-10-03 17:25:41 -0700697 }
698 if (retry) {
699 sent -= len;
700 }
701
702
Holger Schuriga78a83252008-06-05 13:09:50 +0200703 if_cs_write16(card, IF_CS_CMD_LEN, len);
Holger Schurig27590d02007-10-03 17:25:41 -0700704
Holger Schuriga78a83252008-06-05 13:09:50 +0200705 if_cs_write16_rep(card, IF_CS_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700706 &fw->data[sent],
707 (len+1) >> 1);
Holger Schurig78cf0742008-06-02 09:25:05 +0200708 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
709 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700710
Holger Schurig78cf0742008-06-02 09:25:05 +0200711 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
712 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700713 if (ret < 0) {
714 lbs_pr_err("can't download firmware at 0x%x\n", sent);
Dan Williams82222e92010-08-07 21:15:19 -0500715 goto done;
Holger Schurig27590d02007-10-03 17:25:41 -0700716 }
717 }
718
719 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
Adrian Bunk9a520282008-08-28 01:05:08 +0300720 if (ret < 0)
Holger Schurig27590d02007-10-03 17:25:41 -0700721 lbs_pr_err("firmware download failed\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700722
Holger Schurig27590d02007-10-03 17:25:41 -0700723done:
724 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
725 return ret;
726}
727
728
729
730/********************************************************************/
731/* Callback functions for libertas.ko */
732/********************************************************************/
733
Holger Schurig27590d02007-10-03 17:25:41 -0700734/* Send commands or data packets to the card */
Holger Schurig69f90322007-11-23 15:43:44 +0100735static int if_cs_host_to_card(struct lbs_private *priv,
736 u8 type,
737 u8 *buf,
738 u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700739{
740 int ret = -1;
741
742 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
743
744 switch (type) {
745 case MVMS_DAT:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400746 priv->dnld_sent = DNLD_DATA_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700747 if_cs_send_data(priv, buf, nb);
748 ret = 0;
749 break;
750 case MVMS_CMD:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400751 priv->dnld_sent = DNLD_CMD_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700752 ret = if_cs_send_cmd(priv, buf, nb);
753 break;
754 default:
Harvey Harrisonc94c93d2008-07-28 23:01:34 -0700755 lbs_pr_err("%s: unsupported type %d\n", __func__, type);
Holger Schurig27590d02007-10-03 17:25:41 -0700756 }
757
758 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
759 return ret;
760}
761
762
Holger Schurig27590d02007-10-03 17:25:41 -0700763/********************************************************************/
764/* Card Services */
765/********************************************************************/
766
767/*
768 * After a card is removed, if_cs_release() will unregister the
769 * device, and release the PCMCIA configuration. If the device is
770 * still open, this will be postponed until it is closed.
771 */
772static void if_cs_release(struct pcmcia_device *p_dev)
773{
774 struct if_cs_card *card = p_dev->priv;
775
776 lbs_deb_enter(LBS_DEB_CS);
777
Dominik Brodowskieb141202010-03-07 12:21:16 +0100778 free_irq(p_dev->irq, card);
Holger Schurigfdfb92e2008-01-25 14:15:48 +0100779 pcmcia_disable_device(p_dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700780 if (card->iobase)
781 ioport_unmap(card->iobase);
782
783 lbs_deb_leave(LBS_DEB_CS);
784}
785
786
787/*
788 * This creates an "instance" of the driver, allocating local data
789 * structures for one device. The device is registered with Card
790 * Services.
791 *
792 * The dev_link structure is initialized, but we don't actually
793 * configure the card at this point -- we wait until we receive a card
794 * insertion event.
795 */
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200796
797static int if_cs_ioprobe(struct pcmcia_device *p_dev,
798 cistpl_cftable_entry_t *cfg,
799 cistpl_cftable_entry_t *dflt,
800 unsigned int vcc,
801 void *priv_data)
802{
803 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
804 p_dev->io.BasePort1 = cfg->io.win[0].base;
805 p_dev->io.NumPorts1 = cfg->io.win[0].len;
806
807 /* Do we need to allocate an interrupt? */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100808 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200809
810 /* IO window settings */
811 if (cfg->io.nwin != 1) {
812 lbs_pr_err("wrong CIS (check number of IO windows)\n");
813 return -ENODEV;
814 }
815
816 /* This reserves IO space but doesn't actually enable it */
817 return pcmcia_request_io(p_dev, &p_dev->io);
818}
819
Holger Schurig27590d02007-10-03 17:25:41 -0700820static int if_cs_probe(struct pcmcia_device *p_dev)
821{
822 int ret = -ENOMEM;
Marek Vasutdc7d2432009-03-24 21:33:43 +0100823 unsigned int prod_id;
Holger Schurig69f90322007-11-23 15:43:44 +0100824 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700825 struct if_cs_card *card;
Dan Williams82222e92010-08-07 21:15:19 -0500826 const struct firmware *helper = NULL;
827 const struct firmware *mainfw = NULL;
Holger Schurig27590d02007-10-03 17:25:41 -0700828
829 lbs_deb_enter(LBS_DEB_CS);
830
831 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
832 if (!card) {
833 lbs_pr_err("error in kzalloc\n");
834 goto out;
835 }
836 card->p_dev = p_dev;
837 p_dev->priv = card;
838
Holger Schurig27590d02007-10-03 17:25:41 -0700839 p_dev->conf.Attributes = 0;
840 p_dev->conf.IntType = INT_MEMORY_AND_IO;
841
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200842 if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
843 lbs_pr_err("error in pcmcia_loop_config\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700844 goto out1;
845 }
846
Holger Schurig27590d02007-10-03 17:25:41 -0700847 /*
848 * Allocate an interrupt line. Note that this does not assign
849 * a handler to the interrupt, unless the 'Handler' member of
850 * the irq structure is initialized.
851 */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100852 if (!p_dev->irq)
853 goto out1;
Holger Schurig27590d02007-10-03 17:25:41 -0700854
855 /* Initialize io access */
856 card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
857 if (!card->iobase) {
858 lbs_pr_err("error in ioport_map\n");
859 ret = -EIO;
860 goto out1;
861 }
862
863 /*
864 * This actually configures the PCMCIA socket -- setting up
865 * the I/O windows and the interrupt mapping, and putting the
866 * card and host interface into "Memory and IO" mode.
867 */
868 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
869 if (ret) {
870 lbs_pr_err("error in pcmcia_request_configuration\n");
871 goto out2;
872 }
873
874 /* Finally, report what we've done */
875 lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
Dominik Brodowskieb141202010-03-07 12:21:16 +0100876 p_dev->irq, p_dev->io.BasePort1,
Holger Schurig27590d02007-10-03 17:25:41 -0700877 p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
878
Marek Vasut9d453682009-08-21 04:08:16 +0200879 /*
880 * Most of the libertas cards can do unaligned register access, but some
881 * weird ones can not. That's especially true for the CF8305 card.
882 */
883 card->align_regs = 0;
884
Dan Williams82222e92010-08-07 21:15:19 -0500885 card->model = get_model(p_dev->manf_id, p_dev->card_id);
886 if (card->model == MODEL_UNKNOWN) {
887 lbs_pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
888 p_dev->manf_id, p_dev->card_id);
889 goto out2;
890 }
891
Holger Schurig4c555232008-06-05 13:08:35 +0200892 /* Check if we have a current silicon */
Marek Vasutdc7d2432009-03-24 21:33:43 +0100893 prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
Dan Williams82222e92010-08-07 21:15:19 -0500894 if (card->model == MODEL_8305) {
Marek Vasut9d453682009-08-21 04:08:16 +0200895 card->align_regs = 1;
896 if (prod_id < IF_CS_CF8305_B1_REV) {
Dan Williams82222e92010-08-07 21:15:19 -0500897 lbs_pr_err("8305 rev B0 and older are not supported\n");
Marek Vasut9d453682009-08-21 04:08:16 +0200898 ret = -ENODEV;
899 goto out2;
900 }
901 }
902
Dan Williams82222e92010-08-07 21:15:19 -0500903 if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
904 lbs_pr_err("8381 rev B2 and older are not supported\n");
Marek Vasutdc7d2432009-03-24 21:33:43 +0100905 ret = -ENODEV;
906 goto out2;
907 }
908
Dan Williams82222e92010-08-07 21:15:19 -0500909 if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
910 lbs_pr_err("8385 rev B0 and older are not supported\n");
Holger Schurig4c555232008-06-05 13:08:35 +0200911 ret = -ENODEV;
912 goto out2;
913 }
Holger Schurig27590d02007-10-03 17:25:41 -0700914
Dan Williams82222e92010-08-07 21:15:19 -0500915 ret = lbs_get_firmware(&p_dev->dev, NULL, NULL, card->model,
916 &fw_table[0], &helper, &mainfw);
917 if (ret) {
918 lbs_pr_err("failed to find firmware (%d)\n", ret);
919 goto out2;
920 }
921
Holger Schurig27590d02007-10-03 17:25:41 -0700922 /* Load the firmware early, before calling into libertas.ko */
Dan Williams82222e92010-08-07 21:15:19 -0500923 ret = if_cs_prog_helper(card, helper);
924 if (ret == 0 && (card->model != MODEL_8305))
925 ret = if_cs_prog_real(card, mainfw);
Holger Schurig27590d02007-10-03 17:25:41 -0700926 if (ret)
927 goto out2;
928
929 /* Make this card known to the libertas driver */
Holger Schurig10078322007-11-15 18:05:47 -0500930 priv = lbs_add_card(card, &p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700931 if (!priv) {
932 ret = -ENOMEM;
933 goto out2;
934 }
935
Holger Schurig7919b892008-04-01 14:50:43 +0200936 /* Finish setting up fields in lbs_private */
Dan Williams954ee162007-08-20 11:43:25 -0400937 card->priv = priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700938 priv->card = card;
Holger Schurig7919b892008-04-01 14:50:43 +0200939 priv->hw_host_to_card = if_cs_host_to_card;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700940 priv->enter_deep_sleep = NULL;
941 priv->exit_deep_sleep = NULL;
942 priv->reset_deep_sleep_wakeup = NULL;
David Woodhouseaa21c002007-12-08 20:04:36 +0000943 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400944
Holger Schurig27590d02007-10-03 17:25:41 -0700945 /* Now actually get the IRQ */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100946 ret = request_irq(p_dev->irq, if_cs_interrupt,
Holger Schurig27590d02007-10-03 17:25:41 -0700947 IRQF_SHARED, DRV_NAME, card);
948 if (ret) {
949 lbs_pr_err("error in request_irq\n");
950 goto out3;
951 }
952
Holger Schurig4ef31702007-10-09 10:41:57 +0200953 /* Clear any interrupt cause that happend while sending
954 * firmware/initializing card */
Holger Schurig78cf0742008-06-02 09:25:05 +0200955 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400956 if_cs_enable_ints(card);
957
Holger Schurig27590d02007-10-03 17:25:41 -0700958 /* And finally bring the card up */
Holger Schurig10078322007-11-15 18:05:47 -0500959 if (lbs_start_card(priv) != 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700960 lbs_pr_err("could not activate card\n");
961 goto out3;
962 }
963
964 ret = 0;
965 goto out;
966
967out3:
Holger Schurig10078322007-11-15 18:05:47 -0500968 lbs_remove_card(priv);
Holger Schurig27590d02007-10-03 17:25:41 -0700969out2:
970 ioport_unmap(card->iobase);
971out1:
972 pcmcia_disable_device(p_dev);
973out:
Dan Williams82222e92010-08-07 21:15:19 -0500974 if (helper)
975 release_firmware(helper);
976 if (mainfw)
977 release_firmware(mainfw);
978
Holger Schurig27590d02007-10-03 17:25:41 -0700979 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
980 return ret;
981}
982
983
984/*
985 * This deletes a driver "instance". The device is de-registered with
986 * Card Services. If it has been released, all local data structures
987 * are freed. Otherwise, the structures will be freed when the device
988 * is released.
989 */
990static void if_cs_detach(struct pcmcia_device *p_dev)
991{
992 struct if_cs_card *card = p_dev->priv;
993
994 lbs_deb_enter(LBS_DEB_CS);
995
Holger Schurig10078322007-11-15 18:05:47 -0500996 lbs_stop_card(card->priv);
997 lbs_remove_card(card->priv);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400998 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700999 if_cs_release(p_dev);
1000 kfree(card);
1001
1002 lbs_deb_leave(LBS_DEB_CS);
1003}
1004
1005
1006
1007/********************************************************************/
1008/* Module initialization */
1009/********************************************************************/
1010
1011static struct pcmcia_device_id if_cs_ids[] = {
Marek Vasut9d453682009-08-21 04:08:16 +02001012 PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
Marek Vasutdc7d2432009-03-24 21:33:43 +01001013 PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
1014 PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
Dan Williams82222e92010-08-07 21:15:19 -05001015 /* NOTE: keep in sync with get_model() */
Holger Schurig27590d02007-10-03 17:25:41 -07001016 PCMCIA_DEVICE_NULL,
1017};
1018MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
1019
1020
Holger Schurig10078322007-11-15 18:05:47 -05001021static struct pcmcia_driver lbs_driver = {
Holger Schurig27590d02007-10-03 17:25:41 -07001022 .owner = THIS_MODULE,
1023 .drv = {
1024 .name = DRV_NAME,
1025 },
1026 .probe = if_cs_probe,
1027 .remove = if_cs_detach,
1028 .id_table = if_cs_ids,
1029};
1030
1031
1032static int __init if_cs_init(void)
1033{
1034 int ret;
1035
1036 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -05001037 ret = pcmcia_register_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -07001038 lbs_deb_leave(LBS_DEB_CS);
1039 return ret;
1040}
1041
1042
1043static void __exit if_cs_exit(void)
1044{
1045 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -05001046 pcmcia_unregister_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -07001047 lbs_deb_leave(LBS_DEB_CS);
1048}
1049
1050
1051module_init(if_cs_init);
1052module_exit(if_cs_exit);