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/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
30 #include "wl12xx_80211.h"
31 #include "wl1271_spi.h"
34 void wl1271_spi_reset(struct wl1271
*wl
)
37 struct spi_transfer t
;
40 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
42 wl1271_error("could not allocate cmd for spi reset");
46 memset(&t
, 0, sizeof(t
));
49 memset(cmd
, 0xff, WSPI_INIT_CMD_LEN
);
52 t
.len
= WSPI_INIT_CMD_LEN
;
53 spi_message_add_tail(&t
, &m
);
55 spi_sync(wl
->spi
, &m
);
57 wl1271_dump(DEBUG_SPI
, "spi reset -> ", cmd
, WSPI_INIT_CMD_LEN
);
60 void wl1271_spi_init(struct wl1271
*wl
)
62 u8 crc
[WSPI_INIT_CMD_CRC_LEN
], *cmd
;
63 struct spi_transfer t
;
66 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
68 wl1271_error("could not allocate cmd for spi init");
72 memset(crc
, 0, sizeof(crc
));
73 memset(&t
, 0, sizeof(t
));
77 * Set WSPI_INIT_COMMAND
78 * the data is being send from the MSB to LSB
82 cmd
[1] = WSPI_INIT_CMD_START
| WSPI_INIT_CMD_TX
;
85 cmd
[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK
<< 3;
86 cmd
[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN
& WSPI_INIT_CMD_FIXEDBUSY_LEN
;
88 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN
== 0)
89 cmd
[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY
;
91 cmd
[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY
;
93 cmd
[5] |= WSPI_INIT_CMD_IOD
| WSPI_INIT_CMD_IP
| WSPI_INIT_CMD_CS
94 | WSPI_INIT_CMD_WSPI
| WSPI_INIT_CMD_WS
;
102 cmd
[4] |= crc7(0, crc
, WSPI_INIT_CMD_CRC_LEN
) << 1;
103 cmd
[4] |= WSPI_INIT_CMD_END
;
106 t
.len
= WSPI_INIT_CMD_LEN
;
107 spi_message_add_tail(&t
, &m
);
109 spi_sync(wl
->spi
, &m
);
111 wl1271_dump(DEBUG_SPI
, "spi init -> ", cmd
, WSPI_INIT_CMD_LEN
);
114 #define WL1271_BUSY_WORD_TIMEOUT 1000
116 /* FIXME: Check busy words, removed due to SPI bug */
118 static void wl1271_spi_read_busy(struct wl1271
*wl
, void *buf
, size_t len
)
120 struct spi_transfer t
[1];
121 struct spi_message m
;
123 int num_busy_bytes
= 0;
125 wl1271_info("spi read BUSY!");
128 * Look for the non-busy word in the read buffer, and if found,
129 * read in the remaining data into the buffer.
131 busy_buf
= (u32
*)buf
;
132 for (; (u32
)busy_buf
< (u32
)buf
+ len
; busy_buf
++) {
133 num_busy_bytes
+= sizeof(u32
);
134 if (*busy_buf
& 0x1) {
135 spi_message_init(&m
);
136 memset(t
, 0, sizeof(t
));
137 memmove(buf
, busy_buf
, len
- num_busy_bytes
);
138 t
[0].rx_buf
= buf
+ (len
- num_busy_bytes
);
139 t
[0].len
= num_busy_bytes
;
140 spi_message_add_tail(&t
[0], &m
);
141 spi_sync(wl
->spi
, &m
);
147 * Read further busy words from SPI until a non-busy word is
148 * encountered, then read the data itself into the buffer.
150 wl1271_info("spi read BUSY-polling needed!");
152 num_busy_bytes
= WL1271_BUSY_WORD_TIMEOUT
;
153 busy_buf
= wl
->buffer_busyword
;
154 while (num_busy_bytes
) {
156 spi_message_init(&m
);
157 memset(t
, 0, sizeof(t
));
158 t
[0].rx_buf
= busy_buf
;
159 t
[0].len
= sizeof(u32
);
160 spi_message_add_tail(&t
[0], &m
);
161 spi_sync(wl
->spi
, &m
);
163 if (*busy_buf
& 0x1) {
164 spi_message_init(&m
);
165 memset(t
, 0, sizeof(t
));
168 spi_message_add_tail(&t
[0], &m
);
169 spi_sync(wl
->spi
, &m
);
174 /* The SPI bus is unresponsive, the read failed. */
176 wl1271_error("SPI read busy-word timeout!\n");
180 void wl1271_spi_raw_read(struct wl1271
*wl
, int addr
, void *buf
,
181 size_t len
, bool fixed
)
183 struct spi_transfer t
[3];
184 struct spi_message m
;
188 cmd
= &wl
->buffer_cmd
;
189 busy_buf
= wl
->buffer_busyword
;
192 *cmd
|= WSPI_CMD_READ
;
193 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
194 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
197 *cmd
|= WSPI_CMD_FIXED
;
199 spi_message_init(&m
);
200 memset(t
, 0, sizeof(t
));
204 spi_message_add_tail(&t
[0], &m
);
206 /* Busy and non busy words read */
207 t
[1].rx_buf
= busy_buf
;
208 t
[1].len
= WL1271_BUSY_WORD_LEN
;
209 spi_message_add_tail(&t
[1], &m
);
213 spi_message_add_tail(&t
[2], &m
);
215 spi_sync(wl
->spi
, &m
);
217 /* FIXME: Check busy words, removed due to SPI bug */
218 /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
219 wl1271_spi_read_busy(wl, buf, len); */
221 wl1271_dump(DEBUG_SPI
, "spi_read cmd -> ", cmd
, sizeof(*cmd
));
222 wl1271_dump(DEBUG_SPI
, "spi_read buf <- ", buf
, len
);
225 void wl1271_spi_raw_write(struct wl1271
*wl
, int addr
, void *buf
,
226 size_t len
, bool fixed
)
228 struct spi_transfer t
[2];
229 struct spi_message m
;
232 cmd
= &wl
->buffer_cmd
;
235 *cmd
|= WSPI_CMD_WRITE
;
236 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
237 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
240 *cmd
|= WSPI_CMD_FIXED
;
242 spi_message_init(&m
);
243 memset(t
, 0, sizeof(t
));
246 t
[0].len
= sizeof(*cmd
);
247 spi_message_add_tail(&t
[0], &m
);
251 spi_message_add_tail(&t
[1], &m
);
253 spi_sync(wl
->spi
, &m
);
255 wl1271_dump(DEBUG_SPI
, "spi_write cmd -> ", cmd
, sizeof(*cmd
));
256 wl1271_dump(DEBUG_SPI
, "spi_write buf -> ", buf
, len
);