blob: db2cfbfb1e40740e3e6fd81d9295a84ea861eb8f [file] [log] [blame]
Bob Copeland0764de62009-08-07 13:32:56 +03001/*
2 * This file is part of wl12xx
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
6 * Contact: Kalle Valo <kalle.valo@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 "wl1251.h"
25#include "reg.h"
26#include "wl1251_io.h"
27
28static int wl1251_translate_reg_addr(struct wl1251 *wl, int addr)
29{
30 /* If the address is lower than REGISTERS_BASE, it means that this is
31 * a chip-specific register address, so look it up in the registers
32 * table */
33 if (addr < REGISTERS_BASE) {
34 /* Make sure we don't go over the table */
35 if (addr >= ACX_REG_TABLE_LEN) {
36 wl1251_error("address out of range (%d)", addr);
37 return -EINVAL;
38 }
39 addr = wl->chip.acx_reg_table[addr];
40 }
41
42 return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
43}
44
45static int wl1251_translate_mem_addr(struct wl1251 *wl, int addr)
46{
47 return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
48}
49
50void wl1251_mem_read(struct wl1251 *wl, int addr, void *buf, size_t len)
51{
52 int physical;
53
54 physical = wl1251_translate_mem_addr(wl, addr);
55
Bob Copeland08d9f5722009-08-07 13:33:11 +030056 wl->if_ops->read(wl, physical, buf, len);
Bob Copeland0764de62009-08-07 13:32:56 +030057}
58
59void wl1251_mem_write(struct wl1251 *wl, int addr, void *buf, size_t len)
60{
61 int physical;
62
63 physical = wl1251_translate_mem_addr(wl, addr);
64
Bob Copeland08d9f5722009-08-07 13:33:11 +030065 wl->if_ops->write(wl, physical, buf, len);
Bob Copeland0764de62009-08-07 13:32:56 +030066}
67
68u32 wl1251_mem_read32(struct wl1251 *wl, int addr)
69{
70 return wl1251_read32(wl, wl1251_translate_mem_addr(wl, addr));
71}
72
73void wl1251_mem_write32(struct wl1251 *wl, int addr, u32 val)
74{
75 wl1251_write32(wl, wl1251_translate_mem_addr(wl, addr), val);
76}
77
78u32 wl1251_reg_read32(struct wl1251 *wl, int addr)
79{
80 return wl1251_read32(wl, wl1251_translate_reg_addr(wl, addr));
81}
82
83void wl1251_reg_write32(struct wl1251 *wl, int addr, u32 val)
84{
85 wl1251_write32(wl, wl1251_translate_reg_addr(wl, addr), val);
86}
Bob Copeland0d77e142009-08-07 13:33:18 +030087
88/* Set the partitions to access the chip addresses.
89 *
90 * There are two VIRTUAL partitions (the memory partition and the
91 * registers partition), which are mapped to two different areas of the
92 * PHYSICAL (hardware) memory. This function also makes other checks to
93 * ensure that the partitions are not overlapping. In the diagram below, the
94 * memory partition comes before the register partition, but the opposite is
95 * also supported.
96 *
97 * PHYSICAL address
98 * space
99 *
100 * | |
101 * ...+----+--> mem_start
102 * VIRTUAL address ... | |
103 * space ... | | [PART_0]
104 * ... | |
105 * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
106 * | | ... | |
107 * |MEM | ... | |
108 * | | ... | |
109 * part_size <--+----+... | | {unused area)
110 * | | ... | |
111 * |REG | ... | |
112 * part_size | | ... | |
113 * + <--+----+... ...+----+--> reg_start
114 * reg_size ... | |
115 * ... | | [PART_1]
116 * ... | |
117 * ...+----+--> reg_start + reg_size
118 * | |
119 *
120 */
121void wl1251_set_partition(struct wl1251 *wl,
122 u32 mem_start, u32 mem_size,
123 u32 reg_start, u32 reg_size)
124{
125 struct wl1251_partition partition[2];
126
127 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
128 mem_start, mem_size);
129 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
130 reg_start, reg_size);
131
132 /* Make sure that the two partitions together don't exceed the
133 * address range */
134 if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
135 wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
136 " address range. Truncating partition[0].");
137 mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
138 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
139 mem_start, mem_size);
140 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
141 reg_start, reg_size);
142 }
143
144 if ((mem_start < reg_start) &&
145 ((mem_start + mem_size) > reg_start)) {
146 /* Guarantee that the memory partition doesn't overlap the
147 * registers partition */
148 wl1251_debug(DEBUG_SPI, "End of partition[0] is "
149 "overlapping partition[1]. Adjusted.");
150 mem_size = reg_start - mem_start;
151 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
152 mem_start, mem_size);
153 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
154 reg_start, reg_size);
155 } else if ((reg_start < mem_start) &&
156 ((reg_start + reg_size) > mem_start)) {
157 /* Guarantee that the register partition doesn't overlap the
158 * memory partition */
159 wl1251_debug(DEBUG_SPI, "End of partition[1] is"
160 " overlapping partition[0]. Adjusted.");
161 reg_size = mem_start - reg_start;
162 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
163 mem_start, mem_size);
164 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
165 reg_start, reg_size);
166 }
167
168 partition[0].start = mem_start;
169 partition[0].size = mem_size;
170 partition[1].start = reg_start;
171 partition[1].size = reg_size;
172
173 wl->physical_mem_addr = mem_start;
174 wl->physical_reg_addr = reg_start;
175
176 wl->virtual_mem_addr = 0;
177 wl->virtual_reg_addr = mem_size;
178
179 wl->if_ops->write(wl, HW_ACCESS_PART0_SIZE_ADDR, partition,
180 sizeof(partition));
181}