blob: 51171d7d5da37a680335a79dd694d8f5bdef950a [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"
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +020033#include "wl1271_io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030034
Teemu Paasikivi760d9692010-02-22 08:38:25 +020035#include "wl1271_reg.h"
36
37#define WSPI_CMD_READ 0x40000000
38#define WSPI_CMD_WRITE 0x00000000
39#define WSPI_CMD_FIXED 0x20000000
40#define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
41#define WSPI_CMD_BYTE_LENGTH_OFFSET 17
42#define WSPI_CMD_BYTE_ADDR 0x0001FFFF
43
44#define WSPI_INIT_CMD_CRC_LEN 5
45
46#define WSPI_INIT_CMD_START 0x00
47#define WSPI_INIT_CMD_TX 0x40
48/* the extra bypass bit is sampled by the TNET as '1' */
49#define WSPI_INIT_CMD_BYPASS_BIT 0x80
50#define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
51#define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
52#define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
53#define WSPI_INIT_CMD_IOD 0x40
54#define WSPI_INIT_CMD_IP 0x20
55#define WSPI_INIT_CMD_CS 0x10
56#define WSPI_INIT_CMD_WS 0x08
57#define WSPI_INIT_CMD_WSPI 0x01
58#define WSPI_INIT_CMD_END 0x01
59
60#define WSPI_INIT_CMD_LEN 8
61
62#define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
63 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
64#define HW_ACCESS_WSPI_INIT_CMD_MASK 0
65
Teemu Paasikivib42f91b2010-02-22 08:38:24 +020066static inline struct spi_device *wl_to_spi(struct wl1271 *wl)
Teemu Paasikivi8197b712010-02-22 08:38:23 +020067{
68 return wl->if_priv;
69}
70
71static struct device *wl1271_spi_wl_to_dev(struct wl1271 *wl)
72{
73 return &(wl_to_spi(wl)->dev);
74}
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030075
Teemu Paasikivi760d9692010-02-22 08:38:25 +020076static void wl1271_spi_disable_interrupts(struct wl1271 *wl)
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020077{
78 disable_irq(wl->irq);
79}
80
Teemu Paasikivi760d9692010-02-22 08:38:25 +020081static void wl1271_spi_enable_interrupts(struct wl1271 *wl)
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020082{
83 enable_irq(wl->irq);
84}
85
Teemu Paasikivi760d9692010-02-22 08:38:25 +020086static void wl1271_spi_reset(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030087{
88 u8 *cmd;
89 struct spi_transfer t;
90 struct spi_message m;
91
92 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
93 if (!cmd) {
94 wl1271_error("could not allocate cmd for spi reset");
95 return;
96 }
97
98 memset(&t, 0, sizeof(t));
99 spi_message_init(&m);
100
101 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
102
103 t.tx_buf = cmd;
104 t.len = WSPI_INIT_CMD_LEN;
105 spi_message_add_tail(&t, &m);
106
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200107 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300108
109 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
110}
111
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200112static void wl1271_spi_init(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300113{
114 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
115 struct spi_transfer t;
116 struct spi_message m;
117
118 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
119 if (!cmd) {
120 wl1271_error("could not allocate cmd for spi init");
121 return;
122 }
123
124 memset(crc, 0, sizeof(crc));
125 memset(&t, 0, sizeof(t));
126 spi_message_init(&m);
127
128 /*
129 * Set WSPI_INIT_COMMAND
130 * the data is being send from the MSB to LSB
131 */
132 cmd[2] = 0xff;
133 cmd[3] = 0xff;
134 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
135 cmd[0] = 0;
136 cmd[7] = 0;
137 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
138 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
139
140 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
141 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
142 else
143 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
144
145 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
146 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
147
148 crc[0] = cmd[1];
149 crc[1] = cmd[0];
150 crc[2] = cmd[7];
151 crc[3] = cmd[6];
152 crc[4] = cmd[5];
153
154 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
155 cmd[4] |= WSPI_INIT_CMD_END;
156
157 t.tx_buf = cmd;
158 t.len = WSPI_INIT_CMD_LEN;
159 spi_message_add_tail(&t, &m);
160
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200161 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300162
163 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
164}
165
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300166#define WL1271_BUSY_WORD_TIMEOUT 1000
167
Luciano Coelho938e30c2009-10-15 10:33:27 +0300168/* FIXME: Check busy words, removed due to SPI bug */
169#if 0
170static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300171{
172 struct spi_transfer t[1];
173 struct spi_message m;
174 u32 *busy_buf;
175 int num_busy_bytes = 0;
176
177 wl1271_info("spi read BUSY!");
178
179 /*
180 * Look for the non-busy word in the read buffer, and if found,
181 * read in the remaining data into the buffer.
182 */
183 busy_buf = (u32 *)buf;
184 for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
185 num_busy_bytes += sizeof(u32);
186 if (*busy_buf & 0x1) {
187 spi_message_init(&m);
188 memset(t, 0, sizeof(t));
189 memmove(buf, busy_buf, len - num_busy_bytes);
190 t[0].rx_buf = buf + (len - num_busy_bytes);
191 t[0].len = num_busy_bytes;
192 spi_message_add_tail(&t[0], &m);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200193 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300194 return;
195 }
196 }
197
198 /*
199 * Read further busy words from SPI until a non-busy word is
200 * encountered, then read the data itself into the buffer.
201 */
202 wl1271_info("spi read BUSY-polling needed!");
203
204 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
205 busy_buf = wl->buffer_busyword;
206 while (num_busy_bytes) {
207 num_busy_bytes--;
208 spi_message_init(&m);
209 memset(t, 0, sizeof(t));
210 t[0].rx_buf = busy_buf;
211 t[0].len = sizeof(u32);
212 spi_message_add_tail(&t[0], &m);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200213 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300214
215 if (*busy_buf & 0x1) {
216 spi_message_init(&m);
217 memset(t, 0, sizeof(t));
218 t[0].rx_buf = buf;
219 t[0].len = len;
220 spi_message_add_tail(&t[0], &m);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200221 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300222 return;
223 }
224 }
225
226 /* The SPI bus is unresponsive, the read failed. */
227 memset(buf, 0, len);
228 wl1271_error("SPI read busy-word timeout!\n");
229}
Luciano Coelho938e30c2009-10-15 10:33:27 +0300230#endif
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300231
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200232static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
Juuso Oikarinen74621412009-10-12 15:08:54 +0300233 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300234{
235 struct spi_transfer t[3];
236 struct spi_message m;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300237 u32 *busy_buf;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300238 u32 *cmd;
239
240 cmd = &wl->buffer_cmd;
241 busy_buf = wl->buffer_busyword;
242
243 *cmd = 0;
244 *cmd |= WSPI_CMD_READ;
245 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
246 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
247
248 if (fixed)
249 *cmd |= WSPI_CMD_FIXED;
250
251 spi_message_init(&m);
252 memset(t, 0, sizeof(t));
253
254 t[0].tx_buf = cmd;
255 t[0].len = 4;
256 spi_message_add_tail(&t[0], &m);
257
258 /* Busy and non busy words read */
259 t[1].rx_buf = busy_buf;
260 t[1].len = WL1271_BUSY_WORD_LEN;
261 spi_message_add_tail(&t[1], &m);
262
263 t[2].rx_buf = buf;
264 t[2].len = len;
265 spi_message_add_tail(&t[2], &m);
266
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200267 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300268
Juuso Oikarinenc6d5d062009-10-13 12:47:47 +0300269 /* FIXME: Check busy words, removed due to SPI bug */
270 /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
271 wl1271_spi_read_busy(wl, buf, len); */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300272
273 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
274 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
275}
276
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200277static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
Juuso Oikarinen74621412009-10-12 15:08:54 +0300278 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300279{
280 struct spi_transfer t[2];
281 struct spi_message m;
282 u32 *cmd;
283
284 cmd = &wl->buffer_cmd;
285
286 *cmd = 0;
287 *cmd |= WSPI_CMD_WRITE;
288 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
289 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
290
291 if (fixed)
292 *cmd |= WSPI_CMD_FIXED;
293
294 spi_message_init(&m);
295 memset(t, 0, sizeof(t));
296
297 t[0].tx_buf = cmd;
298 t[0].len = sizeof(*cmd);
299 spi_message_add_tail(&t[0], &m);
300
301 t[1].tx_buf = buf;
302 t[1].len = len;
303 spi_message_add_tail(&t[1], &m);
304
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200305 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300306
307 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
308 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
309}
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200310
311static irqreturn_t wl1271_irq(int irq, void *cookie)
312{
313 struct wl1271 *wl;
314 unsigned long flags;
315
316 wl1271_debug(DEBUG_IRQ, "IRQ");
317
318 wl = cookie;
319
320 /* complete the ELP completion */
321 spin_lock_irqsave(&wl->wl_lock, flags);
322 if (wl->elp_compl) {
323 complete(wl->elp_compl);
324 wl->elp_compl = NULL;
325 }
326
327 ieee80211_queue_work(wl->hw, &wl->irq_work);
328 spin_unlock_irqrestore(&wl->wl_lock, flags);
329
330 return IRQ_HANDLED;
331}
332
333static void wl1271_device_release(struct device *dev)
334{
335
336}
337
338static struct platform_device wl1271_device = {
339 .name = "wl1271",
340 .id = -1,
341
342 /* device model insists to have a release function */
343 .dev = {
344 .release = wl1271_device_release,
345 },
346};
347
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200348static struct wl1271_if_operations spi_ops = {
349 .read = wl1271_spi_raw_read,
350 .write = wl1271_spi_raw_write,
351 .reset = wl1271_spi_reset,
352 .init = wl1271_spi_init,
353 .dev = wl1271_spi_wl_to_dev,
354 .enable_irq = wl1271_spi_enable_interrupts,
355 .disable_irq = wl1271_spi_disable_interrupts
356};
357
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200358static int __devinit wl1271_probe(struct spi_device *spi)
359{
360 struct wl12xx_platform_data *pdata;
361 struct ieee80211_hw *hw;
362 struct wl1271 *wl;
363 int ret;
364
365 pdata = spi->dev.platform_data;
366 if (!pdata) {
367 wl1271_error("no platform data");
368 return -ENODEV;
369 }
370
371 hw = wl1271_alloc_hw();
372 if (IS_ERR(hw))
373 return PTR_ERR(hw);
374
375 wl = hw->priv;
376
377 dev_set_drvdata(&spi->dev, wl);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200378 wl->if_priv = spi;
379
380 wl->if_ops = &spi_ops;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200381
382 /* This is the only SPI value that we need to set here, the rest
383 * comes from the board-peripherals file */
384 spi->bits_per_word = 32;
385
386 ret = spi_setup(spi);
387 if (ret < 0) {
388 wl1271_error("spi_setup failed");
389 goto out_free;
390 }
391
392 wl->set_power = pdata->set_power;
393 if (!wl->set_power) {
394 wl1271_error("set power function missing in platform data");
395 ret = -ENODEV;
396 goto out_free;
397 }
398
399 wl->irq = spi->irq;
400 if (wl->irq < 0) {
401 wl1271_error("irq missing in platform data");
402 ret = -ENODEV;
403 goto out_free;
404 }
405
406 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
407 if (ret < 0) {
408 wl1271_error("request_irq() failed: %d", ret);
409 goto out_free;
410 }
411
412 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
413
414 disable_irq(wl->irq);
415
416 ret = platform_device_register(&wl1271_device);
417 if (ret) {
418 wl1271_error("couldn't register platform device");
419 goto out_irq;
420 }
421 dev_set_drvdata(&wl1271_device.dev, wl);
422
423 ret = wl1271_init_ieee80211(wl);
424 if (ret)
425 goto out_platform;
426
427 ret = wl1271_register_hw(wl);
428 if (ret)
429 goto out_platform;
430
431 wl1271_notice("initialized");
432
433 return 0;
434
435 out_platform:
436 platform_device_unregister(&wl1271_device);
437
438 out_irq:
439 free_irq(wl->irq, wl);
440
441 out_free:
442 ieee80211_free_hw(hw);
443
444 return ret;
445}
446
447static int __devexit wl1271_remove(struct spi_device *spi)
448{
449 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
450
451 platform_device_unregister(&wl1271_device);
452 free_irq(wl->irq, wl);
453
454 wl1271_free_hw(wl);
455
456 return 0;
457}
458
459
460static struct spi_driver wl1271_spi_driver = {
461 .driver = {
462 .name = "wl1271",
463 .bus = &spi_bus_type,
464 .owner = THIS_MODULE,
465 },
466
467 .probe = wl1271_probe,
468 .remove = __devexit_p(wl1271_remove),
469};
470
471static int __init wl1271_init(void)
472{
473 int ret;
474
475 ret = spi_register_driver(&wl1271_spi_driver);
476 if (ret < 0) {
477 wl1271_error("failed to register spi driver: %d", ret);
478 goto out;
479 }
480
481out:
482 return ret;
483}
484
485static void __exit wl1271_exit(void)
486{
487 spi_unregister_driver(&wl1271_spi_driver);
488
489 wl1271_notice("unloaded");
490}
491
492module_init(wl1271_init);
493module_exit(wl1271_exit);
494
495MODULE_LICENSE("GPL");
496MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
497MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
498MODULE_FIRMWARE(WL1271_FW_NAME);