if_spi.c 31 KB

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