if_cs.c 26 KB

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