if_cs.c 22 KB

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