spi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. * wl18xx driver aggregation buffer size is (13 * PAGE_SIZE) compared to
  65. * (4 * PAGE_SIZE) for wl12xx, so use the larger buffer needed for wl18xx
  66. */
  67. #define SPI_AGGR_BUFFER_SIZE (13 * 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 wilink_familiy_data {
  72. char name[8];
  73. };
  74. const struct wilink_familiy_data *wilink_data;
  75. static const struct wilink_familiy_data wl18xx_data = {
  76. .name = "wl18xx",
  77. };
  78. static const struct wilink_familiy_data wl12xx_data = {
  79. .name = "wl12xx",
  80. };
  81. struct wl12xx_spi_glue {
  82. struct device *dev;
  83. struct platform_device *core;
  84. struct regulator *reg; /* Power regulator */
  85. };
  86. static void wl12xx_spi_reset(struct device *child)
  87. {
  88. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  89. u8 *cmd;
  90. struct spi_transfer t;
  91. struct spi_message m;
  92. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  93. if (!cmd) {
  94. dev_err(child->parent,
  95. "could not allocate cmd for spi reset\n");
  96. return;
  97. }
  98. memset(&t, 0, sizeof(t));
  99. spi_message_init(&m);
  100. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  101. t.tx_buf = cmd;
  102. t.len = WSPI_INIT_CMD_LEN;
  103. spi_message_add_tail(&t, &m);
  104. spi_sync(to_spi_device(glue->dev), &m);
  105. kfree(cmd);
  106. }
  107. static void wl12xx_spi_init(struct device *child)
  108. {
  109. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  110. struct spi_transfer t;
  111. struct spi_message m;
  112. struct spi_device *spi = to_spi_device(glue->dev);
  113. u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  114. if (!cmd) {
  115. dev_err(child->parent,
  116. "could not allocate cmd for spi init\n");
  117. return;
  118. }
  119. memset(&t, 0, sizeof(t));
  120. spi_message_init(&m);
  121. /*
  122. * Set WSPI_INIT_COMMAND
  123. * the data is being send from the MSB to LSB
  124. */
  125. cmd[0] = 0xff;
  126. cmd[1] = 0xff;
  127. cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  128. cmd[3] = 0;
  129. cmd[4] = 0;
  130. cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  131. cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  132. cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  133. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  134. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  135. cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  136. else
  137. cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  138. cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
  139. /*
  140. * The above is the logical order; it must actually be stored
  141. * in the buffer byte-swapped.
  142. */
  143. __swab32s((u32 *)cmd);
  144. __swab32s((u32 *)cmd+1);
  145. t.tx_buf = cmd;
  146. t.len = WSPI_INIT_CMD_LEN;
  147. spi_message_add_tail(&t, &m);
  148. spi_sync(to_spi_device(glue->dev), &m);
  149. /* Send extra clocks with inverted CS (high). this is required
  150. * by the wilink family in order to successfully enter WSPI mode.
  151. */
  152. spi->mode ^= SPI_CS_HIGH;
  153. memset(&m, 0, sizeof(m));
  154. spi_message_init(&m);
  155. cmd[0] = 0xff;
  156. cmd[1] = 0xff;
  157. cmd[2] = 0xff;
  158. cmd[3] = 0xff;
  159. __swab32s((u32 *)cmd);
  160. t.tx_buf = cmd;
  161. t.len = 4;
  162. spi_message_add_tail(&t, &m);
  163. spi_sync(to_spi_device(glue->dev), &m);
  164. /* Restore chip select configration to normal */
  165. spi->mode ^= SPI_CS_HIGH;
  166. kfree(cmd);
  167. }
  168. #define WL1271_BUSY_WORD_TIMEOUT 1000
  169. static int wl12xx_spi_read_busy(struct device *child)
  170. {
  171. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  172. struct wl1271 *wl = dev_get_drvdata(child);
  173. struct spi_transfer t[1];
  174. struct spi_message m;
  175. u32 *busy_buf;
  176. int num_busy_bytes = 0;
  177. /*
  178. * Read further busy words from SPI until a non-busy word is
  179. * encountered, then read the data itself into the buffer.
  180. */
  181. num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
  182. busy_buf = wl->buffer_busyword;
  183. while (num_busy_bytes) {
  184. num_busy_bytes--;
  185. spi_message_init(&m);
  186. memset(t, 0, sizeof(t));
  187. t[0].rx_buf = busy_buf;
  188. t[0].len = sizeof(u32);
  189. t[0].cs_change = true;
  190. spi_message_add_tail(&t[0], &m);
  191. spi_sync(to_spi_device(glue->dev), &m);
  192. if (*busy_buf & 0x1)
  193. return 0;
  194. }
  195. /* The SPI bus is unresponsive, the read failed. */
  196. dev_err(child->parent, "SPI read busy-word timeout!\n");
  197. return -ETIMEDOUT;
  198. }
  199. static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
  200. void *buf, size_t len, bool fixed)
  201. {
  202. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  203. struct wl1271 *wl = dev_get_drvdata(child);
  204. struct spi_transfer t[2];
  205. struct spi_message m;
  206. u32 *busy_buf;
  207. u32 *cmd;
  208. u32 chunk_len;
  209. while (len > 0) {
  210. chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
  211. cmd = &wl->buffer_cmd;
  212. busy_buf = wl->buffer_busyword;
  213. *cmd = 0;
  214. *cmd |= WSPI_CMD_READ;
  215. *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
  216. WSPI_CMD_BYTE_LENGTH;
  217. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  218. if (fixed)
  219. *cmd |= WSPI_CMD_FIXED;
  220. spi_message_init(&m);
  221. memset(t, 0, sizeof(t));
  222. t[0].tx_buf = cmd;
  223. t[0].len = 4;
  224. t[0].cs_change = true;
  225. spi_message_add_tail(&t[0], &m);
  226. /* Busy and non busy words read */
  227. t[1].rx_buf = busy_buf;
  228. t[1].len = WL1271_BUSY_WORD_LEN;
  229. t[1].cs_change = true;
  230. spi_message_add_tail(&t[1], &m);
  231. spi_sync(to_spi_device(glue->dev), &m);
  232. if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
  233. wl12xx_spi_read_busy(child)) {
  234. memset(buf, 0, chunk_len);
  235. return 0;
  236. }
  237. spi_message_init(&m);
  238. memset(t, 0, sizeof(t));
  239. t[0].rx_buf = buf;
  240. t[0].len = chunk_len;
  241. t[0].cs_change = true;
  242. spi_message_add_tail(&t[0], &m);
  243. spi_sync(to_spi_device(glue->dev), &m);
  244. if (!fixed)
  245. addr += chunk_len;
  246. buf += chunk_len;
  247. len -= chunk_len;
  248. }
  249. return 0;
  250. }
  251. static int __wl12xx_spi_raw_write(struct device *child, int addr,
  252. void *buf, size_t len, bool fixed)
  253. {
  254. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  255. struct spi_transfer *t;
  256. struct spi_message m;
  257. u32 commands[WSPI_MAX_NUM_OF_CHUNKS]; /* 1 command per chunk */
  258. u32 *cmd;
  259. u32 chunk_len;
  260. int i;
  261. /* SPI write buffers - 2 for each chunk */
  262. t = kzalloc(sizeof(*t) * 2 * WSPI_MAX_NUM_OF_CHUNKS, GFP_KERNEL);
  263. if (!t)
  264. return -ENOMEM;
  265. WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
  266. spi_message_init(&m);
  267. cmd = &commands[0];
  268. i = 0;
  269. while (len > 0) {
  270. chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
  271. *cmd = 0;
  272. *cmd |= WSPI_CMD_WRITE;
  273. *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
  274. WSPI_CMD_BYTE_LENGTH;
  275. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  276. if (fixed)
  277. *cmd |= WSPI_CMD_FIXED;
  278. t[i].tx_buf = cmd;
  279. t[i].len = sizeof(*cmd);
  280. spi_message_add_tail(&t[i++], &m);
  281. t[i].tx_buf = buf;
  282. t[i].len = chunk_len;
  283. spi_message_add_tail(&t[i++], &m);
  284. if (!fixed)
  285. addr += chunk_len;
  286. buf += chunk_len;
  287. len -= chunk_len;
  288. cmd++;
  289. }
  290. spi_sync(to_spi_device(glue->dev), &m);
  291. kfree(t);
  292. return 0;
  293. }
  294. static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
  295. void *buf, size_t len, bool fixed)
  296. {
  297. int ret;
  298. /* The ELP wakeup write may fail the first time due to internal
  299. * hardware latency. It is safer to send the wakeup command twice to
  300. * avoid unexpected failures.
  301. */
  302. if (addr == HW_ACCESS_ELP_CTRL_REG)
  303. ret = __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
  304. ret = __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
  305. return ret;
  306. }
  307. /**
  308. * wl12xx_spi_set_power - power on/off the wl12xx unit
  309. * @child: wl12xx device handle.
  310. * @enable: true/false to power on/off the unit.
  311. *
  312. * use the WiFi enable regulator to enable/disable the WiFi unit.
  313. */
  314. static int wl12xx_spi_set_power(struct device *child, bool enable)
  315. {
  316. int ret = 0;
  317. struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
  318. WARN_ON(!glue->reg);
  319. /* Update regulator state */
  320. if (enable) {
  321. ret = regulator_enable(glue->reg);
  322. if (ret)
  323. dev_err(child, "Power enable failure\n");
  324. } else {
  325. ret = regulator_disable(glue->reg);
  326. if (ret)
  327. dev_err(child, "Power disable failure\n");
  328. }
  329. return ret;
  330. }
  331. /**
  332. * wl12xx_spi_set_block_size
  333. *
  334. * This function is not needed for spi mode, but need to be present.
  335. * Without it defined the wlcore fallback to use the wrong packet
  336. * allignment on tx.
  337. */
  338. static void wl12xx_spi_set_block_size(struct device *child,
  339. unsigned int blksz)
  340. {
  341. }
  342. static struct wl1271_if_operations spi_ops = {
  343. .read = wl12xx_spi_raw_read,
  344. .write = wl12xx_spi_raw_write,
  345. .reset = wl12xx_spi_reset,
  346. .init = wl12xx_spi_init,
  347. .power = wl12xx_spi_set_power,
  348. .set_block_size = wl12xx_spi_set_block_size,
  349. };
  350. static const struct of_device_id wlcore_spi_of_match_table[] = {
  351. { .compatible = "ti,wl1271", .data = &wl12xx_data},
  352. { .compatible = "ti,wl1273", .data = &wl12xx_data},
  353. { .compatible = "ti,wl1281", .data = &wl12xx_data},
  354. { .compatible = "ti,wl1283", .data = &wl12xx_data},
  355. { .compatible = "ti,wl1801", .data = &wl18xx_data},
  356. { .compatible = "ti,wl1805", .data = &wl18xx_data},
  357. { .compatible = "ti,wl1807", .data = &wl18xx_data},
  358. { .compatible = "ti,wl1831", .data = &wl18xx_data},
  359. { .compatible = "ti,wl1835", .data = &wl18xx_data},
  360. { .compatible = "ti,wl1837", .data = &wl18xx_data},
  361. { }
  362. };
  363. MODULE_DEVICE_TABLE(of, wlcore_spi_of_match_table);
  364. /**
  365. * wlcore_probe_of - DT node parsing.
  366. * @spi: SPI slave device parameters.
  367. * @res: resource parameters.
  368. * @glue: wl12xx SPI bus to slave device glue parameters.
  369. * @pdev_data: wlcore device parameters
  370. */
  371. static int wlcore_probe_of(struct spi_device *spi, struct wl12xx_spi_glue *glue,
  372. struct wlcore_platdev_data *pdev_data)
  373. {
  374. struct device_node *dt_node = spi->dev.of_node;
  375. const struct of_device_id *of_id;
  376. of_id = of_match_node(wlcore_spi_of_match_table, dt_node);
  377. if (!of_id)
  378. return -ENODEV;
  379. wilink_data = of_id->data;
  380. dev_info(&spi->dev, "selected chip familiy is %s\n",
  381. wilink_data->name);
  382. if (of_find_property(dt_node, "clock-xtal", NULL))
  383. pdev_data->ref_clock_xtal = true;
  384. /* optional clock frequency params */
  385. of_property_read_u32(dt_node, "ref-clock-frequency",
  386. &pdev_data->ref_clock_freq);
  387. of_property_read_u32(dt_node, "tcxo-clock-frequency",
  388. &pdev_data->tcxo_clock_freq);
  389. return 0;
  390. }
  391. static int wl1271_probe(struct spi_device *spi)
  392. {
  393. struct wl12xx_spi_glue *glue;
  394. struct wlcore_platdev_data pdev_data;
  395. struct resource res[1];
  396. int ret;
  397. memset(&pdev_data, 0x00, sizeof(pdev_data));
  398. pdev_data.if_ops = &spi_ops;
  399. glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL);
  400. if (!glue) {
  401. dev_err(&spi->dev, "can't allocate glue\n");
  402. return -ENOMEM;
  403. }
  404. glue->dev = &spi->dev;
  405. spi_set_drvdata(spi, glue);
  406. /* This is the only SPI value that we need to set here, the rest
  407. * comes from the board-peripherals file */
  408. spi->bits_per_word = 32;
  409. glue->reg = devm_regulator_get(&spi->dev, "vwlan");
  410. if (PTR_ERR(glue->reg) == -EPROBE_DEFER)
  411. return -EPROBE_DEFER;
  412. if (IS_ERR(glue->reg)) {
  413. dev_err(glue->dev, "can't get regulator\n");
  414. return PTR_ERR(glue->reg);
  415. }
  416. ret = wlcore_probe_of(spi, glue, &pdev_data);
  417. if (ret) {
  418. dev_err(glue->dev,
  419. "can't get device tree parameters (%d)\n", ret);
  420. return ret;
  421. }
  422. ret = spi_setup(spi);
  423. if (ret < 0) {
  424. dev_err(glue->dev, "spi_setup failed\n");
  425. return ret;
  426. }
  427. glue->core = platform_device_alloc(wilink_data->name,
  428. PLATFORM_DEVID_AUTO);
  429. if (!glue->core) {
  430. dev_err(glue->dev, "can't allocate platform_device\n");
  431. return -ENOMEM;
  432. }
  433. glue->core->dev.parent = &spi->dev;
  434. memset(res, 0x00, sizeof(res));
  435. res[0].start = spi->irq;
  436. res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(spi->irq);
  437. res[0].name = "irq";
  438. ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
  439. if (ret) {
  440. dev_err(glue->dev, "can't add resources\n");
  441. goto out_dev_put;
  442. }
  443. ret = platform_device_add_data(glue->core, &pdev_data,
  444. sizeof(pdev_data));
  445. if (ret) {
  446. dev_err(glue->dev, "can't add platform data\n");
  447. goto out_dev_put;
  448. }
  449. ret = platform_device_add(glue->core);
  450. if (ret) {
  451. dev_err(glue->dev, "can't register platform device\n");
  452. goto out_dev_put;
  453. }
  454. return 0;
  455. out_dev_put:
  456. platform_device_put(glue->core);
  457. return ret;
  458. }
  459. static int wl1271_remove(struct spi_device *spi)
  460. {
  461. struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
  462. platform_device_unregister(glue->core);
  463. return 0;
  464. }
  465. static struct spi_driver wl1271_spi_driver = {
  466. .driver = {
  467. .name = "wl1271_spi",
  468. .of_match_table = of_match_ptr(wlcore_spi_of_match_table),
  469. },
  470. .probe = wl1271_probe,
  471. .remove = wl1271_remove,
  472. };
  473. module_spi_driver(wl1271_spi_driver);
  474. MODULE_LICENSE("GPL");
  475. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  476. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
  477. MODULE_ALIAS("spi:wl1271");