spi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/crc7.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/wl12xx.h>
  28. #include "wl1251.h"
  29. #include "reg.h"
  30. #include "spi.h"
  31. static irqreturn_t wl1251_irq(int irq, void *cookie)
  32. {
  33. struct wl1251 *wl;
  34. wl1251_debug(DEBUG_IRQ, "IRQ");
  35. wl = cookie;
  36. ieee80211_queue_work(wl->hw, &wl->irq_work);
  37. return IRQ_HANDLED;
  38. }
  39. static struct spi_device *wl_to_spi(struct wl1251 *wl)
  40. {
  41. return wl->if_priv;
  42. }
  43. static void wl1251_spi_reset(struct wl1251 *wl)
  44. {
  45. u8 *cmd;
  46. struct spi_transfer t;
  47. struct spi_message m;
  48. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  49. if (!cmd) {
  50. wl1251_error("could not allocate cmd for spi reset");
  51. return;
  52. }
  53. memset(&t, 0, sizeof(t));
  54. spi_message_init(&m);
  55. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  56. t.tx_buf = cmd;
  57. t.len = WSPI_INIT_CMD_LEN;
  58. spi_message_add_tail(&t, &m);
  59. spi_sync(wl_to_spi(wl), &m);
  60. wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  61. kfree(cmd);
  62. }
  63. static void wl1251_spi_wake(struct wl1251 *wl)
  64. {
  65. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  66. struct spi_transfer t;
  67. struct spi_message m;
  68. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  69. if (!cmd) {
  70. wl1251_error("could not allocate cmd for spi init");
  71. return;
  72. }
  73. memset(crc, 0, sizeof(crc));
  74. memset(&t, 0, sizeof(t));
  75. spi_message_init(&m);
  76. /*
  77. * Set WSPI_INIT_COMMAND
  78. * the data is being send from the MSB to LSB
  79. */
  80. cmd[2] = 0xff;
  81. cmd[3] = 0xff;
  82. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  83. cmd[0] = 0;
  84. cmd[7] = 0;
  85. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  86. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  87. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  88. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  89. else
  90. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  91. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  92. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  93. crc[0] = cmd[1];
  94. crc[1] = cmd[0];
  95. crc[2] = cmd[7];
  96. crc[3] = cmd[6];
  97. crc[4] = cmd[5];
  98. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  99. cmd[4] |= WSPI_INIT_CMD_END;
  100. t.tx_buf = cmd;
  101. t.len = WSPI_INIT_CMD_LEN;
  102. spi_message_add_tail(&t, &m);
  103. spi_sync(wl_to_spi(wl), &m);
  104. wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  105. kfree(cmd);
  106. }
  107. static void wl1251_spi_reset_wake(struct wl1251 *wl)
  108. {
  109. wl1251_spi_reset(wl);
  110. wl1251_spi_wake(wl);
  111. }
  112. static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
  113. size_t len)
  114. {
  115. struct spi_transfer t[3];
  116. struct spi_message m;
  117. u8 *busy_buf;
  118. u32 *cmd;
  119. cmd = &wl->buffer_cmd;
  120. busy_buf = wl->buffer_busyword;
  121. *cmd = 0;
  122. *cmd |= WSPI_CMD_READ;
  123. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  124. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  125. spi_message_init(&m);
  126. memset(t, 0, sizeof(t));
  127. t[0].tx_buf = cmd;
  128. t[0].len = 4;
  129. spi_message_add_tail(&t[0], &m);
  130. /* Busy and non busy words read */
  131. t[1].rx_buf = busy_buf;
  132. t[1].len = WL1251_BUSY_WORD_LEN;
  133. spi_message_add_tail(&t[1], &m);
  134. t[2].rx_buf = buf;
  135. t[2].len = len;
  136. spi_message_add_tail(&t[2], &m);
  137. spi_sync(wl_to_spi(wl), &m);
  138. /* FIXME: check busy words */
  139. wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  140. wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  141. }
  142. static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
  143. size_t len)
  144. {
  145. struct spi_transfer t[2];
  146. struct spi_message m;
  147. u32 *cmd;
  148. cmd = &wl->buffer_cmd;
  149. *cmd = 0;
  150. *cmd |= WSPI_CMD_WRITE;
  151. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  152. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  153. spi_message_init(&m);
  154. memset(t, 0, sizeof(t));
  155. t[0].tx_buf = cmd;
  156. t[0].len = sizeof(*cmd);
  157. spi_message_add_tail(&t[0], &m);
  158. t[1].tx_buf = buf;
  159. t[1].len = len;
  160. spi_message_add_tail(&t[1], &m);
  161. spi_sync(wl_to_spi(wl), &m);
  162. wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  163. wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  164. }
  165. static void wl1251_spi_enable_irq(struct wl1251 *wl)
  166. {
  167. return enable_irq(wl->irq);
  168. }
  169. static void wl1251_spi_disable_irq(struct wl1251 *wl)
  170. {
  171. return disable_irq(wl->irq);
  172. }
  173. static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
  174. {
  175. if (wl->set_power)
  176. wl->set_power(enable);
  177. return 0;
  178. }
  179. static const struct wl1251_if_operations wl1251_spi_ops = {
  180. .read = wl1251_spi_read,
  181. .write = wl1251_spi_write,
  182. .reset = wl1251_spi_reset_wake,
  183. .enable_irq = wl1251_spi_enable_irq,
  184. .disable_irq = wl1251_spi_disable_irq,
  185. .power = wl1251_spi_set_power,
  186. };
  187. static int wl1251_spi_probe(struct spi_device *spi)
  188. {
  189. struct wl12xx_platform_data *pdata;
  190. struct ieee80211_hw *hw;
  191. struct wl1251 *wl;
  192. int ret;
  193. pdata = spi->dev.platform_data;
  194. if (!pdata) {
  195. wl1251_error("no platform data");
  196. return -ENODEV;
  197. }
  198. hw = wl1251_alloc_hw();
  199. if (IS_ERR(hw))
  200. return PTR_ERR(hw);
  201. wl = hw->priv;
  202. SET_IEEE80211_DEV(hw, &spi->dev);
  203. dev_set_drvdata(&spi->dev, wl);
  204. wl->if_priv = spi;
  205. wl->if_ops = &wl1251_spi_ops;
  206. /* This is the only SPI value that we need to set here, the rest
  207. * comes from the board-peripherals file */
  208. spi->bits_per_word = 32;
  209. ret = spi_setup(spi);
  210. if (ret < 0) {
  211. wl1251_error("spi_setup failed");
  212. goto out_free;
  213. }
  214. wl->set_power = pdata->set_power;
  215. if (!wl->set_power) {
  216. wl1251_error("set power function missing in platform data");
  217. return -ENODEV;
  218. }
  219. wl->irq = spi->irq;
  220. if (wl->irq < 0) {
  221. wl1251_error("irq missing in platform data");
  222. return -ENODEV;
  223. }
  224. wl->use_eeprom = pdata->use_eeprom;
  225. irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
  226. ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
  227. if (ret < 0) {
  228. wl1251_error("request_irq() failed: %d", ret);
  229. goto out_free;
  230. }
  231. irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  232. ret = wl1251_init_ieee80211(wl);
  233. if (ret)
  234. goto out_irq;
  235. return 0;
  236. out_irq:
  237. free_irq(wl->irq, wl);
  238. out_free:
  239. ieee80211_free_hw(hw);
  240. return ret;
  241. }
  242. static int wl1251_spi_remove(struct spi_device *spi)
  243. {
  244. struct wl1251 *wl = dev_get_drvdata(&spi->dev);
  245. free_irq(wl->irq, wl);
  246. wl1251_free_hw(wl);
  247. return 0;
  248. }
  249. static struct spi_driver wl1251_spi_driver = {
  250. .driver = {
  251. .name = DRIVER_NAME,
  252. .owner = THIS_MODULE,
  253. },
  254. .probe = wl1251_spi_probe,
  255. .remove = wl1251_spi_remove,
  256. };
  257. static int __init wl1251_spi_init(void)
  258. {
  259. int ret;
  260. ret = spi_register_driver(&wl1251_spi_driver);
  261. if (ret < 0) {
  262. wl1251_error("failed to register spi driver: %d", ret);
  263. goto out;
  264. }
  265. out:
  266. return ret;
  267. }
  268. static void __exit wl1251_spi_exit(void)
  269. {
  270. spi_unregister_driver(&wl1251_spi_driver);
  271. wl1251_notice("unloaded");
  272. }
  273. module_init(wl1251_spi_init);
  274. module_exit(wl1251_spi_exit);
  275. MODULE_LICENSE("GPL");
  276. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
  277. MODULE_ALIAS("spi:wl1251");