1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 "wlcore.h"
34#include "wl12xx_80211.h"
35#include "io.h"
36
37#define WSPI_CMD_READ 0x40000000
38#define WSPI_CMD_WRITE 0x00000000
39#define WSPI_CMD_FIXED 0x20000000
40#define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
41#define WSPI_CMD_BYTE_LENGTH_OFFSET 17
42#define WSPI_CMD_BYTE_ADDR 0x0001FFFF
43
44#define WSPI_INIT_CMD_CRC_LEN 5
45
46#define WSPI_INIT_CMD_START 0x00
47#define WSPI_INIT_CMD_TX 0x40
48
49#define WSPI_INIT_CMD_BYPASS_BIT 0x80
50#define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
51#define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
52#define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
53#define WSPI_INIT_CMD_IOD 0x40
54#define WSPI_INIT_CMD_IP 0x20
55#define WSPI_INIT_CMD_CS 0x10
56#define WSPI_INIT_CMD_WS 0x08
57#define WSPI_INIT_CMD_WSPI 0x01
58#define WSPI_INIT_CMD_END 0x01
59
60#define WSPI_INIT_CMD_LEN 8
61
62#define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
63 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
64#define HW_ACCESS_WSPI_INIT_CMD_MASK 0
65
66
67#define WSPI_MAX_CHUNK_SIZE 4092
68
69
70
71
72
73#define SPI_AGGR_BUFFER_SIZE (4 * PAGE_SIZE)
74
75#define WSPI_MAX_NUM_OF_CHUNKS (SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE)
76
77struct wl12xx_spi_glue {
78 struct device *dev;
79 struct platform_device *core;
80};
81
82static void wl12xx_spi_reset(struct device *child)
83{
84 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
85 u8 *cmd;
86 struct spi_transfer t;
87 struct spi_message m;
88
89 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
90 if (!cmd) {
91 dev_err(child->parent,
92 "could not allocate cmd for spi reset\n");
93 return;
94 }
95
96 memset(&t, 0, sizeof(t));
97 spi_message_init(&m);
98
99 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
100
101 t.tx_buf = cmd;
102 t.len = WSPI_INIT_CMD_LEN;
103 spi_message_add_tail(&t, &m);
104
105 spi_sync(to_spi_device(glue->dev), &m);
106
107 kfree(cmd);
108}
109
110static void wl12xx_spi_init(struct device *child)
111{
112 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
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 dev_err(child->parent,
120 "could not allocate cmd for spi init\n");
121 return;
122 }
123
124 memset(crc, 0, sizeof(crc));
125 memset(&t, 0, sizeof(t));
126 spi_message_init(&m);
127
128
129
130
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
161 spi_sync(to_spi_device(glue->dev), &m);
162 kfree(cmd);
163}
164
165#define WL1271_BUSY_WORD_TIMEOUT 1000
166
167static int wl12xx_spi_read_busy(struct device *child)
168{
169 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
170 struct wl1271 *wl = dev_get_drvdata(child);
171 struct spi_transfer t[1];
172 struct spi_message m;
173 u32 *busy_buf;
174 int num_busy_bytes = 0;
175
176
177
178
179
180
181 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
182 busy_buf = wl->buffer_busyword;
183 while (num_busy_bytes) {
184 num_busy_bytes--;
185 spi_message_init(&m);
186 memset(t, 0, sizeof(t));
187 t[0].rx_buf = busy_buf;
188 t[0].len = sizeof(u32);
189 t[0].cs_change = true;
190 spi_message_add_tail(&t[0], &m);
191 spi_sync(to_spi_device(glue->dev), &m);
192
193 if (*busy_buf & 0x1)
194 return 0;
195 }
196
197
198 dev_err(child->parent, "SPI read busy-word timeout!\n");
199 return -ETIMEDOUT;
200}
201
202static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
203 void *buf, size_t len, bool fixed)
204{
205 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
206 struct wl1271 *wl = dev_get_drvdata(child);
207 struct spi_transfer t[2];
208 struct spi_message m;
209 u32 *busy_buf;
210 u32 *cmd;
211 u32 chunk_len;
212
213 while (len > 0) {
214 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
215
216 cmd = &wl->buffer_cmd;
217 busy_buf = wl->buffer_busyword;
218
219 *cmd = 0;
220 *cmd |= WSPI_CMD_READ;
221 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
222 WSPI_CMD_BYTE_LENGTH;
223 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
224
225 if (fixed)
226 *cmd |= WSPI_CMD_FIXED;
227
228 spi_message_init(&m);
229 memset(t, 0, sizeof(t));
230
231 t[0].tx_buf = cmd;
232 t[0].len = 4;
233 t[0].cs_change = true;
234 spi_message_add_tail(&t[0], &m);
235
236
237 t[1].rx_buf = busy_buf;
238 t[1].len = WL1271_BUSY_WORD_LEN;
239 t[1].cs_change = true;
240 spi_message_add_tail(&t[1], &m);
241
242 spi_sync(to_spi_device(glue->dev), &m);
243
244 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
245 wl12xx_spi_read_busy(child)) {
246 memset(buf, 0, chunk_len);
247 return 0;
248 }
249
250 spi_message_init(&m);
251 memset(t, 0, sizeof(t));
252
253 t[0].rx_buf = buf;
254 t[0].len = chunk_len;
255 t[0].cs_change = true;
256 spi_message_add_tail(&t[0], &m);
257
258 spi_sync(to_spi_device(glue->dev), &m);
259
260 if (!fixed)
261 addr += chunk_len;
262 buf += chunk_len;
263 len -= chunk_len;
264 }
265
266 return 0;
267}
268
269static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
270 void *buf, size_t len, bool fixed)
271{
272 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
273 struct spi_transfer t[2 * (WSPI_MAX_NUM_OF_CHUNKS + 1)];
274 struct spi_message m;
275 u32 commands[WSPI_MAX_NUM_OF_CHUNKS];
276 u32 *cmd;
277 u32 chunk_len;
278 int i;
279
280 WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
281
282 spi_message_init(&m);
283 memset(t, 0, sizeof(t));
284
285 cmd = &commands[0];
286 i = 0;
287 while (len > 0) {
288 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
289
290 *cmd = 0;
291 *cmd |= WSPI_CMD_WRITE;
292 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
293 WSPI_CMD_BYTE_LENGTH;
294 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
295
296 if (fixed)
297 *cmd |= WSPI_CMD_FIXED;
298
299 t[i].tx_buf = cmd;
300 t[i].len = sizeof(*cmd);
301 spi_message_add_tail(&t[i++], &m);
302
303 t[i].tx_buf = buf;
304 t[i].len = chunk_len;
305 spi_message_add_tail(&t[i++], &m);
306
307 if (!fixed)
308 addr += chunk_len;
309 buf += chunk_len;
310 len -= chunk_len;
311 cmd++;
312 }
313
314 spi_sync(to_spi_device(glue->dev), &m);
315
316 return 0;
317}
318
319static struct wl1271_if_operations spi_ops = {
320 .read = wl12xx_spi_raw_read,
321 .write = wl12xx_spi_raw_write,
322 .reset = wl12xx_spi_reset,
323 .init = wl12xx_spi_init,
324 .set_block_size = NULL,
325};
326
327static int wl1271_probe(struct spi_device *spi)
328{
329 struct wl12xx_spi_glue *glue;
330 struct wlcore_platdev_data *pdev_data;
331 struct resource res[1];
332 int ret = -ENOMEM;
333
334 pdev_data = kzalloc(sizeof(*pdev_data), GFP_KERNEL);
335 if (!pdev_data)
336 goto out;
337
338 pdev_data->pdata = spi->dev.platform_data;
339 if (!pdev_data->pdata) {
340 dev_err(&spi->dev, "no platform data\n");
341 ret = -ENODEV;
342 goto out_free_pdev_data;
343 }
344
345 pdev_data->if_ops = &spi_ops;
346
347 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
348 if (!glue) {
349 dev_err(&spi->dev, "can't allocate glue\n");
350 goto out_free_pdev_data;
351 }
352
353 glue->dev = &spi->dev;
354
355 spi_set_drvdata(spi, glue);
356
357
358
359 spi->bits_per_word = 32;
360
361 ret = spi_setup(spi);
362 if (ret < 0) {
363 dev_err(glue->dev, "spi_setup failed\n");
364 goto out_free_glue;
365 }
366
367 glue->core = platform_device_alloc("wl12xx", PLATFORM_DEVID_AUTO);
368 if (!glue->core) {
369 dev_err(glue->dev, "can't allocate platform_device\n");
370 ret = -ENOMEM;
371 goto out_free_glue;
372 }
373
374 glue->core->dev.parent = &spi->dev;
375
376 memset(res, 0x00, sizeof(res));
377
378 res[0].start = spi->irq;
379 res[0].flags = IORESOURCE_IRQ;
380 res[0].name = "irq";
381
382 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
383 if (ret) {
384 dev_err(glue->dev, "can't add resources\n");
385 goto out_dev_put;
386 }
387
388 ret = platform_device_add_data(glue->core, pdev_data,
389 sizeof(*pdev_data));
390 if (ret) {
391 dev_err(glue->dev, "can't add platform data\n");
392 goto out_dev_put;
393 }
394
395 ret = platform_device_add(glue->core);
396 if (ret) {
397 dev_err(glue->dev, "can't register platform device\n");
398 goto out_dev_put;
399 }
400
401 return 0;
402
403out_dev_put:
404 platform_device_put(glue->core);
405
406out_free_glue:
407 kfree(glue);
408
409out_free_pdev_data:
410 kfree(pdev_data);
411
412out:
413 return ret;
414}
415
416static int wl1271_remove(struct spi_device *spi)
417{
418 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
419
420 platform_device_unregister(glue->core);
421 kfree(glue);
422
423 return 0;
424}
425
426
427static struct spi_driver wl1271_spi_driver = {
428 .driver = {
429 .name = "wl1271_spi",
430 .owner = THIS_MODULE,
431 },
432
433 .probe = wl1271_probe,
434 .remove = wl1271_remove,
435};
436
437static int __init wl1271_init(void)
438{
439 return spi_register_driver(&wl1271_spi_driver);
440}
441
442static void __exit wl1271_exit(void)
443{
444 spi_unregister_driver(&wl1271_spi_driver);
445}
446
447module_init(wl1271_init);
448module_exit(wl1271_exit);
449
450MODULE_LICENSE("GPL");
451MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
452MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
453MODULE_ALIAS("spi:wl1271");
454