2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
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>
35 #include "wl12xx_80211.h"
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
47 #define WSPI_INIT_CMD_CRC_LEN 5
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
63 #define WSPI_INIT_CMD_LEN 8
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
69 /* HW limitation: maximum possible chunk size is 4095 bytes */
70 #define WSPI_MAX_CHUNK_SIZE 4092
72 #define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE)
74 struct wl12xx_spi_glue
{
76 struct platform_device
*core
;
79 static void wl12xx_spi_reset(struct device
*child
)
81 struct wl12xx_spi_glue
*glue
= dev_get_drvdata(child
->parent
);
83 struct spi_transfer t
;
86 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
88 wl1271_error("could not allocate cmd for spi reset");
92 memset(&t
, 0, sizeof(t
));
95 memset(cmd
, 0xff, WSPI_INIT_CMD_LEN
);
98 t
.len
= WSPI_INIT_CMD_LEN
;
99 spi_message_add_tail(&t
, &m
);
101 spi_sync(to_spi_device(glue
->dev
), &m
);
103 wl1271_dump(DEBUG_SPI
, "spi reset -> ", cmd
, WSPI_INIT_CMD_LEN
);
107 static void wl12xx_spi_init(struct device
*child
)
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
;
114 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
116 wl1271_error("could not allocate cmd for spi init");
120 memset(crc
, 0, sizeof(crc
));
121 memset(&t
, 0, sizeof(t
));
122 spi_message_init(&m
);
125 * Set WSPI_INIT_COMMAND
126 * the data is being send from the MSB to LSB
130 cmd
[1] = WSPI_INIT_CMD_START
| WSPI_INIT_CMD_TX
;
133 cmd
[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK
<< 3;
134 cmd
[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN
& WSPI_INIT_CMD_FIXEDBUSY_LEN
;
136 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN
== 0)
137 cmd
[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY
;
139 cmd
[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY
;
141 cmd
[5] |= WSPI_INIT_CMD_IOD
| WSPI_INIT_CMD_IP
| WSPI_INIT_CMD_CS
142 | WSPI_INIT_CMD_WSPI
| WSPI_INIT_CMD_WS
;
150 cmd
[4] |= crc7(0, crc
, WSPI_INIT_CMD_CRC_LEN
) << 1;
151 cmd
[4] |= WSPI_INIT_CMD_END
;
154 t
.len
= WSPI_INIT_CMD_LEN
;
155 spi_message_add_tail(&t
, &m
);
157 spi_sync(to_spi_device(glue
->dev
), &m
);
158 wl1271_dump(DEBUG_SPI
, "spi init -> ", cmd
, WSPI_INIT_CMD_LEN
);
162 #define WL1271_BUSY_WORD_TIMEOUT 1000
164 static int wl12xx_spi_read_busy(struct device
*child
)
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
;
171 int num_busy_bytes
= 0;
174 * Read further busy words from SPI until a non-busy word is
175 * encountered, then read the data itself into the buffer.
178 num_busy_bytes
= WL1271_BUSY_WORD_TIMEOUT
;
179 busy_buf
= wl
->buffer_busyword
;
180 while (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
);
194 /* The SPI bus is unresponsive, the read failed. */
195 wl1271_error("SPI read busy-word timeout!\n");
199 static void wl12xx_spi_raw_read(struct device
*child
, int addr
, void *buf
,
200 size_t len
, bool fixed
)
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
;
211 chunk_len
= min((size_t)WSPI_MAX_CHUNK_SIZE
, len
);
213 cmd
= &wl
->buffer_cmd
;
214 busy_buf
= wl
->buffer_busyword
;
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
;
223 *cmd
|= WSPI_CMD_FIXED
;
225 spi_message_init(&m
);
226 memset(t
, 0, sizeof(t
));
230 t
[0].cs_change
= true;
231 spi_message_add_tail(&t
[0], &m
);
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
);
239 spi_sync(to_spi_device(glue
->dev
), &m
);
241 if (!(busy_buf
[WL1271_BUSY_WORD_CNT
- 1] & 0x1) &&
242 wl12xx_spi_read_busy(child
)) {
243 memset(buf
, 0, chunk_len
);
247 spi_message_init(&m
);
248 memset(t
, 0, sizeof(t
));
251 t
[0].len
= chunk_len
;
252 t
[0].cs_change
= true;
253 spi_message_add_tail(&t
[0], &m
);
255 spi_sync(to_spi_device(glue
->dev
), &m
);
257 wl1271_dump(DEBUG_SPI
, "spi_read cmd -> ", cmd
, sizeof(*cmd
));
258 wl1271_dump(DEBUG_SPI
, "spi_read buf <- ", buf
, chunk_len
);
267 static void wl12xx_spi_raw_write(struct device
*child
, int addr
, void *buf
,
268 size_t len
, bool fixed
)
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
];
278 WARN_ON(len
> WL1271_AGGR_BUFFER_SIZE
);
280 spi_message_init(&m
);
281 memset(t
, 0, sizeof(t
));
286 chunk_len
= min((size_t)WSPI_MAX_CHUNK_SIZE
, len
);
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
;
295 *cmd
|= WSPI_CMD_FIXED
;
298 t
[i
].len
= sizeof(*cmd
);
299 spi_message_add_tail(&t
[i
++], &m
);
302 t
[i
].len
= chunk_len
;
303 spi_message_add_tail(&t
[i
++], &m
);
305 wl1271_dump(DEBUG_SPI
, "spi_write cmd -> ", cmd
, sizeof(*cmd
));
306 wl1271_dump(DEBUG_SPI
, "spi_write buf -> ", buf
, chunk_len
);
315 spi_sync(to_spi_device(glue
->dev
), &m
);
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
,
326 static int __devinit
wl1271_probe(struct spi_device
*spi
)
328 struct wl12xx_spi_glue
*glue
;
329 struct wl12xx_platform_data
*pdata
;
330 struct resource res
[1];
333 pdata
= spi
->dev
.platform_data
;
335 wl1271_error("no platform data");
339 pdata
->ops
= &spi_ops
;
341 glue
= kzalloc(sizeof(*glue
), GFP_KERNEL
);
343 wl1271_error("can't allocate glue");
347 glue
->dev
= &spi
->dev
;
349 spi_set_drvdata(spi
, glue
);
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;
355 ret
= spi_setup(spi
);
357 wl1271_error("spi_setup failed");
361 glue
->core
= platform_device_alloc("wl12xx-spi", -1);
363 wl1271_error("can't allocate platform_device");
368 glue
->core
->dev
.parent
= &spi
->dev
;
370 memset(res
, 0x00, sizeof(res
));
372 res
[0].start
= spi
->irq
;
373 res
[0].flags
= IORESOURCE_IRQ
;
376 ret
= platform_device_add_resources(glue
->core
, res
, ARRAY_SIZE(res
));
378 wl1271_error("can't add resources");
382 ret
= platform_device_add_data(glue
->core
, pdata
, sizeof(*pdata
));
384 wl1271_error("can't add platform data");
388 ret
= platform_device_add(glue
->core
);
390 wl1271_error("can't register platform device");
397 platform_device_put(glue
->core
);
405 static int __devexit
wl1271_remove(struct spi_device
*spi
)
407 struct wl12xx_spi_glue
*glue
= spi_get_drvdata(spi
);
409 platform_device_del(glue
->core
);
410 platform_device_put(glue
->core
);
417 static struct spi_driver wl1271_spi_driver
= {
419 .name
= "wl1271_spi",
420 .bus
= &spi_bus_type
,
421 .owner
= THIS_MODULE
,
424 .probe
= wl1271_probe
,
425 .remove
= __devexit_p(wl1271_remove
),
428 static int __init
wl1271_init(void)
430 return spi_register_driver(&wl1271_spi_driver
);
433 static void __exit
wl1271_exit(void)
435 spi_unregister_driver(&wl1271_spi_driver
);
438 module_init(wl1271_init
);
439 module_exit(wl1271_exit
);
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");