spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/irq.h>
  24. #include <linux/module.h>
  25. #include <linux/crc7.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/wl12xx.h>
  28. #include <linux/slab.h>
  29. #include "wl12xx.h"
  30. #include "wl12xx_80211.h"
  31. #include "io.h"
  32. #include "reg.h"
  33. #define WSPI_CMD_READ 0x40000000
  34. #define WSPI_CMD_WRITE 0x00000000
  35. #define WSPI_CMD_FIXED 0x20000000
  36. #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
  37. #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
  38. #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
  39. #define WSPI_INIT_CMD_CRC_LEN 5
  40. #define WSPI_INIT_CMD_START 0x00
  41. #define WSPI_INIT_CMD_TX 0x40
  42. /* the extra bypass bit is sampled by the TNET as '1' */
  43. #define WSPI_INIT_CMD_BYPASS_BIT 0x80
  44. #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
  45. #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
  46. #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
  47. #define WSPI_INIT_CMD_IOD 0x40
  48. #define WSPI_INIT_CMD_IP 0x20
  49. #define WSPI_INIT_CMD_CS 0x10
  50. #define WSPI_INIT_CMD_WS 0x08
  51. #define WSPI_INIT_CMD_WSPI 0x01
  52. #define WSPI_INIT_CMD_END 0x01
  53. #define WSPI_INIT_CMD_LEN 8
  54. #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
  55. ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
  56. #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
  57. /* HW limitation: maximum possible chunk size is 4095 bytes */
  58. #define WSPI_MAX_CHUNK_SIZE 4092
  59. #define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE)
  60. static inline struct spi_device *wl_to_spi(struct wl1271 *wl)
  61. {
  62. return wl->if_priv;
  63. }
  64. static struct device *wl1271_spi_wl_to_dev(struct wl1271 *wl)
  65. {
  66. return &(wl_to_spi(wl)->dev);
  67. }
  68. static void wl1271_spi_disable_interrupts(struct wl1271 *wl)
  69. {
  70. disable_irq(wl->irq);
  71. }
  72. static void wl1271_spi_enable_interrupts(struct wl1271 *wl)
  73. {
  74. enable_irq(wl->irq);
  75. }
  76. static void wl1271_spi_reset(struct wl1271 *wl)
  77. {
  78. u8 *cmd;
  79. struct spi_transfer t;
  80. struct spi_message m;
  81. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  82. if (!cmd) {
  83. wl1271_error("could not allocate cmd for spi reset");
  84. return;
  85. }
  86. memset(&t, 0, sizeof(t));
  87. spi_message_init(&m);
  88. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  89. t.tx_buf = cmd;
  90. t.len = WSPI_INIT_CMD_LEN;
  91. spi_message_add_tail(&t, &m);
  92. spi_sync(wl_to_spi(wl), &m);
  93. wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  94. kfree(cmd);
  95. }
  96. static void wl1271_spi_init(struct wl1271 *wl)
  97. {
  98. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  99. struct spi_transfer t;
  100. struct spi_message m;
  101. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  102. if (!cmd) {
  103. wl1271_error("could not allocate cmd for spi init");
  104. return;
  105. }
  106. memset(crc, 0, sizeof(crc));
  107. memset(&t, 0, sizeof(t));
  108. spi_message_init(&m);
  109. /*
  110. * Set WSPI_INIT_COMMAND
  111. * the data is being send from the MSB to LSB
  112. */
  113. cmd[2] = 0xff;
  114. cmd[3] = 0xff;
  115. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  116. cmd[0] = 0;
  117. cmd[7] = 0;
  118. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  119. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  120. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  121. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  122. else
  123. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  124. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  125. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  126. crc[0] = cmd[1];
  127. crc[1] = cmd[0];
  128. crc[2] = cmd[7];
  129. crc[3] = cmd[6];
  130. crc[4] = cmd[5];
  131. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  132. cmd[4] |= WSPI_INIT_CMD_END;
  133. t.tx_buf = cmd;
  134. t.len = WSPI_INIT_CMD_LEN;
  135. spi_message_add_tail(&t, &m);
  136. spi_sync(wl_to_spi(wl), &m);
  137. wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  138. kfree(cmd);
  139. }
  140. #define WL1271_BUSY_WORD_TIMEOUT 1000
  141. static int wl1271_spi_read_busy(struct wl1271 *wl)
  142. {
  143. struct spi_transfer t[1];
  144. struct spi_message m;
  145. u32 *busy_buf;
  146. int num_busy_bytes = 0;
  147. /*
  148. * Read further busy words from SPI until a non-busy word is
  149. * encountered, then read the data itself into the buffer.
  150. */
  151. num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
  152. busy_buf = wl->buffer_busyword;
  153. while (num_busy_bytes) {
  154. num_busy_bytes--;
  155. spi_message_init(&m);
  156. memset(t, 0, sizeof(t));
  157. t[0].rx_buf = busy_buf;
  158. t[0].len = sizeof(u32);
  159. t[0].cs_change = true;
  160. spi_message_add_tail(&t[0], &m);
  161. spi_sync(wl_to_spi(wl), &m);
  162. if (*busy_buf & 0x1)
  163. return 0;
  164. }
  165. /* The SPI bus is unresponsive, the read failed. */
  166. wl1271_error("SPI read busy-word timeout!\n");
  167. return -ETIMEDOUT;
  168. }
  169. static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
  170. size_t len, bool fixed)
  171. {
  172. struct spi_transfer t[2];
  173. struct spi_message m;
  174. u32 *busy_buf;
  175. u32 *cmd;
  176. u32 chunk_len;
  177. while (len > 0) {
  178. chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
  179. cmd = &wl->buffer_cmd;
  180. busy_buf = wl->buffer_busyword;
  181. *cmd = 0;
  182. *cmd |= WSPI_CMD_READ;
  183. *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
  184. WSPI_CMD_BYTE_LENGTH;
  185. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  186. if (fixed)
  187. *cmd |= WSPI_CMD_FIXED;
  188. spi_message_init(&m);
  189. memset(t, 0, sizeof(t));
  190. t[0].tx_buf = cmd;
  191. t[0].len = 4;
  192. t[0].cs_change = true;
  193. spi_message_add_tail(&t[0], &m);
  194. /* Busy and non busy words read */
  195. t[1].rx_buf = busy_buf;
  196. t[1].len = WL1271_BUSY_WORD_LEN;
  197. t[1].cs_change = true;
  198. spi_message_add_tail(&t[1], &m);
  199. spi_sync(wl_to_spi(wl), &m);
  200. if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
  201. wl1271_spi_read_busy(wl)) {
  202. memset(buf, 0, chunk_len);
  203. return;
  204. }
  205. spi_message_init(&m);
  206. memset(t, 0, sizeof(t));
  207. t[0].rx_buf = buf;
  208. t[0].len = chunk_len;
  209. t[0].cs_change = true;
  210. spi_message_add_tail(&t[0], &m);
  211. spi_sync(wl_to_spi(wl), &m);
  212. wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  213. wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, chunk_len);
  214. if (!fixed)
  215. addr += chunk_len;
  216. buf += chunk_len;
  217. len -= chunk_len;
  218. }
  219. }
  220. static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
  221. size_t len, bool fixed)
  222. {
  223. struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS];
  224. struct spi_message m;
  225. u32 commands[WSPI_MAX_NUM_OF_CHUNKS];
  226. u32 *cmd;
  227. u32 chunk_len;
  228. int i;
  229. WARN_ON(len > WL1271_AGGR_BUFFER_SIZE);
  230. spi_message_init(&m);
  231. memset(t, 0, sizeof(t));
  232. cmd = &commands[0];
  233. i = 0;
  234. while (len > 0) {
  235. chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
  236. *cmd = 0;
  237. *cmd |= WSPI_CMD_WRITE;
  238. *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
  239. WSPI_CMD_BYTE_LENGTH;
  240. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  241. if (fixed)
  242. *cmd |= WSPI_CMD_FIXED;
  243. t[i].tx_buf = cmd;
  244. t[i].len = sizeof(*cmd);
  245. spi_message_add_tail(&t[i++], &m);
  246. t[i].tx_buf = buf;
  247. t[i].len = chunk_len;
  248. spi_message_add_tail(&t[i++], &m);
  249. wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  250. wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, chunk_len);
  251. if (!fixed)
  252. addr += chunk_len;
  253. buf += chunk_len;
  254. len -= chunk_len;
  255. cmd++;
  256. }
  257. spi_sync(wl_to_spi(wl), &m);
  258. }
  259. static irqreturn_t wl1271_hardirq(int irq, void *cookie)
  260. {
  261. struct wl1271 *wl = cookie;
  262. unsigned long flags;
  263. wl1271_debug(DEBUG_IRQ, "IRQ");
  264. /* complete the ELP completion */
  265. spin_lock_irqsave(&wl->wl_lock, flags);
  266. set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
  267. if (wl->elp_compl) {
  268. complete(wl->elp_compl);
  269. wl->elp_compl = NULL;
  270. }
  271. spin_unlock_irqrestore(&wl->wl_lock, flags);
  272. return IRQ_WAKE_THREAD;
  273. }
  274. static int wl1271_spi_set_power(struct wl1271 *wl, bool enable)
  275. {
  276. if (wl->set_power)
  277. wl->set_power(enable);
  278. return 0;
  279. }
  280. static struct wl1271_if_operations spi_ops = {
  281. .read = wl1271_spi_raw_read,
  282. .write = wl1271_spi_raw_write,
  283. .reset = wl1271_spi_reset,
  284. .init = wl1271_spi_init,
  285. .power = wl1271_spi_set_power,
  286. .dev = wl1271_spi_wl_to_dev,
  287. .enable_irq = wl1271_spi_enable_interrupts,
  288. .disable_irq = wl1271_spi_disable_interrupts,
  289. .set_block_size = NULL,
  290. };
  291. static int __devinit wl1271_probe(struct spi_device *spi)
  292. {
  293. struct wl12xx_platform_data *pdata;
  294. struct ieee80211_hw *hw;
  295. struct wl1271 *wl;
  296. unsigned long irqflags;
  297. int ret;
  298. pdata = spi->dev.platform_data;
  299. if (!pdata) {
  300. wl1271_error("no platform data");
  301. return -ENODEV;
  302. }
  303. hw = wl1271_alloc_hw();
  304. if (IS_ERR(hw))
  305. return PTR_ERR(hw);
  306. wl = hw->priv;
  307. dev_set_drvdata(&spi->dev, wl);
  308. wl->if_priv = spi;
  309. wl->if_ops = &spi_ops;
  310. /* This is the only SPI value that we need to set here, the rest
  311. * comes from the board-peripherals file */
  312. spi->bits_per_word = 32;
  313. ret = spi_setup(spi);
  314. if (ret < 0) {
  315. wl1271_error("spi_setup failed");
  316. goto out_free;
  317. }
  318. wl->set_power = pdata->set_power;
  319. if (!wl->set_power) {
  320. wl1271_error("set power function missing in platform data");
  321. ret = -ENODEV;
  322. goto out_free;
  323. }
  324. wl->ref_clock = pdata->board_ref_clock;
  325. wl->tcxo_clock = pdata->board_tcxo_clock;
  326. wl->platform_quirks = pdata->platform_quirks;
  327. if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
  328. irqflags = IRQF_TRIGGER_RISING;
  329. else
  330. irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
  331. wl->irq = spi->irq;
  332. if (wl->irq < 0) {
  333. wl1271_error("irq missing in platform data");
  334. ret = -ENODEV;
  335. goto out_free;
  336. }
  337. ret = request_threaded_irq(wl->irq, wl1271_hardirq, wl1271_irq,
  338. irqflags,
  339. DRIVER_NAME, wl);
  340. if (ret < 0) {
  341. wl1271_error("request_irq() failed: %d", ret);
  342. goto out_free;
  343. }
  344. disable_irq(wl->irq);
  345. ret = wl1271_init_ieee80211(wl);
  346. if (ret)
  347. goto out_irq;
  348. ret = wl1271_register_hw(wl);
  349. if (ret)
  350. goto out_irq;
  351. wl1271_notice("initialized");
  352. return 0;
  353. out_irq:
  354. free_irq(wl->irq, wl);
  355. out_free:
  356. wl1271_free_hw(wl);
  357. return ret;
  358. }
  359. static int __devexit wl1271_remove(struct spi_device *spi)
  360. {
  361. struct wl1271 *wl = dev_get_drvdata(&spi->dev);
  362. wl1271_unregister_hw(wl);
  363. free_irq(wl->irq, wl);
  364. wl1271_free_hw(wl);
  365. return 0;
  366. }
  367. static struct spi_driver wl1271_spi_driver = {
  368. .driver = {
  369. .name = "wl1271_spi",
  370. .bus = &spi_bus_type,
  371. .owner = THIS_MODULE,
  372. },
  373. .probe = wl1271_probe,
  374. .remove = __devexit_p(wl1271_remove),
  375. };
  376. static int __init wl1271_init(void)
  377. {
  378. int ret;
  379. ret = spi_register_driver(&wl1271_spi_driver);
  380. if (ret < 0) {
  381. wl1271_error("failed to register spi driver: %d", ret);
  382. goto out;
  383. }
  384. out:
  385. return ret;
  386. }
  387. static void __exit wl1271_exit(void)
  388. {
  389. spi_unregister_driver(&wl1271_spi_driver);
  390. wl1271_notice("unloaded");
  391. }
  392. module_init(wl1271_init);
  393. module_exit(wl1271_exit);
  394. MODULE_LICENSE("GPL");
  395. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  396. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
  397. MODULE_FIRMWARE(WL1271_FW_NAME);
  398. MODULE_FIRMWARE(WL128X_FW_NAME);
  399. MODULE_FIRMWARE(WL127X_AP_FW_NAME);
  400. MODULE_FIRMWARE(WL128X_AP_FW_NAME);
  401. MODULE_ALIAS("spi:wl1271");