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/crc7.h>
28 #include <linux/spi/spi.h>
29 #include <linux/wl12xx.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
34 #include "wl12xx_80211.h"
39 #define WSPI_CMD_READ 0x40000000
40 #define WSPI_CMD_WRITE 0x00000000
41 #define WSPI_CMD_FIXED 0x20000000
42 #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
43 #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
44 #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
46 #define WSPI_INIT_CMD_CRC_LEN 5
48 #define WSPI_INIT_CMD_START 0x00
49 #define WSPI_INIT_CMD_TX 0x40
50 /* the extra bypass bit is sampled by the TNET as '1' */
51 #define WSPI_INIT_CMD_BYPASS_BIT 0x80
52 #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
53 #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
54 #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
55 #define WSPI_INIT_CMD_IOD 0x40
56 #define WSPI_INIT_CMD_IP 0x20
57 #define WSPI_INIT_CMD_CS 0x10
58 #define WSPI_INIT_CMD_WS 0x08
59 #define WSPI_INIT_CMD_WSPI 0x01
60 #define WSPI_INIT_CMD_END 0x01
62 #define WSPI_INIT_CMD_LEN 8
64 #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
65 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
66 #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
68 /* HW limitation: maximum possible chunk size is 4095 bytes */
69 #define WSPI_MAX_CHUNK_SIZE 4092
71 #define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE)
73 struct wl12xx_spi_glue {
75 struct platform_device *core;
78 static void wl12xx_spi_reset(struct device *child)
80 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
82 struct spi_transfer t;
85 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
87 dev_err(child->parent,
88 "could not allocate cmd for spi reset\n");
92 memset(&t, 0, sizeof(t));
95 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
98 t.len = WSPI_INIT_CMD_LEN;
99 spi_message_add_tail(&t, &m);
101 spi_sync(to_spi_device(glue->dev), &m);
106 static void wl12xx_spi_init(struct device *child)
108 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
109 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
110 struct spi_transfer t;
111 struct spi_message m;
113 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
115 dev_err(child->parent,
116 "could not allocate cmd for spi init\n");
120 memset(crc, 0, sizeof(crc));
121 memset(&t, 0, sizeof(t));
122 spi_message_init(&m);
125 * Set WSPI_INIT_COMMAND
126 * the data is being send from the MSB to LSB
130 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
133 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
134 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
136 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
137 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
139 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
141 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
142 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
150 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
151 cmd[4] |= WSPI_INIT_CMD_END;
154 t.len = WSPI_INIT_CMD_LEN;
155 spi_message_add_tail(&t, &m);
157 spi_sync(to_spi_device(glue->dev), &m);
161 #define WL1271_BUSY_WORD_TIMEOUT 1000
163 static int wl12xx_spi_read_busy(struct device *child)
165 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
166 struct wl1271 *wl = dev_get_drvdata(child);
167 struct spi_transfer t[1];
168 struct spi_message m;
170 int num_busy_bytes = 0;
173 * Read further busy words from SPI until a non-busy word is
174 * encountered, then read the data itself into the buffer.
177 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
178 busy_buf = wl->buffer_busyword;
179 while (num_busy_bytes) {
181 spi_message_init(&m);
182 memset(t, 0, sizeof(t));
183 t[0].rx_buf = busy_buf;
184 t[0].len = sizeof(u32);
185 t[0].cs_change = true;
186 spi_message_add_tail(&t[0], &m);
187 spi_sync(to_spi_device(glue->dev), &m);
193 /* The SPI bus is unresponsive, the read failed. */
194 dev_err(child->parent, "SPI read busy-word timeout!\n");
198 static void wl12xx_spi_raw_read(struct device *child, int addr, void *buf,
199 size_t len, bool fixed)
201 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
202 struct wl1271 *wl = dev_get_drvdata(child);
203 struct spi_transfer t[2];
204 struct spi_message m;
210 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
212 cmd = &wl->buffer_cmd;
213 busy_buf = wl->buffer_busyword;
216 *cmd |= WSPI_CMD_READ;
217 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
218 WSPI_CMD_BYTE_LENGTH;
219 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
222 *cmd |= WSPI_CMD_FIXED;
224 spi_message_init(&m);
225 memset(t, 0, sizeof(t));
229 t[0].cs_change = true;
230 spi_message_add_tail(&t[0], &m);
232 /* Busy and non busy words read */
233 t[1].rx_buf = busy_buf;
234 t[1].len = WL1271_BUSY_WORD_LEN;
235 t[1].cs_change = true;
236 spi_message_add_tail(&t[1], &m);
238 spi_sync(to_spi_device(glue->dev), &m);
240 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
241 wl12xx_spi_read_busy(child)) {
242 memset(buf, 0, chunk_len);
246 spi_message_init(&m);
247 memset(t, 0, sizeof(t));
250 t[0].len = chunk_len;
251 t[0].cs_change = true;
252 spi_message_add_tail(&t[0], &m);
254 spi_sync(to_spi_device(glue->dev), &m);
263 static void wl12xx_spi_raw_write(struct device *child, int addr, void *buf,
264 size_t len, bool fixed)
266 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
267 struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS];
268 struct spi_message m;
269 u32 commands[WSPI_MAX_NUM_OF_CHUNKS];
274 WARN_ON(len > WL1271_AGGR_BUFFER_SIZE);
276 spi_message_init(&m);
277 memset(t, 0, sizeof(t));
282 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
285 *cmd |= WSPI_CMD_WRITE;
286 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
287 WSPI_CMD_BYTE_LENGTH;
288 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
291 *cmd |= WSPI_CMD_FIXED;
294 t[i].len = sizeof(*cmd);
295 spi_message_add_tail(&t[i++], &m);
298 t[i].len = chunk_len;
299 spi_message_add_tail(&t[i++], &m);
308 spi_sync(to_spi_device(glue->dev), &m);
311 static struct wl1271_if_operations spi_ops = {
312 .read = wl12xx_spi_raw_read,
313 .write = wl12xx_spi_raw_write,
314 .reset = wl12xx_spi_reset,
315 .init = wl12xx_spi_init,
316 .set_block_size = NULL,
319 static int __devinit wl1271_probe(struct spi_device *spi)
321 struct wl12xx_spi_glue *glue;
322 struct wl12xx_platform_data *pdata;
323 struct resource res[1];
326 pdata = spi->dev.platform_data;
328 dev_err(&spi->dev, "no platform data\n");
332 pdata->ops = &spi_ops;
334 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
336 dev_err(&spi->dev, "can't allocate glue\n");
340 glue->dev = &spi->dev;
342 spi_set_drvdata(spi, glue);
344 /* This is the only SPI value that we need to set here, the rest
345 * comes from the board-peripherals file */
346 spi->bits_per_word = 32;
348 ret = spi_setup(spi);
350 dev_err(glue->dev, "spi_setup failed\n");
354 glue->core = platform_device_alloc("wl12xx", -1);
356 dev_err(glue->dev, "can't allocate platform_device\n");
361 glue->core->dev.parent = &spi->dev;
363 memset(res, 0x00, sizeof(res));
365 res[0].start = spi->irq;
366 res[0].flags = IORESOURCE_IRQ;
369 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
371 dev_err(glue->dev, "can't add resources\n");
375 ret = platform_device_add_data(glue->core, pdata, sizeof(*pdata));
377 dev_err(glue->dev, "can't add platform data\n");
381 ret = platform_device_add(glue->core);
383 dev_err(glue->dev, "can't register platform device\n");
390 platform_device_put(glue->core);
398 static int __devexit wl1271_remove(struct spi_device *spi)
400 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
402 platform_device_del(glue->core);
403 platform_device_put(glue->core);
410 static struct spi_driver wl1271_spi_driver = {
412 .name = "wl1271_spi",
413 .owner = THIS_MODULE,
416 .probe = wl1271_probe,
417 .remove = __devexit_p(wl1271_remove),
420 static int __init wl1271_init(void)
422 return spi_register_driver(&wl1271_spi_driver);
425 static void __exit wl1271_exit(void)
427 spi_unregister_driver(&wl1271_spi_driver);
430 module_init(wl1271_init);
431 module_exit(wl1271_exit);
433 MODULE_LICENSE("GPL");
434 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
435 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
436 MODULE_FIRMWARE(WL127X_FW_NAME_SINGLE);
437 MODULE_FIRMWARE(WL127X_FW_NAME_MULTI);
438 MODULE_FIRMWARE(WL127X_PLT_FW_NAME);
439 MODULE_FIRMWARE(WL128X_FW_NAME_SINGLE);
440 MODULE_FIRMWARE(WL128X_FW_NAME_MULTI);
441 MODULE_FIRMWARE(WL128X_PLT_FW_NAME);
442 MODULE_ALIAS("spi:wl1271");