wl1271_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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/spi/wl12xx.h>
  28. #include "wl1271.h"
  29. #include "wl12xx_80211.h"
  30. #include "wl1271_io.h"
  31. #include "wl1271_reg.h"
  32. #define WSPI_CMD_READ 0x40000000
  33. #define WSPI_CMD_WRITE 0x00000000
  34. #define WSPI_CMD_FIXED 0x20000000
  35. #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
  36. #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
  37. #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
  38. #define WSPI_INIT_CMD_CRC_LEN 5
  39. #define WSPI_INIT_CMD_START 0x00
  40. #define WSPI_INIT_CMD_TX 0x40
  41. /* the extra bypass bit is sampled by the TNET as '1' */
  42. #define WSPI_INIT_CMD_BYPASS_BIT 0x80
  43. #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
  44. #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
  45. #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
  46. #define WSPI_INIT_CMD_IOD 0x40
  47. #define WSPI_INIT_CMD_IP 0x20
  48. #define WSPI_INIT_CMD_CS 0x10
  49. #define WSPI_INIT_CMD_WS 0x08
  50. #define WSPI_INIT_CMD_WSPI 0x01
  51. #define WSPI_INIT_CMD_END 0x01
  52. #define WSPI_INIT_CMD_LEN 8
  53. #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
  54. ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
  55. #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
  56. static inline struct spi_device *wl_to_spi(struct wl1271 *wl)
  57. {
  58. return wl->if_priv;
  59. }
  60. static struct device *wl1271_spi_wl_to_dev(struct wl1271 *wl)
  61. {
  62. return &(wl_to_spi(wl)->dev);
  63. }
  64. static void wl1271_spi_disable_interrupts(struct wl1271 *wl)
  65. {
  66. disable_irq(wl->irq);
  67. }
  68. static void wl1271_spi_enable_interrupts(struct wl1271 *wl)
  69. {
  70. enable_irq(wl->irq);
  71. }
  72. static void wl1271_spi_reset(struct wl1271 *wl)
  73. {
  74. u8 *cmd;
  75. struct spi_transfer t;
  76. struct spi_message m;
  77. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  78. if (!cmd) {
  79. wl1271_error("could not allocate cmd for spi reset");
  80. return;
  81. }
  82. memset(&t, 0, sizeof(t));
  83. spi_message_init(&m);
  84. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  85. t.tx_buf = cmd;
  86. t.len = WSPI_INIT_CMD_LEN;
  87. spi_message_add_tail(&t, &m);
  88. spi_sync(wl_to_spi(wl), &m);
  89. wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  90. }
  91. static void wl1271_spi_init(struct wl1271 *wl)
  92. {
  93. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  94. struct spi_transfer t;
  95. struct spi_message m;
  96. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  97. if (!cmd) {
  98. wl1271_error("could not allocate cmd for spi init");
  99. return;
  100. }
  101. memset(crc, 0, sizeof(crc));
  102. memset(&t, 0, sizeof(t));
  103. spi_message_init(&m);
  104. /*
  105. * Set WSPI_INIT_COMMAND
  106. * the data is being send from the MSB to LSB
  107. */
  108. cmd[2] = 0xff;
  109. cmd[3] = 0xff;
  110. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  111. cmd[0] = 0;
  112. cmd[7] = 0;
  113. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  114. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  115. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  116. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  117. else
  118. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  119. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  120. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  121. crc[0] = cmd[1];
  122. crc[1] = cmd[0];
  123. crc[2] = cmd[7];
  124. crc[3] = cmd[6];
  125. crc[4] = cmd[5];
  126. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  127. cmd[4] |= WSPI_INIT_CMD_END;
  128. t.tx_buf = cmd;
  129. t.len = WSPI_INIT_CMD_LEN;
  130. spi_message_add_tail(&t, &m);
  131. spi_sync(wl_to_spi(wl), &m);
  132. wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  133. }
  134. #define WL1271_BUSY_WORD_TIMEOUT 1000
  135. /* FIXME: Check busy words, removed due to SPI bug */
  136. #if 0
  137. static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
  138. {
  139. struct spi_transfer t[1];
  140. struct spi_message m;
  141. u32 *busy_buf;
  142. int num_busy_bytes = 0;
  143. wl1271_info("spi read BUSY!");
  144. /*
  145. * Look for the non-busy word in the read buffer, and if found,
  146. * read in the remaining data into the buffer.
  147. */
  148. busy_buf = (u32 *)buf;
  149. for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
  150. num_busy_bytes += sizeof(u32);
  151. if (*busy_buf & 0x1) {
  152. spi_message_init(&m);
  153. memset(t, 0, sizeof(t));
  154. memmove(buf, busy_buf, len - num_busy_bytes);
  155. t[0].rx_buf = buf + (len - num_busy_bytes);
  156. t[0].len = num_busy_bytes;
  157. spi_message_add_tail(&t[0], &m);
  158. spi_sync(wl_to_spi(wl), &m);
  159. return;
  160. }
  161. }
  162. /*
  163. * Read further busy words from SPI until a non-busy word is
  164. * encountered, then read the data itself into the buffer.
  165. */
  166. wl1271_info("spi read BUSY-polling needed!");
  167. num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
  168. busy_buf = wl->buffer_busyword;
  169. while (num_busy_bytes) {
  170. num_busy_bytes--;
  171. spi_message_init(&m);
  172. memset(t, 0, sizeof(t));
  173. t[0].rx_buf = busy_buf;
  174. t[0].len = sizeof(u32);
  175. spi_message_add_tail(&t[0], &m);
  176. spi_sync(wl_to_spi(wl), &m);
  177. if (*busy_buf & 0x1) {
  178. spi_message_init(&m);
  179. memset(t, 0, sizeof(t));
  180. t[0].rx_buf = buf;
  181. t[0].len = len;
  182. spi_message_add_tail(&t[0], &m);
  183. spi_sync(wl_to_spi(wl), &m);
  184. return;
  185. }
  186. }
  187. /* The SPI bus is unresponsive, the read failed. */
  188. memset(buf, 0, len);
  189. wl1271_error("SPI read busy-word timeout!\n");
  190. }
  191. #endif
  192. static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
  193. size_t len, bool fixed)
  194. {
  195. struct spi_transfer t[3];
  196. struct spi_message m;
  197. u32 *busy_buf;
  198. u32 *cmd;
  199. cmd = &wl->buffer_cmd;
  200. busy_buf = wl->buffer_busyword;
  201. *cmd = 0;
  202. *cmd |= WSPI_CMD_READ;
  203. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  204. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  205. if (fixed)
  206. *cmd |= WSPI_CMD_FIXED;
  207. spi_message_init(&m);
  208. memset(t, 0, sizeof(t));
  209. t[0].tx_buf = cmd;
  210. t[0].len = 4;
  211. spi_message_add_tail(&t[0], &m);
  212. /* Busy and non busy words read */
  213. t[1].rx_buf = busy_buf;
  214. t[1].len = WL1271_BUSY_WORD_LEN;
  215. spi_message_add_tail(&t[1], &m);
  216. t[2].rx_buf = buf;
  217. t[2].len = len;
  218. spi_message_add_tail(&t[2], &m);
  219. spi_sync(wl_to_spi(wl), &m);
  220. /* FIXME: Check busy words, removed due to SPI bug */
  221. /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
  222. wl1271_spi_read_busy(wl, buf, len); */
  223. wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  224. wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  225. }
  226. static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
  227. size_t len, bool fixed)
  228. {
  229. struct spi_transfer t[2];
  230. struct spi_message m;
  231. u32 *cmd;
  232. cmd = &wl->buffer_cmd;
  233. *cmd = 0;
  234. *cmd |= WSPI_CMD_WRITE;
  235. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  236. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  237. if (fixed)
  238. *cmd |= WSPI_CMD_FIXED;
  239. spi_message_init(&m);
  240. memset(t, 0, sizeof(t));
  241. t[0].tx_buf = cmd;
  242. t[0].len = sizeof(*cmd);
  243. spi_message_add_tail(&t[0], &m);
  244. t[1].tx_buf = buf;
  245. t[1].len = len;
  246. spi_message_add_tail(&t[1], &m);
  247. spi_sync(wl_to_spi(wl), &m);
  248. wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  249. wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  250. }
  251. static irqreturn_t wl1271_irq(int irq, void *cookie)
  252. {
  253. struct wl1271 *wl;
  254. unsigned long flags;
  255. wl1271_debug(DEBUG_IRQ, "IRQ");
  256. wl = cookie;
  257. /* complete the ELP completion */
  258. spin_lock_irqsave(&wl->wl_lock, flags);
  259. if (wl->elp_compl) {
  260. complete(wl->elp_compl);
  261. wl->elp_compl = NULL;
  262. }
  263. if (!test_and_set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
  264. ieee80211_queue_work(wl->hw, &wl->irq_work);
  265. set_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags);
  266. spin_unlock_irqrestore(&wl->wl_lock, flags);
  267. return IRQ_HANDLED;
  268. }
  269. static void wl1271_spi_set_power(struct wl1271 *wl, bool enable)
  270. {
  271. if (wl->set_power)
  272. wl->set_power(enable);
  273. }
  274. static struct wl1271_if_operations spi_ops = {
  275. .read = wl1271_spi_raw_read,
  276. .write = wl1271_spi_raw_write,
  277. .reset = wl1271_spi_reset,
  278. .init = wl1271_spi_init,
  279. .power = wl1271_spi_set_power,
  280. .dev = wl1271_spi_wl_to_dev,
  281. .enable_irq = wl1271_spi_enable_interrupts,
  282. .disable_irq = wl1271_spi_disable_interrupts
  283. };
  284. static int __devinit wl1271_probe(struct spi_device *spi)
  285. {
  286. struct wl12xx_platform_data *pdata;
  287. struct ieee80211_hw *hw;
  288. struct wl1271 *wl;
  289. int ret;
  290. pdata = spi->dev.platform_data;
  291. if (!pdata) {
  292. wl1271_error("no platform data");
  293. return -ENODEV;
  294. }
  295. hw = wl1271_alloc_hw();
  296. if (IS_ERR(hw))
  297. return PTR_ERR(hw);
  298. wl = hw->priv;
  299. dev_set_drvdata(&spi->dev, wl);
  300. wl->if_priv = spi;
  301. wl->if_ops = &spi_ops;
  302. /* This is the only SPI value that we need to set here, the rest
  303. * comes from the board-peripherals file */
  304. spi->bits_per_word = 32;
  305. ret = spi_setup(spi);
  306. if (ret < 0) {
  307. wl1271_error("spi_setup failed");
  308. goto out_free;
  309. }
  310. wl->set_power = pdata->set_power;
  311. if (!wl->set_power) {
  312. wl1271_error("set power function missing in platform data");
  313. ret = -ENODEV;
  314. goto out_free;
  315. }
  316. wl->irq = spi->irq;
  317. if (wl->irq < 0) {
  318. wl1271_error("irq missing in platform data");
  319. ret = -ENODEV;
  320. goto out_free;
  321. }
  322. ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
  323. if (ret < 0) {
  324. wl1271_error("request_irq() failed: %d", ret);
  325. goto out_free;
  326. }
  327. set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  328. disable_irq(wl->irq);
  329. ret = wl1271_init_ieee80211(wl);
  330. if (ret)
  331. goto out_irq;
  332. ret = wl1271_register_hw(wl);
  333. if (ret)
  334. goto out_irq;
  335. wl1271_notice("initialized");
  336. return 0;
  337. out_irq:
  338. free_irq(wl->irq, wl);
  339. out_free:
  340. wl1271_free_hw(wl);
  341. return ret;
  342. }
  343. static int __devexit wl1271_remove(struct spi_device *spi)
  344. {
  345. struct wl1271 *wl = dev_get_drvdata(&spi->dev);
  346. free_irq(wl->irq, wl);
  347. wl1271_unregister_hw(wl);
  348. wl1271_free_hw(wl);
  349. return 0;
  350. }
  351. static struct spi_driver wl1271_spi_driver = {
  352. .driver = {
  353. .name = "wl1271_spi",
  354. .bus = &spi_bus_type,
  355. .owner = THIS_MODULE,
  356. },
  357. .probe = wl1271_probe,
  358. .remove = __devexit_p(wl1271_remove),
  359. };
  360. static int __init wl1271_init(void)
  361. {
  362. int ret;
  363. ret = spi_register_driver(&wl1271_spi_driver);
  364. if (ret < 0) {
  365. wl1271_error("failed to register spi driver: %d", ret);
  366. goto out;
  367. }
  368. out:
  369. return ret;
  370. }
  371. static void __exit wl1271_exit(void)
  372. {
  373. spi_unregister_driver(&wl1271_spi_driver);
  374. wl1271_notice("unloaded");
  375. }
  376. module_init(wl1271_init);
  377. module_exit(wl1271_exit);
  378. MODULE_LICENSE("GPL");
  379. MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
  380. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
  381. MODULE_FIRMWARE(WL1271_FW_NAME);