if_cs.c 26 KB

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