if_cs.c 26 KB

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