blob: b422c9fdcd22691a72058b2bfcdc65114ab5626d [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
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
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +020024#include <linux/irq.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030025#include <linux/module.h>
26#include <linux/platform_device.h>
27#include <linux/crc7.h>
28#include <linux/spi/spi.h>
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +020029#include <linux/spi/wl12xx.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030030
31#include "wl1271.h"
32#include "wl12xx_80211.h"
33#include "wl1271_spi.h"
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +020034#include "wl1271_io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030035
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030036
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020037void wl1271_spi_disable_interrupts(struct wl1271 *wl)
38{
39 disable_irq(wl->irq);
40}
41
42void wl1271_spi_enable_interrupts(struct wl1271 *wl)
43{
44 enable_irq(wl->irq);
45}
46
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030047void wl1271_spi_reset(struct wl1271 *wl)
48{
49 u8 *cmd;
50 struct spi_transfer t;
51 struct spi_message m;
52
53 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
54 if (!cmd) {
55 wl1271_error("could not allocate cmd for spi reset");
56 return;
57 }
58
59 memset(&t, 0, sizeof(t));
60 spi_message_init(&m);
61
62 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
63
64 t.tx_buf = cmd;
65 t.len = WSPI_INIT_CMD_LEN;
66 spi_message_add_tail(&t, &m);
67
68 spi_sync(wl->spi, &m);
69
70 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
71}
72
73void wl1271_spi_init(struct wl1271 *wl)
74{
75 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
76 struct spi_transfer t;
77 struct spi_message m;
78
79 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
80 if (!cmd) {
81 wl1271_error("could not allocate cmd for spi init");
82 return;
83 }
84
85 memset(crc, 0, sizeof(crc));
86 memset(&t, 0, sizeof(t));
87 spi_message_init(&m);
88
89 /*
90 * Set WSPI_INIT_COMMAND
91 * the data is being send from the MSB to LSB
92 */
93 cmd[2] = 0xff;
94 cmd[3] = 0xff;
95 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
96 cmd[0] = 0;
97 cmd[7] = 0;
98 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
99 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
100
101 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
102 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
103 else
104 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
105
106 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
107 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
108
109 crc[0] = cmd[1];
110 crc[1] = cmd[0];
111 crc[2] = cmd[7];
112 crc[3] = cmd[6];
113 crc[4] = cmd[5];
114
115 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
116 cmd[4] |= WSPI_INIT_CMD_END;
117
118 t.tx_buf = cmd;
119 t.len = WSPI_INIT_CMD_LEN;
120 spi_message_add_tail(&t, &m);
121
122 spi_sync(wl->spi, &m);
123
124 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
125}
126
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300127#define WL1271_BUSY_WORD_TIMEOUT 1000
128
Luciano Coelho938e30c2009-10-15 10:33:27 +0300129/* FIXME: Check busy words, removed due to SPI bug */
130#if 0
131static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300132{
133 struct spi_transfer t[1];
134 struct spi_message m;
135 u32 *busy_buf;
136 int num_busy_bytes = 0;
137
138 wl1271_info("spi read BUSY!");
139
140 /*
141 * Look for the non-busy word in the read buffer, and if found,
142 * read in the remaining data into the buffer.
143 */
144 busy_buf = (u32 *)buf;
145 for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
146 num_busy_bytes += sizeof(u32);
147 if (*busy_buf & 0x1) {
148 spi_message_init(&m);
149 memset(t, 0, sizeof(t));
150 memmove(buf, busy_buf, len - num_busy_bytes);
151 t[0].rx_buf = buf + (len - num_busy_bytes);
152 t[0].len = num_busy_bytes;
153 spi_message_add_tail(&t[0], &m);
154 spi_sync(wl->spi, &m);
155 return;
156 }
157 }
158
159 /*
160 * Read further busy words from SPI until a non-busy word is
161 * encountered, then read the data itself into the buffer.
162 */
163 wl1271_info("spi read BUSY-polling needed!");
164
165 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
166 busy_buf = wl->buffer_busyword;
167 while (num_busy_bytes) {
168 num_busy_bytes--;
169 spi_message_init(&m);
170 memset(t, 0, sizeof(t));
171 t[0].rx_buf = busy_buf;
172 t[0].len = sizeof(u32);
173 spi_message_add_tail(&t[0], &m);
174 spi_sync(wl->spi, &m);
175
176 if (*busy_buf & 0x1) {
177 spi_message_init(&m);
178 memset(t, 0, sizeof(t));
179 t[0].rx_buf = buf;
180 t[0].len = len;
181 spi_message_add_tail(&t[0], &m);
182 spi_sync(wl->spi, &m);
183 return;
184 }
185 }
186
187 /* The SPI bus is unresponsive, the read failed. */
188 memset(buf, 0, len);
189 wl1271_error("SPI read busy-word timeout!\n");
190}
Luciano Coelho938e30c2009-10-15 10:33:27 +0300191#endif
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300192
Juuso Oikarinen74621412009-10-12 15:08:54 +0300193void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
194 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300195{
196 struct spi_transfer t[3];
197 struct spi_message m;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300198 u32 *busy_buf;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300199 u32 *cmd;
200
201 cmd = &wl->buffer_cmd;
202 busy_buf = wl->buffer_busyword;
203
204 *cmd = 0;
205 *cmd |= WSPI_CMD_READ;
206 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
207 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
208
209 if (fixed)
210 *cmd |= WSPI_CMD_FIXED;
211
212 spi_message_init(&m);
213 memset(t, 0, sizeof(t));
214
215 t[0].tx_buf = cmd;
216 t[0].len = 4;
217 spi_message_add_tail(&t[0], &m);
218
219 /* Busy and non busy words read */
220 t[1].rx_buf = busy_buf;
221 t[1].len = WL1271_BUSY_WORD_LEN;
222 spi_message_add_tail(&t[1], &m);
223
224 t[2].rx_buf = buf;
225 t[2].len = len;
226 spi_message_add_tail(&t[2], &m);
227
228 spi_sync(wl->spi, &m);
229
Juuso Oikarinenc6d5d062009-10-13 12:47:47 +0300230 /* FIXME: Check busy words, removed due to SPI bug */
231 /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
232 wl1271_spi_read_busy(wl, buf, len); */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300233
234 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
235 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
236}
237
Juuso Oikarinen74621412009-10-12 15:08:54 +0300238void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
239 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300240{
241 struct spi_transfer t[2];
242 struct spi_message m;
243 u32 *cmd;
244
245 cmd = &wl->buffer_cmd;
246
247 *cmd = 0;
248 *cmd |= WSPI_CMD_WRITE;
249 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
250 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
251
252 if (fixed)
253 *cmd |= WSPI_CMD_FIXED;
254
255 spi_message_init(&m);
256 memset(t, 0, sizeof(t));
257
258 t[0].tx_buf = cmd;
259 t[0].len = sizeof(*cmd);
260 spi_message_add_tail(&t[0], &m);
261
262 t[1].tx_buf = buf;
263 t[1].len = len;
264 spi_message_add_tail(&t[1], &m);
265
266 spi_sync(wl->spi, &m);
267
268 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
269 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
270}
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200271
272static irqreturn_t wl1271_irq(int irq, void *cookie)
273{
274 struct wl1271 *wl;
275 unsigned long flags;
276
277 wl1271_debug(DEBUG_IRQ, "IRQ");
278
279 wl = cookie;
280
281 /* complete the ELP completion */
282 spin_lock_irqsave(&wl->wl_lock, flags);
283 if (wl->elp_compl) {
284 complete(wl->elp_compl);
285 wl->elp_compl = NULL;
286 }
287
288 ieee80211_queue_work(wl->hw, &wl->irq_work);
289 spin_unlock_irqrestore(&wl->wl_lock, flags);
290
291 return IRQ_HANDLED;
292}
293
294static void wl1271_device_release(struct device *dev)
295{
296
297}
298
299static struct platform_device wl1271_device = {
300 .name = "wl1271",
301 .id = -1,
302
303 /* device model insists to have a release function */
304 .dev = {
305 .release = wl1271_device_release,
306 },
307};
308
309static int __devinit wl1271_probe(struct spi_device *spi)
310{
311 struct wl12xx_platform_data *pdata;
312 struct ieee80211_hw *hw;
313 struct wl1271 *wl;
314 int ret;
315
316 pdata = spi->dev.platform_data;
317 if (!pdata) {
318 wl1271_error("no platform data");
319 return -ENODEV;
320 }
321
322 hw = wl1271_alloc_hw();
323 if (IS_ERR(hw))
324 return PTR_ERR(hw);
325
326 wl = hw->priv;
327
328 dev_set_drvdata(&spi->dev, wl);
329 wl->spi = spi;
330
331 /* This is the only SPI value that we need to set here, the rest
332 * comes from the board-peripherals file */
333 spi->bits_per_word = 32;
334
335 ret = spi_setup(spi);
336 if (ret < 0) {
337 wl1271_error("spi_setup failed");
338 goto out_free;
339 }
340
341 wl->set_power = pdata->set_power;
342 if (!wl->set_power) {
343 wl1271_error("set power function missing in platform data");
344 ret = -ENODEV;
345 goto out_free;
346 }
347
348 wl->irq = spi->irq;
349 if (wl->irq < 0) {
350 wl1271_error("irq missing in platform data");
351 ret = -ENODEV;
352 goto out_free;
353 }
354
355 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
356 if (ret < 0) {
357 wl1271_error("request_irq() failed: %d", ret);
358 goto out_free;
359 }
360
361 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
362
363 disable_irq(wl->irq);
364
365 ret = platform_device_register(&wl1271_device);
366 if (ret) {
367 wl1271_error("couldn't register platform device");
368 goto out_irq;
369 }
370 dev_set_drvdata(&wl1271_device.dev, wl);
371
372 ret = wl1271_init_ieee80211(wl);
373 if (ret)
374 goto out_platform;
375
376 ret = wl1271_register_hw(wl);
377 if (ret)
378 goto out_platform;
379
380 wl1271_notice("initialized");
381
382 return 0;
383
384 out_platform:
385 platform_device_unregister(&wl1271_device);
386
387 out_irq:
388 free_irq(wl->irq, wl);
389
390 out_free:
391 ieee80211_free_hw(hw);
392
393 return ret;
394}
395
396static int __devexit wl1271_remove(struct spi_device *spi)
397{
398 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
399
400 platform_device_unregister(&wl1271_device);
401 free_irq(wl->irq, wl);
402
403 wl1271_free_hw(wl);
404
405 return 0;
406}
407
408
409static struct spi_driver wl1271_spi_driver = {
410 .driver = {
411 .name = "wl1271",
412 .bus = &spi_bus_type,
413 .owner = THIS_MODULE,
414 },
415
416 .probe = wl1271_probe,
417 .remove = __devexit_p(wl1271_remove),
418};
419
420static int __init wl1271_init(void)
421{
422 int ret;
423
424 ret = spi_register_driver(&wl1271_spi_driver);
425 if (ret < 0) {
426 wl1271_error("failed to register spi driver: %d", ret);
427 goto out;
428 }
429
430out:
431 return ret;
432}
433
434static void __exit wl1271_exit(void)
435{
436 spi_unregister_driver(&wl1271_spi_driver);
437
438 wl1271_notice("unloaded");
439}
440
441module_init(wl1271_init);
442module_exit(wl1271_exit);
443
444MODULE_LICENSE("GPL");
445MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
446MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
447MODULE_FIRMWARE(WL1271_FW_NAME);