2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
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>
37 #include "wl12xx_80211.h"
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
47 #define WSPI_INIT_CMD_CRC_LEN 5
49 #define WSPI_INIT_CMD_START 0x00
50 #define WSPI_INIT_CMD_TX 0x40
51 /* the extra bypass bit is sampled by the TNET as '1' */
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
63 #define WSPI_INIT_CMD_LEN 8
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
69 /* HW limitation: maximum possible chunk size is 4095 bytes */
70 #define WSPI_MAX_CHUNK_SIZE 4092
73 * wl18xx driver aggregation buffer size is (13 * 4K) compared to
74 * (4 * 4K) for wl12xx, so use the larger buffer needed for wl18xx
76 #define SPI_AGGR_BUFFER_SIZE (13 * SZ_4K)
78 /* Maximum number of SPI write chunks */
79 #define WSPI_MAX_NUM_OF_CHUNKS \
80 ((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
82 static const struct wilink_family_data wl127x_data = {
84 .nvs_name = "/*(DEBLOBBED)*/",
87 static const struct wilink_family_data wl128x_data = {
89 .nvs_name = "/*(DEBLOBBED)*/",
92 static const struct wilink_family_data wl18xx_data = {
94 .cfg_name = "/*(DEBLOBBED)*/",
95 .nvs_name = "/*(DEBLOBBED)*/",
98 struct wl12xx_spi_glue {
100 struct platform_device *core;
101 struct regulator *reg; /* Power regulator */
104 static void wl12xx_spi_reset(struct device *child)
106 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
108 struct spi_transfer t;
109 struct spi_message m;
111 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
113 dev_err(child->parent,
114 "could not allocate cmd for spi reset\n");
118 memset(&t, 0, sizeof(t));
119 spi_message_init(&m);
121 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
124 t.len = WSPI_INIT_CMD_LEN;
125 spi_message_add_tail(&t, &m);
127 spi_sync(to_spi_device(glue->dev), &m);
132 static void wl12xx_spi_init(struct device *child)
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);
141 dev_err(child->parent,
142 "could not allocate cmd for spi init\n");
146 memset(&t, 0, sizeof(t));
147 spi_message_init(&m);
150 * Set WSPI_INIT_COMMAND
151 * the data is being send from the MSB to LSB
155 cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
158 cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
159 cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
161 cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
162 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
164 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
165 cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
167 cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
169 cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
172 * The above is the logical order; it must actually be stored
173 * in the buffer byte-swapped.
175 __swab32s((u32 *)cmd);
176 __swab32s((u32 *)cmd+1);
179 t.len = WSPI_INIT_CMD_LEN;
180 spi_message_add_tail(&t, &m);
182 spi_sync(to_spi_device(glue->dev), &m);
184 /* Send extra clocks with inverted CS (high). this is required
185 * by the wilink family in order to successfully enter WSPI mode.
187 spi->mode ^= SPI_CS_HIGH;
188 memset(&m, 0, sizeof(m));
189 spi_message_init(&m);
195 __swab32s((u32 *)cmd);
199 spi_message_add_tail(&t, &m);
201 spi_sync(to_spi_device(glue->dev), &m);
203 /* Restore chip select configration to normal */
204 spi->mode ^= SPI_CS_HIGH;
208 #define WL1271_BUSY_WORD_TIMEOUT 1000
210 static int wl12xx_spi_read_busy(struct device *child)
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;
217 int num_busy_bytes = 0;
220 * Read further busy words from SPI until a non-busy word is
221 * encountered, then read the data itself into the buffer.
224 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
225 busy_buf = wl->buffer_busyword;
226 while (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);
240 /* The SPI bus is unresponsive, the read failed. */
241 dev_err(child->parent, "SPI read busy-word timeout!\n");
245 static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
246 void *buf, size_t len, bool fixed)
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;
257 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
259 cmd = &wl->buffer_cmd;
260 busy_buf = wl->buffer_busyword;
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;
269 *cmd |= WSPI_CMD_FIXED;
271 spi_message_init(&m);
272 memset(t, 0, sizeof(t));
276 t[0].cs_change = true;
277 spi_message_add_tail(&t[0], &m);
279 /* Busy and non busy words read */
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);
285 spi_sync(to_spi_device(glue->dev), &m);
287 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
288 wl12xx_spi_read_busy(child)) {
289 memset(buf, 0, chunk_len);
293 spi_message_init(&m);
294 memset(t, 0, sizeof(t));
297 t[0].len = chunk_len;
298 t[0].cs_change = true;
299 spi_message_add_tail(&t[0], &m);
301 spi_sync(to_spi_device(glue->dev), &m);
312 static int __wl12xx_spi_raw_write(struct device *child, int addr,
313 void *buf, size_t len, bool fixed)
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]; /* 1 command per chunk */
323 /* SPI write buffers - 2 for each chunk */
324 t = kzalloc(sizeof(*t) * 2 * WSPI_MAX_NUM_OF_CHUNKS, GFP_KERNEL);
328 WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
330 spi_message_init(&m);
335 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len);
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;
344 *cmd |= WSPI_CMD_FIXED;
347 t[i].len = sizeof(*cmd);
348 spi_message_add_tail(&t[i++], &m);
351 t[i].len = chunk_len;
352 spi_message_add_tail(&t[i++], &m);
361 spi_sync(to_spi_device(glue->dev), &m);
367 static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
368 void *buf, size_t len, bool fixed)
370 /* The ELP wakeup write may fail the first time due to internal
371 * hardware latency. It is safer to send the wakeup command twice to
372 * avoid unexpected failures.
374 if (addr == HW_ACCESS_ELP_CTRL_REG)
375 __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
377 return __wl12xx_spi_raw_write(child, addr, buf, len, fixed);
381 * wl12xx_spi_set_power - power on/off the wl12xx unit
382 * @child: wl12xx device handle.
383 * @enable: true/false to power on/off the unit.
385 * use the WiFi enable regulator to enable/disable the WiFi unit.
387 static int wl12xx_spi_set_power(struct device *child, bool enable)
390 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
394 /* Update regulator state */
396 ret = regulator_enable(glue->reg);
398 dev_err(child, "Power enable failure\n");
400 ret = regulator_disable(glue->reg);
402 dev_err(child, "Power disable failure\n");
409 * wl12xx_spi_set_block_size
411 * This function is not needed for spi mode, but need to be present.
412 * Without it defined the wlcore fallback to use the wrong packet
415 static void wl12xx_spi_set_block_size(struct device *child,
420 static 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,
429 static 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},
443 MODULE_DEVICE_TABLE(of, wlcore_spi_of_match_table);
446 * wlcore_probe_of - DT node parsing.
447 * @spi: SPI slave device parameters.
448 * @res: resource parameters.
449 * @glue: wl12xx SPI bus to slave device glue parameters.
450 * @pdev_data: wlcore device parameters
452 static int wlcore_probe_of(struct spi_device *spi, struct wl12xx_spi_glue *glue,
453 struct wlcore_platdev_data *pdev_data)
455 struct device_node *dt_node = spi->dev.of_node;
456 const struct of_device_id *of_id;
458 of_id = of_match_node(wlcore_spi_of_match_table, dt_node);
462 pdev_data->family = of_id->data;
463 dev_info(&spi->dev, "selected chip family is %s\n",
464 pdev_data->family->name);
466 if (of_find_property(dt_node, "clock-xtal", NULL))
467 pdev_data->ref_clock_xtal = true;
469 /* optional clock frequency params */
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);
478 static int wl1271_probe(struct spi_device *spi)
480 struct wl12xx_spi_glue *glue;
481 struct wlcore_platdev_data *pdev_data;
482 struct resource res[1];
485 pdev_data = devm_kzalloc(&spi->dev, sizeof(*pdev_data), GFP_KERNEL);
489 pdev_data->if_ops = &spi_ops;
491 glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL);
493 dev_err(&spi->dev, "can't allocate glue\n");
497 glue->dev = &spi->dev;
499 spi_set_drvdata(spi, glue);
501 /* This is the only SPI value that we need to set here, the rest
502 * comes from the board-peripherals file */
503 spi->bits_per_word = 32;
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);
513 ret = wlcore_probe_of(spi, glue, pdev_data);
516 "can't get device tree parameters (%d)\n", ret);
520 ret = spi_setup(spi);
522 dev_err(glue->dev, "spi_setup failed\n");
526 glue->core = platform_device_alloc(pdev_data->family->name,
527 PLATFORM_DEVID_AUTO);
529 dev_err(glue->dev, "can't allocate platform_device\n");
533 glue->core->dev.parent = &spi->dev;
535 memset(res, 0x00, sizeof(res));
537 res[0].start = spi->irq;
538 res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(spi->irq);
541 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
543 dev_err(glue->dev, "can't add resources\n");
547 ret = platform_device_add_data(glue->core, pdev_data,
550 dev_err(glue->dev, "can't add platform data\n");
554 ret = platform_device_add(glue->core);
556 dev_err(glue->dev, "can't register platform device\n");
563 platform_device_put(glue->core);
567 static int wl1271_remove(struct spi_device *spi)
569 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
571 platform_device_unregister(glue->core);
576 static struct spi_driver wl1271_spi_driver = {
578 .name = "wl1271_spi",
579 .of_match_table = of_match_ptr(wlcore_spi_of_match_table),
582 .probe = wl1271_probe,
583 .remove = wl1271_remove,
586 module_spi_driver(wl1271_spi_driver);
587 MODULE_LICENSE("GPL");
588 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
589 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
590 MODULE_ALIAS("spi:wl1271");