spi.c 10 KB

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