if_spi.c 30 KB

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