if_cs.c 25 KB

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