wl1271_spi.c 11 KB

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