Commit | Line | Data |
---|---|---|
27590d06 HS |
1 | /* |
2 | ||
3 | Driver for the Marvell 8385 based compact flash WLAN cards. | |
4 | ||
5 | (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de> | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor, | |
20 | Boston, MA 02110-1301, USA. | |
21 | ||
22 | */ | |
23 | ||
24 | #include <linux/module.h> | |
25 | #include <linux/delay.h> | |
26 | #include <linux/moduleparam.h> | |
27 | #include <linux/firmware.h> | |
28 | #include <linux/netdevice.h> | |
29 | ||
30 | #include <pcmcia/cs_types.h> | |
31 | #include <pcmcia/cs.h> | |
32 | #include <pcmcia/cistpl.h> | |
33 | #include <pcmcia/ds.h> | |
34 | ||
35 | #include <linux/io.h> | |
36 | ||
37 | #define DRV_NAME "libertas_cs" | |
38 | ||
39 | #include "decl.h" | |
40 | #include "defs.h" | |
41 | #include "dev.h" | |
42 | ||
43 | ||
44 | /********************************************************************/ | |
45 | /* Module stuff */ | |
46 | /********************************************************************/ | |
47 | ||
48 | MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>"); | |
49 | MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards"); | |
50 | MODULE_LICENSE("GPL"); | |
51 | ||
52 | ||
53 | ||
54 | /********************************************************************/ | |
55 | /* Data structures */ | |
56 | /********************************************************************/ | |
57 | ||
58 | struct if_cs_card { | |
59 | struct pcmcia_device *p_dev; | |
69f9032d | 60 | struct lbs_private *priv; |
27590d06 HS |
61 | void __iomem *iobase; |
62 | }; | |
63 | ||
64 | ||
65 | ||
66 | /********************************************************************/ | |
67 | /* Hardware access */ | |
68 | /********************************************************************/ | |
69 | ||
70 | /* This define enables wrapper functions which allow you | |
71 | to dump all register accesses. You normally won't this, | |
72 | except for development */ | |
73 | /* #define DEBUG_IO */ | |
74 | ||
75 | #ifdef DEBUG_IO | |
76 | static int debug_output = 0; | |
77 | #else | |
78 | /* This way the compiler optimizes the printk's away */ | |
79 | #define debug_output 0 | |
80 | #endif | |
81 | ||
82 | static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg) | |
83 | { | |
84 | unsigned int val = ioread8(card->iobase + reg); | |
85 | if (debug_output) | |
86 | printk(KERN_INFO "##inb %08x<%02x\n", reg, val); | |
87 | return val; | |
88 | } | |
89 | static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg) | |
90 | { | |
91 | unsigned int val = ioread16(card->iobase + reg); | |
92 | if (debug_output) | |
93 | printk(KERN_INFO "##inw %08x<%04x\n", reg, val); | |
94 | return val; | |
95 | } | |
96 | static inline void if_cs_read16_rep( | |
97 | struct if_cs_card *card, | |
98 | uint reg, | |
99 | void *buf, | |
100 | unsigned long count) | |
101 | { | |
102 | if (debug_output) | |
103 | printk(KERN_INFO "##insw %08x<(0x%lx words)\n", | |
104 | reg, count); | |
105 | ioread16_rep(card->iobase + reg, buf, count); | |
106 | } | |
107 | ||
108 | static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val) | |
109 | { | |
110 | if (debug_output) | |
111 | printk(KERN_INFO "##outb %08x>%02x\n", reg, val); | |
112 | iowrite8(val, card->iobase + reg); | |
113 | } | |
114 | ||
115 | static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val) | |
116 | { | |
117 | if (debug_output) | |
118 | printk(KERN_INFO "##outw %08x>%04x\n", reg, val); | |
119 | iowrite16(val, card->iobase + reg); | |
120 | } | |
121 | ||
122 | static inline void if_cs_write16_rep( | |
123 | struct if_cs_card *card, | |
124 | uint reg, | |
125 | void *buf, | |
126 | unsigned long count) | |
127 | { | |
128 | if (debug_output) | |
129 | printk(KERN_INFO "##outsw %08x>(0x%lx words)\n", | |
130 | reg, count); | |
131 | iowrite16_rep(card->iobase + reg, buf, count); | |
132 | } | |
133 | ||
134 | ||
135 | /* | |
136 | * I know that polling/delaying is frowned upon. However, this procedure | |
137 | * with polling is needed while downloading the firmware. At this stage, | |
138 | * the hardware does unfortunately not create any interrupts. | |
139 | * | |
140 | * Fortunately, this function is never used once the firmware is in | |
141 | * the card. :-) | |
142 | * | |
143 | * As a reference, see the "Firmware Specification v5.1", page 18 | |
144 | * and 19. I did not follow their suggested timing to the word, | |
145 | * but this works nice & fast anyway. | |
146 | */ | |
147 | static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg) | |
148 | { | |
149 | int i; | |
150 | ||
4ef31702 | 151 | for (i = 0; i < 1000; i++) { |
27590d06 HS |
152 | u8 val = if_cs_read8(card, addr); |
153 | if (val == reg) | |
154 | return i; | |
4ef31702 | 155 | udelay(500); |
27590d06 HS |
156 | } |
157 | return -ETIME; | |
158 | } | |
159 | ||
160 | ||
161 | ||
162 | /* Host control registers and their bit definitions */ | |
163 | ||
164 | #define IF_CS_H_STATUS 0x00000000 | |
165 | #define IF_CS_H_STATUS_TX_OVER 0x0001 | |
166 | #define IF_CS_H_STATUS_RX_OVER 0x0002 | |
167 | #define IF_CS_H_STATUS_DNLD_OVER 0x0004 | |
168 | ||
169 | #define IF_CS_H_INT_CAUSE 0x00000002 | |
170 | #define IF_CS_H_IC_TX_OVER 0x0001 | |
171 | #define IF_CS_H_IC_RX_OVER 0x0002 | |
172 | #define IF_CS_H_IC_DNLD_OVER 0x0004 | |
6591e36a HS |
173 | #define IF_CS_H_IC_POWER_DOWN 0x0008 |
174 | #define IF_CS_H_IC_HOST_EVENT 0x0010 | |
27590d06 HS |
175 | #define IF_CS_H_IC_MASK 0x001f |
176 | ||
177 | #define IF_CS_H_INT_MASK 0x00000004 | |
178 | #define IF_CS_H_IM_MASK 0x001f | |
179 | ||
180 | #define IF_CS_H_WRITE_LEN 0x00000014 | |
181 | ||
182 | #define IF_CS_H_WRITE 0x00000016 | |
183 | ||
184 | #define IF_CS_H_CMD_LEN 0x00000018 | |
185 | ||
186 | #define IF_CS_H_CMD 0x0000001A | |
187 | ||
188 | #define IF_CS_C_READ_LEN 0x00000024 | |
189 | ||
190 | #define IF_CS_H_READ 0x00000010 | |
191 | ||
192 | /* Card control registers and their bit definitions */ | |
193 | ||
194 | #define IF_CS_C_STATUS 0x00000020 | |
195 | #define IF_CS_C_S_TX_DNLD_RDY 0x0001 | |
196 | #define IF_CS_C_S_RX_UPLD_RDY 0x0002 | |
197 | #define IF_CS_C_S_CMD_DNLD_RDY 0x0004 | |
198 | #define IF_CS_C_S_CMD_UPLD_RDY 0x0008 | |
199 | #define IF_CS_C_S_CARDEVENT 0x0010 | |
200 | #define IF_CS_C_S_MASK 0x001f | |
201 | #define IF_CS_C_S_STATUS_MASK 0x7f00 | |
202 | /* The following definitions should be the same as the MRVDRV_ ones */ | |
203 | ||
204 | #if MRVDRV_CMD_DNLD_RDY != IF_CS_C_S_CMD_DNLD_RDY | |
205 | #error MRVDRV_CMD_DNLD_RDY and IF_CS_C_S_CMD_DNLD_RDY not in sync | |
206 | #endif | |
207 | #if MRVDRV_CMD_UPLD_RDY != IF_CS_C_S_CMD_UPLD_RDY | |
208 | #error MRVDRV_CMD_UPLD_RDY and IF_CS_C_S_CMD_UPLD_RDY not in sync | |
209 | #endif | |
210 | #if MRVDRV_CARDEVENT != IF_CS_C_S_CARDEVENT | |
211 | #error MRVDRV_CARDEVENT and IF_CS_C_S_CARDEVENT not in sync | |
212 | #endif | |
213 | ||
214 | #define IF_CS_C_INT_CAUSE 0x00000022 | |
215 | #define IF_CS_C_IC_MASK 0x001f | |
216 | ||
217 | #define IF_CS_C_SQ_READ_LOW 0x00000028 | |
218 | #define IF_CS_C_SQ_HELPER_OK 0x10 | |
219 | ||
220 | #define IF_CS_C_CMD_LEN 0x00000030 | |
221 | ||
222 | #define IF_CS_C_CMD 0x00000012 | |
223 | ||
224 | #define IF_CS_SCRATCH 0x0000003F | |
225 | ||
226 | ||
227 | ||
228 | /********************************************************************/ | |
229 | /* Interrupts */ | |
230 | /********************************************************************/ | |
231 | ||
232 | static inline void if_cs_enable_ints(struct if_cs_card *card) | |
233 | { | |
234 | lbs_deb_enter(LBS_DEB_CS); | |
235 | if_cs_write16(card, IF_CS_H_INT_MASK, 0); | |
236 | } | |
237 | ||
238 | static inline void if_cs_disable_ints(struct if_cs_card *card) | |
239 | { | |
240 | lbs_deb_enter(LBS_DEB_CS); | |
241 | if_cs_write16(card, IF_CS_H_INT_MASK, IF_CS_H_IM_MASK); | |
242 | } | |
243 | ||
244 | static irqreturn_t if_cs_interrupt(int irq, void *data) | |
245 | { | |
28fc1f5a | 246 | struct if_cs_card *card = data; |
27590d06 HS |
247 | u16 int_cause; |
248 | ||
249 | lbs_deb_enter(LBS_DEB_CS); | |
250 | ||
251 | int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE); | |
fdfb92ea | 252 | if (int_cause == 0x0) { |
28de0b36 | 253 | /* Not for us */ |
27590d06 | 254 | return IRQ_NONE; |
28de0b36 | 255 | |
e775ed7c | 256 | } else if (int_cause == 0xffff) { |
28de0b36 | 257 | /* Read in junk, the card has probably been removed */ |
aa21c004 | 258 | card->priv->surpriseremoved = 1; |
fdfb92ea | 259 | return IRQ_HANDLED; |
28de0b36 | 260 | } else { |
e775ed7c DW |
261 | if (int_cause & IF_CS_H_IC_TX_OVER) |
262 | lbs_host_to_card_done(card->priv); | |
28de0b36 | 263 | |
27590d06 HS |
264 | /* clear interrupt */ |
265 | if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause & IF_CS_C_IC_MASK); | |
27590d06 | 266 | } |
4f679496 DW |
267 | spin_lock(&card->priv->driver_lock); |
268 | lbs_interrupt(card->priv); | |
269 | spin_unlock(&card->priv->driver_lock); | |
27590d06 HS |
270 | |
271 | return IRQ_HANDLED; | |
272 | } | |
273 | ||
274 | ||
275 | ||
276 | ||
277 | /********************************************************************/ | |
278 | /* I/O */ | |
279 | /********************************************************************/ | |
280 | ||
281 | /* | |
282 | * Called from if_cs_host_to_card to send a command to the hardware | |
283 | */ | |
69f9032d | 284 | static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb) |
27590d06 HS |
285 | { |
286 | struct if_cs_card *card = (struct if_cs_card *)priv->card; | |
287 | int ret = -1; | |
288 | int loops = 0; | |
289 | ||
290 | lbs_deb_enter(LBS_DEB_CS); | |
291 | ||
292 | /* Is hardware ready? */ | |
293 | while (1) { | |
294 | u16 val = if_cs_read16(card, IF_CS_C_STATUS); | |
295 | if (val & IF_CS_C_S_CMD_DNLD_RDY) | |
296 | break; | |
297 | if (++loops > 100) { | |
298 | lbs_pr_err("card not ready for commands\n"); | |
299 | goto done; | |
300 | } | |
301 | mdelay(1); | |
302 | } | |
303 | ||
304 | if_cs_write16(card, IF_CS_H_CMD_LEN, nb); | |
305 | ||
306 | if_cs_write16_rep(card, IF_CS_H_CMD, buf, nb / 2); | |
307 | /* Are we supposed to transfer an odd amount of bytes? */ | |
308 | if (nb & 1) | |
309 | if_cs_write8(card, IF_CS_H_CMD, buf[nb-1]); | |
310 | ||
311 | /* "Assert the download over interrupt command in the Host | |
312 | * status register" */ | |
313 | if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER); | |
314 | ||
315 | /* "Assert the download over interrupt command in the Card | |
316 | * interrupt case register" */ | |
317 | if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER); | |
318 | ret = 0; | |
319 | ||
320 | done: | |
321 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); | |
322 | return ret; | |
323 | } | |
324 | ||
325 | ||
326 | /* | |
327 | * Called from if_cs_host_to_card to send a data to the hardware | |
328 | */ | |
69f9032d | 329 | static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb) |
27590d06 HS |
330 | { |
331 | struct if_cs_card *card = (struct if_cs_card *)priv->card; | |
332 | ||
333 | lbs_deb_enter(LBS_DEB_CS); | |
334 | ||
335 | if_cs_write16(card, IF_CS_H_WRITE_LEN, nb); | |
336 | ||
337 | /* write even number of bytes, then odd byte if necessary */ | |
338 | if_cs_write16_rep(card, IF_CS_H_WRITE, buf, nb / 2); | |
339 | if (nb & 1) | |
340 | if_cs_write8(card, IF_CS_H_WRITE, buf[nb-1]); | |
341 | ||
342 | if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_TX_OVER); | |
343 | if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_STATUS_TX_OVER); | |
344 | ||
345 | lbs_deb_leave(LBS_DEB_CS); | |
346 | } | |
347 | ||
348 | ||
349 | /* | |
350 | * Get the command result out of the card. | |
351 | */ | |
69f9032d | 352 | static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len) |
27590d06 HS |
353 | { |
354 | int ret = -1; | |
355 | u16 val; | |
356 | ||
357 | lbs_deb_enter(LBS_DEB_CS); | |
358 | ||
359 | /* is hardware ready? */ | |
360 | val = if_cs_read16(priv->card, IF_CS_C_STATUS); | |
361 | if ((val & IF_CS_C_S_CMD_UPLD_RDY) == 0) { | |
362 | lbs_pr_err("card not ready for CMD\n"); | |
363 | goto out; | |
364 | } | |
365 | ||
366 | *len = if_cs_read16(priv->card, IF_CS_C_CMD_LEN); | |
ddac4526 | 367 | if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) { |
27590d06 HS |
368 | lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len); |
369 | goto out; | |
370 | } | |
371 | ||
372 | /* read even number of bytes, then odd byte if necessary */ | |
373 | if_cs_read16_rep(priv->card, IF_CS_C_CMD, data, *len/sizeof(u16)); | |
374 | if (*len & 1) | |
375 | data[*len-1] = if_cs_read8(priv->card, IF_CS_C_CMD); | |
376 | ||
09d4fad6 HS |
377 | /* This is a workaround for a firmware that reports too much |
378 | * bytes */ | |
379 | *len -= 8; | |
27590d06 HS |
380 | ret = 0; |
381 | out: | |
382 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len); | |
383 | return ret; | |
384 | } | |
385 | ||
386 | ||
69f9032d | 387 | static struct sk_buff *if_cs_receive_data(struct lbs_private *priv) |
27590d06 HS |
388 | { |
389 | struct sk_buff *skb = NULL; | |
390 | u16 len; | |
391 | u8 *data; | |
392 | ||
393 | lbs_deb_enter(LBS_DEB_CS); | |
394 | ||
395 | len = if_cs_read16(priv->card, IF_CS_C_READ_LEN); | |
396 | if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { | |
397 | lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len); | |
398 | priv->stats.rx_dropped++; | |
399 | printk(KERN_INFO "##HS %s:%d TODO\n", __FUNCTION__, __LINE__); | |
400 | goto dat_err; | |
401 | } | |
402 | ||
403 | //TODO: skb = dev_alloc_skb(len+ETH_FRAME_LEN+MRVDRV_SNAP_HEADER_LEN+EXTRA_LEN); | |
f31ce76b | 404 | skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2); |
27590d06 HS |
405 | if (!skb) |
406 | goto out; | |
f31ce76b VD |
407 | skb_put(skb, len); |
408 | skb_reserve(skb, 2);/* 16 byte align */ | |
409 | data = skb->data; | |
27590d06 HS |
410 | |
411 | /* read even number of bytes, then odd byte if necessary */ | |
412 | if_cs_read16_rep(priv->card, IF_CS_H_READ, data, len/sizeof(u16)); | |
413 | if (len & 1) | |
414 | data[len-1] = if_cs_read8(priv->card, IF_CS_H_READ); | |
415 | ||
416 | dat_err: | |
417 | if_cs_write16(priv->card, IF_CS_H_STATUS, IF_CS_H_STATUS_RX_OVER); | |
418 | if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_RX_OVER); | |
419 | ||
420 | out: | |
421 | lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb); | |
422 | return skb; | |
423 | } | |
424 | ||
425 | ||
426 | ||
427 | /********************************************************************/ | |
428 | /* Firmware */ | |
429 | /********************************************************************/ | |
430 | ||
431 | /* | |
432 | * Tries to program the helper firmware. | |
433 | * | |
434 | * Return 0 on success | |
435 | */ | |
436 | static int if_cs_prog_helper(struct if_cs_card *card) | |
437 | { | |
438 | int ret = 0; | |
439 | int sent = 0; | |
440 | u8 scratch; | |
441 | const struct firmware *fw; | |
442 | ||
443 | lbs_deb_enter(LBS_DEB_CS); | |
444 | ||
445 | scratch = if_cs_read8(card, IF_CS_SCRATCH); | |
446 | ||
447 | /* "If the value is 0x5a, the firmware is already | |
448 | * downloaded successfully" | |
449 | */ | |
450 | if (scratch == 0x5a) | |
451 | goto done; | |
452 | ||
453 | /* "If the value is != 00, it is invalid value of register */ | |
454 | if (scratch != 0x00) { | |
455 | ret = -ENODEV; | |
456 | goto done; | |
457 | } | |
458 | ||
459 | /* TODO: make firmware file configurable */ | |
460 | ret = request_firmware(&fw, "libertas_cs_helper.fw", | |
461 | &handle_to_dev(card->p_dev)); | |
462 | if (ret) { | |
463 | lbs_pr_err("can't load helper firmware\n"); | |
464 | ret = -ENODEV; | |
465 | goto done; | |
466 | } | |
4ecd41bd | 467 | lbs_deb_cs("helper size %td\n", fw->size); |
27590d06 HS |
468 | |
469 | /* "Set the 5 bytes of the helper image to 0" */ | |
470 | /* Not needed, this contains an ARM branch instruction */ | |
471 | ||
472 | for (;;) { | |
473 | /* "the number of bytes to send is 256" */ | |
474 | int count = 256; | |
475 | int remain = fw->size - sent; | |
476 | ||
477 | if (remain < count) | |
478 | count = remain; | |
479 | /* printk(KERN_INFO "//HS %d loading %d of %d bytes\n", | |
480 | __LINE__, sent, fw->size); */ | |
481 | ||
482 | /* "write the number of bytes to be sent to the I/O Command | |
483 | * write length register" */ | |
484 | if_cs_write16(card, IF_CS_H_CMD_LEN, count); | |
485 | ||
486 | /* "write this to I/O Command port register as 16 bit writes */ | |
487 | if (count) | |
488 | if_cs_write16_rep(card, IF_CS_H_CMD, | |
489 | &fw->data[sent], | |
490 | count >> 1); | |
491 | ||
492 | /* "Assert the download over interrupt command in the Host | |
493 | * status register" */ | |
494 | if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER); | |
495 | ||
496 | /* "Assert the download over interrupt command in the Card | |
497 | * interrupt case register" */ | |
498 | if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER); | |
499 | ||
500 | /* "The host polls the Card Status register ... for 50 ms before | |
501 | declaring a failure */ | |
502 | ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS, | |
503 | IF_CS_C_S_CMD_DNLD_RDY); | |
504 | if (ret < 0) { | |
505 | lbs_pr_err("can't download helper at 0x%x, ret %d\n", | |
506 | sent, ret); | |
507 | goto done; | |
508 | } | |
509 | ||
510 | if (count == 0) | |
511 | break; | |
512 | ||
513 | sent += count; | |
514 | } | |
515 | ||
516 | release_firmware(fw); | |
517 | ret = 0; | |
518 | ||
519 | done: | |
520 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); | |
521 | return ret; | |
522 | } | |
523 | ||
524 | ||
525 | static int if_cs_prog_real(struct if_cs_card *card) | |
526 | { | |
527 | const struct firmware *fw; | |
528 | int ret = 0; | |
529 | int retry = 0; | |
530 | int len = 0; | |
531 | int sent; | |
532 | ||
533 | lbs_deb_enter(LBS_DEB_CS); | |
534 | ||
535 | /* TODO: make firmware file configurable */ | |
536 | ret = request_firmware(&fw, "libertas_cs.fw", | |
537 | &handle_to_dev(card->p_dev)); | |
538 | if (ret) { | |
539 | lbs_pr_err("can't load firmware\n"); | |
540 | ret = -ENODEV; | |
541 | goto done; | |
542 | } | |
4ecd41bd | 543 | lbs_deb_cs("fw size %td\n", fw->size); |
27590d06 HS |
544 | |
545 | ret = if_cs_poll_while_fw_download(card, IF_CS_C_SQ_READ_LOW, IF_CS_C_SQ_HELPER_OK); | |
546 | if (ret < 0) { | |
547 | int i; | |
548 | lbs_pr_err("helper firmware doesn't answer\n"); | |
549 | for (i = 0; i < 0x50; i += 2) | |
550 | printk(KERN_INFO "## HS %02x: %04x\n", | |
551 | i, if_cs_read16(card, i)); | |
552 | goto err_release; | |
553 | } | |
554 | ||
555 | for (sent = 0; sent < fw->size; sent += len) { | |
556 | len = if_cs_read16(card, IF_CS_C_SQ_READ_LOW); | |
557 | /* printk(KERN_INFO "//HS %d loading %d of %d bytes\n", | |
558 | __LINE__, sent, fw->size); */ | |
559 | if (len & 1) { | |
560 | retry++; | |
561 | lbs_pr_info("odd, need to retry this firmware block\n"); | |
562 | } else { | |
563 | retry = 0; | |
564 | } | |
565 | ||
566 | if (retry > 20) { | |
567 | lbs_pr_err("could not download firmware\n"); | |
568 | ret = -ENODEV; | |
569 | goto err_release; | |
570 | } | |
571 | if (retry) { | |
572 | sent -= len; | |
573 | } | |
574 | ||
575 | ||
576 | if_cs_write16(card, IF_CS_H_CMD_LEN, len); | |
577 | ||
578 | if_cs_write16_rep(card, IF_CS_H_CMD, | |
579 | &fw->data[sent], | |
580 | (len+1) >> 1); | |
581 | if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER); | |
582 | if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER); | |
583 | ||
584 | ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS, | |
585 | IF_CS_C_S_CMD_DNLD_RDY); | |
586 | if (ret < 0) { | |
587 | lbs_pr_err("can't download firmware at 0x%x\n", sent); | |
588 | goto err_release; | |
589 | } | |
590 | } | |
591 | ||
592 | ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a); | |
593 | if (ret < 0) { | |
594 | lbs_pr_err("firmware download failed\n"); | |
595 | goto err_release; | |
596 | } | |
597 | ||
598 | ret = 0; | |
599 | goto done; | |
600 | ||
601 | ||
602 | err_release: | |
603 | release_firmware(fw); | |
604 | ||
605 | done: | |
606 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); | |
607 | return ret; | |
608 | } | |
609 | ||
610 | ||
611 | ||
612 | /********************************************************************/ | |
613 | /* Callback functions for libertas.ko */ | |
614 | /********************************************************************/ | |
615 | ||
27590d06 | 616 | /* Send commands or data packets to the card */ |
69f9032d HS |
617 | static int if_cs_host_to_card(struct lbs_private *priv, |
618 | u8 type, | |
619 | u8 *buf, | |
620 | u16 nb) | |
27590d06 HS |
621 | { |
622 | int ret = -1; | |
623 | ||
624 | lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb); | |
625 | ||
626 | switch (type) { | |
627 | case MVMS_DAT: | |
6f05cbe5 | 628 | priv->dnld_sent = DNLD_DATA_SENT; |
27590d06 HS |
629 | if_cs_send_data(priv, buf, nb); |
630 | ret = 0; | |
631 | break; | |
632 | case MVMS_CMD: | |
6f05cbe5 | 633 | priv->dnld_sent = DNLD_CMD_SENT; |
27590d06 HS |
634 | ret = if_cs_send_cmd(priv, buf, nb); |
635 | break; | |
636 | default: | |
637 | lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type); | |
638 | } | |
639 | ||
640 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); | |
641 | return ret; | |
642 | } | |
643 | ||
644 | ||
69f9032d | 645 | static int if_cs_get_int_status(struct lbs_private *priv, u8 *ireg) |
27590d06 HS |
646 | { |
647 | struct if_cs_card *card = (struct if_cs_card *)priv->card; | |
27590d06 HS |
648 | int ret = 0; |
649 | u16 int_cause; | |
27590d06 HS |
650 | *ireg = 0; |
651 | ||
652 | lbs_deb_enter(LBS_DEB_CS); | |
653 | ||
aa21c004 | 654 | if (priv->surpriseremoved) |
27590d06 HS |
655 | goto out; |
656 | ||
657 | int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE) & IF_CS_C_IC_MASK; | |
658 | if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause); | |
659 | ||
660 | *ireg = if_cs_read16(card, IF_CS_C_STATUS) & IF_CS_C_S_MASK; | |
27590d06 HS |
661 | |
662 | if (!*ireg) | |
663 | goto sbi_get_int_status_exit; | |
664 | ||
665 | sbi_get_int_status_exit: | |
666 | ||
667 | /* is there a data packet for us? */ | |
668 | if (*ireg & IF_CS_C_S_RX_UPLD_RDY) { | |
669 | struct sk_buff *skb = if_cs_receive_data(priv); | |
10078321 | 670 | lbs_process_rxed_packet(priv, skb); |
27590d06 HS |
671 | *ireg &= ~IF_CS_C_S_RX_UPLD_RDY; |
672 | } | |
673 | ||
674 | if (*ireg & IF_CS_C_S_TX_DNLD_RDY) { | |
675 | priv->dnld_sent = DNLD_RES_RECEIVED; | |
676 | } | |
677 | ||
678 | /* Card has a command result for us */ | |
679 | if (*ireg & IF_CS_C_S_CMD_UPLD_RDY) { | |
7003b078 | 680 | ret = if_cs_receive_cmdres(priv, priv->upld_buf, &priv->upld_len); |
27590d06 HS |
681 | if (ret < 0) |
682 | lbs_pr_err("could not receive cmd from card\n"); | |
683 | } | |
684 | ||
685 | out: | |
aa21c004 | 686 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d, ireg 0x%x, hisregcpy 0x%x", ret, *ireg, priv->hisregcpy); |
27590d06 HS |
687 | return ret; |
688 | } | |
689 | ||
690 | ||
69f9032d | 691 | static int if_cs_read_event_cause(struct lbs_private *priv) |
27590d06 HS |
692 | { |
693 | lbs_deb_enter(LBS_DEB_CS); | |
694 | ||
aa21c004 | 695 | priv->eventcause = (if_cs_read16(priv->card, IF_CS_C_STATUS) & IF_CS_C_S_STATUS_MASK) >> 5; |
27590d06 HS |
696 | if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_HOST_EVENT); |
697 | ||
698 | return 0; | |
699 | } | |
700 | ||
701 | ||
702 | ||
703 | /********************************************************************/ | |
704 | /* Card Services */ | |
705 | /********************************************************************/ | |
706 | ||
707 | /* | |
708 | * After a card is removed, if_cs_release() will unregister the | |
709 | * device, and release the PCMCIA configuration. If the device is | |
710 | * still open, this will be postponed until it is closed. | |
711 | */ | |
712 | static void if_cs_release(struct pcmcia_device *p_dev) | |
713 | { | |
714 | struct if_cs_card *card = p_dev->priv; | |
715 | ||
716 | lbs_deb_enter(LBS_DEB_CS); | |
717 | ||
27590d06 | 718 | free_irq(p_dev->irq.AssignedIRQ, card); |
fdfb92ea | 719 | pcmcia_disable_device(p_dev); |
27590d06 HS |
720 | if (card->iobase) |
721 | ioport_unmap(card->iobase); | |
722 | ||
723 | lbs_deb_leave(LBS_DEB_CS); | |
724 | } | |
725 | ||
726 | ||
727 | /* | |
728 | * This creates an "instance" of the driver, allocating local data | |
729 | * structures for one device. The device is registered with Card | |
730 | * Services. | |
731 | * | |
732 | * The dev_link structure is initialized, but we don't actually | |
733 | * configure the card at this point -- we wait until we receive a card | |
734 | * insertion event. | |
735 | */ | |
736 | static int if_cs_probe(struct pcmcia_device *p_dev) | |
737 | { | |
738 | int ret = -ENOMEM; | |
69f9032d | 739 | struct lbs_private *priv; |
27590d06 HS |
740 | struct if_cs_card *card; |
741 | /* CIS parsing */ | |
742 | tuple_t tuple; | |
743 | cisparse_t parse; | |
744 | cistpl_cftable_entry_t *cfg = &parse.cftable_entry; | |
745 | cistpl_io_t *io = &cfg->io; | |
746 | u_char buf[64]; | |
747 | ||
748 | lbs_deb_enter(LBS_DEB_CS); | |
749 | ||
750 | card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL); | |
751 | if (!card) { | |
752 | lbs_pr_err("error in kzalloc\n"); | |
753 | goto out; | |
754 | } | |
755 | card->p_dev = p_dev; | |
756 | p_dev->priv = card; | |
757 | ||
758 | p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING; | |
759 | p_dev->irq.Handler = NULL; | |
760 | p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID; | |
761 | ||
762 | p_dev->conf.Attributes = 0; | |
763 | p_dev->conf.IntType = INT_MEMORY_AND_IO; | |
764 | ||
765 | tuple.Attributes = 0; | |
766 | tuple.TupleData = buf; | |
767 | tuple.TupleDataMax = sizeof(buf); | |
768 | tuple.TupleOffset = 0; | |
769 | ||
770 | tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; | |
771 | if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 || | |
772 | (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 || | |
773 | (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0) | |
774 | { | |
775 | lbs_pr_err("error in pcmcia_get_first_tuple etc\n"); | |
776 | goto out1; | |
777 | } | |
778 | ||
779 | p_dev->conf.ConfigIndex = cfg->index; | |
780 | ||
781 | /* Do we need to allocate an interrupt? */ | |
782 | if (cfg->irq.IRQInfo1) { | |
783 | p_dev->conf.Attributes |= CONF_ENABLE_IRQ; | |
784 | } | |
785 | ||
786 | /* IO window settings */ | |
787 | if (cfg->io.nwin != 1) { | |
788 | lbs_pr_err("wrong CIS (check number of IO windows)\n"); | |
789 | ret = -ENODEV; | |
790 | goto out1; | |
791 | } | |
792 | p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO; | |
793 | p_dev->io.BasePort1 = io->win[0].base; | |
794 | p_dev->io.NumPorts1 = io->win[0].len; | |
795 | ||
796 | /* This reserves IO space but doesn't actually enable it */ | |
797 | ret = pcmcia_request_io(p_dev, &p_dev->io); | |
798 | if (ret) { | |
799 | lbs_pr_err("error in pcmcia_request_io\n"); | |
800 | goto out1; | |
801 | } | |
802 | ||
803 | /* | |
804 | * Allocate an interrupt line. Note that this does not assign | |
805 | * a handler to the interrupt, unless the 'Handler' member of | |
806 | * the irq structure is initialized. | |
807 | */ | |
808 | if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) { | |
809 | ret = pcmcia_request_irq(p_dev, &p_dev->irq); | |
810 | if (ret) { | |
811 | lbs_pr_err("error in pcmcia_request_irq\n"); | |
812 | goto out1; | |
813 | } | |
814 | } | |
815 | ||
816 | /* Initialize io access */ | |
817 | card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1); | |
818 | if (!card->iobase) { | |
819 | lbs_pr_err("error in ioport_map\n"); | |
820 | ret = -EIO; | |
821 | goto out1; | |
822 | } | |
823 | ||
824 | /* | |
825 | * This actually configures the PCMCIA socket -- setting up | |
826 | * the I/O windows and the interrupt mapping, and putting the | |
827 | * card and host interface into "Memory and IO" mode. | |
828 | */ | |
829 | ret = pcmcia_request_configuration(p_dev, &p_dev->conf); | |
830 | if (ret) { | |
831 | lbs_pr_err("error in pcmcia_request_configuration\n"); | |
832 | goto out2; | |
833 | } | |
834 | ||
835 | /* Finally, report what we've done */ | |
836 | lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n", | |
837 | p_dev->irq.AssignedIRQ, p_dev->io.BasePort1, | |
838 | p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1); | |
839 | ||
27590d06 HS |
840 | |
841 | /* Load the firmware early, before calling into libertas.ko */ | |
842 | ret = if_cs_prog_helper(card); | |
843 | if (ret == 0) | |
844 | ret = if_cs_prog_real(card); | |
845 | if (ret) | |
846 | goto out2; | |
847 | ||
848 | /* Make this card known to the libertas driver */ | |
10078321 | 849 | priv = lbs_add_card(card, &p_dev->dev); |
27590d06 HS |
850 | if (!priv) { |
851 | ret = -ENOMEM; | |
852 | goto out2; | |
853 | } | |
854 | ||
855 | /* Store pointers to our call-back functions */ | |
954ee164 | 856 | card->priv = priv; |
27590d06 | 857 | priv->card = card; |
27590d06 HS |
858 | priv->hw_host_to_card = if_cs_host_to_card; |
859 | priv->hw_get_int_status = if_cs_get_int_status; | |
860 | priv->hw_read_event_cause = if_cs_read_event_cause; | |
861 | ||
aa21c004 | 862 | priv->fw_ready = 1; |
954ee164 | 863 | |
27590d06 HS |
864 | /* Now actually get the IRQ */ |
865 | ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt, | |
866 | IRQF_SHARED, DRV_NAME, card); | |
867 | if (ret) { | |
868 | lbs_pr_err("error in request_irq\n"); | |
869 | goto out3; | |
870 | } | |
871 | ||
4ef31702 HS |
872 | /* Clear any interrupt cause that happend while sending |
873 | * firmware/initializing card */ | |
874 | if_cs_write16(card, IF_CS_C_INT_CAUSE, IF_CS_C_IC_MASK); | |
28de0b36 RM |
875 | if_cs_enable_ints(card); |
876 | ||
27590d06 | 877 | /* And finally bring the card up */ |
10078321 | 878 | if (lbs_start_card(priv) != 0) { |
27590d06 HS |
879 | lbs_pr_err("could not activate card\n"); |
880 | goto out3; | |
881 | } | |
882 | ||
d4ff0ef6 HS |
883 | /* The firmware for the CF card supports powersave */ |
884 | priv->ps_supported = 1; | |
885 | ||
27590d06 HS |
886 | ret = 0; |
887 | goto out; | |
888 | ||
889 | out3: | |
10078321 | 890 | lbs_remove_card(priv); |
27590d06 HS |
891 | out2: |
892 | ioport_unmap(card->iobase); | |
893 | out1: | |
894 | pcmcia_disable_device(p_dev); | |
895 | out: | |
896 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); | |
897 | return ret; | |
898 | } | |
899 | ||
900 | ||
901 | /* | |
902 | * This deletes a driver "instance". The device is de-registered with | |
903 | * Card Services. If it has been released, all local data structures | |
904 | * are freed. Otherwise, the structures will be freed when the device | |
905 | * is released. | |
906 | */ | |
907 | static void if_cs_detach(struct pcmcia_device *p_dev) | |
908 | { | |
909 | struct if_cs_card *card = p_dev->priv; | |
910 | ||
911 | lbs_deb_enter(LBS_DEB_CS); | |
912 | ||
10078321 HS |
913 | lbs_stop_card(card->priv); |
914 | lbs_remove_card(card->priv); | |
28de0b36 | 915 | if_cs_disable_ints(card); |
27590d06 HS |
916 | if_cs_release(p_dev); |
917 | kfree(card); | |
918 | ||
919 | lbs_deb_leave(LBS_DEB_CS); | |
920 | } | |
921 | ||
922 | ||
923 | ||
924 | /********************************************************************/ | |
925 | /* Module initialization */ | |
926 | /********************************************************************/ | |
927 | ||
928 | static struct pcmcia_device_id if_cs_ids[] = { | |
929 | PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103), | |
930 | PCMCIA_DEVICE_NULL, | |
931 | }; | |
932 | MODULE_DEVICE_TABLE(pcmcia, if_cs_ids); | |
933 | ||
934 | ||
10078321 | 935 | static struct pcmcia_driver lbs_driver = { |
27590d06 HS |
936 | .owner = THIS_MODULE, |
937 | .drv = { | |
938 | .name = DRV_NAME, | |
939 | }, | |
940 | .probe = if_cs_probe, | |
941 | .remove = if_cs_detach, | |
942 | .id_table = if_cs_ids, | |
943 | }; | |
944 | ||
945 | ||
946 | static int __init if_cs_init(void) | |
947 | { | |
948 | int ret; | |
949 | ||
950 | lbs_deb_enter(LBS_DEB_CS); | |
10078321 | 951 | ret = pcmcia_register_driver(&lbs_driver); |
27590d06 HS |
952 | lbs_deb_leave(LBS_DEB_CS); |
953 | return ret; | |
954 | } | |
955 | ||
956 | ||
957 | static void __exit if_cs_exit(void) | |
958 | { | |
959 | lbs_deb_enter(LBS_DEB_CS); | |
10078321 | 960 | pcmcia_unregister_driver(&lbs_driver); |
27590d06 HS |
961 | lbs_deb_leave(LBS_DEB_CS); |
962 | } | |
963 | ||
964 | ||
965 | module_init(if_cs_init); | |
966 | module_exit(if_cs_exit); |