1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of wl1271
5 * Copyright (C) 2008-2009 Nokia Corporation
7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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/wl12xx.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_irq.h>
20 #include <linux/regulator/consumer.h>
23 #include "wl12xx_80211.h"
26 #define WSPI_CMD_READ 0x40000000
27 #define WSPI_CMD_WRITE 0x00000000
28 #define WSPI_CMD_FIXED 0x20000000
29 #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
30 #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
31 #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
33 #define WSPI_INIT_CMD_CRC_LEN 5
35 #define WSPI_INIT_CMD_START 0x00
36 #define WSPI_INIT_CMD_TX 0x40
37 /* the extra bypass bit is sampled by the TNET as '1' */
38 #define WSPI_INIT_CMD_BYPASS_BIT 0x80
39 #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
40 #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
41 #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
42 #define WSPI_INIT_CMD_IOD 0x40
43 #define WSPI_INIT_CMD_IP 0x20
44 #define WSPI_INIT_CMD_CS 0x10
45 #define WSPI_INIT_CMD_WS 0x08
46 #define WSPI_INIT_CMD_WSPI 0x01
47 #define WSPI_INIT_CMD_END 0x01
49 #define WSPI_INIT_CMD_LEN 8
51 #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
52 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
53 #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
55 /* HW limitation: maximum possible chunk size is 4095 bytes */
56 #define WSPI_MAX_CHUNK_SIZE 4092
59 * wl18xx driver aggregation buffer size is (13 * 4K) compared to
60 * (4 * 4K) for wl12xx, so use the larger buffer needed for wl18xx
62 #define SPI_AGGR_BUFFER_SIZE (13 * SZ_4K)
64 /* Maximum number of SPI write chunks */
65 #define WSPI_MAX_NUM_OF_CHUNKS \
66 ((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
68 static const struct wilink_family_data wl127x_data
= {
70 .nvs_name
= "ti-connectivity/wl127x-nvs.bin",
73 static const struct wilink_family_data wl128x_data
= {
75 .nvs_name
= "ti-connectivity/wl128x-nvs.bin",
78 static const struct wilink_family_data wl18xx_data
= {
80 .cfg_name
= "ti-connectivity/wl18xx-conf.bin",
81 .nvs_name
= "ti-connectivity/wl1271-nvs.bin",
84 struct wl12xx_spi_glue
{
86 struct platform_device
*core
;
87 struct regulator
*reg
; /* Power regulator */
90 static void wl12xx_spi_reset(struct device
*child
)
92 struct wl12xx_spi_glue
*glue
= dev_get_drvdata(child
->parent
);
94 struct spi_transfer t
;
97 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
99 dev_err(child
->parent
,
100 "could not allocate cmd for spi reset\n");
104 memset(&t
, 0, sizeof(t
));
105 spi_message_init(&m
);
107 memset(cmd
, 0xff, WSPI_INIT_CMD_LEN
);
110 t
.len
= WSPI_INIT_CMD_LEN
;
111 spi_message_add_tail(&t
, &m
);
113 spi_sync(to_spi_device(glue
->dev
), &m
);
118 static void wl12xx_spi_init(struct device
*child
)
120 struct wl12xx_spi_glue
*glue
= dev_get_drvdata(child
->parent
);
121 struct spi_transfer t
;
122 struct spi_message m
;
123 struct spi_device
*spi
= to_spi_device(glue
->dev
);
124 u8
*cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
127 dev_err(child
->parent
,
128 "could not allocate cmd for spi init\n");
132 memset(&t
, 0, sizeof(t
));
133 spi_message_init(&m
);
136 * Set WSPI_INIT_COMMAND
137 * the data is being send from the MSB to LSB
141 cmd
[2] = WSPI_INIT_CMD_START
| WSPI_INIT_CMD_TX
;
144 cmd
[5] = HW_ACCESS_WSPI_INIT_CMD_MASK
<< 3;
145 cmd
[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN
& WSPI_INIT_CMD_FIXEDBUSY_LEN
;
147 cmd
[6] = WSPI_INIT_CMD_IOD
| WSPI_INIT_CMD_IP
| WSPI_INIT_CMD_CS
148 | WSPI_INIT_CMD_WSPI
| WSPI_INIT_CMD_WS
;
150 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN
== 0)
151 cmd
[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY
;
153 cmd
[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY
;
155 cmd
[7] = crc7_be(0, cmd
+2, WSPI_INIT_CMD_CRC_LEN
) | WSPI_INIT_CMD_END
;
158 * The above is the logical order; it must actually be stored
159 * in the buffer byte-swapped.
161 __swab32s((u32
*)cmd
);
162 __swab32s((u32
*)cmd
+1);
165 t
.len
= WSPI_INIT_CMD_LEN
;
166 spi_message_add_tail(&t
, &m
);
168 spi_sync(to_spi_device(glue
->dev
), &m
);
170 /* Send extra clocks with inverted CS (high). this is required
171 * by the wilink family in order to successfully enter WSPI mode.
173 spi
->mode
^= SPI_CS_HIGH
;
174 memset(&m
, 0, sizeof(m
));
175 spi_message_init(&m
);
181 __swab32s((u32
*)cmd
);
185 spi_message_add_tail(&t
, &m
);
187 spi_sync(to_spi_device(glue
->dev
), &m
);
189 /* Restore chip select configuration to normal */
190 spi
->mode
^= SPI_CS_HIGH
;
194 #define WL1271_BUSY_WORD_TIMEOUT 1000
196 static int wl12xx_spi_read_busy(struct device
*child
)
198 struct wl12xx_spi_glue
*glue
= dev_get_drvdata(child
->parent
);
199 struct wl1271
*wl
= dev_get_drvdata(child
);
200 struct spi_transfer t
[1];
201 struct spi_message m
;
203 int num_busy_bytes
= 0;
206 * Read further busy words from SPI until a non-busy word is
207 * encountered, then read the data itself into the buffer.
210 num_busy_bytes
= WL1271_BUSY_WORD_TIMEOUT
;
211 busy_buf
= wl
->buffer_busyword
;
212 while (num_busy_bytes
) {
214 spi_message_init(&m
);
215 memset(t
, 0, sizeof(t
));
216 t
[0].rx_buf
= busy_buf
;
217 t
[0].len
= sizeof(u32
);
218 t
[0].cs_change
= true;
219 spi_message_add_tail(&t
[0], &m
);
220 spi_sync(to_spi_device(glue
->dev
), &m
);
226 /* The SPI bus is unresponsive, the read failed. */
227 dev_err(child
->parent
, "SPI read busy-word timeout!\n");
231 static int __must_check
wl12xx_spi_raw_read(struct device
*child
, int addr
,
232 void *buf
, size_t len
, bool fixed
)
234 struct wl12xx_spi_glue
*glue
= dev_get_drvdata(child
->parent
);
235 struct wl1271
*wl
= dev_get_drvdata(child
);
236 struct spi_transfer t
[2];
237 struct spi_message m
;
243 chunk_len
= min_t(size_t, WSPI_MAX_CHUNK_SIZE
, len
);
245 cmd
= &wl
->buffer_cmd
;
246 busy_buf
= wl
->buffer_busyword
;
249 *cmd
|= WSPI_CMD_READ
;
250 *cmd
|= (chunk_len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) &
251 WSPI_CMD_BYTE_LENGTH
;
252 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
255 *cmd
|= WSPI_CMD_FIXED
;
257 spi_message_init(&m
);
258 memset(t
, 0, sizeof(t
));
262 t
[0].cs_change
= true;
263 spi_message_add_tail(&t
[0], &m
);
265 /* Busy and non busy words read */
266 t
[1].rx_buf
= busy_buf
;
267 t
[1].len
= WL1271_BUSY_WORD_LEN
;
268 t
[1].cs_change
= true;
269 spi_message_add_tail(&t
[1], &m
);
271 spi_sync(to_spi_device(glue
->dev
), &m
);
273 if (!(busy_buf
[WL1271_BUSY_WORD_CNT
- 1] & 0x1) &&
274 wl12xx_spi_read_busy(child
)) {
275 memset(buf
, 0, chunk_len
);
279 spi_message_init(&m
);
280 memset(t
, 0, sizeof(t
));
283 t
[0].len
= chunk_len
;
284 t
[0].cs_change
= true;
285 spi_message_add_tail(&t
[0], &m
);
287 spi_sync(to_spi_device(glue
->dev
), &m
);
298 static int __wl12xx_spi_raw_write(struct device
*child
, int addr
,
299 void *buf
, size_t len
, bool fixed
)
301 struct wl12xx_spi_glue
*glue
= dev_get_drvdata(child
->parent
);
302 struct spi_transfer
*t
;
303 struct spi_message m
;
304 u32 commands
[WSPI_MAX_NUM_OF_CHUNKS
]; /* 1 command per chunk */
309 /* SPI write buffers - 2 for each chunk */
310 t
= kzalloc(sizeof(*t
) * 2 * WSPI_MAX_NUM_OF_CHUNKS
, GFP_KERNEL
);
314 WARN_ON(len
> SPI_AGGR_BUFFER_SIZE
);
316 spi_message_init(&m
);
321 chunk_len
= min_t(size_t, WSPI_MAX_CHUNK_SIZE
, len
);
324 *cmd
|= WSPI_CMD_WRITE
;
325 *cmd
|= (chunk_len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) &
326 WSPI_CMD_BYTE_LENGTH
;
327 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
330 *cmd
|= WSPI_CMD_FIXED
;
333 t
[i
].len
= sizeof(*cmd
);
334 spi_message_add_tail(&t
[i
++], &m
);
337 t
[i
].len
= chunk_len
;
338 spi_message_add_tail(&t
[i
++], &m
);
347 spi_sync(to_spi_device(glue
->dev
), &m
);
353 static int __must_check
wl12xx_spi_raw_write(struct device
*child
, int addr
,
354 void *buf
, size_t len
, bool fixed
)
356 /* The ELP wakeup write may fail the first time due to internal
357 * hardware latency. It is safer to send the wakeup command twice to
358 * avoid unexpected failures.
360 if (addr
== HW_ACCESS_ELP_CTRL_REG
)
361 __wl12xx_spi_raw_write(child
, addr
, buf
, len
, fixed
);
363 return __wl12xx_spi_raw_write(child
, addr
, buf
, len
, fixed
);
367 * wl12xx_spi_set_power - power on/off the wl12xx unit
368 * @child: wl12xx device handle.
369 * @enable: true/false to power on/off the unit.
371 * use the WiFi enable regulator to enable/disable the WiFi unit.
373 static int wl12xx_spi_set_power(struct device
*child
, bool enable
)
376 struct wl12xx_spi_glue
*glue
= dev_get_drvdata(child
->parent
);
380 /* Update regulator state */
382 ret
= regulator_enable(glue
->reg
);
384 dev_err(child
, "Power enable failure\n");
386 ret
= regulator_disable(glue
->reg
);
388 dev_err(child
, "Power disable failure\n");
395 * wl12xx_spi_set_block_size
397 * This function is not needed for spi mode, but need to be present.
398 * Without it defined the wlcore fallback to use the wrong packet
401 static void wl12xx_spi_set_block_size(struct device
*child
,
406 static struct wl1271_if_operations spi_ops
= {
407 .read
= wl12xx_spi_raw_read
,
408 .write
= wl12xx_spi_raw_write
,
409 .reset
= wl12xx_spi_reset
,
410 .init
= wl12xx_spi_init
,
411 .power
= wl12xx_spi_set_power
,
412 .set_block_size
= wl12xx_spi_set_block_size
,
415 static const struct of_device_id wlcore_spi_of_match_table
[] = {
416 { .compatible
= "ti,wl1271", .data
= &wl127x_data
},
417 { .compatible
= "ti,wl1273", .data
= &wl127x_data
},
418 { .compatible
= "ti,wl1281", .data
= &wl128x_data
},
419 { .compatible
= "ti,wl1283", .data
= &wl128x_data
},
420 { .compatible
= "ti,wl1285", .data
= &wl128x_data
},
421 { .compatible
= "ti,wl1801", .data
= &wl18xx_data
},
422 { .compatible
= "ti,wl1805", .data
= &wl18xx_data
},
423 { .compatible
= "ti,wl1807", .data
= &wl18xx_data
},
424 { .compatible
= "ti,wl1831", .data
= &wl18xx_data
},
425 { .compatible
= "ti,wl1835", .data
= &wl18xx_data
},
426 { .compatible
= "ti,wl1837", .data
= &wl18xx_data
},
429 MODULE_DEVICE_TABLE(of
, wlcore_spi_of_match_table
);
432 * wlcore_probe_of - DT node parsing.
433 * @spi: SPI slave device parameters.
434 * @res: resource parameters.
435 * @glue: wl12xx SPI bus to slave device glue parameters.
436 * @pdev_data: wlcore device parameters
438 static int wlcore_probe_of(struct spi_device
*spi
, struct wl12xx_spi_glue
*glue
,
439 struct wlcore_platdev_data
*pdev_data
)
441 struct device_node
*dt_node
= spi
->dev
.of_node
;
442 const struct of_device_id
*of_id
;
444 of_id
= of_match_node(wlcore_spi_of_match_table
, dt_node
);
448 pdev_data
->family
= of_id
->data
;
449 dev_info(&spi
->dev
, "selected chip family is %s\n",
450 pdev_data
->family
->name
);
452 if (of_find_property(dt_node
, "clock-xtal", NULL
))
453 pdev_data
->ref_clock_xtal
= true;
455 /* optional clock frequency params */
456 of_property_read_u32(dt_node
, "ref-clock-frequency",
457 &pdev_data
->ref_clock_freq
);
458 of_property_read_u32(dt_node
, "tcxo-clock-frequency",
459 &pdev_data
->tcxo_clock_freq
);
464 static int wl1271_probe(struct spi_device
*spi
)
466 struct wl12xx_spi_glue
*glue
;
467 struct wlcore_platdev_data
*pdev_data
;
468 struct resource res
[1];
471 pdev_data
= devm_kzalloc(&spi
->dev
, sizeof(*pdev_data
), GFP_KERNEL
);
475 pdev_data
->if_ops
= &spi_ops
;
477 glue
= devm_kzalloc(&spi
->dev
, sizeof(*glue
), GFP_KERNEL
);
479 dev_err(&spi
->dev
, "can't allocate glue\n");
483 glue
->dev
= &spi
->dev
;
485 spi_set_drvdata(spi
, glue
);
487 /* This is the only SPI value that we need to set here, the rest
488 * comes from the board-peripherals file */
489 spi
->bits_per_word
= 32;
491 glue
->reg
= devm_regulator_get(&spi
->dev
, "vwlan");
492 if (PTR_ERR(glue
->reg
) == -EPROBE_DEFER
)
493 return -EPROBE_DEFER
;
494 if (IS_ERR(glue
->reg
)) {
495 dev_err(glue
->dev
, "can't get regulator\n");
496 return PTR_ERR(glue
->reg
);
499 ret
= wlcore_probe_of(spi
, glue
, pdev_data
);
502 "can't get device tree parameters (%d)\n", ret
);
506 ret
= spi_setup(spi
);
508 dev_err(glue
->dev
, "spi_setup failed\n");
512 glue
->core
= platform_device_alloc(pdev_data
->family
->name
,
513 PLATFORM_DEVID_AUTO
);
515 dev_err(glue
->dev
, "can't allocate platform_device\n");
519 glue
->core
->dev
.parent
= &spi
->dev
;
521 memset(res
, 0x00, sizeof(res
));
523 res
[0].start
= spi
->irq
;
524 res
[0].flags
= IORESOURCE_IRQ
| irq_get_trigger_type(spi
->irq
);
527 ret
= platform_device_add_resources(glue
->core
, res
, ARRAY_SIZE(res
));
529 dev_err(glue
->dev
, "can't add resources\n");
533 ret
= platform_device_add_data(glue
->core
, pdev_data
,
536 dev_err(glue
->dev
, "can't add platform data\n");
540 ret
= platform_device_add(glue
->core
);
542 dev_err(glue
->dev
, "can't register platform device\n");
549 platform_device_put(glue
->core
);
553 static int wl1271_remove(struct spi_device
*spi
)
555 struct wl12xx_spi_glue
*glue
= spi_get_drvdata(spi
);
557 platform_device_unregister(glue
->core
);
562 static struct spi_driver wl1271_spi_driver
= {
564 .name
= "wl1271_spi",
565 .of_match_table
= of_match_ptr(wlcore_spi_of_match_table
),
568 .probe
= wl1271_probe
,
569 .remove
= wl1271_remove
,
572 module_spi_driver(wl1271_spi_driver
);
573 MODULE_LICENSE("GPL");
574 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
575 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
576 MODULE_ALIAS("spi:wl1271");