if_cs.c 24 KB

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