if_spi.c 31 KB

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