if_cs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. #define IF_CS_CF8381_B3_REV 0x04
  232. /*
  233. * Used to detect other cards than CF8385 since their revisions of silicon
  234. * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
  235. */
  236. #define CF8381_MANFID 0x02db
  237. #define CF8381_CARDID 0x6064
  238. #define CF8385_MANFID 0x02df
  239. #define CF8385_CARDID 0x8103
  240. static inline int if_cs_hw_is_cf8381(struct pcmcia_device *p_dev)
  241. {
  242. return (p_dev->manf_id == CF8381_MANFID &&
  243. p_dev->card_id == CF8381_CARDID);
  244. }
  245. static inline int if_cs_hw_is_cf8385(struct pcmcia_device *p_dev)
  246. {
  247. return (p_dev->manf_id == CF8385_MANFID &&
  248. p_dev->card_id == CF8385_CARDID);
  249. }
  250. /********************************************************************/
  251. /* I/O and interrupt handling */
  252. /********************************************************************/
  253. static inline void if_cs_enable_ints(struct if_cs_card *card)
  254. {
  255. lbs_deb_enter(LBS_DEB_CS);
  256. if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
  257. }
  258. static inline void if_cs_disable_ints(struct if_cs_card *card)
  259. {
  260. lbs_deb_enter(LBS_DEB_CS);
  261. if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
  262. }
  263. /*
  264. * Called from if_cs_host_to_card to send a command to the hardware
  265. */
  266. static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
  267. {
  268. struct if_cs_card *card = (struct if_cs_card *)priv->card;
  269. int ret = -1;
  270. int loops = 0;
  271. lbs_deb_enter(LBS_DEB_CS);
  272. if_cs_disable_ints(card);
  273. /* Is hardware ready? */
  274. while (1) {
  275. u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
  276. if (status & IF_CS_BIT_COMMAND)
  277. break;
  278. if (++loops > 100) {
  279. lbs_pr_err("card not ready for commands\n");
  280. goto done;
  281. }
  282. mdelay(1);
  283. }
  284. if_cs_write16(card, IF_CS_CMD_LEN, nb);
  285. if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
  286. /* Are we supposed to transfer an odd amount of bytes? */
  287. if (nb & 1)
  288. if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
  289. /* "Assert the download over interrupt command in the Host
  290. * status register" */
  291. if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  292. /* "Assert the download over interrupt command in the Card
  293. * interrupt case register" */
  294. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  295. ret = 0;
  296. done:
  297. if_cs_enable_ints(card);
  298. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  299. return ret;
  300. }
  301. /*
  302. * Called from if_cs_host_to_card to send a data to the hardware
  303. */
  304. static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
  305. {
  306. struct if_cs_card *card = (struct if_cs_card *)priv->card;
  307. u16 status;
  308. lbs_deb_enter(LBS_DEB_CS);
  309. if_cs_disable_ints(card);
  310. status = if_cs_read16(card, IF_CS_CARD_STATUS);
  311. BUG_ON((status & IF_CS_BIT_TX) == 0);
  312. if_cs_write16(card, IF_CS_WRITE_LEN, nb);
  313. /* write even number of bytes, then odd byte if necessary */
  314. if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
  315. if (nb & 1)
  316. if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
  317. if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
  318. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
  319. if_cs_enable_ints(card);
  320. lbs_deb_leave(LBS_DEB_CS);
  321. }
  322. /*
  323. * Get the command result out of the card.
  324. */
  325. static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
  326. {
  327. unsigned long flags;
  328. int ret = -1;
  329. u16 status;
  330. lbs_deb_enter(LBS_DEB_CS);
  331. /* is hardware ready? */
  332. status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
  333. if ((status & IF_CS_BIT_RESP) == 0) {
  334. lbs_pr_err("no cmd response in card\n");
  335. *len = 0;
  336. goto out;
  337. }
  338. *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
  339. if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
  340. lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
  341. goto out;
  342. }
  343. /* read even number of bytes, then odd byte if necessary */
  344. if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
  345. if (*len & 1)
  346. data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
  347. /* This is a workaround for a firmware that reports too much
  348. * bytes */
  349. *len -= 8;
  350. ret = 0;
  351. /* Clear this flag again */
  352. spin_lock_irqsave(&priv->driver_lock, flags);
  353. priv->dnld_sent = DNLD_RES_RECEIVED;
  354. spin_unlock_irqrestore(&priv->driver_lock, flags);
  355. out:
  356. lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
  357. return ret;
  358. }
  359. static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
  360. {
  361. struct sk_buff *skb = NULL;
  362. u16 len;
  363. u8 *data;
  364. lbs_deb_enter(LBS_DEB_CS);
  365. len = if_cs_read16(priv->card, IF_CS_READ_LEN);
  366. if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
  367. lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
  368. priv->dev->stats.rx_dropped++;
  369. goto dat_err;
  370. }
  371. skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
  372. if (!skb)
  373. goto out;
  374. skb_put(skb, len);
  375. skb_reserve(skb, 2);/* 16 byte align */
  376. data = skb->data;
  377. /* read even number of bytes, then odd byte if necessary */
  378. if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
  379. if (len & 1)
  380. data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
  381. dat_err:
  382. if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
  383. if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
  384. out:
  385. lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
  386. return skb;
  387. }
  388. static irqreturn_t if_cs_interrupt(int irq, void *data)
  389. {
  390. struct if_cs_card *card = data;
  391. struct lbs_private *priv = card->priv;
  392. u16 cause;
  393. lbs_deb_enter(LBS_DEB_CS);
  394. /* Ask card interrupt cause register if there is something for us */
  395. cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
  396. lbs_deb_cs("cause 0x%04x\n", cause);
  397. if (cause == 0) {
  398. /* Not for us */
  399. return IRQ_NONE;
  400. }
  401. if (cause == 0xffff) {
  402. /* Read in junk, the card has probably been removed */
  403. card->priv->surpriseremoved = 1;
  404. return IRQ_HANDLED;
  405. }
  406. if (cause & IF_CS_BIT_RX) {
  407. struct sk_buff *skb;
  408. lbs_deb_cs("rx packet\n");
  409. skb = if_cs_receive_data(priv);
  410. if (skb)
  411. lbs_process_rxed_packet(priv, skb);
  412. }
  413. if (cause & IF_CS_BIT_TX) {
  414. lbs_deb_cs("tx done\n");
  415. lbs_host_to_card_done(priv);
  416. }
  417. if (cause & IF_CS_BIT_RESP) {
  418. unsigned long flags;
  419. u8 i;
  420. lbs_deb_cs("cmd resp\n");
  421. spin_lock_irqsave(&priv->driver_lock, flags);
  422. i = (priv->resp_idx == 0) ? 1 : 0;
  423. spin_unlock_irqrestore(&priv->driver_lock, flags);
  424. BUG_ON(priv->resp_len[i]);
  425. if_cs_receive_cmdres(priv, priv->resp_buf[i],
  426. &priv->resp_len[i]);
  427. spin_lock_irqsave(&priv->driver_lock, flags);
  428. lbs_notify_command_response(priv, i);
  429. spin_unlock_irqrestore(&priv->driver_lock, flags);
  430. }
  431. if (cause & IF_CS_BIT_EVENT) {
  432. u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
  433. if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
  434. IF_CS_BIT_EVENT);
  435. lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
  436. }
  437. /* Clear interrupt cause */
  438. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
  439. lbs_deb_leave(LBS_DEB_CS);
  440. return IRQ_HANDLED;
  441. }
  442. /********************************************************************/
  443. /* Firmware */
  444. /********************************************************************/
  445. /*
  446. * Tries to program the helper firmware.
  447. *
  448. * Return 0 on success
  449. */
  450. static int if_cs_prog_helper(struct if_cs_card *card)
  451. {
  452. int ret = 0;
  453. int sent = 0;
  454. u8 scratch;
  455. const struct firmware *fw;
  456. lbs_deb_enter(LBS_DEB_CS);
  457. scratch = if_cs_read8(card, IF_CS_SCRATCH);
  458. /* "If the value is 0x5a, the firmware is already
  459. * downloaded successfully"
  460. */
  461. if (scratch == IF_CS_SCRATCH_HELPER_OK)
  462. goto done;
  463. /* "If the value is != 00, it is invalid value of register */
  464. if (scratch != IF_CS_SCRATCH_BOOT_OK) {
  465. ret = -ENODEV;
  466. goto done;
  467. }
  468. /* TODO: make firmware file configurable */
  469. ret = request_firmware(&fw, "libertas_cs_helper.fw",
  470. &handle_to_dev(card->p_dev));
  471. if (ret) {
  472. lbs_pr_err("can't load helper firmware\n");
  473. ret = -ENODEV;
  474. goto done;
  475. }
  476. lbs_deb_cs("helper size %td\n", fw->size);
  477. /* "Set the 5 bytes of the helper image to 0" */
  478. /* Not needed, this contains an ARM branch instruction */
  479. for (;;) {
  480. /* "the number of bytes to send is 256" */
  481. int count = 256;
  482. int remain = fw->size - sent;
  483. if (remain < count)
  484. count = remain;
  485. /* "write the number of bytes to be sent to the I/O Command
  486. * write length register" */
  487. if_cs_write16(card, IF_CS_CMD_LEN, count);
  488. /* "write this to I/O Command port register as 16 bit writes */
  489. if (count)
  490. if_cs_write16_rep(card, IF_CS_CMD,
  491. &fw->data[sent],
  492. count >> 1);
  493. /* "Assert the download over interrupt command in the Host
  494. * status register" */
  495. if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  496. /* "Assert the download over interrupt command in the Card
  497. * interrupt case register" */
  498. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  499. /* "The host polls the Card Status register ... for 50 ms before
  500. declaring a failure */
  501. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
  502. IF_CS_BIT_COMMAND);
  503. if (ret < 0) {
  504. lbs_pr_err("can't download helper at 0x%x, ret %d\n",
  505. sent, ret);
  506. goto err_release;
  507. }
  508. if (count == 0)
  509. break;
  510. sent += count;
  511. }
  512. err_release:
  513. release_firmware(fw);
  514. done:
  515. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  516. return ret;
  517. }
  518. static int if_cs_prog_real(struct if_cs_card *card)
  519. {
  520. const struct firmware *fw;
  521. int ret = 0;
  522. int retry = 0;
  523. int len = 0;
  524. int sent;
  525. lbs_deb_enter(LBS_DEB_CS);
  526. /* TODO: make firmware file configurable */
  527. ret = request_firmware(&fw, "libertas_cs.fw",
  528. &handle_to_dev(card->p_dev));
  529. if (ret) {
  530. lbs_pr_err("can't load firmware\n");
  531. ret = -ENODEV;
  532. goto done;
  533. }
  534. lbs_deb_cs("fw size %td\n", fw->size);
  535. ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
  536. IF_CS_SQ_HELPER_OK);
  537. if (ret < 0) {
  538. lbs_pr_err("helper firmware doesn't answer\n");
  539. goto err_release;
  540. }
  541. for (sent = 0; sent < fw->size; sent += len) {
  542. len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
  543. if (len & 1) {
  544. retry++;
  545. lbs_pr_info("odd, need to retry this firmware block\n");
  546. } else {
  547. retry = 0;
  548. }
  549. if (retry > 20) {
  550. lbs_pr_err("could not download firmware\n");
  551. ret = -ENODEV;
  552. goto err_release;
  553. }
  554. if (retry) {
  555. sent -= len;
  556. }
  557. if_cs_write16(card, IF_CS_CMD_LEN, len);
  558. if_cs_write16_rep(card, IF_CS_CMD,
  559. &fw->data[sent],
  560. (len+1) >> 1);
  561. if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  562. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  563. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
  564. IF_CS_BIT_COMMAND);
  565. if (ret < 0) {
  566. lbs_pr_err("can't download firmware at 0x%x\n", sent);
  567. goto err_release;
  568. }
  569. }
  570. ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
  571. if (ret < 0)
  572. lbs_pr_err("firmware download failed\n");
  573. err_release:
  574. release_firmware(fw);
  575. done:
  576. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  577. return ret;
  578. }
  579. /********************************************************************/
  580. /* Callback functions for libertas.ko */
  581. /********************************************************************/
  582. /* Send commands or data packets to the card */
  583. static int if_cs_host_to_card(struct lbs_private *priv,
  584. u8 type,
  585. u8 *buf,
  586. u16 nb)
  587. {
  588. int ret = -1;
  589. lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
  590. switch (type) {
  591. case MVMS_DAT:
  592. priv->dnld_sent = DNLD_DATA_SENT;
  593. if_cs_send_data(priv, buf, nb);
  594. ret = 0;
  595. break;
  596. case MVMS_CMD:
  597. priv->dnld_sent = DNLD_CMD_SENT;
  598. ret = if_cs_send_cmd(priv, buf, nb);
  599. break;
  600. default:
  601. lbs_pr_err("%s: unsupported type %d\n", __func__, type);
  602. }
  603. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  604. return ret;
  605. }
  606. /********************************************************************/
  607. /* Card Services */
  608. /********************************************************************/
  609. /*
  610. * After a card is removed, if_cs_release() will unregister the
  611. * device, and release the PCMCIA configuration. If the device is
  612. * still open, this will be postponed until it is closed.
  613. */
  614. static void if_cs_release(struct pcmcia_device *p_dev)
  615. {
  616. struct if_cs_card *card = p_dev->priv;
  617. lbs_deb_enter(LBS_DEB_CS);
  618. free_irq(p_dev->irq.AssignedIRQ, card);
  619. pcmcia_disable_device(p_dev);
  620. if (card->iobase)
  621. ioport_unmap(card->iobase);
  622. lbs_deb_leave(LBS_DEB_CS);
  623. }
  624. /*
  625. * This creates an "instance" of the driver, allocating local data
  626. * structures for one device. The device is registered with Card
  627. * Services.
  628. *
  629. * The dev_link structure is initialized, but we don't actually
  630. * configure the card at this point -- we wait until we receive a card
  631. * insertion event.
  632. */
  633. static int if_cs_probe(struct pcmcia_device *p_dev)
  634. {
  635. int ret = -ENOMEM;
  636. unsigned int prod_id;
  637. struct lbs_private *priv;
  638. struct if_cs_card *card;
  639. /* CIS parsing */
  640. tuple_t tuple;
  641. cisparse_t parse;
  642. cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
  643. cistpl_io_t *io = &cfg->io;
  644. u_char buf[64];
  645. lbs_deb_enter(LBS_DEB_CS);
  646. card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
  647. if (!card) {
  648. lbs_pr_err("error in kzalloc\n");
  649. goto out;
  650. }
  651. card->p_dev = p_dev;
  652. p_dev->priv = card;
  653. p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
  654. p_dev->irq.Handler = NULL;
  655. p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
  656. p_dev->conf.Attributes = 0;
  657. p_dev->conf.IntType = INT_MEMORY_AND_IO;
  658. tuple.Attributes = 0;
  659. tuple.TupleData = buf;
  660. tuple.TupleDataMax = sizeof(buf);
  661. tuple.TupleOffset = 0;
  662. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  663. if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
  664. (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
  665. (ret = pcmcia_parse_tuple(&tuple, &parse)) != 0)
  666. {
  667. lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
  668. goto out1;
  669. }
  670. p_dev->conf.ConfigIndex = cfg->index;
  671. /* Do we need to allocate an interrupt? */
  672. if (cfg->irq.IRQInfo1) {
  673. p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
  674. }
  675. /* IO window settings */
  676. if (cfg->io.nwin != 1) {
  677. lbs_pr_err("wrong CIS (check number of IO windows)\n");
  678. ret = -ENODEV;
  679. goto out1;
  680. }
  681. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  682. p_dev->io.BasePort1 = io->win[0].base;
  683. p_dev->io.NumPorts1 = io->win[0].len;
  684. /* This reserves IO space but doesn't actually enable it */
  685. ret = pcmcia_request_io(p_dev, &p_dev->io);
  686. if (ret) {
  687. lbs_pr_err("error in pcmcia_request_io\n");
  688. goto out1;
  689. }
  690. /*
  691. * Allocate an interrupt line. Note that this does not assign
  692. * a handler to the interrupt, unless the 'Handler' member of
  693. * the irq structure is initialized.
  694. */
  695. if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
  696. ret = pcmcia_request_irq(p_dev, &p_dev->irq);
  697. if (ret) {
  698. lbs_pr_err("error in pcmcia_request_irq\n");
  699. goto out1;
  700. }
  701. }
  702. /* Initialize io access */
  703. card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
  704. if (!card->iobase) {
  705. lbs_pr_err("error in ioport_map\n");
  706. ret = -EIO;
  707. goto out1;
  708. }
  709. /*
  710. * This actually configures the PCMCIA socket -- setting up
  711. * the I/O windows and the interrupt mapping, and putting the
  712. * card and host interface into "Memory and IO" mode.
  713. */
  714. ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
  715. if (ret) {
  716. lbs_pr_err("error in pcmcia_request_configuration\n");
  717. goto out2;
  718. }
  719. /* Finally, report what we've done */
  720. lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
  721. p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
  722. p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
  723. /* Check if we have a current silicon */
  724. prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
  725. if (if_cs_hw_is_cf8381(p_dev) && prod_id < IF_CS_CF8381_B3_REV) {
  726. lbs_pr_err("old chips like 8381 rev B3 aren't supported\n");
  727. ret = -ENODEV;
  728. goto out2;
  729. }
  730. if (if_cs_hw_is_cf8385(p_dev) && prod_id < IF_CS_CF8385_B1_REV) {
  731. lbs_pr_err("old chips like 8385 rev B1 aren't supported\n");
  732. ret = -ENODEV;
  733. goto out2;
  734. }
  735. /* Load the firmware early, before calling into libertas.ko */
  736. ret = if_cs_prog_helper(card);
  737. if (ret == 0)
  738. ret = if_cs_prog_real(card);
  739. if (ret)
  740. goto out2;
  741. /* Make this card known to the libertas driver */
  742. priv = lbs_add_card(card, &p_dev->dev);
  743. if (!priv) {
  744. ret = -ENOMEM;
  745. goto out2;
  746. }
  747. /* Finish setting up fields in lbs_private */
  748. card->priv = priv;
  749. priv->card = card;
  750. priv->hw_host_to_card = if_cs_host_to_card;
  751. priv->fw_ready = 1;
  752. /* Now actually get the IRQ */
  753. ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
  754. IRQF_SHARED, DRV_NAME, card);
  755. if (ret) {
  756. lbs_pr_err("error in request_irq\n");
  757. goto out3;
  758. }
  759. /* Clear any interrupt cause that happend while sending
  760. * firmware/initializing card */
  761. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
  762. if_cs_enable_ints(card);
  763. /* And finally bring the card up */
  764. if (lbs_start_card(priv) != 0) {
  765. lbs_pr_err("could not activate card\n");
  766. goto out3;
  767. }
  768. /* The firmware for the CF card supports powersave */
  769. priv->ps_supported = 1;
  770. ret = 0;
  771. goto out;
  772. out3:
  773. lbs_remove_card(priv);
  774. out2:
  775. ioport_unmap(card->iobase);
  776. out1:
  777. pcmcia_disable_device(p_dev);
  778. out:
  779. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  780. return ret;
  781. }
  782. /*
  783. * This deletes a driver "instance". The device is de-registered with
  784. * Card Services. If it has been released, all local data structures
  785. * are freed. Otherwise, the structures will be freed when the device
  786. * is released.
  787. */
  788. static void if_cs_detach(struct pcmcia_device *p_dev)
  789. {
  790. struct if_cs_card *card = p_dev->priv;
  791. lbs_deb_enter(LBS_DEB_CS);
  792. lbs_stop_card(card->priv);
  793. lbs_remove_card(card->priv);
  794. if_cs_disable_ints(card);
  795. if_cs_release(p_dev);
  796. kfree(card);
  797. lbs_deb_leave(LBS_DEB_CS);
  798. }
  799. /********************************************************************/
  800. /* Module initialization */
  801. /********************************************************************/
  802. static struct pcmcia_device_id if_cs_ids[] = {
  803. PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
  804. PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
  805. PCMCIA_DEVICE_NULL,
  806. };
  807. MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
  808. static struct pcmcia_driver lbs_driver = {
  809. .owner = THIS_MODULE,
  810. .drv = {
  811. .name = DRV_NAME,
  812. },
  813. .probe = if_cs_probe,
  814. .remove = if_cs_detach,
  815. .id_table = if_cs_ids,
  816. };
  817. static int __init if_cs_init(void)
  818. {
  819. int ret;
  820. lbs_deb_enter(LBS_DEB_CS);
  821. ret = pcmcia_register_driver(&lbs_driver);
  822. lbs_deb_leave(LBS_DEB_CS);
  823. return ret;
  824. }
  825. static void __exit if_cs_exit(void)
  826. {
  827. lbs_deb_enter(LBS_DEB_CS);
  828. pcmcia_unregister_driver(&lbs_driver);
  829. lbs_deb_leave(LBS_DEB_CS);
  830. }
  831. module_init(if_cs_init);
  832. module_exit(if_cs_exit);