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>
28 #include <linux/slab.h>
31 #include "wl12xx_80211.h"
32 #include "wl1271_spi.h"
35 void wl1271_spi_reset(struct wl1271
*wl
)
38 struct spi_transfer t
;
41 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
43 wl1271_error("could not allocate cmd for spi reset");
47 memset(&t
, 0, sizeof(t
));
50 memset(cmd
, 0xff, WSPI_INIT_CMD_LEN
);
53 t
.len
= WSPI_INIT_CMD_LEN
;
54 spi_message_add_tail(&t
, &m
);
56 spi_sync(wl
->spi
, &m
);
58 wl1271_dump(DEBUG_SPI
, "spi reset -> ", cmd
, WSPI_INIT_CMD_LEN
);
61 void wl1271_spi_init(struct wl1271
*wl
)
63 u8 crc
[WSPI_INIT_CMD_CRC_LEN
], *cmd
;
64 struct spi_transfer t
;
67 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
69 wl1271_error("could not allocate cmd for spi init");
73 memset(crc
, 0, sizeof(crc
));
74 memset(&t
, 0, sizeof(t
));
78 * Set WSPI_INIT_COMMAND
79 * the data is being send from the MSB to LSB
83 cmd
[1] = WSPI_INIT_CMD_START
| WSPI_INIT_CMD_TX
;
86 cmd
[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK
<< 3;
87 cmd
[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN
& WSPI_INIT_CMD_FIXEDBUSY_LEN
;
89 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN
== 0)
90 cmd
[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY
;
92 cmd
[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY
;
94 cmd
[5] |= WSPI_INIT_CMD_IOD
| WSPI_INIT_CMD_IP
| WSPI_INIT_CMD_CS
95 | WSPI_INIT_CMD_WSPI
| WSPI_INIT_CMD_WS
;
103 cmd
[4] |= crc7(0, crc
, WSPI_INIT_CMD_CRC_LEN
) << 1;
104 cmd
[4] |= WSPI_INIT_CMD_END
;
107 t
.len
= WSPI_INIT_CMD_LEN
;
108 spi_message_add_tail(&t
, &m
);
110 spi_sync(wl
->spi
, &m
);
112 wl1271_dump(DEBUG_SPI
, "spi init -> ", cmd
, WSPI_INIT_CMD_LEN
);
115 #define WL1271_BUSY_WORD_TIMEOUT 1000
117 /* FIXME: Check busy words, removed due to SPI bug */
119 static void wl1271_spi_read_busy(struct wl1271
*wl
, void *buf
, size_t len
)
121 struct spi_transfer t
[1];
122 struct spi_message m
;
124 int num_busy_bytes
= 0;
126 wl1271_info("spi read BUSY!");
129 * Look for the non-busy word in the read buffer, and if found,
130 * read in the remaining data into the buffer.
132 busy_buf
= (u32
*)buf
;
133 for (; (u32
)busy_buf
< (u32
)buf
+ len
; busy_buf
++) {
134 num_busy_bytes
+= sizeof(u32
);
135 if (*busy_buf
& 0x1) {
136 spi_message_init(&m
);
137 memset(t
, 0, sizeof(t
));
138 memmove(buf
, busy_buf
, len
- num_busy_bytes
);
139 t
[0].rx_buf
= buf
+ (len
- num_busy_bytes
);
140 t
[0].len
= num_busy_bytes
;
141 spi_message_add_tail(&t
[0], &m
);
142 spi_sync(wl
->spi
, &m
);
148 * Read further busy words from SPI until a non-busy word is
149 * encountered, then read the data itself into the buffer.
151 wl1271_info("spi read BUSY-polling needed!");
153 num_busy_bytes
= WL1271_BUSY_WORD_TIMEOUT
;
154 busy_buf
= wl
->buffer_busyword
;
155 while (num_busy_bytes
) {
157 spi_message_init(&m
);
158 memset(t
, 0, sizeof(t
));
159 t
[0].rx_buf
= busy_buf
;
160 t
[0].len
= sizeof(u32
);
161 spi_message_add_tail(&t
[0], &m
);
162 spi_sync(wl
->spi
, &m
);
164 if (*busy_buf
& 0x1) {
165 spi_message_init(&m
);
166 memset(t
, 0, sizeof(t
));
169 spi_message_add_tail(&t
[0], &m
);
170 spi_sync(wl
->spi
, &m
);
175 /* The SPI bus is unresponsive, the read failed. */
177 wl1271_error("SPI read busy-word timeout!\n");
181 void wl1271_spi_raw_read(struct wl1271
*wl
, int addr
, void *buf
,
182 size_t len
, bool fixed
)
184 struct spi_transfer t
[3];
185 struct spi_message m
;
189 cmd
= &wl
->buffer_cmd
;
190 busy_buf
= wl
->buffer_busyword
;
193 *cmd
|= WSPI_CMD_READ
;
194 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
195 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
198 *cmd
|= WSPI_CMD_FIXED
;
200 spi_message_init(&m
);
201 memset(t
, 0, sizeof(t
));
205 spi_message_add_tail(&t
[0], &m
);
207 /* Busy and non busy words read */
208 t
[1].rx_buf
= busy_buf
;
209 t
[1].len
= WL1271_BUSY_WORD_LEN
;
210 spi_message_add_tail(&t
[1], &m
);
214 spi_message_add_tail(&t
[2], &m
);
216 spi_sync(wl
->spi
, &m
);
218 /* FIXME: Check busy words, removed due to SPI bug */
219 /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
220 wl1271_spi_read_busy(wl, buf, len); */
222 wl1271_dump(DEBUG_SPI
, "spi_read cmd -> ", cmd
, sizeof(*cmd
));
223 wl1271_dump(DEBUG_SPI
, "spi_read buf <- ", buf
, len
);
226 void wl1271_spi_raw_write(struct wl1271
*wl
, int addr
, void *buf
,
227 size_t len
, bool fixed
)
229 struct spi_transfer t
[2];
230 struct spi_message m
;
233 cmd
= &wl
->buffer_cmd
;
236 *cmd
|= WSPI_CMD_WRITE
;
237 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
238 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
241 *cmd
|= WSPI_CMD_FIXED
;
243 spi_message_init(&m
);
244 memset(t
, 0, sizeof(t
));
247 t
[0].len
= sizeof(*cmd
);
248 spi_message_add_tail(&t
[0], &m
);
252 spi_message_add_tail(&t
[1], &m
);
254 spi_sync(wl
->spi
, &m
);
256 wl1271_dump(DEBUG_SPI
, "spi_write cmd -> ", cmd
, sizeof(*cmd
));
257 wl1271_dump(DEBUG_SPI
, "spi_write buf -> ", buf
, len
);