2 * This file is part of wl1251
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Kalle Valo <kalle.valo@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/crc7.h>
26 #include <linux/spi/spi.h>
30 #include "wl1251_spi.h"
32 static int wl1251_translate_reg_addr(struct wl1251 *wl, int addr)
34 /* If the address is lower than REGISTERS_BASE, it means that this is
35 * a chip-specific register address, so look it up in the registers
37 if (addr < REGISTERS_BASE) {
38 /* Make sure we don't go over the table */
39 if (addr >= ACX_REG_TABLE_LEN) {
40 wl1251_error("address out of range (%d)", addr);
43 addr = wl->chip.acx_reg_table[addr];
46 return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
49 static int wl1251_translate_mem_addr(struct wl1251 *wl, int addr)
51 return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
55 void wl1251_spi_reset(struct wl1251 *wl)
58 struct spi_transfer t;
61 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
63 wl1251_error("could not allocate cmd for spi reset");
67 memset(&t, 0, sizeof(t));
70 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
73 t.len = WSPI_INIT_CMD_LEN;
74 spi_message_add_tail(&t, &m);
76 spi_sync(wl->spi, &m);
78 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
81 void wl1251_spi_init(struct wl1251 *wl)
83 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
84 struct spi_transfer t;
87 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
89 wl1251_error("could not allocate cmd for spi init");
93 memset(crc, 0, sizeof(crc));
94 memset(&t, 0, sizeof(t));
98 * Set WSPI_INIT_COMMAND
99 * the data is being send from the MSB to LSB
103 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
106 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
107 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
109 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
110 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
112 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
114 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
115 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
123 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
124 cmd[4] |= WSPI_INIT_CMD_END;
127 t.len = WSPI_INIT_CMD_LEN;
128 spi_message_add_tail(&t, &m);
130 spi_sync(wl->spi, &m);
132 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
135 /* Set the SPI partitions to access the chip addresses
137 * There are two VIRTUAL (SPI) partitions (the memory partition and the
138 * registers partition), which are mapped to two different areas of the
139 * PHYSICAL (hardware) memory. This function also makes other checks to
140 * ensure that the partitions are not overlapping. In the diagram below, the
141 * memory partition comes before the register partition, but the opposite is
148 * ...+----+--> mem_start
149 * VIRTUAL address ... | |
150 * space ... | | [PART_0]
152 * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
156 * part_size <--+----+... | | {unused area)
159 * part_size | | ... | |
160 * + <--+----+... ...+----+--> reg_start
164 * ...+----+--> reg_start + reg_size
168 int wl1251_set_partition(struct wl1251 *wl,
169 u32 mem_start, u32 mem_size,
170 u32 reg_start, u32 reg_size)
172 struct wl1251_partition *partition;
173 struct spi_transfer t;
174 struct spi_message m;
179 cmd_len = sizeof(u32) + 2 * sizeof(struct wl1251_partition);
180 cmd = kzalloc(cmd_len, GFP_KERNEL);
184 spi_message_init(&m);
185 memset(&t, 0, sizeof(t));
187 partition = (struct wl1251_partition *) (cmd + 1);
188 addr = HW_ACCESS_PART0_SIZE_ADDR;
189 len = 2 * sizeof(struct wl1251_partition);
191 *cmd |= WSPI_CMD_WRITE;
192 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
193 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
195 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
196 mem_start, mem_size);
197 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
198 reg_start, reg_size);
200 /* Make sure that the two partitions together don't exceed the
202 if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
203 wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
204 " address range. Truncating partition[0].");
205 mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
206 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
207 mem_start, mem_size);
208 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
209 reg_start, reg_size);
212 if ((mem_start < reg_start) &&
213 ((mem_start + mem_size) > reg_start)) {
214 /* Guarantee that the memory partition doesn't overlap the
215 * registers partition */
216 wl1251_debug(DEBUG_SPI, "End of partition[0] is "
217 "overlapping partition[1]. Adjusted.");
218 mem_size = reg_start - mem_start;
219 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
220 mem_start, mem_size);
221 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
222 reg_start, reg_size);
223 } else if ((reg_start < mem_start) &&
224 ((reg_start + reg_size) > mem_start)) {
225 /* Guarantee that the register partition doesn't overlap the
226 * memory partition */
227 wl1251_debug(DEBUG_SPI, "End of partition[1] is"
228 " overlapping partition[0]. Adjusted.");
229 reg_size = mem_start - reg_start;
230 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
231 mem_start, mem_size);
232 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
233 reg_start, reg_size);
236 partition[0].start = mem_start;
237 partition[0].size = mem_size;
238 partition[1].start = reg_start;
239 partition[1].size = reg_size;
241 wl->physical_mem_addr = mem_start;
242 wl->physical_reg_addr = reg_start;
244 wl->virtual_mem_addr = 0;
245 wl->virtual_reg_addr = mem_size;
249 spi_message_add_tail(&t, &m);
251 spi_sync(wl->spi, &m);
258 void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, size_t len)
260 struct spi_transfer t[3];
261 struct spi_message m;
265 cmd = &wl->buffer_cmd;
266 busy_buf = wl->buffer_busyword;
269 *cmd |= WSPI_CMD_READ;
270 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
271 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
273 spi_message_init(&m);
274 memset(t, 0, sizeof(t));
278 spi_message_add_tail(&t[0], &m);
280 /* Busy and non busy words read */
281 t[1].rx_buf = busy_buf;
282 t[1].len = WL1251_BUSY_WORD_LEN;
283 spi_message_add_tail(&t[1], &m);
287 spi_message_add_tail(&t[2], &m);
289 spi_sync(wl->spi, &m);
291 /* FIXME: check busy words */
293 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
294 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
297 void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, size_t len)
299 struct spi_transfer t[2];
300 struct spi_message m;
303 cmd = &wl->buffer_cmd;
306 *cmd |= WSPI_CMD_WRITE;
307 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
308 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
310 spi_message_init(&m);
311 memset(t, 0, sizeof(t));
314 t[0].len = sizeof(*cmd);
315 spi_message_add_tail(&t[0], &m);
319 spi_message_add_tail(&t[1], &m);
321 spi_sync(wl->spi, &m);
323 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
324 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
327 void wl1251_spi_mem_read(struct wl1251 *wl, int addr, void *buf,
332 physical = wl1251_translate_mem_addr(wl, addr);
334 wl1251_spi_read(wl, physical, buf, len);
337 void wl1251_spi_mem_write(struct wl1251 *wl, int addr, void *buf,
342 physical = wl1251_translate_mem_addr(wl, addr);
344 wl1251_spi_write(wl, physical, buf, len);
347 void wl1251_spi_reg_read(struct wl1251 *wl, int addr, void *buf, size_t len)
351 physical = wl1251_translate_reg_addr(wl, addr);
353 wl1251_spi_read(wl, physical, buf, len);
356 void wl1251_spi_reg_write(struct wl1251 *wl, int addr, void *buf, size_t len)
360 physical = wl1251_translate_reg_addr(wl, addr);
362 wl1251_spi_write(wl, physical, buf, len);
365 u32 wl1251_mem_read32(struct wl1251 *wl, int addr)
367 return wl1251_read32(wl, wl1251_translate_mem_addr(wl, addr));
370 void wl1251_mem_write32(struct wl1251 *wl, int addr, u32 val)
372 wl1251_write32(wl, wl1251_translate_mem_addr(wl, addr), val);
375 u32 wl1251_reg_read32(struct wl1251 *wl, int addr)
377 return wl1251_read32(wl, wl1251_translate_reg_addr(wl, addr));
380 void wl1251_reg_write32(struct wl1251 *wl, int addr, u32 val)
382 wl1251_write32(wl, wl1251_translate_reg_addr(wl, addr), val);