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/irq.h>
25 #include <linux/module.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
28 #include <linux/wl12xx.h>
29 #include <linux/slab.h>
32 #include "wl12xx_80211.h"
33 #include "wl1271_io.h"
35 #include "wl1271_reg.h"
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
44 #define WSPI_INIT_CMD_CRC_LEN 5
46 #define WSPI_INIT_CMD_START 0x00
47 #define WSPI_INIT_CMD_TX 0x40
48 /* the extra bypass bit is sampled by the TNET as '1' */
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
60 #define WSPI_INIT_CMD_LEN 8
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
66 static inline struct spi_device
*wl_to_spi(struct wl1271
*wl
)
71 static struct device
*wl1271_spi_wl_to_dev(struct wl1271
*wl
)
73 return &(wl_to_spi(wl
)->dev
);
76 static void wl1271_spi_disable_interrupts(struct wl1271
*wl
)
81 static void wl1271_spi_enable_interrupts(struct wl1271
*wl
)
86 static void wl1271_spi_reset(struct wl1271
*wl
)
89 struct spi_transfer t
;
92 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
94 wl1271_error("could not allocate cmd for spi reset");
98 memset(&t
, 0, sizeof(t
));
101 memset(cmd
, 0xff, WSPI_INIT_CMD_LEN
);
104 t
.len
= WSPI_INIT_CMD_LEN
;
105 spi_message_add_tail(&t
, &m
);
107 spi_sync(wl_to_spi(wl
), &m
);
110 wl1271_dump(DEBUG_SPI
, "spi reset -> ", cmd
, WSPI_INIT_CMD_LEN
);
113 static void wl1271_spi_init(struct wl1271
*wl
)
115 u8 crc
[WSPI_INIT_CMD_CRC_LEN
], *cmd
;
116 struct spi_transfer t
;
117 struct spi_message m
;
119 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
121 wl1271_error("could not allocate cmd for spi init");
125 memset(crc
, 0, sizeof(crc
));
126 memset(&t
, 0, sizeof(t
));
127 spi_message_init(&m
);
130 * Set WSPI_INIT_COMMAND
131 * the data is being send from the MSB to LSB
135 cmd
[1] = WSPI_INIT_CMD_START
| WSPI_INIT_CMD_TX
;
138 cmd
[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK
<< 3;
139 cmd
[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN
& WSPI_INIT_CMD_FIXEDBUSY_LEN
;
141 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN
== 0)
142 cmd
[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY
;
144 cmd
[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY
;
146 cmd
[5] |= WSPI_INIT_CMD_IOD
| WSPI_INIT_CMD_IP
| WSPI_INIT_CMD_CS
147 | WSPI_INIT_CMD_WSPI
| WSPI_INIT_CMD_WS
;
155 cmd
[4] |= crc7(0, crc
, WSPI_INIT_CMD_CRC_LEN
) << 1;
156 cmd
[4] |= WSPI_INIT_CMD_END
;
159 t
.len
= WSPI_INIT_CMD_LEN
;
160 spi_message_add_tail(&t
, &m
);
162 spi_sync(wl_to_spi(wl
), &m
);
163 wl1271_dump(DEBUG_SPI
, "spi init -> ", cmd
, WSPI_INIT_CMD_LEN
);
167 #define WL1271_BUSY_WORD_TIMEOUT 1000
169 static int wl1271_spi_read_busy(struct wl1271
*wl
)
171 struct spi_transfer t
[1];
172 struct spi_message m
;
174 int num_busy_bytes
= 0;
177 * Read further busy words from SPI until a non-busy word is
178 * encountered, then read the data itself into the buffer.
181 num_busy_bytes
= WL1271_BUSY_WORD_TIMEOUT
;
182 busy_buf
= wl
->buffer_busyword
;
183 while (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(wl_to_spi(wl
), &m
);
197 /* The SPI bus is unresponsive, the read failed. */
198 wl1271_error("SPI read busy-word timeout!\n");
202 static void wl1271_spi_raw_read(struct wl1271
*wl
, int addr
, void *buf
,
203 size_t len
, bool fixed
)
205 struct spi_transfer t
[3];
206 struct spi_message m
;
210 cmd
= &wl
->buffer_cmd
;
211 busy_buf
= wl
->buffer_busyword
;
214 *cmd
|= WSPI_CMD_READ
;
215 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
216 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
219 *cmd
|= WSPI_CMD_FIXED
;
221 spi_message_init(&m
);
222 memset(t
, 0, sizeof(t
));
226 t
[0].cs_change
= true;
227 spi_message_add_tail(&t
[0], &m
);
229 /* Busy and non busy words read */
230 t
[1].rx_buf
= busy_buf
;
231 t
[1].len
= WL1271_BUSY_WORD_LEN
;
232 t
[1].cs_change
= true;
233 spi_message_add_tail(&t
[1], &m
);
235 spi_sync(wl_to_spi(wl
), &m
);
237 if (!(busy_buf
[WL1271_BUSY_WORD_CNT
- 1] & 0x1) &&
238 wl1271_spi_read_busy(wl
)) {
243 spi_message_init(&m
);
244 memset(t
, 0, sizeof(t
));
248 t
[0].cs_change
= true;
249 spi_message_add_tail(&t
[0], &m
);
251 spi_sync(wl_to_spi(wl
), &m
);
253 wl1271_dump(DEBUG_SPI
, "spi_read cmd -> ", cmd
, sizeof(*cmd
));
254 wl1271_dump(DEBUG_SPI
, "spi_read buf <- ", buf
, len
);
257 static void wl1271_spi_raw_write(struct wl1271
*wl
, int addr
, void *buf
,
258 size_t len
, bool fixed
)
260 struct spi_transfer t
[2];
261 struct spi_message m
;
264 cmd
= &wl
->buffer_cmd
;
267 *cmd
|= WSPI_CMD_WRITE
;
268 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
269 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
272 *cmd
|= WSPI_CMD_FIXED
;
274 spi_message_init(&m
);
275 memset(t
, 0, sizeof(t
));
278 t
[0].len
= sizeof(*cmd
);
279 spi_message_add_tail(&t
[0], &m
);
283 spi_message_add_tail(&t
[1], &m
);
285 spi_sync(wl_to_spi(wl
), &m
);
287 wl1271_dump(DEBUG_SPI
, "spi_write cmd -> ", cmd
, sizeof(*cmd
));
288 wl1271_dump(DEBUG_SPI
, "spi_write buf -> ", buf
, len
);
291 static irqreturn_t
wl1271_irq(int irq
, void *cookie
)
296 wl1271_debug(DEBUG_IRQ
, "IRQ");
300 /* complete the ELP completion */
301 spin_lock_irqsave(&wl
->wl_lock
, flags
);
303 complete(wl
->elp_compl
);
304 wl
->elp_compl
= NULL
;
307 if (!test_and_set_bit(WL1271_FLAG_IRQ_RUNNING
, &wl
->flags
))
308 ieee80211_queue_work(wl
->hw
, &wl
->irq_work
);
309 set_bit(WL1271_FLAG_IRQ_PENDING
, &wl
->flags
);
310 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
315 static void wl1271_spi_set_power(struct wl1271
*wl
, bool enable
)
318 wl
->set_power(enable
);
321 static struct wl1271_if_operations spi_ops
= {
322 .read
= wl1271_spi_raw_read
,
323 .write
= wl1271_spi_raw_write
,
324 .reset
= wl1271_spi_reset
,
325 .init
= wl1271_spi_init
,
326 .power
= wl1271_spi_set_power
,
327 .dev
= wl1271_spi_wl_to_dev
,
328 .enable_irq
= wl1271_spi_enable_interrupts
,
329 .disable_irq
= wl1271_spi_disable_interrupts
332 static int __devinit
wl1271_probe(struct spi_device
*spi
)
334 struct wl12xx_platform_data
*pdata
;
335 struct ieee80211_hw
*hw
;
339 pdata
= spi
->dev
.platform_data
;
341 wl1271_error("no platform data");
345 hw
= wl1271_alloc_hw();
351 dev_set_drvdata(&spi
->dev
, wl
);
354 wl
->if_ops
= &spi_ops
;
356 /* This is the only SPI value that we need to set here, the rest
357 * comes from the board-peripherals file */
358 spi
->bits_per_word
= 32;
360 ret
= spi_setup(spi
);
362 wl1271_error("spi_setup failed");
366 wl
->set_power
= pdata
->set_power
;
367 if (!wl
->set_power
) {
368 wl1271_error("set power function missing in platform data");
375 wl1271_error("irq missing in platform data");
380 ret
= request_irq(wl
->irq
, wl1271_irq
, 0, DRIVER_NAME
, wl
);
382 wl1271_error("request_irq() failed: %d", ret
);
386 set_irq_type(wl
->irq
, IRQ_TYPE_EDGE_RISING
);
388 disable_irq(wl
->irq
);
390 ret
= wl1271_init_ieee80211(wl
);
394 ret
= wl1271_register_hw(wl
);
398 wl1271_notice("initialized");
403 free_irq(wl
->irq
, wl
);
411 static int __devexit
wl1271_remove(struct spi_device
*spi
)
413 struct wl1271
*wl
= dev_get_drvdata(&spi
->dev
);
415 free_irq(wl
->irq
, wl
);
417 wl1271_unregister_hw(wl
);
424 static struct spi_driver wl1271_spi_driver
= {
426 .name
= "wl1271_spi",
427 .bus
= &spi_bus_type
,
428 .owner
= THIS_MODULE
,
431 .probe
= wl1271_probe
,
432 .remove
= __devexit_p(wl1271_remove
),
435 static int __init
wl1271_init(void)
439 ret
= spi_register_driver(&wl1271_spi_driver
);
441 wl1271_error("failed to register spi driver: %d", ret
);
449 static void __exit
wl1271_exit(void)
451 spi_unregister_driver(&wl1271_spi_driver
);
453 wl1271_notice("unloaded");
456 module_init(wl1271_init
);
457 module_exit(wl1271_exit
);
459 MODULE_LICENSE("GPL");
460 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
461 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
462 MODULE_FIRMWARE(WL1271_FW_NAME
);
463 MODULE_ALIAS("spi:wl1271");