if_spi.c 31 KB

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