blob: 96d25fb50495eac40cc160619ab7676d1a933091 [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>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030026#include <linux/crc7.h>
27#include <linux/spi/spi.h>
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +020028#include <linux/spi/wl12xx.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.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);
Juuso Oikarinenf83cce32010-03-26 12:53:13 +0200108 kfree(cmd);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300109
110 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
111}
112
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200113static void wl1271_spi_init(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300114{
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
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200162 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinenf83cce32010-03-26 12:53:13 +0200163 kfree(cmd);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300164
165 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
166}
167
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300168#define WL1271_BUSY_WORD_TIMEOUT 1000
169
Juuso Oikarinen259da432010-03-26 12:53:18 +0200170static int wl1271_spi_read_busy(struct wl1271 *wl)
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
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300177 /*
178 * Read further busy words from SPI until a non-busy word is
179 * encountered, then read the data itself into the buffer.
180 */
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300181
182 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
183 busy_buf = wl->buffer_busyword;
184 while (num_busy_bytes) {
185 num_busy_bytes--;
186 spi_message_init(&m);
187 memset(t, 0, sizeof(t));
188 t[0].rx_buf = busy_buf;
189 t[0].len = sizeof(u32);
Juuso Oikarinen259da432010-03-26 12:53:18 +0200190 t[0].cs_change = true;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300191 spi_message_add_tail(&t[0], &m);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200192 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300193
Juuso Oikarinen259da432010-03-26 12:53:18 +0200194 if (*busy_buf & 0x1)
195 return 0;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300196 }
197
198 /* The SPI bus is unresponsive, the read failed. */
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300199 wl1271_error("SPI read busy-word timeout!\n");
Juuso Oikarinen259da432010-03-26 12:53:18 +0200200 return -ETIMEDOUT;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300201}
202
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200203static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
Juuso Oikarinen259da432010-03-26 12:53:18 +0200204 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300205{
206 struct spi_transfer t[3];
207 struct spi_message m;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300208 u32 *busy_buf;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300209 u32 *cmd;
210
211 cmd = &wl->buffer_cmd;
212 busy_buf = wl->buffer_busyword;
213
214 *cmd = 0;
215 *cmd |= WSPI_CMD_READ;
216 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
217 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
218
219 if (fixed)
220 *cmd |= WSPI_CMD_FIXED;
221
222 spi_message_init(&m);
223 memset(t, 0, sizeof(t));
224
225 t[0].tx_buf = cmd;
226 t[0].len = 4;
Juuso Oikarinen259da432010-03-26 12:53:18 +0200227 t[0].cs_change = true;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300228 spi_message_add_tail(&t[0], &m);
229
230 /* Busy and non busy words read */
231 t[1].rx_buf = busy_buf;
232 t[1].len = WL1271_BUSY_WORD_LEN;
Juuso Oikarinen259da432010-03-26 12:53:18 +0200233 t[1].cs_change = true;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300234 spi_message_add_tail(&t[1], &m);
235
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200236 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300237
Juuso Oikarinen259da432010-03-26 12:53:18 +0200238 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
239 wl1271_spi_read_busy(wl)) {
240 memset(buf, 0, len);
241 return;
242 }
243
244 spi_message_init(&m);
245 memset(t, 0, sizeof(t));
246
247 t[0].rx_buf = buf;
248 t[0].len = len;
249 t[0].cs_change = true;
250 spi_message_add_tail(&t[0], &m);
251
252 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300253
254 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
255 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
256}
257
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200258static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
Juuso Oikarinen74621412009-10-12 15:08:54 +0300259 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300260{
261 struct spi_transfer t[2];
262 struct spi_message m;
263 u32 *cmd;
264
265 cmd = &wl->buffer_cmd;
266
267 *cmd = 0;
268 *cmd |= WSPI_CMD_WRITE;
269 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
270 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
271
272 if (fixed)
273 *cmd |= WSPI_CMD_FIXED;
274
275 spi_message_init(&m);
276 memset(t, 0, sizeof(t));
277
278 t[0].tx_buf = cmd;
279 t[0].len = sizeof(*cmd);
280 spi_message_add_tail(&t[0], &m);
281
282 t[1].tx_buf = buf;
283 t[1].len = len;
284 spi_message_add_tail(&t[1], &m);
285
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200286 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300287
288 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
289 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
290}
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200291
292static irqreturn_t wl1271_irq(int irq, void *cookie)
293{
294 struct wl1271 *wl;
295 unsigned long flags;
296
297 wl1271_debug(DEBUG_IRQ, "IRQ");
298
299 wl = cookie;
300
301 /* complete the ELP completion */
302 spin_lock_irqsave(&wl->wl_lock, flags);
303 if (wl->elp_compl) {
304 complete(wl->elp_compl);
305 wl->elp_compl = NULL;
306 }
307
Juuso Oikarinen1e73eb62010-02-22 08:38:37 +0200308 if (!test_and_set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
309 ieee80211_queue_work(wl->hw, &wl->irq_work);
310 set_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200311 spin_unlock_irqrestore(&wl->wl_lock, flags);
312
313 return IRQ_HANDLED;
314}
315
Teemu Paasikivibecd5512010-03-18 12:26:27 +0200316static void wl1271_spi_set_power(struct wl1271 *wl, bool enable)
317{
318 if (wl->set_power)
319 wl->set_power(enable);
320}
321
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200322static struct wl1271_if_operations spi_ops = {
323 .read = wl1271_spi_raw_read,
324 .write = wl1271_spi_raw_write,
325 .reset = wl1271_spi_reset,
326 .init = wl1271_spi_init,
Teemu Paasikivibecd5512010-03-18 12:26:27 +0200327 .power = wl1271_spi_set_power,
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200328 .dev = wl1271_spi_wl_to_dev,
329 .enable_irq = wl1271_spi_enable_interrupts,
330 .disable_irq = wl1271_spi_disable_interrupts
331};
332
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200333static int __devinit wl1271_probe(struct spi_device *spi)
334{
335 struct wl12xx_platform_data *pdata;
336 struct ieee80211_hw *hw;
337 struct wl1271 *wl;
338 int ret;
339
340 pdata = spi->dev.platform_data;
341 if (!pdata) {
342 wl1271_error("no platform data");
343 return -ENODEV;
344 }
345
346 hw = wl1271_alloc_hw();
347 if (IS_ERR(hw))
348 return PTR_ERR(hw);
349
350 wl = hw->priv;
351
352 dev_set_drvdata(&spi->dev, wl);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200353 wl->if_priv = spi;
354
355 wl->if_ops = &spi_ops;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200356
357 /* This is the only SPI value that we need to set here, the rest
358 * comes from the board-peripherals file */
359 spi->bits_per_word = 32;
360
361 ret = spi_setup(spi);
362 if (ret < 0) {
363 wl1271_error("spi_setup failed");
364 goto out_free;
365 }
366
367 wl->set_power = pdata->set_power;
368 if (!wl->set_power) {
369 wl1271_error("set power function missing in platform data");
370 ret = -ENODEV;
371 goto out_free;
372 }
373
374 wl->irq = spi->irq;
375 if (wl->irq < 0) {
376 wl1271_error("irq missing in platform data");
377 ret = -ENODEV;
378 goto out_free;
379 }
380
381 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
382 if (ret < 0) {
383 wl1271_error("request_irq() failed: %d", ret);
384 goto out_free;
385 }
386
387 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
388
389 disable_irq(wl->irq);
390
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200391 ret = wl1271_init_ieee80211(wl);
392 if (ret)
Juuso Oikarinena1dd8182010-03-18 12:26:31 +0200393 goto out_irq;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200394
395 ret = wl1271_register_hw(wl);
396 if (ret)
Juuso Oikarinena1dd8182010-03-18 12:26:31 +0200397 goto out_irq;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200398
399 wl1271_notice("initialized");
400
401 return 0;
402
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200403 out_irq:
404 free_irq(wl->irq, wl);
405
406 out_free:
Teemu Paasikivi3b56dd62010-03-18 12:26:46 +0200407 wl1271_free_hw(wl);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200408
409 return ret;
410}
411
412static int __devexit wl1271_remove(struct spi_device *spi)
413{
414 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
415
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200416 free_irq(wl->irq, wl);
417
Teemu Paasikivi3b56dd62010-03-18 12:26:46 +0200418 wl1271_unregister_hw(wl);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200419 wl1271_free_hw(wl);
420
421 return 0;
422}
423
424
425static struct spi_driver wl1271_spi_driver = {
426 .driver = {
Luciano Coelho7fdd50d2010-03-26 12:53:10 +0200427 .name = "wl1271_spi",
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200428 .bus = &spi_bus_type,
429 .owner = THIS_MODULE,
430 },
431
432 .probe = wl1271_probe,
433 .remove = __devexit_p(wl1271_remove),
434};
435
436static int __init wl1271_init(void)
437{
438 int ret;
439
440 ret = spi_register_driver(&wl1271_spi_driver);
441 if (ret < 0) {
442 wl1271_error("failed to register spi driver: %d", ret);
443 goto out;
444 }
445
446out:
447 return ret;
448}
449
450static void __exit wl1271_exit(void)
451{
452 spi_unregister_driver(&wl1271_spi_driver);
453
454 wl1271_notice("unloaded");
455}
456
457module_init(wl1271_init);
458module_exit(wl1271_exit);
459
460MODULE_LICENSE("GPL");
461MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
462MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
463MODULE_FIRMWARE(WL1271_FW_NAME);
Ameya Palandef148cfd2010-07-05 17:12:38 +0300464MODULE_ALIAS("spi:wl1271");