blob: 6d55439a7b97a59f32a309cc43a35ca7484b7178 [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");
Ben Hutchingsa974a4b2009-11-07 22:00:03 +000052MODULE_FIRMWARE("libertas_cs_helper.fw");
Holger Schurig27590d02007-10-03 17:25:41 -070053
54
55
56/********************************************************************/
57/* Data structures */
58/********************************************************************/
59
60struct if_cs_card {
61 struct pcmcia_device *p_dev;
Holger Schurig69f90322007-11-23 15:43:44 +010062 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -070063 void __iomem *iobase;
Marek Vasut9d453682009-08-21 04:08:16 +020064 bool align_regs;
Holger Schurig27590d02007-10-03 17:25:41 -070065};
66
67
68
69/********************************************************************/
70/* Hardware access */
71/********************************************************************/
72
73/* This define enables wrapper functions which allow you
74 to dump all register accesses. You normally won't this,
75 except for development */
76/* #define DEBUG_IO */
77
78#ifdef DEBUG_IO
79static int debug_output = 0;
80#else
81/* This way the compiler optimizes the printk's away */
82#define debug_output 0
83#endif
84
85static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
86{
87 unsigned int val = ioread8(card->iobase + reg);
88 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +020089 printk(KERN_INFO "inb %08x<%02x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -070090 return val;
91}
92static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
93{
94 unsigned int val = ioread16(card->iobase + reg);
95 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +020096 printk(KERN_INFO "inw %08x<%04x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -070097 return val;
98}
99static inline void if_cs_read16_rep(
100 struct if_cs_card *card,
101 uint reg,
102 void *buf,
103 unsigned long count)
104{
105 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200106 printk(KERN_INFO "insw %08x<(0x%lx words)\n",
Holger Schurig27590d02007-10-03 17:25:41 -0700107 reg, count);
108 ioread16_rep(card->iobase + reg, buf, count);
109}
110
111static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
112{
113 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200114 printk(KERN_INFO "outb %08x>%02x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700115 iowrite8(val, card->iobase + reg);
116}
117
118static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
119{
120 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200121 printk(KERN_INFO "outw %08x>%04x\n", reg, val);
Holger Schurig27590d02007-10-03 17:25:41 -0700122 iowrite16(val, card->iobase + reg);
123}
124
125static inline void if_cs_write16_rep(
126 struct if_cs_card *card,
127 uint reg,
David Woodhouse6dfff892008-05-23 18:37:51 +0100128 const void *buf,
Holger Schurig27590d02007-10-03 17:25:41 -0700129 unsigned long count)
130{
131 if (debug_output)
Holger Schurig7919b892008-04-01 14:50:43 +0200132 printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
Holger Schurig27590d02007-10-03 17:25:41 -0700133 reg, count);
134 iowrite16_rep(card->iobase + reg, buf, count);
135}
136
137
138/*
139 * I know that polling/delaying is frowned upon. However, this procedure
140 * with polling is needed while downloading the firmware. At this stage,
141 * the hardware does unfortunately not create any interrupts.
142 *
143 * Fortunately, this function is never used once the firmware is in
144 * the card. :-)
145 *
146 * As a reference, see the "Firmware Specification v5.1", page 18
147 * and 19. I did not follow their suggested timing to the word,
148 * but this works nice & fast anyway.
149 */
150static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
151{
152 int i;
153
Holger Schurig93416c82008-05-23 10:18:26 +0200154 for (i = 0; i < 100000; i++) {
Holger Schurig27590d02007-10-03 17:25:41 -0700155 u8 val = if_cs_read8(card, addr);
156 if (val == reg)
Dan Williams1fac36e2009-01-23 11:55:33 -0500157 return 0;
Holger Schurig93416c82008-05-23 10:18:26 +0200158 udelay(5);
Holger Schurig27590d02007-10-03 17:25:41 -0700159 }
160 return -ETIME;
161}
162
163
164
Holger Schurig53143252008-06-05 13:07:04 +0200165/*
166 * First the bitmasks for the host/card interrupt/status registers:
167 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200168#define IF_CS_BIT_TX 0x0001
169#define IF_CS_BIT_RX 0x0002
170#define IF_CS_BIT_COMMAND 0x0004
171#define IF_CS_BIT_RESP 0x0008
172#define IF_CS_BIT_EVENT 0x0010
173#define IF_CS_BIT_MASK 0x001f
Holger Schurig27590d02007-10-03 17:25:41 -0700174
Holger Schurig53143252008-06-05 13:07:04 +0200175
176
177/*
178 * It's not really clear to me what the host status register is for. It
179 * needs to be set almost in union with "host int cause". The following
180 * bits from above are used:
181 *
182 * IF_CS_BIT_TX driver downloaded a data packet
183 * IF_CS_BIT_RX driver got a data packet
184 * IF_CS_BIT_COMMAND driver downloaded a command
185 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
186 * IF_CS_BIT_EVENT driver read a host event
187 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200188#define IF_CS_HOST_STATUS 0x00000000
Holger Schurig27590d02007-10-03 17:25:41 -0700189
Holger Schurig53143252008-06-05 13:07:04 +0200190/*
191 * With the host int cause register can the host (that is, Linux) cause
192 * an interrupt in the firmware, to tell the firmware about those events:
193 *
194 * IF_CS_BIT_TX a data packet has been downloaded
195 * IF_CS_BIT_RX a received data packet has retrieved
196 * IF_CS_BIT_COMMAND a firmware block or a command has been downloaded
197 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
198 * IF_CS_BIT_EVENT a host event (link lost etc) has been retrieved
199 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200200#define IF_CS_HOST_INT_CAUSE 0x00000002
Holger Schurig27590d02007-10-03 17:25:41 -0700201
Holger Schurig53143252008-06-05 13:07:04 +0200202/*
203 * The host int mask register is used to enable/disable interrupt. However,
204 * I have the suspicion that disabled interrupts are lost.
205 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200206#define IF_CS_HOST_INT_MASK 0x00000004
Holger Schurig27590d02007-10-03 17:25:41 -0700207
Holger Schurig53143252008-06-05 13:07:04 +0200208/*
209 * Used to send or receive data packets:
210 */
Holger Schuriga78a8322008-06-05 13:09:50 +0200211#define IF_CS_WRITE 0x00000016
212#define IF_CS_WRITE_LEN 0x00000014
Holger Schurig78cf0742008-06-02 09:25:05 +0200213#define IF_CS_READ 0x00000010
214#define IF_CS_READ_LEN 0x00000024
Holger Schurig27590d02007-10-03 17:25:41 -0700215
Holger Schurig53143252008-06-05 13:07:04 +0200216/*
217 * Used to send commands (and to send firmware block) and to
218 * receive command responses:
219 */
Holger Schuriga78a8322008-06-05 13:09:50 +0200220#define IF_CS_CMD 0x0000001A
221#define IF_CS_CMD_LEN 0x00000018
222#define IF_CS_RESP 0x00000012
223#define IF_CS_RESP_LEN 0x00000030
Holger Schurig27590d02007-10-03 17:25:41 -0700224
Holger Schurig53143252008-06-05 13:07:04 +0200225/*
226 * The card status registers shows what the card/firmware actually
227 * accepts:
228 *
229 * IF_CS_BIT_TX you may send a data packet
230 * IF_CS_BIT_RX you may retrieve a data packet
231 * IF_CS_BIT_COMMAND you may send a command
232 * IF_CS_BIT_RESP you may retrieve a command response
233 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
234 *
235 * When reading this register several times, you will get back the same
236 * results --- with one exception: the IF_CS_BIT_EVENT clear itself
237 * automatically.
238 *
239 * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
240 * we handle this via the card int cause register.
241 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200242#define IF_CS_CARD_STATUS 0x00000020
243#define IF_CS_CARD_STATUS_MASK 0x7f00
Holger Schurig27590d02007-10-03 17:25:41 -0700244
Holger Schurig53143252008-06-05 13:07:04 +0200245/*
246 * The card int cause register is used by the card/firmware to notify us
247 * about the following events:
248 *
249 * IF_CS_BIT_TX a data packet has successfully been sentx
250 * IF_CS_BIT_RX a data packet has been received and can be retrieved
251 * IF_CS_BIT_COMMAND not used
252 * IF_CS_BIT_RESP the firmware has a command response for us
253 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
254 */
Holger Schurig78cf0742008-06-02 09:25:05 +0200255#define IF_CS_CARD_INT_CAUSE 0x00000022
Holger Schurig27590d02007-10-03 17:25:41 -0700256
Holger Schurig53143252008-06-05 13:07:04 +0200257/*
258 * This is used to for handshaking with the card's bootloader/helper image
259 * to synchronize downloading of firmware blocks.
260 */
Holger Schuriga78a8322008-06-05 13:09:50 +0200261#define IF_CS_SQ_READ_LOW 0x00000028
262#define IF_CS_SQ_HELPER_OK 0x10
Holger Schurig27590d02007-10-03 17:25:41 -0700263
Holger Schurig53143252008-06-05 13:07:04 +0200264/*
265 * The scratch register tells us ...
266 *
267 * IF_CS_SCRATCH_BOOT_OK the bootloader runs
268 * IF_CS_SCRATCH_HELPER_OK the helper firmware already runs
269 */
Holger Schurig27590d02007-10-03 17:25:41 -0700270#define IF_CS_SCRATCH 0x0000003F
Holger Schurig53143252008-06-05 13:07:04 +0200271#define IF_CS_SCRATCH_BOOT_OK 0x00
272#define IF_CS_SCRATCH_HELPER_OK 0x5a
Holger Schurig27590d02007-10-03 17:25:41 -0700273
Holger Schurig4c555232008-06-05 13:08:35 +0200274/*
275 * Used to detect ancient chips:
276 */
277#define IF_CS_PRODUCT_ID 0x0000001C
278#define IF_CS_CF8385_B1_REV 0x12
Marek Vasutdc7d2432009-03-24 21:33:43 +0100279#define IF_CS_CF8381_B3_REV 0x04
Marek Vasut9d453682009-08-21 04:08:16 +0200280#define IF_CS_CF8305_B1_REV 0x03
Holger Schurig4c555232008-06-05 13:08:35 +0200281
Marek Vasutdc7d2432009-03-24 21:33:43 +0100282/*
283 * Used to detect other cards than CF8385 since their revisions of silicon
284 * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
285 */
Marek Vasut9d453682009-08-21 04:08:16 +0200286#define CF8305_MANFID 0x02db
287#define CF8305_CARDID 0x8103
Marek Vasutdc7d2432009-03-24 21:33:43 +0100288#define CF8381_MANFID 0x02db
289#define CF8381_CARDID 0x6064
290#define CF8385_MANFID 0x02df
291#define CF8385_CARDID 0x8103
292
Marek Vasut9d453682009-08-21 04:08:16 +0200293static inline int if_cs_hw_is_cf8305(struct pcmcia_device *p_dev)
294{
295 return (p_dev->manf_id == CF8305_MANFID &&
296 p_dev->card_id == CF8305_CARDID);
297}
298
Marek Vasutdc7d2432009-03-24 21:33:43 +0100299static inline int if_cs_hw_is_cf8381(struct pcmcia_device *p_dev)
300{
301 return (p_dev->manf_id == CF8381_MANFID &&
302 p_dev->card_id == CF8381_CARDID);
303}
304
305static inline int if_cs_hw_is_cf8385(struct pcmcia_device *p_dev)
306{
307 return (p_dev->manf_id == CF8385_MANFID &&
308 p_dev->card_id == CF8385_CARDID);
309}
Holger Schurig27590d02007-10-03 17:25:41 -0700310
311/********************************************************************/
Holger Schurig43d01c52008-05-26 12:50:23 +0200312/* I/O and interrupt handling */
Holger Schurig27590d02007-10-03 17:25:41 -0700313/********************************************************************/
314
Holger Schurig43d01c52008-05-26 12:50:23 +0200315static inline void if_cs_enable_ints(struct if_cs_card *card)
316{
317 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200318 if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
Holger Schurig43d01c52008-05-26 12:50:23 +0200319}
320
321static inline void if_cs_disable_ints(struct if_cs_card *card)
322{
323 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200324 if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
Holger Schurig43d01c52008-05-26 12:50:23 +0200325}
326
Holger Schurig27590d02007-10-03 17:25:41 -0700327/*
328 * Called from if_cs_host_to_card to send a command to the hardware
329 */
Holger Schurig69f90322007-11-23 15:43:44 +0100330static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700331{
332 struct if_cs_card *card = (struct if_cs_card *)priv->card;
333 int ret = -1;
334 int loops = 0;
335
336 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200337 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700338
339 /* Is hardware ready? */
340 while (1) {
Holger Schuriga78a8322008-06-05 13:09:50 +0200341 u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
342 if (status & IF_CS_BIT_COMMAND)
Holger Schurig27590d02007-10-03 17:25:41 -0700343 break;
344 if (++loops > 100) {
345 lbs_pr_err("card not ready for commands\n");
346 goto done;
347 }
348 mdelay(1);
349 }
350
Holger Schuriga78a8322008-06-05 13:09:50 +0200351 if_cs_write16(card, IF_CS_CMD_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700352
Holger Schuriga78a8322008-06-05 13:09:50 +0200353 if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700354 /* Are we supposed to transfer an odd amount of bytes? */
355 if (nb & 1)
Holger Schuriga78a8322008-06-05 13:09:50 +0200356 if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700357
358 /* "Assert the download over interrupt command in the Host
359 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200360 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700361
362 /* "Assert the download over interrupt command in the Card
363 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200364 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700365 ret = 0;
366
367done:
Holger Schurig43d01c52008-05-26 12:50:23 +0200368 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700369 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
370 return ret;
371}
372
Holger Schurig27590d02007-10-03 17:25:41 -0700373/*
374 * Called from if_cs_host_to_card to send a data to the hardware
375 */
Holger Schurig69f90322007-11-23 15:43:44 +0100376static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700377{
378 struct if_cs_card *card = (struct if_cs_card *)priv->card;
Holger Schurig43d01c52008-05-26 12:50:23 +0200379 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700380
381 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig43d01c52008-05-26 12:50:23 +0200382 if_cs_disable_ints(card);
383
Holger Schurig78cf0742008-06-02 09:25:05 +0200384 status = if_cs_read16(card, IF_CS_CARD_STATUS);
385 BUG_ON((status & IF_CS_BIT_TX) == 0);
Holger Schurig27590d02007-10-03 17:25:41 -0700386
Holger Schuriga78a8322008-06-05 13:09:50 +0200387 if_cs_write16(card, IF_CS_WRITE_LEN, nb);
Holger Schurig27590d02007-10-03 17:25:41 -0700388
389 /* write even number of bytes, then odd byte if necessary */
Holger Schuriga78a8322008-06-05 13:09:50 +0200390 if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700391 if (nb & 1)
Holger Schuriga78a8322008-06-05 13:09:50 +0200392 if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
Holger Schurig27590d02007-10-03 17:25:41 -0700393
Holger Schurig78cf0742008-06-02 09:25:05 +0200394 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
395 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
Holger Schurig43d01c52008-05-26 12:50:23 +0200396 if_cs_enable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700397
398 lbs_deb_leave(LBS_DEB_CS);
399}
400
Holger Schurig27590d02007-10-03 17:25:41 -0700401/*
402 * Get the command result out of the card.
403 */
Holger Schurig69f90322007-11-23 15:43:44 +0100404static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
Holger Schurig27590d02007-10-03 17:25:41 -0700405{
Holger Schurig7919b892008-04-01 14:50:43 +0200406 unsigned long flags;
Holger Schurig27590d02007-10-03 17:25:41 -0700407 int ret = -1;
Holger Schurig78cf0742008-06-02 09:25:05 +0200408 u16 status;
Holger Schurig27590d02007-10-03 17:25:41 -0700409
410 lbs_deb_enter(LBS_DEB_CS);
411
412 /* is hardware ready? */
Holger Schurig78cf0742008-06-02 09:25:05 +0200413 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
414 if ((status & IF_CS_BIT_RESP) == 0) {
415 lbs_pr_err("no cmd response in card\n");
416 *len = 0;
Holger Schurig27590d02007-10-03 17:25:41 -0700417 goto out;
418 }
419
Holger Schuriga78a8322008-06-05 13:09:50 +0200420 *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
Dan Williamsddac4522007-12-11 13:49:39 -0500421 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
Holger Schurig27590d02007-10-03 17:25:41 -0700422 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
423 goto out;
424 }
425
426 /* read even number of bytes, then odd byte if necessary */
Holger Schuriga78a8322008-06-05 13:09:50 +0200427 if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700428 if (*len & 1)
Holger Schuriga78a8322008-06-05 13:09:50 +0200429 data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
Holger Schurig27590d02007-10-03 17:25:41 -0700430
Holger Schurig09d4fad2007-12-06 13:50:30 +0100431 /* This is a workaround for a firmware that reports too much
432 * bytes */
433 *len -= 8;
Holger Schurig27590d02007-10-03 17:25:41 -0700434 ret = 0;
Holger Schurig7919b892008-04-01 14:50:43 +0200435
436 /* Clear this flag again */
437 spin_lock_irqsave(&priv->driver_lock, flags);
438 priv->dnld_sent = DNLD_RES_RECEIVED;
439 spin_unlock_irqrestore(&priv->driver_lock, flags);
440
Holger Schurig27590d02007-10-03 17:25:41 -0700441out:
442 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
443 return ret;
444}
445
Holger Schurig69f90322007-11-23 15:43:44 +0100446static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
Holger Schurig27590d02007-10-03 17:25:41 -0700447{
448 struct sk_buff *skb = NULL;
449 u16 len;
450 u8 *data;
451
452 lbs_deb_enter(LBS_DEB_CS);
453
Holger Schurig78cf0742008-06-02 09:25:05 +0200454 len = if_cs_read16(priv->card, IF_CS_READ_LEN);
Holger Schurig27590d02007-10-03 17:25:41 -0700455 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
456 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000457 priv->dev->stats.rx_dropped++;
Holger Schurig27590d02007-10-03 17:25:41 -0700458 goto dat_err;
459 }
460
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400461 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
Holger Schurig27590d02007-10-03 17:25:41 -0700462 if (!skb)
463 goto out;
Vladimir Davydovf31ce76b2007-09-06 21:41:02 -0400464 skb_put(skb, len);
465 skb_reserve(skb, 2);/* 16 byte align */
466 data = skb->data;
Holger Schurig27590d02007-10-03 17:25:41 -0700467
468 /* read even number of bytes, then odd byte if necessary */
Holger Schurig78cf0742008-06-02 09:25:05 +0200469 if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
Holger Schurig27590d02007-10-03 17:25:41 -0700470 if (len & 1)
Holger Schurig78cf0742008-06-02 09:25:05 +0200471 data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
Holger Schurig27590d02007-10-03 17:25:41 -0700472
473dat_err:
Holger Schurig78cf0742008-06-02 09:25:05 +0200474 if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
475 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
Holger Schurig27590d02007-10-03 17:25:41 -0700476
477out:
478 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
479 return skb;
480}
481
Holger Schurig7919b892008-04-01 14:50:43 +0200482static irqreturn_t if_cs_interrupt(int irq, void *data)
483{
484 struct if_cs_card *card = data;
485 struct lbs_private *priv = card->priv;
486 u16 cause;
487
488 lbs_deb_enter(LBS_DEB_CS);
489
Holger Schurig43d01c52008-05-26 12:50:23 +0200490 /* Ask card interrupt cause register if there is something for us */
Holger Schurig78cf0742008-06-02 09:25:05 +0200491 cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
Holger Schurig30735562008-06-05 13:06:15 +0200492 lbs_deb_cs("cause 0x%04x\n", cause);
493
Holger Schurig7919b892008-04-01 14:50:43 +0200494 if (cause == 0) {
495 /* Not for us */
496 return IRQ_NONE;
497 }
498
499 if (cause == 0xffff) {
500 /* Read in junk, the card has probably been removed */
501 card->priv->surpriseremoved = 1;
502 return IRQ_HANDLED;
503 }
504
Holger Schurig78cf0742008-06-02 09:25:05 +0200505 if (cause & IF_CS_BIT_RX) {
Holger Schurig7919b892008-04-01 14:50:43 +0200506 struct sk_buff *skb;
507 lbs_deb_cs("rx packet\n");
508 skb = if_cs_receive_data(priv);
509 if (skb)
510 lbs_process_rxed_packet(priv, skb);
511 }
512
Holger Schurig78cf0742008-06-02 09:25:05 +0200513 if (cause & IF_CS_BIT_TX) {
Holger Schurig43d01c52008-05-26 12:50:23 +0200514 lbs_deb_cs("tx done\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200515 lbs_host_to_card_done(priv);
516 }
517
Holger Schurig78cf0742008-06-02 09:25:05 +0200518 if (cause & IF_CS_BIT_RESP) {
Holger Schurig7919b892008-04-01 14:50:43 +0200519 unsigned long flags;
520 u8 i;
521
Holger Schurig43d01c52008-05-26 12:50:23 +0200522 lbs_deb_cs("cmd resp\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200523 spin_lock_irqsave(&priv->driver_lock, flags);
524 i = (priv->resp_idx == 0) ? 1 : 0;
525 spin_unlock_irqrestore(&priv->driver_lock, flags);
526
527 BUG_ON(priv->resp_len[i]);
528 if_cs_receive_cmdres(priv, priv->resp_buf[i],
529 &priv->resp_len[i]);
530
531 spin_lock_irqsave(&priv->driver_lock, flags);
532 lbs_notify_command_response(priv, i);
533 spin_unlock_irqrestore(&priv->driver_lock, flags);
534 }
535
Holger Schurig78cf0742008-06-02 09:25:05 +0200536 if (cause & IF_CS_BIT_EVENT) {
Holger Schuriga78a8322008-06-05 13:09:50 +0200537 u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
Holger Schurig78cf0742008-06-02 09:25:05 +0200538 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
539 IF_CS_BIT_EVENT);
Holger Schuriga78a8322008-06-05 13:09:50 +0200540 lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
Holger Schurig7919b892008-04-01 14:50:43 +0200541 }
542
Holger Schurig30735562008-06-05 13:06:15 +0200543 /* Clear interrupt cause */
544 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
545
Holger Schurig43d01c52008-05-26 12:50:23 +0200546 lbs_deb_leave(LBS_DEB_CS);
Holger Schurig7919b892008-04-01 14:50:43 +0200547 return IRQ_HANDLED;
548}
549
550
551
552
553/********************************************************************/
Holger Schurig27590d02007-10-03 17:25:41 -0700554/* Firmware */
555/********************************************************************/
556
557/*
558 * Tries to program the helper firmware.
559 *
560 * Return 0 on success
561 */
562static int if_cs_prog_helper(struct if_cs_card *card)
563{
564 int ret = 0;
565 int sent = 0;
566 u8 scratch;
567 const struct firmware *fw;
568
569 lbs_deb_enter(LBS_DEB_CS);
570
Marek Vasut9d453682009-08-21 04:08:16 +0200571 /*
572 * This is the only place where an unaligned register access happens on
573 * the CF8305 card, therefore for the sake of speed of the driver, we do
574 * the alignment correction here.
575 */
576 if (card->align_regs)
577 scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
578 else
579 scratch = if_cs_read8(card, IF_CS_SCRATCH);
Holger Schurig27590d02007-10-03 17:25:41 -0700580
581 /* "If the value is 0x5a, the firmware is already
582 * downloaded successfully"
583 */
Holger Schurig53143252008-06-05 13:07:04 +0200584 if (scratch == IF_CS_SCRATCH_HELPER_OK)
Holger Schurig27590d02007-10-03 17:25:41 -0700585 goto done;
586
587 /* "If the value is != 00, it is invalid value of register */
Holger Schurig53143252008-06-05 13:07:04 +0200588 if (scratch != IF_CS_SCRATCH_BOOT_OK) {
Holger Schurig27590d02007-10-03 17:25:41 -0700589 ret = -ENODEV;
590 goto done;
591 }
592
593 /* TODO: make firmware file configurable */
594 ret = request_firmware(&fw, "libertas_cs_helper.fw",
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +0100595 &card->p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700596 if (ret) {
597 lbs_pr_err("can't load helper firmware\n");
598 ret = -ENODEV;
599 goto done;
600 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700601 lbs_deb_cs("helper size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700602
603 /* "Set the 5 bytes of the helper image to 0" */
604 /* Not needed, this contains an ARM branch instruction */
605
606 for (;;) {
607 /* "the number of bytes to send is 256" */
608 int count = 256;
609 int remain = fw->size - sent;
610
611 if (remain < count)
612 count = remain;
Holger Schurig27590d02007-10-03 17:25:41 -0700613
614 /* "write the number of bytes to be sent to the I/O Command
615 * write length register" */
Holger Schuriga78a8322008-06-05 13:09:50 +0200616 if_cs_write16(card, IF_CS_CMD_LEN, count);
Holger Schurig27590d02007-10-03 17:25:41 -0700617
618 /* "write this to I/O Command port register as 16 bit writes */
619 if (count)
Holger Schuriga78a8322008-06-05 13:09:50 +0200620 if_cs_write16_rep(card, IF_CS_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700621 &fw->data[sent],
622 count >> 1);
623
624 /* "Assert the download over interrupt command in the Host
625 * status register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200626 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700627
628 /* "Assert the download over interrupt command in the Card
629 * interrupt case register" */
Holger Schurig78cf0742008-06-02 09:25:05 +0200630 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700631
632 /* "The host polls the Card Status register ... for 50 ms before
633 declaring a failure */
Holger Schurig78cf0742008-06-02 09:25:05 +0200634 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
635 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700636 if (ret < 0) {
637 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
638 sent, ret);
Adrian Bunk9a520282008-08-28 01:05:08 +0300639 goto err_release;
Holger Schurig27590d02007-10-03 17:25:41 -0700640 }
641
642 if (count == 0)
643 break;
644
645 sent += count;
646 }
647
Adrian Bunk9a520282008-08-28 01:05:08 +0300648err_release:
Holger Schurig27590d02007-10-03 17:25:41 -0700649 release_firmware(fw);
Holger Schurig27590d02007-10-03 17:25:41 -0700650done:
651 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
652 return ret;
653}
654
655
656static int if_cs_prog_real(struct if_cs_card *card)
657{
658 const struct firmware *fw;
659 int ret = 0;
660 int retry = 0;
661 int len = 0;
662 int sent;
663
664 lbs_deb_enter(LBS_DEB_CS);
665
666 /* TODO: make firmware file configurable */
667 ret = request_firmware(&fw, "libertas_cs.fw",
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +0100668 &card->p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700669 if (ret) {
670 lbs_pr_err("can't load firmware\n");
671 ret = -ENODEV;
672 goto done;
673 }
Andrew Morton4ecd41b2007-08-21 02:15:45 -0700674 lbs_deb_cs("fw size %td\n", fw->size);
Holger Schurig27590d02007-10-03 17:25:41 -0700675
Holger Schuriga78a8322008-06-05 13:09:50 +0200676 ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
677 IF_CS_SQ_HELPER_OK);
Holger Schurig27590d02007-10-03 17:25:41 -0700678 if (ret < 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700679 lbs_pr_err("helper firmware doesn't answer\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700680 goto err_release;
681 }
682
683 for (sent = 0; sent < fw->size; sent += len) {
Holger Schuriga78a8322008-06-05 13:09:50 +0200684 len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
Holger Schurig27590d02007-10-03 17:25:41 -0700685 if (len & 1) {
686 retry++;
687 lbs_pr_info("odd, need to retry this firmware block\n");
688 } else {
689 retry = 0;
690 }
691
692 if (retry > 20) {
693 lbs_pr_err("could not download firmware\n");
694 ret = -ENODEV;
695 goto err_release;
696 }
697 if (retry) {
698 sent -= len;
699 }
700
701
Holger Schuriga78a8322008-06-05 13:09:50 +0200702 if_cs_write16(card, IF_CS_CMD_LEN, len);
Holger Schurig27590d02007-10-03 17:25:41 -0700703
Holger Schuriga78a8322008-06-05 13:09:50 +0200704 if_cs_write16_rep(card, IF_CS_CMD,
Holger Schurig27590d02007-10-03 17:25:41 -0700705 &fw->data[sent],
706 (len+1) >> 1);
Holger Schurig78cf0742008-06-02 09:25:05 +0200707 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
708 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700709
Holger Schurig78cf0742008-06-02 09:25:05 +0200710 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
711 IF_CS_BIT_COMMAND);
Holger Schurig27590d02007-10-03 17:25:41 -0700712 if (ret < 0) {
713 lbs_pr_err("can't download firmware at 0x%x\n", sent);
714 goto err_release;
715 }
716 }
717
718 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
Adrian Bunk9a520282008-08-28 01:05:08 +0300719 if (ret < 0)
Holger Schurig27590d02007-10-03 17:25:41 -0700720 lbs_pr_err("firmware download failed\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700721
722err_release:
723 release_firmware(fw);
724
725done:
726 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
727 return ret;
728}
729
730
731
732/********************************************************************/
733/* Callback functions for libertas.ko */
734/********************************************************************/
735
Holger Schurig27590d02007-10-03 17:25:41 -0700736/* Send commands or data packets to the card */
Holger Schurig69f90322007-11-23 15:43:44 +0100737static int if_cs_host_to_card(struct lbs_private *priv,
738 u8 type,
739 u8 *buf,
740 u16 nb)
Holger Schurig27590d02007-10-03 17:25:41 -0700741{
742 int ret = -1;
743
744 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
745
746 switch (type) {
747 case MVMS_DAT:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400748 priv->dnld_sent = DNLD_DATA_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700749 if_cs_send_data(priv, buf, nb);
750 ret = 0;
751 break;
752 case MVMS_CMD:
Ryan Mallon6f05cbe2007-09-06 21:30:32 -0400753 priv->dnld_sent = DNLD_CMD_SENT;
Holger Schurig27590d02007-10-03 17:25:41 -0700754 ret = if_cs_send_cmd(priv, buf, nb);
755 break;
756 default:
Harvey Harrisonc94c93d2008-07-28 23:01:34 -0700757 lbs_pr_err("%s: unsupported type %d\n", __func__, type);
Holger Schurig27590d02007-10-03 17:25:41 -0700758 }
759
760 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
761 return ret;
762}
763
764
Holger Schurig27590d02007-10-03 17:25:41 -0700765/********************************************************************/
766/* Card Services */
767/********************************************************************/
768
769/*
770 * After a card is removed, if_cs_release() will unregister the
771 * device, and release the PCMCIA configuration. If the device is
772 * still open, this will be postponed until it is closed.
773 */
774static void if_cs_release(struct pcmcia_device *p_dev)
775{
776 struct if_cs_card *card = p_dev->priv;
777
778 lbs_deb_enter(LBS_DEB_CS);
779
Holger Schurig27590d02007-10-03 17:25:41 -0700780 free_irq(p_dev->irq.AssignedIRQ, card);
Holger Schurigfdfb92e2008-01-25 14:15:48 +0100781 pcmcia_disable_device(p_dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700782 if (card->iobase)
783 ioport_unmap(card->iobase);
784
785 lbs_deb_leave(LBS_DEB_CS);
786}
787
788
789/*
790 * This creates an "instance" of the driver, allocating local data
791 * structures for one device. The device is registered with Card
792 * Services.
793 *
794 * The dev_link structure is initialized, but we don't actually
795 * configure the card at this point -- we wait until we receive a card
796 * insertion event.
797 */
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200798
799static int if_cs_ioprobe(struct pcmcia_device *p_dev,
800 cistpl_cftable_entry_t *cfg,
801 cistpl_cftable_entry_t *dflt,
802 unsigned int vcc,
803 void *priv_data)
804{
805 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
806 p_dev->io.BasePort1 = cfg->io.win[0].base;
807 p_dev->io.NumPorts1 = cfg->io.win[0].len;
808
809 /* Do we need to allocate an interrupt? */
810 if (cfg->irq.IRQInfo1)
811 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
812
813 /* IO window settings */
814 if (cfg->io.nwin != 1) {
815 lbs_pr_err("wrong CIS (check number of IO windows)\n");
816 return -ENODEV;
817 }
818
819 /* This reserves IO space but doesn't actually enable it */
820 return pcmcia_request_io(p_dev, &p_dev->io);
821}
822
Holger Schurig27590d02007-10-03 17:25:41 -0700823static int if_cs_probe(struct pcmcia_device *p_dev)
824{
825 int ret = -ENOMEM;
Marek Vasutdc7d2432009-03-24 21:33:43 +0100826 unsigned int prod_id;
Holger Schurig69f90322007-11-23 15:43:44 +0100827 struct lbs_private *priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700828 struct if_cs_card *card;
Holger Schurig27590d02007-10-03 17:25:41 -0700829
830 lbs_deb_enter(LBS_DEB_CS);
831
832 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
833 if (!card) {
834 lbs_pr_err("error in kzalloc\n");
835 goto out;
836 }
837 card->p_dev = p_dev;
838 p_dev->priv = card;
839
840 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
841 p_dev->irq.Handler = NULL;
Holger Schurig27590d02007-10-03 17:25:41 -0700842
843 p_dev->conf.Attributes = 0;
844 p_dev->conf.IntType = INT_MEMORY_AND_IO;
845
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200846 if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
847 lbs_pr_err("error in pcmcia_loop_config\n");
Holger Schurig27590d02007-10-03 17:25:41 -0700848 goto out1;
849 }
850
Holger Schurig27590d02007-10-03 17:25:41 -0700851
852 /*
853 * Allocate an interrupt line. Note that this does not assign
854 * a handler to the interrupt, unless the 'Handler' member of
855 * the irq structure is initialized.
856 */
857 if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
858 ret = pcmcia_request_irq(p_dev, &p_dev->irq);
859 if (ret) {
860 lbs_pr_err("error in pcmcia_request_irq\n");
861 goto out1;
862 }
863 }
864
865 /* Initialize io access */
866 card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
867 if (!card->iobase) {
868 lbs_pr_err("error in ioport_map\n");
869 ret = -EIO;
870 goto out1;
871 }
872
873 /*
874 * This actually configures the PCMCIA socket -- setting up
875 * the I/O windows and the interrupt mapping, and putting the
876 * card and host interface into "Memory and IO" mode.
877 */
878 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
879 if (ret) {
880 lbs_pr_err("error in pcmcia_request_configuration\n");
881 goto out2;
882 }
883
884 /* Finally, report what we've done */
885 lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
886 p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
887 p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
888
Marek Vasut9d453682009-08-21 04:08:16 +0200889 /*
890 * Most of the libertas cards can do unaligned register access, but some
891 * weird ones can not. That's especially true for the CF8305 card.
892 */
893 card->align_regs = 0;
894
Holger Schurig4c555232008-06-05 13:08:35 +0200895 /* Check if we have a current silicon */
Marek Vasutdc7d2432009-03-24 21:33:43 +0100896 prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
Marek Vasut9d453682009-08-21 04:08:16 +0200897 if (if_cs_hw_is_cf8305(p_dev)) {
898 card->align_regs = 1;
899 if (prod_id < IF_CS_CF8305_B1_REV) {
900 lbs_pr_err("old chips like 8305 rev B3 "
901 "aren't supported\n");
902 ret = -ENODEV;
903 goto out2;
904 }
905 }
906
Marek Vasutdc7d2432009-03-24 21:33:43 +0100907 if (if_cs_hw_is_cf8381(p_dev) && prod_id < IF_CS_CF8381_B3_REV) {
908 lbs_pr_err("old chips like 8381 rev B3 aren't supported\n");
909 ret = -ENODEV;
910 goto out2;
911 }
912
913 if (if_cs_hw_is_cf8385(p_dev) && prod_id < IF_CS_CF8385_B1_REV) {
Holger Schurig4c555232008-06-05 13:08:35 +0200914 lbs_pr_err("old chips like 8385 rev B1 aren't supported\n");
915 ret = -ENODEV;
916 goto out2;
917 }
Holger Schurig27590d02007-10-03 17:25:41 -0700918
919 /* Load the firmware early, before calling into libertas.ko */
920 ret = if_cs_prog_helper(card);
Marek Vasut9d453682009-08-21 04:08:16 +0200921 if (ret == 0 && !if_cs_hw_is_cf8305(p_dev))
Holger Schurig27590d02007-10-03 17:25:41 -0700922 ret = if_cs_prog_real(card);
923 if (ret)
924 goto out2;
925
926 /* Make this card known to the libertas driver */
Holger Schurig10078322007-11-15 18:05:47 -0500927 priv = lbs_add_card(card, &p_dev->dev);
Holger Schurig27590d02007-10-03 17:25:41 -0700928 if (!priv) {
929 ret = -ENOMEM;
930 goto out2;
931 }
932
Holger Schurig7919b892008-04-01 14:50:43 +0200933 /* Finish setting up fields in lbs_private */
Dan Williams954ee162007-08-20 11:43:25 -0400934 card->priv = priv;
Holger Schurig27590d02007-10-03 17:25:41 -0700935 priv->card = card;
Holger Schurig7919b892008-04-01 14:50:43 +0200936 priv->hw_host_to_card = if_cs_host_to_card;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700937 priv->enter_deep_sleep = NULL;
938 priv->exit_deep_sleep = NULL;
939 priv->reset_deep_sleep_wakeup = NULL;
David Woodhouseaa21c002007-12-08 20:04:36 +0000940 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400941
Holger Schurig27590d02007-10-03 17:25:41 -0700942 /* Now actually get the IRQ */
943 ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
944 IRQF_SHARED, DRV_NAME, card);
945 if (ret) {
946 lbs_pr_err("error in request_irq\n");
947 goto out3;
948 }
949
Holger Schurig4ef31702007-10-09 10:41:57 +0200950 /* Clear any interrupt cause that happend while sending
951 * firmware/initializing card */
Holger Schurig78cf0742008-06-02 09:25:05 +0200952 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400953 if_cs_enable_ints(card);
954
Holger Schurig27590d02007-10-03 17:25:41 -0700955 /* And finally bring the card up */
Holger Schurig10078322007-11-15 18:05:47 -0500956 if (lbs_start_card(priv) != 0) {
Holger Schurig27590d02007-10-03 17:25:41 -0700957 lbs_pr_err("could not activate card\n");
958 goto out3;
959 }
960
961 ret = 0;
962 goto out;
963
964out3:
Holger Schurig10078322007-11-15 18:05:47 -0500965 lbs_remove_card(priv);
Holger Schurig27590d02007-10-03 17:25:41 -0700966out2:
967 ioport_unmap(card->iobase);
968out1:
969 pcmcia_disable_device(p_dev);
970out:
971 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
972 return ret;
973}
974
975
976/*
977 * This deletes a driver "instance". The device is de-registered with
978 * Card Services. If it has been released, all local data structures
979 * are freed. Otherwise, the structures will be freed when the device
980 * is released.
981 */
982static void if_cs_detach(struct pcmcia_device *p_dev)
983{
984 struct if_cs_card *card = p_dev->priv;
985
986 lbs_deb_enter(LBS_DEB_CS);
987
Holger Schurig10078322007-11-15 18:05:47 -0500988 lbs_stop_card(card->priv);
989 lbs_remove_card(card->priv);
Ryan Mallon28de0b32007-09-06 21:32:42 -0400990 if_cs_disable_ints(card);
Holger Schurig27590d02007-10-03 17:25:41 -0700991 if_cs_release(p_dev);
992 kfree(card);
993
994 lbs_deb_leave(LBS_DEB_CS);
995}
996
997
998
999/********************************************************************/
1000/* Module initialization */
1001/********************************************************************/
1002
1003static struct pcmcia_device_id if_cs_ids[] = {
Marek Vasut9d453682009-08-21 04:08:16 +02001004 PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
Marek Vasutdc7d2432009-03-24 21:33:43 +01001005 PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
1006 PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
Holger Schurig27590d02007-10-03 17:25:41 -07001007 PCMCIA_DEVICE_NULL,
1008};
1009MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
1010
1011
Holger Schurig10078322007-11-15 18:05:47 -05001012static struct pcmcia_driver lbs_driver = {
Holger Schurig27590d02007-10-03 17:25:41 -07001013 .owner = THIS_MODULE,
1014 .drv = {
1015 .name = DRV_NAME,
1016 },
1017 .probe = if_cs_probe,
1018 .remove = if_cs_detach,
1019 .id_table = if_cs_ids,
1020};
1021
1022
1023static int __init if_cs_init(void)
1024{
1025 int ret;
1026
1027 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -05001028 ret = pcmcia_register_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -07001029 lbs_deb_leave(LBS_DEB_CS);
1030 return ret;
1031}
1032
1033
1034static void __exit if_cs_exit(void)
1035{
1036 lbs_deb_enter(LBS_DEB_CS);
Holger Schurig10078322007-11-15 18:05:47 -05001037 pcmcia_unregister_driver(&lbs_driver);
Holger Schurig27590d02007-10-03 17:25:41 -07001038 lbs_deb_leave(LBS_DEB_CS);
1039}
1040
1041
1042module_init(if_cs_init);
1043module_exit(if_cs_exit);