2 * This file is part of wl12xx
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>
29 #include "wl12xx_80211.h"
34 static int wl12xx_translate_reg_addr(struct wl12xx *wl, int addr)
36 /* If the address is lower than REGISTERS_BASE, it means that this is
37 * a chip-specific register address, so look it up in the registers
39 if (addr < REGISTERS_BASE) {
40 /* Make sure we don't go over the table */
41 if (addr >= ACX_REG_TABLE_LEN) {
42 wl12xx_error("address out of range (%d)", addr);
45 addr = wl->chip.acx_reg_table[addr];
48 return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
51 static int wl12xx_translate_mem_addr(struct wl12xx *wl, int addr)
53 return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
57 void wl12xx_spi_reset(struct wl12xx *wl)
60 struct spi_transfer t;
63 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
65 wl12xx_error("could not allocate cmd for spi reset");
69 memset(&t, 0, sizeof(t));
72 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
75 t.len = WSPI_INIT_CMD_LEN;
76 spi_message_add_tail(&t, &m);
78 spi_sync(wl->spi, &m);
80 wl12xx_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
83 void wl12xx_spi_init(struct wl12xx *wl)
85 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
86 struct spi_transfer t;
89 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
91 wl12xx_error("could not allocate cmd for spi init");
95 memset(crc, 0, sizeof(crc));
96 memset(&t, 0, sizeof(t));
100 * Set WSPI_INIT_COMMAND
101 * the data is being send from the MSB to LSB
105 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
108 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
109 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
111 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
112 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
114 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
116 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
117 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
125 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
126 cmd[4] |= WSPI_INIT_CMD_END;
129 t.len = WSPI_INIT_CMD_LEN;
130 spi_message_add_tail(&t, &m);
132 spi_sync(wl->spi, &m);
134 wl12xx_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
137 /* Set the SPI partitions to access the chip addresses
139 * There are two VIRTUAL (SPI) partitions (the memory partition and the
140 * registers partition), which are mapped to two different areas of the
141 * PHYSICAL (hardware) memory. This function also makes other checks to
142 * ensure that the partitions are not overlapping. In the diagram below, the
143 * memory partition comes before the register partition, but the opposite is
150 * ...+----+--> mem_start
151 * VIRTUAL address ... | |
152 * space ... | | [PART_0]
154 * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
158 * part_size <--+----+... | | {unused area)
161 * part_size | | ... | |
162 * + <--+----+... ...+----+--> reg_start
166 * ...+----+--> reg_start + reg_size
170 int wl12xx_set_partition(struct wl12xx *wl,
171 u32 mem_start, u32 mem_size,
172 u32 reg_start, u32 reg_size)
174 struct wl12xx_partition *partition;
175 struct spi_transfer t;
176 struct spi_message m;
181 cmd_len = sizeof(u32) + 2 * sizeof(struct wl12xx_partition);
182 cmd = kzalloc(cmd_len, GFP_KERNEL);
186 spi_message_init(&m);
187 memset(&t, 0, sizeof(t));
189 partition = (struct wl12xx_partition *) (cmd + 1);
190 addr = HW_ACCESS_PART0_SIZE_ADDR;
191 len = 2 * sizeof(struct wl12xx_partition);
193 *cmd |= WSPI_CMD_WRITE;
194 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
195 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
197 wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
198 mem_start, mem_size);
199 wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
200 reg_start, reg_size);
202 /* Make sure that the two partitions together don't exceed the
204 if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
205 wl12xx_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
206 " address range. Truncating partition[0].");
207 mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
208 wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
209 mem_start, mem_size);
210 wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
211 reg_start, reg_size);
214 if ((mem_start < reg_start) &&
215 ((mem_start + mem_size) > reg_start)) {
216 /* Guarantee that the memory partition doesn't overlap the
217 * registers partition */
218 wl12xx_debug(DEBUG_SPI, "End of partition[0] is "
219 "overlapping partition[1]. Adjusted.");
220 mem_size = reg_start - mem_start;
221 wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
222 mem_start, mem_size);
223 wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
224 reg_start, reg_size);
225 } else if ((reg_start < mem_start) &&
226 ((reg_start + reg_size) > mem_start)) {
227 /* Guarantee that the register partition doesn't overlap the
228 * memory partition */
229 wl12xx_debug(DEBUG_SPI, "End of partition[1] is"
230 " overlapping partition[0]. Adjusted.");
231 reg_size = mem_start - reg_start;
232 wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
233 mem_start, mem_size);
234 wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
235 reg_start, reg_size);
238 partition[0].start = mem_start;
239 partition[0].size = mem_size;
240 partition[1].start = reg_start;
241 partition[1].size = reg_size;
243 wl->physical_mem_addr = mem_start;
244 wl->physical_reg_addr = reg_start;
246 wl->virtual_mem_addr = 0;
247 wl->virtual_reg_addr = mem_size;
251 spi_message_add_tail(&t, &m);
253 spi_sync(wl->spi, &m);
260 void wl12xx_spi_read(struct wl12xx *wl, int addr, void *buf,
261 size_t len, bool fixed)
263 struct spi_transfer t[3];
264 struct spi_message m;
268 cmd = &wl->buffer_cmd;
269 busy_buf = wl->buffer_busyword;
272 *cmd |= WSPI_CMD_READ;
273 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
274 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
277 *cmd |= WSPI_CMD_FIXED;
279 spi_message_init(&m);
280 memset(t, 0, sizeof(t));
284 spi_message_add_tail(&t[0], &m);
286 /* Busy and non busy words read */
287 t[1].rx_buf = busy_buf;
288 t[1].len = WL12XX_BUSY_WORD_LEN;
289 spi_message_add_tail(&t[1], &m);
293 spi_message_add_tail(&t[2], &m);
295 spi_sync(wl->spi, &m);
297 /* FIXME: check busy words */
299 wl12xx_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
300 wl12xx_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
303 void wl12xx_spi_write(struct wl12xx *wl, int addr, void *buf,
304 size_t len, bool fixed)
306 struct spi_transfer t[2];
307 struct spi_message m;
310 cmd = &wl->buffer_cmd;
313 *cmd |= WSPI_CMD_WRITE;
314 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
315 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
318 *cmd |= WSPI_CMD_FIXED;
320 spi_message_init(&m);
321 memset(t, 0, sizeof(t));
324 t[0].len = sizeof(*cmd);
325 spi_message_add_tail(&t[0], &m);
329 spi_message_add_tail(&t[1], &m);
331 spi_sync(wl->spi, &m);
333 wl12xx_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
334 wl12xx_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
337 void wl12xx_spi_mem_read(struct wl12xx *wl, int addr, void *buf,
342 physical = wl12xx_translate_mem_addr(wl, addr);
344 wl12xx_spi_read(wl, physical, buf, len, false);
347 void wl12xx_spi_mem_write(struct wl12xx *wl, int addr, void *buf,
352 physical = wl12xx_translate_mem_addr(wl, addr);
354 wl12xx_spi_write(wl, physical, buf, len, false);
357 void wl12xx_spi_reg_read(struct wl12xx *wl, int addr, void *buf, size_t len,
362 physical = wl12xx_translate_reg_addr(wl, addr);
364 wl12xx_spi_read(wl, physical, buf, len, fixed);
367 void wl12xx_spi_reg_write(struct wl12xx *wl, int addr, void *buf, size_t len,
372 physical = wl12xx_translate_reg_addr(wl, addr);
374 wl12xx_spi_write(wl, physical, buf, len, fixed);
377 u32 wl12xx_mem_read32(struct wl12xx *wl, int addr)
379 return wl12xx_read32(wl, wl12xx_translate_mem_addr(wl, addr));
382 void wl12xx_mem_write32(struct wl12xx *wl, int addr, u32 val)
384 wl12xx_write32(wl, wl12xx_translate_mem_addr(wl, addr), val);
387 u32 wl12xx_reg_read32(struct wl12xx *wl, int addr)
389 return wl12xx_read32(wl, wl12xx_translate_reg_addr(wl, addr));
392 void wl12xx_reg_write32(struct wl12xx *wl, int addr, u32 val)
394 wl12xx_write32(wl, wl12xx_translate_reg_addr(wl, addr), val);