if_cs.c 27 KB

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