if_cs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/firmware.h>
  22. #include <linux/netdevice.h>
  23. #include <pcmcia/cistpl.h>
  24. #include <pcmcia/ds.h>
  25. #include <linux/io.h>
  26. #define DRV_NAME "libertas_cs"
  27. #include "decl.h"
  28. #include "defs.h"
  29. #include "dev.h"
  30. /********************************************************************/
  31. /* Module stuff */
  32. /********************************************************************/
  33. MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
  34. MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
  35. MODULE_LICENSE("GPL");
  36. MODULE_FIRMWARE("libertas_cs_helper.fw");
  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. &card->p_dev->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. &card->p_dev->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. static void if_cs_release(struct pcmcia_device *p_dev)
  624. {
  625. struct if_cs_card *card = p_dev->priv;
  626. lbs_deb_enter(LBS_DEB_CS);
  627. free_irq(p_dev->irq, card);
  628. pcmcia_disable_device(p_dev);
  629. if (card->iobase)
  630. ioport_unmap(card->iobase);
  631. lbs_deb_leave(LBS_DEB_CS);
  632. }
  633. static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
  634. {
  635. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  636. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
  637. if (p_dev->resource[1]->end) {
  638. lbs_pr_err("wrong CIS (check number of IO windows)\n");
  639. return -ENODEV;
  640. }
  641. /* This reserves IO space but doesn't actually enable it */
  642. return pcmcia_request_io(p_dev);
  643. }
  644. static int if_cs_probe(struct pcmcia_device *p_dev)
  645. {
  646. int ret = -ENOMEM;
  647. unsigned int prod_id;
  648. struct lbs_private *priv;
  649. struct if_cs_card *card;
  650. lbs_deb_enter(LBS_DEB_CS);
  651. card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
  652. if (!card) {
  653. lbs_pr_err("error in kzalloc\n");
  654. goto out;
  655. }
  656. card->p_dev = p_dev;
  657. p_dev->priv = card;
  658. p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
  659. if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
  660. lbs_pr_err("error in pcmcia_loop_config\n");
  661. goto out1;
  662. }
  663. /*
  664. * Allocate an interrupt line. Note that this does not assign
  665. * a handler to the interrupt, unless the 'Handler' member of
  666. * the irq structure is initialized.
  667. */
  668. if (!p_dev->irq)
  669. goto out1;
  670. /* Initialize io access */
  671. card->iobase = ioport_map(p_dev->resource[0]->start,
  672. resource_size(p_dev->resource[0]));
  673. if (!card->iobase) {
  674. lbs_pr_err("error in ioport_map\n");
  675. ret = -EIO;
  676. goto out1;
  677. }
  678. ret = pcmcia_enable_device(p_dev);
  679. if (ret) {
  680. lbs_pr_err("error in pcmcia_enable_device\n");
  681. goto out2;
  682. }
  683. /* Finally, report what we've done */
  684. lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
  685. /*
  686. * Most of the libertas cards can do unaligned register access, but some
  687. * weird ones can not. That's especially true for the CF8305 card.
  688. */
  689. card->align_regs = 0;
  690. /* Check if we have a current silicon */
  691. prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
  692. if (if_cs_hw_is_cf8305(p_dev)) {
  693. card->align_regs = 1;
  694. if (prod_id < IF_CS_CF8305_B1_REV) {
  695. lbs_pr_err("old chips like 8305 rev B3 "
  696. "aren't supported\n");
  697. ret = -ENODEV;
  698. goto out2;
  699. }
  700. }
  701. if (if_cs_hw_is_cf8381(p_dev) && prod_id < IF_CS_CF8381_B3_REV) {
  702. lbs_pr_err("old chips like 8381 rev B3 aren't supported\n");
  703. ret = -ENODEV;
  704. goto out2;
  705. }
  706. if (if_cs_hw_is_cf8385(p_dev) && prod_id < IF_CS_CF8385_B1_REV) {
  707. lbs_pr_err("old chips like 8385 rev B1 aren't supported\n");
  708. ret = -ENODEV;
  709. goto out2;
  710. }
  711. /* Load the firmware early, before calling into libertas.ko */
  712. ret = if_cs_prog_helper(card);
  713. if (ret == 0 && !if_cs_hw_is_cf8305(p_dev))
  714. ret = if_cs_prog_real(card);
  715. if (ret)
  716. goto out2;
  717. /* Make this card known to the libertas driver */
  718. priv = lbs_add_card(card, &p_dev->dev);
  719. if (!priv) {
  720. ret = -ENOMEM;
  721. goto out2;
  722. }
  723. /* Finish setting up fields in lbs_private */
  724. card->priv = priv;
  725. priv->card = card;
  726. priv->hw_host_to_card = if_cs_host_to_card;
  727. priv->enter_deep_sleep = NULL;
  728. priv->exit_deep_sleep = NULL;
  729. priv->reset_deep_sleep_wakeup = NULL;
  730. priv->fw_ready = 1;
  731. /* Now actually get the IRQ */
  732. ret = request_irq(p_dev->irq, if_cs_interrupt,
  733. IRQF_SHARED, DRV_NAME, card);
  734. if (ret) {
  735. lbs_pr_err("error in request_irq\n");
  736. goto out3;
  737. }
  738. /* Clear any interrupt cause that happend while sending
  739. * firmware/initializing card */
  740. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
  741. if_cs_enable_ints(card);
  742. /* And finally bring the card up */
  743. if (lbs_start_card(priv) != 0) {
  744. lbs_pr_err("could not activate card\n");
  745. goto out3;
  746. }
  747. ret = 0;
  748. goto out;
  749. out3:
  750. lbs_remove_card(priv);
  751. out2:
  752. ioport_unmap(card->iobase);
  753. out1:
  754. pcmcia_disable_device(p_dev);
  755. out:
  756. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  757. return ret;
  758. }
  759. static void if_cs_detach(struct pcmcia_device *p_dev)
  760. {
  761. struct if_cs_card *card = p_dev->priv;
  762. lbs_deb_enter(LBS_DEB_CS);
  763. lbs_stop_card(card->priv);
  764. lbs_remove_card(card->priv);
  765. if_cs_disable_ints(card);
  766. if_cs_release(p_dev);
  767. kfree(card);
  768. lbs_deb_leave(LBS_DEB_CS);
  769. }
  770. /********************************************************************/
  771. /* Module initialization */
  772. /********************************************************************/
  773. static struct pcmcia_device_id if_cs_ids[] = {
  774. PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
  775. PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
  776. PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
  777. PCMCIA_DEVICE_NULL,
  778. };
  779. MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
  780. static struct pcmcia_driver lbs_driver = {
  781. .owner = THIS_MODULE,
  782. .name = DRV_NAME,
  783. .probe = if_cs_probe,
  784. .remove = if_cs_detach,
  785. .id_table = if_cs_ids,
  786. };
  787. static int __init if_cs_init(void)
  788. {
  789. int ret;
  790. lbs_deb_enter(LBS_DEB_CS);
  791. ret = pcmcia_register_driver(&lbs_driver);
  792. lbs_deb_leave(LBS_DEB_CS);
  793. return ret;
  794. }
  795. static void __exit if_cs_exit(void)
  796. {
  797. lbs_deb_enter(LBS_DEB_CS);
  798. pcmcia_unregister_driver(&lbs_driver);
  799. lbs_deb_leave(LBS_DEB_CS);
  800. }
  801. module_init(if_cs_init);
  802. module_exit(if_cs_exit);