2 * This file is part of wl1271
4 * Copyright (C) 2008-2010 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"
32 #include "wl1271_io.h"
34 static int wl1271_translate_addr(struct wl1271
*wl
, int addr
)
37 * To translate, first check to which window of addresses the
38 * particular address belongs. Then subtract the starting address
39 * of that window from the address. Then, add offset of the
42 * The translated regions occur next to each other in physical device
43 * memory, so just add the sizes of the preceeding address regions to
44 * get the offset to the new region.
46 * Currently, only the two first regions are addressed, and the
47 * assumption is that all addresses will fall into either of those
50 if ((addr
>= wl
->part
.reg
.start
) &&
51 (addr
< wl
->part
.reg
.start
+ wl
->part
.reg
.size
))
52 return addr
- wl
->part
.reg
.start
+ wl
->part
.mem
.size
;
54 return addr
- wl
->part
.mem
.start
;
57 /* Set the SPI partitions to access the chip addresses
59 * To simplify driver code, a fixed (virtual) memory map is defined for
60 * register and memory addresses. Because in the chipset, in different stages
61 * of operation, those addresses will move around, an address translation
62 * mechanism is required.
64 * There are four partitions (three memory and one register partition),
65 * which are mapped to two different areas of the hardware memory.
71 * ...+----+--> mem.start
72 * Physical address ... | |
73 * space ... | | [PART_0]
75 * 00000000 <--+----+... ...+----+--> mem.start + mem.size
79 * mem.size <--+----+... | | {unused area)
82 * mem.size | | ... | |
83 * + <--+----+... ...+----+--> reg.start
84 * reg.size | | ... | |
85 * |MEM2| ... | | [PART_1]
87 * ...+----+--> reg.start + reg.size
91 int wl1271_set_partition(struct wl1271
*wl
,
92 struct wl1271_partition_set
*p
)
94 /* copy partition info */
95 memcpy(&wl
->part
, p
, sizeof(*p
));
97 wl1271_debug(DEBUG_SPI
, "mem_start %08X mem_size %08X",
98 p
->mem
.start
, p
->mem
.size
);
99 wl1271_debug(DEBUG_SPI
, "reg_start %08X reg_size %08X",
100 p
->reg
.start
, p
->reg
.size
);
101 wl1271_debug(DEBUG_SPI
, "mem2_start %08X mem2_size %08X",
102 p
->mem2
.start
, p
->mem2
.size
);
103 wl1271_debug(DEBUG_SPI
, "mem3_start %08X mem3_size %08X",
104 p
->mem3
.start
, p
->mem3
.size
);
106 /* write partition info to the chipset */
107 wl1271_raw_write32(wl
, HW_PART0_START_ADDR
, p
->mem
.start
);
108 wl1271_raw_write32(wl
, HW_PART0_SIZE_ADDR
, p
->mem
.size
);
109 wl1271_raw_write32(wl
, HW_PART1_START_ADDR
, p
->reg
.start
);
110 wl1271_raw_write32(wl
, HW_PART1_SIZE_ADDR
, p
->reg
.size
);
111 wl1271_raw_write32(wl
, HW_PART2_START_ADDR
, p
->mem2
.start
);
112 wl1271_raw_write32(wl
, HW_PART2_SIZE_ADDR
, p
->mem2
.size
);
113 wl1271_raw_write32(wl
, HW_PART3_START_ADDR
, p
->mem3
.start
);
118 void wl1271_io_reset(struct wl1271
*wl
)
120 wl1271_spi_reset(wl
);
123 void wl1271_io_init(struct wl1271
*wl
)
128 void wl1271_raw_write(struct wl1271
*wl
, int addr
, void *buf
,
129 size_t len
, bool fixed
)
131 wl1271_spi_raw_write(wl
, addr
, buf
, len
, fixed
);
134 void wl1271_raw_read(struct wl1271
*wl
, int addr
, void *buf
,
135 size_t len
, bool fixed
)
137 wl1271_spi_raw_read(wl
, addr
, buf
, len
, fixed
);
140 void wl1271_read(struct wl1271
*wl
, int addr
, void *buf
, size_t len
,
145 physical
= wl1271_translate_addr(wl
, addr
);
147 wl1271_spi_raw_read(wl
, physical
, buf
, len
, fixed
);
150 void wl1271_write(struct wl1271
*wl
, int addr
, void *buf
, size_t len
,
155 physical
= wl1271_translate_addr(wl
, addr
);
157 wl1271_spi_raw_write(wl
, physical
, buf
, len
, fixed
);
160 u32
wl1271_read32(struct wl1271
*wl
, int addr
)
162 return wl1271_raw_read32(wl
, wl1271_translate_addr(wl
, addr
));
165 void wl1271_write32(struct wl1271
*wl
, int addr
, u32 val
)
167 wl1271_raw_write32(wl
, wl1271_translate_addr(wl
, addr
), val
);
170 void wl1271_top_reg_write(struct wl1271
*wl
, int addr
, u16 val
)
172 /* write address >> 1 + 0x30000 to OCP_POR_CTR */
173 addr
= (addr
>> 1) + 0x30000;
174 wl1271_write32(wl
, OCP_POR_CTR
, addr
);
176 /* write value to OCP_POR_WDATA */
177 wl1271_write32(wl
, OCP_DATA_WRITE
, val
);
179 /* write 1 to OCP_CMD */
180 wl1271_write32(wl
, OCP_CMD
, OCP_CMD_WRITE
);
183 u16
wl1271_top_reg_read(struct wl1271
*wl
, int addr
)
186 int timeout
= OCP_CMD_LOOP
;
188 /* write address >> 1 + 0x30000 to OCP_POR_CTR */
189 addr
= (addr
>> 1) + 0x30000;
190 wl1271_write32(wl
, OCP_POR_CTR
, addr
);
192 /* write 2 to OCP_CMD */
193 wl1271_write32(wl
, OCP_CMD
, OCP_CMD_READ
);
195 /* poll for data ready */
197 val
= wl1271_read32(wl
, OCP_DATA_READ
);
198 } while (!(val
& OCP_READY_MASK
) && --timeout
);
201 wl1271_warning("Top register access timed out.");
205 /* check data status and return if OK */
206 if ((val
& OCP_STATUS_MASK
) == OCP_STATUS_OK
)
209 wl1271_warning("Top register access returned error.");