spi.c 10 KB

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