if_cs.c 27 KB

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