Loading...
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/platform_device.h>
18#include <linux/of_irq.h>
19#include <linux/regulator/consumer.h>
20
21#include "wlcore.h"
22#include "wl12xx_80211.h"
23#include "io.h"
24
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
31
32#define WSPI_INIT_CMD_CRC_LEN 5
33
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
47
48#define WSPI_INIT_CMD_LEN 8
49
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
53
54/* HW limitation: maximum possible chunk size is 4095 bytes */
55#define WSPI_MAX_CHUNK_SIZE 4092
56
57/*
58 * wl18xx driver aggregation buffer size is (13 * 4K) compared to
59 * (4 * 4K) for wl12xx, so use the larger buffer needed for wl18xx
60 */
61#define SPI_AGGR_BUFFER_SIZE (13 * SZ_4K)
62
63/* Maximum number of SPI write chunks */
64#define WSPI_MAX_NUM_OF_CHUNKS \
65 ((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
66
67static const struct wilink_family_data wl127x_data = {
68 .name = "wl127x",
69 .nvs_name = "ti-connectivity/wl127x-nvs.bin",
70};
71
72static const struct wilink_family_data wl128x_data = {
73 .name = "wl128x",
74 .nvs_name = "ti-connectivity/wl128x-nvs.bin",
75};
76
77static const struct wilink_family_data wl18xx_data = {
78 .name = "wl18xx",
79 .cfg_name = "ti-connectivity/wl18xx-conf.bin",
80 .nvs_name = "ti-connectivity/wl1271-nvs.bin",
81};
82
83struct wl12xx_spi_glue {
84 struct device *dev;
85 struct platform_device *core;
86 struct regulator *reg; /* Power regulator */
87};
88
89static void wl12xx_spi_reset(struct device *child)
90{
91 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
92 u8 *cmd;
93 struct spi_transfer t;
94 struct spi_message m;
95
96 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
97 if (!cmd) {
98 dev_err(child->parent,
99 "could not allocate cmd for spi reset\n");
100 return;
101 }
102
103 memset(&t, 0, sizeof(t));
104 spi_message_init(&m);
105
106 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
107
108 t.tx_buf = cmd;
109 t.len = WSPI_INIT_CMD_LEN;
110 spi_message_add_tail(&t, &m);
111
112 spi_sync(to_spi_device(glue->dev), &m);
113
114 kfree(cmd);
115}
116
117static void wl12xx_spi_init(struct device *child)
118{
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);
124
125 if (!cmd) {
126 dev_err(child->parent,
127 "could not allocate cmd for spi init\n");
128 return;
129 }
130
131 memset(&t, 0, sizeof(t));
132 spi_message_init(&m);
133
134 /*
135 * Set WSPI_INIT_COMMAND
136 * the data is being send from the MSB to LSB
137 */
138 cmd[0] = 0xff;
139 cmd[1] = 0xff;
140 cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
141 cmd[3] = 0;
142 cmd[4] = 0;
143 cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
144 cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
145
146 cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
147 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
148
149 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
150 cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
151 else
152 cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
153
154 cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
155
156 /*
157 * The above is the logical order; it must actually be stored
158 * in the buffer byte-swapped.
159 */
160 __swab32s((u32 *)cmd);
161 __swab32s((u32 *)cmd+1);
162
163 t.tx_buf = cmd;
164 t.len = WSPI_INIT_CMD_LEN;
165 spi_message_add_tail(&t, &m);
166
167 spi_sync(to_spi_device(glue->dev), &m);
168
169 /* Send extra clocks with inverted CS (high). this is required
170 * by the wilink family in order to successfully enter WSPI mode.
171 */
172 spi->mode ^= SPI_CS_HIGH;
173 memset(&m, 0, sizeof(m));
174 spi_message_init(&m);
175
176 cmd[0] = 0xff;
177 cmd[1] = 0xff;
178 cmd[2] = 0xff;
179 cmd[3] = 0xff;
180 __swab32s((u32 *)cmd);
181
182 t.tx_buf = cmd;
183 t.len = 4;
184 spi_message_add_tail(&t, &m);
185
186 spi_sync(to_spi_device(glue->dev), &m);
187
188 /* Restore chip select configuration to normal */
189 spi->mode ^= SPI_CS_HIGH;
190 kfree(cmd);
191}
192
193#define WL1271_BUSY_WORD_TIMEOUT 1000
194
195static int wl12xx_spi_read_busy(struct device *child)
196{
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;
201 u32 *busy_buf;
202 int num_busy_bytes = 0;
203
204 /*
205 * Read further busy words from SPI until a non-busy word is
206 * encountered, then read the data itself into the buffer.
207 */
208
209 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
210 busy_buf = wl->buffer_busyword;
211 while (num_busy_bytes) {
212 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);
220
221 if (*busy_buf & 0x1)
222 return 0;
223 }
224
225 /* The SPI bus is unresponsive, the read failed. */
226 dev_err(child->parent, "SPI read busy-word timeout!\n");
227 return -ETIMEDOUT;
228}
229
230static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
231 void *buf, size_t len, bool fixed)
232{
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;
237 u32 *busy_buf;
238 u32 *cmd;
239 u32 chunk_len;
240
241 while (len > 0) {
242 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
243
244 cmd = &wl->buffer_cmd;
245 busy_buf = wl->buffer_busyword;
246
247 *cmd = 0;
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;
252
253 if (fixed)
254 *cmd |= WSPI_CMD_FIXED;
255
256 spi_message_init(&m);
257 memset(t, 0, sizeof(t));
258
259 t[0].tx_buf = cmd;
260 t[0].len = 4;
261 t[0].cs_change = true;
262 spi_message_add_tail(&t[0], &m);
263
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);
269
270 spi_sync(to_spi_device(glue->dev), &m);
271
272 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
273 wl12xx_spi_read_busy(child)) {
274 memset(buf, 0, chunk_len);
275 return 0;
276 }
277
278 spi_message_init(&m);
279 memset(t, 0, sizeof(t));
280
281 t[0].rx_buf = buf;
282 t[0].len = chunk_len;
283 t[0].cs_change = true;
284 spi_message_add_tail(&t[0], &m);
285
286 spi_sync(to_spi_device(glue->dev), &m);
287
288 if (!fixed)
289 addr += chunk_len;
290 buf += chunk_len;
291 len -= chunk_len;
292 }
293
294 return 0;
295}
296
297static int __wl12xx_spi_raw_write(struct device *child, int addr,
298 void *buf, size_t len, bool fixed)
299{
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 */
304 u32 *cmd;
305 u32 chunk_len;
306 int i;
307
308 /* SPI write buffers - 2 for each chunk */
309 t = kzalloc(sizeof(*t) * 2 * WSPI_MAX_NUM_OF_CHUNKS, GFP_KERNEL);
310 if (!t)
311 return -ENOMEM;
312
313 WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
314
315 spi_message_init(&m);
316
317 cmd = &commands[0];
318 i = 0;
319 while (len > 0) {
320 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
321
322 *cmd = 0;
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;
327
328 if (fixed)
329 *cmd |= WSPI_CMD_FIXED;
330
331 t[i].tx_buf = cmd;
332 t[i].len = sizeof(*cmd);
333 spi_message_add_tail(&t[i++], &m);
334
335 t[i].tx_buf = buf;
336 t[i].len = chunk_len;
337 spi_message_add_tail(&t[i++], &m);
338
339 if (!fixed)
340 addr += chunk_len;
341 buf += chunk_len;
342 len -= chunk_len;
343 cmd++;
344 }
345
346 spi_sync(to_spi_device(glue->dev), &m);
347
348 kfree(t);
349 return 0;
350}
351
352static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
353 void *buf, size_t len, bool fixed)
354{
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.
358 */
359 if (addr == HW_ACCESS_ELP_CTRL_REG)
360 __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
361
362 return __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
363}
364
365/**
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.
369 *
370 * use the WiFi enable regulator to enable/disable the WiFi unit.
371 */
372static int wl12xx_spi_set_power(struct device *child, bool enable)
373{
374 int ret = 0;
375 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
376
377 WARN_ON(!glue->reg);
378
379 /* Update regulator state */
380 if (enable) {
381 ret = regulator_enable(glue->reg);
382 if (ret)
383 dev_err(child, "Power enable failure\n");
384 } else {
385 ret = regulator_disable(glue->reg);
386 if (ret)
387 dev_err(child, "Power disable failure\n");
388 }
389
390 return ret;
391}
392
393/*
394 * wl12xx_spi_set_block_size
395 *
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
398 * allignment on tx.
399 */
400static void wl12xx_spi_set_block_size(struct device *child,
401 unsigned int blksz)
402{
403}
404
405static 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,
412};
413
414static 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},
426 { }
427};
428MODULE_DEVICE_TABLE(of, wlcore_spi_of_match_table);
429
430/**
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
435 */
436static int wlcore_probe_of(struct spi_device *spi, struct wl12xx_spi_glue *glue,
437 struct wlcore_platdev_data *pdev_data)
438{
439 struct device_node *dt_node = spi->dev.of_node;
440 const struct of_device_id *of_id;
441
442 of_id = of_match_node(wlcore_spi_of_match_table, dt_node);
443 if (!of_id)
444 return -ENODEV;
445
446 pdev_data->family = of_id->data;
447 dev_info(&spi->dev, "selected chip family is %s\n",
448 pdev_data->family->name);
449
450 pdev_data->ref_clock_xtal = of_property_read_bool(dt_node, "clock-xtal");
451
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);
457
458 return 0;
459}
460
461static int wl1271_probe(struct spi_device *spi)
462{
463 struct wl12xx_spi_glue *glue;
464 struct wlcore_platdev_data *pdev_data;
465 struct resource res[1];
466 int ret;
467
468 pdev_data = devm_kzalloc(&spi->dev, sizeof(*pdev_data), GFP_KERNEL);
469 if (!pdev_data)
470 return -ENOMEM;
471
472 pdev_data->if_ops = &spi_ops;
473
474 glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL);
475 if (!glue) {
476 dev_err(&spi->dev, "can't allocate glue\n");
477 return -ENOMEM;
478 }
479
480 glue->dev = &spi->dev;
481
482 spi_set_drvdata(spi, glue);
483
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;
487
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");
492
493 ret = wlcore_probe_of(spi, glue, pdev_data);
494 if (ret) {
495 dev_err(glue->dev,
496 "can't get device tree parameters (%d)\n", ret);
497 return ret;
498 }
499
500 ret = spi_setup(spi);
501 if (ret < 0) {
502 dev_err(glue->dev, "spi_setup failed\n");
503 return ret;
504 }
505
506 glue->core = platform_device_alloc(pdev_data->family->name,
507 PLATFORM_DEVID_AUTO);
508 if (!glue->core) {
509 dev_err(glue->dev, "can't allocate platform_device\n");
510 return -ENOMEM;
511 }
512
513 glue->core->dev.parent = &spi->dev;
514
515 memset(res, 0x00, sizeof(res));
516
517 res[0].start = spi->irq;
518 res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(spi->irq);
519 res[0].name = "irq";
520
521 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
522 if (ret) {
523 dev_err(glue->dev, "can't add resources\n");
524 goto out_dev_put;
525 }
526
527 ret = platform_device_add_data(glue->core, pdev_data,
528 sizeof(*pdev_data));
529 if (ret) {
530 dev_err(glue->dev, "can't add platform data\n");
531 goto out_dev_put;
532 }
533
534 ret = platform_device_add(glue->core);
535 if (ret) {
536 dev_err(glue->dev, "can't register platform device\n");
537 goto out_dev_put;
538 }
539
540 return 0;
541
542out_dev_put:
543 platform_device_put(glue->core);
544 return ret;
545}
546
547static void wl1271_remove(struct spi_device *spi)
548{
549 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
550
551 platform_device_unregister(glue->core);
552}
553
554static struct spi_driver wl1271_spi_driver = {
555 .driver = {
556 .name = "wl1271_spi",
557 .of_match_table = wlcore_spi_of_match_table,
558 },
559
560 .probe = wl1271_probe,
561 .remove = wl1271_remove,
562};
563
564module_spi_driver(wl1271_spi_driver);
565MODULE_DESCRIPTION("TI WLAN SPI helpers");
566MODULE_LICENSE("GPL");
567MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
568MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
569MODULE_ALIAS("spi:wl1271");
1/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/interrupt.h>
25#include <linux/irq.h>
26#include <linux/module.h>
27#include <linux/crc7.h>
28#include <linux/spi/spi.h>
29#include <linux/wl12xx.h>
30#include <linux/platform_device.h>
31#include <linux/slab.h>
32
33#include "wlcore.h"
34#include "wl12xx_80211.h"
35#include "io.h"
36
37#define WSPI_CMD_READ 0x40000000
38#define WSPI_CMD_WRITE 0x00000000
39#define WSPI_CMD_FIXED 0x20000000
40#define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
41#define WSPI_CMD_BYTE_LENGTH_OFFSET 17
42#define WSPI_CMD_BYTE_ADDR 0x0001FFFF
43
44#define WSPI_INIT_CMD_CRC_LEN 5
45
46#define WSPI_INIT_CMD_START 0x00
47#define WSPI_INIT_CMD_TX 0x40
48/* the extra bypass bit is sampled by the TNET as '1' */
49#define WSPI_INIT_CMD_BYPASS_BIT 0x80
50#define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
51#define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
52#define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
53#define WSPI_INIT_CMD_IOD 0x40
54#define WSPI_INIT_CMD_IP 0x20
55#define WSPI_INIT_CMD_CS 0x10
56#define WSPI_INIT_CMD_WS 0x08
57#define WSPI_INIT_CMD_WSPI 0x01
58#define WSPI_INIT_CMD_END 0x01
59
60#define WSPI_INIT_CMD_LEN 8
61
62#define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
63 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
64#define HW_ACCESS_WSPI_INIT_CMD_MASK 0
65
66/* HW limitation: maximum possible chunk size is 4095 bytes */
67#define WSPI_MAX_CHUNK_SIZE 4092
68
69#define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE)
70
71struct wl12xx_spi_glue {
72 struct device *dev;
73 struct platform_device *core;
74};
75
76static void wl12xx_spi_reset(struct device *child)
77{
78 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
79 u8 *cmd;
80 struct spi_transfer t;
81 struct spi_message m;
82
83 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
84 if (!cmd) {
85 dev_err(child->parent,
86 "could not allocate cmd for spi reset\n");
87 return;
88 }
89
90 memset(&t, 0, sizeof(t));
91 spi_message_init(&m);
92
93 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
94
95 t.tx_buf = cmd;
96 t.len = WSPI_INIT_CMD_LEN;
97 spi_message_add_tail(&t, &m);
98
99 spi_sync(to_spi_device(glue->dev), &m);
100
101 kfree(cmd);
102}
103
104static void wl12xx_spi_init(struct device *child)
105{
106 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
107 u8 crc[WSPI_INIT_CMD_CRC_LEN], *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 init\n");
115 return;
116 }
117
118 memset(crc, 0, sizeof(crc));
119 memset(&t, 0, sizeof(t));
120 spi_message_init(&m);
121
122 /*
123 * Set WSPI_INIT_COMMAND
124 * the data is being send from the MSB to LSB
125 */
126 cmd[2] = 0xff;
127 cmd[3] = 0xff;
128 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
129 cmd[0] = 0;
130 cmd[7] = 0;
131 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
132 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
133
134 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
135 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
136 else
137 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
138
139 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
140 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
141
142 crc[0] = cmd[1];
143 crc[1] = cmd[0];
144 crc[2] = cmd[7];
145 crc[3] = cmd[6];
146 crc[4] = cmd[5];
147
148 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
149 cmd[4] |= WSPI_INIT_CMD_END;
150
151 t.tx_buf = cmd;
152 t.len = WSPI_INIT_CMD_LEN;
153 spi_message_add_tail(&t, &m);
154
155 spi_sync(to_spi_device(glue->dev), &m);
156 kfree(cmd);
157}
158
159#define WL1271_BUSY_WORD_TIMEOUT 1000
160
161static int wl12xx_spi_read_busy(struct device *child)
162{
163 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
164 struct wl1271 *wl = dev_get_drvdata(child);
165 struct spi_transfer t[1];
166 struct spi_message m;
167 u32 *busy_buf;
168 int num_busy_bytes = 0;
169
170 /*
171 * Read further busy words from SPI until a non-busy word is
172 * encountered, then read the data itself into the buffer.
173 */
174
175 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
176 busy_buf = wl->buffer_busyword;
177 while (num_busy_bytes) {
178 num_busy_bytes--;
179 spi_message_init(&m);
180 memset(t, 0, sizeof(t));
181 t[0].rx_buf = busy_buf;
182 t[0].len = sizeof(u32);
183 t[0].cs_change = true;
184 spi_message_add_tail(&t[0], &m);
185 spi_sync(to_spi_device(glue->dev), &m);
186
187 if (*busy_buf & 0x1)
188 return 0;
189 }
190
191 /* The SPI bus is unresponsive, the read failed. */
192 dev_err(child->parent, "SPI read busy-word timeout!\n");
193 return -ETIMEDOUT;
194}
195
196static void wl12xx_spi_raw_read(struct device *child, int addr, void *buf,
197 size_t len, bool fixed)
198{
199 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
200 struct wl1271 *wl = dev_get_drvdata(child);
201 struct spi_transfer t[2];
202 struct spi_message m;
203 u32 *busy_buf;
204 u32 *cmd;
205 u32 chunk_len;
206
207 while (len > 0) {
208 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
209
210 cmd = &wl->buffer_cmd;
211 busy_buf = wl->buffer_busyword;
212
213 *cmd = 0;
214 *cmd |= WSPI_CMD_READ;
215 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
216 WSPI_CMD_BYTE_LENGTH;
217 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
218
219 if (fixed)
220 *cmd |= WSPI_CMD_FIXED;
221
222 spi_message_init(&m);
223 memset(t, 0, sizeof(t));
224
225 t[0].tx_buf = cmd;
226 t[0].len = 4;
227 t[0].cs_change = true;
228 spi_message_add_tail(&t[0], &m);
229
230 /* Busy and non busy words read */
231 t[1].rx_buf = busy_buf;
232 t[1].len = WL1271_BUSY_WORD_LEN;
233 t[1].cs_change = true;
234 spi_message_add_tail(&t[1], &m);
235
236 spi_sync(to_spi_device(glue->dev), &m);
237
238 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
239 wl12xx_spi_read_busy(child)) {
240 memset(buf, 0, chunk_len);
241 return;
242 }
243
244 spi_message_init(&m);
245 memset(t, 0, sizeof(t));
246
247 t[0].rx_buf = buf;
248 t[0].len = chunk_len;
249 t[0].cs_change = true;
250 spi_message_add_tail(&t[0], &m);
251
252 spi_sync(to_spi_device(glue->dev), &m);
253
254 if (!fixed)
255 addr += chunk_len;
256 buf += chunk_len;
257 len -= chunk_len;
258 }
259}
260
261static void wl12xx_spi_raw_write(struct device *child, int addr, void *buf,
262 size_t len, bool fixed)
263{
264 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
265 struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS];
266 struct spi_message m;
267 u32 commands[WSPI_MAX_NUM_OF_CHUNKS];
268 u32 *cmd;
269 u32 chunk_len;
270 int i;
271
272 WARN_ON(len > WL1271_AGGR_BUFFER_SIZE);
273
274 spi_message_init(&m);
275 memset(t, 0, sizeof(t));
276
277 cmd = &commands[0];
278 i = 0;
279 while (len > 0) {
280 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
281
282 *cmd = 0;
283 *cmd |= WSPI_CMD_WRITE;
284 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
285 WSPI_CMD_BYTE_LENGTH;
286 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
287
288 if (fixed)
289 *cmd |= WSPI_CMD_FIXED;
290
291 t[i].tx_buf = cmd;
292 t[i].len = sizeof(*cmd);
293 spi_message_add_tail(&t[i++], &m);
294
295 t[i].tx_buf = buf;
296 t[i].len = chunk_len;
297 spi_message_add_tail(&t[i++], &m);
298
299 if (!fixed)
300 addr += chunk_len;
301 buf += chunk_len;
302 len -= chunk_len;
303 cmd++;
304 }
305
306 spi_sync(to_spi_device(glue->dev), &m);
307}
308
309static struct wl1271_if_operations spi_ops = {
310 .read = wl12xx_spi_raw_read,
311 .write = wl12xx_spi_raw_write,
312 .reset = wl12xx_spi_reset,
313 .init = wl12xx_spi_init,
314 .set_block_size = NULL,
315};
316
317static int __devinit wl1271_probe(struct spi_device *spi)
318{
319 struct wl12xx_spi_glue *glue;
320 struct wl12xx_platform_data *pdata;
321 struct resource res[1];
322 int ret = -ENOMEM;
323
324 pdata = spi->dev.platform_data;
325 if (!pdata) {
326 dev_err(&spi->dev, "no platform data\n");
327 return -ENODEV;
328 }
329
330 pdata->ops = &spi_ops;
331
332 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
333 if (!glue) {
334 dev_err(&spi->dev, "can't allocate glue\n");
335 goto out;
336 }
337
338 glue->dev = &spi->dev;
339
340 spi_set_drvdata(spi, glue);
341
342 /* This is the only SPI value that we need to set here, the rest
343 * comes from the board-peripherals file */
344 spi->bits_per_word = 32;
345
346 ret = spi_setup(spi);
347 if (ret < 0) {
348 dev_err(glue->dev, "spi_setup failed\n");
349 goto out_free_glue;
350 }
351
352 glue->core = platform_device_alloc("wl12xx", -1);
353 if (!glue->core) {
354 dev_err(glue->dev, "can't allocate platform_device\n");
355 ret = -ENOMEM;
356 goto out_free_glue;
357 }
358
359 glue->core->dev.parent = &spi->dev;
360
361 memset(res, 0x00, sizeof(res));
362
363 res[0].start = spi->irq;
364 res[0].flags = IORESOURCE_IRQ;
365 res[0].name = "irq";
366
367 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
368 if (ret) {
369 dev_err(glue->dev, "can't add resources\n");
370 goto out_dev_put;
371 }
372
373 ret = platform_device_add_data(glue->core, pdata, sizeof(*pdata));
374 if (ret) {
375 dev_err(glue->dev, "can't add platform data\n");
376 goto out_dev_put;
377 }
378
379 ret = platform_device_add(glue->core);
380 if (ret) {
381 dev_err(glue->dev, "can't register platform device\n");
382 goto out_dev_put;
383 }
384
385 return 0;
386
387out_dev_put:
388 platform_device_put(glue->core);
389
390out_free_glue:
391 kfree(glue);
392out:
393 return ret;
394}
395
396static int __devexit wl1271_remove(struct spi_device *spi)
397{
398 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
399
400 platform_device_del(glue->core);
401 platform_device_put(glue->core);
402 kfree(glue);
403
404 return 0;
405}
406
407
408static struct spi_driver wl1271_spi_driver = {
409 .driver = {
410 .name = "wl1271_spi",
411 .owner = THIS_MODULE,
412 },
413
414 .probe = wl1271_probe,
415 .remove = __devexit_p(wl1271_remove),
416};
417
418static int __init wl1271_init(void)
419{
420 return spi_register_driver(&wl1271_spi_driver);
421}
422
423static void __exit wl1271_exit(void)
424{
425 spi_unregister_driver(&wl1271_spi_driver);
426}
427
428module_init(wl1271_init);
429module_exit(wl1271_exit);
430
431MODULE_LICENSE("GPL");
432MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
433MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
434MODULE_FIRMWARE(WL127X_FW_NAME_SINGLE);
435MODULE_FIRMWARE(WL127X_FW_NAME_MULTI);
436MODULE_FIRMWARE(WL127X_PLT_FW_NAME);
437MODULE_FIRMWARE(WL128X_FW_NAME_SINGLE);
438MODULE_FIRMWARE(WL128X_FW_NAME_MULTI);
439MODULE_FIRMWARE(WL128X_PLT_FW_NAME);
440MODULE_ALIAS("spi:wl1271");