blob: ee9564aa6ecc0851bf5f3daabe05588935765286 [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/platform_device.h>
26#include <linux/crc7.h>
27#include <linux/spi/spi.h>
28
29#include "wl1271.h"
30#include "wl12xx_80211.h"
31#include "wl1271_spi.h"
32
Juuso Oikarinen451de972009-10-12 15:08:46 +030033static int wl1271_translate_addr(struct wl1271 *wl, int addr)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030034{
Juuso Oikarinen451de972009-10-12 15:08:46 +030035 /*
36 * To translate, first check to which window of addresses the
37 * particular address belongs. Then subtract the starting address
38 * of that window from the address. Then, add offset of the
39 * translated region.
40 *
41 * The translated regions occur next to each other in physical device
42 * memory, so just add the sizes of the preceeding address regions to
43 * get the offset to the new region.
44 *
45 * Currently, only the two first regions are addressed, and the
46 * assumption is that all addresses will fall into either of those
47 * two.
48 */
49 if ((addr >= wl->part.reg.start) &&
50 (addr < wl->part.reg.start + wl->part.reg.size))
51 return addr - wl->part.reg.start + wl->part.mem.size;
52 else
53 return addr - wl->part.mem.start;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030054}
55
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030056void wl1271_spi_reset(struct wl1271 *wl)
57{
58 u8 *cmd;
59 struct spi_transfer t;
60 struct spi_message m;
61
62 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
63 if (!cmd) {
64 wl1271_error("could not allocate cmd for spi reset");
65 return;
66 }
67
68 memset(&t, 0, sizeof(t));
69 spi_message_init(&m);
70
71 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
72
73 t.tx_buf = cmd;
74 t.len = WSPI_INIT_CMD_LEN;
75 spi_message_add_tail(&t, &m);
76
77 spi_sync(wl->spi, &m);
78
79 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
80}
81
82void wl1271_spi_init(struct wl1271 *wl)
83{
84 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
85 struct spi_transfer t;
86 struct spi_message m;
87
88 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
89 if (!cmd) {
90 wl1271_error("could not allocate cmd for spi init");
91 return;
92 }
93
94 memset(crc, 0, sizeof(crc));
95 memset(&t, 0, sizeof(t));
96 spi_message_init(&m);
97
98 /*
99 * Set WSPI_INIT_COMMAND
100 * the data is being send from the MSB to LSB
101 */
102 cmd[2] = 0xff;
103 cmd[3] = 0xff;
104 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
105 cmd[0] = 0;
106 cmd[7] = 0;
107 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
108 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
109
110 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
111 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
112 else
113 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
114
115 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
116 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
117
118 crc[0] = cmd[1];
119 crc[1] = cmd[0];
120 crc[2] = cmd[7];
121 crc[3] = cmd[6];
122 crc[4] = cmd[5];
123
124 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
125 cmd[4] |= WSPI_INIT_CMD_END;
126
127 t.tx_buf = cmd;
128 t.len = WSPI_INIT_CMD_LEN;
129 spi_message_add_tail(&t, &m);
130
131 spi_sync(wl->spi, &m);
132
133 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
134}
135
136/* Set the SPI partitions to access the chip addresses
137 *
Juuso Oikarinen451de972009-10-12 15:08:46 +0300138 * To simplify driver code, a fixed (virtual) memory map is defined for
139 * register and memory addresses. Because in the chipset, in different stages
140 * of operation, those addresses will move around, an address translation
141 * mechanism is required.
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300142 *
Juuso Oikarinen451de972009-10-12 15:08:46 +0300143 * There are four partitions (three memory and one register partition),
144 * which are mapped to two different areas of the hardware memory.
145 *
146 * Virtual address
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300147 * space
148 *
149 * | |
Juuso Oikarinen451de972009-10-12 15:08:46 +0300150 * ...+----+--> mem.start
151 * Physical address ... | |
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300152 * space ... | | [PART_0]
153 * ... | |
Juuso Oikarinen451de972009-10-12 15:08:46 +0300154 * 00000000 <--+----+... ...+----+--> mem.start + mem.size
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300155 * | | ... | |
156 * |MEM | ... | |
157 * | | ... | |
Juuso Oikarinen451de972009-10-12 15:08:46 +0300158 * mem.size <--+----+... | | {unused area)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300159 * | | ... | |
160 * |REG | ... | |
Juuso Oikarinen451de972009-10-12 15:08:46 +0300161 * mem.size | | ... | |
162 * + <--+----+... ...+----+--> reg.start
163 * reg.size | | ... | |
164 * |MEM2| ... | | [PART_1]
165 * | | ... | |
166 * ...+----+--> reg.start + reg.size
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300167 * | |
168 *
169 */
170int wl1271_set_partition(struct wl1271 *wl,
Juuso Oikarinen451de972009-10-12 15:08:46 +0300171 struct wl1271_partition_set *p)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300172{
Juuso Oikarinen451de972009-10-12 15:08:46 +0300173 /* copy partition info */
174 memcpy(&wl->part, p, sizeof(*p));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300175
176 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Juuso Oikarinen451de972009-10-12 15:08:46 +0300177 p->mem.start, p->mem.size);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300178 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Juuso Oikarinen451de972009-10-12 15:08:46 +0300179 p->reg.start, p->reg.size);
180 wl1271_debug(DEBUG_SPI, "mem2_start %08X mem2_size %08X",
181 p->mem2.start, p->mem2.size);
182 wl1271_debug(DEBUG_SPI, "mem3_start %08X mem3_size %08X",
183 p->mem3.start, p->mem3.size);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300184
Juuso Oikarinen451de972009-10-12 15:08:46 +0300185 /* write partition info to the chipset */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300186 wl1271_raw_write32(wl, HW_PART0_START_ADDR, p->mem.start);
187 wl1271_raw_write32(wl, HW_PART0_SIZE_ADDR, p->mem.size);
188 wl1271_raw_write32(wl, HW_PART1_START_ADDR, p->reg.start);
189 wl1271_raw_write32(wl, HW_PART1_SIZE_ADDR, p->reg.size);
190 wl1271_raw_write32(wl, HW_PART2_START_ADDR, p->mem2.start);
191 wl1271_raw_write32(wl, HW_PART2_SIZE_ADDR, p->mem2.size);
192 wl1271_raw_write32(wl, HW_PART3_START_ADDR, p->mem3.start);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300193
194 return 0;
195}
196
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300197#define WL1271_BUSY_WORD_TIMEOUT 1000
198
Luciano Coelho938e30c2009-10-15 10:33:27 +0300199/* FIXME: Check busy words, removed due to SPI bug */
200#if 0
201static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300202{
203 struct spi_transfer t[1];
204 struct spi_message m;
205 u32 *busy_buf;
206 int num_busy_bytes = 0;
207
208 wl1271_info("spi read BUSY!");
209
210 /*
211 * Look for the non-busy word in the read buffer, and if found,
212 * read in the remaining data into the buffer.
213 */
214 busy_buf = (u32 *)buf;
215 for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
216 num_busy_bytes += sizeof(u32);
217 if (*busy_buf & 0x1) {
218 spi_message_init(&m);
219 memset(t, 0, sizeof(t));
220 memmove(buf, busy_buf, len - num_busy_bytes);
221 t[0].rx_buf = buf + (len - num_busy_bytes);
222 t[0].len = num_busy_bytes;
223 spi_message_add_tail(&t[0], &m);
224 spi_sync(wl->spi, &m);
225 return;
226 }
227 }
228
229 /*
230 * Read further busy words from SPI until a non-busy word is
231 * encountered, then read the data itself into the buffer.
232 */
233 wl1271_info("spi read BUSY-polling needed!");
234
235 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
236 busy_buf = wl->buffer_busyword;
237 while (num_busy_bytes) {
238 num_busy_bytes--;
239 spi_message_init(&m);
240 memset(t, 0, sizeof(t));
241 t[0].rx_buf = busy_buf;
242 t[0].len = sizeof(u32);
243 spi_message_add_tail(&t[0], &m);
244 spi_sync(wl->spi, &m);
245
246 if (*busy_buf & 0x1) {
247 spi_message_init(&m);
248 memset(t, 0, sizeof(t));
249 t[0].rx_buf = buf;
250 t[0].len = len;
251 spi_message_add_tail(&t[0], &m);
252 spi_sync(wl->spi, &m);
253 return;
254 }
255 }
256
257 /* The SPI bus is unresponsive, the read failed. */
258 memset(buf, 0, len);
259 wl1271_error("SPI read busy-word timeout!\n");
260}
Luciano Coelho938e30c2009-10-15 10:33:27 +0300261#endif
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300262
Juuso Oikarinen74621412009-10-12 15:08:54 +0300263void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
264 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300265{
266 struct spi_transfer t[3];
267 struct spi_message m;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300268 u32 *busy_buf;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300269 u32 *cmd;
270
271 cmd = &wl->buffer_cmd;
272 busy_buf = wl->buffer_busyword;
273
274 *cmd = 0;
275 *cmd |= WSPI_CMD_READ;
276 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
277 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
278
279 if (fixed)
280 *cmd |= WSPI_CMD_FIXED;
281
282 spi_message_init(&m);
283 memset(t, 0, sizeof(t));
284
285 t[0].tx_buf = cmd;
286 t[0].len = 4;
287 spi_message_add_tail(&t[0], &m);
288
289 /* Busy and non busy words read */
290 t[1].rx_buf = busy_buf;
291 t[1].len = WL1271_BUSY_WORD_LEN;
292 spi_message_add_tail(&t[1], &m);
293
294 t[2].rx_buf = buf;
295 t[2].len = len;
296 spi_message_add_tail(&t[2], &m);
297
298 spi_sync(wl->spi, &m);
299
Juuso Oikarinenc6d5d062009-10-13 12:47:47 +0300300 /* FIXME: Check busy words, removed due to SPI bug */
301 /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
302 wl1271_spi_read_busy(wl, buf, len); */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300303
304 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
305 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
306}
307
Juuso Oikarinen74621412009-10-12 15:08:54 +0300308void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
309 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300310{
311 struct spi_transfer t[2];
312 struct spi_message m;
313 u32 *cmd;
314
315 cmd = &wl->buffer_cmd;
316
317 *cmd = 0;
318 *cmd |= WSPI_CMD_WRITE;
319 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
320 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
321
322 if (fixed)
323 *cmd |= WSPI_CMD_FIXED;
324
325 spi_message_init(&m);
326 memset(t, 0, sizeof(t));
327
328 t[0].tx_buf = cmd;
329 t[0].len = sizeof(*cmd);
330 spi_message_add_tail(&t[0], &m);
331
332 t[1].tx_buf = buf;
333 t[1].len = len;
334 spi_message_add_tail(&t[1], &m);
335
336 spi_sync(wl->spi, &m);
337
338 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
339 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
340}
341
Juuso Oikarinen74621412009-10-12 15:08:54 +0300342void wl1271_spi_read(struct wl1271 *wl, int addr, void *buf, size_t len,
343 bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300344{
345 int physical;
346
Juuso Oikarinen451de972009-10-12 15:08:46 +0300347 physical = wl1271_translate_addr(wl, addr);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300348
Juuso Oikarinen74621412009-10-12 15:08:54 +0300349 wl1271_spi_raw_read(wl, physical, buf, len, fixed);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300350}
351
Juuso Oikarinen74621412009-10-12 15:08:54 +0300352void wl1271_spi_write(struct wl1271 *wl, int addr, void *buf, size_t len,
353 bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300354{
355 int physical;
356
Juuso Oikarinen451de972009-10-12 15:08:46 +0300357 physical = wl1271_translate_addr(wl, addr);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300358
Juuso Oikarinen74621412009-10-12 15:08:54 +0300359 wl1271_spi_raw_write(wl, physical, buf, len, fixed);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300360}
361
Juuso Oikarinen74621412009-10-12 15:08:54 +0300362u32 wl1271_spi_read32(struct wl1271 *wl, int addr)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300363{
Juuso Oikarinen74621412009-10-12 15:08:54 +0300364 return wl1271_raw_read32(wl, wl1271_translate_addr(wl, addr));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300365}
366
Juuso Oikarinen74621412009-10-12 15:08:54 +0300367void wl1271_spi_write32(struct wl1271 *wl, int addr, u32 val)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300368{
Juuso Oikarinen74621412009-10-12 15:08:54 +0300369 wl1271_raw_write32(wl, wl1271_translate_addr(wl, addr), val);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300370}
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300371
372void wl1271_top_reg_write(struct wl1271 *wl, int addr, u16 val)
373{
374 /* write address >> 1 + 0x30000 to OCP_POR_CTR */
375 addr = (addr >> 1) + 0x30000;
Juuso Oikarinen74621412009-10-12 15:08:54 +0300376 wl1271_spi_write32(wl, OCP_POR_CTR, addr);
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300377
378 /* write value to OCP_POR_WDATA */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300379 wl1271_spi_write32(wl, OCP_DATA_WRITE, val);
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300380
381 /* write 1 to OCP_CMD */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300382 wl1271_spi_write32(wl, OCP_CMD, OCP_CMD_WRITE);
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300383}
384
385u16 wl1271_top_reg_read(struct wl1271 *wl, int addr)
386{
387 u32 val;
388 int timeout = OCP_CMD_LOOP;
389
390 /* write address >> 1 + 0x30000 to OCP_POR_CTR */
391 addr = (addr >> 1) + 0x30000;
Juuso Oikarinen74621412009-10-12 15:08:54 +0300392 wl1271_spi_write32(wl, OCP_POR_CTR, addr);
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300393
394 /* write 2 to OCP_CMD */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300395 wl1271_spi_write32(wl, OCP_CMD, OCP_CMD_READ);
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300396
397 /* poll for data ready */
398 do {
Juuso Oikarinen74621412009-10-12 15:08:54 +0300399 val = wl1271_spi_read32(wl, OCP_DATA_READ);
Juha Leppanen51633632010-01-04 15:52:50 -0500400 } while (!(val & OCP_READY_MASK) && --timeout);
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300401
402 if (!timeout) {
403 wl1271_warning("Top register access timed out.");
404 return 0xffff;
405 }
406
407 /* check data status and return if OK */
408 if ((val & OCP_STATUS_MASK) == OCP_STATUS_OK)
409 return val & 0xffff;
410 else {
411 wl1271_warning("Top register access returned error.");
412 return 0xffff;
413 }
414}