]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/wl12xx/wl1271_spi.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / drivers / net / wireless / wl12xx / wl1271_spi.c
CommitLineData
f5fc0f86
LC
1/*
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
2d5e82b8 24#include <linux/irq.h>
f5fc0f86 25#include <linux/module.h>
f5fc0f86
LC
26#include <linux/crc7.h>
27#include <linux/spi/spi.h>
c1f9a095 28#include <linux/wl12xx.h>
5a0e3ad6 29#include <linux/slab.h>
f5fc0f86
LC
30
31#include "wl1271.h"
32#include "wl12xx_80211.h"
2d5e82b8 33#include "wl1271_io.h"
f5fc0f86 34
760d969f
TP
35#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
b42f91ba 66static inline struct spi_device *wl_to_spi(struct wl1271 *wl)
8197b711
TP
67{
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}
f5fc0f86 75
760d969f 76static void wl1271_spi_disable_interrupts(struct wl1271 *wl)
54f7e503
TP
77{
78 disable_irq(wl->irq);
79}
80
760d969f 81static void wl1271_spi_enable_interrupts(struct wl1271 *wl)
54f7e503
TP
82{
83 enable_irq(wl->irq);
84}
85
760d969f 86static void wl1271_spi_reset(struct wl1271 *wl)
f5fc0f86
LC
87{
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
8197b711 107 spi_sync(wl_to_spi(wl), &m);
f83cce35 108 kfree(cmd);
f5fc0f86
LC
109
110 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
111}
112
760d969f 113static void wl1271_spi_init(struct wl1271 *wl)
f5fc0f86
LC
114{
115 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
116 struct spi_transfer t;
117 struct spi_message m;
118
119 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
120 if (!cmd) {
121 wl1271_error("could not allocate cmd for spi init");
122 return;
123 }
124
125 memset(crc, 0, sizeof(crc));
126 memset(&t, 0, sizeof(t));
127 spi_message_init(&m);
128
129 /*
130 * Set WSPI_INIT_COMMAND
131 * the data is being send from the MSB to LSB
132 */
133 cmd[2] = 0xff;
134 cmd[3] = 0xff;
135 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
136 cmd[0] = 0;
137 cmd[7] = 0;
138 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
139 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
140
141 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
142 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
143 else
144 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
145
146 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
147 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
148
149 crc[0] = cmd[1];
150 crc[1] = cmd[0];
151 crc[2] = cmd[7];
152 crc[3] = cmd[6];
153 crc[4] = cmd[5];
154
155 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
156 cmd[4] |= WSPI_INIT_CMD_END;
157
158 t.tx_buf = cmd;
159 t.len = WSPI_INIT_CMD_LEN;
160 spi_message_add_tail(&t, &m);
161
8197b711 162 spi_sync(wl_to_spi(wl), &m);
f5fc0f86 163 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
bb123611 164 kfree(cmd);
f5fc0f86
LC
165}
166
545f1da8
JO
167#define WL1271_BUSY_WORD_TIMEOUT 1000
168
259da430 169static int wl1271_spi_read_busy(struct wl1271 *wl)
545f1da8
JO
170{
171 struct spi_transfer t[1];
172 struct spi_message m;
173 u32 *busy_buf;
174 int num_busy_bytes = 0;
175
545f1da8
JO
176 /*
177 * Read further busy words from SPI until a non-busy word is
178 * encountered, then read the data itself into the buffer.
179 */
545f1da8
JO
180
181 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
182 busy_buf = wl->buffer_busyword;
183 while (num_busy_bytes) {
184 num_busy_bytes--;
185 spi_message_init(&m);
186 memset(t, 0, sizeof(t));
187 t[0].rx_buf = busy_buf;
188 t[0].len = sizeof(u32);
259da430 189 t[0].cs_change = true;
545f1da8 190 spi_message_add_tail(&t[0], &m);
8197b711 191 spi_sync(wl_to_spi(wl), &m);
545f1da8 192
259da430
JO
193 if (*busy_buf & 0x1)
194 return 0;
545f1da8
JO
195 }
196
197 /* The SPI bus is unresponsive, the read failed. */
545f1da8 198 wl1271_error("SPI read busy-word timeout!\n");
259da430 199 return -ETIMEDOUT;
545f1da8
JO
200}
201
760d969f 202static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
259da430 203 size_t len, bool fixed)
f5fc0f86
LC
204{
205 struct spi_transfer t[3];
206 struct spi_message m;
545f1da8 207 u32 *busy_buf;
f5fc0f86
LC
208 u32 *cmd;
209
210 cmd = &wl->buffer_cmd;
211 busy_buf = wl->buffer_busyword;
212
213 *cmd = 0;
214 *cmd |= WSPI_CMD_READ;
215 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
216 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
217
218 if (fixed)
219 *cmd |= WSPI_CMD_FIXED;
220
221 spi_message_init(&m);
222 memset(t, 0, sizeof(t));
223
224 t[0].tx_buf = cmd;
225 t[0].len = 4;
259da430 226 t[0].cs_change = true;
f5fc0f86
LC
227 spi_message_add_tail(&t[0], &m);
228
229 /* Busy and non busy words read */
230 t[1].rx_buf = busy_buf;
231 t[1].len = WL1271_BUSY_WORD_LEN;
259da430 232 t[1].cs_change = true;
f5fc0f86
LC
233 spi_message_add_tail(&t[1], &m);
234
8197b711 235 spi_sync(wl_to_spi(wl), &m);
f5fc0f86 236
259da430
JO
237 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
238 wl1271_spi_read_busy(wl)) {
239 memset(buf, 0, len);
240 return;
241 }
242
243 spi_message_init(&m);
244 memset(t, 0, sizeof(t));
245
246 t[0].rx_buf = buf;
247 t[0].len = len;
248 t[0].cs_change = true;
249 spi_message_add_tail(&t[0], &m);
250
251 spi_sync(wl_to_spi(wl), &m);
f5fc0f86
LC
252
253 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
254 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
255}
256
760d969f 257static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
74621417 258 size_t len, bool fixed)
f5fc0f86
LC
259{
260 struct spi_transfer t[2];
261 struct spi_message m;
262 u32 *cmd;
263
264 cmd = &wl->buffer_cmd;
265
266 *cmd = 0;
267 *cmd |= WSPI_CMD_WRITE;
268 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
269 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
270
271 if (fixed)
272 *cmd |= WSPI_CMD_FIXED;
273
274 spi_message_init(&m);
275 memset(t, 0, sizeof(t));
276
277 t[0].tx_buf = cmd;
278 t[0].len = sizeof(*cmd);
279 spi_message_add_tail(&t[0], &m);
280
281 t[1].tx_buf = buf;
282 t[1].len = len;
283 spi_message_add_tail(&t[1], &m);
284
8197b711 285 spi_sync(wl_to_spi(wl), &m);
f5fc0f86
LC
286
287 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
288 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
289}
2d5e82b8
TP
290
291static irqreturn_t wl1271_irq(int irq, void *cookie)
292{
293 struct wl1271 *wl;
294 unsigned long flags;
295
296 wl1271_debug(DEBUG_IRQ, "IRQ");
297
298 wl = cookie;
299
300 /* complete the ELP completion */
301 spin_lock_irqsave(&wl->wl_lock, flags);
302 if (wl->elp_compl) {
303 complete(wl->elp_compl);
304 wl->elp_compl = NULL;
305 }
306
1e73eb62
JO
307 if (!test_and_set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
308 ieee80211_queue_work(wl->hw, &wl->irq_work);
309 set_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags);
2d5e82b8
TP
310 spin_unlock_irqrestore(&wl->wl_lock, flags);
311
312 return IRQ_HANDLED;
313}
314
2cc78ff7 315static int wl1271_spi_set_power(struct wl1271 *wl, bool enable)
becd551c
TP
316{
317 if (wl->set_power)
318 wl->set_power(enable);
2cc78ff7
OBC
319
320 return 0;
becd551c
TP
321}
322
8197b711
TP
323static struct wl1271_if_operations spi_ops = {
324 .read = wl1271_spi_raw_read,
325 .write = wl1271_spi_raw_write,
326 .reset = wl1271_spi_reset,
327 .init = wl1271_spi_init,
becd551c 328 .power = wl1271_spi_set_power,
8197b711
TP
329 .dev = wl1271_spi_wl_to_dev,
330 .enable_irq = wl1271_spi_enable_interrupts,
331 .disable_irq = wl1271_spi_disable_interrupts
332};
333
2d5e82b8
TP
334static int __devinit wl1271_probe(struct spi_device *spi)
335{
336 struct wl12xx_platform_data *pdata;
337 struct ieee80211_hw *hw;
338 struct wl1271 *wl;
339 int ret;
340
341 pdata = spi->dev.platform_data;
342 if (!pdata) {
343 wl1271_error("no platform data");
344 return -ENODEV;
345 }
346
347 hw = wl1271_alloc_hw();
348 if (IS_ERR(hw))
349 return PTR_ERR(hw);
350
351 wl = hw->priv;
352
353 dev_set_drvdata(&spi->dev, wl);
8197b711
TP
354 wl->if_priv = spi;
355
356 wl->if_ops = &spi_ops;
2d5e82b8
TP
357
358 /* This is the only SPI value that we need to set here, the rest
359 * comes from the board-peripherals file */
360 spi->bits_per_word = 32;
361
362 ret = spi_setup(spi);
363 if (ret < 0) {
364 wl1271_error("spi_setup failed");
365 goto out_free;
366 }
367
368 wl->set_power = pdata->set_power;
369 if (!wl->set_power) {
370 wl1271_error("set power function missing in platform data");
371 ret = -ENODEV;
372 goto out_free;
373 }
374
15cea993
OBC
375 wl->ref_clock = pdata->board_ref_clock;
376
2d5e82b8
TP
377 wl->irq = spi->irq;
378 if (wl->irq < 0) {
379 wl1271_error("irq missing in platform data");
380 ret = -ENODEV;
381 goto out_free;
382 }
383
384 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
385 if (ret < 0) {
386 wl1271_error("request_irq() failed: %d", ret);
387 goto out_free;
388 }
389
390 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
391
392 disable_irq(wl->irq);
393
2d5e82b8
TP
394 ret = wl1271_init_ieee80211(wl);
395 if (ret)
a1dd8187 396 goto out_irq;
2d5e82b8
TP
397
398 ret = wl1271_register_hw(wl);
399 if (ret)
a1dd8187 400 goto out_irq;
2d5e82b8
TP
401
402 wl1271_notice("initialized");
403
404 return 0;
405
2d5e82b8
TP
406 out_irq:
407 free_irq(wl->irq, wl);
408
409 out_free:
3b56dd6a 410 wl1271_free_hw(wl);
2d5e82b8
TP
411
412 return ret;
413}
414
415static int __devexit wl1271_remove(struct spi_device *spi)
416{
417 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
418
2d5e82b8
TP
419 free_irq(wl->irq, wl);
420
3b56dd6a 421 wl1271_unregister_hw(wl);
2d5e82b8
TP
422 wl1271_free_hw(wl);
423
424 return 0;
425}
426
427
428static struct spi_driver wl1271_spi_driver = {
429 .driver = {
7fdd50d0 430 .name = "wl1271_spi",
2d5e82b8
TP
431 .bus = &spi_bus_type,
432 .owner = THIS_MODULE,
433 },
434
435 .probe = wl1271_probe,
436 .remove = __devexit_p(wl1271_remove),
437};
438
439static int __init wl1271_init(void)
440{
441 int ret;
442
443 ret = spi_register_driver(&wl1271_spi_driver);
444 if (ret < 0) {
445 wl1271_error("failed to register spi driver: %d", ret);
446 goto out;
447 }
448
449out:
450 return ret;
451}
452
453static void __exit wl1271_exit(void)
454{
455 spi_unregister_driver(&wl1271_spi_driver);
456
457 wl1271_notice("unloaded");
458}
459
460module_init(wl1271_init);
461module_exit(wl1271_exit);
462
463MODULE_LICENSE("GPL");
464MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
465MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
466MODULE_FIRMWARE(WL1271_FW_NAME);
f148cfdd 467MODULE_ALIAS("spi:wl1271");