if_cs.c 23 KB

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