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