if_cs.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. return;
  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. return;
  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. }
  644. /********************************************************************/
  645. /* Callback functions for libertas.ko */
  646. /********************************************************************/
  647. /* Send commands or data packets to the card */
  648. static int if_cs_host_to_card(struct lbs_private *priv,
  649. u8 type,
  650. u8 *buf,
  651. u16 nb)
  652. {
  653. int ret = -1;
  654. lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
  655. switch (type) {
  656. case MVMS_DAT:
  657. priv->dnld_sent = DNLD_DATA_SENT;
  658. if_cs_send_data(priv, buf, nb);
  659. ret = 0;
  660. break;
  661. case MVMS_CMD:
  662. priv->dnld_sent = DNLD_CMD_SENT;
  663. ret = if_cs_send_cmd(priv, buf, nb);
  664. break;
  665. default:
  666. netdev_err(priv->dev, "%s: unsupported type %d\n",
  667. __func__, type);
  668. }
  669. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  670. return ret;
  671. }
  672. static void if_cs_release(struct pcmcia_device *p_dev)
  673. {
  674. struct if_cs_card *card = p_dev->priv;
  675. lbs_deb_enter(LBS_DEB_CS);
  676. free_irq(p_dev->irq, card);
  677. pcmcia_disable_device(p_dev);
  678. if (card->iobase)
  679. ioport_unmap(card->iobase);
  680. lbs_deb_leave(LBS_DEB_CS);
  681. }
  682. static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
  683. {
  684. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  685. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
  686. if (p_dev->resource[1]->end) {
  687. pr_err("wrong CIS (check number of IO windows)\n");
  688. return -ENODEV;
  689. }
  690. /* This reserves IO space but doesn't actually enable it */
  691. return pcmcia_request_io(p_dev);
  692. }
  693. static int if_cs_probe(struct pcmcia_device *p_dev)
  694. {
  695. int ret = -ENOMEM;
  696. unsigned int prod_id;
  697. struct lbs_private *priv;
  698. struct if_cs_card *card;
  699. lbs_deb_enter(LBS_DEB_CS);
  700. card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
  701. if (!card)
  702. goto out;
  703. card->p_dev = p_dev;
  704. p_dev->priv = card;
  705. p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
  706. if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
  707. pr_err("error in pcmcia_loop_config\n");
  708. goto out1;
  709. }
  710. /*
  711. * Allocate an interrupt line. Note that this does not assign
  712. * a handler to the interrupt, unless the 'Handler' member of
  713. * the irq structure is initialized.
  714. */
  715. if (!p_dev->irq)
  716. goto out1;
  717. /* Initialize io access */
  718. card->iobase = ioport_map(p_dev->resource[0]->start,
  719. resource_size(p_dev->resource[0]));
  720. if (!card->iobase) {
  721. pr_err("error in ioport_map\n");
  722. ret = -EIO;
  723. goto out1;
  724. }
  725. ret = pcmcia_enable_device(p_dev);
  726. if (ret) {
  727. pr_err("error in pcmcia_enable_device\n");
  728. goto out2;
  729. }
  730. /* Finally, report what we've done */
  731. lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
  732. /*
  733. * Most of the libertas cards can do unaligned register access, but some
  734. * weird ones cannot. That's especially true for the CF8305 card.
  735. */
  736. card->align_regs = false;
  737. card->model = get_model(p_dev->manf_id, p_dev->card_id);
  738. if (card->model == MODEL_UNKNOWN) {
  739. pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
  740. p_dev->manf_id, p_dev->card_id);
  741. ret = -ENODEV;
  742. goto out2;
  743. }
  744. /* Check if we have a current silicon */
  745. prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
  746. if (card->model == MODEL_8305) {
  747. card->align_regs = true;
  748. if (prod_id < IF_CS_CF8305_B1_REV) {
  749. pr_err("8305 rev B0 and older are not supported\n");
  750. ret = -ENODEV;
  751. goto out2;
  752. }
  753. }
  754. if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
  755. pr_err("8381 rev B2 and older are not supported\n");
  756. ret = -ENODEV;
  757. goto out2;
  758. }
  759. if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
  760. pr_err("8385 rev B0 and older are not supported\n");
  761. ret = -ENODEV;
  762. goto out2;
  763. }
  764. /* Make this card known to the libertas driver */
  765. priv = lbs_add_card(card, &p_dev->dev);
  766. if (!priv) {
  767. ret = -ENOMEM;
  768. goto out2;
  769. }
  770. /* Set up fields in lbs_private */
  771. card->priv = priv;
  772. priv->card = card;
  773. priv->hw_host_to_card = if_cs_host_to_card;
  774. priv->enter_deep_sleep = NULL;
  775. priv->exit_deep_sleep = NULL;
  776. priv->reset_deep_sleep_wakeup = NULL;
  777. /* Get firmware */
  778. ret = lbs_get_firmware_async(priv, &p_dev->dev, card->model, fw_table,
  779. if_cs_prog_firmware);
  780. if (ret) {
  781. pr_err("failed to find firmware (%d)\n", ret);
  782. goto out3;
  783. }
  784. goto out;
  785. out3:
  786. lbs_remove_card(priv);
  787. out2:
  788. ioport_unmap(card->iobase);
  789. out1:
  790. pcmcia_disable_device(p_dev);
  791. out:
  792. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  793. return ret;
  794. }
  795. static void if_cs_detach(struct pcmcia_device *p_dev)
  796. {
  797. struct if_cs_card *card = p_dev->priv;
  798. lbs_deb_enter(LBS_DEB_CS);
  799. lbs_stop_card(card->priv);
  800. lbs_remove_card(card->priv);
  801. if_cs_disable_ints(card);
  802. if_cs_release(p_dev);
  803. kfree(card);
  804. lbs_deb_leave(LBS_DEB_CS);
  805. }
  806. /********************************************************************/
  807. /* Module initialization */
  808. /********************************************************************/
  809. static const struct pcmcia_device_id if_cs_ids[] = {
  810. PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
  811. PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
  812. PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
  813. /* NOTE: keep in sync with get_model() */
  814. PCMCIA_DEVICE_NULL,
  815. };
  816. MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
  817. static struct pcmcia_driver lbs_driver = {
  818. .owner = THIS_MODULE,
  819. .name = DRV_NAME,
  820. .probe = if_cs_probe,
  821. .remove = if_cs_detach,
  822. .id_table = if_cs_ids,
  823. };
  824. module_pcmcia_driver(lbs_driver);