spi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/irq.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/crc7.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/wl12xx.h>
  27. #include "wl1251.h"
  28. #include "reg.h"
  29. #include "spi.h"
  30. static irqreturn_t wl1251_irq(int irq, void *cookie)
  31. {
  32. struct wl1251 *wl;
  33. wl1251_debug(DEBUG_IRQ, "IRQ");
  34. wl = cookie;
  35. ieee80211_queue_work(wl->hw, &wl->irq_work);
  36. return IRQ_HANDLED;
  37. }
  38. static struct spi_device *wl_to_spi(struct wl1251 *wl)
  39. {
  40. return wl->if_priv;
  41. }
  42. static void wl1251_spi_reset(struct wl1251 *wl)
  43. {
  44. u8 *cmd;
  45. struct spi_transfer t;
  46. struct spi_message m;
  47. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  48. if (!cmd) {
  49. wl1251_error("could not allocate cmd for spi reset");
  50. return;
  51. }
  52. memset(&t, 0, sizeof(t));
  53. spi_message_init(&m);
  54. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  55. t.tx_buf = cmd;
  56. t.len = WSPI_INIT_CMD_LEN;
  57. spi_message_add_tail(&t, &m);
  58. spi_sync(wl_to_spi(wl), &m);
  59. wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  60. }
  61. static void wl1251_spi_wake(struct wl1251 *wl)
  62. {
  63. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  64. struct spi_transfer t;
  65. struct spi_message m;
  66. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  67. if (!cmd) {
  68. wl1251_error("could not allocate cmd for spi init");
  69. return;
  70. }
  71. memset(crc, 0, sizeof(crc));
  72. memset(&t, 0, sizeof(t));
  73. spi_message_init(&m);
  74. /*
  75. * Set WSPI_INIT_COMMAND
  76. * the data is being send from the MSB to LSB
  77. */
  78. cmd[2] = 0xff;
  79. cmd[3] = 0xff;
  80. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  81. cmd[0] = 0;
  82. cmd[7] = 0;
  83. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  84. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  85. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  86. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  87. else
  88. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  89. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  90. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  91. crc[0] = cmd[1];
  92. crc[1] = cmd[0];
  93. crc[2] = cmd[7];
  94. crc[3] = cmd[6];
  95. crc[4] = cmd[5];
  96. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  97. cmd[4] |= WSPI_INIT_CMD_END;
  98. t.tx_buf = cmd;
  99. t.len = WSPI_INIT_CMD_LEN;
  100. spi_message_add_tail(&t, &m);
  101. spi_sync(wl_to_spi(wl), &m);
  102. wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  103. }
  104. static void wl1251_spi_reset_wake(struct wl1251 *wl)
  105. {
  106. wl1251_spi_reset(wl);
  107. wl1251_spi_wake(wl);
  108. }
  109. static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
  110. size_t len)
  111. {
  112. struct spi_transfer t[3];
  113. struct spi_message m;
  114. u8 *busy_buf;
  115. u32 *cmd;
  116. cmd = &wl->buffer_cmd;
  117. busy_buf = wl->buffer_busyword;
  118. *cmd = 0;
  119. *cmd |= WSPI_CMD_READ;
  120. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  121. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  122. spi_message_init(&m);
  123. memset(t, 0, sizeof(t));
  124. t[0].tx_buf = cmd;
  125. t[0].len = 4;
  126. spi_message_add_tail(&t[0], &m);
  127. /* Busy and non busy words read */
  128. t[1].rx_buf = busy_buf;
  129. t[1].len = WL1251_BUSY_WORD_LEN;
  130. spi_message_add_tail(&t[1], &m);
  131. t[2].rx_buf = buf;
  132. t[2].len = len;
  133. spi_message_add_tail(&t[2], &m);
  134. spi_sync(wl_to_spi(wl), &m);
  135. /* FIXME: check busy words */
  136. wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  137. wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  138. }
  139. static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
  140. size_t len)
  141. {
  142. struct spi_transfer t[2];
  143. struct spi_message m;
  144. u32 *cmd;
  145. cmd = &wl->buffer_cmd;
  146. *cmd = 0;
  147. *cmd |= WSPI_CMD_WRITE;
  148. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  149. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  150. spi_message_init(&m);
  151. memset(t, 0, sizeof(t));
  152. t[0].tx_buf = cmd;
  153. t[0].len = sizeof(*cmd);
  154. spi_message_add_tail(&t[0], &m);
  155. t[1].tx_buf = buf;
  156. t[1].len = len;
  157. spi_message_add_tail(&t[1], &m);
  158. spi_sync(wl_to_spi(wl), &m);
  159. wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  160. wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  161. }
  162. static void wl1251_spi_enable_irq(struct wl1251 *wl)
  163. {
  164. return enable_irq(wl->irq);
  165. }
  166. static void wl1251_spi_disable_irq(struct wl1251 *wl)
  167. {
  168. return disable_irq(wl->irq);
  169. }
  170. static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
  171. {
  172. if (wl->set_power)
  173. wl->set_power(enable);
  174. return 0;
  175. }
  176. static const struct wl1251_if_operations wl1251_spi_ops = {
  177. .read = wl1251_spi_read,
  178. .write = wl1251_spi_write,
  179. .reset = wl1251_spi_reset_wake,
  180. .enable_irq = wl1251_spi_enable_irq,
  181. .disable_irq = wl1251_spi_disable_irq,
  182. .power = wl1251_spi_set_power,
  183. };
  184. static int __devinit wl1251_spi_probe(struct spi_device *spi)
  185. {
  186. struct wl12xx_platform_data *pdata;
  187. struct ieee80211_hw *hw;
  188. struct wl1251 *wl;
  189. int ret;
  190. pdata = spi->dev.platform_data;
  191. if (!pdata) {
  192. wl1251_error("no platform data");
  193. return -ENODEV;
  194. }
  195. hw = wl1251_alloc_hw();
  196. if (IS_ERR(hw))
  197. return PTR_ERR(hw);
  198. wl = hw->priv;
  199. SET_IEEE80211_DEV(hw, &spi->dev);
  200. dev_set_drvdata(&spi->dev, wl);
  201. wl->if_priv = spi;
  202. wl->if_ops = &wl1251_spi_ops;
  203. /* This is the only SPI value that we need to set here, the rest
  204. * comes from the board-peripherals file */
  205. spi->bits_per_word = 32;
  206. ret = spi_setup(spi);
  207. if (ret < 0) {
  208. wl1251_error("spi_setup failed");
  209. goto out_free;
  210. }
  211. wl->set_power = pdata->set_power;
  212. if (!wl->set_power) {
  213. wl1251_error("set power function missing in platform data");
  214. return -ENODEV;
  215. }
  216. wl->irq = spi->irq;
  217. if (wl->irq < 0) {
  218. wl1251_error("irq missing in platform data");
  219. return -ENODEV;
  220. }
  221. wl->use_eeprom = pdata->use_eeprom;
  222. ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
  223. if (ret < 0) {
  224. wl1251_error("request_irq() failed: %d", ret);
  225. goto out_free;
  226. }
  227. irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  228. disable_irq(wl->irq);
  229. ret = wl1251_init_ieee80211(wl);
  230. if (ret)
  231. goto out_irq;
  232. return 0;
  233. out_irq:
  234. free_irq(wl->irq, wl);
  235. out_free:
  236. ieee80211_free_hw(hw);
  237. return ret;
  238. }
  239. static int __devexit wl1251_spi_remove(struct spi_device *spi)
  240. {
  241. struct wl1251 *wl = dev_get_drvdata(&spi->dev);
  242. free_irq(wl->irq, wl);
  243. wl1251_free_hw(wl);
  244. return 0;
  245. }
  246. static struct spi_driver wl1251_spi_driver = {
  247. .driver = {
  248. .name = DRIVER_NAME,
  249. .bus = &spi_bus_type,
  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");