if_spi.c 30 KB

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