blob: 39b7dea6358ee1314d1263101fbb58856dd87444 [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>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030029
30#include "wl1271.h"
31#include "wl12xx_80211.h"
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +020032#include "wl1271_io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030033
Teemu Paasikivi760d9692010-02-22 08:38:25 +020034#include "wl1271_reg.h"
35
36#define WSPI_CMD_READ 0x40000000
37#define WSPI_CMD_WRITE 0x00000000
38#define WSPI_CMD_FIXED 0x20000000
39#define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
40#define WSPI_CMD_BYTE_LENGTH_OFFSET 17
41#define WSPI_CMD_BYTE_ADDR 0x0001FFFF
42
43#define WSPI_INIT_CMD_CRC_LEN 5
44
45#define WSPI_INIT_CMD_START 0x00
46#define WSPI_INIT_CMD_TX 0x40
47/* the extra bypass bit is sampled by the TNET as '1' */
48#define WSPI_INIT_CMD_BYPASS_BIT 0x80
49#define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
50#define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
51#define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
52#define WSPI_INIT_CMD_IOD 0x40
53#define WSPI_INIT_CMD_IP 0x20
54#define WSPI_INIT_CMD_CS 0x10
55#define WSPI_INIT_CMD_WS 0x08
56#define WSPI_INIT_CMD_WSPI 0x01
57#define WSPI_INIT_CMD_END 0x01
58
59#define WSPI_INIT_CMD_LEN 8
60
61#define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
62 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
63#define HW_ACCESS_WSPI_INIT_CMD_MASK 0
64
Teemu Paasikivib42f91b2010-02-22 08:38:24 +020065static inline struct spi_device *wl_to_spi(struct wl1271 *wl)
Teemu Paasikivi8197b712010-02-22 08:38:23 +020066{
67 return wl->if_priv;
68}
69
70static struct device *wl1271_spi_wl_to_dev(struct wl1271 *wl)
71{
72 return &(wl_to_spi(wl)->dev);
73}
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030074
Teemu Paasikivi760d9692010-02-22 08:38:25 +020075static void wl1271_spi_disable_interrupts(struct wl1271 *wl)
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020076{
77 disable_irq(wl->irq);
78}
79
Teemu Paasikivi760d9692010-02-22 08:38:25 +020080static void wl1271_spi_enable_interrupts(struct wl1271 *wl)
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020081{
82 enable_irq(wl->irq);
83}
84
Teemu Paasikivi760d9692010-02-22 08:38:25 +020085static void wl1271_spi_reset(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030086{
87 u8 *cmd;
88 struct spi_transfer t;
89 struct spi_message m;
90
91 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
92 if (!cmd) {
93 wl1271_error("could not allocate cmd for spi reset");
94 return;
95 }
96
97 memset(&t, 0, sizeof(t));
98 spi_message_init(&m);
99
100 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
101
102 t.tx_buf = cmd;
103 t.len = WSPI_INIT_CMD_LEN;
104 spi_message_add_tail(&t, &m);
105
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200106 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinenf83cce32010-03-26 12:53:13 +0200107 kfree(cmd);
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);
Juuso Oikarinenf83cce32010-03-26 12:53:13 +0200162 kfree(cmd);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300163
164 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
165}
166
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300167#define WL1271_BUSY_WORD_TIMEOUT 1000
168
Luciano Coelho938e30c2009-10-15 10:33:27 +0300169/* FIXME: Check busy words, removed due to SPI bug */
170#if 0
171static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300172{
173 struct spi_transfer t[1];
174 struct spi_message m;
175 u32 *busy_buf;
176 int num_busy_bytes = 0;
177
178 wl1271_info("spi read BUSY!");
179
180 /*
181 * Look for the non-busy word in the read buffer, and if found,
182 * read in the remaining data into the buffer.
183 */
184 busy_buf = (u32 *)buf;
185 for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
186 num_busy_bytes += sizeof(u32);
187 if (*busy_buf & 0x1) {
188 spi_message_init(&m);
189 memset(t, 0, sizeof(t));
190 memmove(buf, busy_buf, len - num_busy_bytes);
191 t[0].rx_buf = buf + (len - num_busy_bytes);
192 t[0].len = num_busy_bytes;
193 spi_message_add_tail(&t[0], &m);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200194 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300195 return;
196 }
197 }
198
199 /*
200 * Read further busy words from SPI until a non-busy word is
201 * encountered, then read the data itself into the buffer.
202 */
203 wl1271_info("spi read BUSY-polling needed!");
204
205 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
206 busy_buf = wl->buffer_busyword;
207 while (num_busy_bytes) {
208 num_busy_bytes--;
209 spi_message_init(&m);
210 memset(t, 0, sizeof(t));
211 t[0].rx_buf = busy_buf;
212 t[0].len = sizeof(u32);
213 spi_message_add_tail(&t[0], &m);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200214 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300215
216 if (*busy_buf & 0x1) {
217 spi_message_init(&m);
218 memset(t, 0, sizeof(t));
219 t[0].rx_buf = buf;
220 t[0].len = len;
221 spi_message_add_tail(&t[0], &m);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200222 spi_sync(wl_to_spi(wl), &m);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300223 return;
224 }
225 }
226
227 /* The SPI bus is unresponsive, the read failed. */
228 memset(buf, 0, len);
229 wl1271_error("SPI read busy-word timeout!\n");
230}
Luciano Coelho938e30c2009-10-15 10:33:27 +0300231#endif
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300232
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200233static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
Juuso Oikarinen74621412009-10-12 15:08:54 +0300234 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300235{
236 struct spi_transfer t[3];
237 struct spi_message m;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300238 u32 *busy_buf;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300239 u32 *cmd;
240
241 cmd = &wl->buffer_cmd;
242 busy_buf = wl->buffer_busyword;
243
244 *cmd = 0;
245 *cmd |= WSPI_CMD_READ;
246 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
247 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
248
249 if (fixed)
250 *cmd |= WSPI_CMD_FIXED;
251
252 spi_message_init(&m);
253 memset(t, 0, sizeof(t));
254
255 t[0].tx_buf = cmd;
256 t[0].len = 4;
257 spi_message_add_tail(&t[0], &m);
258
259 /* Busy and non busy words read */
260 t[1].rx_buf = busy_buf;
261 t[1].len = WL1271_BUSY_WORD_LEN;
262 spi_message_add_tail(&t[1], &m);
263
264 t[2].rx_buf = buf;
265 t[2].len = len;
266 spi_message_add_tail(&t[2], &m);
267
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200268 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300269
Juuso Oikarinenc6d5d062009-10-13 12:47:47 +0300270 /* FIXME: Check busy words, removed due to SPI bug */
271 /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
272 wl1271_spi_read_busy(wl, buf, len); */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300273
274 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
275 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
276}
277
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200278static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
Juuso Oikarinen74621412009-10-12 15:08:54 +0300279 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300280{
281 struct spi_transfer t[2];
282 struct spi_message m;
283 u32 *cmd;
284
285 cmd = &wl->buffer_cmd;
286
287 *cmd = 0;
288 *cmd |= WSPI_CMD_WRITE;
289 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
290 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
291
292 if (fixed)
293 *cmd |= WSPI_CMD_FIXED;
294
295 spi_message_init(&m);
296 memset(t, 0, sizeof(t));
297
298 t[0].tx_buf = cmd;
299 t[0].len = sizeof(*cmd);
300 spi_message_add_tail(&t[0], &m);
301
302 t[1].tx_buf = buf;
303 t[1].len = len;
304 spi_message_add_tail(&t[1], &m);
305
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200306 spi_sync(wl_to_spi(wl), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300307
308 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
309 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
310}
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200311
312static irqreturn_t wl1271_irq(int irq, void *cookie)
313{
314 struct wl1271 *wl;
315 unsigned long flags;
316
317 wl1271_debug(DEBUG_IRQ, "IRQ");
318
319 wl = cookie;
320
321 /* complete the ELP completion */
322 spin_lock_irqsave(&wl->wl_lock, flags);
323 if (wl->elp_compl) {
324 complete(wl->elp_compl);
325 wl->elp_compl = NULL;
326 }
327
Juuso Oikarinen1e73eb62010-02-22 08:38:37 +0200328 if (!test_and_set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
329 ieee80211_queue_work(wl->hw, &wl->irq_work);
330 set_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200331 spin_unlock_irqrestore(&wl->wl_lock, flags);
332
333 return IRQ_HANDLED;
334}
335
Teemu Paasikivibecd5512010-03-18 12:26:27 +0200336static void wl1271_spi_set_power(struct wl1271 *wl, bool enable)
337{
338 if (wl->set_power)
339 wl->set_power(enable);
340}
341
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200342static struct wl1271_if_operations spi_ops = {
343 .read = wl1271_spi_raw_read,
344 .write = wl1271_spi_raw_write,
345 .reset = wl1271_spi_reset,
346 .init = wl1271_spi_init,
Teemu Paasikivibecd5512010-03-18 12:26:27 +0200347 .power = wl1271_spi_set_power,
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200348 .dev = wl1271_spi_wl_to_dev,
349 .enable_irq = wl1271_spi_enable_interrupts,
350 .disable_irq = wl1271_spi_disable_interrupts
351};
352
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200353static int __devinit wl1271_probe(struct spi_device *spi)
354{
355 struct wl12xx_platform_data *pdata;
356 struct ieee80211_hw *hw;
357 struct wl1271 *wl;
358 int ret;
359
360 pdata = spi->dev.platform_data;
361 if (!pdata) {
362 wl1271_error("no platform data");
363 return -ENODEV;
364 }
365
366 hw = wl1271_alloc_hw();
367 if (IS_ERR(hw))
368 return PTR_ERR(hw);
369
370 wl = hw->priv;
371
372 dev_set_drvdata(&spi->dev, wl);
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200373 wl->if_priv = spi;
374
375 wl->if_ops = &spi_ops;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200376
377 /* This is the only SPI value that we need to set here, the rest
378 * comes from the board-peripherals file */
379 spi->bits_per_word = 32;
380
381 ret = spi_setup(spi);
382 if (ret < 0) {
383 wl1271_error("spi_setup failed");
384 goto out_free;
385 }
386
387 wl->set_power = pdata->set_power;
388 if (!wl->set_power) {
389 wl1271_error("set power function missing in platform data");
390 ret = -ENODEV;
391 goto out_free;
392 }
393
394 wl->irq = spi->irq;
395 if (wl->irq < 0) {
396 wl1271_error("irq missing in platform data");
397 ret = -ENODEV;
398 goto out_free;
399 }
400
401 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
402 if (ret < 0) {
403 wl1271_error("request_irq() failed: %d", ret);
404 goto out_free;
405 }
406
407 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
408
409 disable_irq(wl->irq);
410
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200411 ret = wl1271_init_ieee80211(wl);
412 if (ret)
Juuso Oikarinena1dd8182010-03-18 12:26:31 +0200413 goto out_irq;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200414
415 ret = wl1271_register_hw(wl);
416 if (ret)
Juuso Oikarinena1dd8182010-03-18 12:26:31 +0200417 goto out_irq;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200418
419 wl1271_notice("initialized");
420
421 return 0;
422
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200423 out_irq:
424 free_irq(wl->irq, wl);
425
426 out_free:
Teemu Paasikivi3b56dd62010-03-18 12:26:46 +0200427 wl1271_free_hw(wl);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200428
429 return ret;
430}
431
432static int __devexit wl1271_remove(struct spi_device *spi)
433{
434 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
435
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200436 free_irq(wl->irq, wl);
437
Teemu Paasikivi3b56dd62010-03-18 12:26:46 +0200438 wl1271_unregister_hw(wl);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200439 wl1271_free_hw(wl);
440
441 return 0;
442}
443
444
445static struct spi_driver wl1271_spi_driver = {
446 .driver = {
Luciano Coelho7fdd50d2010-03-26 12:53:10 +0200447 .name = "wl1271_spi",
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200448 .bus = &spi_bus_type,
449 .owner = THIS_MODULE,
450 },
451
452 .probe = wl1271_probe,
453 .remove = __devexit_p(wl1271_remove),
454};
455
456static int __init wl1271_init(void)
457{
458 int ret;
459
460 ret = spi_register_driver(&wl1271_spi_driver);
461 if (ret < 0) {
462 wl1271_error("failed to register spi driver: %d", ret);
463 goto out;
464 }
465
466out:
467 return ret;
468}
469
470static void __exit wl1271_exit(void)
471{
472 spi_unregister_driver(&wl1271_spi_driver);
473
474 wl1271_notice("unloaded");
475}
476
477module_init(wl1271_init);
478module_exit(wl1271_exit);
479
480MODULE_LICENSE("GPL");
481MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
482MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
483MODULE_FIRMWARE(WL1271_FW_NAME);