1
2
3
4
5
6
7
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
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
56#define WSPI_MAX_CHUNK_SIZE 4092
57
58
59
60
61
62#define SPI_AGGR_BUFFER_SIZE (13 * SZ_4K)
63
64
65#define WSPI_MAX_NUM_OF_CHUNKS \
66 ((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
67
68static const struct wilink_family_data wl127x_data = {
69 .name = "wl127x",
70 .nvs_name = "ti-connectivity/wl127x-nvs.bin",
71};
72
73static const struct wilink_family_data wl128x_data = {
74 .name = "wl128x",
75 .nvs_name = "ti-connectivity/wl128x-nvs.bin",
76};
77
78static 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
84struct wl12xx_spi_glue {
85 struct device *dev;
86 struct platform_device *core;
87 struct regulator *reg;
88};
89
90static 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
118static 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
137
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
159
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
171
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
190 spi->mode ^= SPI_CS_HIGH;
191 kfree(cmd);
192}
193
194#define WL1271_BUSY_WORD_TIMEOUT 1000
195
196static 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
207
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
227 dev_err(child->parent, "SPI read busy-word timeout!\n");
228 return -ETIMEDOUT;
229}
230
231static 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
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
298static 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];
305 u32 *cmd;
306 u32 chunk_len;
307 int i;
308
309
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
353static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
354 void *buf, size_t len, bool fixed)
355{
356
357
358
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
368
369
370
371
372
373static 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
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
396
397
398
399
400
401static void wl12xx_spi_set_block_size(struct device *child,
402 unsigned int blksz)
403{
404}
405
406static 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
415static 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};
429MODULE_DEVICE_TABLE(of, wlcore_spi_of_match_table);
430
431
432
433
434
435
436
437
438static int wlcore_probe_of(struct spi_device *spi, struct wl12xx_spi_glue *glue,
439 struct wlcore_platdev_data *pdev_data)
440{
441 struct device_node *dt_node = spi->dev.of_node;
442 const struct of_device_id *of_id;
443
444 of_id = of_match_node(wlcore_spi_of_match_table, dt_node);
445 if (!of_id)
446 return -ENODEV;
447
448 pdev_data->family = of_id->data;
449 dev_info(&spi->dev, "selected chip family is %s\n",
450 pdev_data->family->name);
451
452 if (of_find_property(dt_node, "clock-xtal", NULL))
453 pdev_data->ref_clock_xtal = true;
454
455
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);
460
461 return 0;
462}
463
464static int wl1271_probe(struct spi_device *spi)
465{
466 struct wl12xx_spi_glue *glue;
467 struct wlcore_platdev_data *pdev_data;
468 struct resource res[1];
469 int ret;
470
471 pdev_data = devm_kzalloc(&spi->dev, sizeof(*pdev_data), GFP_KERNEL);
472 if (!pdev_data)
473 return -ENOMEM;
474
475 pdev_data->if_ops = &spi_ops;
476
477 glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL);
478 if (!glue) {
479 dev_err(&spi->dev, "can't allocate glue\n");
480 return -ENOMEM;
481 }
482
483 glue->dev = &spi->dev;
484
485 spi_set_drvdata(spi, glue);
486
487
488
489 spi->bits_per_word = 32;
490
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);
497 }
498
499 ret = wlcore_probe_of(spi, glue, pdev_data);
500 if (ret) {
501 dev_err(glue->dev,
502 "can't get device tree parameters (%d)\n", ret);
503 return ret;
504 }
505
506 ret = spi_setup(spi);
507 if (ret < 0) {
508 dev_err(glue->dev, "spi_setup failed\n");
509 return ret;
510 }
511
512 glue->core = platform_device_alloc(pdev_data->family->name,
513 PLATFORM_DEVID_AUTO);
514 if (!glue->core) {
515 dev_err(glue->dev, "can't allocate platform_device\n");
516 return -ENOMEM;
517 }
518
519 glue->core->dev.parent = &spi->dev;
520
521 memset(res, 0x00, sizeof(res));
522
523 res[0].start = spi->irq;
524 res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(spi->irq);
525 res[0].name = "irq";
526
527 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
528 if (ret) {
529 dev_err(glue->dev, "can't add resources\n");
530 goto out_dev_put;
531 }
532
533 ret = platform_device_add_data(glue->core, pdev_data,
534 sizeof(*pdev_data));
535 if (ret) {
536 dev_err(glue->dev, "can't add platform data\n");
537 goto out_dev_put;
538 }
539
540 ret = platform_device_add(glue->core);
541 if (ret) {
542 dev_err(glue->dev, "can't register platform device\n");
543 goto out_dev_put;
544 }
545
546 return 0;
547
548out_dev_put:
549 platform_device_put(glue->core);
550 return ret;
551}
552
553static int wl1271_remove(struct spi_device *spi)
554{
555 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
556
557 platform_device_unregister(glue->core);
558
559 return 0;
560}
561
562static struct spi_driver wl1271_spi_driver = {
563 .driver = {
564 .name = "wl1271_spi",
565 .of_match_table = of_match_ptr(wlcore_spi_of_match_table),
566 },
567
568 .probe = wl1271_probe,
569 .remove = wl1271_remove,
570};
571
572module_spi_driver(wl1271_spi_driver);
573MODULE_LICENSE("GPL");
574MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
575MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
576MODULE_ALIAS("spi:wl1271");
577