if_cs.c 27 KB

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