if_spi.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. spin_lock_irqsave(&priv->driver_lock, flags);
  718. lbs_queue_event(priv, cause & 0xff);
  719. spin_unlock_irqrestore(&priv->driver_lock, flags);
  720. out:
  721. if (err)
  722. lbs_pr_err("%s: error %d\n", __func__, err);
  723. }
  724. static int lbs_spi_thread(void *data)
  725. {
  726. int err;
  727. struct if_spi_card *card = data;
  728. u16 hiStatus;
  729. unsigned long flags;
  730. struct if_spi_packet *packet;
  731. while (1) {
  732. /* Wait to be woken up by one of two things. First, our ISR
  733. * could tell us that something happened on the WLAN.
  734. * Secondly, libertas could call hw_host_to_card with more
  735. * data, which we might be able to send.
  736. */
  737. do {
  738. err = down_interruptible(&card->spi_ready);
  739. if (!card->run_thread) {
  740. up(&card->spi_thread_terminated);
  741. do_exit(0);
  742. }
  743. } while (err == EINTR);
  744. /* Read the host interrupt status register to see what we
  745. * can do. */
  746. err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  747. &hiStatus);
  748. if (err) {
  749. lbs_pr_err("I/O error\n");
  750. goto err;
  751. }
  752. if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY)
  753. err = if_spi_c2h_cmd(card);
  754. if (err)
  755. goto err;
  756. if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY)
  757. err = if_spi_c2h_data(card);
  758. if (err)
  759. goto err;
  760. if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY) {
  761. /* This means two things. First of all,
  762. * if there was a previous command sent, the card has
  763. * successfully received it.
  764. * Secondly, it is now ready to download another
  765. * command.
  766. */
  767. lbs_host_to_card_done(card->priv);
  768. /* Do we have any command packets from the host to
  769. * send? */
  770. packet = NULL;
  771. spin_lock_irqsave(&card->buffer_lock, flags);
  772. if (!list_empty(&card->cmd_packet_list)) {
  773. packet = (struct if_spi_packet *)(card->
  774. cmd_packet_list.next);
  775. list_del(&packet->list);
  776. }
  777. spin_unlock_irqrestore(&card->buffer_lock, flags);
  778. if (packet)
  779. if_spi_h2c(card, packet, MVMS_CMD);
  780. }
  781. if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
  782. /* Do we have any data packets from the host to
  783. * send? */
  784. packet = NULL;
  785. spin_lock_irqsave(&card->buffer_lock, flags);
  786. if (!list_empty(&card->data_packet_list)) {
  787. packet = (struct if_spi_packet *)(card->
  788. data_packet_list.next);
  789. list_del(&packet->list);
  790. }
  791. spin_unlock_irqrestore(&card->buffer_lock, flags);
  792. if (packet)
  793. if_spi_h2c(card, packet, MVMS_DAT);
  794. }
  795. if (hiStatus & IF_SPI_HIST_CARD_EVENT)
  796. if_spi_e2h(card);
  797. err:
  798. if (err)
  799. lbs_pr_err("%s: got error %d\n", __func__, err);
  800. }
  801. }
  802. /* Block until lbs_spi_thread thread has terminated */
  803. static void if_spi_terminate_spi_thread(struct if_spi_card *card)
  804. {
  805. /* It would be nice to use kthread_stop here, but that function
  806. * can't wake threads waiting for a semaphore. */
  807. card->run_thread = 0;
  808. up(&card->spi_ready);
  809. down(&card->spi_thread_terminated);
  810. }
  811. /*
  812. * Host to Card
  813. *
  814. * Called from Libertas to transfer some data to the WLAN device
  815. * We can't sleep here. */
  816. static int if_spi_host_to_card(struct lbs_private *priv,
  817. u8 type, u8 *buf, u16 nb)
  818. {
  819. int err = 0;
  820. unsigned long flags;
  821. struct if_spi_card *card = priv->card;
  822. struct if_spi_packet *packet;
  823. u16 blen;
  824. lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
  825. if (nb == 0) {
  826. lbs_pr_err("%s: invalid size requested: %d\n", __func__, nb);
  827. err = -EINVAL;
  828. goto out;
  829. }
  830. blen = ALIGN(nb, 4);
  831. packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
  832. if (!packet) {
  833. err = -ENOMEM;
  834. goto out;
  835. }
  836. packet->blen = blen;
  837. memcpy(packet->buffer, buf, nb);
  838. memset(packet->buffer + nb, 0, blen - nb);
  839. switch (type) {
  840. case MVMS_CMD:
  841. priv->dnld_sent = DNLD_CMD_SENT;
  842. spin_lock_irqsave(&card->buffer_lock, flags);
  843. list_add_tail(&packet->list, &card->cmd_packet_list);
  844. spin_unlock_irqrestore(&card->buffer_lock, flags);
  845. break;
  846. case MVMS_DAT:
  847. priv->dnld_sent = DNLD_DATA_SENT;
  848. spin_lock_irqsave(&card->buffer_lock, flags);
  849. list_add_tail(&packet->list, &card->data_packet_list);
  850. spin_unlock_irqrestore(&card->buffer_lock, flags);
  851. break;
  852. default:
  853. lbs_pr_err("can't transfer buffer of type %d", type);
  854. err = -EINVAL;
  855. break;
  856. }
  857. /* Wake up the spi thread */
  858. up(&card->spi_ready);
  859. out:
  860. lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
  861. return err;
  862. }
  863. /*
  864. * Host Interrupts
  865. *
  866. * Service incoming interrupts from the WLAN device. We can't sleep here, so
  867. * don't try to talk on the SPI bus, just wake up the SPI thread.
  868. */
  869. static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
  870. {
  871. struct if_spi_card *card = dev_id;
  872. up(&card->spi_ready);
  873. return IRQ_HANDLED;
  874. }
  875. /*
  876. * SPI callbacks
  877. */
  878. static int if_spi_calculate_fw_names(u16 card_id,
  879. char *helper_fw, char *main_fw)
  880. {
  881. int i;
  882. for (i = 0; i < ARRAY_SIZE(chip_id_to_device_name); ++i) {
  883. if (card_id == chip_id_to_device_name[i].chip_id)
  884. break;
  885. }
  886. if (i == ARRAY_SIZE(chip_id_to_device_name)) {
  887. lbs_pr_err("Unsupported chip_id: 0x%02x\n", card_id);
  888. return -EAFNOSUPPORT;
  889. }
  890. snprintf(helper_fw, FIRMWARE_NAME_MAX, "libertas/gspi%d_hlp.bin",
  891. chip_id_to_device_name[i].name);
  892. snprintf(main_fw, FIRMWARE_NAME_MAX, "libertas/gspi%d.bin",
  893. chip_id_to_device_name[i].name);
  894. return 0;
  895. }
  896. static int __devinit if_spi_probe(struct spi_device *spi)
  897. {
  898. struct if_spi_card *card;
  899. struct lbs_private *priv = NULL;
  900. struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
  901. int err = 0;
  902. u32 scratch;
  903. struct sched_param param = { .sched_priority = 1 };
  904. lbs_deb_enter(LBS_DEB_SPI);
  905. if (!pdata) {
  906. err = -EINVAL;
  907. goto out;
  908. }
  909. if (pdata->setup) {
  910. err = pdata->setup(spi);
  911. if (err)
  912. goto out;
  913. }
  914. /* Allocate card structure to represent this specific device */
  915. card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
  916. if (!card) {
  917. err = -ENOMEM;
  918. goto out;
  919. }
  920. spi_set_drvdata(spi, card);
  921. card->pdata = pdata;
  922. card->spi = spi;
  923. card->gpio_cs = pdata->gpio_cs;
  924. card->prev_xfer_time = jiffies;
  925. sema_init(&card->spi_ready, 0);
  926. sema_init(&card->spi_thread_terminated, 0);
  927. INIT_LIST_HEAD(&card->cmd_packet_list);
  928. INIT_LIST_HEAD(&card->data_packet_list);
  929. spin_lock_init(&card->buffer_lock);
  930. /* set up GPIO CS line. TODO: use regular CS line */
  931. err = gpio_request(card->gpio_cs, "if_spi_gpio_chip_select");
  932. if (err)
  933. goto free_card;
  934. err = gpio_direction_output(card->gpio_cs, 1);
  935. if (err)
  936. goto free_gpio;
  937. /* Initialize the SPI Interface Unit */
  938. err = spu_init(card, pdata->use_dummy_writes);
  939. if (err)
  940. goto free_gpio;
  941. err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
  942. if (err)
  943. goto free_gpio;
  944. /* Firmware load */
  945. err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
  946. if (err)
  947. goto free_gpio;
  948. if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
  949. lbs_deb_spi("Firmware is already loaded for "
  950. "Marvell WLAN 802.11 adapter\n");
  951. else {
  952. err = if_spi_calculate_fw_names(card->card_id,
  953. card->helper_fw_name, card->main_fw_name);
  954. if (err)
  955. goto free_gpio;
  956. lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
  957. "(chip_id = 0x%04x, chip_rev = 0x%02x) "
  958. "attached to SPI bus_num %d, chip_select %d. "
  959. "spi->max_speed_hz=%d\n",
  960. card->card_id, card->card_rev,
  961. spi->master->bus_num, spi->chip_select,
  962. spi->max_speed_hz);
  963. err = if_spi_prog_helper_firmware(card);
  964. if (err)
  965. goto free_gpio;
  966. err = if_spi_prog_main_firmware(card);
  967. if (err)
  968. goto free_gpio;
  969. lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
  970. }
  971. err = spu_set_interrupt_mode(card, 0, 1);
  972. if (err)
  973. goto free_gpio;
  974. /* Register our card with libertas.
  975. * This will call alloc_etherdev */
  976. priv = lbs_add_card(card, &spi->dev);
  977. if (!priv) {
  978. err = -ENOMEM;
  979. goto free_gpio;
  980. }
  981. card->priv = priv;
  982. priv->card = card;
  983. priv->hw_host_to_card = if_spi_host_to_card;
  984. priv->fw_ready = 1;
  985. priv->ps_supported = 1;
  986. /* Initialize interrupt handling stuff. */
  987. card->run_thread = 1;
  988. card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
  989. if (IS_ERR(card->spi_thread)) {
  990. card->run_thread = 0;
  991. err = PTR_ERR(card->spi_thread);
  992. lbs_pr_err("error creating SPI thread: err=%d\n", err);
  993. goto remove_card;
  994. }
  995. if (sched_setscheduler(card->spi_thread, SCHED_FIFO, &param))
  996. lbs_pr_err("Error setting scheduler, using default.\n");
  997. err = request_irq(spi->irq, if_spi_host_interrupt,
  998. IRQF_TRIGGER_FALLING, "libertas_spi", card);
  999. if (err) {
  1000. lbs_pr_err("can't get host irq line-- request_irq failed\n");
  1001. goto terminate_thread;
  1002. }
  1003. /* Start the card.
  1004. * This will call register_netdev, and we'll start
  1005. * getting interrupts... */
  1006. err = lbs_start_card(priv);
  1007. if (err)
  1008. goto release_irq;
  1009. lbs_deb_spi("Finished initializing WLAN module.\n");
  1010. /* successful exit */
  1011. goto out;
  1012. release_irq:
  1013. free_irq(spi->irq, card);
  1014. terminate_thread:
  1015. if_spi_terminate_spi_thread(card);
  1016. remove_card:
  1017. lbs_remove_card(priv); /* will call free_netdev */
  1018. free_gpio:
  1019. gpio_free(card->gpio_cs);
  1020. free_card:
  1021. free_if_spi_card(card);
  1022. out:
  1023. lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
  1024. return err;
  1025. }
  1026. static int __devexit libertas_spi_remove(struct spi_device *spi)
  1027. {
  1028. struct if_spi_card *card = spi_get_drvdata(spi);
  1029. struct lbs_private *priv = card->priv;
  1030. lbs_deb_spi("libertas_spi_remove\n");
  1031. lbs_deb_enter(LBS_DEB_SPI);
  1032. priv->surpriseremoved = 1;
  1033. lbs_stop_card(priv);
  1034. free_irq(spi->irq, card);
  1035. if_spi_terminate_spi_thread(card);
  1036. lbs_remove_card(priv); /* will call free_netdev */
  1037. gpio_free(card->gpio_cs);
  1038. if (card->pdata->teardown)
  1039. card->pdata->teardown(spi);
  1040. free_if_spi_card(card);
  1041. lbs_deb_leave(LBS_DEB_SPI);
  1042. return 0;
  1043. }
  1044. static struct spi_driver libertas_spi_driver = {
  1045. .probe = if_spi_probe,
  1046. .remove = __devexit_p(libertas_spi_remove),
  1047. .driver = {
  1048. .name = "libertas_spi",
  1049. .bus = &spi_bus_type,
  1050. .owner = THIS_MODULE,
  1051. },
  1052. };
  1053. /*
  1054. * Module functions
  1055. */
  1056. static int __init if_spi_init_module(void)
  1057. {
  1058. int ret = 0;
  1059. lbs_deb_enter(LBS_DEB_SPI);
  1060. printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
  1061. ret = spi_register_driver(&libertas_spi_driver);
  1062. lbs_deb_leave(LBS_DEB_SPI);
  1063. return ret;
  1064. }
  1065. static void __exit if_spi_exit_module(void)
  1066. {
  1067. lbs_deb_enter(LBS_DEB_SPI);
  1068. spi_unregister_driver(&libertas_spi_driver);
  1069. lbs_deb_leave(LBS_DEB_SPI);
  1070. }
  1071. module_init(if_spi_init_module);
  1072. module_exit(if_spi_exit_module);
  1073. MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
  1074. MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
  1075. "Colin McCabe <colin@cozybit.com>");
  1076. MODULE_LICENSE("GPL");