if_cs.c 26 KB

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