if_cs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. * Used to detect ancient chips:
  228. */
  229. #define IF_CS_PRODUCT_ID 0x0000001C
  230. #define IF_CS_CF8385_B1_REV 0x12
  231. /********************************************************************/
  232. /* I/O and interrupt handling */
  233. /********************************************************************/
  234. static inline void if_cs_enable_ints(struct if_cs_card *card)
  235. {
  236. lbs_deb_enter(LBS_DEB_CS);
  237. if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
  238. }
  239. static inline void if_cs_disable_ints(struct if_cs_card *card)
  240. {
  241. lbs_deb_enter(LBS_DEB_CS);
  242. if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
  243. }
  244. /*
  245. * Called from if_cs_host_to_card to send a command to the hardware
  246. */
  247. static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
  248. {
  249. struct if_cs_card *card = (struct if_cs_card *)priv->card;
  250. int ret = -1;
  251. int loops = 0;
  252. lbs_deb_enter(LBS_DEB_CS);
  253. if_cs_disable_ints(card);
  254. /* Is hardware ready? */
  255. while (1) {
  256. u16 val = if_cs_read16(card, IF_CS_CARD_STATUS);
  257. if (val & IF_CS_BIT_COMMAND)
  258. break;
  259. if (++loops > 100) {
  260. lbs_pr_err("card not ready for commands\n");
  261. goto done;
  262. }
  263. mdelay(1);
  264. }
  265. if_cs_write16(card, IF_CS_HOST_CMD_LEN, nb);
  266. if_cs_write16_rep(card, IF_CS_HOST_CMD, buf, nb / 2);
  267. /* Are we supposed to transfer an odd amount of bytes? */
  268. if (nb & 1)
  269. if_cs_write8(card, IF_CS_HOST_CMD, buf[nb-1]);
  270. /* "Assert the download over interrupt command in the Host
  271. * status register" */
  272. if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  273. /* "Assert the download over interrupt command in the Card
  274. * interrupt case register" */
  275. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  276. ret = 0;
  277. done:
  278. if_cs_enable_ints(card);
  279. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  280. return ret;
  281. }
  282. /*
  283. * Called from if_cs_host_to_card to send a data to the hardware
  284. */
  285. static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
  286. {
  287. struct if_cs_card *card = (struct if_cs_card *)priv->card;
  288. u16 status;
  289. lbs_deb_enter(LBS_DEB_CS);
  290. if_cs_disable_ints(card);
  291. status = if_cs_read16(card, IF_CS_CARD_STATUS);
  292. BUG_ON((status & IF_CS_BIT_TX) == 0);
  293. if_cs_write16(card, IF_CS_HOST_WRITE_LEN, nb);
  294. /* write even number of bytes, then odd byte if necessary */
  295. if_cs_write16_rep(card, IF_CS_HOST_WRITE, buf, nb / 2);
  296. if (nb & 1)
  297. if_cs_write8(card, IF_CS_HOST_WRITE, buf[nb-1]);
  298. if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
  299. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
  300. if_cs_enable_ints(card);
  301. lbs_deb_leave(LBS_DEB_CS);
  302. }
  303. /*
  304. * Get the command result out of the card.
  305. */
  306. static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
  307. {
  308. unsigned long flags;
  309. int ret = -1;
  310. u16 status;
  311. lbs_deb_enter(LBS_DEB_CS);
  312. /* is hardware ready? */
  313. status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
  314. if ((status & IF_CS_BIT_RESP) == 0) {
  315. lbs_pr_err("no cmd response in card\n");
  316. *len = 0;
  317. goto out;
  318. }
  319. *len = if_cs_read16(priv->card, IF_CS_CARD_CMD_LEN);
  320. if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
  321. lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
  322. goto out;
  323. }
  324. /* read even number of bytes, then odd byte if necessary */
  325. if_cs_read16_rep(priv->card, IF_CS_CARD_CMD, data, *len/sizeof(u16));
  326. if (*len & 1)
  327. data[*len-1] = if_cs_read8(priv->card, IF_CS_CARD_CMD);
  328. /* This is a workaround for a firmware that reports too much
  329. * bytes */
  330. *len -= 8;
  331. ret = 0;
  332. /* Clear this flag again */
  333. spin_lock_irqsave(&priv->driver_lock, flags);
  334. priv->dnld_sent = DNLD_RES_RECEIVED;
  335. spin_unlock_irqrestore(&priv->driver_lock, flags);
  336. out:
  337. lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
  338. return ret;
  339. }
  340. static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
  341. {
  342. struct sk_buff *skb = NULL;
  343. u16 len;
  344. u8 *data;
  345. lbs_deb_enter(LBS_DEB_CS);
  346. len = if_cs_read16(priv->card, IF_CS_READ_LEN);
  347. if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
  348. lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
  349. priv->stats.rx_dropped++;
  350. goto dat_err;
  351. }
  352. skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
  353. if (!skb)
  354. goto out;
  355. skb_put(skb, len);
  356. skb_reserve(skb, 2);/* 16 byte align */
  357. data = skb->data;
  358. /* read even number of bytes, then odd byte if necessary */
  359. if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
  360. if (len & 1)
  361. data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
  362. dat_err:
  363. if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
  364. if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
  365. out:
  366. lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
  367. return skb;
  368. }
  369. static irqreturn_t if_cs_interrupt(int irq, void *data)
  370. {
  371. struct if_cs_card *card = data;
  372. struct lbs_private *priv = card->priv;
  373. u16 cause;
  374. lbs_deb_enter(LBS_DEB_CS);
  375. /* Ask card interrupt cause register if there is something for us */
  376. cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
  377. lbs_deb_cs("cause 0x%04x\n", cause);
  378. if (cause == 0) {
  379. /* Not for us */
  380. return IRQ_NONE;
  381. }
  382. if (cause == 0xffff) {
  383. /* Read in junk, the card has probably been removed */
  384. card->priv->surpriseremoved = 1;
  385. return IRQ_HANDLED;
  386. }
  387. if (cause & IF_CS_BIT_RX) {
  388. struct sk_buff *skb;
  389. lbs_deb_cs("rx packet\n");
  390. skb = if_cs_receive_data(priv);
  391. if (skb)
  392. lbs_process_rxed_packet(priv, skb);
  393. }
  394. if (cause & IF_CS_BIT_TX) {
  395. lbs_deb_cs("tx done\n");
  396. lbs_host_to_card_done(priv);
  397. }
  398. if (cause & IF_CS_BIT_RESP) {
  399. unsigned long flags;
  400. u8 i;
  401. lbs_deb_cs("cmd resp\n");
  402. spin_lock_irqsave(&priv->driver_lock, flags);
  403. i = (priv->resp_idx == 0) ? 1 : 0;
  404. spin_unlock_irqrestore(&priv->driver_lock, flags);
  405. BUG_ON(priv->resp_len[i]);
  406. if_cs_receive_cmdres(priv, priv->resp_buf[i],
  407. &priv->resp_len[i]);
  408. spin_lock_irqsave(&priv->driver_lock, flags);
  409. lbs_notify_command_response(priv, i);
  410. spin_unlock_irqrestore(&priv->driver_lock, flags);
  411. }
  412. if (cause & IF_CS_BIT_EVENT) {
  413. u16 event = if_cs_read16(priv->card, IF_CS_CARD_STATUS)
  414. & IF_CS_CARD_STATUS_MASK;
  415. if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
  416. IF_CS_BIT_EVENT);
  417. lbs_deb_cs("host event 0x%04x\n", event);
  418. lbs_queue_event(priv, event >> 8 & 0xff);
  419. }
  420. /* Clear interrupt cause */
  421. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
  422. lbs_deb_leave(LBS_DEB_CS);
  423. return IRQ_HANDLED;
  424. }
  425. /********************************************************************/
  426. /* Firmware */
  427. /********************************************************************/
  428. /*
  429. * Tries to program the helper firmware.
  430. *
  431. * Return 0 on success
  432. */
  433. static int if_cs_prog_helper(struct if_cs_card *card)
  434. {
  435. int ret = 0;
  436. int sent = 0;
  437. u8 scratch;
  438. const struct firmware *fw;
  439. lbs_deb_enter(LBS_DEB_CS);
  440. scratch = if_cs_read8(card, IF_CS_SCRATCH);
  441. /* "If the value is 0x5a, the firmware is already
  442. * downloaded successfully"
  443. */
  444. if (scratch == IF_CS_SCRATCH_HELPER_OK)
  445. goto done;
  446. /* "If the value is != 00, it is invalid value of register */
  447. if (scratch != IF_CS_SCRATCH_BOOT_OK) {
  448. ret = -ENODEV;
  449. goto done;
  450. }
  451. /* TODO: make firmware file configurable */
  452. ret = request_firmware(&fw, "libertas_cs_helper.fw",
  453. &handle_to_dev(card->p_dev));
  454. if (ret) {
  455. lbs_pr_err("can't load helper firmware\n");
  456. ret = -ENODEV;
  457. goto done;
  458. }
  459. lbs_deb_cs("helper size %td\n", fw->size);
  460. /* "Set the 5 bytes of the helper image to 0" */
  461. /* Not needed, this contains an ARM branch instruction */
  462. for (;;) {
  463. /* "the number of bytes to send is 256" */
  464. int count = 256;
  465. int remain = fw->size - sent;
  466. if (remain < count)
  467. count = remain;
  468. /* "write the number of bytes to be sent to the I/O Command
  469. * write length register" */
  470. if_cs_write16(card, IF_CS_HOST_CMD_LEN, count);
  471. /* "write this to I/O Command port register as 16 bit writes */
  472. if (count)
  473. if_cs_write16_rep(card, IF_CS_HOST_CMD,
  474. &fw->data[sent],
  475. count >> 1);
  476. /* "Assert the download over interrupt command in the Host
  477. * status register" */
  478. if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  479. /* "Assert the download over interrupt command in the Card
  480. * interrupt case register" */
  481. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  482. /* "The host polls the Card Status register ... for 50 ms before
  483. declaring a failure */
  484. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
  485. IF_CS_BIT_COMMAND);
  486. if (ret < 0) {
  487. lbs_pr_err("can't download helper at 0x%x, ret %d\n",
  488. sent, ret);
  489. goto done;
  490. }
  491. if (count == 0)
  492. break;
  493. sent += count;
  494. }
  495. release_firmware(fw);
  496. ret = 0;
  497. done:
  498. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  499. return ret;
  500. }
  501. static int if_cs_prog_real(struct if_cs_card *card)
  502. {
  503. const struct firmware *fw;
  504. int ret = 0;
  505. int retry = 0;
  506. int len = 0;
  507. int sent;
  508. lbs_deb_enter(LBS_DEB_CS);
  509. /* TODO: make firmware file configurable */
  510. ret = request_firmware(&fw, "libertas_cs.fw",
  511. &handle_to_dev(card->p_dev));
  512. if (ret) {
  513. lbs_pr_err("can't load firmware\n");
  514. ret = -ENODEV;
  515. goto done;
  516. }
  517. lbs_deb_cs("fw size %td\n", fw->size);
  518. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_SQ_READ_LOW,
  519. IF_CS_CARD_SQ_HELPER_OK);
  520. if (ret < 0) {
  521. lbs_pr_err("helper firmware doesn't answer\n");
  522. goto err_release;
  523. }
  524. for (sent = 0; sent < fw->size; sent += len) {
  525. len = if_cs_read16(card, IF_CS_CARD_SQ_READ_LOW);
  526. if (len & 1) {
  527. retry++;
  528. lbs_pr_info("odd, need to retry this firmware block\n");
  529. } else {
  530. retry = 0;
  531. }
  532. if (retry > 20) {
  533. lbs_pr_err("could not download firmware\n");
  534. ret = -ENODEV;
  535. goto err_release;
  536. }
  537. if (retry) {
  538. sent -= len;
  539. }
  540. if_cs_write16(card, IF_CS_HOST_CMD_LEN, len);
  541. if_cs_write16_rep(card, IF_CS_HOST_CMD,
  542. &fw->data[sent],
  543. (len+1) >> 1);
  544. if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
  545. if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
  546. ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
  547. IF_CS_BIT_COMMAND);
  548. if (ret < 0) {
  549. lbs_pr_err("can't download firmware at 0x%x\n", sent);
  550. goto err_release;
  551. }
  552. }
  553. ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
  554. if (ret < 0) {
  555. lbs_pr_err("firmware download failed\n");
  556. goto err_release;
  557. }
  558. ret = 0;
  559. goto done;
  560. err_release:
  561. release_firmware(fw);
  562. done:
  563. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  564. return ret;
  565. }
  566. /********************************************************************/
  567. /* Callback functions for libertas.ko */
  568. /********************************************************************/
  569. /* Send commands or data packets to the card */
  570. static int if_cs_host_to_card(struct lbs_private *priv,
  571. u8 type,
  572. u8 *buf,
  573. u16 nb)
  574. {
  575. int ret = -1;
  576. lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
  577. switch (type) {
  578. case MVMS_DAT:
  579. priv->dnld_sent = DNLD_DATA_SENT;
  580. if_cs_send_data(priv, buf, nb);
  581. ret = 0;
  582. break;
  583. case MVMS_CMD:
  584. priv->dnld_sent = DNLD_CMD_SENT;
  585. ret = if_cs_send_cmd(priv, buf, nb);
  586. break;
  587. default:
  588. lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
  589. }
  590. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  591. return ret;
  592. }
  593. /********************************************************************/
  594. /* Card Services */
  595. /********************************************************************/
  596. /*
  597. * After a card is removed, if_cs_release() will unregister the
  598. * device, and release the PCMCIA configuration. If the device is
  599. * still open, this will be postponed until it is closed.
  600. */
  601. static void if_cs_release(struct pcmcia_device *p_dev)
  602. {
  603. struct if_cs_card *card = p_dev->priv;
  604. lbs_deb_enter(LBS_DEB_CS);
  605. free_irq(p_dev->irq.AssignedIRQ, card);
  606. pcmcia_disable_device(p_dev);
  607. if (card->iobase)
  608. ioport_unmap(card->iobase);
  609. lbs_deb_leave(LBS_DEB_CS);
  610. }
  611. /*
  612. * This creates an "instance" of the driver, allocating local data
  613. * structures for one device. The device is registered with Card
  614. * Services.
  615. *
  616. * The dev_link structure is initialized, but we don't actually
  617. * configure the card at this point -- we wait until we receive a card
  618. * insertion event.
  619. */
  620. static int if_cs_probe(struct pcmcia_device *p_dev)
  621. {
  622. int ret = -ENOMEM;
  623. struct lbs_private *priv;
  624. struct if_cs_card *card;
  625. /* CIS parsing */
  626. tuple_t tuple;
  627. cisparse_t parse;
  628. cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
  629. cistpl_io_t *io = &cfg->io;
  630. u_char buf[64];
  631. lbs_deb_enter(LBS_DEB_CS);
  632. card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
  633. if (!card) {
  634. lbs_pr_err("error in kzalloc\n");
  635. goto out;
  636. }
  637. card->p_dev = p_dev;
  638. p_dev->priv = card;
  639. p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
  640. p_dev->irq.Handler = NULL;
  641. p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
  642. p_dev->conf.Attributes = 0;
  643. p_dev->conf.IntType = INT_MEMORY_AND_IO;
  644. tuple.Attributes = 0;
  645. tuple.TupleData = buf;
  646. tuple.TupleDataMax = sizeof(buf);
  647. tuple.TupleOffset = 0;
  648. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  649. if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
  650. (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
  651. (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
  652. {
  653. lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
  654. goto out1;
  655. }
  656. p_dev->conf.ConfigIndex = cfg->index;
  657. /* Do we need to allocate an interrupt? */
  658. if (cfg->irq.IRQInfo1) {
  659. p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
  660. }
  661. /* IO window settings */
  662. if (cfg->io.nwin != 1) {
  663. lbs_pr_err("wrong CIS (check number of IO windows)\n");
  664. ret = -ENODEV;
  665. goto out1;
  666. }
  667. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  668. p_dev->io.BasePort1 = io->win[0].base;
  669. p_dev->io.NumPorts1 = io->win[0].len;
  670. /* This reserves IO space but doesn't actually enable it */
  671. ret = pcmcia_request_io(p_dev, &p_dev->io);
  672. if (ret) {
  673. lbs_pr_err("error in pcmcia_request_io\n");
  674. goto out1;
  675. }
  676. /*
  677. * Allocate an interrupt line. Note that this does not assign
  678. * a handler to the interrupt, unless the 'Handler' member of
  679. * the irq structure is initialized.
  680. */
  681. if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
  682. ret = pcmcia_request_irq(p_dev, &p_dev->irq);
  683. if (ret) {
  684. lbs_pr_err("error in pcmcia_request_irq\n");
  685. goto out1;
  686. }
  687. }
  688. /* Initialize io access */
  689. card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
  690. if (!card->iobase) {
  691. lbs_pr_err("error in ioport_map\n");
  692. ret = -EIO;
  693. goto out1;
  694. }
  695. /*
  696. * This actually configures the PCMCIA socket -- setting up
  697. * the I/O windows and the interrupt mapping, and putting the
  698. * card and host interface into "Memory and IO" mode.
  699. */
  700. ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
  701. if (ret) {
  702. lbs_pr_err("error in pcmcia_request_configuration\n");
  703. goto out2;
  704. }
  705. /* Finally, report what we've done */
  706. lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
  707. p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
  708. p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
  709. /* Check if we have a current silicon */
  710. if (if_cs_read8(card, IF_CS_PRODUCT_ID) < IF_CS_CF8385_B1_REV) {
  711. lbs_pr_err("old chips like 8385 rev B1 aren't supported\n");
  712. ret = -ENODEV;
  713. goto out2;
  714. }
  715. /* Load the firmware early, before calling into libertas.ko */
  716. ret = if_cs_prog_helper(card);
  717. if (ret == 0)
  718. ret = if_cs_prog_real(card);
  719. if (ret)
  720. goto out2;
  721. /* Make this card known to the libertas driver */
  722. priv = lbs_add_card(card, &p_dev->dev);
  723. if (!priv) {
  724. ret = -ENOMEM;
  725. goto out2;
  726. }
  727. /* Finish setting up fields in lbs_private */
  728. card->priv = priv;
  729. priv->card = card;
  730. priv->hw_host_to_card = if_cs_host_to_card;
  731. priv->fw_ready = 1;
  732. /* Now actually get the IRQ */
  733. ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
  734. IRQF_SHARED, DRV_NAME, card);
  735. if (ret) {
  736. lbs_pr_err("error in request_irq\n");
  737. goto out3;
  738. }
  739. /* Clear any interrupt cause that happend while sending
  740. * firmware/initializing card */
  741. if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
  742. if_cs_enable_ints(card);
  743. /* And finally bring the card up */
  744. if (lbs_start_card(priv) != 0) {
  745. lbs_pr_err("could not activate card\n");
  746. goto out3;
  747. }
  748. /* The firmware for the CF card supports powersave */
  749. priv->ps_supported = 1;
  750. ret = 0;
  751. goto out;
  752. out3:
  753. lbs_remove_card(priv);
  754. out2:
  755. ioport_unmap(card->iobase);
  756. out1:
  757. pcmcia_disable_device(p_dev);
  758. out:
  759. lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
  760. return ret;
  761. }
  762. /*
  763. * This deletes a driver "instance". The device is de-registered with
  764. * Card Services. If it has been released, all local data structures
  765. * are freed. Otherwise, the structures will be freed when the device
  766. * is released.
  767. */
  768. static void if_cs_detach(struct pcmcia_device *p_dev)
  769. {
  770. struct if_cs_card *card = p_dev->priv;
  771. lbs_deb_enter(LBS_DEB_CS);
  772. lbs_stop_card(card->priv);
  773. lbs_remove_card(card->priv);
  774. if_cs_disable_ints(card);
  775. if_cs_release(p_dev);
  776. kfree(card);
  777. lbs_deb_leave(LBS_DEB_CS);
  778. }
  779. /********************************************************************/
  780. /* Module initialization */
  781. /********************************************************************/
  782. static struct pcmcia_device_id if_cs_ids[] = {
  783. PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
  784. PCMCIA_DEVICE_NULL,
  785. };
  786. MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
  787. static struct pcmcia_driver lbs_driver = {
  788. .owner = THIS_MODULE,
  789. .drv = {
  790. .name = DRV_NAME,
  791. },
  792. .probe = if_cs_probe,
  793. .remove = if_cs_detach,
  794. .id_table = if_cs_ids,
  795. };
  796. static int __init if_cs_init(void)
  797. {
  798. int ret;
  799. lbs_deb_enter(LBS_DEB_CS);
  800. ret = pcmcia_register_driver(&lbs_driver);
  801. lbs_deb_leave(LBS_DEB_CS);
  802. return ret;
  803. }
  804. static void __exit if_cs_exit(void)
  805. {
  806. lbs_deb_enter(LBS_DEB_CS);
  807. pcmcia_unregister_driver(&lbs_driver);
  808. lbs_deb_leave(LBS_DEB_CS);
  809. }
  810. module_init(if_cs_init);
  811. module_exit(if_cs_exit);