if_cs.c 25 KB

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