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/irq.h>
25 #include <linux/module.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/wl12xx.h>
31 #include "wl12xx_80211.h"
32 #include "wl1271_io.h"
34 #include "wl1271_reg.h"
36 #define WSPI_CMD_READ 0x40000000
37 #define WSPI_CMD_WRITE 0x00000000
38 #define WSPI_CMD_FIXED 0x20000000
39 #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
40 #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
41 #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
43 #define WSPI_INIT_CMD_CRC_LEN 5
45 #define WSPI_INIT_CMD_START 0x00
46 #define WSPI_INIT_CMD_TX 0x40
47 /* the extra bypass bit is sampled by the TNET as '1' */
48 #define WSPI_INIT_CMD_BYPASS_BIT 0x80
49 #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
50 #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
51 #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
52 #define WSPI_INIT_CMD_IOD 0x40
53 #define WSPI_INIT_CMD_IP 0x20
54 #define WSPI_INIT_CMD_CS 0x10
55 #define WSPI_INIT_CMD_WS 0x08
56 #define WSPI_INIT_CMD_WSPI 0x01
57 #define WSPI_INIT_CMD_END 0x01
59 #define WSPI_INIT_CMD_LEN 8
61 #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
62 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
63 #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
65 static inline struct spi_device *wl_to_spi(struct wl1271 *wl)
70 static struct device *wl1271_spi_wl_to_dev(struct wl1271 *wl)
72 return &(wl_to_spi(wl)->dev);
75 static void wl1271_spi_disable_interrupts(struct wl1271 *wl)
80 static void wl1271_spi_enable_interrupts(struct wl1271 *wl)
85 static void wl1271_spi_reset(struct wl1271 *wl)
88 struct spi_transfer t;
91 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
93 wl1271_error("could not allocate cmd for spi reset");
97 memset(&t, 0, sizeof(t));
100 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
103 t.len = WSPI_INIT_CMD_LEN;
104 spi_message_add_tail(&t, &m);
106 spi_sync(wl_to_spi(wl), &m);
108 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
111 static void wl1271_spi_init(struct wl1271 *wl)
113 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
114 struct spi_transfer t;
115 struct spi_message m;
117 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
119 wl1271_error("could not allocate cmd for spi init");
123 memset(crc, 0, sizeof(crc));
124 memset(&t, 0, sizeof(t));
125 spi_message_init(&m);
128 * Set WSPI_INIT_COMMAND
129 * the data is being send from the MSB to LSB
133 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
136 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
137 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
139 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
140 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
142 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
144 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
145 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
153 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
154 cmd[4] |= WSPI_INIT_CMD_END;
157 t.len = WSPI_INIT_CMD_LEN;
158 spi_message_add_tail(&t, &m);
160 spi_sync(wl_to_spi(wl), &m);
162 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
165 #define WL1271_BUSY_WORD_TIMEOUT 1000
167 /* FIXME: Check busy words, removed due to SPI bug */
169 static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
171 struct spi_transfer t[1];
172 struct spi_message m;
174 int num_busy_bytes = 0;
176 wl1271_info("spi read BUSY!");
179 * Look for the non-busy word in the read buffer, and if found,
180 * read in the remaining data into the buffer.
182 busy_buf = (u32 *)buf;
183 for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
184 num_busy_bytes += sizeof(u32);
185 if (*busy_buf & 0x1) {
186 spi_message_init(&m);
187 memset(t, 0, sizeof(t));
188 memmove(buf, busy_buf, len - num_busy_bytes);
189 t[0].rx_buf = buf + (len - num_busy_bytes);
190 t[0].len = num_busy_bytes;
191 spi_message_add_tail(&t[0], &m);
192 spi_sync(wl_to_spi(wl), &m);
198 * Read further busy words from SPI until a non-busy word is
199 * encountered, then read the data itself into the buffer.
201 wl1271_info("spi read BUSY-polling needed!");
203 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
204 busy_buf = wl->buffer_busyword;
205 while (num_busy_bytes) {
207 spi_message_init(&m);
208 memset(t, 0, sizeof(t));
209 t[0].rx_buf = busy_buf;
210 t[0].len = sizeof(u32);
211 spi_message_add_tail(&t[0], &m);
212 spi_sync(wl_to_spi(wl), &m);
214 if (*busy_buf & 0x1) {
215 spi_message_init(&m);
216 memset(t, 0, sizeof(t));
219 spi_message_add_tail(&t[0], &m);
220 spi_sync(wl_to_spi(wl), &m);
225 /* The SPI bus is unresponsive, the read failed. */
227 wl1271_error("SPI read busy-word timeout!\n");
231 static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
232 size_t len, bool fixed)
234 struct spi_transfer t[3];
235 struct spi_message m;
239 cmd = &wl->buffer_cmd;
240 busy_buf = wl->buffer_busyword;
243 *cmd |= WSPI_CMD_READ;
244 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
245 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
248 *cmd |= WSPI_CMD_FIXED;
250 spi_message_init(&m);
251 memset(t, 0, sizeof(t));
255 spi_message_add_tail(&t[0], &m);
257 /* Busy and non busy words read */
258 t[1].rx_buf = busy_buf;
259 t[1].len = WL1271_BUSY_WORD_LEN;
260 spi_message_add_tail(&t[1], &m);
264 spi_message_add_tail(&t[2], &m);
266 spi_sync(wl_to_spi(wl), &m);
268 /* FIXME: Check busy words, removed due to SPI bug */
269 /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
270 wl1271_spi_read_busy(wl, buf, len); */
272 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
273 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
276 static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
277 size_t len, bool fixed)
279 struct spi_transfer t[2];
280 struct spi_message m;
283 cmd = &wl->buffer_cmd;
286 *cmd |= WSPI_CMD_WRITE;
287 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
288 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
291 *cmd |= WSPI_CMD_FIXED;
293 spi_message_init(&m);
294 memset(t, 0, sizeof(t));
297 t[0].len = sizeof(*cmd);
298 spi_message_add_tail(&t[0], &m);
302 spi_message_add_tail(&t[1], &m);
304 spi_sync(wl_to_spi(wl), &m);
306 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
307 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
310 static irqreturn_t wl1271_irq(int irq, void *cookie)
315 wl1271_debug(DEBUG_IRQ, "IRQ");
319 /* complete the ELP completion */
320 spin_lock_irqsave(&wl->wl_lock, flags);
322 complete(wl->elp_compl);
323 wl->elp_compl = NULL;
326 if (!test_and_set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
327 ieee80211_queue_work(wl->hw, &wl->irq_work);
328 set_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags);
329 spin_unlock_irqrestore(&wl->wl_lock, flags);
334 static void wl1271_spi_set_power(struct wl1271 *wl, bool enable)
337 wl->set_power(enable);
340 static struct wl1271_if_operations spi_ops = {
341 .read = wl1271_spi_raw_read,
342 .write = wl1271_spi_raw_write,
343 .reset = wl1271_spi_reset,
344 .init = wl1271_spi_init,
345 .power = wl1271_spi_set_power,
346 .dev = wl1271_spi_wl_to_dev,
347 .enable_irq = wl1271_spi_enable_interrupts,
348 .disable_irq = wl1271_spi_disable_interrupts
351 static int __devinit wl1271_probe(struct spi_device *spi)
353 struct wl12xx_platform_data *pdata;
354 struct ieee80211_hw *hw;
358 pdata = spi->dev.platform_data;
360 wl1271_error("no platform data");
364 hw = wl1271_alloc_hw();
370 dev_set_drvdata(&spi->dev, wl);
373 wl->if_ops = &spi_ops;
375 /* This is the only SPI value that we need to set here, the rest
376 * comes from the board-peripherals file */
377 spi->bits_per_word = 32;
379 ret = spi_setup(spi);
381 wl1271_error("spi_setup failed");
385 wl->set_power = pdata->set_power;
386 if (!wl->set_power) {
387 wl1271_error("set power function missing in platform data");
394 wl1271_error("irq missing in platform data");
399 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
401 wl1271_error("request_irq() failed: %d", ret);
405 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
407 disable_irq(wl->irq);
409 ret = wl1271_init_ieee80211(wl);
413 ret = wl1271_register_hw(wl);
417 wl1271_notice("initialized");
422 free_irq(wl->irq, wl);
430 static int __devexit wl1271_remove(struct spi_device *spi)
432 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
434 free_irq(wl->irq, wl);
436 wl1271_unregister_hw(wl);
443 static struct spi_driver wl1271_spi_driver = {
446 .bus = &spi_bus_type,
447 .owner = THIS_MODULE,
450 .probe = wl1271_probe,
451 .remove = __devexit_p(wl1271_remove),
454 static int __init wl1271_init(void)
458 ret = spi_register_driver(&wl1271_spi_driver);
460 wl1271_error("failed to register spi driver: %d", ret);
468 static void __exit wl1271_exit(void)
470 spi_unregister_driver(&wl1271_spi_driver);
472 wl1271_notice("unloaded");
475 module_init(wl1271_init);
476 module_exit(wl1271_exit);
478 MODULE_LICENSE("GPL");
479 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
480 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
481 MODULE_FIRMWARE(WL1271_FW_NAME);