wl1251_spi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Kalle Valo <kalle.valo@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/slab.h>
  26. #include <linux/crc7.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/wl12xx.h>
  29. #include "wl1251.h"
  30. #include "wl1251_reg.h"
  31. #include "wl1251_spi.h"
  32. static irqreturn_t wl1251_irq(int irq, void *cookie)
  33. {
  34. struct wl1251 *wl;
  35. wl1251_debug(DEBUG_IRQ, "IRQ");
  36. wl = cookie;
  37. ieee80211_queue_work(wl->hw, &wl->irq_work);
  38. return IRQ_HANDLED;
  39. }
  40. static struct spi_device *wl_to_spi(struct wl1251 *wl)
  41. {
  42. return wl->if_priv;
  43. }
  44. static void wl1251_spi_reset(struct wl1251 *wl)
  45. {
  46. u8 *cmd;
  47. struct spi_transfer t;
  48. struct spi_message m;
  49. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  50. if (!cmd) {
  51. wl1251_error("could not allocate cmd for spi reset");
  52. return;
  53. }
  54. memset(&t, 0, sizeof(t));
  55. spi_message_init(&m);
  56. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  57. t.tx_buf = cmd;
  58. t.len = WSPI_INIT_CMD_LEN;
  59. spi_message_add_tail(&t, &m);
  60. spi_sync(wl_to_spi(wl), &m);
  61. wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  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. }
  106. static void wl1251_spi_reset_wake(struct wl1251 *wl)
  107. {
  108. wl1251_spi_reset(wl);
  109. wl1251_spi_wake(wl);
  110. }
  111. static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
  112. size_t len)
  113. {
  114. struct spi_transfer t[3];
  115. struct spi_message m;
  116. u8 *busy_buf;
  117. u32 *cmd;
  118. cmd = &wl->buffer_cmd;
  119. busy_buf = wl->buffer_busyword;
  120. *cmd = 0;
  121. *cmd |= WSPI_CMD_READ;
  122. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  123. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  124. spi_message_init(&m);
  125. memset(t, 0, sizeof(t));
  126. t[0].tx_buf = cmd;
  127. t[0].len = 4;
  128. spi_message_add_tail(&t[0], &m);
  129. /* Busy and non busy words read */
  130. t[1].rx_buf = busy_buf;
  131. t[1].len = WL1251_BUSY_WORD_LEN;
  132. spi_message_add_tail(&t[1], &m);
  133. t[2].rx_buf = buf;
  134. t[2].len = len;
  135. spi_message_add_tail(&t[2], &m);
  136. spi_sync(wl_to_spi(wl), &m);
  137. /* FIXME: check busy words */
  138. wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  139. wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  140. }
  141. static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
  142. size_t len)
  143. {
  144. struct spi_transfer t[2];
  145. struct spi_message m;
  146. u32 *cmd;
  147. cmd = &wl->buffer_cmd;
  148. *cmd = 0;
  149. *cmd |= WSPI_CMD_WRITE;
  150. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  151. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  152. spi_message_init(&m);
  153. memset(t, 0, sizeof(t));
  154. t[0].tx_buf = cmd;
  155. t[0].len = sizeof(*cmd);
  156. spi_message_add_tail(&t[0], &m);
  157. t[1].tx_buf = buf;
  158. t[1].len = len;
  159. spi_message_add_tail(&t[1], &m);
  160. spi_sync(wl_to_spi(wl), &m);
  161. wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  162. wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  163. }
  164. static void wl1251_spi_enable_irq(struct wl1251 *wl)
  165. {
  166. return enable_irq(wl->irq);
  167. }
  168. static void wl1251_spi_disable_irq(struct wl1251 *wl)
  169. {
  170. return disable_irq(wl->irq);
  171. }
  172. static const struct wl1251_if_operations wl1251_spi_ops = {
  173. .read = wl1251_spi_read,
  174. .write = wl1251_spi_write,
  175. .reset = wl1251_spi_reset_wake,
  176. .enable_irq = wl1251_spi_enable_irq,
  177. .disable_irq = wl1251_spi_disable_irq,
  178. };
  179. static int __devinit wl1251_spi_probe(struct spi_device *spi)
  180. {
  181. struct wl12xx_platform_data *pdata;
  182. struct ieee80211_hw *hw;
  183. struct wl1251 *wl;
  184. int ret;
  185. pdata = spi->dev.platform_data;
  186. if (!pdata) {
  187. wl1251_error("no platform data");
  188. return -ENODEV;
  189. }
  190. hw = wl1251_alloc_hw();
  191. if (IS_ERR(hw))
  192. return PTR_ERR(hw);
  193. wl = hw->priv;
  194. SET_IEEE80211_DEV(hw, &spi->dev);
  195. dev_set_drvdata(&spi->dev, wl);
  196. wl->if_priv = spi;
  197. wl->if_ops = &wl1251_spi_ops;
  198. /* This is the only SPI value that we need to set here, the rest
  199. * comes from the board-peripherals file */
  200. spi->bits_per_word = 32;
  201. ret = spi_setup(spi);
  202. if (ret < 0) {
  203. wl1251_error("spi_setup failed");
  204. goto out_free;
  205. }
  206. wl->set_power = pdata->set_power;
  207. if (!wl->set_power) {
  208. wl1251_error("set power function missing in platform data");
  209. return -ENODEV;
  210. }
  211. wl->irq = spi->irq;
  212. if (wl->irq < 0) {
  213. wl1251_error("irq missing in platform data");
  214. return -ENODEV;
  215. }
  216. wl->use_eeprom = pdata->use_eeprom;
  217. ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
  218. if (ret < 0) {
  219. wl1251_error("request_irq() failed: %d", ret);
  220. goto out_free;
  221. }
  222. set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  223. disable_irq(wl->irq);
  224. ret = wl1251_init_ieee80211(wl);
  225. if (ret)
  226. goto out_irq;
  227. return 0;
  228. out_irq:
  229. free_irq(wl->irq, wl);
  230. out_free:
  231. ieee80211_free_hw(hw);
  232. return ret;
  233. }
  234. static int __devexit wl1251_spi_remove(struct spi_device *spi)
  235. {
  236. struct wl1251 *wl = dev_get_drvdata(&spi->dev);
  237. free_irq(wl->irq, wl);
  238. wl1251_free_hw(wl);
  239. return 0;
  240. }
  241. static struct spi_driver wl1251_spi_driver = {
  242. .driver = {
  243. .name = DRIVER_NAME,
  244. .bus = &spi_bus_type,
  245. .owner = THIS_MODULE,
  246. },
  247. .probe = wl1251_spi_probe,
  248. .remove = __devexit_p(wl1251_spi_remove),
  249. };
  250. static int __init wl1251_spi_init(void)
  251. {
  252. int ret;
  253. ret = spi_register_driver(&wl1251_spi_driver);
  254. if (ret < 0) {
  255. wl1251_error("failed to register spi driver: %d", ret);
  256. goto out;
  257. }
  258. out:
  259. return ret;
  260. }
  261. static void __exit wl1251_spi_exit(void)
  262. {
  263. spi_unregister_driver(&wl1251_spi_driver);
  264. wl1251_notice("unloaded");
  265. }
  266. module_init(wl1251_spi_init);
  267. module_exit(wl1251_spi_exit);
  268. MODULE_LICENSE("GPL");
  269. MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
  270. MODULE_ALIAS("spi:wl1251");