if_cs.c 25 KB

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