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. #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. lbs_deb_enter(LBS_DEB_CS);
  288. if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
  289. }
  290. static inline void if_cs_disable_ints(struct if_cs_card *card)
  291. {
  292. lbs_deb_enter(LBS_DEB_CS);
  293. if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
  294. }
  295. /*
  296. * Called from if_cs_host_to_card to send a command to the hardware
  297. */
  298. static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
  299. {
  300. struct if_cs_card *card = (struct if_cs_card *)priv->card;
  301. int ret = -1;
  302. int loops = 0;
  303. lbs_deb_enter(LBS_DEB_CS);
  304. if_cs_disable_ints(card);
  305. /* Is hardware ready? */
  306. while (1) {
  307. u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
  308. if (status & IF_CS_BIT_COMMAND)
  309. break;
  310. if (++loops > 100) {
  311. netdev_err(priv->dev, "card not ready for commands\n");
  312. goto done;
  313. }
  314. mdelay(1);
  315. }
  316. if_cs_write16(card, IF_CS_CMD_LEN, nb);
  317. if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
  318. /* Are we supposed to transfer an odd amount of bytes? */
  319. if (nb & 1)
  320. if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
  321. /* "Assert the download over interrupt command in the Host
  322. * status register" */
  323. if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  324. /* "Assert the download over interrupt command in the Card
  325. * interrupt case register" */
  326. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  327. ret = 0;
  328. done:
  329. if_cs_enable_ints(card);
  330. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  331. return ret;
  332. }
  333. /*
  334. * Called from if_cs_host_to_card to send a data to the hardware
  335. */
  336. static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
  337. {
  338. struct if_cs_card *card = (struct if_cs_card *)priv->card;
  339. u16 status;
  340. lbs_deb_enter(LBS_DEB_CS);
  341. if_cs_disable_ints(card);
  342. status = if_cs_read16(card, IF_CS_CARD_STATUS);
  343. BUG_ON((status & IF_CS_BIT_TX) == 0);
  344. if_cs_write16(card, IF_CS_WRITE_LEN, nb);
  345. /* write even number of bytes, then odd byte if necessary */
  346. if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
  347. if (nb & 1)
  348. if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
  349. if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
  350. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
  351. if_cs_enable_ints(card);
  352. lbs_deb_leave(LBS_DEB_CS);
  353. }
  354. /*
  355. * Get the command result out of the card.
  356. */
  357. static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
  358. {
  359. unsigned long flags;
  360. int ret = -1;
  361. u16 status;
  362. lbs_deb_enter(LBS_DEB_CS);
  363. /* is hardware ready? */
  364. status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
  365. if ((status & IF_CS_BIT_RESP) == 0) {
  366. netdev_err(priv->dev, "no cmd response in card\n");
  367. *len = 0;
  368. goto out;
  369. }
  370. *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
  371. if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
  372. netdev_err(priv->dev,
  373. "card cmd buffer has invalid # of bytes (%d)\n",
  374. *len);
  375. goto out;
  376. }
  377. /* read even number of bytes, then odd byte if necessary */
  378. if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
  379. if (*len & 1)
  380. data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
  381. /* This is a workaround for a firmware that reports too much
  382. * bytes */
  383. *len -= 8;
  384. ret = 0;
  385. /* Clear this flag again */
  386. spin_lock_irqsave(&priv->driver_lock, flags);
  387. priv->dnld_sent = DNLD_RES_RECEIVED;
  388. spin_unlock_irqrestore(&priv->driver_lock, flags);
  389. out:
  390. lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
  391. return ret;
  392. }
  393. static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
  394. {
  395. struct sk_buff *skb = NULL;
  396. u16 len;
  397. u8 *data;
  398. lbs_deb_enter(LBS_DEB_CS);
  399. len = if_cs_read16(priv->card, IF_CS_READ_LEN);
  400. if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
  401. netdev_err(priv->dev,
  402. "card data buffer has invalid # of bytes (%d)\n",
  403. len);
  404. priv->dev->stats.rx_dropped++;
  405. goto dat_err;
  406. }
  407. skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
  408. if (!skb)
  409. goto out;
  410. skb_put(skb, len);
  411. skb_reserve(skb, 2);/* 16 byte align */
  412. data = skb->data;
  413. /* read even number of bytes, then odd byte if necessary */
  414. if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
  415. if (len & 1)
  416. data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
  417. dat_err:
  418. if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
  419. if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
  420. out:
  421. lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
  422. return skb;
  423. }
  424. static irqreturn_t if_cs_interrupt(int irq, void *data)
  425. {
  426. struct if_cs_card *card = data;
  427. struct lbs_private *priv = card->priv;
  428. u16 cause;
  429. lbs_deb_enter(LBS_DEB_CS);
  430. /* Ask card interrupt cause register if there is something for us */
  431. cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
  432. lbs_deb_cs("cause 0x%04x\n", cause);
  433. if (cause == 0) {
  434. /* Not for us */
  435. return IRQ_NONE;
  436. }
  437. if (cause == 0xffff) {
  438. /* Read in junk, the card has probably been removed */
  439. card->priv->surpriseremoved = 1;
  440. return IRQ_HANDLED;
  441. }
  442. if (cause & IF_CS_BIT_RX) {
  443. struct sk_buff *skb;
  444. lbs_deb_cs("rx packet\n");
  445. skb = if_cs_receive_data(priv);
  446. if (skb)
  447. lbs_process_rxed_packet(priv, skb);
  448. }
  449. if (cause & IF_CS_BIT_TX) {
  450. lbs_deb_cs("tx done\n");
  451. lbs_host_to_card_done(priv);
  452. }
  453. if (cause & IF_CS_BIT_RESP) {
  454. unsigned long flags;
  455. u8 i;
  456. lbs_deb_cs("cmd resp\n");
  457. spin_lock_irqsave(&priv->driver_lock, flags);
  458. i = (priv->resp_idx == 0) ? 1 : 0;
  459. spin_unlock_irqrestore(&priv->driver_lock, flags);
  460. BUG_ON(priv->resp_len[i]);
  461. if_cs_receive_cmdres(priv, priv->resp_buf[i],
  462. &priv->resp_len[i]);
  463. spin_lock_irqsave(&priv->driver_lock, flags);
  464. lbs_notify_command_response(priv, i);
  465. spin_unlock_irqrestore(&priv->driver_lock, flags);
  466. }
  467. if (cause & IF_CS_BIT_EVENT) {
  468. u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
  469. if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
  470. IF_CS_BIT_EVENT);
  471. lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
  472. }
  473. /* Clear interrupt cause */
  474. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
  475. lbs_deb_leave(LBS_DEB_CS);
  476. return IRQ_HANDLED;
  477. }
  478. /********************************************************************/
  479. /* Firmware */
  480. /********************************************************************/
  481. /*
  482. * Tries to program the helper firmware.
  483. *
  484. * Return 0 on success
  485. */
  486. static int if_cs_prog_helper(struct if_cs_card *card, const struct firmware *fw)
  487. {
  488. int ret = 0;
  489. int sent = 0;
  490. u8 scratch;
  491. lbs_deb_enter(LBS_DEB_CS);
  492. /*
  493. * This is the only place where an unaligned register access happens on
  494. * the CF8305 card, therefore for the sake of speed of the driver, we do
  495. * the alignment correction here.
  496. */
  497. if (card->align_regs)
  498. scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
  499. else
  500. scratch = if_cs_read8(card, IF_CS_SCRATCH);
  501. /* "If the value is 0x5a, the firmware is already
  502. * downloaded successfully"
  503. */
  504. if (scratch == IF_CS_SCRATCH_HELPER_OK)
  505. goto done;
  506. /* "If the value is != 00, it is invalid value of register */
  507. if (scratch != IF_CS_SCRATCH_BOOT_OK) {
  508. ret = -ENODEV;
  509. goto done;
  510. }
  511. lbs_deb_cs("helper size %td\n", fw->size);
  512. /* "Set the 5 bytes of the helper image to 0" */
  513. /* Not needed, this contains an ARM branch instruction */
  514. for (;;) {
  515. /* "the number of bytes to send is 256" */
  516. int count = 256;
  517. int remain = fw->size - sent;
  518. if (remain < count)
  519. count = remain;
  520. /*
  521. * "write the number of bytes to be sent to the I/O Command
  522. * write length register"
  523. */
  524. if_cs_write16(card, IF_CS_CMD_LEN, count);
  525. /* "write this to I/O Command port register as 16 bit writes */
  526. if (count)
  527. if_cs_write16_rep(card, IF_CS_CMD,
  528. &fw->data[sent],
  529. count >> 1);
  530. /*
  531. * "Assert the download over interrupt command in the Host
  532. * status register"
  533. */
  534. if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  535. /*
  536. * "Assert the download over interrupt command in the Card
  537. * interrupt case register"
  538. */
  539. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  540. /*
  541. * "The host polls the Card Status register ... for 50 ms before
  542. * declaring a failure"
  543. */
  544. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
  545. IF_CS_BIT_COMMAND);
  546. if (ret < 0) {
  547. pr_err("can't download helper at 0x%x, ret %d\n",
  548. sent, ret);
  549. goto done;
  550. }
  551. if (count == 0)
  552. break;
  553. sent += count;
  554. }
  555. done:
  556. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  557. return ret;
  558. }
  559. static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
  560. {
  561. int ret = 0;
  562. int retry = 0;
  563. int len = 0;
  564. int sent;
  565. lbs_deb_enter(LBS_DEB_CS);
  566. lbs_deb_cs("fw size %td\n", fw->size);
  567. ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
  568. IF_CS_SQ_HELPER_OK);
  569. if (ret < 0) {
  570. pr_err("helper firmware doesn't answer\n");
  571. goto done;
  572. }
  573. for (sent = 0; sent < fw->size; sent += len) {
  574. len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
  575. if (len & 1) {
  576. retry++;
  577. pr_info("odd, need to retry this firmware block\n");
  578. } else {
  579. retry = 0;
  580. }
  581. if (retry > 20) {
  582. pr_err("could not download firmware\n");
  583. ret = -ENODEV;
  584. goto done;
  585. }
  586. if (retry) {
  587. sent -= len;
  588. }
  589. if_cs_write16(card, IF_CS_CMD_LEN, len);
  590. if_cs_write16_rep(card, IF_CS_CMD,
  591. &fw->data[sent],
  592. (len+1) >> 1);
  593. if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  594. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  595. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
  596. IF_CS_BIT_COMMAND);
  597. if (ret < 0) {
  598. pr_err("can't download firmware at 0x%x\n", sent);
  599. goto done;
  600. }
  601. }
  602. ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
  603. if (ret < 0)
  604. pr_err("firmware download failed\n");
  605. done:
  606. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  607. return ret;
  608. }
  609. static void if_cs_prog_firmware(struct lbs_private *priv, int ret,
  610. const struct firmware *helper,
  611. const struct firmware *mainfw)
  612. {
  613. struct if_cs_card *card = priv->card;
  614. if (ret) {
  615. pr_err("failed to find firmware (%d)\n", ret);
  616. return;
  617. }
  618. /* Load the firmware */
  619. ret = if_cs_prog_helper(card, helper);
  620. if (ret == 0 && (card->model != MODEL_8305))
  621. ret = if_cs_prog_real(card, mainfw);
  622. if (ret)
  623. goto out;
  624. /* Now actually get the IRQ */
  625. ret = request_irq(card->p_dev->irq, if_cs_interrupt,
  626. IRQF_SHARED, DRV_NAME, card);
  627. if (ret) {
  628. pr_err("error in request_irq\n");
  629. goto out;
  630. }
  631. /*
  632. * Clear any interrupt cause that happened while sending
  633. * firmware/initializing card
  634. */
  635. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
  636. if_cs_enable_ints(card);
  637. /* And finally bring the card up */
  638. priv->fw_ready = 1;
  639. if (lbs_start_card(priv) != 0) {
  640. pr_err("could not activate card\n");
  641. free_irq(card->p_dev->irq, card);
  642. }
  643. out:
  644. release_firmware(helper);
  645. release_firmware(mainfw);
  646. }
  647. /********************************************************************/
  648. /* Callback functions for libertas.ko */
  649. /********************************************************************/
  650. /* Send commands or data packets to the card */
  651. static int if_cs_host_to_card(struct lbs_private *priv,
  652. u8 type,
  653. u8 *buf,
  654. u16 nb)
  655. {
  656. int ret = -1;
  657. lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
  658. switch (type) {
  659. case MVMS_DAT:
  660. priv->dnld_sent = DNLD_DATA_SENT;
  661. if_cs_send_data(priv, buf, nb);
  662. ret = 0;
  663. break;
  664. case MVMS_CMD:
  665. priv->dnld_sent = DNLD_CMD_SENT;
  666. ret = if_cs_send_cmd(priv, buf, nb);
  667. break;
  668. default:
  669. netdev_err(priv->dev, "%s: unsupported type %d\n",
  670. __func__, type);
  671. }
  672. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  673. return ret;
  674. }
  675. static void if_cs_release(struct pcmcia_device *p_dev)
  676. {
  677. struct if_cs_card *card = p_dev->priv;
  678. lbs_deb_enter(LBS_DEB_CS);
  679. free_irq(p_dev->irq, card);
  680. pcmcia_disable_device(p_dev);
  681. if (card->iobase)
  682. ioport_unmap(card->iobase);
  683. lbs_deb_leave(LBS_DEB_CS);
  684. }
  685. static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
  686. {
  687. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  688. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
  689. if (p_dev->resource[1]->end) {
  690. pr_err("wrong CIS (check number of IO windows)\n");
  691. return -ENODEV;
  692. }
  693. /* This reserves IO space but doesn't actually enable it */
  694. return pcmcia_request_io(p_dev);
  695. }
  696. static int if_cs_probe(struct pcmcia_device *p_dev)
  697. {
  698. int ret = -ENOMEM;
  699. unsigned int prod_id;
  700. struct lbs_private *priv;
  701. struct if_cs_card *card;
  702. lbs_deb_enter(LBS_DEB_CS);
  703. card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
  704. if (!card)
  705. goto out;
  706. card->p_dev = p_dev;
  707. p_dev->priv = card;
  708. p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
  709. if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
  710. pr_err("error in pcmcia_loop_config\n");
  711. goto out1;
  712. }
  713. /*
  714. * Allocate an interrupt line. Note that this does not assign
  715. * a handler to the interrupt, unless the 'Handler' member of
  716. * the irq structure is initialized.
  717. */
  718. if (!p_dev->irq)
  719. goto out1;
  720. /* Initialize io access */
  721. card->iobase = ioport_map(p_dev->resource[0]->start,
  722. resource_size(p_dev->resource[0]));
  723. if (!card->iobase) {
  724. pr_err("error in ioport_map\n");
  725. ret = -EIO;
  726. goto out1;
  727. }
  728. ret = pcmcia_enable_device(p_dev);
  729. if (ret) {
  730. pr_err("error in pcmcia_enable_device\n");
  731. goto out2;
  732. }
  733. /* Finally, report what we've done */
  734. lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
  735. /*
  736. * Most of the libertas cards can do unaligned register access, but some
  737. * weird ones cannot. That's especially true for the CF8305 card.
  738. */
  739. card->align_regs = false;
  740. card->model = get_model(p_dev->manf_id, p_dev->card_id);
  741. if (card->model == MODEL_UNKNOWN) {
  742. pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
  743. p_dev->manf_id, p_dev->card_id);
  744. goto out2;
  745. }
  746. /* Check if we have a current silicon */
  747. prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
  748. if (card->model == MODEL_8305) {
  749. card->align_regs = true;
  750. if (prod_id < IF_CS_CF8305_B1_REV) {
  751. pr_err("8305 rev B0 and older are not supported\n");
  752. ret = -ENODEV;
  753. goto out2;
  754. }
  755. }
  756. if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
  757. pr_err("8381 rev B2 and older are not supported\n");
  758. ret = -ENODEV;
  759. goto out2;
  760. }
  761. if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
  762. pr_err("8385 rev B0 and older are not supported\n");
  763. ret = -ENODEV;
  764. goto out2;
  765. }
  766. /* Make this card known to the libertas driver */
  767. priv = lbs_add_card(card, &p_dev->dev);
  768. if (!priv) {
  769. ret = -ENOMEM;
  770. goto out2;
  771. }
  772. /* Set up fields in lbs_private */
  773. card->priv = priv;
  774. priv->card = card;
  775. priv->hw_host_to_card = if_cs_host_to_card;
  776. priv->enter_deep_sleep = NULL;
  777. priv->exit_deep_sleep = NULL;
  778. priv->reset_deep_sleep_wakeup = NULL;
  779. /* Get firmware */
  780. ret = lbs_get_firmware_async(priv, &p_dev->dev, card->model, fw_table,
  781. if_cs_prog_firmware);
  782. if (ret) {
  783. pr_err("failed to find firmware (%d)\n", ret);
  784. goto out3;
  785. }
  786. goto out;
  787. out3:
  788. lbs_remove_card(priv);
  789. out2:
  790. ioport_unmap(card->iobase);
  791. out1:
  792. pcmcia_disable_device(p_dev);
  793. out:
  794. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  795. return ret;
  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 const 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. /* NOTE: keep in sync with get_model() */
  816. PCMCIA_DEVICE_NULL,
  817. };
  818. MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
  819. static struct pcmcia_driver lbs_driver = {
  820. .owner = THIS_MODULE,
  821. .name = DRV_NAME,
  822. .probe = if_cs_probe,
  823. .remove = if_cs_detach,
  824. .id_table = if_cs_ids,
  825. };
  826. static int __init if_cs_init(void)
  827. {
  828. int ret;
  829. lbs_deb_enter(LBS_DEB_CS);
  830. ret = pcmcia_register_driver(&lbs_driver);
  831. lbs_deb_leave(LBS_DEB_CS);
  832. return ret;
  833. }
  834. static void __exit if_cs_exit(void)
  835. {
  836. lbs_deb_enter(LBS_DEB_CS);
  837. pcmcia_unregister_driver(&lbs_driver);
  838. lbs_deb_leave(LBS_DEB_CS);
  839. }
  840. module_init(if_cs_init);
  841. module_exit(if_cs_exit);