if_cs.c 26 KB

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