wl1251_spi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/crc7.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/spi/wl12xx.h>
  28. #include "wl1251.h"
  29. #include "wl1251_reg.h"
  30. #include "wl1251_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. }
  62. static void wl1251_spi_wake(struct wl1251 *wl)
  63. {
  64. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  65. struct spi_transfer t;
  66. struct spi_message m;
  67. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  68. if (!cmd) {
  69. wl1251_error("could not allocate cmd for spi init");
  70. return;
  71. }
  72. memset(crc, 0, sizeof(crc));
  73. memset(&t, 0, sizeof(t));
  74. spi_message_init(&m);
  75. /*
  76. * Set WSPI_INIT_COMMAND
  77. * the data is being send from the MSB to LSB
  78. */
  79. cmd[2] = 0xff;
  80. cmd[3] = 0xff;
  81. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  82. cmd[0] = 0;
  83. cmd[7] = 0;
  84. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  85. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  86. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  87. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  88. else
  89. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  90. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  91. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  92. crc[0] = cmd[1];
  93. crc[1] = cmd[0];
  94. crc[2] = cmd[7];
  95. crc[3] = cmd[6];
  96. crc[4] = cmd[5];
  97. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  98. cmd[4] |= WSPI_INIT_CMD_END;
  99. t.tx_buf = cmd;
  100. t.len = WSPI_INIT_CMD_LEN;
  101. spi_message_add_tail(&t, &m);
  102. spi_sync(wl_to_spi(wl), &m);
  103. wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  104. }
  105. static void wl1251_spi_reset_wake(struct wl1251 *wl)
  106. {
  107. wl1251_spi_reset(wl);
  108. wl1251_spi_wake(wl);
  109. }
  110. static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
  111. size_t len)
  112. {
  113. struct spi_transfer t[3];
  114. struct spi_message m;
  115. u8 *busy_buf;
  116. u32 *cmd;
  117. cmd = &wl->buffer_cmd;
  118. busy_buf = wl->buffer_busyword;
  119. *cmd = 0;
  120. *cmd |= WSPI_CMD_READ;
  121. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  122. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  123. spi_message_init(&m);
  124. memset(t, 0, sizeof(t));
  125. t[0].tx_buf = cmd;
  126. t[0].len = 4;
  127. spi_message_add_tail(&t[0], &m);
  128. /* Busy and non busy words read */
  129. t[1].rx_buf = busy_buf;
  130. t[1].len = WL1251_BUSY_WORD_LEN;
  131. spi_message_add_tail(&t[1], &m);
  132. t[2].rx_buf = buf;
  133. t[2].len = len;
  134. spi_message_add_tail(&t[2], &m);
  135. spi_sync(wl_to_spi(wl), &m);
  136. /* FIXME: check busy words */
  137. wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  138. wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  139. }
  140. static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
  141. size_t len)
  142. {
  143. struct spi_transfer t[2];
  144. struct spi_message m;
  145. u32 *cmd;
  146. cmd = &wl->buffer_cmd;
  147. *cmd = 0;
  148. *cmd |= WSPI_CMD_WRITE;
  149. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  150. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  151. spi_message_init(&m);
  152. memset(t, 0, sizeof(t));
  153. t[0].tx_buf = cmd;
  154. t[0].len = sizeof(*cmd);
  155. spi_message_add_tail(&t[0], &m);
  156. t[1].tx_buf = buf;
  157. t[1].len = len;
  158. spi_message_add_tail(&t[1], &m);
  159. spi_sync(wl_to_spi(wl), &m);
  160. wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  161. wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  162. }
  163. static void wl1251_spi_enable_irq(struct wl1251 *wl)
  164. {
  165. return enable_irq(wl->irq);
  166. }
  167. static void wl1251_spi_disable_irq(struct wl1251 *wl)
  168. {
  169. return disable_irq(wl->irq);
  170. }
  171. static const struct wl1251_if_operations wl1251_spi_ops = {
  172. .read = wl1251_spi_read,
  173. .write = wl1251_spi_write,
  174. .reset = wl1251_spi_reset_wake,
  175. .enable_irq = wl1251_spi_enable_irq,
  176. .disable_irq = wl1251_spi_disable_irq,
  177. };
  178. static int __devinit wl1251_spi_probe(struct spi_device *spi)
  179. {
  180. struct wl12xx_platform_data *pdata;
  181. struct ieee80211_hw *hw;
  182. struct wl1251 *wl;
  183. int ret;
  184. pdata = spi->dev.platform_data;
  185. if (!pdata) {
  186. wl1251_error("no platform data");
  187. return -ENODEV;
  188. }
  189. hw = wl1251_alloc_hw();
  190. if (IS_ERR(hw))
  191. return PTR_ERR(hw);
  192. wl = hw->priv;
  193. SET_IEEE80211_DEV(hw, &spi->dev);
  194. dev_set_drvdata(&spi->dev, wl);
  195. wl->if_priv = spi;
  196. wl->if_ops = &wl1251_spi_ops;
  197. /* This is the only SPI value that we need to set here, the rest
  198. * comes from the board-peripherals file */
  199. spi->bits_per_word = 32;
  200. ret = spi_setup(spi);
  201. if (ret < 0) {
  202. wl1251_error("spi_setup failed");
  203. goto out_free;
  204. }
  205. wl->set_power = pdata->set_power;
  206. if (!wl->set_power) {
  207. wl1251_error("set power function missing in platform data");
  208. return -ENODEV;
  209. }
  210. wl->irq = spi->irq;
  211. if (wl->irq < 0) {
  212. wl1251_error("irq missing in platform data");
  213. return -ENODEV;
  214. }
  215. ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
  216. if (ret < 0) {
  217. wl1251_error("request_irq() failed: %d", ret);
  218. goto out_free;
  219. }
  220. set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  221. disable_irq(wl->irq);
  222. ret = wl1251_init_ieee80211(wl);
  223. if (ret)
  224. goto out_irq;
  225. return 0;
  226. out_irq:
  227. free_irq(wl->irq, wl);
  228. out_free:
  229. ieee80211_free_hw(hw);
  230. return ret;
  231. }
  232. static int __devexit wl1251_spi_remove(struct spi_device *spi)
  233. {
  234. struct wl1251 *wl = dev_get_drvdata(&spi->dev);
  235. free_irq(wl->irq, wl);
  236. wl1251_free_hw(wl);
  237. return 0;
  238. }
  239. static struct spi_driver wl1251_spi_driver = {
  240. .driver = {
  241. .name = "wl12xx",
  242. .bus = &spi_bus_type,
  243. .owner = THIS_MODULE,
  244. },
  245. .probe = wl1251_spi_probe,
  246. .remove = __devexit_p(wl1251_spi_remove),
  247. };
  248. static int __init wl1251_spi_init(void)
  249. {
  250. int ret;
  251. ret = spi_register_driver(&wl1251_spi_driver);
  252. if (ret < 0) {
  253. wl1251_error("failed to register spi driver: %d", ret);
  254. goto out;
  255. }
  256. out:
  257. return ret;
  258. }
  259. static void __exit wl1251_spi_exit(void)
  260. {
  261. spi_unregister_driver(&wl1251_spi_driver);
  262. wl1251_notice("unloaded");
  263. }
  264. module_init(wl1251_spi_init);
  265. module_exit(wl1251_spi_exit);
  266. MODULE_LICENSE("GPL");
  267. MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");