spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/slab.h>
  27. #include <linux/swab.h>
  28. #include <linux/crc7.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/wl12xx.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/regulator/consumer.h>
  34. #include "wlcore.h"
  35. #include "wl12xx_80211.h"
  36. #include "io.h"
  37. #define WSPI_CMD_READ 0x40000000
  38. #define WSPI_CMD_WRITE 0x00000000
  39. #define WSPI_CMD_FIXED 0x20000000
  40. #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
  41. #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
  42. #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
  43. #define WSPI_INIT_CMD_CRC_LEN 5
  44. #define WSPI_INIT_CMD_START 0x00
  45. #define WSPI_INIT_CMD_TX 0x40
  46. /* the extra bypass bit is sampled by the TNET as '1' */
  47. #define WSPI_INIT_CMD_BYPASS_BIT 0x80
  48. #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
  49. #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
  50. #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
  51. #define WSPI_INIT_CMD_IOD 0x40
  52. #define WSPI_INIT_CMD_IP 0x20
  53. #define WSPI_INIT_CMD_CS 0x10
  54. #define WSPI_INIT_CMD_WS 0x08
  55. #define WSPI_INIT_CMD_WSPI 0x01
  56. #define WSPI_INIT_CMD_END 0x01
  57. #define WSPI_INIT_CMD_LEN 8
  58. #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
  59. ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
  60. #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
  61. /* HW limitation: maximum possible chunk size is 4095 bytes */
  62. #define WSPI_MAX_CHUNK_SIZE 4092
  63. /*
  64. * only support SPI for 12xx - this code should be reworked when 18xx
  65. * support is introduced
  66. */
  67. #define SPI_AGGR_BUFFER_SIZE (4 * PAGE_SIZE)
  68. /* Maximum number of SPI write chunks */
  69. #define WSPI_MAX_NUM_OF_CHUNKS \
  70. ((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
  71. struct wl12xx_spi_glue {
  72. struct device *dev;
  73. struct platform_device *core;
  74. struct regulator *reg; /* Power regulator */
  75. };
  76. static void wl12xx_spi_reset(struct device *child)
  77. {
  78. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  79. u8 *cmd;
  80. struct spi_transfer t;
  81. struct spi_message m;
  82. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  83. if (!cmd) {
  84. dev_err(child->parent,
  85. "could not allocate cmd for spi reset\n");
  86. return;
  87. }
  88. memset(&t, 0, sizeof(t));
  89. spi_message_init(&m);
  90. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  91. t.tx_buf = cmd;
  92. t.len = WSPI_INIT_CMD_LEN;
  93. spi_message_add_tail(&t, &m);
  94. spi_sync(to_spi_device(glue->dev), &m);
  95. kfree(cmd);
  96. }
  97. static void wl12xx_spi_init(struct device *child)
  98. {
  99. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  100. struct spi_transfer t;
  101. struct spi_message m;
  102. u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  103. if (!cmd) {
  104. dev_err(child->parent,
  105. "could not allocate cmd for spi init\n");
  106. return;
  107. }
  108. memset(&t, 0, sizeof(t));
  109. spi_message_init(&m);
  110. /*
  111. * Set WSPI_INIT_COMMAND
  112. * the data is being send from the MSB to LSB
  113. */
  114. cmd[0] = 0xff;
  115. cmd[1] = 0xff;
  116. cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  117. cmd[3] = 0;
  118. cmd[4] = 0;
  119. cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  120. cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  121. cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  122. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  123. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  124. cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  125. else
  126. cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  127. cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
  128. /*
  129. * The above is the logical order; it must actually be stored
  130. * in the buffer byte-swapped.
  131. */
  132. __swab32s((u32 *)cmd);
  133. __swab32s((u32 *)cmd+1);
  134. t.tx_buf = cmd;
  135. t.len = WSPI_INIT_CMD_LEN;
  136. spi_message_add_tail(&t, &m);
  137. spi_sync(to_spi_device(glue->dev), &m);
  138. kfree(cmd);
  139. }
  140. #define WL1271_BUSY_WORD_TIMEOUT 1000
  141. static int wl12xx_spi_read_busy(struct device *child)
  142. {
  143. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  144. struct wl1271 *wl = dev_get_drvdata(child);
  145. struct spi_transfer t[1];
  146. struct spi_message m;
  147. u32 *busy_buf;
  148. int num_busy_bytes = 0;
  149. /*
  150. * Read further busy words from SPI until a non-busy word is
  151. * encountered, then read the data itself into the buffer.
  152. */
  153. num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
  154. busy_buf = wl->buffer_busyword;
  155. while (num_busy_bytes) {
  156. num_busy_bytes--;
  157. spi_message_init(&m);
  158. memset(t, 0, sizeof(t));
  159. t[0].rx_buf = busy_buf;
  160. t[0].len = sizeof(u32);
  161. t[0].cs_change = true;
  162. spi_message_add_tail(&t[0], &m);
  163. spi_sync(to_spi_device(glue->dev), &m);
  164. if (*busy_buf & 0x1)
  165. return 0;
  166. }
  167. /* The SPI bus is unresponsive, the read failed. */
  168. dev_err(child->parent, "SPI read busy-word timeout!\n");
  169. return -ETIMEDOUT;
  170. }
  171. static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
  172. void *buf, size_t len, bool fixed)
  173. {
  174. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  175. struct wl1271 *wl = dev_get_drvdata(child);
  176. struct spi_transfer t[2];
  177. struct spi_message m;
  178. u32 *busy_buf;
  179. u32 *cmd;
  180. u32 chunk_len;
  181. while (len > 0) {
  182. chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
  183. cmd = &wl->buffer_cmd;
  184. busy_buf = wl->buffer_busyword;
  185. *cmd = 0;
  186. *cmd |= WSPI_CMD_READ;
  187. *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
  188. WSPI_CMD_BYTE_LENGTH;
  189. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  190. if (fixed)
  191. *cmd |= WSPI_CMD_FIXED;
  192. spi_message_init(&m);
  193. memset(t, 0, sizeof(t));
  194. t[0].tx_buf = cmd;
  195. t[0].len = 4;
  196. t[0].cs_change = true;
  197. spi_message_add_tail(&t[0], &m);
  198. /* Busy and non busy words read */
  199. t[1].rx_buf = busy_buf;
  200. t[1].len = WL1271_BUSY_WORD_LEN;
  201. t[1].cs_change = true;
  202. spi_message_add_tail(&t[1], &m);
  203. spi_sync(to_spi_device(glue->dev), &m);
  204. if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
  205. wl12xx_spi_read_busy(child)) {
  206. memset(buf, 0, chunk_len);
  207. return 0;
  208. }
  209. spi_message_init(&m);
  210. memset(t, 0, sizeof(t));
  211. t[0].rx_buf = buf;
  212. t[0].len = chunk_len;
  213. t[0].cs_change = true;
  214. spi_message_add_tail(&t[0], &m);
  215. spi_sync(to_spi_device(glue->dev), &m);
  216. if (!fixed)
  217. addr += chunk_len;
  218. buf += chunk_len;
  219. len -= chunk_len;
  220. }
  221. return 0;
  222. }
  223. static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
  224. void *buf, size_t len, bool fixed)
  225. {
  226. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  227. /* SPI write buffers - 2 for each chunk */
  228. struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS];
  229. struct spi_message m;
  230. u32 commands[WSPI_MAX_NUM_OF_CHUNKS]; /* 1 command per chunk */
  231. u32 *cmd;
  232. u32 chunk_len;
  233. int i;
  234. WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
  235. spi_message_init(&m);
  236. memset(t, 0, sizeof(t));
  237. cmd = &commands[0];
  238. i = 0;
  239. while (len > 0) {
  240. chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
  241. *cmd = 0;
  242. *cmd |= WSPI_CMD_WRITE;
  243. *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
  244. WSPI_CMD_BYTE_LENGTH;
  245. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  246. if (fixed)
  247. *cmd |= WSPI_CMD_FIXED;
  248. t[i].tx_buf = cmd;
  249. t[i].len = sizeof(*cmd);
  250. spi_message_add_tail(&t[i++], &m);
  251. t[i].tx_buf = buf;
  252. t[i].len = chunk_len;
  253. spi_message_add_tail(&t[i++], &m);
  254. if (!fixed)
  255. addr += chunk_len;
  256. buf += chunk_len;
  257. len -= chunk_len;
  258. cmd++;
  259. }
  260. spi_sync(to_spi_device(glue->dev), &m);
  261. return 0;
  262. }
  263. /**
  264. * wl12xx_spi_set_power - power on/off the wl12xx unit
  265. * @child: wl12xx device handle.
  266. * @enable: true/false to power on/off the unit.
  267. *
  268. * use the WiFi enable regulator to enable/disable the WiFi unit.
  269. */
  270. static int wl12xx_spi_set_power(struct device *child, bool enable)
  271. {
  272. int ret = 0;
  273. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  274. WARN_ON(!glue->reg);
  275. /* Update regulator state */
  276. if (enable) {
  277. ret = regulator_enable(glue->reg);
  278. if (ret)
  279. dev_err(child, "Power enable failure\n");
  280. } else {
  281. ret = regulator_disable(glue->reg);
  282. if (ret)
  283. dev_err(child, "Power disable failure\n");
  284. }
  285. return ret;
  286. }
  287. static struct wl1271_if_operations spi_ops = {
  288. .read = wl12xx_spi_raw_read,
  289. .write = wl12xx_spi_raw_write,
  290. .reset = wl12xx_spi_reset,
  291. .init = wl12xx_spi_init,
  292. .power = wl12xx_spi_set_power,
  293. .set_block_size = NULL,
  294. };
  295. static const struct of_device_id wlcore_spi_of_match_table[] = {
  296. { .compatible = "ti,wl1271" },
  297. { }
  298. };
  299. MODULE_DEVICE_TABLE(of, wlcore_spi_of_match_table);
  300. /**
  301. * wlcore_probe_of - DT node parsing.
  302. * @spi: SPI slave device parameters.
  303. * @res: resource parameters.
  304. * @glue: wl12xx SPI bus to slave device glue parameters.
  305. * @pdev_data: wlcore device parameters
  306. */
  307. static int wlcore_probe_of(struct spi_device *spi, struct wl12xx_spi_glue *glue,
  308. struct wlcore_platdev_data *pdev_data)
  309. {
  310. struct device_node *dt_node = spi->dev.of_node;
  311. int ret;
  312. if (of_find_property(dt_node, "clock-xtal", NULL))
  313. pdev_data->ref_clock_xtal = true;
  314. ret = of_property_read_u32(dt_node, "ref-clock-frequency",
  315. &pdev_data->ref_clock_freq);
  316. if (IS_ERR_VALUE(ret)) {
  317. dev_err(glue->dev,
  318. "can't get reference clock frequency (%d)\n", ret);
  319. return ret;
  320. }
  321. return 0;
  322. }
  323. static int wl1271_probe(struct spi_device *spi)
  324. {
  325. struct wl12xx_spi_glue *glue;
  326. struct wlcore_platdev_data pdev_data;
  327. struct resource res[1];
  328. int ret;
  329. memset(&pdev_data, 0x00, sizeof(pdev_data));
  330. pdev_data.if_ops = &spi_ops;
  331. glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL);
  332. if (!glue) {
  333. dev_err(&spi->dev, "can't allocate glue\n");
  334. return -ENOMEM;
  335. }
  336. glue->dev = &spi->dev;
  337. spi_set_drvdata(spi, glue);
  338. /* This is the only SPI value that we need to set here, the rest
  339. * comes from the board-peripherals file */
  340. spi->bits_per_word = 32;
  341. glue->reg = devm_regulator_get(&spi->dev, "vwlan");
  342. if (PTR_ERR(glue->reg) == -EPROBE_DEFER)
  343. return -EPROBE_DEFER;
  344. if (IS_ERR(glue->reg)) {
  345. dev_err(glue->dev, "can't get regulator\n");
  346. return PTR_ERR(glue->reg);
  347. }
  348. ret = wlcore_probe_of(spi, glue, &pdev_data);
  349. if (IS_ERR_VALUE(ret)) {
  350. dev_err(glue->dev,
  351. "can't get device tree parameters (%d)\n", ret);
  352. return ret;
  353. }
  354. ret = spi_setup(spi);
  355. if (ret < 0) {
  356. dev_err(glue->dev, "spi_setup failed\n");
  357. return ret;
  358. }
  359. glue->core = platform_device_alloc("wl12xx", PLATFORM_DEVID_AUTO);
  360. if (!glue->core) {
  361. dev_err(glue->dev, "can't allocate platform_device\n");
  362. return -ENOMEM;
  363. }
  364. glue->core->dev.parent = &spi->dev;
  365. memset(res, 0x00, sizeof(res));
  366. res[0].start = spi->irq;
  367. res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(spi->irq);
  368. res[0].name = "irq";
  369. ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
  370. if (ret) {
  371. dev_err(glue->dev, "can't add resources\n");
  372. goto out_dev_put;
  373. }
  374. ret = platform_device_add_data(glue->core, &pdev_data,
  375. sizeof(pdev_data));
  376. if (ret) {
  377. dev_err(glue->dev, "can't add platform data\n");
  378. goto out_dev_put;
  379. }
  380. ret = platform_device_add(glue->core);
  381. if (ret) {
  382. dev_err(glue->dev, "can't register platform device\n");
  383. goto out_dev_put;
  384. }
  385. return 0;
  386. out_dev_put:
  387. platform_device_put(glue->core);
  388. return ret;
  389. }
  390. static int wl1271_remove(struct spi_device *spi)
  391. {
  392. struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
  393. platform_device_unregister(glue->core);
  394. return 0;
  395. }
  396. static struct spi_driver wl1271_spi_driver = {
  397. .driver = {
  398. .name = "wl1271_spi",
  399. .of_match_table = of_match_ptr(wlcore_spi_of_match_table),
  400. },
  401. .probe = wl1271_probe,
  402. .remove = wl1271_remove,
  403. };
  404. module_spi_driver(wl1271_spi_driver);
  405. MODULE_LICENSE("GPL");
  406. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  407. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
  408. MODULE_ALIAS("spi:wl1271");