spi.c 14 KB

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