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