spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@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/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/module.h>
  26. #include <linux/crc7.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/wl12xx.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include "wl12xx.h"
  32. #include "wl12xx_80211.h"
  33. #include "io.h"
  34. #include "reg.h"
  35. #define WSPI_CMD_READ 0x40000000
  36. #define WSPI_CMD_WRITE 0x00000000
  37. #define WSPI_CMD_FIXED 0x20000000
  38. #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
  39. #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
  40. #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
  41. #define WSPI_INIT_CMD_CRC_LEN 5
  42. #define WSPI_INIT_CMD_START 0x00
  43. #define WSPI_INIT_CMD_TX 0x40
  44. /* the extra bypass bit is sampled by the TNET as '1' */
  45. #define WSPI_INIT_CMD_BYPASS_BIT 0x80
  46. #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
  47. #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
  48. #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
  49. #define WSPI_INIT_CMD_IOD 0x40
  50. #define WSPI_INIT_CMD_IP 0x20
  51. #define WSPI_INIT_CMD_CS 0x10
  52. #define WSPI_INIT_CMD_WS 0x08
  53. #define WSPI_INIT_CMD_WSPI 0x01
  54. #define WSPI_INIT_CMD_END 0x01
  55. #define WSPI_INIT_CMD_LEN 8
  56. #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
  57. ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
  58. #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
  59. /* HW limitation: maximum possible chunk size is 4095 bytes */
  60. #define WSPI_MAX_CHUNK_SIZE 4092
  61. #define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE)
  62. struct wl12xx_spi_glue {
  63. struct device *dev;
  64. struct platform_device *core;
  65. };
  66. static void wl12xx_spi_reset(struct device *child)
  67. {
  68. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  69. u8 *cmd;
  70. struct spi_transfer t;
  71. struct spi_message m;
  72. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  73. if (!cmd) {
  74. wl1271_error("could not allocate cmd for spi reset");
  75. return;
  76. }
  77. memset(&t, 0, sizeof(t));
  78. spi_message_init(&m);
  79. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  80. t.tx_buf = cmd;
  81. t.len = WSPI_INIT_CMD_LEN;
  82. spi_message_add_tail(&t, &m);
  83. spi_sync(to_spi_device(glue->dev), &m);
  84. wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  85. kfree(cmd);
  86. }
  87. static void wl12xx_spi_init(struct device *child)
  88. {
  89. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  90. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  91. struct spi_transfer t;
  92. struct spi_message m;
  93. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  94. if (!cmd) {
  95. wl1271_error("could not allocate cmd for spi init");
  96. return;
  97. }
  98. memset(crc, 0, sizeof(crc));
  99. memset(&t, 0, sizeof(t));
  100. spi_message_init(&m);
  101. /*
  102. * Set WSPI_INIT_COMMAND
  103. * the data is being send from the MSB to LSB
  104. */
  105. cmd[2] = 0xff;
  106. cmd[3] = 0xff;
  107. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  108. cmd[0] = 0;
  109. cmd[7] = 0;
  110. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  111. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  112. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  113. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  114. else
  115. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  116. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  117. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  118. crc[0] = cmd[1];
  119. crc[1] = cmd[0];
  120. crc[2] = cmd[7];
  121. crc[3] = cmd[6];
  122. crc[4] = cmd[5];
  123. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  124. cmd[4] |= WSPI_INIT_CMD_END;
  125. t.tx_buf = cmd;
  126. t.len = WSPI_INIT_CMD_LEN;
  127. spi_message_add_tail(&t, &m);
  128. spi_sync(to_spi_device(glue->dev), &m);
  129. wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  130. kfree(cmd);
  131. }
  132. #define WL1271_BUSY_WORD_TIMEOUT 1000
  133. static int wl12xx_spi_read_busy(struct device *child)
  134. {
  135. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  136. struct wl1271 *wl = dev_get_drvdata(child);
  137. struct spi_transfer t[1];
  138. struct spi_message m;
  139. u32 *busy_buf;
  140. int num_busy_bytes = 0;
  141. /*
  142. * Read further busy words from SPI until a non-busy word is
  143. * encountered, then read the data itself into the buffer.
  144. */
  145. num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
  146. busy_buf = wl->buffer_busyword;
  147. while (num_busy_bytes) {
  148. num_busy_bytes--;
  149. spi_message_init(&m);
  150. memset(t, 0, sizeof(t));
  151. t[0].rx_buf = busy_buf;
  152. t[0].len = sizeof(u32);
  153. t[0].cs_change = true;
  154. spi_message_add_tail(&t[0], &m);
  155. spi_sync(to_spi_device(glue->dev), &m);
  156. if (*busy_buf & 0x1)
  157. return 0;
  158. }
  159. /* The SPI bus is unresponsive, the read failed. */
  160. wl1271_error("SPI read busy-word timeout!\n");
  161. return -ETIMEDOUT;
  162. }
  163. static void wl12xx_spi_raw_read(struct device *child, int addr, void *buf,
  164. size_t len, bool fixed)
  165. {
  166. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  167. struct wl1271 *wl = dev_get_drvdata(child);
  168. struct spi_transfer t[2];
  169. struct spi_message m;
  170. u32 *busy_buf;
  171. u32 *cmd;
  172. u32 chunk_len;
  173. while (len > 0) {
  174. chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
  175. cmd = &wl->buffer_cmd;
  176. busy_buf = wl->buffer_busyword;
  177. *cmd = 0;
  178. *cmd |= WSPI_CMD_READ;
  179. *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
  180. WSPI_CMD_BYTE_LENGTH;
  181. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  182. if (fixed)
  183. *cmd |= WSPI_CMD_FIXED;
  184. spi_message_init(&m);
  185. memset(t, 0, sizeof(t));
  186. t[0].tx_buf = cmd;
  187. t[0].len = 4;
  188. t[0].cs_change = true;
  189. spi_message_add_tail(&t[0], &m);
  190. /* Busy and non busy words read */
  191. t[1].rx_buf = busy_buf;
  192. t[1].len = WL1271_BUSY_WORD_LEN;
  193. t[1].cs_change = true;
  194. spi_message_add_tail(&t[1], &m);
  195. spi_sync(to_spi_device(glue->dev), &m);
  196. if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
  197. wl12xx_spi_read_busy(child)) {
  198. memset(buf, 0, chunk_len);
  199. return;
  200. }
  201. spi_message_init(&m);
  202. memset(t, 0, sizeof(t));
  203. t[0].rx_buf = buf;
  204. t[0].len = chunk_len;
  205. t[0].cs_change = true;
  206. spi_message_add_tail(&t[0], &m);
  207. spi_sync(to_spi_device(glue->dev), &m);
  208. wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  209. wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, chunk_len);
  210. if (!fixed)
  211. addr += chunk_len;
  212. buf += chunk_len;
  213. len -= chunk_len;
  214. }
  215. }
  216. static void wl12xx_spi_raw_write(struct device *child, int addr, void *buf,
  217. size_t len, bool fixed)
  218. {
  219. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  220. struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS];
  221. struct spi_message m;
  222. u32 commands[WSPI_MAX_NUM_OF_CHUNKS];
  223. u32 *cmd;
  224. u32 chunk_len;
  225. int i;
  226. WARN_ON(len > WL1271_AGGR_BUFFER_SIZE);
  227. spi_message_init(&m);
  228. memset(t, 0, sizeof(t));
  229. cmd = &commands[0];
  230. i = 0;
  231. while (len > 0) {
  232. chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
  233. *cmd = 0;
  234. *cmd |= WSPI_CMD_WRITE;
  235. *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
  236. WSPI_CMD_BYTE_LENGTH;
  237. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  238. if (fixed)
  239. *cmd |= WSPI_CMD_FIXED;
  240. t[i].tx_buf = cmd;
  241. t[i].len = sizeof(*cmd);
  242. spi_message_add_tail(&t[i++], &m);
  243. t[i].tx_buf = buf;
  244. t[i].len = chunk_len;
  245. spi_message_add_tail(&t[i++], &m);
  246. wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  247. wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, chunk_len);
  248. if (!fixed)
  249. addr += chunk_len;
  250. buf += chunk_len;
  251. len -= chunk_len;
  252. cmd++;
  253. }
  254. spi_sync(to_spi_device(glue->dev), &m);
  255. }
  256. static struct wl1271_if_operations spi_ops = {
  257. .read = wl12xx_spi_raw_read,
  258. .write = wl12xx_spi_raw_write,
  259. .reset = wl12xx_spi_reset,
  260. .init = wl12xx_spi_init,
  261. .set_block_size = NULL,
  262. };
  263. static int __devinit wl1271_probe(struct spi_device *spi)
  264. {
  265. struct wl12xx_spi_glue *glue;
  266. struct wl12xx_platform_data *pdata;
  267. struct resource res[1];
  268. int ret = -ENOMEM;
  269. pdata = spi->dev.platform_data;
  270. if (!pdata) {
  271. wl1271_error("no platform data");
  272. return -ENODEV;
  273. }
  274. pdata->ops = &spi_ops;
  275. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  276. if (!glue) {
  277. wl1271_error("can't allocate glue");
  278. goto out;
  279. }
  280. glue->dev = &spi->dev;
  281. spi_set_drvdata(spi, glue);
  282. /* This is the only SPI value that we need to set here, the rest
  283. * comes from the board-peripherals file */
  284. spi->bits_per_word = 32;
  285. ret = spi_setup(spi);
  286. if (ret < 0) {
  287. wl1271_error("spi_setup failed");
  288. goto out_free_glue;
  289. }
  290. glue->core = platform_device_alloc("wl12xx-spi", -1);
  291. if (!glue->core) {
  292. wl1271_error("can't allocate platform_device");
  293. ret = -ENOMEM;
  294. goto out_free_glue;
  295. }
  296. glue->core->dev.parent = &spi->dev;
  297. memset(res, 0x00, sizeof(res));
  298. res[0].start = spi->irq;
  299. res[0].flags = IORESOURCE_IRQ;
  300. res[0].name = "irq";
  301. ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
  302. if (ret) {
  303. wl1271_error("can't add resources");
  304. goto out_dev_put;
  305. }
  306. ret = platform_device_add_data(glue->core, pdata, sizeof(*pdata));
  307. if (ret) {
  308. wl1271_error("can't add platform data");
  309. goto out_dev_put;
  310. }
  311. ret = platform_device_add(glue->core);
  312. if (ret) {
  313. wl1271_error("can't register platform device");
  314. goto out_dev_put;
  315. }
  316. return 0;
  317. out_dev_put:
  318. platform_device_put(glue->core);
  319. out_free_glue:
  320. kfree(glue);
  321. out:
  322. return ret;
  323. }
  324. static int __devexit wl1271_remove(struct spi_device *spi)
  325. {
  326. struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
  327. platform_device_del(glue->core);
  328. platform_device_put(glue->core);
  329. kfree(glue);
  330. return 0;
  331. }
  332. static struct spi_driver wl1271_spi_driver = {
  333. .driver = {
  334. .name = "wl1271_spi",
  335. .bus = &spi_bus_type,
  336. .owner = THIS_MODULE,
  337. },
  338. .probe = wl1271_probe,
  339. .remove = __devexit_p(wl1271_remove),
  340. };
  341. static int __init wl1271_init(void)
  342. {
  343. return spi_register_driver(&wl1271_spi_driver);
  344. }
  345. static void __exit wl1271_exit(void)
  346. {
  347. spi_unregister_driver(&wl1271_spi_driver);
  348. }
  349. module_init(wl1271_init);
  350. module_exit(wl1271_exit);
  351. MODULE_LICENSE("GPL");
  352. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  353. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
  354. MODULE_FIRMWARE(WL127X_FW_NAME);
  355. MODULE_FIRMWARE(WL128X_FW_NAME);
  356. MODULE_ALIAS("spi:wl1271");