1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/interrupt.h>
25#include <linux/irq.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/swab.h>
29#include <linux/crc7.h>
30#include <linux/spi/spi.h>
31#include <linux/wl12xx.h>
32#include <linux/platform_device.h>
33#include <linux/of_irq.h>
34#include <linux/regulator/consumer.h>
35
36#include "wlcore.h"
37#include "wl12xx_80211.h"
38#include "io.h"
39
40#define WSPI_CMD_READ 0x40000000
41#define WSPI_CMD_WRITE 0x00000000
42#define WSPI_CMD_FIXED 0x20000000
43#define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
44#define WSPI_CMD_BYTE_LENGTH_OFFSET 17
45#define WSPI_CMD_BYTE_ADDR 0x0001FFFF
46
47#define WSPI_INIT_CMD_CRC_LEN 5
48
49#define WSPI_INIT_CMD_START 0x00
50#define WSPI_INIT_CMD_TX 0x40
51
52#define WSPI_INIT_CMD_BYPASS_BIT 0x80
53#define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
54#define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
55#define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
56#define WSPI_INIT_CMD_IOD 0x40
57#define WSPI_INIT_CMD_IP 0x20
58#define WSPI_INIT_CMD_CS 0x10
59#define WSPI_INIT_CMD_WS 0x08
60#define WSPI_INIT_CMD_WSPI 0x01
61#define WSPI_INIT_CMD_END 0x01
62
63#define WSPI_INIT_CMD_LEN 8
64
65#define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
66 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
67#define HW_ACCESS_WSPI_INIT_CMD_MASK 0
68
69
70#define WSPI_MAX_CHUNK_SIZE 4092
71
72
73
74
75
76#define SPI_AGGR_BUFFER_SIZE (13 * SZ_4K)
77
78
79#define WSPI_MAX_NUM_OF_CHUNKS \
80 ((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
81
82static const struct wilink_family_data wl127x_data = {
83 .name = "wl127x",
84 .nvs_name = "ti-connectivity/wl127x-nvs.bin",
85};
86
87static const struct wilink_family_data wl128x_data = {
88 .name = "wl128x",
89 .nvs_name = "ti-connectivity/wl128x-nvs.bin",
90};
91
92static const struct wilink_family_data wl18xx_data = {
93 .name = "wl18xx",
94 .cfg_name = "ti-connectivity/wl18xx-conf.bin",
95 .nvs_name = "ti-connectivity/wl1271-nvs.bin",
96};
97
98struct wl12xx_spi_glue {
99 struct device *dev;
100 struct platform_device *core;
101 struct regulator *reg;
102};
103
104static void wl12xx_spi_reset(struct device *child)
105{
106 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
107 u8 *cmd;
108 struct spi_transfer t;
109 struct spi_message m;
110
111 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
112 if (!cmd) {
113 dev_err(child->parent,
114 "could not allocate cmd for spi reset\n");
115 return;
116 }
117
118 memset(&t, 0, sizeof(t));
119 spi_message_init(&m);
120
121 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
122
123 t.tx_buf = cmd;
124 t.len = WSPI_INIT_CMD_LEN;
125 spi_message_add_tail(&t, &m);
126
127 spi_sync(to_spi_device(glue->dev), &m);
128
129 kfree(cmd);
130}
131
132static void wl12xx_spi_init(struct device *child)
133{
134 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
135 struct spi_transfer t;
136 struct spi_message m;
137 struct spi_device *spi = to_spi_device(glue->dev);
138 u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
139
140 if (!cmd) {
141 dev_err(child->parent,
142 "could not allocate cmd for spi init\n");
143 return;
144 }
145
146 memset(&t, 0, sizeof(t));
147 spi_message_init(&m);
148
149
150
151
152
153 cmd[0] = 0xff;
154 cmd[1] = 0xff;
155 cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
156 cmd[3] = 0;
157 cmd[4] = 0;
158 cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
159 cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
160
161 cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
162 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
163
164 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
165 cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
166 else
167 cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
168
169 cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
170
171
172
173
174
175 __swab32s((u32 *)cmd);
176 __swab32s((u32 *)cmd+1);
177
178 t.tx_buf = cmd;
179 t.len = WSPI_INIT_CMD_LEN;
180 spi_message_add_tail(&t, &m);
181
182 spi_sync(to_spi_device(glue->dev), &m);
183
184
185
186
187 spi->mode ^= SPI_CS_HIGH;
188 memset(&m, 0, sizeof(m));
189 spi_message_init(&m);
190
191 cmd[0] = 0xff;
192 cmd[1] = 0xff;
193 cmd[2] = 0xff;
194 cmd[3] = 0xff;
195 __swab32s((u32 *)cmd);
196
197 t.tx_buf = cmd;
198 t.len = 4;
199 spi_message_add_tail(&t, &m);
200
201 spi_sync(to_spi_device(glue->dev), &m);
202
203
204 spi->mode ^= SPI_CS_HIGH;
205 kfree(cmd);
206}
207
208#define WL1271_BUSY_WORD_TIMEOUT 1000
209
210static int wl12xx_spi_read_busy(struct device *child)
211{
212 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
213 struct wl1271 *wl = dev_get_drvdata(child);
214 struct spi_transfer t[1];
215 struct spi_message m;
216 u32 *busy_buf;
217 int num_busy_bytes = 0;
218
219
220
221
222
223
224 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
225 busy_buf = wl->buffer_busyword;
226 while (num_busy_bytes) {
227 num_busy_bytes--;
228 spi_message_init(&m);
229 memset(t, 0, sizeof(t));
230 t[0].rx_buf = busy_buf;
231 t[0].len = sizeof(u32);
232 t[0].cs_change = true;
233 spi_message_add_tail(&t[0], &m);
234 spi_sync(to_spi_device(glue->dev), &m);
235
236 if (*busy_buf & 0x1)
237 return 0;
238 }
239
240
241 dev_err(child->parent, "SPI read busy-word timeout!\n");
242 return -ETIMEDOUT;
243}
244
245static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
246 void *buf, size_t len, bool fixed)
247{
248 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
249 struct wl1271 *wl = dev_get_drvdata(child);
250 struct spi_transfer t[2];
251 struct spi_message m;
252 u32 *busy_buf;
253 u32 *cmd;
254 u32 chunk_len;
255
256 while (len > 0) {
257 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
258
259 cmd = &wl->buffer_cmd;
260 busy_buf = wl->buffer_busyword;
261
262 *cmd = 0;
263 *cmd |= WSPI_CMD_READ;
264 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
265 WSPI_CMD_BYTE_LENGTH;
266 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
267
268 if (fixed)
269 *cmd |= WSPI_CMD_FIXED;
270
271 spi_message_init(&m);
272 memset(t, 0, sizeof(t));
273
274 t[0].tx_buf = cmd;
275 t[0].len = 4;
276 t[0].cs_change = true;
277 spi_message_add_tail(&t[0], &m);
278
279
280 t[1].rx_buf = busy_buf;
281 t[1].len = WL1271_BUSY_WORD_LEN;
282 t[1].cs_change = true;
283 spi_message_add_tail(&t[1], &m);
284
285 spi_sync(to_spi_device(glue->dev), &m);
286
287 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
288 wl12xx_spi_read_busy(child)) {
289 memset(buf, 0, chunk_len);
290 return 0;
291 }
292
293 spi_message_init(&m);
294 memset(t, 0, sizeof(t));
295
296 t[0].rx_buf = buf;
297 t[0].len = chunk_len;
298 t[0].cs_change = true;
299 spi_message_add_tail(&t[0], &m);
300
301 spi_sync(to_spi_device(glue->dev), &m);
302
303 if (!fixed)
304 addr += chunk_len;
305 buf += chunk_len;
306 len -= chunk_len;
307 }
308
309 return 0;
310}
311
312static int __wl12xx_spi_raw_write(struct device *child, int addr,
313 void *buf, size_t len, bool fixed)
314{
315 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
316 struct spi_transfer *t;
317 struct spi_message m;
318 u32 commands[WSPI_MAX_NUM_OF_CHUNKS];
319 u32 *cmd;
320 u32 chunk_len;
321 int i;
322
323
324 t = kzalloc(sizeof(*t) * 2 * WSPI_MAX_NUM_OF_CHUNKS, GFP_KERNEL);
325 if (!t)
326 return -ENOMEM;
327
328 WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
329
330 spi_message_init(&m);
331
332 cmd = &commands[0];
333 i = 0;
334 while (len > 0) {
335 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
336
337 *cmd = 0;
338 *cmd |= WSPI_CMD_WRITE;
339 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
340 WSPI_CMD_BYTE_LENGTH;
341 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
342
343 if (fixed)
344 *cmd |= WSPI_CMD_FIXED;
345
346 t[i].tx_buf = cmd;
347 t[i].len = sizeof(*cmd);
348 spi_message_add_tail(&t[i++], &m);
349
350 t[i].tx_buf = buf;
351 t[i].len = chunk_len;
352 spi_message_add_tail(&t[i++], &m);
353
354 if (!fixed)
355 addr += chunk_len;
356 buf += chunk_len;
357 len -= chunk_len;
358 cmd++;
359 }
360
361 spi_sync(to_spi_device(glue->dev), &m);
362
363 kfree(t);
364 return 0;
365}
366
367static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
368 void *buf, size_t len, bool fixed)
369{
370
371
372
373
374 if (addr == HW_ACCESS_ELP_CTRL_REG)
375 __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
376
377 return __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
378}
379
380
381
382
383
384
385
386
387static int wl12xx_spi_set_power(struct device *child, bool enable)
388{
389 int ret = 0;
390 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
391
392 WARN_ON(!glue->reg);
393
394
395 if (enable) {
396 ret = regulator_enable(glue->reg);
397 if (ret)
398 dev_err(child, "Power enable failure\n");
399 } else {
400 ret = regulator_disable(glue->reg);
401 if (ret)
402 dev_err(child, "Power disable failure\n");
403 }
404
405 return ret;
406}
407
408
409
410
411
412
413
414
415static void wl12xx_spi_set_block_size(struct device *child,
416 unsigned int blksz)
417{
418}
419
420static struct wl1271_if_operations spi_ops = {
421 .read = wl12xx_spi_raw_read,
422 .write = wl12xx_spi_raw_write,
423 .reset = wl12xx_spi_reset,
424 .init = wl12xx_spi_init,
425 .power = wl12xx_spi_set_power,
426 .set_block_size = wl12xx_spi_set_block_size,
427};
428
429static const struct of_device_id wlcore_spi_of_match_table[] = {
430 { .compatible = "ti,wl1271", .data = &wl127x_data},
431 { .compatible = "ti,wl1273", .data = &wl127x_data},
432 { .compatible = "ti,wl1281", .data = &wl128x_data},
433 { .compatible = "ti,wl1283", .data = &wl128x_data},
434 { .compatible = "ti,wl1285", .data = &wl128x_data},
435 { .compatible = "ti,wl1801", .data = &wl18xx_data},
436 { .compatible = "ti,wl1805", .data = &wl18xx_data},
437 { .compatible = "ti,wl1807", .data = &wl18xx_data},
438 { .compatible = "ti,wl1831", .data = &wl18xx_data},
439 { .compatible = "ti,wl1835", .data = &wl18xx_data},
440 { .compatible = "ti,wl1837", .data = &wl18xx_data},
441 { }
442};
443MODULE_DEVICE_TABLE(of, wlcore_spi_of_match_table);
444
445
446
447
448
449
450
451
452static int wlcore_probe_of(struct spi_device *spi, struct wl12xx_spi_glue *glue,
453 struct wlcore_platdev_data *pdev_data)
454{
455 struct device_node *dt_node = spi->dev.of_node;
456 const struct of_device_id *of_id;
457
458 of_id = of_match_node(wlcore_spi_of_match_table, dt_node);
459 if (!of_id)
460 return -ENODEV;
461
462 pdev_data->family = of_id->data;
463 dev_info(&spi->dev, "selected chip family is %s\n",
464 pdev_data->family->name);
465
466 if (of_find_property(dt_node, "clock-xtal", NULL))
467 pdev_data->ref_clock_xtal = true;
468
469
470 of_property_read_u32(dt_node, "ref-clock-frequency",
471 &pdev_data->ref_clock_freq);
472 of_property_read_u32(dt_node, "tcxo-clock-frequency",
473 &pdev_data->tcxo_clock_freq);
474
475 return 0;
476}
477
478static int wl1271_probe(struct spi_device *spi)
479{
480 struct wl12xx_spi_glue *glue;
481 struct wlcore_platdev_data *pdev_data;
482 struct resource res[1];
483 int ret;
484
485 pdev_data = devm_kzalloc(&spi->dev, sizeof(*pdev_data), GFP_KERNEL);
486 if (!pdev_data)
487 return -ENOMEM;
488
489 pdev_data->if_ops = &spi_ops;
490
491 glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL);
492 if (!glue) {
493 dev_err(&spi->dev, "can't allocate glue\n");
494 return -ENOMEM;
495 }
496
497 glue->dev = &spi->dev;
498
499 spi_set_drvdata(spi, glue);
500
501
502
503 spi->bits_per_word = 32;
504
505 glue->reg = devm_regulator_get(&spi->dev, "vwlan");
506 if (PTR_ERR(glue->reg) == -EPROBE_DEFER)
507 return -EPROBE_DEFER;
508 if (IS_ERR(glue->reg)) {
509 dev_err(glue->dev, "can't get regulator\n");
510 return PTR_ERR(glue->reg);
511 }
512
513 ret = wlcore_probe_of(spi, glue, pdev_data);
514 if (ret) {
515 dev_err(glue->dev,
516 "can't get device tree parameters (%d)\n", ret);
517 return ret;
518 }
519
520 ret = spi_setup(spi);
521 if (ret < 0) {
522 dev_err(glue->dev, "spi_setup failed\n");
523 return ret;
524 }
525
526 glue->core = platform_device_alloc(pdev_data->family->name,
527 PLATFORM_DEVID_AUTO);
528 if (!glue->core) {
529 dev_err(glue->dev, "can't allocate platform_device\n");
530 return -ENOMEM;
531 }
532
533 glue->core->dev.parent = &spi->dev;
534
535 memset(res, 0x00, sizeof(res));
536
537 res[0].start = spi->irq;
538 res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(spi->irq);
539 res[0].name = "irq";
540
541 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
542 if (ret) {
543 dev_err(glue->dev, "can't add resources\n");
544 goto out_dev_put;
545 }
546
547 ret = platform_device_add_data(glue->core, pdev_data,
548 sizeof(*pdev_data));
549 if (ret) {
550 dev_err(glue->dev, "can't add platform data\n");
551 goto out_dev_put;
552 }
553
554 ret = platform_device_add(glue->core);
555 if (ret) {
556 dev_err(glue->dev, "can't register platform device\n");
557 goto out_dev_put;
558 }
559
560 return 0;
561
562out_dev_put:
563 platform_device_put(glue->core);
564 return ret;
565}
566
567static int wl1271_remove(struct spi_device *spi)
568{
569 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
570
571 platform_device_unregister(glue->core);
572
573 return 0;
574}
575
576static struct spi_driver wl1271_spi_driver = {
577 .driver = {
578 .name = "wl1271_spi",
579 .of_match_table = of_match_ptr(wlcore_spi_of_match_table),
580 },
581
582 .probe = wl1271_probe,
583 .remove = wl1271_remove,
584};
585
586module_spi_driver(wl1271_spi_driver);
587MODULE_LICENSE("GPL");
588MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
589MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
590MODULE_ALIAS("spi:wl1271");
591