]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/wireless/wl12xx/spi.c
wl12xx: move debugging definitions to a separate file
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / wl12xx / spi.c
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
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/module.h>
27 #include <linux/crc7.h>
28 #include <linux/spi/spi.h>
29 #include <linux/wl12xx.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32
33 #include "wl12xx.h"
34 #include "debug.h"
35 #include "wl12xx_80211.h"
36 #include "io.h"
37
38 #include "reg.h"
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
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
74 struct wl12xx_spi_glue {
75 struct device *dev;
76 struct platform_device *core;
77 };
78
79 static void wl12xx_spi_reset(struct device *child)
80 {
81 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
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
101 spi_sync(to_spi_device(glue->dev), &m);
102
103 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
104 kfree(cmd);
105 }
106
107 static void wl12xx_spi_init(struct device *child)
108 {
109 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
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
157 spi_sync(to_spi_device(glue->dev), &m);
158 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
159 kfree(cmd);
160 }
161
162 #define WL1271_BUSY_WORD_TIMEOUT 1000
163
164 static int wl12xx_spi_read_busy(struct device *child)
165 {
166 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
167 struct wl1271 *wl = dev_get_drvdata(child);
168 struct spi_transfer t[1];
169 struct spi_message m;
170 u32 *busy_buf;
171 int num_busy_bytes = 0;
172
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 */
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);
186 t[0].cs_change = true;
187 spi_message_add_tail(&t[0], &m);
188 spi_sync(to_spi_device(glue->dev), &m);
189
190 if (*busy_buf & 0x1)
191 return 0;
192 }
193
194 /* The SPI bus is unresponsive, the read failed. */
195 wl1271_error("SPI read busy-word timeout!\n");
196 return -ETIMEDOUT;
197 }
198
199 static void wl12xx_spi_raw_read(struct device *child, int addr, void *buf,
200 size_t len, bool fixed)
201 {
202 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
203 struct wl1271 *wl = dev_get_drvdata(child);
204 struct spi_transfer t[2];
205 struct spi_message m;
206 u32 *busy_buf;
207 u32 *cmd;
208 u32 chunk_len;
209
210 while (len > 0) {
211 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
212
213 cmd = &wl->buffer_cmd;
214 busy_buf = wl->buffer_busyword;
215
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;
221
222 if (fixed)
223 *cmd |= WSPI_CMD_FIXED;
224
225 spi_message_init(&m);
226 memset(t, 0, sizeof(t));
227
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);
232
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);
238
239 spi_sync(to_spi_device(glue->dev), &m);
240
241 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
242 wl12xx_spi_read_busy(child)) {
243 memset(buf, 0, chunk_len);
244 return;
245 }
246
247 spi_message_init(&m);
248 memset(t, 0, sizeof(t));
249
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
255 spi_sync(to_spi_device(glue->dev), &m);
256
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 }
265 }
266
267 static void wl12xx_spi_raw_write(struct device *child, int addr, void *buf,
268 size_t len, bool fixed)
269 {
270 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
271 struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS];
272 struct spi_message m;
273 u32 commands[WSPI_MAX_NUM_OF_CHUNKS];
274 u32 *cmd;
275 u32 chunk_len;
276 int i;
277
278 WARN_ON(len > WL1271_AGGR_BUFFER_SIZE);
279
280 spi_message_init(&m);
281 memset(t, 0, sizeof(t));
282
283 cmd = &commands[0];
284 i = 0;
285 while (len > 0) {
286 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
287
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;
293
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);
304
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
315 spi_sync(to_spi_device(glue->dev), &m);
316 }
317
318 static struct wl1271_if_operations spi_ops = {
319 .read = wl12xx_spi_raw_read,
320 .write = wl12xx_spi_raw_write,
321 .reset = wl12xx_spi_reset,
322 .init = wl12xx_spi_init,
323 .set_block_size = NULL,
324 };
325
326 static int __devinit wl1271_probe(struct spi_device *spi)
327 {
328 struct wl12xx_spi_glue *glue;
329 struct wl12xx_platform_data *pdata;
330 struct resource res[1];
331 int ret = -ENOMEM;
332
333 pdata = spi->dev.platform_data;
334 if (!pdata) {
335 wl1271_error("no platform data");
336 return -ENODEV;
337 }
338
339 pdata->ops = &spi_ops;
340
341 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
342 if (!glue) {
343 wl1271_error("can't allocate glue");
344 goto out;
345 }
346
347 glue->dev = &spi->dev;
348
349 spi_set_drvdata(spi, glue);
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");
358 goto out_free_glue;
359 }
360
361 glue->core = platform_device_alloc("wl12xx-spi", -1);
362 if (!glue->core) {
363 wl1271_error("can't allocate platform_device");
364 ret = -ENOMEM;
365 goto out_free_glue;
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
394 return 0;
395
396 out_dev_put:
397 platform_device_put(glue->core);
398
399 out_free_glue:
400 kfree(glue);
401 out:
402 return ret;
403 }
404
405 static int __devexit wl1271_remove(struct spi_device *spi)
406 {
407 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
408
409 platform_device_del(glue->core);
410 platform_device_put(glue->core);
411 kfree(glue);
412
413 return 0;
414 }
415
416
417 static struct spi_driver wl1271_spi_driver = {
418 .driver = {
419 .name = "wl1271_spi",
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 {
430 return spi_register_driver(&wl1271_spi_driver);
431 }
432
433 static void __exit wl1271_exit(void)
434 {
435 spi_unregister_driver(&wl1271_spi_driver);
436 }
437
438 module_init(wl1271_init);
439 module_exit(wl1271_exit);
440
441 MODULE_LICENSE("GPL");
442 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
443 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
444 MODULE_FIRMWARE(WL127X_FW_NAME);
445 MODULE_FIRMWARE(WL128X_FW_NAME);
446 MODULE_ALIAS("spi:wl1271");