xref: /openbmc/linux/drivers/net/wireless/ti/wlcore/spi.c (revision bacf743e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file is part of wl1271
4  *
5  * Copyright (C) 2008-2009 Nokia Corporation
6  *
7  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8  */
9 
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>
21 
22 #include "wlcore.h"
23 #include "wl12xx_80211.h"
24 #include "io.h"
25 
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
32 
33 #define WSPI_INIT_CMD_CRC_LEN       5
34 
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
48 
49 #define WSPI_INIT_CMD_LEN           8
50 
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
54 
55 /* HW limitation: maximum possible chunk size is 4095 bytes */
56 #define WSPI_MAX_CHUNK_SIZE    4092
57 
58 /*
59  * wl18xx driver aggregation buffer size is (13 * 4K) compared to
60  * (4 * 4K) for wl12xx, so use the larger buffer needed for wl18xx
61  */
62 #define SPI_AGGR_BUFFER_SIZE (13 * SZ_4K)
63 
64 /* Maximum number of SPI write chunks */
65 #define WSPI_MAX_NUM_OF_CHUNKS \
66 	((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
67 
68 static const struct wilink_family_data wl127x_data = {
69 	.name = "wl127x",
70 	.nvs_name = "ti-connectivity/wl127x-nvs.bin",
71 };
72 
73 static const struct wilink_family_data wl128x_data = {
74 	.name = "wl128x",
75 	.nvs_name = "ti-connectivity/wl128x-nvs.bin",
76 };
77 
78 static const struct wilink_family_data wl18xx_data = {
79 	.name = "wl18xx",
80 	.cfg_name = "ti-connectivity/wl18xx-conf.bin",
81 	.nvs_name = "ti-connectivity/wl1271-nvs.bin",
82 };
83 
84 struct wl12xx_spi_glue {
85 	struct device *dev;
86 	struct platform_device *core;
87 	struct regulator *reg; /* Power regulator */
88 };
89 
90 static void wl12xx_spi_reset(struct device *child)
91 {
92 	struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
93 	u8 *cmd;
94 	struct spi_transfer t;
95 	struct spi_message m;
96 
97 	cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
98 	if (!cmd) {
99 		dev_err(child->parent,
100 			"could not allocate cmd for spi reset\n");
101 		return;
102 	}
103 
104 	memset(&t, 0, sizeof(t));
105 	spi_message_init(&m);
106 
107 	memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
108 
109 	t.tx_buf = cmd;
110 	t.len = WSPI_INIT_CMD_LEN;
111 	spi_message_add_tail(&t, &m);
112 
113 	spi_sync(to_spi_device(glue->dev), &m);
114 
115 	kfree(cmd);
116 }
117 
118 static void wl12xx_spi_init(struct device *child)
119 {
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);
125 
126 	if (!cmd) {
127 		dev_err(child->parent,
128 			"could not allocate cmd for spi init\n");
129 		return;
130 	}
131 
132 	memset(&t, 0, sizeof(t));
133 	spi_message_init(&m);
134 
135 	/*
136 	 * Set WSPI_INIT_COMMAND
137 	 * the data is being send from the MSB to LSB
138 	 */
139 	cmd[0] = 0xff;
140 	cmd[1] = 0xff;
141 	cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
142 	cmd[3] = 0;
143 	cmd[4] = 0;
144 	cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
145 	cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
146 
147 	cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
148 		| WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
149 
150 	if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
151 		cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
152 	else
153 		cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
154 
155 	cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
156 
157 	/*
158 	 * The above is the logical order; it must actually be stored
159 	 * in the buffer byte-swapped.
160 	 */
161 	__swab32s((u32 *)cmd);
162 	__swab32s((u32 *)cmd+1);
163 
164 	t.tx_buf = cmd;
165 	t.len = WSPI_INIT_CMD_LEN;
166 	spi_message_add_tail(&t, &m);
167 
168 	spi_sync(to_spi_device(glue->dev), &m);
169 
170 	/* Send extra clocks with inverted CS (high). this is required
171 	 * by the wilink family in order to successfully enter WSPI mode.
172 	 */
173 	spi->mode ^= SPI_CS_HIGH;
174 	memset(&m, 0, sizeof(m));
175 	spi_message_init(&m);
176 
177 	cmd[0] = 0xff;
178 	cmd[1] = 0xff;
179 	cmd[2] = 0xff;
180 	cmd[3] = 0xff;
181 	__swab32s((u32 *)cmd);
182 
183 	t.tx_buf = cmd;
184 	t.len = 4;
185 	spi_message_add_tail(&t, &m);
186 
187 	spi_sync(to_spi_device(glue->dev), &m);
188 
189 	/* Restore chip select configuration to normal */
190 	spi->mode ^= SPI_CS_HIGH;
191 	kfree(cmd);
192 }
193 
194 #define WL1271_BUSY_WORD_TIMEOUT 1000
195 
196 static int wl12xx_spi_read_busy(struct device *child)
197 {
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;
202 	u32 *busy_buf;
203 	int num_busy_bytes = 0;
204 
205 	/*
206 	 * Read further busy words from SPI until a non-busy word is
207 	 * encountered, then read the data itself into the buffer.
208 	 */
209 
210 	num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
211 	busy_buf = wl->buffer_busyword;
212 	while (num_busy_bytes) {
213 		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);
221 
222 		if (*busy_buf & 0x1)
223 			return 0;
224 	}
225 
226 	/* The SPI bus is unresponsive, the read failed. */
227 	dev_err(child->parent, "SPI read busy-word timeout!\n");
228 	return -ETIMEDOUT;
229 }
230 
231 static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
232 					    void *buf, size_t len, bool fixed)
233 {
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;
238 	u32 *busy_buf;
239 	u32 *cmd;
240 	u32 chunk_len;
241 
242 	while (len > 0) {
243 		chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
244 
245 		cmd = &wl->buffer_cmd;
246 		busy_buf = wl->buffer_busyword;
247 
248 		*cmd = 0;
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;
253 
254 		if (fixed)
255 			*cmd |= WSPI_CMD_FIXED;
256 
257 		spi_message_init(&m);
258 		memset(t, 0, sizeof(t));
259 
260 		t[0].tx_buf = cmd;
261 		t[0].len = 4;
262 		t[0].cs_change = true;
263 		spi_message_add_tail(&t[0], &m);
264 
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);
270 
271 		spi_sync(to_spi_device(glue->dev), &m);
272 
273 		if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
274 		    wl12xx_spi_read_busy(child)) {
275 			memset(buf, 0, chunk_len);
276 			return 0;
277 		}
278 
279 		spi_message_init(&m);
280 		memset(t, 0, sizeof(t));
281 
282 		t[0].rx_buf = buf;
283 		t[0].len = chunk_len;
284 		t[0].cs_change = true;
285 		spi_message_add_tail(&t[0], &m);
286 
287 		spi_sync(to_spi_device(glue->dev), &m);
288 
289 		if (!fixed)
290 			addr += chunk_len;
291 		buf += chunk_len;
292 		len -= chunk_len;
293 	}
294 
295 	return 0;
296 }
297 
298 static int __wl12xx_spi_raw_write(struct device *child, int addr,
299 				  void *buf, size_t len, bool fixed)
300 {
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 */
305 	u32 *cmd;
306 	u32 chunk_len;
307 	int i;
308 
309 	/* SPI write buffers - 2 for each chunk */
310 	t = kzalloc(sizeof(*t) * 2 * WSPI_MAX_NUM_OF_CHUNKS, GFP_KERNEL);
311 	if (!t)
312 		return -ENOMEM;
313 
314 	WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
315 
316 	spi_message_init(&m);
317 
318 	cmd = &commands[0];
319 	i = 0;
320 	while (len > 0) {
321 		chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
322 
323 		*cmd = 0;
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;
328 
329 		if (fixed)
330 			*cmd |= WSPI_CMD_FIXED;
331 
332 		t[i].tx_buf = cmd;
333 		t[i].len = sizeof(*cmd);
334 		spi_message_add_tail(&t[i++], &m);
335 
336 		t[i].tx_buf = buf;
337 		t[i].len = chunk_len;
338 		spi_message_add_tail(&t[i++], &m);
339 
340 		if (!fixed)
341 			addr += chunk_len;
342 		buf += chunk_len;
343 		len -= chunk_len;
344 		cmd++;
345 	}
346 
347 	spi_sync(to_spi_device(glue->dev), &m);
348 
349 	kfree(t);
350 	return 0;
351 }
352 
353 static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
354 					     void *buf, size_t len, bool fixed)
355 {
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.
359 	 */
360 	if (addr == HW_ACCESS_ELP_CTRL_REG)
361 		__wl12xx_spi_raw_write(child, addr, buf, len, fixed);
362 
363 	return __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
364 }
365 
366 /**
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.
370  *
371  * use the WiFi enable regulator to enable/disable the WiFi unit.
372  */
373 static int wl12xx_spi_set_power(struct device *child, bool enable)
374 {
375 	int ret = 0;
376 	struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
377 
378 	WARN_ON(!glue->reg);
379 
380 	/* Update regulator state */
381 	if (enable) {
382 		ret = regulator_enable(glue->reg);
383 		if (ret)
384 			dev_err(child, "Power enable failure\n");
385 	} else {
386 		ret =  regulator_disable(glue->reg);
387 		if (ret)
388 			dev_err(child, "Power disable failure\n");
389 	}
390 
391 	return ret;
392 }
393 
394 /*
395  * wl12xx_spi_set_block_size
396  *
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
399  * allignment on tx.
400  */
401 static void wl12xx_spi_set_block_size(struct device *child,
402 				      unsigned int blksz)
403 {
404 }
405 
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,
413 };
414 
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},
427 	{ }
428 };
429 MODULE_DEVICE_TABLE(of, wlcore_spi_of_match_table);
430 
431 /**
432  * wlcore_probe_of - DT node parsing.
433  * @spi: SPI slave device parameters.
434  * @glue: wl12xx SPI bus to slave device glue parameters.
435  * @pdev_data: wlcore device parameters
436  */
437 static int wlcore_probe_of(struct spi_device *spi, struct wl12xx_spi_glue *glue,
438 			   struct wlcore_platdev_data *pdev_data)
439 {
440 	struct device_node *dt_node = spi->dev.of_node;
441 	const struct of_device_id *of_id;
442 
443 	of_id = of_match_node(wlcore_spi_of_match_table, dt_node);
444 	if (!of_id)
445 		return -ENODEV;
446 
447 	pdev_data->family = of_id->data;
448 	dev_info(&spi->dev, "selected chip family is %s\n",
449 		 pdev_data->family->name);
450 
451 	if (of_find_property(dt_node, "clock-xtal", NULL))
452 		pdev_data->ref_clock_xtal = true;
453 
454 	/* optional clock frequency params */
455 	of_property_read_u32(dt_node, "ref-clock-frequency",
456 			     &pdev_data->ref_clock_freq);
457 	of_property_read_u32(dt_node, "tcxo-clock-frequency",
458 			     &pdev_data->tcxo_clock_freq);
459 
460 	return 0;
461 }
462 
463 static int wl1271_probe(struct spi_device *spi)
464 {
465 	struct wl12xx_spi_glue *glue;
466 	struct wlcore_platdev_data *pdev_data;
467 	struct resource res[1];
468 	int ret;
469 
470 	pdev_data = devm_kzalloc(&spi->dev, sizeof(*pdev_data), GFP_KERNEL);
471 	if (!pdev_data)
472 		return -ENOMEM;
473 
474 	pdev_data->if_ops = &spi_ops;
475 
476 	glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL);
477 	if (!glue) {
478 		dev_err(&spi->dev, "can't allocate glue\n");
479 		return -ENOMEM;
480 	}
481 
482 	glue->dev = &spi->dev;
483 
484 	spi_set_drvdata(spi, glue);
485 
486 	/* This is the only SPI value that we need to set here, the rest
487 	 * comes from the board-peripherals file */
488 	spi->bits_per_word = 32;
489 
490 	glue->reg = devm_regulator_get(&spi->dev, "vwlan");
491 	if (IS_ERR(glue->reg))
492 		return dev_err_probe(glue->dev, PTR_ERR(glue->reg),
493 				     "can't get regulator\n");
494 
495 	ret = wlcore_probe_of(spi, glue, pdev_data);
496 	if (ret) {
497 		dev_err(glue->dev,
498 			"can't get device tree parameters (%d)\n", ret);
499 		return ret;
500 	}
501 
502 	ret = spi_setup(spi);
503 	if (ret < 0) {
504 		dev_err(glue->dev, "spi_setup failed\n");
505 		return ret;
506 	}
507 
508 	glue->core = platform_device_alloc(pdev_data->family->name,
509 					   PLATFORM_DEVID_AUTO);
510 	if (!glue->core) {
511 		dev_err(glue->dev, "can't allocate platform_device\n");
512 		return -ENOMEM;
513 	}
514 
515 	glue->core->dev.parent = &spi->dev;
516 
517 	memset(res, 0x00, sizeof(res));
518 
519 	res[0].start = spi->irq;
520 	res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(spi->irq);
521 	res[0].name = "irq";
522 
523 	ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
524 	if (ret) {
525 		dev_err(glue->dev, "can't add resources\n");
526 		goto out_dev_put;
527 	}
528 
529 	ret = platform_device_add_data(glue->core, pdev_data,
530 				       sizeof(*pdev_data));
531 	if (ret) {
532 		dev_err(glue->dev, "can't add platform data\n");
533 		goto out_dev_put;
534 	}
535 
536 	ret = platform_device_add(glue->core);
537 	if (ret) {
538 		dev_err(glue->dev, "can't register platform device\n");
539 		goto out_dev_put;
540 	}
541 
542 	return 0;
543 
544 out_dev_put:
545 	platform_device_put(glue->core);
546 	return ret;
547 }
548 
549 static void wl1271_remove(struct spi_device *spi)
550 {
551 	struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
552 
553 	platform_device_unregister(glue->core);
554 }
555 
556 static struct spi_driver wl1271_spi_driver = {
557 	.driver = {
558 		.name		= "wl1271_spi",
559 		.of_match_table = of_match_ptr(wlcore_spi_of_match_table),
560 	},
561 
562 	.probe		= wl1271_probe,
563 	.remove		= wl1271_remove,
564 };
565 
566 module_spi_driver(wl1271_spi_driver);
567 MODULE_LICENSE("GPL");
568 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
569 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
570 MODULE_ALIAS("spi:wl1271");
571