if_spi.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /*
  2. * linux/drivers/net/wireless/libertas/if_spi.c
  3. *
  4. * Driver for Marvell SPI WLAN cards.
  5. *
  6. * Copyright 2008 Analog Devices Inc.
  7. *
  8. * Authors:
  9. * Andrey Yurovsky <andrey@cozybit.com>
  10. * Colin McCabe <colin@cozybit.com>
  11. *
  12. * Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. */
  19. #include <linux/moduleparam.h>
  20. #include <linux/firmware.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/kthread.h>
  23. #include <linux/list.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/semaphore.h>
  26. #include <linux/slab.h>
  27. #include <linux/spi/libertas_spi.h>
  28. #include <linux/spi/spi.h>
  29. #include "host.h"
  30. #include "decl.h"
  31. #include "defs.h"
  32. #include "dev.h"
  33. #include "if_spi.h"
  34. struct if_spi_card {
  35. struct spi_device *spi;
  36. struct lbs_private *priv;
  37. struct libertas_spi_platform_data *pdata;
  38. /* The card ID and card revision, as reported by the hardware. */
  39. u16 card_id;
  40. u8 card_rev;
  41. /* The last time that we initiated an SPU operation */
  42. unsigned long prev_xfer_time;
  43. int use_dummy_writes;
  44. unsigned long spu_port_delay;
  45. unsigned long spu_reg_delay;
  46. /* Handles all SPI communication (except for FW load) */
  47. struct task_struct *spi_thread;
  48. int run_thread;
  49. /* Used to wake up the spi_thread */
  50. struct semaphore spi_ready;
  51. struct semaphore spi_thread_terminated;
  52. u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
  53. };
  54. static void free_if_spi_card(struct if_spi_card *card)
  55. {
  56. spi_set_drvdata(card->spi, NULL);
  57. kfree(card);
  58. }
  59. #define MODEL_8385 0x04
  60. #define MODEL_8686 0x0b
  61. #define MODEL_8688 0x10
  62. static const struct lbs_fw_table fw_table[] = {
  63. { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
  64. { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
  65. { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
  66. { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
  67. { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
  68. { 0, NULL, NULL }
  69. };
  70. MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
  71. MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
  72. MODULE_FIRMWARE("libertas/gspi8385.bin");
  73. MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
  74. MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
  75. MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
  76. MODULE_FIRMWARE("libertas/gspi8686.bin");
  77. MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
  78. MODULE_FIRMWARE("libertas/gspi8688.bin");
  79. /*
  80. * SPI Interface Unit Routines
  81. *
  82. * The SPU sits between the host and the WLAN module.
  83. * All communication with the firmware is through SPU transactions.
  84. *
  85. * First we have to put a SPU register name on the bus. Then we can
  86. * either read from or write to that register.
  87. *
  88. */
  89. static void spu_transaction_init(struct if_spi_card *card)
  90. {
  91. if (!time_after(jiffies, card->prev_xfer_time + 1)) {
  92. /* Unfortunately, the SPU requires a delay between successive
  93. * transactions. If our last transaction was more than a jiffy
  94. * ago, we have obviously already delayed enough.
  95. * If not, we have to busy-wait to be on the safe side. */
  96. ndelay(400);
  97. }
  98. }
  99. static void spu_transaction_finish(struct if_spi_card *card)
  100. {
  101. card->prev_xfer_time = jiffies;
  102. }
  103. /* Write out a byte buffer to an SPI register,
  104. * using a series of 16-bit transfers. */
  105. static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
  106. {
  107. int err = 0;
  108. __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
  109. struct spi_message m;
  110. struct spi_transfer reg_trans;
  111. struct spi_transfer data_trans;
  112. spi_message_init(&m);
  113. memset(&reg_trans, 0, sizeof(reg_trans));
  114. memset(&data_trans, 0, sizeof(data_trans));
  115. /* You must give an even number of bytes to the SPU, even if it
  116. * doesn't care about the last one. */
  117. BUG_ON(len & 0x1);
  118. spu_transaction_init(card);
  119. /* write SPU register index */
  120. reg_trans.tx_buf = &reg_out;
  121. reg_trans.len = sizeof(reg_out);
  122. data_trans.tx_buf = buf;
  123. data_trans.len = len;
  124. spi_message_add_tail(&reg_trans, &m);
  125. spi_message_add_tail(&data_trans, &m);
  126. err = spi_sync(card->spi, &m);
  127. spu_transaction_finish(card);
  128. return err;
  129. }
  130. static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
  131. {
  132. __le16 buff;
  133. buff = cpu_to_le16(val);
  134. return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
  135. }
  136. static inline int spu_reg_is_port_reg(u16 reg)
  137. {
  138. switch (reg) {
  139. case IF_SPI_IO_RDWRPORT_REG:
  140. case IF_SPI_CMD_RDWRPORT_REG:
  141. case IF_SPI_DATA_RDWRPORT_REG:
  142. return 1;
  143. default:
  144. return 0;
  145. }
  146. }
  147. static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
  148. {
  149. unsigned int delay;
  150. int err = 0;
  151. __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
  152. struct spi_message m;
  153. struct spi_transfer reg_trans;
  154. struct spi_transfer dummy_trans;
  155. struct spi_transfer data_trans;
  156. /* You must take an even number of bytes from the SPU, even if you
  157. * don't care about the last one. */
  158. BUG_ON(len & 0x1);
  159. spu_transaction_init(card);
  160. spi_message_init(&m);
  161. memset(&reg_trans, 0, sizeof(reg_trans));
  162. memset(&dummy_trans, 0, sizeof(dummy_trans));
  163. memset(&data_trans, 0, sizeof(data_trans));
  164. /* write SPU register index */
  165. reg_trans.tx_buf = &reg_out;
  166. reg_trans.len = sizeof(reg_out);
  167. spi_message_add_tail(&reg_trans, &m);
  168. delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
  169. card->spu_reg_delay;
  170. if (card->use_dummy_writes) {
  171. /* Clock in dummy cycles while the SPU fills the FIFO */
  172. dummy_trans.len = delay / 8;
  173. spi_message_add_tail(&dummy_trans, &m);
  174. } else {
  175. /* Busy-wait while the SPU fills the FIFO */
  176. reg_trans.delay_usecs =
  177. DIV_ROUND_UP((100 + (delay * 10)), 1000);
  178. }
  179. /* read in data */
  180. data_trans.rx_buf = buf;
  181. data_trans.len = len;
  182. spi_message_add_tail(&data_trans, &m);
  183. err = spi_sync(card->spi, &m);
  184. spu_transaction_finish(card);
  185. return err;
  186. }
  187. /* Read 16 bits from an SPI register */
  188. static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
  189. {
  190. __le16 buf;
  191. int ret;
  192. ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
  193. if (ret == 0)
  194. *val = le16_to_cpup(&buf);
  195. return ret;
  196. }
  197. /* Read 32 bits from an SPI register.
  198. * The low 16 bits are read first. */
  199. static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
  200. {
  201. __le32 buf;
  202. int err;
  203. err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
  204. if (!err)
  205. *val = le32_to_cpup(&buf);
  206. return err;
  207. }
  208. /* Keep reading 16 bits from an SPI register until you get the correct result.
  209. *
  210. * If mask = 0, the correct result is any non-zero number.
  211. * If mask != 0, the correct result is any number where
  212. * number & target_mask == target
  213. *
  214. * Returns -ETIMEDOUT if a second passes without the correct result. */
  215. static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
  216. u16 target_mask, u16 target)
  217. {
  218. int err;
  219. unsigned long timeout = jiffies + 5*HZ;
  220. while (1) {
  221. u16 val;
  222. err = spu_read_u16(card, reg, &val);
  223. if (err)
  224. return err;
  225. if (target_mask) {
  226. if ((val & target_mask) == target)
  227. return 0;
  228. } else {
  229. if (val)
  230. return 0;
  231. }
  232. udelay(100);
  233. if (time_after(jiffies, timeout)) {
  234. lbs_pr_err("%s: timeout with val=%02x, "
  235. "target_mask=%02x, target=%02x\n",
  236. __func__, val, target_mask, target);
  237. return -ETIMEDOUT;
  238. }
  239. }
  240. }
  241. /* Read 16 bits from an SPI register until you receive a specific value.
  242. * Returns -ETIMEDOUT if a 4 tries pass without success. */
  243. static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
  244. {
  245. int err, try;
  246. for (try = 0; try < 4; ++try) {
  247. u32 val = 0;
  248. err = spu_read_u32(card, reg, &val);
  249. if (err)
  250. return err;
  251. if (val == target)
  252. return 0;
  253. mdelay(100);
  254. }
  255. return -ETIMEDOUT;
  256. }
  257. static int spu_set_interrupt_mode(struct if_spi_card *card,
  258. int suppress_host_int,
  259. int auto_int)
  260. {
  261. int err = 0;
  262. /* We can suppress a host interrupt by clearing the appropriate
  263. * bit in the "host interrupt status mask" register */
  264. if (suppress_host_int) {
  265. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
  266. if (err)
  267. return err;
  268. } else {
  269. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
  270. IF_SPI_HISM_TX_DOWNLOAD_RDY |
  271. IF_SPI_HISM_RX_UPLOAD_RDY |
  272. IF_SPI_HISM_CMD_DOWNLOAD_RDY |
  273. IF_SPI_HISM_CARDEVENT |
  274. IF_SPI_HISM_CMD_UPLOAD_RDY);
  275. if (err)
  276. return err;
  277. }
  278. /* If auto-interrupts are on, the completion of certain transactions
  279. * will trigger an interrupt automatically. If auto-interrupts
  280. * are off, we need to set the "Card Interrupt Cause" register to
  281. * trigger a card interrupt. */
  282. if (auto_int) {
  283. err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
  284. IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
  285. IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
  286. IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
  287. IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
  288. if (err)
  289. return err;
  290. } else {
  291. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
  292. if (err)
  293. return err;
  294. }
  295. return err;
  296. }
  297. static int spu_get_chip_revision(struct if_spi_card *card,
  298. u16 *card_id, u8 *card_rev)
  299. {
  300. int err = 0;
  301. u32 dev_ctrl;
  302. err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
  303. if (err)
  304. return err;
  305. *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
  306. *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
  307. return err;
  308. }
  309. static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
  310. {
  311. int err = 0;
  312. u16 rval;
  313. /* set bus mode */
  314. err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
  315. if (err)
  316. return err;
  317. /* Check that we were able to read back what we just wrote. */
  318. err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
  319. if (err)
  320. return err;
  321. if ((rval & 0xF) != mode) {
  322. lbs_pr_err("Can't read bus mode register.\n");
  323. return -EIO;
  324. }
  325. return 0;
  326. }
  327. static int spu_init(struct if_spi_card *card, int use_dummy_writes)
  328. {
  329. int err = 0;
  330. u32 delay;
  331. /* We have to start up in timed delay mode so that we can safely
  332. * read the Delay Read Register. */
  333. card->use_dummy_writes = 0;
  334. err = spu_set_bus_mode(card,
  335. IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
  336. IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
  337. IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
  338. if (err)
  339. return err;
  340. card->spu_port_delay = 1000;
  341. card->spu_reg_delay = 1000;
  342. err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
  343. if (err)
  344. return err;
  345. card->spu_port_delay = delay & 0x0000ffff;
  346. card->spu_reg_delay = (delay & 0xffff0000) >> 16;
  347. /* If dummy clock delay mode has been requested, switch to it now */
  348. if (use_dummy_writes) {
  349. card->use_dummy_writes = 1;
  350. err = spu_set_bus_mode(card,
  351. IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
  352. IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
  353. IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
  354. if (err)
  355. return err;
  356. }
  357. lbs_deb_spi("Initialized SPU unit. "
  358. "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
  359. card->spu_port_delay, card->spu_reg_delay);
  360. return err;
  361. }
  362. /*
  363. * Firmware Loading
  364. */
  365. static int if_spi_prog_helper_firmware(struct if_spi_card *card,
  366. const struct firmware *firmware)
  367. {
  368. int err = 0;
  369. int bytes_remaining;
  370. const u8 *fw;
  371. u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
  372. lbs_deb_enter(LBS_DEB_SPI);
  373. err = spu_set_interrupt_mode(card, 1, 0);
  374. if (err)
  375. goto out;
  376. bytes_remaining = firmware->size;
  377. fw = firmware->data;
  378. /* Load helper firmware image */
  379. while (bytes_remaining > 0) {
  380. /* Scratch pad 1 should contain the number of bytes we
  381. * want to download to the firmware */
  382. err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
  383. HELPER_FW_LOAD_CHUNK_SZ);
  384. if (err)
  385. goto out;
  386. err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  387. IF_SPI_HIST_CMD_DOWNLOAD_RDY,
  388. IF_SPI_HIST_CMD_DOWNLOAD_RDY);
  389. if (err)
  390. goto out;
  391. /* Feed the data into the command read/write port reg
  392. * in chunks of 64 bytes */
  393. memset(temp, 0, sizeof(temp));
  394. memcpy(temp, fw,
  395. min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
  396. mdelay(10);
  397. err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
  398. temp, HELPER_FW_LOAD_CHUNK_SZ);
  399. if (err)
  400. goto out;
  401. /* Interrupt the boot code */
  402. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
  403. if (err)
  404. goto out;
  405. err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
  406. IF_SPI_CIC_CMD_DOWNLOAD_OVER);
  407. if (err)
  408. goto out;
  409. bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
  410. fw += HELPER_FW_LOAD_CHUNK_SZ;
  411. }
  412. /* Once the helper / single stage firmware download is complete,
  413. * write 0 to scratch pad 1 and interrupt the
  414. * bootloader. This completes the helper download. */
  415. err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
  416. if (err)
  417. goto out;
  418. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
  419. if (err)
  420. goto out;
  421. err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
  422. IF_SPI_CIC_CMD_DOWNLOAD_OVER);
  423. goto out;
  424. lbs_deb_spi("waiting for helper to boot...\n");
  425. out:
  426. if (err)
  427. lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
  428. lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
  429. return err;
  430. }
  431. /* Returns the length of the next packet the firmware expects us to send
  432. * Sets crc_err if the previous transfer had a CRC error. */
  433. static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
  434. int *crc_err)
  435. {
  436. u16 len;
  437. int err = 0;
  438. /* wait until the host interrupt status register indicates
  439. * that we are ready to download */
  440. err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  441. IF_SPI_HIST_CMD_DOWNLOAD_RDY,
  442. IF_SPI_HIST_CMD_DOWNLOAD_RDY);
  443. if (err) {
  444. lbs_pr_err("timed out waiting for host_int_status\n");
  445. return err;
  446. }
  447. /* Ask the device how many bytes of firmware it wants. */
  448. err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
  449. if (err)
  450. return err;
  451. if (len > IF_SPI_CMD_BUF_SIZE) {
  452. lbs_pr_err("firmware load device requested a larger "
  453. "tranfer than we are prepared to "
  454. "handle. (len = %d)\n", len);
  455. return -EIO;
  456. }
  457. if (len & 0x1) {
  458. lbs_deb_spi("%s: crc error\n", __func__);
  459. len &= ~0x1;
  460. *crc_err = 1;
  461. } else
  462. *crc_err = 0;
  463. return len;
  464. }
  465. static int if_spi_prog_main_firmware(struct if_spi_card *card,
  466. const struct firmware *firmware)
  467. {
  468. int len, prev_len;
  469. int bytes, crc_err = 0, err = 0;
  470. const u8 *fw;
  471. u16 num_crc_errs;
  472. lbs_deb_enter(LBS_DEB_SPI);
  473. err = spu_set_interrupt_mode(card, 1, 0);
  474. if (err)
  475. goto out;
  476. err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
  477. if (err) {
  478. lbs_pr_err("%s: timed out waiting for initial "
  479. "scratch reg = 0\n", __func__);
  480. goto out;
  481. }
  482. num_crc_errs = 0;
  483. prev_len = 0;
  484. bytes = firmware->size;
  485. fw = firmware->data;
  486. while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
  487. if (len < 0) {
  488. err = len;
  489. goto out;
  490. }
  491. if (bytes < 0) {
  492. /* If there are no more bytes left, we would normally
  493. * expect to have terminated with len = 0 */
  494. lbs_pr_err("Firmware load wants more bytes "
  495. "than we have to offer.\n");
  496. break;
  497. }
  498. if (crc_err) {
  499. /* Previous transfer failed. */
  500. if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
  501. lbs_pr_err("Too many CRC errors encountered "
  502. "in firmware load.\n");
  503. err = -EIO;
  504. goto out;
  505. }
  506. } else {
  507. /* Previous transfer succeeded. Advance counters. */
  508. bytes -= prev_len;
  509. fw += prev_len;
  510. }
  511. if (bytes < len) {
  512. memset(card->cmd_buffer, 0, len);
  513. memcpy(card->cmd_buffer, fw, bytes);
  514. } else
  515. memcpy(card->cmd_buffer, fw, len);
  516. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
  517. if (err)
  518. goto out;
  519. err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
  520. card->cmd_buffer, len);
  521. if (err)
  522. goto out;
  523. err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
  524. IF_SPI_CIC_CMD_DOWNLOAD_OVER);
  525. if (err)
  526. goto out;
  527. prev_len = len;
  528. }
  529. if (bytes > prev_len) {
  530. lbs_pr_err("firmware load wants fewer bytes than "
  531. "we have to offer.\n");
  532. }
  533. /* Confirm firmware download */
  534. err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
  535. SUCCESSFUL_FW_DOWNLOAD_MAGIC);
  536. if (err) {
  537. lbs_pr_err("failed to confirm the firmware download\n");
  538. goto out;
  539. }
  540. out:
  541. if (err)
  542. lbs_pr_err("failed to load firmware (err=%d)\n", err);
  543. lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
  544. return err;
  545. }
  546. /*
  547. * SPI Transfer Thread
  548. *
  549. * The SPI thread handles all SPI transfers, so there is no need for a lock.
  550. */
  551. /* Move a command from the card to the host */
  552. static int if_spi_c2h_cmd(struct if_spi_card *card)
  553. {
  554. struct lbs_private *priv = card->priv;
  555. unsigned long flags;
  556. int err = 0;
  557. u16 len;
  558. u8 i;
  559. /* We need a buffer big enough to handle whatever people send to
  560. * hw_host_to_card */
  561. BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
  562. BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
  563. /* It's just annoying if the buffer size isn't a multiple of 4, because
  564. * then we might have len < IF_SPI_CMD_BUF_SIZE but
  565. * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
  566. BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
  567. lbs_deb_enter(LBS_DEB_SPI);
  568. /* How many bytes are there to read? */
  569. err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
  570. if (err)
  571. goto out;
  572. if (!len) {
  573. lbs_pr_err("%s: error: card has no data for host\n",
  574. __func__);
  575. err = -EINVAL;
  576. goto out;
  577. } else if (len > IF_SPI_CMD_BUF_SIZE) {
  578. lbs_pr_err("%s: error: response packet too large: "
  579. "%d bytes, but maximum is %d\n",
  580. __func__, len, IF_SPI_CMD_BUF_SIZE);
  581. err = -EINVAL;
  582. goto out;
  583. }
  584. /* Read the data from the WLAN module into our command buffer */
  585. err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
  586. card->cmd_buffer, ALIGN(len, 4));
  587. if (err)
  588. goto out;
  589. spin_lock_irqsave(&priv->driver_lock, flags);
  590. i = (priv->resp_idx == 0) ? 1 : 0;
  591. BUG_ON(priv->resp_len[i]);
  592. priv->resp_len[i] = len;
  593. memcpy(priv->resp_buf[i], card->cmd_buffer, len);
  594. lbs_notify_command_response(priv, i);
  595. spin_unlock_irqrestore(&priv->driver_lock, flags);
  596. out:
  597. if (err)
  598. lbs_pr_err("%s: err=%d\n", __func__, err);
  599. lbs_deb_leave(LBS_DEB_SPI);
  600. return err;
  601. }
  602. /* Move data from the card to the host */
  603. static int if_spi_c2h_data(struct if_spi_card *card)
  604. {
  605. struct sk_buff *skb;
  606. char *data;
  607. u16 len;
  608. int err = 0;
  609. lbs_deb_enter(LBS_DEB_SPI);
  610. /* How many bytes are there to read? */
  611. err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
  612. if (err)
  613. goto out;
  614. if (!len) {
  615. lbs_pr_err("%s: error: card has no data for host\n",
  616. __func__);
  617. err = -EINVAL;
  618. goto out;
  619. } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
  620. lbs_pr_err("%s: error: card has %d bytes of data, but "
  621. "our maximum skb size is %zu\n",
  622. __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
  623. err = -EINVAL;
  624. goto out;
  625. }
  626. /* TODO: should we allocate a smaller skb if we have less data? */
  627. skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
  628. if (!skb) {
  629. err = -ENOBUFS;
  630. goto out;
  631. }
  632. skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
  633. data = skb_put(skb, len);
  634. /* Read the data from the WLAN module into our skb... */
  635. err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
  636. if (err)
  637. goto free_skb;
  638. /* pass the SKB to libertas */
  639. err = lbs_process_rxed_packet(card->priv, skb);
  640. if (err)
  641. goto free_skb;
  642. /* success */
  643. goto out;
  644. free_skb:
  645. dev_kfree_skb(skb);
  646. out:
  647. if (err)
  648. lbs_pr_err("%s: err=%d\n", __func__, err);
  649. lbs_deb_leave(LBS_DEB_SPI);
  650. return err;
  651. }
  652. /* Inform the host about a card event */
  653. static void if_spi_e2h(struct if_spi_card *card)
  654. {
  655. int err = 0;
  656. u32 cause;
  657. struct lbs_private *priv = card->priv;
  658. err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
  659. if (err)
  660. goto out;
  661. /* re-enable the card event interrupt */
  662. spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  663. ~IF_SPI_HICU_CARD_EVENT);
  664. /* generate a card interrupt */
  665. spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
  666. lbs_queue_event(priv, cause & 0xff);
  667. out:
  668. if (err)
  669. lbs_pr_err("%s: error %d\n", __func__, err);
  670. }
  671. static int lbs_spi_thread(void *data)
  672. {
  673. int err;
  674. struct if_spi_card *card = data;
  675. u16 hiStatus;
  676. while (1) {
  677. /* Wait to be woken up by one of two things. First, our ISR
  678. * could tell us that something happened on the WLAN.
  679. * Secondly, libertas could call hw_host_to_card with more
  680. * data, which we might be able to send.
  681. */
  682. do {
  683. err = down_interruptible(&card->spi_ready);
  684. if (!card->run_thread) {
  685. up(&card->spi_thread_terminated);
  686. do_exit(0);
  687. }
  688. } while (err == EINTR);
  689. /* Read the host interrupt status register to see what we
  690. * can do. */
  691. err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  692. &hiStatus);
  693. if (err) {
  694. lbs_pr_err("I/O error\n");
  695. goto err;
  696. }
  697. if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
  698. err = if_spi_c2h_cmd(card);
  699. if (err)
  700. goto err;
  701. }
  702. if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
  703. err = if_spi_c2h_data(card);
  704. if (err)
  705. goto err;
  706. }
  707. /* workaround: in PS mode, the card does not set the Command
  708. * Download Ready bit, but it sets TX Download Ready. */
  709. if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
  710. (card->priv->psstate != PS_STATE_FULL_POWER &&
  711. (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
  712. lbs_host_to_card_done(card->priv);
  713. }
  714. if (hiStatus & IF_SPI_HIST_CARD_EVENT)
  715. if_spi_e2h(card);
  716. err:
  717. if (err)
  718. lbs_pr_err("%s: got error %d\n", __func__, err);
  719. }
  720. }
  721. /* Block until lbs_spi_thread thread has terminated */
  722. static void if_spi_terminate_spi_thread(struct if_spi_card *card)
  723. {
  724. /* It would be nice to use kthread_stop here, but that function
  725. * can't wake threads waiting for a semaphore. */
  726. card->run_thread = 0;
  727. up(&card->spi_ready);
  728. down(&card->spi_thread_terminated);
  729. }
  730. /*
  731. * Host to Card
  732. *
  733. * Called from Libertas to transfer some data to the WLAN device
  734. * We can't sleep here. */
  735. static int if_spi_host_to_card(struct lbs_private *priv,
  736. u8 type, u8 *buf, u16 nb)
  737. {
  738. int err = 0;
  739. struct if_spi_card *card = priv->card;
  740. lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
  741. nb = ALIGN(nb, 4);
  742. switch (type) {
  743. case MVMS_CMD:
  744. err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG, buf, nb);
  745. break;
  746. case MVMS_DAT:
  747. err = spu_write(card, IF_SPI_DATA_RDWRPORT_REG, buf, nb);
  748. break;
  749. default:
  750. lbs_pr_err("can't transfer buffer of type %d", type);
  751. err = -EINVAL;
  752. break;
  753. }
  754. lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
  755. return err;
  756. }
  757. /*
  758. * Host Interrupts
  759. *
  760. * Service incoming interrupts from the WLAN device. We can't sleep here, so
  761. * don't try to talk on the SPI bus, just wake up the SPI thread.
  762. */
  763. static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
  764. {
  765. struct if_spi_card *card = dev_id;
  766. up(&card->spi_ready);
  767. return IRQ_HANDLED;
  768. }
  769. /*
  770. * SPI callbacks
  771. */
  772. static int __devinit if_spi_probe(struct spi_device *spi)
  773. {
  774. struct if_spi_card *card;
  775. struct lbs_private *priv = NULL;
  776. struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
  777. int err = 0, i;
  778. u32 scratch;
  779. struct sched_param param = { .sched_priority = 1 };
  780. const struct firmware *helper = NULL;
  781. const struct firmware *mainfw = NULL;
  782. lbs_deb_enter(LBS_DEB_SPI);
  783. if (!pdata) {
  784. err = -EINVAL;
  785. goto out;
  786. }
  787. if (pdata->setup) {
  788. err = pdata->setup(spi);
  789. if (err)
  790. goto out;
  791. }
  792. /* Allocate card structure to represent this specific device */
  793. card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
  794. if (!card) {
  795. err = -ENOMEM;
  796. goto out;
  797. }
  798. spi_set_drvdata(spi, card);
  799. card->pdata = pdata;
  800. card->spi = spi;
  801. card->prev_xfer_time = jiffies;
  802. sema_init(&card->spi_ready, 0);
  803. sema_init(&card->spi_thread_terminated, 0);
  804. /* Initialize the SPI Interface Unit */
  805. err = spu_init(card, pdata->use_dummy_writes);
  806. if (err)
  807. goto free_card;
  808. err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
  809. if (err)
  810. goto free_card;
  811. /* Firmware load */
  812. err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
  813. if (err)
  814. goto free_card;
  815. if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
  816. lbs_deb_spi("Firmware is already loaded for "
  817. "Marvell WLAN 802.11 adapter\n");
  818. else {
  819. /* Check if we support this card */
  820. for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
  821. if (card->card_id == fw_table[i].model)
  822. break;
  823. }
  824. if (i == ARRAY_SIZE(fw_table)) {
  825. lbs_pr_err("Unsupported chip_id: 0x%02x\n",
  826. card->card_id);
  827. err = -ENODEV;
  828. goto free_card;
  829. }
  830. err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
  831. card->card_id, &fw_table[0], &helper,
  832. &mainfw);
  833. if (err) {
  834. lbs_pr_err("failed to find firmware (%d)\n", err);
  835. goto free_card;
  836. }
  837. lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
  838. "(chip_id = 0x%04x, chip_rev = 0x%02x) "
  839. "attached to SPI bus_num %d, chip_select %d. "
  840. "spi->max_speed_hz=%d\n",
  841. card->card_id, card->card_rev,
  842. spi->master->bus_num, spi->chip_select,
  843. spi->max_speed_hz);
  844. err = if_spi_prog_helper_firmware(card, helper);
  845. if (err)
  846. goto free_card;
  847. err = if_spi_prog_main_firmware(card, mainfw);
  848. if (err)
  849. goto free_card;
  850. lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
  851. }
  852. err = spu_set_interrupt_mode(card, 0, 1);
  853. if (err)
  854. goto free_card;
  855. /* Register our card with libertas.
  856. * This will call alloc_etherdev */
  857. priv = lbs_add_card(card, &spi->dev);
  858. if (!priv) {
  859. err = -ENOMEM;
  860. goto free_card;
  861. }
  862. card->priv = priv;
  863. priv->card = card;
  864. priv->hw_host_to_card = if_spi_host_to_card;
  865. priv->enter_deep_sleep = NULL;
  866. priv->exit_deep_sleep = NULL;
  867. priv->reset_deep_sleep_wakeup = NULL;
  868. priv->fw_ready = 1;
  869. /* Initialize interrupt handling stuff. */
  870. card->run_thread = 1;
  871. card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
  872. if (IS_ERR(card->spi_thread)) {
  873. card->run_thread = 0;
  874. err = PTR_ERR(card->spi_thread);
  875. lbs_pr_err("error creating SPI thread: err=%d\n", err);
  876. goto remove_card;
  877. }
  878. if (sched_setscheduler(card->spi_thread, SCHED_FIFO, &param))
  879. lbs_pr_err("Error setting scheduler, using default.\n");
  880. err = request_irq(spi->irq, if_spi_host_interrupt,
  881. IRQF_TRIGGER_FALLING, "libertas_spi", card);
  882. if (err) {
  883. lbs_pr_err("can't get host irq line-- request_irq failed\n");
  884. goto terminate_thread;
  885. }
  886. /* poke the IRQ handler so that we don't miss the first interrupt */
  887. up(&card->spi_ready);
  888. /* Start the card.
  889. * This will call register_netdev, and we'll start
  890. * getting interrupts... */
  891. err = lbs_start_card(priv);
  892. if (err)
  893. goto release_irq;
  894. lbs_deb_spi("Finished initializing WLAN module.\n");
  895. /* successful exit */
  896. goto out;
  897. release_irq:
  898. free_irq(spi->irq, card);
  899. terminate_thread:
  900. if_spi_terminate_spi_thread(card);
  901. remove_card:
  902. lbs_remove_card(priv); /* will call free_netdev */
  903. free_card:
  904. free_if_spi_card(card);
  905. out:
  906. if (helper)
  907. release_firmware(helper);
  908. if (mainfw)
  909. release_firmware(mainfw);
  910. lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
  911. return err;
  912. }
  913. static int __devexit libertas_spi_remove(struct spi_device *spi)
  914. {
  915. struct if_spi_card *card = spi_get_drvdata(spi);
  916. struct lbs_private *priv = card->priv;
  917. lbs_deb_spi("libertas_spi_remove\n");
  918. lbs_deb_enter(LBS_DEB_SPI);
  919. lbs_stop_card(priv);
  920. lbs_remove_card(priv); /* will call free_netdev */
  921. priv->surpriseremoved = 1;
  922. free_irq(spi->irq, card);
  923. if_spi_terminate_spi_thread(card);
  924. if (card->pdata->teardown)
  925. card->pdata->teardown(spi);
  926. free_if_spi_card(card);
  927. lbs_deb_leave(LBS_DEB_SPI);
  928. return 0;
  929. }
  930. static struct spi_driver libertas_spi_driver = {
  931. .probe = if_spi_probe,
  932. .remove = __devexit_p(libertas_spi_remove),
  933. .driver = {
  934. .name = "libertas_spi",
  935. .bus = &spi_bus_type,
  936. .owner = THIS_MODULE,
  937. },
  938. };
  939. /*
  940. * Module functions
  941. */
  942. static int __init if_spi_init_module(void)
  943. {
  944. int ret = 0;
  945. lbs_deb_enter(LBS_DEB_SPI);
  946. printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
  947. ret = spi_register_driver(&libertas_spi_driver);
  948. lbs_deb_leave(LBS_DEB_SPI);
  949. return ret;
  950. }
  951. static void __exit if_spi_exit_module(void)
  952. {
  953. lbs_deb_enter(LBS_DEB_SPI);
  954. spi_unregister_driver(&libertas_spi_driver);
  955. lbs_deb_leave(LBS_DEB_SPI);
  956. }
  957. module_init(if_spi_init_module);
  958. module_exit(if_spi_exit_module);
  959. MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
  960. MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
  961. "Colin McCabe <colin@cozybit.com>");
  962. MODULE_LICENSE("GPL");
  963. MODULE_ALIAS("spi:libertas_spi");