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>
34 #include "wl12xx_80211.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 /* HW limitation: maximum possible chunk size is 4095 bytes */
67 #define WSPI_MAX_CHUNK_SIZE 4092
70 * only support SPI for 12xx - this code should be reworked when 18xx
71 * support is introduced
73 #define SPI_AGGR_BUFFER_SIZE (4 * PAGE_SIZE)
75 #define WSPI_MAX_NUM_OF_CHUNKS (SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE)
77 struct wl12xx_spi_glue
{
79 struct platform_device
*core
;
82 static void wl12xx_spi_reset(struct device
*child
)
84 struct wl12xx_spi_glue
*glue
= dev_get_drvdata(child
->parent
);
86 struct spi_transfer t
;
89 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
91 dev_err(child
->parent
,
92 "could not allocate cmd for spi reset\n");
96 memset(&t
, 0, sizeof(t
));
99 memset(cmd
, 0xff, WSPI_INIT_CMD_LEN
);
102 t
.len
= WSPI_INIT_CMD_LEN
;
103 spi_message_add_tail(&t
, &m
);
105 spi_sync(to_spi_device(glue
->dev
), &m
);
110 static void wl12xx_spi_init(struct device
*child
)
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
;
117 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
119 dev_err(child
->parent
,
120 "could not allocate cmd for spi init\n");
124 memset(crc
, 0, sizeof(crc
));
125 memset(&t
, 0, sizeof(t
));
126 spi_message_init(&m
);
129 * Set WSPI_INIT_COMMAND
130 * the data is being send from the MSB to LSB
134 cmd
[1] = WSPI_INIT_CMD_START
| WSPI_INIT_CMD_TX
;
137 cmd
[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK
<< 3;
138 cmd
[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN
& WSPI_INIT_CMD_FIXEDBUSY_LEN
;
140 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN
== 0)
141 cmd
[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY
;
143 cmd
[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY
;
145 cmd
[5] |= WSPI_INIT_CMD_IOD
| WSPI_INIT_CMD_IP
| WSPI_INIT_CMD_CS
146 | WSPI_INIT_CMD_WSPI
| WSPI_INIT_CMD_WS
;
154 cmd
[4] |= crc7(0, crc
, WSPI_INIT_CMD_CRC_LEN
) << 1;
155 cmd
[4] |= WSPI_INIT_CMD_END
;
158 t
.len
= WSPI_INIT_CMD_LEN
;
159 spi_message_add_tail(&t
, &m
);
161 spi_sync(to_spi_device(glue
->dev
), &m
);
165 #define WL1271_BUSY_WORD_TIMEOUT 1000
167 static int wl12xx_spi_read_busy(struct device
*child
)
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
;
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(to_spi_device(glue
->dev
), &m
);
197 /* The SPI bus is unresponsive, the read failed. */
198 dev_err(child
->parent
, "SPI read busy-word timeout!\n");
202 static int __must_check
wl12xx_spi_raw_read(struct device
*child
, int addr
,
203 void *buf
, size_t len
, bool fixed
)
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
;
214 chunk_len
= min_t(size_t, WSPI_MAX_CHUNK_SIZE
, len
);
216 cmd
= &wl
->buffer_cmd
;
217 busy_buf
= wl
->buffer_busyword
;
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
;
226 *cmd
|= WSPI_CMD_FIXED
;
228 spi_message_init(&m
);
229 memset(t
, 0, sizeof(t
));
233 t
[0].cs_change
= true;
234 spi_message_add_tail(&t
[0], &m
);
236 /* Busy and non busy words read */
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
);
242 spi_sync(to_spi_device(glue
->dev
), &m
);
244 if (!(busy_buf
[WL1271_BUSY_WORD_CNT
- 1] & 0x1) &&
245 wl12xx_spi_read_busy(child
)) {
246 memset(buf
, 0, chunk_len
);
250 spi_message_init(&m
);
251 memset(t
, 0, sizeof(t
));
254 t
[0].len
= chunk_len
;
255 t
[0].cs_change
= true;
256 spi_message_add_tail(&t
[0], &m
);
258 spi_sync(to_spi_device(glue
->dev
), &m
);
269 static int __must_check
wl12xx_spi_raw_write(struct device
*child
, int addr
,
270 void *buf
, size_t len
, bool fixed
)
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
];
280 WARN_ON(len
> SPI_AGGR_BUFFER_SIZE
);
282 spi_message_init(&m
);
283 memset(t
, 0, sizeof(t
));
288 chunk_len
= min_t(size_t, WSPI_MAX_CHUNK_SIZE
, len
);
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
;
297 *cmd
|= WSPI_CMD_FIXED
;
300 t
[i
].len
= sizeof(*cmd
);
301 spi_message_add_tail(&t
[i
++], &m
);
304 t
[i
].len
= chunk_len
;
305 spi_message_add_tail(&t
[i
++], &m
);
314 spi_sync(to_spi_device(glue
->dev
), &m
);
319 static 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
,
327 static int wl1271_probe(struct spi_device
*spi
)
329 struct wl12xx_spi_glue
*glue
;
330 struct wlcore_platdev_data
*pdev_data
;
331 struct resource res
[1];
334 pdev_data
= kzalloc(sizeof(*pdev_data
), GFP_KERNEL
);
338 pdev_data
->pdata
= dev_get_platdata(&spi
->dev
);
339 if (!pdev_data
->pdata
) {
340 dev_err(&spi
->dev
, "no platform data\n");
342 goto out_free_pdev_data
;
345 pdev_data
->if_ops
= &spi_ops
;
347 glue
= kzalloc(sizeof(*glue
), GFP_KERNEL
);
349 dev_err(&spi
->dev
, "can't allocate glue\n");
350 goto out_free_pdev_data
;
353 glue
->dev
= &spi
->dev
;
355 spi_set_drvdata(spi
, glue
);
357 /* This is the only SPI value that we need to set here, the rest
358 * comes from the board-peripherals file */
359 spi
->bits_per_word
= 32;
361 ret
= spi_setup(spi
);
363 dev_err(glue
->dev
, "spi_setup failed\n");
367 glue
->core
= platform_device_alloc("wl12xx", PLATFORM_DEVID_AUTO
);
369 dev_err(glue
->dev
, "can't allocate platform_device\n");
374 glue
->core
->dev
.parent
= &spi
->dev
;
376 memset(res
, 0x00, sizeof(res
));
378 res
[0].start
= spi
->irq
;
379 res
[0].flags
= IORESOURCE_IRQ
;
382 ret
= platform_device_add_resources(glue
->core
, res
, ARRAY_SIZE(res
));
384 dev_err(glue
->dev
, "can't add resources\n");
388 ret
= platform_device_add_data(glue
->core
, pdev_data
,
391 dev_err(glue
->dev
, "can't add platform data\n");
395 ret
= platform_device_add(glue
->core
);
397 dev_err(glue
->dev
, "can't register platform device\n");
404 platform_device_put(glue
->core
);
416 static int wl1271_remove(struct spi_device
*spi
)
418 struct wl12xx_spi_glue
*glue
= spi_get_drvdata(spi
);
420 platform_device_unregister(glue
->core
);
427 static struct spi_driver wl1271_spi_driver
= {
429 .name
= "wl1271_spi",
430 .owner
= THIS_MODULE
,
433 .probe
= wl1271_probe
,
434 .remove
= wl1271_remove
,
437 module_spi_driver(wl1271_spi_driver
);
438 MODULE_LICENSE("GPL");
439 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
440 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
441 MODULE_ALIAS("spi:wl1271");