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 | ||
a6b7a407 | 24 | #include <linux/interrupt.h> |
2d5e82b8 | 25 | #include <linux/irq.h> |
f5fc0f86 | 26 | #include <linux/module.h> |
f5fc0f86 LC |
27 | #include <linux/crc7.h> |
28 | #include <linux/spi/spi.h> | |
c1f9a095 | 29 | #include <linux/wl12xx.h> |
0969d679 | 30 | #include <linux/platform_device.h> |
5a0e3ad6 | 31 | #include <linux/slab.h> |
f5fc0f86 | 32 | |
00d20100 | 33 | #include "wl12xx.h" |
0f4e3122 | 34 | #include "debug.h" |
f5fc0f86 | 35 | #include "wl12xx_80211.h" |
00d20100 | 36 | #include "io.h" |
f5fc0f86 | 37 | |
00d20100 | 38 | #include "reg.h" |
760d969f TP |
39 | |
40 | #define WSPI_CMD_READ 0x40000000 | |
41 | #define WSPI_CMD_WRITE 0x00000000 | |
42 | #define WSPI_CMD_FIXED 0x20000000 | |
43 | #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000 | |
44 | #define WSPI_CMD_BYTE_LENGTH_OFFSET 17 | |
45 | #define WSPI_CMD_BYTE_ADDR 0x0001FFFF | |
46 | ||
47 | #define WSPI_INIT_CMD_CRC_LEN 5 | |
48 | ||
49 | #define WSPI_INIT_CMD_START 0x00 | |
50 | #define WSPI_INIT_CMD_TX 0x40 | |
51 | /* the extra bypass bit is sampled by the TNET as '1' */ | |
52 | #define WSPI_INIT_CMD_BYPASS_BIT 0x80 | |
53 | #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07 | |
54 | #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80 | |
55 | #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00 | |
56 | #define WSPI_INIT_CMD_IOD 0x40 | |
57 | #define WSPI_INIT_CMD_IP 0x20 | |
58 | #define WSPI_INIT_CMD_CS 0x10 | |
59 | #define WSPI_INIT_CMD_WS 0x08 | |
60 | #define WSPI_INIT_CMD_WSPI 0x01 | |
61 | #define WSPI_INIT_CMD_END 0x01 | |
62 | ||
63 | #define WSPI_INIT_CMD_LEN 8 | |
64 | ||
65 | #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \ | |
66 | ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32)) | |
67 | #define HW_ACCESS_WSPI_INIT_CMD_MASK 0 | |
68 | ||
5c57a901 IY |
69 | /* HW limitation: maximum possible chunk size is 4095 bytes */ |
70 | #define WSPI_MAX_CHUNK_SIZE 4092 | |
71 | ||
72 | #define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) | |
73 | ||
b65019f6 FB |
74 | struct wl12xx_spi_glue { |
75 | struct device *dev; | |
0969d679 | 76 | struct platform_device *core; |
b65019f6 FB |
77 | }; |
78 | ||
a390e85c | 79 | static void wl12xx_spi_reset(struct device *child) |
8197b711 | 80 | { |
a390e85c | 81 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
f5fc0f86 LC |
82 | u8 *cmd; |
83 | struct spi_transfer t; | |
84 | struct spi_message m; | |
85 | ||
86 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); | |
87 | if (!cmd) { | |
88 | wl1271_error("could not allocate cmd for spi reset"); | |
89 | return; | |
90 | } | |
91 | ||
92 | memset(&t, 0, sizeof(t)); | |
93 | spi_message_init(&m); | |
94 | ||
95 | memset(cmd, 0xff, WSPI_INIT_CMD_LEN); | |
96 | ||
97 | t.tx_buf = cmd; | |
98 | t.len = WSPI_INIT_CMD_LEN; | |
99 | spi_message_add_tail(&t, &m); | |
100 | ||
b65019f6 | 101 | spi_sync(to_spi_device(glue->dev), &m); |
f5fc0f86 LC |
102 | |
103 | wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); | |
0dd38667 | 104 | kfree(cmd); |
f5fc0f86 LC |
105 | } |
106 | ||
a390e85c | 107 | static void wl12xx_spi_init(struct device *child) |
f5fc0f86 | 108 | { |
a390e85c | 109 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
f5fc0f86 LC |
110 | u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; |
111 | struct spi_transfer t; | |
112 | struct spi_message m; | |
113 | ||
114 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); | |
115 | if (!cmd) { | |
116 | wl1271_error("could not allocate cmd for spi init"); | |
117 | return; | |
118 | } | |
119 | ||
120 | memset(crc, 0, sizeof(crc)); | |
121 | memset(&t, 0, sizeof(t)); | |
122 | spi_message_init(&m); | |
123 | ||
124 | /* | |
125 | * Set WSPI_INIT_COMMAND | |
126 | * the data is being send from the MSB to LSB | |
127 | */ | |
128 | cmd[2] = 0xff; | |
129 | cmd[3] = 0xff; | |
130 | cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; | |
131 | cmd[0] = 0; | |
132 | cmd[7] = 0; | |
133 | cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; | |
134 | cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; | |
135 | ||
136 | if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) | |
137 | cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; | |
138 | else | |
139 | cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; | |
140 | ||
141 | cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS | |
142 | | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; | |
143 | ||
144 | crc[0] = cmd[1]; | |
145 | crc[1] = cmd[0]; | |
146 | crc[2] = cmd[7]; | |
147 | crc[3] = cmd[6]; | |
148 | crc[4] = cmd[5]; | |
149 | ||
150 | cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; | |
151 | cmd[4] |= WSPI_INIT_CMD_END; | |
152 | ||
153 | t.tx_buf = cmd; | |
154 | t.len = WSPI_INIT_CMD_LEN; | |
155 | spi_message_add_tail(&t, &m); | |
156 | ||
b65019f6 | 157 | spi_sync(to_spi_device(glue->dev), &m); |
f5fc0f86 | 158 | wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
bb123611 | 159 | kfree(cmd); |
f5fc0f86 LC |
160 | } |
161 | ||
545f1da8 JO |
162 | #define WL1271_BUSY_WORD_TIMEOUT 1000 |
163 | ||
a390e85c | 164 | static int wl12xx_spi_read_busy(struct device *child) |
545f1da8 | 165 | { |
a390e85c FB |
166 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
167 | struct wl1271 *wl = dev_get_drvdata(child); | |
545f1da8 JO |
168 | struct spi_transfer t[1]; |
169 | struct spi_message m; | |
170 | u32 *busy_buf; | |
171 | int num_busy_bytes = 0; | |
172 | ||
545f1da8 JO |
173 | /* |
174 | * Read further busy words from SPI until a non-busy word is | |
175 | * encountered, then read the data itself into the buffer. | |
176 | */ | |
545f1da8 JO |
177 | |
178 | num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT; | |
179 | busy_buf = wl->buffer_busyword; | |
180 | while (num_busy_bytes) { | |
181 | num_busy_bytes--; | |
182 | spi_message_init(&m); | |
183 | memset(t, 0, sizeof(t)); | |
184 | t[0].rx_buf = busy_buf; | |
185 | t[0].len = sizeof(u32); | |
259da430 | 186 | t[0].cs_change = true; |
545f1da8 | 187 | spi_message_add_tail(&t[0], &m); |
b65019f6 | 188 | spi_sync(to_spi_device(glue->dev), &m); |
545f1da8 | 189 | |
259da430 JO |
190 | if (*busy_buf & 0x1) |
191 | return 0; | |
545f1da8 JO |
192 | } |
193 | ||
194 | /* The SPI bus is unresponsive, the read failed. */ | |
545f1da8 | 195 | wl1271_error("SPI read busy-word timeout!\n"); |
259da430 | 196 | return -ETIMEDOUT; |
545f1da8 JO |
197 | } |
198 | ||
a390e85c | 199 | static void wl12xx_spi_raw_read(struct device *child, int addr, void *buf, |
259da430 | 200 | size_t len, bool fixed) |
f5fc0f86 | 201 | { |
a390e85c FB |
202 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
203 | struct wl1271 *wl = dev_get_drvdata(child); | |
5c57a901 | 204 | struct spi_transfer t[2]; |
f5fc0f86 | 205 | struct spi_message m; |
545f1da8 | 206 | u32 *busy_buf; |
f5fc0f86 | 207 | u32 *cmd; |
5c57a901 | 208 | u32 chunk_len; |
f5fc0f86 | 209 | |
5c57a901 IY |
210 | while (len > 0) { |
211 | chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len); | |
f5fc0f86 | 212 | |
5c57a901 IY |
213 | cmd = &wl->buffer_cmd; |
214 | busy_buf = wl->buffer_busyword; | |
f5fc0f86 | 215 | |
5c57a901 IY |
216 | *cmd = 0; |
217 | *cmd |= WSPI_CMD_READ; | |
218 | *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) & | |
219 | WSPI_CMD_BYTE_LENGTH; | |
220 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; | |
f5fc0f86 | 221 | |
5c57a901 IY |
222 | if (fixed) |
223 | *cmd |= WSPI_CMD_FIXED; | |
f5fc0f86 | 224 | |
5c57a901 IY |
225 | spi_message_init(&m); |
226 | memset(t, 0, sizeof(t)); | |
f5fc0f86 | 227 | |
5c57a901 IY |
228 | t[0].tx_buf = cmd; |
229 | t[0].len = 4; | |
230 | t[0].cs_change = true; | |
231 | spi_message_add_tail(&t[0], &m); | |
f5fc0f86 | 232 | |
5c57a901 IY |
233 | /* Busy and non busy words read */ |
234 | t[1].rx_buf = busy_buf; | |
235 | t[1].len = WL1271_BUSY_WORD_LEN; | |
236 | t[1].cs_change = true; | |
237 | spi_message_add_tail(&t[1], &m); | |
f5fc0f86 | 238 | |
b65019f6 | 239 | spi_sync(to_spi_device(glue->dev), &m); |
259da430 | 240 | |
5c57a901 | 241 | if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) && |
a390e85c | 242 | wl12xx_spi_read_busy(child)) { |
5c57a901 IY |
243 | memset(buf, 0, chunk_len); |
244 | return; | |
245 | } | |
259da430 | 246 | |
5c57a901 IY |
247 | spi_message_init(&m); |
248 | memset(t, 0, sizeof(t)); | |
259da430 | 249 | |
5c57a901 IY |
250 | t[0].rx_buf = buf; |
251 | t[0].len = chunk_len; | |
252 | t[0].cs_change = true; | |
253 | spi_message_add_tail(&t[0], &m); | |
254 | ||
b65019f6 | 255 | spi_sync(to_spi_device(glue->dev), &m); |
f5fc0f86 | 256 | |
5c57a901 IY |
257 | wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
258 | wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, chunk_len); | |
259 | ||
260 | if (!fixed) | |
261 | addr += chunk_len; | |
262 | buf += chunk_len; | |
263 | len -= chunk_len; | |
264 | } | |
f5fc0f86 LC |
265 | } |
266 | ||
a390e85c FB |
267 | static void wl12xx_spi_raw_write(struct device *child, int addr, void *buf, |
268 | size_t len, bool fixed) | |
f5fc0f86 | 269 | { |
a390e85c | 270 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
5c57a901 | 271 | struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS]; |
f5fc0f86 | 272 | struct spi_message m; |
5c57a901 | 273 | u32 commands[WSPI_MAX_NUM_OF_CHUNKS]; |
f5fc0f86 | 274 | u32 *cmd; |
5c57a901 IY |
275 | u32 chunk_len; |
276 | int i; | |
f5fc0f86 | 277 | |
5c57a901 | 278 | WARN_ON(len > WL1271_AGGR_BUFFER_SIZE); |
f5fc0f86 LC |
279 | |
280 | spi_message_init(&m); | |
281 | memset(t, 0, sizeof(t)); | |
282 | ||
5c57a901 IY |
283 | cmd = &commands[0]; |
284 | i = 0; | |
285 | while (len > 0) { | |
286 | chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len); | |
f5fc0f86 | 287 | |
5c57a901 IY |
288 | *cmd = 0; |
289 | *cmd |= WSPI_CMD_WRITE; | |
290 | *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) & | |
291 | WSPI_CMD_BYTE_LENGTH; | |
292 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; | |
f5fc0f86 | 293 | |
5c57a901 IY |
294 | if (fixed) |
295 | *cmd |= WSPI_CMD_FIXED; | |
296 | ||
297 | t[i].tx_buf = cmd; | |
298 | t[i].len = sizeof(*cmd); | |
299 | spi_message_add_tail(&t[i++], &m); | |
300 | ||
301 | t[i].tx_buf = buf; | |
302 | t[i].len = chunk_len; | |
303 | spi_message_add_tail(&t[i++], &m); | |
f5fc0f86 | 304 | |
5c57a901 IY |
305 | wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
306 | wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, chunk_len); | |
307 | ||
308 | if (!fixed) | |
309 | addr += chunk_len; | |
310 | buf += chunk_len; | |
311 | len -= chunk_len; | |
312 | cmd++; | |
313 | } | |
314 | ||
b65019f6 | 315 | spi_sync(to_spi_device(glue->dev), &m); |
f5fc0f86 | 316 | } |
2d5e82b8 | 317 | |
8197b711 | 318 | static struct wl1271_if_operations spi_ops = { |
a390e85c FB |
319 | .read = wl12xx_spi_raw_read, |
320 | .write = wl12xx_spi_raw_write, | |
321 | .reset = wl12xx_spi_reset, | |
322 | .init = wl12xx_spi_init, | |
a81159ed | 323 | .set_block_size = NULL, |
8197b711 TP |
324 | }; |
325 | ||
2d5e82b8 TP |
326 | static int __devinit wl1271_probe(struct spi_device *spi) |
327 | { | |
b65019f6 | 328 | struct wl12xx_spi_glue *glue; |
2d5e82b8 | 329 | struct wl12xx_platform_data *pdata; |
0969d679 | 330 | struct resource res[1]; |
b65019f6 | 331 | int ret = -ENOMEM; |
2d5e82b8 TP |
332 | |
333 | pdata = spi->dev.platform_data; | |
334 | if (!pdata) { | |
335 | wl1271_error("no platform data"); | |
336 | return -ENODEV; | |
337 | } | |
338 | ||
a390e85c FB |
339 | pdata->ops = &spi_ops; |
340 | ||
b65019f6 FB |
341 | glue = kzalloc(sizeof(*glue), GFP_KERNEL); |
342 | if (!glue) { | |
343 | wl1271_error("can't allocate glue"); | |
344 | goto out; | |
345 | } | |
346 | ||
b65019f6 | 347 | glue->dev = &spi->dev; |
b65019f6 FB |
348 | |
349 | spi_set_drvdata(spi, glue); | |
2d5e82b8 TP |
350 | |
351 | /* This is the only SPI value that we need to set here, the rest | |
352 | * comes from the board-peripherals file */ | |
353 | spi->bits_per_word = 32; | |
354 | ||
355 | ret = spi_setup(spi); | |
356 | if (ret < 0) { | |
357 | wl1271_error("spi_setup failed"); | |
a390e85c | 358 | goto out_free_glue; |
2d5e82b8 TP |
359 | } |
360 | ||
0969d679 FB |
361 | glue->core = platform_device_alloc("wl12xx-spi", -1); |
362 | if (!glue->core) { | |
363 | wl1271_error("can't allocate platform_device"); | |
364 | ret = -ENOMEM; | |
a390e85c | 365 | goto out_free_glue; |
0969d679 FB |
366 | } |
367 | ||
368 | glue->core->dev.parent = &spi->dev; | |
369 | ||
370 | memset(res, 0x00, sizeof(res)); | |
371 | ||
372 | res[0].start = spi->irq; | |
373 | res[0].flags = IORESOURCE_IRQ; | |
374 | res[0].name = "irq"; | |
375 | ||
376 | ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res)); | |
377 | if (ret) { | |
378 | wl1271_error("can't add resources"); | |
379 | goto out_dev_put; | |
380 | } | |
381 | ||
382 | ret = platform_device_add_data(glue->core, pdata, sizeof(*pdata)); | |
383 | if (ret) { | |
384 | wl1271_error("can't add platform data"); | |
385 | goto out_dev_put; | |
386 | } | |
387 | ||
388 | ret = platform_device_add(glue->core); | |
389 | if (ret) { | |
390 | wl1271_error("can't register platform device"); | |
391 | goto out_dev_put; | |
392 | } | |
393 | ||
2d5e82b8 TP |
394 | return 0; |
395 | ||
0969d679 FB |
396 | out_dev_put: |
397 | platform_device_put(glue->core); | |
398 | ||
b65019f6 FB |
399 | out_free_glue: |
400 | kfree(glue); | |
401 | out: | |
2d5e82b8 TP |
402 | return ret; |
403 | } | |
404 | ||
405 | static int __devexit wl1271_remove(struct spi_device *spi) | |
406 | { | |
b65019f6 | 407 | struct wl12xx_spi_glue *glue = spi_get_drvdata(spi); |
2d5e82b8 | 408 | |
0969d679 FB |
409 | platform_device_del(glue->core); |
410 | platform_device_put(glue->core); | |
b65019f6 | 411 | kfree(glue); |
2d5e82b8 TP |
412 | |
413 | return 0; | |
414 | } | |
415 | ||
416 | ||
417 | static struct spi_driver wl1271_spi_driver = { | |
418 | .driver = { | |
7fdd50d0 | 419 | .name = "wl1271_spi", |
2d5e82b8 TP |
420 | .bus = &spi_bus_type, |
421 | .owner = THIS_MODULE, | |
422 | }, | |
423 | ||
424 | .probe = wl1271_probe, | |
425 | .remove = __devexit_p(wl1271_remove), | |
426 | }; | |
427 | ||
428 | static int __init wl1271_init(void) | |
429 | { | |
ba2274c6 | 430 | return spi_register_driver(&wl1271_spi_driver); |
2d5e82b8 TP |
431 | } |
432 | ||
433 | static void __exit wl1271_exit(void) | |
434 | { | |
435 | spi_unregister_driver(&wl1271_spi_driver); | |
2d5e82b8 TP |
436 | } |
437 | ||
438 | module_init(wl1271_init); | |
439 | module_exit(wl1271_exit); | |
440 | ||
441 | MODULE_LICENSE("GPL"); | |
5245e3a9 | 442 | MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>"); |
2d5e82b8 | 443 | MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>"); |
c302b2c9 | 444 | MODULE_FIRMWARE(WL127X_FW_NAME); |
5aa42346 | 445 | MODULE_FIRMWARE(WL128X_FW_NAME); |
f148cfdd | 446 | MODULE_ALIAS("spi:wl1271"); |