/kernel/2.6.32_froyo_photon_nightly/drivers/net/wireless/libertas/if_cs.c

http://photon-android.googlecode.com/ · C · 1052 lines · 623 code · 184 blank · 245 comment · 92 complexity · 1819a9e4900ff1974decee561b00bbe9 MD5 · raw file

  1. /*
  2. Driver for the Marvell 8385 based compact flash WLAN cards.
  3. (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/firmware.h>
  21. #include <linux/netdevice.h>
  22. #include <pcmcia/cs_types.h>
  23. #include <pcmcia/cs.h>
  24. #include <pcmcia/cistpl.h>
  25. #include <pcmcia/ds.h>
  26. #include <linux/io.h>
  27. #define DRV_NAME "libertas_cs"
  28. #include "decl.h"
  29. #include "defs.h"
  30. #include "dev.h"
  31. /********************************************************************/
  32. /* Module stuff */
  33. /********************************************************************/
  34. MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
  35. MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
  36. MODULE_LICENSE("GPL");
  37. /********************************************************************/
  38. /* Data structures */
  39. /********************************************************************/
  40. struct if_cs_card {
  41. struct pcmcia_device *p_dev;
  42. struct lbs_private *priv;
  43. void __iomem *iobase;
  44. bool align_regs;
  45. };
  46. /********************************************************************/
  47. /* Hardware access */
  48. /********************************************************************/
  49. /* This define enables wrapper functions which allow you
  50. to dump all register accesses. You normally won't this,
  51. except for development */
  52. /* #define DEBUG_IO */
  53. #ifdef DEBUG_IO
  54. static int debug_output = 0;
  55. #else
  56. /* This way the compiler optimizes the printk's away */
  57. #define debug_output 0
  58. #endif
  59. static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
  60. {
  61. unsigned int val = ioread8(card->iobase + reg);
  62. if (debug_output)
  63. printk(KERN_INFO "inb %08x<%02x\n", reg, val);
  64. return val;
  65. }
  66. static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
  67. {
  68. unsigned int val = ioread16(card->iobase + reg);
  69. if (debug_output)
  70. printk(KERN_INFO "inw %08x<%04x\n", reg, val);
  71. return val;
  72. }
  73. static inline void if_cs_read16_rep(
  74. struct if_cs_card *card,
  75. uint reg,
  76. void *buf,
  77. unsigned long count)
  78. {
  79. if (debug_output)
  80. printk(KERN_INFO "insw %08x<(0x%lx words)\n",
  81. reg, count);
  82. ioread16_rep(card->iobase + reg, buf, count);
  83. }
  84. static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
  85. {
  86. if (debug_output)
  87. printk(KERN_INFO "outb %08x>%02x\n", reg, val);
  88. iowrite8(val, card->iobase + reg);
  89. }
  90. static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
  91. {
  92. if (debug_output)
  93. printk(KERN_INFO "outw %08x>%04x\n", reg, val);
  94. iowrite16(val, card->iobase + reg);
  95. }
  96. static inline void if_cs_write16_rep(
  97. struct if_cs_card *card,
  98. uint reg,
  99. const void *buf,
  100. unsigned long count)
  101. {
  102. if (debug_output)
  103. printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
  104. reg, count);
  105. iowrite16_rep(card->iobase + reg, buf, count);
  106. }
  107. /*
  108. * I know that polling/delaying is frowned upon. However, this procedure
  109. * with polling is needed while downloading the firmware. At this stage,
  110. * the hardware does unfortunately not create any interrupts.
  111. *
  112. * Fortunately, this function is never used once the firmware is in
  113. * the card. :-)
  114. *
  115. * As a reference, see the "Firmware Specification v5.1", page 18
  116. * and 19. I did not follow their suggested timing to the word,
  117. * but this works nice & fast anyway.
  118. */
  119. static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
  120. {
  121. int i;
  122. for (i = 0; i < 100000; i++) {
  123. u8 val = if_cs_read8(card, addr);
  124. if (val == reg)
  125. return 0;
  126. udelay(5);
  127. }
  128. return -ETIME;
  129. }
  130. /*
  131. * First the bitmasks for the host/card interrupt/status registers:
  132. */
  133. #define IF_CS_BIT_TX 0x0001
  134. #define IF_CS_BIT_RX 0x0002
  135. #define IF_CS_BIT_COMMAND 0x0004
  136. #define IF_CS_BIT_RESP 0x0008
  137. #define IF_CS_BIT_EVENT 0x0010
  138. #define IF_CS_BIT_MASK 0x001f
  139. /*
  140. * It's not really clear to me what the host status register is for. It
  141. * needs to be set almost in union with "host int cause". The following
  142. * bits from above are used:
  143. *
  144. * IF_CS_BIT_TX driver downloaded a data packet
  145. * IF_CS_BIT_RX driver got a data packet
  146. * IF_CS_BIT_COMMAND driver downloaded a command
  147. * IF_CS_BIT_RESP not used (has some meaning with powerdown)
  148. * IF_CS_BIT_EVENT driver read a host event
  149. */
  150. #define IF_CS_HOST_STATUS 0x00000000
  151. /*
  152. * With the host int cause register can the host (that is, Linux) cause
  153. * an interrupt in the firmware, to tell the firmware about those events:
  154. *
  155. * IF_CS_BIT_TX a data packet has been downloaded
  156. * IF_CS_BIT_RX a received data packet has retrieved
  157. * IF_CS_BIT_COMMAND a firmware block or a command has been downloaded
  158. * IF_CS_BIT_RESP not used (has some meaning with powerdown)
  159. * IF_CS_BIT_EVENT a host event (link lost etc) has been retrieved
  160. */
  161. #define IF_CS_HOST_INT_CAUSE 0x00000002
  162. /*
  163. * The host int mask register is used to enable/disable interrupt. However,
  164. * I have the suspicion that disabled interrupts are lost.
  165. */
  166. #define IF_CS_HOST_INT_MASK 0x00000004
  167. /*
  168. * Used to send or receive data packets:
  169. */
  170. #define IF_CS_WRITE 0x00000016
  171. #define IF_CS_WRITE_LEN 0x00000014
  172. #define IF_CS_READ 0x00000010
  173. #define IF_CS_READ_LEN 0x00000024
  174. /*
  175. * Used to send commands (and to send firmware block) and to
  176. * receive command responses:
  177. */
  178. #define IF_CS_CMD 0x0000001A
  179. #define IF_CS_CMD_LEN 0x00000018
  180. #define IF_CS_RESP 0x00000012
  181. #define IF_CS_RESP_LEN 0x00000030
  182. /*
  183. * The card status registers shows what the card/firmware actually
  184. * accepts:
  185. *
  186. * IF_CS_BIT_TX you may send a data packet
  187. * IF_CS_BIT_RX you may retrieve a data packet
  188. * IF_CS_BIT_COMMAND you may send a command
  189. * IF_CS_BIT_RESP you may retrieve a command response
  190. * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
  191. *
  192. * When reading this register several times, you will get back the same
  193. * results --- with one exception: the IF_CS_BIT_EVENT clear itself
  194. * automatically.
  195. *
  196. * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
  197. * we handle this via the card int cause register.
  198. */
  199. #define IF_CS_CARD_STATUS 0x00000020
  200. #define IF_CS_CARD_STATUS_MASK 0x7f00
  201. /*
  202. * The card int cause register is used by the card/firmware to notify us
  203. * about the following events:
  204. *
  205. * IF_CS_BIT_TX a data packet has successfully been sentx
  206. * IF_CS_BIT_RX a data packet has been received and can be retrieved
  207. * IF_CS_BIT_COMMAND not used
  208. * IF_CS_BIT_RESP the firmware has a command response for us
  209. * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
  210. */
  211. #define IF_CS_CARD_INT_CAUSE 0x00000022
  212. /*
  213. * This is used to for handshaking with the card's bootloader/helper image
  214. * to synchronize downloading of firmware blocks.
  215. */
  216. #define IF_CS_SQ_READ_LOW 0x00000028
  217. #define IF_CS_SQ_HELPER_OK 0x10
  218. /*
  219. * The scratch register tells us ...
  220. *
  221. * IF_CS_SCRATCH_BOOT_OK the bootloader runs
  222. * IF_CS_SCRATCH_HELPER_OK the helper firmware already runs
  223. */
  224. #define IF_CS_SCRATCH 0x0000003F
  225. #define IF_CS_SCRATCH_BOOT_OK 0x00
  226. #define IF_CS_SCRATCH_HELPER_OK 0x5a
  227. /*
  228. * Used to detect ancient chips:
  229. */
  230. #define IF_CS_PRODUCT_ID 0x0000001C
  231. #define IF_CS_CF8385_B1_REV 0x12
  232. #define IF_CS_CF8381_B3_REV 0x04
  233. #define IF_CS_CF8305_B1_REV 0x03
  234. /*
  235. * Used to detect other cards than CF8385 since their revisions of silicon
  236. * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
  237. */
  238. #define CF8305_MANFID 0x02db
  239. #define CF8305_CARDID 0x8103
  240. #define CF8381_MANFID 0x02db
  241. #define CF8381_CARDID 0x6064
  242. #define CF8385_MANFID 0x02df
  243. #define CF8385_CARDID 0x8103
  244. static inline int if_cs_hw_is_cf8305(struct pcmcia_device *p_dev)
  245. {
  246. return (p_dev->manf_id == CF8305_MANFID &&
  247. p_dev->card_id == CF8305_CARDID);
  248. }
  249. static inline int if_cs_hw_is_cf8381(struct pcmcia_device *p_dev)
  250. {
  251. return (p_dev->manf_id == CF8381_MANFID &&
  252. p_dev->card_id == CF8381_CARDID);
  253. }
  254. static inline int if_cs_hw_is_cf8385(struct pcmcia_device *p_dev)
  255. {
  256. return (p_dev->manf_id == CF8385_MANFID &&
  257. p_dev->card_id == CF8385_CARDID);
  258. }
  259. /********************************************************************/
  260. /* I/O and interrupt handling */
  261. /********************************************************************/
  262. static inline void if_cs_enable_ints(struct if_cs_card *card)
  263. {
  264. lbs_deb_enter(LBS_DEB_CS);
  265. if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
  266. }
  267. static inline void if_cs_disable_ints(struct if_cs_card *card)
  268. {
  269. lbs_deb_enter(LBS_DEB_CS);
  270. if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
  271. }
  272. /*
  273. * Called from if_cs_host_to_card to send a command to the hardware
  274. */
  275. static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
  276. {
  277. struct if_cs_card *card = (struct if_cs_card *)priv->card;
  278. int ret = -1;
  279. int loops = 0;
  280. lbs_deb_enter(LBS_DEB_CS);
  281. if_cs_disable_ints(card);
  282. /* Is hardware ready? */
  283. while (1) {
  284. u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
  285. if (status & IF_CS_BIT_COMMAND)
  286. break;
  287. if (++loops > 100) {
  288. lbs_pr_err("card not ready for commands\n");
  289. goto done;
  290. }
  291. mdelay(1);
  292. }
  293. if_cs_write16(card, IF_CS_CMD_LEN, nb);
  294. if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
  295. /* Are we supposed to transfer an odd amount of bytes? */
  296. if (nb & 1)
  297. if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
  298. /* "Assert the download over interrupt command in the Host
  299. * status register" */
  300. if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  301. /* "Assert the download over interrupt command in the Card
  302. * interrupt case register" */
  303. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  304. ret = 0;
  305. done:
  306. if_cs_enable_ints(card);
  307. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  308. return ret;
  309. }
  310. /*
  311. * Called from if_cs_host_to_card to send a data to the hardware
  312. */
  313. static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
  314. {
  315. struct if_cs_card *card = (struct if_cs_card *)priv->card;
  316. u16 status;
  317. lbs_deb_enter(LBS_DEB_CS);
  318. if_cs_disable_ints(card);
  319. status = if_cs_read16(card, IF_CS_CARD_STATUS);
  320. BUG_ON((status & IF_CS_BIT_TX) == 0);
  321. if_cs_write16(card, IF_CS_WRITE_LEN, nb);
  322. /* write even number of bytes, then odd byte if necessary */
  323. if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
  324. if (nb & 1)
  325. if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
  326. if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
  327. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
  328. if_cs_enable_ints(card);
  329. lbs_deb_leave(LBS_DEB_CS);
  330. }
  331. /*
  332. * Get the command result out of the card.
  333. */
  334. static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
  335. {
  336. unsigned long flags;
  337. int ret = -1;
  338. u16 status;
  339. lbs_deb_enter(LBS_DEB_CS);
  340. /* is hardware ready? */
  341. status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
  342. if ((status & IF_CS_BIT_RESP) == 0) {
  343. lbs_pr_err("no cmd response in card\n");
  344. *len = 0;
  345. goto out;
  346. }
  347. *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
  348. if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
  349. lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
  350. goto out;
  351. }
  352. /* read even number of bytes, then odd byte if necessary */
  353. if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
  354. if (*len & 1)
  355. data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
  356. /* This is a workaround for a firmware that reports too much
  357. * bytes */
  358. *len -= 8;
  359. ret = 0;
  360. /* Clear this flag again */
  361. spin_lock_irqsave(&priv->driver_lock, flags);
  362. priv->dnld_sent = DNLD_RES_RECEIVED;
  363. spin_unlock_irqrestore(&priv->driver_lock, flags);
  364. out:
  365. lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
  366. return ret;
  367. }
  368. static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
  369. {
  370. struct sk_buff *skb = NULL;
  371. u16 len;
  372. u8 *data;
  373. lbs_deb_enter(LBS_DEB_CS);
  374. len = if_cs_read16(priv->card, IF_CS_READ_LEN);
  375. if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
  376. lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
  377. priv->dev->stats.rx_dropped++;
  378. goto dat_err;
  379. }
  380. skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
  381. if (!skb)
  382. goto out;
  383. skb_put(skb, len);
  384. skb_reserve(skb, 2);/* 16 byte align */
  385. data = skb->data;
  386. /* read even number of bytes, then odd byte if necessary */
  387. if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
  388. if (len & 1)
  389. data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
  390. dat_err:
  391. if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
  392. if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
  393. out:
  394. lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
  395. return skb;
  396. }
  397. static irqreturn_t if_cs_interrupt(int irq, void *data)
  398. {
  399. struct if_cs_card *card = data;
  400. struct lbs_private *priv = card->priv;
  401. u16 cause;
  402. lbs_deb_enter(LBS_DEB_CS);
  403. /* Ask card interrupt cause register if there is something for us */
  404. cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
  405. lbs_deb_cs("cause 0x%04x\n", cause);
  406. if (cause == 0) {
  407. /* Not for us */
  408. return IRQ_NONE;
  409. }
  410. if (cause == 0xffff) {
  411. /* Read in junk, the card has probably been removed */
  412. card->priv->surpriseremoved = 1;
  413. return IRQ_HANDLED;
  414. }
  415. if (cause & IF_CS_BIT_RX) {
  416. struct sk_buff *skb;
  417. lbs_deb_cs("rx packet\n");
  418. skb = if_cs_receive_data(priv);
  419. if (skb)
  420. lbs_process_rxed_packet(priv, skb);
  421. }
  422. if (cause & IF_CS_BIT_TX) {
  423. lbs_deb_cs("tx done\n");
  424. lbs_host_to_card_done(priv);
  425. }
  426. if (cause & IF_CS_BIT_RESP) {
  427. unsigned long flags;
  428. u8 i;
  429. lbs_deb_cs("cmd resp\n");
  430. spin_lock_irqsave(&priv->driver_lock, flags);
  431. i = (priv->resp_idx == 0) ? 1 : 0;
  432. spin_unlock_irqrestore(&priv->driver_lock, flags);
  433. BUG_ON(priv->resp_len[i]);
  434. if_cs_receive_cmdres(priv, priv->resp_buf[i],
  435. &priv->resp_len[i]);
  436. spin_lock_irqsave(&priv->driver_lock, flags);
  437. lbs_notify_command_response(priv, i);
  438. spin_unlock_irqrestore(&priv->driver_lock, flags);
  439. }
  440. if (cause & IF_CS_BIT_EVENT) {
  441. u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
  442. if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
  443. IF_CS_BIT_EVENT);
  444. lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
  445. }
  446. /* Clear interrupt cause */
  447. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
  448. lbs_deb_leave(LBS_DEB_CS);
  449. return IRQ_HANDLED;
  450. }
  451. /********************************************************************/
  452. /* Firmware */
  453. /********************************************************************/
  454. /*
  455. * Tries to program the helper firmware.
  456. *
  457. * Return 0 on success
  458. */
  459. static int if_cs_prog_helper(struct if_cs_card *card)
  460. {
  461. int ret = 0;
  462. int sent = 0;
  463. u8 scratch;
  464. const struct firmware *fw;
  465. lbs_deb_enter(LBS_DEB_CS);
  466. /*
  467. * This is the only place where an unaligned register access happens on
  468. * the CF8305 card, therefore for the sake of speed of the driver, we do
  469. * the alignment correction here.
  470. */
  471. if (card->align_regs)
  472. scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
  473. else
  474. scratch = if_cs_read8(card, IF_CS_SCRATCH);
  475. /* "If the value is 0x5a, the firmware is already
  476. * downloaded successfully"
  477. */
  478. if (scratch == IF_CS_SCRATCH_HELPER_OK)
  479. goto done;
  480. /* "If the value is != 00, it is invalid value of register */
  481. if (scratch != IF_CS_SCRATCH_BOOT_OK) {
  482. ret = -ENODEV;
  483. goto done;
  484. }
  485. /* TODO: make firmware file configurable */
  486. ret = request_firmware(&fw, "libertas_cs_helper.fw",
  487. &handle_to_dev(card->p_dev));
  488. if (ret) {
  489. lbs_pr_err("can't load helper firmware\n");
  490. ret = -ENODEV;
  491. goto done;
  492. }
  493. lbs_deb_cs("helper size %td\n", fw->size);
  494. /* "Set the 5 bytes of the helper image to 0" */
  495. /* Not needed, this contains an ARM branch instruction */
  496. for (;;) {
  497. /* "the number of bytes to send is 256" */
  498. int count = 256;
  499. int remain = fw->size - sent;
  500. if (remain < count)
  501. count = remain;
  502. /* "write the number of bytes to be sent to the I/O Command
  503. * write length register" */
  504. if_cs_write16(card, IF_CS_CMD_LEN, count);
  505. /* "write this to I/O Command port register as 16 bit writes */
  506. if (count)
  507. if_cs_write16_rep(card, IF_CS_CMD,
  508. &fw->data[sent],
  509. count >> 1);
  510. /* "Assert the download over interrupt command in the Host
  511. * status register" */
  512. if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  513. /* "Assert the download over interrupt command in the Card
  514. * interrupt case register" */
  515. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  516. /* "The host polls the Card Status register ... for 50 ms before
  517. declaring a failure */
  518. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
  519. IF_CS_BIT_COMMAND);
  520. if (ret < 0) {
  521. lbs_pr_err("can't download helper at 0x%x, ret %d\n",
  522. sent, ret);
  523. goto err_release;
  524. }
  525. if (count == 0)
  526. break;
  527. sent += count;
  528. }
  529. err_release:
  530. release_firmware(fw);
  531. done:
  532. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  533. return ret;
  534. }
  535. static int if_cs_prog_real(struct if_cs_card *card)
  536. {
  537. const struct firmware *fw;
  538. int ret = 0;
  539. int retry = 0;
  540. int len = 0;
  541. int sent;
  542. lbs_deb_enter(LBS_DEB_CS);
  543. /* TODO: make firmware file configurable */
  544. ret = request_firmware(&fw, "libertas_cs.fw",
  545. &handle_to_dev(card->p_dev));
  546. if (ret) {
  547. lbs_pr_err("can't load firmware\n");
  548. ret = -ENODEV;
  549. goto done;
  550. }
  551. lbs_deb_cs("fw size %td\n", fw->size);
  552. ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
  553. IF_CS_SQ_HELPER_OK);
  554. if (ret < 0) {
  555. lbs_pr_err("helper firmware doesn't answer\n");
  556. goto err_release;
  557. }
  558. for (sent = 0; sent < fw->size; sent += len) {
  559. len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
  560. if (len & 1) {
  561. retry++;
  562. lbs_pr_info("odd, need to retry this firmware block\n");
  563. } else {
  564. retry = 0;
  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. if_cs_write16(card, IF_CS_CMD_LEN, len);
  575. if_cs_write16_rep(card, IF_CS_CMD,
  576. &fw->data[sent],
  577. (len+1) >> 1);
  578. if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  579. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  580. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
  581. IF_CS_BIT_COMMAND);
  582. if (ret < 0) {
  583. lbs_pr_err("can't download firmware at 0x%x\n", sent);
  584. goto err_release;
  585. }
  586. }
  587. ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
  588. if (ret < 0)
  589. lbs_pr_err("firmware download failed\n");
  590. err_release:
  591. release_firmware(fw);
  592. done:
  593. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  594. return ret;
  595. }
  596. /********************************************************************/
  597. /* Callback functions for libertas.ko */
  598. /********************************************************************/
  599. /* Send commands or data packets to the card */
  600. static int if_cs_host_to_card(struct lbs_private *priv,
  601. u8 type,
  602. u8 *buf,
  603. u16 nb)
  604. {
  605. int ret = -1;
  606. lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
  607. switch (type) {
  608. case MVMS_DAT:
  609. priv->dnld_sent = DNLD_DATA_SENT;
  610. if_cs_send_data(priv, buf, nb);
  611. ret = 0;
  612. break;
  613. case MVMS_CMD:
  614. priv->dnld_sent = DNLD_CMD_SENT;
  615. ret = if_cs_send_cmd(priv, buf, nb);
  616. break;
  617. default:
  618. lbs_pr_err("%s: unsupported type %d\n", __func__, type);
  619. }
  620. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  621. return ret;
  622. }
  623. /********************************************************************/
  624. /* Card Services */
  625. /********************************************************************/
  626. /*
  627. * After a card is removed, if_cs_release() will unregister the
  628. * device, and release the PCMCIA configuration. If the device is
  629. * still open, this will be postponed until it is closed.
  630. */
  631. static void if_cs_release(struct pcmcia_device *p_dev)
  632. {
  633. struct if_cs_card *card = p_dev->priv;
  634. lbs_deb_enter(LBS_DEB_CS);
  635. free_irq(p_dev->irq.AssignedIRQ, card);
  636. pcmcia_disable_device(p_dev);
  637. if (card->iobase)
  638. ioport_unmap(card->iobase);
  639. lbs_deb_leave(LBS_DEB_CS);
  640. }
  641. /*
  642. * This creates an "instance" of the driver, allocating local data
  643. * structures for one device. The device is registered with Card
  644. * Services.
  645. *
  646. * The dev_link structure is initialized, but we don't actually
  647. * configure the card at this point -- we wait until we receive a card
  648. * insertion event.
  649. */
  650. static int if_cs_probe(struct pcmcia_device *p_dev)
  651. {
  652. int ret = -ENOMEM;
  653. unsigned int prod_id;
  654. struct lbs_private *priv;
  655. struct if_cs_card *card;
  656. /* CIS parsing */
  657. tuple_t tuple;
  658. cisparse_t parse;
  659. cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
  660. cistpl_io_t *io = &cfg->io;
  661. u_char buf[64];
  662. lbs_deb_enter(LBS_DEB_CS);
  663. card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
  664. if (!card) {
  665. lbs_pr_err("error in kzalloc\n");
  666. goto out;
  667. }
  668. card->p_dev = p_dev;
  669. p_dev->priv = card;
  670. p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
  671. p_dev->irq.Handler = NULL;
  672. p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
  673. p_dev->conf.Attributes = 0;
  674. p_dev->conf.IntType = INT_MEMORY_AND_IO;
  675. tuple.Attributes = 0;
  676. tuple.TupleData = buf;
  677. tuple.TupleDataMax = sizeof(buf);
  678. tuple.TupleOffset = 0;
  679. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  680. if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
  681. (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
  682. (ret = pcmcia_parse_tuple(&tuple, &parse)) != 0)
  683. {
  684. lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
  685. goto out1;
  686. }
  687. p_dev->conf.ConfigIndex = cfg->index;
  688. /* Do we need to allocate an interrupt? */
  689. if (cfg->irq.IRQInfo1) {
  690. p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
  691. }
  692. /* IO window settings */
  693. if (cfg->io.nwin != 1) {
  694. lbs_pr_err("wrong CIS (check number of IO windows)\n");
  695. ret = -ENODEV;
  696. goto out1;
  697. }
  698. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  699. p_dev->io.BasePort1 = io->win[0].base;
  700. p_dev->io.NumPorts1 = io->win[0].len;
  701. /* This reserves IO space but doesn't actually enable it */
  702. ret = pcmcia_request_io(p_dev, &p_dev->io);
  703. if (ret) {
  704. lbs_pr_err("error in pcmcia_request_io\n");
  705. goto out1;
  706. }
  707. /*
  708. * Allocate an interrupt line. Note that this does not assign
  709. * a handler to the interrupt, unless the 'Handler' member of
  710. * the irq structure is initialized.
  711. */
  712. if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
  713. ret = pcmcia_request_irq(p_dev, &p_dev->irq);
  714. if (ret) {
  715. lbs_pr_err("error in pcmcia_request_irq\n");
  716. goto out1;
  717. }
  718. }
  719. /* Initialize io access */
  720. card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
  721. if (!card->iobase) {
  722. lbs_pr_err("error in ioport_map\n");
  723. ret = -EIO;
  724. goto out1;
  725. }
  726. /*
  727. * This actually configures the PCMCIA socket -- setting up
  728. * the I/O windows and the interrupt mapping, and putting the
  729. * card and host interface into "Memory and IO" mode.
  730. */
  731. ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
  732. if (ret) {
  733. lbs_pr_err("error in pcmcia_request_configuration\n");
  734. goto out2;
  735. }
  736. /* Finally, report what we've done */
  737. lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
  738. p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
  739. p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
  740. /*
  741. * Most of the libertas cards can do unaligned register access, but some
  742. * weird ones can not. That's especially true for the CF8305 card.
  743. */
  744. card->align_regs = 0;
  745. /* Check if we have a current silicon */
  746. prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
  747. if (if_cs_hw_is_cf8305(p_dev)) {
  748. card->align_regs = 1;
  749. if (prod_id < IF_CS_CF8305_B1_REV) {
  750. lbs_pr_err("old chips like 8305 rev B3 "
  751. "aren't supported\n");
  752. ret = -ENODEV;
  753. goto out2;
  754. }
  755. }
  756. if (if_cs_hw_is_cf8381(p_dev) && prod_id < IF_CS_CF8381_B3_REV) {
  757. lbs_pr_err("old chips like 8381 rev B3 aren't supported\n");
  758. ret = -ENODEV;
  759. goto out2;
  760. }
  761. if (if_cs_hw_is_cf8385(p_dev) && prod_id < IF_CS_CF8385_B1_REV) {
  762. lbs_pr_err("old chips like 8385 rev B1 aren't supported\n");
  763. ret = -ENODEV;
  764. goto out2;
  765. }
  766. /* Load the firmware early, before calling into libertas.ko */
  767. ret = if_cs_prog_helper(card);
  768. if (ret == 0 && !if_cs_hw_is_cf8305(p_dev))
  769. ret = if_cs_prog_real(card);
  770. if (ret)
  771. goto out2;
  772. /* Make this card known to the libertas driver */
  773. priv = lbs_add_card(card, &p_dev->dev);
  774. if (!priv) {
  775. ret = -ENOMEM;
  776. goto out2;
  777. }
  778. /* Finish setting up fields in lbs_private */
  779. card->priv = priv;
  780. priv->card = card;
  781. priv->hw_host_to_card = if_cs_host_to_card;
  782. priv->fw_ready = 1;
  783. /* Now actually get the IRQ */
  784. ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
  785. IRQF_SHARED, DRV_NAME, card);
  786. if (ret) {
  787. lbs_pr_err("error in request_irq\n");
  788. goto out3;
  789. }
  790. /* Clear any interrupt cause that happend while sending
  791. * firmware/initializing card */
  792. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
  793. if_cs_enable_ints(card);
  794. /* And finally bring the card up */
  795. if (lbs_start_card(priv) != 0) {
  796. lbs_pr_err("could not activate card\n");
  797. goto out3;
  798. }
  799. ret = 0;
  800. goto out;
  801. out3:
  802. lbs_remove_card(priv);
  803. out2:
  804. ioport_unmap(card->iobase);
  805. out1:
  806. pcmcia_disable_device(p_dev);
  807. out:
  808. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  809. return ret;
  810. }
  811. /*
  812. * This deletes a driver "instance". The device is de-registered with
  813. * Card Services. If it has been released, all local data structures
  814. * are freed. Otherwise, the structures will be freed when the device
  815. * is released.
  816. */
  817. static void if_cs_detach(struct pcmcia_device *p_dev)
  818. {
  819. struct if_cs_card *card = p_dev->priv;
  820. lbs_deb_enter(LBS_DEB_CS);
  821. lbs_stop_card(card->priv);
  822. lbs_remove_card(card->priv);
  823. if_cs_disable_ints(card);
  824. if_cs_release(p_dev);
  825. kfree(card);
  826. lbs_deb_leave(LBS_DEB_CS);
  827. }
  828. /********************************************************************/
  829. /* Module initialization */
  830. /********************************************************************/
  831. static struct pcmcia_device_id if_cs_ids[] = {
  832. PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
  833. PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
  834. PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
  835. PCMCIA_DEVICE_NULL,
  836. };
  837. MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
  838. static struct pcmcia_driver lbs_driver = {
  839. .owner = THIS_MODULE,
  840. .drv = {
  841. .name = DRV_NAME,
  842. },
  843. .probe = if_cs_probe,
  844. .remove = if_cs_detach,
  845. .id_table = if_cs_ids,
  846. };
  847. static int __init if_cs_init(void)
  848. {
  849. int ret;
  850. lbs_deb_enter(LBS_DEB_CS);
  851. ret = pcmcia_register_driver(&lbs_driver);
  852. lbs_deb_leave(LBS_DEB_CS);
  853. return ret;
  854. }
  855. static void __exit if_cs_exit(void)
  856. {
  857. lbs_deb_enter(LBS_DEB_CS);
  858. pcmcia_unregister_driver(&lbs_driver);
  859. lbs_deb_leave(LBS_DEB_CS);
  860. }
  861. module_init(if_cs_init);
  862. module_exit(if_cs_exit);