if_spi.c 31 KB

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