blob: 01390158fafafa1f6e965266b0351caf5707f2d2 [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
Holger Schurig27590d02007-10-03 17:25:41 -070031#include <pcmcia/cistpl.h>
32#include <pcmcia/ds.h>
33
34#include <linux/io.h>
35
36#define DRV_NAME "libertas_cs"
37
38#include "decl.h"
39#include "defs.h"
40#include "dev.h"
41
42
43/********************************************************************/
44/* Module stuff */
45/********************************************************************/
46
47MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
48MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
49MODULE_LICENSE("GPL");
50
51
52
53/********************************************************************/
54/* Data structures */
55/********************************************************************/
56
57struct if_cs_card {
58 struct pcmcia_device *p_dev;
Holger Schurig69f90322007-11-23 15:43:44 +010059 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -070060 void __iomem *iobase;
Marek Vasut9d453682009-08-21 04:08:16 +020061 bool align_regs;
Dan Williams82222e92010-08-07 21:15:19 -050062 u32 model;
Holger Schurig27590d02007-10-03 17:25:41 -070063};
64
65
Dan Williams82222e92010-08-07 21:15:19 -050066enum {
67 MODEL_UNKNOWN = 0x00,
68 MODEL_8305 = 0x01,
69 MODEL_8381 = 0x02,
70 MODEL_8385 = 0x03
71};
72
73static const struct lbs_fw_table fw_table[] = {
74 { MODEL_8305, "libertas/cf8305.bin", NULL },
75 { MODEL_8305, "libertas_cs_helper.fw", NULL },
76 { MODEL_8381, "libertas/cf8381_helper.bin", "libertas/cf8381.bin" },
77 { MODEL_8381, "libertas_cs_helper.fw", "libertas_cs.fw" },
78 { MODEL_8385, "libertas/cf8385_helper.bin", "libertas/cf8385.bin" },
79 { MODEL_8385, "libertas_cs_helper.fw", "libertas_cs.fw" },
80 { 0, NULL, NULL }
81};
82MODULE_FIRMWARE("libertas/cf8305.bin");
83MODULE_FIRMWARE("libertas/cf8381_helper.bin");
84MODULE_FIRMWARE("libertas/cf8381.bin");
85MODULE_FIRMWARE("libertas/cf8385_helper.bin");
86MODULE_FIRMWARE("libertas/cf8385.bin");
87MODULE_FIRMWARE("libertas_cs_helper.fw");
88MODULE_FIRMWARE("libertas_cs.fw");
89
Holger Schurig27590d02007-10-03 17:25:41 -070090
91/********************************************************************/
92/* Hardware access */
93/********************************************************************/
94
95/* This define enables wrapper functions which allow you
96 to dump all register accesses. You normally won't this,
97 except for development */
98/* #define DEBUG_IO */
99
100#ifdef DEBUG_IO
101static int debug_output = 0;
102#else
103/* This way the compiler optimizes the printk's away */
104#define debug_output 0
105#endif
106
107static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
108{
109 unsigned int val = ioread8(card->iobase + reg);
110 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200111 printk(KERN_INFO "inb %08x<%02x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700112 return val;
113}
114static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
115{
116 unsigned int val = ioread16(card->iobase + reg);
117 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200118 printk(KERN_INFO "inw %08x<%04x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700119 return val;
120}
121static inline void if_cs_read16_rep(
122 struct if_cs_card *card,
123 uint reg,
124 void *buf,
125 unsigned long count)
126{
127 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200128 printk(KERN_INFO "insw %08x<(0x%lx words)\n",
Holger Schurig27590d02007-10-03 17:25:41 -0700129 reg, count);
130 ioread16_rep(card->iobase + reg, buf, count);
131}
132
133static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
134{
135 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200136 printk(KERN_INFO "outb %08x>%02x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700137 iowrite8(val, card->iobase + reg);
138}
139
140static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
141{
142 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200143 printk(KERN_INFO "outw %08x>%04x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700144 iowrite16(val, card->iobase + reg);
145}
146
147static inline void if_cs_write16_rep(
148 struct if_cs_card *card,
149 uint reg,
David Woodhouse6dfff892008-05-23 18:37:51 +0100150 const void *buf,
Holger Schurig27590d02007-10-03 17:25:41 -0700151 unsigned long count)
152{
153 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200154 printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
Holger Schurig27590d02007-10-03 17:25:41 -0700155 reg, count);
156 iowrite16_rep(card->iobase + reg, buf, count);
157}
158
159
160/*
161 * I know that polling/delaying is frowned upon. However, this procedure
162 * with polling is needed while downloading the firmware. At this stage,
163 * the hardware does unfortunately not create any interrupts.
164 *
165 * Fortunately, this function is never used once the firmware is in
166 * the card. :-)
167 *
168 * As a reference, see the "Firmware Specification v5.1", page 18
169 * and 19. I did not follow their suggested timing to the word,
170 * but this works nice & fast anyway.
171 */
172static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
173{
174 int i;
175
Holger Schurig93416c82008-05-23 10:18:26 +0200176 for (i = 0; i < 100000; i++) {
Holger Schurig27590d02007-10-03 17:25:41 -0700177 u8 val = if_cs_read8(card, addr);
178 if (val == reg)
Dan Williams1fac36e2009-01-23 11:55:33 -0500179 return 0;
Holger Schurig93416c82008-05-23 10:18:26 +0200180 udelay(5);
Holger Schurig27590d02007-10-03 17:25:41 -0700181 }
182 return -ETIME;
183}
184
185
186
Holger Schurig53143252008-06-05 13:07:04 +0200187/*
188 * First the bitmasks for the host/card interrupt/status registers:
189 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200190#define IF_CS_BIT_TX 0x0001
191#define IF_CS_BIT_RX 0x0002
192#define IF_CS_BIT_COMMAND 0x0004
193#define IF_CS_BIT_RESP 0x0008
194#define IF_CS_BIT_EVENT 0x0010
195#define IF_CS_BIT_MASK 0x001f
Holger Schurig27590d02007-10-03 17:25:41 -0700196
Holger Schurig53143252008-06-05 13:07:04 +0200197
198
199/*
200 * It's not really clear to me what the host status register is for. It
201 * needs to be set almost in union with "host int cause". The following
202 * bits from above are used:
203 *
204 * IF_CS_BIT_TX driver downloaded a data packet
205 * IF_CS_BIT_RX driver got a data packet
206 * IF_CS_BIT_COMMAND driver downloaded a command
207 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
208 * IF_CS_BIT_EVENT driver read a host event
209 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200210#define IF_CS_HOST_STATUS 0x00000000
Holger Schurig27590d02007-10-03 17:25:41 -0700211
Holger Schurig53143252008-06-05 13:07:04 +0200212/*
213 * With the host int cause register can the host (that is, Linux) cause
214 * an interrupt in the firmware, to tell the firmware about those events:
215 *
216 * IF_CS_BIT_TX a data packet has been downloaded
217 * IF_CS_BIT_RX a received data packet has retrieved
218 * IF_CS_BIT_COMMAND a firmware block or a command has been downloaded
219 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
220 * IF_CS_BIT_EVENT a host event (link lost etc) has been retrieved
221 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200222#define IF_CS_HOST_INT_CAUSE 0x00000002
Holger Schurig27590d02007-10-03 17:25:41 -0700223
Holger Schurig53143252008-06-05 13:07:04 +0200224/*
225 * The host int mask register is used to enable/disable interrupt. However,
226 * I have the suspicion that disabled interrupts are lost.
227 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200228#define IF_CS_HOST_INT_MASK 0x00000004
Holger Schurig27590d02007-10-03 17:25:41 -0700229
Holger Schurig53143252008-06-05 13:07:04 +0200230/*
231 * Used to send or receive data packets:
232 */
Holger Schuriga78a8322008-06-05 13:09:50 +0200233#define IF_CS_WRITE 0x00000016
234#define IF_CS_WRITE_LEN 0x00000014
Holger Schurig78cf0742008-06-02 09:25:05 +0200235#define IF_CS_READ 0x00000010
236#define IF_CS_READ_LEN 0x00000024
Holger Schurig27590d02007-10-03 17:25:41 -0700237
Holger Schurig53143252008-06-05 13:07:04 +0200238/*
239 * Used to send commands (and to send firmware block) and to
240 * receive command responses:
241 */
Holger Schuriga78a8322008-06-05 13:09:50 +0200242#define IF_CS_CMD 0x0000001A
243#define IF_CS_CMD_LEN 0x00000018
244#define IF_CS_RESP 0x00000012
245#define IF_CS_RESP_LEN 0x00000030
Holger Schurig27590d02007-10-03 17:25:41 -0700246
Holger Schurig53143252008-06-05 13:07:04 +0200247/*
248 * The card status registers shows what the card/firmware actually
249 * accepts:
250 *
251 * IF_CS_BIT_TX you may send a data packet
252 * IF_CS_BIT_RX you may retrieve a data packet
253 * IF_CS_BIT_COMMAND you may send a command
254 * IF_CS_BIT_RESP you may retrieve a command response
255 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
256 *
257 * When reading this register several times, you will get back the same
258 * results --- with one exception: the IF_CS_BIT_EVENT clear itself
259 * automatically.
260 *
261 * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
262 * we handle this via the card int cause register.
263 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200264#define IF_CS_CARD_STATUS 0x00000020
265#define IF_CS_CARD_STATUS_MASK 0x7f00
Holger Schurig27590d02007-10-03 17:25:41 -0700266
Holger Schurig53143252008-06-05 13:07:04 +0200267/*
268 * The card int cause register is used by the card/firmware to notify us
269 * about the following events:
270 *
271 * IF_CS_BIT_TX a data packet has successfully been sentx
272 * IF_CS_BIT_RX a data packet has been received and can be retrieved
273 * IF_CS_BIT_COMMAND not used
274 * IF_CS_BIT_RESP the firmware has a command response for us
275 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
276 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200277#define IF_CS_CARD_INT_CAUSE 0x00000022
Holger Schurig27590d02007-10-03 17:25:41 -0700278
Holger Schurig53143252008-06-05 13:07:04 +0200279/*
280 * This is used to for handshaking with the card's bootloader/helper image
281 * to synchronize downloading of firmware blocks.
282 */
Holger Schuriga78a8322008-06-05 13:09:50 +0200283#define IF_CS_SQ_READ_LOW 0x00000028
284#define IF_CS_SQ_HELPER_OK 0x10
Holger Schurig27590d02007-10-03 17:25:41 -0700285
Holger Schurig53143252008-06-05 13:07:04 +0200286/*
287 * The scratch register tells us ...
288 *
289 * IF_CS_SCRATCH_BOOT_OK the bootloader runs
290 * IF_CS_SCRATCH_HELPER_OK the helper firmware already runs
291 */
Holger Schurig27590d02007-10-03 17:25:41 -0700292#define IF_CS_SCRATCH 0x0000003F
Holger Schurig53143252008-06-05 13:07:04 +0200293#define IF_CS_SCRATCH_BOOT_OK 0x00
294#define IF_CS_SCRATCH_HELPER_OK 0x5a
Holger Schurig27590d02007-10-03 17:25:41 -0700295
Holger Schurig4c555232008-06-05 13:08:35 +0200296/*
297 * Used to detect ancient chips:
298 */
299#define IF_CS_PRODUCT_ID 0x0000001C
300#define IF_CS_CF8385_B1_REV 0x12
Marek Vasutdc7d2432009-03-24 21:33:43 +0100301#define IF_CS_CF8381_B3_REV 0x04
Marek Vasut9d453682009-08-21 04:08:16 +0200302#define IF_CS_CF8305_B1_REV 0x03
Holger Schurig4c555232008-06-05 13:08:35 +0200303
Marek Vasutdc7d2432009-03-24 21:33:43 +0100304/*
305 * Used to detect other cards than CF8385 since their revisions of silicon
306 * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
307 */
Marek Vasut9d453682009-08-21 04:08:16 +0200308#define CF8305_MANFID 0x02db
309#define CF8305_CARDID 0x8103
Marek Vasutdc7d2432009-03-24 21:33:43 +0100310#define CF8381_MANFID 0x02db
311#define CF8381_CARDID 0x6064
312#define CF8385_MANFID 0x02df
313#define CF8385_CARDID 0x8103
314
Dan Williams82222e92010-08-07 21:15:19 -0500315/* FIXME: just use the 'driver_info' field of 'struct pcmcia_device_id' when
316 * that gets fixed. Currently there's no way to access it from the probe hook.
317 */
318static inline u32 get_model(u16 manf_id, u16 card_id)
Marek Vasut9d453682009-08-21 04:08:16 +0200319{
Dan Williams82222e92010-08-07 21:15:19 -0500320 /* NOTE: keep in sync with if_cs_ids */
321 if (manf_id == CF8305_MANFID && card_id == CF8305_CARDID)
322 return MODEL_8305;
323 else if (manf_id == CF8381_MANFID && card_id == CF8381_CARDID)
324 return MODEL_8381;
325 else if (manf_id == CF8385_MANFID && card_id == CF8385_CARDID)
326 return MODEL_8385;
327 return MODEL_UNKNOWN;
Marek Vasutdc7d2432009-03-24 21:33:43 +0100328}
Holger Schurig27590d02007-10-03 17:25:41 -0700329
330/********************************************************************/
Holger Schurig43d01c52008-05-26 12:50:23 +0200331/* I/O and interrupt handling */
Holger Schurig27590d02007-10-03 17:25:41 -0700332/********************************************************************/
333
Holger Schurig43d01c52008-05-26 12:50:23 +0200334static inline void if_cs_enable_ints(struct if_cs_card *card)
335{
336 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200337 if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
Holger Schurig43d01c52008-05-26 12:50:23 +0200338}
339
340static inline void if_cs_disable_ints(struct if_cs_card *card)
341{
342 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200343 if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
Holger Schurig43d01c52008-05-26 12:50:23 +0200344}
345
Holger Schurig27590d02007-10-03 17:25:41 -0700346/*
347 * Called from if_cs_host_to_card to send a command to the hardware
348 */
Holger Schurig69f90322007-11-23 15:43:44 +0100349static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700350{
351 struct if_cs_card *card = (struct if_cs_card *)priv->card;
352 int ret = -1;
353 int loops = 0;
354
355 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200356 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700357
358 /* Is hardware ready? */
359 while (1) {
Holger Schuriga78a8322008-06-05 13:09:50 +0200360 u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
361 if (status & IF_CS_BIT_COMMAND)
Holger Schurig27590d02007-10-03 17:25:41 -0700362 break;
363 if (++loops > 100) {
364 lbs_pr_err("card not ready for commands\n");
365 goto done;
366 }
367 mdelay(1);
368 }
369
Holger Schuriga78a8322008-06-05 13:09:50 +0200370 if_cs_write16(card, IF_CS_CMD_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700371
Holger Schuriga78a8322008-06-05 13:09:50 +0200372 if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700373 /* Are we supposed to transfer an odd amount of bytes? */
374 if (nb & 1)
Holger Schuriga78a8322008-06-05 13:09:50 +0200375 if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700376
377 /* "Assert the download over interrupt command in the Host
378 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200379 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700380
381 /* "Assert the download over interrupt command in the Card
382 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200383 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700384 ret = 0;
385
386done:
Holger Schurig43d01c52008-05-26 12:50:23 +0200387 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700388 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
389 return ret;
390}
391
Holger Schurig27590d02007-10-03 17:25:41 -0700392/*
393 * Called from if_cs_host_to_card to send a data to the hardware
394 */
Holger Schurig69f90322007-11-23 15:43:44 +0100395static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700396{
397 struct if_cs_card *card = (struct if_cs_card *)priv->card;
Holger Schurig43d01c52008-05-26 12:50:23 +0200398 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700399
400 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200401 if_cs_disable_ints(card);
402
Holger Schurig78cf0742008-06-02 09:25:05 +0200403 status = if_cs_read16(card, IF_CS_CARD_STATUS);
404 BUG_ON((status & IF_CS_BIT_TX) == 0);
Holger Schurig27590d02007-10-03 17:25:41 -0700405
Holger Schuriga78a8322008-06-05 13:09:50 +0200406 if_cs_write16(card, IF_CS_WRITE_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700407
408 /* write even number of bytes, then odd byte if necessary */
Holger Schuriga78a8322008-06-05 13:09:50 +0200409 if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700410 if (nb & 1)
Holger Schuriga78a8322008-06-05 13:09:50 +0200411 if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700412
Holger Schurig78cf0742008-06-02 09:25:05 +0200413 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
414 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
Holger Schurig43d01c52008-05-26 12:50:23 +0200415 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700416
417 lbs_deb_leave(LBS_DEB_CS);
418}
419
Holger Schurig27590d02007-10-03 17:25:41 -0700420/*
421 * Get the command result out of the card.
422 */
Holger Schurig69f90322007-11-23 15:43:44 +0100423static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
Holger Schurig27590d02007-10-03 17:25:41 -0700424{
Holger Schurig7919b892008-04-01 14:50:43 +0200425 unsigned long flags;
Holger Schurig27590d02007-10-03 17:25:41 -0700426 int ret = -1;
Holger Schurig78cf0742008-06-02 09:25:05 +0200427 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700428
429 lbs_deb_enter(LBS_DEB_CS);
430
431 /* is hardware ready? */
Holger Schurig78cf0742008-06-02 09:25:05 +0200432 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
433 if ((status & IF_CS_BIT_RESP) == 0) {
434 lbs_pr_err("no cmd response in card\n");
435 *len = 0;
Holger Schurig27590d02007-10-03 17:25:41 -0700436 goto out;
437 }
438
Holger Schuriga78a8322008-06-05 13:09:50 +0200439 *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
Dan Williamsddac4522007-12-11 13:49:39 -0500440 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
Holger Schurig27590d02007-10-03 17:25:41 -0700441 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
442 goto out;
443 }
444
445 /* read even number of bytes, then odd byte if necessary */
Holger Schuriga78a8322008-06-05 13:09:50 +0200446 if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700447 if (*len & 1)
Holger Schuriga78a8322008-06-05 13:09:50 +0200448 data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
Holger Schurig27590d02007-10-03 17:25:41 -0700449
Holger Schurig09d4fad2007-12-06 13:50:30 +0100450 /* This is a workaround for a firmware that reports too much
451 * bytes */
452 *len -= 8;
Holger Schurig27590d02007-10-03 17:25:41 -0700453 ret = 0;
Holger Schurig7919b892008-04-01 14:50:43 +0200454
455 /* Clear this flag again */
456 spin_lock_irqsave(&priv->driver_lock, flags);
457 priv->dnld_sent = DNLD_RES_RECEIVED;
458 spin_unlock_irqrestore(&priv->driver_lock, flags);
459
Holger Schurig27590d02007-10-03 17:25:41 -0700460out:
461 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
462 return ret;
463}
464
Holger Schurig69f90322007-11-23 15:43:44 +0100465static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
Holger Schurig27590d02007-10-03 17:25:41 -0700466{
467 struct sk_buff *skb = NULL;
468 u16 len;
469 u8 *data;
470
471 lbs_deb_enter(LBS_DEB_CS);
472
Holger Schurig78cf0742008-06-02 09:25:05 +0200473 len = if_cs_read16(priv->card, IF_CS_READ_LEN);
Holger Schurig27590d02007-10-03 17:25:41 -0700474 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
475 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000476 priv->dev->stats.rx_dropped++;
Holger Schurig27590d02007-10-03 17:25:41 -0700477 goto dat_err;
478 }
479
Vladimir Davydovf31ce762007-09-06 21:41:02 -0400480 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700481 if (!skb)
482 goto out;
Vladimir Davydovf31ce762007-09-06 21:41:02 -0400483 skb_put(skb, len);
484 skb_reserve(skb, 2);/* 16 byte align */
485 data = skb->data;
Holger Schurig27590d02007-10-03 17:25:41 -0700486
487 /* read even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200488 if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700489 if (len & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200490 data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
Holger Schurig27590d02007-10-03 17:25:41 -0700491
492dat_err:
Holger Schurig78cf0742008-06-02 09:25:05 +0200493 if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
494 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
Holger Schurig27590d02007-10-03 17:25:41 -0700495
496out:
497 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
498 return skb;
499}
500
Holger Schurig7919b892008-04-01 14:50:43 +0200501static irqreturn_t if_cs_interrupt(int irq, void *data)
502{
503 struct if_cs_card *card = data;
504 struct lbs_private *priv = card->priv;
505 u16 cause;
506
507 lbs_deb_enter(LBS_DEB_CS);
508
Holger Schurig43d01c52008-05-26 12:50:23 +0200509 /* Ask card interrupt cause register if there is something for us */
Holger Schurig78cf0742008-06-02 09:25:05 +0200510 cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
Holger Schurig30735562008-06-05 13:06:15 +0200511 lbs_deb_cs("cause 0x%04x\n", cause);
512
Holger Schurig7919b892008-04-01 14:50:43 +0200513 if (cause == 0) {
514 /* Not for us */
515 return IRQ_NONE;
516 }
517
518 if (cause == 0xffff) {
519 /* Read in junk, the card has probably been removed */
520 card->priv->surpriseremoved = 1;
521 return IRQ_HANDLED;
522 }
523
Holger Schurig78cf0742008-06-02 09:25:05 +0200524 if (cause & IF_CS_BIT_RX) {
Holger Schurig7919b892008-04-01 14:50:43 +0200525 struct sk_buff *skb;
526 lbs_deb_cs("rx packet\n");
527 skb = if_cs_receive_data(priv);
528 if (skb)
529 lbs_process_rxed_packet(priv, skb);
530 }
531
Holger Schurig78cf0742008-06-02 09:25:05 +0200532 if (cause & IF_CS_BIT_TX) {
Holger Schurig43d01c52008-05-26 12:50:23 +0200533 lbs_deb_cs("tx done\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200534 lbs_host_to_card_done(priv);
535 }
536
Holger Schurig78cf0742008-06-02 09:25:05 +0200537 if (cause & IF_CS_BIT_RESP) {
Holger Schurig7919b892008-04-01 14:50:43 +0200538 unsigned long flags;
539 u8 i;
540
Holger Schurig43d01c52008-05-26 12:50:23 +0200541 lbs_deb_cs("cmd resp\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200542 spin_lock_irqsave(&priv->driver_lock, flags);
543 i = (priv->resp_idx == 0) ? 1 : 0;
544 spin_unlock_irqrestore(&priv->driver_lock, flags);
545
546 BUG_ON(priv->resp_len[i]);
547 if_cs_receive_cmdres(priv, priv->resp_buf[i],
548 &priv->resp_len[i]);
549
550 spin_lock_irqsave(&priv->driver_lock, flags);
551 lbs_notify_command_response(priv, i);
552 spin_unlock_irqrestore(&priv->driver_lock, flags);
553 }
554
Holger Schurig78cf0742008-06-02 09:25:05 +0200555 if (cause & IF_CS_BIT_EVENT) {
Holger Schuriga78a8322008-06-05 13:09:50 +0200556 u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200557 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
558 IF_CS_BIT_EVENT);
Holger Schuriga78a8322008-06-05 13:09:50 +0200559 lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
Holger Schurig7919b892008-04-01 14:50:43 +0200560 }
561
Holger Schurig30735562008-06-05 13:06:15 +0200562 /* Clear interrupt cause */
563 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
564
Holger Schurig43d01c52008-05-26 12:50:23 +0200565 lbs_deb_leave(LBS_DEB_CS);
Holger Schurig7919b892008-04-01 14:50:43 +0200566 return IRQ_HANDLED;
567}
568
569
570
571
572/********************************************************************/
Holger Schurig27590d02007-10-03 17:25:41 -0700573/* Firmware */
574/********************************************************************/
575
576/*
577 * Tries to program the helper firmware.
578 *
579 * Return 0 on success
580 */
Dan Williams82222e92010-08-07 21:15:19 -0500581static int if_cs_prog_helper(struct if_cs_card *card, const struct firmware *fw)
Holger Schurig27590d02007-10-03 17:25:41 -0700582{
583 int ret = 0;
584 int sent = 0;
585 u8 scratch;
Holger Schurig27590d02007-10-03 17:25:41 -0700586
587 lbs_deb_enter(LBS_DEB_CS);
588
Marek Vasut9d453682009-08-21 04:08:16 +0200589 /*
590 * This is the only place where an unaligned register access happens on
591 * the CF8305 card, therefore for the sake of speed of the driver, we do
592 * the alignment correction here.
593 */
594 if (card->align_regs)
595 scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
596 else
597 scratch = if_cs_read8(card, IF_CS_SCRATCH);
Holger Schurig27590d02007-10-03 17:25:41 -0700598
599 /* "If the value is 0x5a, the firmware is already
600 * downloaded successfully"
601 */
Holger Schurig53143252008-06-05 13:07:04 +0200602 if (scratch == IF_CS_SCRATCH_HELPER_OK)
Holger Schurig27590d02007-10-03 17:25:41 -0700603 goto done;
604
605 /* "If the value is != 00, it is invalid value of register */
Holger Schurig53143252008-06-05 13:07:04 +0200606 if (scratch != IF_CS_SCRATCH_BOOT_OK) {
Holger Schurig27590d02007-10-03 17:25:41 -0700607 ret = -ENODEV;
608 goto done;
609 }
610
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700611 lbs_deb_cs("helper size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700612
613 /* "Set the 5 bytes of the helper image to 0" */
614 /* Not needed, this contains an ARM branch instruction */
615
616 for (;;) {
617 /* "the number of bytes to send is 256" */
618 int count = 256;
619 int remain = fw->size - sent;
620
621 if (remain < count)
622 count = remain;
Holger Schurig27590d02007-10-03 17:25:41 -0700623
624 /* "write the number of bytes to be sent to the I/O Command
625 * write length register" */
Holger Schuriga78a8322008-06-05 13:09:50 +0200626 if_cs_write16(card, IF_CS_CMD_LEN, count);
Holger Schurig27590d02007-10-03 17:25:41 -0700627
628 /* "write this to I/O Command port register as 16 bit writes */
629 if (count)
Holger Schuriga78a8322008-06-05 13:09:50 +0200630 if_cs_write16_rep(card, IF_CS_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700631 &fw->data[sent],
632 count >> 1);
633
634 /* "Assert the download over interrupt command in the Host
635 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200636 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700637
638 /* "Assert the download over interrupt command in the Card
639 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200640 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700641
642 /* "The host polls the Card Status register ... for 50 ms before
643 declaring a failure */
Holger Schurig78cf0742008-06-02 09:25:05 +0200644 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
645 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700646 if (ret < 0) {
647 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
648 sent, ret);
Dan Williams82222e92010-08-07 21:15:19 -0500649 goto done;
Holger Schurig27590d02007-10-03 17:25:41 -0700650 }
651
652 if (count == 0)
653 break;
654
655 sent += count;
656 }
657
Holger Schurig27590d02007-10-03 17:25:41 -0700658done:
659 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
660 return ret;
661}
662
663
Dan Williams82222e92010-08-07 21:15:19 -0500664static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
Holger Schurig27590d02007-10-03 17:25:41 -0700665{
Holger Schurig27590d02007-10-03 17:25:41 -0700666 int ret = 0;
667 int retry = 0;
668 int len = 0;
669 int sent;
670
671 lbs_deb_enter(LBS_DEB_CS);
672
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700673 lbs_deb_cs("fw size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700674
Holger Schuriga78a8322008-06-05 13:09:50 +0200675 ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
676 IF_CS_SQ_HELPER_OK);
Holger Schurig27590d02007-10-03 17:25:41 -0700677 if (ret < 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700678 lbs_pr_err("helper firmware doesn't answer\n");
Dan Williams82222e92010-08-07 21:15:19 -0500679 goto done;
Holger Schurig27590d02007-10-03 17:25:41 -0700680 }
681
682 for (sent = 0; sent < fw->size; sent += len) {
Holger Schuriga78a8322008-06-05 13:09:50 +0200683 len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
Holger Schurig27590d02007-10-03 17:25:41 -0700684 if (len & 1) {
685 retry++;
686 lbs_pr_info("odd, need to retry this firmware block\n");
687 } else {
688 retry = 0;
689 }
690
691 if (retry > 20) {
692 lbs_pr_err("could not download firmware\n");
693 ret = -ENODEV;
Dan Williams82222e92010-08-07 21:15:19 -0500694 goto done;
Holger Schurig27590d02007-10-03 17:25:41 -0700695 }
696 if (retry) {
697 sent -= len;
698 }
699
700
Holger Schuriga78a8322008-06-05 13:09:50 +0200701 if_cs_write16(card, IF_CS_CMD_LEN, len);
Holger Schurig27590d02007-10-03 17:25:41 -0700702
Holger Schuriga78a8322008-06-05 13:09:50 +0200703 if_cs_write16_rep(card, IF_CS_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700704 &fw->data[sent],
705 (len+1) >> 1);
Holger Schurig78cf0742008-06-02 09:25:05 +0200706 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
707 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700708
Holger Schurig78cf0742008-06-02 09:25:05 +0200709 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
710 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700711 if (ret < 0) {
712 lbs_pr_err("can't download firmware at 0x%x\n", sent);
Dan Williams82222e92010-08-07 21:15:19 -0500713 goto done;
Holger Schurig27590d02007-10-03 17:25:41 -0700714 }
715 }
716
717 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
Adrian Bunk9a520282008-08-28 01:05:08 +0300718 if (ret < 0)
Holger Schurig27590d02007-10-03 17:25:41 -0700719 lbs_pr_err("firmware download failed\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700720
Holger Schurig27590d02007-10-03 17:25:41 -0700721done:
722 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
723 return ret;
724}
725
726
727
728/********************************************************************/
729/* Callback functions for libertas.ko */
730/********************************************************************/
731
Holger Schurig27590d02007-10-03 17:25:41 -0700732/* Send commands or data packets to the card */
Holger Schurig69f90322007-11-23 15:43:44 +0100733static int if_cs_host_to_card(struct lbs_private *priv,
734 u8 type,
735 u8 *buf,
736 u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700737{
738 int ret = -1;
739
740 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
741
742 switch (type) {
743 case MVMS_DAT:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400744 priv->dnld_sent = DNLD_DATA_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700745 if_cs_send_data(priv, buf, nb);
746 ret = 0;
747 break;
748 case MVMS_CMD:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400749 priv->dnld_sent = DNLD_CMD_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700750 ret = if_cs_send_cmd(priv, buf, nb);
751 break;
752 default:
Harvey Harrisonc94c93d2008-07-28 23:01:34 -0700753 lbs_pr_err("%s: unsupported type %d\n", __func__, type);
Holger Schurig27590d02007-10-03 17:25:41 -0700754 }
755
756 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
757 return ret;
758}
759
760
Holger Schurig27590d02007-10-03 17:25:41 -0700761static void if_cs_release(struct pcmcia_device *p_dev)
762{
763 struct if_cs_card *card = p_dev->priv;
764
765 lbs_deb_enter(LBS_DEB_CS);
766
Dominik Brodowskieb141202010-03-07 12:21:16 +0100767 free_irq(p_dev->irq, card);
Holger Schurigfdfb92e2008-01-25 14:15:48 +0100768 pcmcia_disable_device(p_dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700769 if (card->iobase)
770 ioport_unmap(card->iobase);
771
772 lbs_deb_leave(LBS_DEB_CS);
773}
774
775
Dominik Brodowski00990e72010-07-30 13:13:46 +0200776static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200777{
Dominik Brodowski00990e72010-07-30 13:13:46 +0200778 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200779 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200780
Dominik Brodowski00990e72010-07-30 13:13:46 +0200781 if (p_dev->resource[1]->end) {
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200782 lbs_pr_err("wrong CIS (check number of IO windows)\n");
783 return -ENODEV;
784 }
785
786 /* This reserves IO space but doesn't actually enable it */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200787 return pcmcia_request_io(p_dev);
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200788}
789
Holger Schurig27590d02007-10-03 17:25:41 -0700790static int if_cs_probe(struct pcmcia_device *p_dev)
791{
792 int ret = -ENOMEM;
Marek Vasutdc7d2432009-03-24 21:33:43 +0100793 unsigned int prod_id;
Holger Schurig69f90322007-11-23 15:43:44 +0100794 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700795 struct if_cs_card *card;
Dan Williams82222e92010-08-07 21:15:19 -0500796 const struct firmware *helper = NULL;
797 const struct firmware *mainfw = NULL;
Holger Schurig27590d02007-10-03 17:25:41 -0700798
799 lbs_deb_enter(LBS_DEB_CS);
800
801 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
802 if (!card) {
803 lbs_pr_err("error in kzalloc\n");
804 goto out;
805 }
806 card->p_dev = p_dev;
807 p_dev->priv = card;
808
Dominik Brodowski00990e72010-07-30 13:13:46 +0200809 p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
810
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200811 if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
812 lbs_pr_err("error in pcmcia_loop_config\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700813 goto out1;
814 }
815
Holger Schurig27590d02007-10-03 17:25:41 -0700816 /*
817 * Allocate an interrupt line. Note that this does not assign
818 * a handler to the interrupt, unless the 'Handler' member of
819 * the irq structure is initialized.
820 */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100821 if (!p_dev->irq)
822 goto out1;
Holger Schurig27590d02007-10-03 17:25:41 -0700823
824 /* Initialize io access */
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200825 card->iobase = ioport_map(p_dev->resource[0]->start,
826 resource_size(p_dev->resource[0]));
Holger Schurig27590d02007-10-03 17:25:41 -0700827 if (!card->iobase) {
828 lbs_pr_err("error in ioport_map\n");
829 ret = -EIO;
830 goto out1;
831 }
832
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200833 ret = pcmcia_enable_device(p_dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700834 if (ret) {
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200835 lbs_pr_err("error in pcmcia_enable_device\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700836 goto out2;
837 }
838
839 /* Finally, report what we've done */
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200840 lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
Holger Schurig27590d02007-10-03 17:25:41 -0700841
Marek Vasut9d453682009-08-21 04:08:16 +0200842 /*
843 * Most of the libertas cards can do unaligned register access, but some
844 * weird ones can not. That's especially true for the CF8305 card.
845 */
846 card->align_regs = 0;
847
Dan Williams82222e92010-08-07 21:15:19 -0500848 card->model = get_model(p_dev->manf_id, p_dev->card_id);
849 if (card->model == MODEL_UNKNOWN) {
850 lbs_pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
851 p_dev->manf_id, p_dev->card_id);
852 goto out2;
853 }
854
Holger Schurig4c555232008-06-05 13:08:35 +0200855 /* Check if we have a current silicon */
Marek Vasutdc7d2432009-03-24 21:33:43 +0100856 prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
Dan Williams82222e92010-08-07 21:15:19 -0500857 if (card->model == MODEL_8305) {
Marek Vasut9d453682009-08-21 04:08:16 +0200858 card->align_regs = 1;
859 if (prod_id < IF_CS_CF8305_B1_REV) {
Dan Williams82222e92010-08-07 21:15:19 -0500860 lbs_pr_err("8305 rev B0 and older are not supported\n");
Marek Vasut9d453682009-08-21 04:08:16 +0200861 ret = -ENODEV;
862 goto out2;
863 }
864 }
865
Dan Williams82222e92010-08-07 21:15:19 -0500866 if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
867 lbs_pr_err("8381 rev B2 and older are not supported\n");
Marek Vasutdc7d2432009-03-24 21:33:43 +0100868 ret = -ENODEV;
869 goto out2;
870 }
871
Dan Williams82222e92010-08-07 21:15:19 -0500872 if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
873 lbs_pr_err("8385 rev B0 and older are not supported\n");
Holger Schurig4c555232008-06-05 13:08:35 +0200874 ret = -ENODEV;
875 goto out2;
876 }
Holger Schurig27590d02007-10-03 17:25:41 -0700877
Dan Williams82222e92010-08-07 21:15:19 -0500878 ret = lbs_get_firmware(&p_dev->dev, NULL, NULL, card->model,
879 &fw_table[0], &helper, &mainfw);
880 if (ret) {
881 lbs_pr_err("failed to find firmware (%d)\n", ret);
882 goto out2;
883 }
884
Holger Schurig27590d02007-10-03 17:25:41 -0700885 /* Load the firmware early, before calling into libertas.ko */
Dan Williams82222e92010-08-07 21:15:19 -0500886 ret = if_cs_prog_helper(card, helper);
887 if (ret == 0 && (card->model != MODEL_8305))
888 ret = if_cs_prog_real(card, mainfw);
Holger Schurig27590d02007-10-03 17:25:41 -0700889 if (ret)
890 goto out2;
891
892 /* Make this card known to the libertas driver */
Holger Schurig10078322007-11-15 18:05:47 -0500893 priv = lbs_add_card(card, &p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700894 if (!priv) {
895 ret = -ENOMEM;
896 goto out2;
897 }
898
Holger Schurig7919b892008-04-01 14:50:43 +0200899 /* Finish setting up fields in lbs_private */
Dan Williams954ee162007-08-20 11:43:25 -0400900 card->priv = priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700901 priv->card = card;
Holger Schurig7919b892008-04-01 14:50:43 +0200902 priv->hw_host_to_card = if_cs_host_to_card;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700903 priv->enter_deep_sleep = NULL;
904 priv->exit_deep_sleep = NULL;
905 priv->reset_deep_sleep_wakeup = NULL;
David Woodhouseaa21c002007-12-08 20:04:36 +0000906 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400907
Holger Schurig27590d02007-10-03 17:25:41 -0700908 /* Now actually get the IRQ */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100909 ret = request_irq(p_dev->irq, if_cs_interrupt,
Holger Schurig27590d02007-10-03 17:25:41 -0700910 IRQF_SHARED, DRV_NAME, card);
911 if (ret) {
912 lbs_pr_err("error in request_irq\n");
913 goto out3;
914 }
915
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300916 /* Clear any interrupt cause that happened while sending
Holger Schurig4ef31702007-10-09 10:41:57 +0200917 * firmware/initializing card */
Holger Schurig78cf0742008-06-02 09:25:05 +0200918 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400919 if_cs_enable_ints(card);
920
Holger Schurig27590d02007-10-03 17:25:41 -0700921 /* And finally bring the card up */
Holger Schurig10078322007-11-15 18:05:47 -0500922 if (lbs_start_card(priv) != 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700923 lbs_pr_err("could not activate card\n");
924 goto out3;
925 }
926
927 ret = 0;
928 goto out;
929
930out3:
Holger Schurig10078322007-11-15 18:05:47 -0500931 lbs_remove_card(priv);
Holger Schurig27590d02007-10-03 17:25:41 -0700932out2:
933 ioport_unmap(card->iobase);
934out1:
935 pcmcia_disable_device(p_dev);
936out:
Dan Williams82222e92010-08-07 21:15:19 -0500937 if (helper)
938 release_firmware(helper);
939 if (mainfw)
940 release_firmware(mainfw);
941
Holger Schurig27590d02007-10-03 17:25:41 -0700942 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
943 return ret;
944}
945
946
Holger Schurig27590d02007-10-03 17:25:41 -0700947static void if_cs_detach(struct pcmcia_device *p_dev)
948{
949 struct if_cs_card *card = p_dev->priv;
950
951 lbs_deb_enter(LBS_DEB_CS);
952
Holger Schurig10078322007-11-15 18:05:47 -0500953 lbs_stop_card(card->priv);
954 lbs_remove_card(card->priv);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400955 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700956 if_cs_release(p_dev);
957 kfree(card);
958
959 lbs_deb_leave(LBS_DEB_CS);
960}
961
962
963
964/********************************************************************/
965/* Module initialization */
966/********************************************************************/
967
Joe Perches25f8f542011-05-03 19:29:01 -0700968static const struct pcmcia_device_id if_cs_ids[] = {
Marek Vasut9d453682009-08-21 04:08:16 +0200969 PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
Marek Vasutdc7d2432009-03-24 21:33:43 +0100970 PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
971 PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
Dan Williams82222e92010-08-07 21:15:19 -0500972 /* NOTE: keep in sync with get_model() */
Holger Schurig27590d02007-10-03 17:25:41 -0700973 PCMCIA_DEVICE_NULL,
974};
975MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
976
977
Holger Schurig10078322007-11-15 18:05:47 -0500978static struct pcmcia_driver lbs_driver = {
Holger Schurig27590d02007-10-03 17:25:41 -0700979 .owner = THIS_MODULE,
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200980 .name = DRV_NAME,
Holger Schurig27590d02007-10-03 17:25:41 -0700981 .probe = if_cs_probe,
982 .remove = if_cs_detach,
983 .id_table = if_cs_ids,
984};
985
986
987static int __init if_cs_init(void)
988{
989 int ret;
990
991 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -0500992 ret = pcmcia_register_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -0700993 lbs_deb_leave(LBS_DEB_CS);
994 return ret;
995}
996
997
998static void __exit if_cs_exit(void)
999{
1000 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -05001001 pcmcia_unregister_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -07001002 lbs_deb_leave(LBS_DEB_CS);
1003}
1004
1005
1006module_init(if_cs_init);
1007module_exit(if_cs_exit);