1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of wl1271
5 * Copyright (C) 2008-2009 Nokia Corporation
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/swab.h>
15 #include <linux/crc7.h>
16 #include <linux/spi/spi.h>
17 #include <linux/platform_device.h>
18 #include <linux/of_irq.h>
19 #include <linux/regulator/consumer.h>
22 #include "wl12xx_80211.h"
25 #define WSPI_CMD_READ 0x40000000
26 #define WSPI_CMD_WRITE 0x00000000
27 #define WSPI_CMD_FIXED 0x20000000
28 #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
29 #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
30 #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
32 #define WSPI_INIT_CMD_CRC_LEN 5
34 #define WSPI_INIT_CMD_START 0x00
35 #define WSPI_INIT_CMD_TX 0x40
36 /* the extra bypass bit is sampled by the TNET as '1' */
37 #define WSPI_INIT_CMD_BYPASS_BIT 0x80
38 #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
39 #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
40 #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
41 #define WSPI_INIT_CMD_IOD 0x40
42 #define WSPI_INIT_CMD_IP 0x20
43 #define WSPI_INIT_CMD_CS 0x10
44 #define WSPI_INIT_CMD_WS 0x08
45 #define WSPI_INIT_CMD_WSPI 0x01
46 #define WSPI_INIT_CMD_END 0x01
48 #define WSPI_INIT_CMD_LEN 8
50 #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
51 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
52 #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
54 /* HW limitation: maximum possible chunk size is 4095 bytes */
55 #define WSPI_MAX_CHUNK_SIZE 4092
58 * wl18xx driver aggregation buffer size is (13 * 4K) compared to
59 * (4 * 4K) for wl12xx, so use the larger buffer needed for wl18xx
61 #define SPI_AGGR_BUFFER_SIZE (13 * SZ_4K)
63 /* Maximum number of SPI write chunks */
64 #define WSPI_MAX_NUM_OF_CHUNKS \
65 ((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
67 static const struct wilink_family_data wl127x_data = {
69 .nvs_name = "ti-connectivity/wl127x-nvs.bin",
72 static const struct wilink_family_data wl128x_data = {
74 .nvs_name = "ti-connectivity/wl128x-nvs.bin",
77 static const struct wilink_family_data wl18xx_data = {
79 .cfg_name = "ti-connectivity/wl18xx-conf.bin",
80 .nvs_name = "ti-connectivity/wl1271-nvs.bin",
83 struct wl12xx_spi_glue {
85 struct platform_device *core;
86 struct regulator *reg; /* Power regulator */
89 static void wl12xx_spi_reset(struct device *child)
91 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
93 struct spi_transfer t;
96 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
98 dev_err(child->parent,
99 "could not allocate cmd for spi reset\n");
103 memset(&t, 0, sizeof(t));
104 spi_message_init(&m);
106 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
109 t.len = WSPI_INIT_CMD_LEN;
110 spi_message_add_tail(&t, &m);
112 spi_sync(to_spi_device(glue->dev), &m);
117 static void wl12xx_spi_init(struct device *child)
119 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
120 struct spi_transfer t;
121 struct spi_message m;
122 struct spi_device *spi = to_spi_device(glue->dev);
123 u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
126 dev_err(child->parent,
127 "could not allocate cmd for spi init\n");
131 memset(&t, 0, sizeof(t));
132 spi_message_init(&m);
135 * Set WSPI_INIT_COMMAND
136 * the data is being send from the MSB to LSB
140 cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
143 cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
144 cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
146 cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
147 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
149 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
150 cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
152 cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
154 cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
157 * The above is the logical order; it must actually be stored
158 * in the buffer byte-swapped.
160 __swab32s((u32 *)cmd);
161 __swab32s((u32 *)cmd+1);
164 t.len = WSPI_INIT_CMD_LEN;
165 spi_message_add_tail(&t, &m);
167 spi_sync(to_spi_device(glue->dev), &m);
169 /* Send extra clocks with inverted CS (high). this is required
170 * by the wilink family in order to successfully enter WSPI mode.
172 spi->mode ^= SPI_CS_HIGH;
173 memset(&m, 0, sizeof(m));
174 spi_message_init(&m);
180 __swab32s((u32 *)cmd);
184 spi_message_add_tail(&t, &m);
186 spi_sync(to_spi_device(glue->dev), &m);
188 /* Restore chip select configuration to normal */
189 spi->mode ^= SPI_CS_HIGH;
193 #define WL1271_BUSY_WORD_TIMEOUT 1000
195 static int wl12xx_spi_read_busy(struct device *child)
197 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
198 struct wl1271 *wl = dev_get_drvdata(child);
199 struct spi_transfer t[1];
200 struct spi_message m;
202 int num_busy_bytes = 0;
205 * Read further busy words from SPI until a non-busy word is
206 * encountered, then read the data itself into the buffer.
209 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
210 busy_buf = wl->buffer_busyword;
211 while (num_busy_bytes) {
213 spi_message_init(&m);
214 memset(t, 0, sizeof(t));
215 t[0].rx_buf = busy_buf;
216 t[0].len = sizeof(u32);
217 t[0].cs_change = true;
218 spi_message_add_tail(&t[0], &m);
219 spi_sync(to_spi_device(glue->dev), &m);
225 /* The SPI bus is unresponsive, the read failed. */
226 dev_err(child->parent, "SPI read busy-word timeout!\n");
230 static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
231 void *buf, size_t len, bool fixed)
233 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
234 struct wl1271 *wl = dev_get_drvdata(child);
235 struct spi_transfer t[2];
236 struct spi_message m;
242 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
244 cmd = &wl->buffer_cmd;
245 busy_buf = wl->buffer_busyword;
248 *cmd |= WSPI_CMD_READ;
249 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
250 WSPI_CMD_BYTE_LENGTH;
251 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
254 *cmd |= WSPI_CMD_FIXED;
256 spi_message_init(&m);
257 memset(t, 0, sizeof(t));
261 t[0].cs_change = true;
262 spi_message_add_tail(&t[0], &m);
264 /* Busy and non busy words read */
265 t[1].rx_buf = busy_buf;
266 t[1].len = WL1271_BUSY_WORD_LEN;
267 t[1].cs_change = true;
268 spi_message_add_tail(&t[1], &m);
270 spi_sync(to_spi_device(glue->dev), &m);
272 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
273 wl12xx_spi_read_busy(child)) {
274 memset(buf, 0, chunk_len);
278 spi_message_init(&m);
279 memset(t, 0, sizeof(t));
282 t[0].len = chunk_len;
283 t[0].cs_change = true;
284 spi_message_add_tail(&t[0], &m);
286 spi_sync(to_spi_device(glue->dev), &m);
297 static int __wl12xx_spi_raw_write(struct device *child, int addr,
298 void *buf, size_t len, bool fixed)
300 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
301 struct spi_transfer *t;
302 struct spi_message m;
303 u32 commands[WSPI_MAX_NUM_OF_CHUNKS]; /* 1 command per chunk */
308 /* SPI write buffers - 2 for each chunk */
309 t = kzalloc(sizeof(*t) * 2 * WSPI_MAX_NUM_OF_CHUNKS, GFP_KERNEL);
313 WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
315 spi_message_init(&m);
320 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
323 *cmd |= WSPI_CMD_WRITE;
324 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
325 WSPI_CMD_BYTE_LENGTH;
326 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
329 *cmd |= WSPI_CMD_FIXED;
332 t[i].len = sizeof(*cmd);
333 spi_message_add_tail(&t[i++], &m);
336 t[i].len = chunk_len;
337 spi_message_add_tail(&t[i++], &m);
346 spi_sync(to_spi_device(glue->dev), &m);
352 static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
353 void *buf, size_t len, bool fixed)
355 /* The ELP wakeup write may fail the first time due to internal
356 * hardware latency. It is safer to send the wakeup command twice to
357 * avoid unexpected failures.
359 if (addr == HW_ACCESS_ELP_CTRL_REG)
360 __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
362 return __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
366 * wl12xx_spi_set_power - power on/off the wl12xx unit
367 * @child: wl12xx device handle.
368 * @enable: true/false to power on/off the unit.
370 * use the WiFi enable regulator to enable/disable the WiFi unit.
372 static int wl12xx_spi_set_power(struct device *child, bool enable)
375 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
379 /* Update regulator state */
381 ret = regulator_enable(glue->reg);
383 dev_err(child, "Power enable failure\n");
385 ret = regulator_disable(glue->reg);
387 dev_err(child, "Power disable failure\n");
394 * wl12xx_spi_set_block_size
396 * This function is not needed for spi mode, but need to be present.
397 * Without it defined the wlcore fallback to use the wrong packet
400 static void wl12xx_spi_set_block_size(struct device *child,
405 static struct wl1271_if_operations spi_ops = {
406 .read = wl12xx_spi_raw_read,
407 .write = wl12xx_spi_raw_write,
408 .reset = wl12xx_spi_reset,
409 .init = wl12xx_spi_init,
410 .power = wl12xx_spi_set_power,
411 .set_block_size = wl12xx_spi_set_block_size,
414 static const struct of_device_id wlcore_spi_of_match_table[] = {
415 { .compatible = "ti,wl1271", .data = &wl127x_data},
416 { .compatible = "ti,wl1273", .data = &wl127x_data},
417 { .compatible = "ti,wl1281", .data = &wl128x_data},
418 { .compatible = "ti,wl1283", .data = &wl128x_data},
419 { .compatible = "ti,wl1285", .data = &wl128x_data},
420 { .compatible = "ti,wl1801", .data = &wl18xx_data},
421 { .compatible = "ti,wl1805", .data = &wl18xx_data},
422 { .compatible = "ti,wl1807", .data = &wl18xx_data},
423 { .compatible = "ti,wl1831", .data = &wl18xx_data},
424 { .compatible = "ti,wl1835", .data = &wl18xx_data},
425 { .compatible = "ti,wl1837", .data = &wl18xx_data},
428 MODULE_DEVICE_TABLE(of, wlcore_spi_of_match_table);
431 * wlcore_probe_of - DT node parsing.
432 * @spi: SPI slave device parameters.
433 * @glue: wl12xx SPI bus to slave device glue parameters.
434 * @pdev_data: wlcore device parameters
436 static int wlcore_probe_of(struct spi_device *spi, struct wl12xx_spi_glue *glue,
437 struct wlcore_platdev_data *pdev_data)
439 struct device_node *dt_node = spi->dev.of_node;
440 const struct of_device_id *of_id;
442 of_id = of_match_node(wlcore_spi_of_match_table, dt_node);
446 pdev_data->family = of_id->data;
447 dev_info(&spi->dev, "selected chip family is %s\n",
448 pdev_data->family->name);
450 pdev_data->ref_clock_xtal = of_property_read_bool(dt_node, "clock-xtal");
452 /* optional clock frequency params */
453 of_property_read_u32(dt_node, "ref-clock-frequency",
454 &pdev_data->ref_clock_freq);
455 of_property_read_u32(dt_node, "tcxo-clock-frequency",
456 &pdev_data->tcxo_clock_freq);
461 static int wl1271_probe(struct spi_device *spi)
463 struct wl12xx_spi_glue *glue;
464 struct wlcore_platdev_data *pdev_data;
465 struct resource res[1];
468 pdev_data = devm_kzalloc(&spi->dev, sizeof(*pdev_data), GFP_KERNEL);
472 pdev_data->if_ops = &spi_ops;
474 glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL);
476 dev_err(&spi->dev, "can't allocate glue\n");
480 glue->dev = &spi->dev;
482 spi_set_drvdata(spi, glue);
484 /* This is the only SPI value that we need to set here, the rest
485 * comes from the board-peripherals file */
486 spi->bits_per_word = 32;
488 glue->reg = devm_regulator_get(&spi->dev, "vwlan");
489 if (IS_ERR(glue->reg))
490 return dev_err_probe(glue->dev, PTR_ERR(glue->reg),
491 "can't get regulator\n");
493 ret = wlcore_probe_of(spi, glue, pdev_data);
496 "can't get device tree parameters (%d)\n", ret);
500 ret = spi_setup(spi);
502 dev_err(glue->dev, "spi_setup failed\n");
506 glue->core = platform_device_alloc(pdev_data->family->name,
507 PLATFORM_DEVID_AUTO);
509 dev_err(glue->dev, "can't allocate platform_device\n");
513 glue->core->dev.parent = &spi->dev;
515 memset(res, 0x00, sizeof(res));
517 res[0].start = spi->irq;
518 res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(spi->irq);
521 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
523 dev_err(glue->dev, "can't add resources\n");
527 ret = platform_device_add_data(glue->core, pdev_data,
530 dev_err(glue->dev, "can't add platform data\n");
534 ret = platform_device_add(glue->core);
536 dev_err(glue->dev, "can't register platform device\n");
543 platform_device_put(glue->core);
547 static void wl1271_remove(struct spi_device *spi)
549 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
551 platform_device_unregister(glue->core);
554 static struct spi_driver wl1271_spi_driver = {
556 .name = "wl1271_spi",
557 .of_match_table = wlcore_spi_of_match_table,
560 .probe = wl1271_probe,
561 .remove = wl1271_remove,
564 module_spi_driver(wl1271_spi_driver);
565 MODULE_DESCRIPTION("TI WLAN SPI helpers");
566 MODULE_LICENSE("GPL");
569 MODULE_ALIAS("spi:wl1271");