wl1251_spi.c 6.7 KB

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