spi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. }
  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 int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
  172. {
  173. if (wl->set_power)
  174. wl->set_power(enable);
  175. return 0;
  176. }
  177. static const struct wl1251_if_operations wl1251_spi_ops = {
  178. .read = wl1251_spi_read,
  179. .write = wl1251_spi_write,
  180. .reset = wl1251_spi_reset_wake,
  181. .enable_irq = wl1251_spi_enable_irq,
  182. .disable_irq = wl1251_spi_disable_irq,
  183. .power = wl1251_spi_set_power,
  184. };
  185. static int __devinit wl1251_spi_probe(struct spi_device *spi)
  186. {
  187. struct wl12xx_platform_data *pdata;
  188. struct ieee80211_hw *hw;
  189. struct wl1251 *wl;
  190. int ret;
  191. pdata = spi->dev.platform_data;
  192. if (!pdata) {
  193. wl1251_error("no platform data");
  194. return -ENODEV;
  195. }
  196. hw = wl1251_alloc_hw();
  197. if (IS_ERR(hw))
  198. return PTR_ERR(hw);
  199. wl = hw->priv;
  200. SET_IEEE80211_DEV(hw, &spi->dev);
  201. dev_set_drvdata(&spi->dev, wl);
  202. wl->if_priv = spi;
  203. wl->if_ops = &wl1251_spi_ops;
  204. /* This is the only SPI value that we need to set here, the rest
  205. * comes from the board-peripherals file */
  206. spi->bits_per_word = 32;
  207. ret = spi_setup(spi);
  208. if (ret < 0) {
  209. wl1251_error("spi_setup failed");
  210. goto out_free;
  211. }
  212. wl->set_power = pdata->set_power;
  213. if (!wl->set_power) {
  214. wl1251_error("set power function missing in platform data");
  215. return -ENODEV;
  216. }
  217. wl->irq = spi->irq;
  218. if (wl->irq < 0) {
  219. wl1251_error("irq missing in platform data");
  220. return -ENODEV;
  221. }
  222. wl->use_eeprom = pdata->use_eeprom;
  223. irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
  224. ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
  225. if (ret < 0) {
  226. wl1251_error("request_irq() failed: %d", ret);
  227. goto out_free;
  228. }
  229. irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  230. ret = wl1251_init_ieee80211(wl);
  231. if (ret)
  232. goto out_irq;
  233. return 0;
  234. out_irq:
  235. free_irq(wl->irq, wl);
  236. out_free:
  237. ieee80211_free_hw(hw);
  238. return ret;
  239. }
  240. static int __devexit wl1251_spi_remove(struct spi_device *spi)
  241. {
  242. struct wl1251 *wl = dev_get_drvdata(&spi->dev);
  243. free_irq(wl->irq, wl);
  244. wl1251_free_hw(wl);
  245. return 0;
  246. }
  247. static struct spi_driver wl1251_spi_driver = {
  248. .driver = {
  249. .name = DRIVER_NAME,
  250. .owner = THIS_MODULE,
  251. },
  252. .probe = wl1251_spi_probe,
  253. .remove = __devexit_p(wl1251_spi_remove),
  254. };
  255. static int __init wl1251_spi_init(void)
  256. {
  257. int ret;
  258. ret = spi_register_driver(&wl1251_spi_driver);
  259. if (ret < 0) {
  260. wl1251_error("failed to register spi driver: %d", ret);
  261. goto out;
  262. }
  263. out:
  264. return ret;
  265. }
  266. static void __exit wl1251_spi_exit(void)
  267. {
  268. spi_unregister_driver(&wl1251_spi_driver);
  269. wl1251_notice("unloaded");
  270. }
  271. module_init(wl1251_spi_init);
  272. module_exit(wl1251_spi_exit);
  273. MODULE_LICENSE("GPL");
  274. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
  275. MODULE_ALIAS("spi:wl1251");