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/platform_device.h>
27 #include <linux/crc7.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/wl12xx.h>
32 #include "wl12xx_80211.h"
33 #include "wl1271_spi.h"
34 #include "wl1271_io.h"
37 void wl1271_spi_reset(struct wl1271 *wl)
40 struct spi_transfer t;
43 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
45 wl1271_error("could not allocate cmd for spi reset");
49 memset(&t, 0, sizeof(t));
52 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
55 t.len = WSPI_INIT_CMD_LEN;
56 spi_message_add_tail(&t, &m);
58 spi_sync(wl->spi, &m);
60 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
63 void wl1271_spi_init(struct wl1271 *wl)
65 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
66 struct spi_transfer t;
69 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
71 wl1271_error("could not allocate cmd for spi init");
75 memset(crc, 0, sizeof(crc));
76 memset(&t, 0, sizeof(t));
80 * Set WSPI_INIT_COMMAND
81 * the data is being send from the MSB to LSB
85 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
88 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
89 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
91 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
92 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
94 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
96 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
97 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
105 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
106 cmd[4] |= WSPI_INIT_CMD_END;
109 t.len = WSPI_INIT_CMD_LEN;
110 spi_message_add_tail(&t, &m);
112 spi_sync(wl->spi, &m);
114 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
117 #define WL1271_BUSY_WORD_TIMEOUT 1000
119 /* FIXME: Check busy words, removed due to SPI bug */
121 static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
123 struct spi_transfer t[1];
124 struct spi_message m;
126 int num_busy_bytes = 0;
128 wl1271_info("spi read BUSY!");
131 * Look for the non-busy word in the read buffer, and if found,
132 * read in the remaining data into the buffer.
134 busy_buf = (u32 *)buf;
135 for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
136 num_busy_bytes += sizeof(u32);
137 if (*busy_buf & 0x1) {
138 spi_message_init(&m);
139 memset(t, 0, sizeof(t));
140 memmove(buf, busy_buf, len - num_busy_bytes);
141 t[0].rx_buf = buf + (len - num_busy_bytes);
142 t[0].len = num_busy_bytes;
143 spi_message_add_tail(&t[0], &m);
144 spi_sync(wl->spi, &m);
150 * Read further busy words from SPI until a non-busy word is
151 * encountered, then read the data itself into the buffer.
153 wl1271_info("spi read BUSY-polling needed!");
155 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
156 busy_buf = wl->buffer_busyword;
157 while (num_busy_bytes) {
159 spi_message_init(&m);
160 memset(t, 0, sizeof(t));
161 t[0].rx_buf = busy_buf;
162 t[0].len = sizeof(u32);
163 spi_message_add_tail(&t[0], &m);
164 spi_sync(wl->spi, &m);
166 if (*busy_buf & 0x1) {
167 spi_message_init(&m);
168 memset(t, 0, sizeof(t));
171 spi_message_add_tail(&t[0], &m);
172 spi_sync(wl->spi, &m);
177 /* The SPI bus is unresponsive, the read failed. */
179 wl1271_error("SPI read busy-word timeout!\n");
183 void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
184 size_t len, bool fixed)
186 struct spi_transfer t[3];
187 struct spi_message m;
191 cmd = &wl->buffer_cmd;
192 busy_buf = wl->buffer_busyword;
195 *cmd |= WSPI_CMD_READ;
196 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
197 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
200 *cmd |= WSPI_CMD_FIXED;
202 spi_message_init(&m);
203 memset(t, 0, sizeof(t));
207 spi_message_add_tail(&t[0], &m);
209 /* Busy and non busy words read */
210 t[1].rx_buf = busy_buf;
211 t[1].len = WL1271_BUSY_WORD_LEN;
212 spi_message_add_tail(&t[1], &m);
216 spi_message_add_tail(&t[2], &m);
218 spi_sync(wl->spi, &m);
220 /* FIXME: Check busy words, removed due to SPI bug */
221 /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
222 wl1271_spi_read_busy(wl, buf, len); */
224 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
225 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
228 void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
229 size_t len, bool fixed)
231 struct spi_transfer t[2];
232 struct spi_message m;
235 cmd = &wl->buffer_cmd;
238 *cmd |= WSPI_CMD_WRITE;
239 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
240 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
243 *cmd |= WSPI_CMD_FIXED;
245 spi_message_init(&m);
246 memset(t, 0, sizeof(t));
249 t[0].len = sizeof(*cmd);
250 spi_message_add_tail(&t[0], &m);
254 spi_message_add_tail(&t[1], &m);
256 spi_sync(wl->spi, &m);
258 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
259 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
262 static irqreturn_t wl1271_irq(int irq, void *cookie)
267 wl1271_debug(DEBUG_IRQ, "IRQ");
271 /* complete the ELP completion */
272 spin_lock_irqsave(&wl->wl_lock, flags);
274 complete(wl->elp_compl);
275 wl->elp_compl = NULL;
278 ieee80211_queue_work(wl->hw, &wl->irq_work);
279 spin_unlock_irqrestore(&wl->wl_lock, flags);
284 static void wl1271_device_release(struct device *dev)
289 static struct platform_device wl1271_device = {
293 /* device model insists to have a release function */
295 .release = wl1271_device_release,
299 static int __devinit wl1271_probe(struct spi_device *spi)
301 struct wl12xx_platform_data *pdata;
302 struct ieee80211_hw *hw;
306 pdata = spi->dev.platform_data;
308 wl1271_error("no platform data");
312 hw = wl1271_alloc_hw();
318 dev_set_drvdata(&spi->dev, wl);
321 /* This is the only SPI value that we need to set here, the rest
322 * comes from the board-peripherals file */
323 spi->bits_per_word = 32;
325 ret = spi_setup(spi);
327 wl1271_error("spi_setup failed");
331 wl->set_power = pdata->set_power;
332 if (!wl->set_power) {
333 wl1271_error("set power function missing in platform data");
340 wl1271_error("irq missing in platform data");
345 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
347 wl1271_error("request_irq() failed: %d", ret);
351 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
353 disable_irq(wl->irq);
355 ret = platform_device_register(&wl1271_device);
357 wl1271_error("couldn't register platform device");
360 dev_set_drvdata(&wl1271_device.dev, wl);
362 ret = wl1271_init_ieee80211(wl);
366 ret = wl1271_register_hw(wl);
370 wl1271_notice("initialized");
375 platform_device_unregister(&wl1271_device);
378 free_irq(wl->irq, wl);
381 ieee80211_free_hw(hw);
386 static int __devexit wl1271_remove(struct spi_device *spi)
388 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
390 platform_device_unregister(&wl1271_device);
391 free_irq(wl->irq, wl);
399 static struct spi_driver wl1271_spi_driver = {
402 .bus = &spi_bus_type,
403 .owner = THIS_MODULE,
406 .probe = wl1271_probe,
407 .remove = __devexit_p(wl1271_remove),
410 static int __init wl1271_init(void)
414 ret = spi_register_driver(&wl1271_spi_driver);
416 wl1271_error("failed to register spi driver: %d", ret);
424 static void __exit wl1271_exit(void)
426 spi_unregister_driver(&wl1271_spi_driver);
428 wl1271_notice("unloaded");
431 module_init(wl1271_init);
432 module_exit(wl1271_exit);
434 MODULE_LICENSE("GPL");
435 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
436 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
437 MODULE_FIRMWARE(WL1271_FW_NAME);