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