Save sram context after changing MPU, DSP or core clocks
[linux-ginger.git] / drivers / net / wireless / wl12xx / wl1271_spi.c
blob4a12880c16a8b83a9d9acc7d950fa7d95154fc3a
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 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
20 * 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
29 #include "wl1271.h"
30 #include "wl12xx_80211.h"
31 #include "wl1271_spi.h"
33 static int wl1271_translate_reg_addr(struct wl1271 *wl, int addr)
35 return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
38 static int wl1271_translate_mem_addr(struct wl1271 *wl, int addr)
40 return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
44 void wl1271_spi_reset(struct wl1271 *wl)
46 u8 *cmd;
47 struct spi_transfer t;
48 struct spi_message m;
50 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
51 if (!cmd) {
52 wl1271_error("could not allocate cmd for spi reset");
53 return;
56 memset(&t, 0, sizeof(t));
57 spi_message_init(&m);
59 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
61 t.tx_buf = cmd;
62 t.len = WSPI_INIT_CMD_LEN;
63 spi_message_add_tail(&t, &m);
65 spi_sync(wl->spi, &m);
67 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
70 void wl1271_spi_init(struct wl1271 *wl)
72 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
73 struct spi_transfer t;
74 struct spi_message m;
76 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
77 if (!cmd) {
78 wl1271_error("could not allocate cmd for spi init");
79 return;
82 memset(crc, 0, sizeof(crc));
83 memset(&t, 0, sizeof(t));
84 spi_message_init(&m);
87 * Set WSPI_INIT_COMMAND
88 * the data is being send from the MSB to LSB
90 cmd[2] = 0xff;
91 cmd[3] = 0xff;
92 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
93 cmd[0] = 0;
94 cmd[7] = 0;
95 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
96 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
98 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
99 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
100 else
101 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
103 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
104 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
106 crc[0] = cmd[1];
107 crc[1] = cmd[0];
108 crc[2] = cmd[7];
109 crc[3] = cmd[6];
110 crc[4] = cmd[5];
112 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
113 cmd[4] |= WSPI_INIT_CMD_END;
115 t.tx_buf = cmd;
116 t.len = WSPI_INIT_CMD_LEN;
117 spi_message_add_tail(&t, &m);
119 spi_sync(wl->spi, &m);
121 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
124 /* Set the SPI partitions to access the chip addresses
126 * There are two VIRTUAL (SPI) partitions (the memory partition and the
127 * registers partition), which are mapped to two different areas of the
128 * PHYSICAL (hardware) memory. This function also makes other checks to
129 * ensure that the partitions are not overlapping. In the diagram below, the
130 * memory partition comes before the register partition, but the opposite is
131 * also supported.
133 * PHYSICAL address
134 * space
136 * | |
137 * ...+----+--> mem_start
138 * VIRTUAL address ... | |
139 * space ... | | [PART_0]
140 * ... | |
141 * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
142 * | | ... | |
143 * |MEM | ... | |
144 * | | ... | |
145 * part_size <--+----+... | | {unused area)
146 * | | ... | |
147 * |REG | ... | |
148 * part_size | | ... | |
149 * + <--+----+... ...+----+--> reg_start
150 * reg_size ... | |
151 * ... | | [PART_1]
152 * ... | |
153 * ...+----+--> reg_start + reg_size
154 * | |
157 int wl1271_set_partition(struct wl1271 *wl,
158 u32 mem_start, u32 mem_size,
159 u32 reg_start, u32 reg_size)
161 struct wl1271_partition *partition;
162 struct spi_transfer t;
163 struct spi_message m;
164 size_t len, cmd_len;
165 u32 *cmd;
166 int addr;
168 cmd_len = sizeof(u32) + 2 * sizeof(struct wl1271_partition);
169 cmd = kzalloc(cmd_len, GFP_KERNEL);
170 if (!cmd)
171 return -ENOMEM;
173 spi_message_init(&m);
174 memset(&t, 0, sizeof(t));
176 partition = (struct wl1271_partition *) (cmd + 1);
177 addr = HW_ACCESS_PART0_SIZE_ADDR;
178 len = 2 * sizeof(struct wl1271_partition);
180 *cmd |= WSPI_CMD_WRITE;
181 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
182 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
184 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
185 mem_start, mem_size);
186 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
187 reg_start, reg_size);
189 /* Make sure that the two partitions together don't exceed the
190 * address range */
191 if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
192 wl1271_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
193 " address range. Truncating partition[0].");
194 mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
195 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
196 mem_start, mem_size);
197 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
198 reg_start, reg_size);
201 if ((mem_start < reg_start) &&
202 ((mem_start + mem_size) > reg_start)) {
203 /* Guarantee that the memory partition doesn't overlap the
204 * registers partition */
205 wl1271_debug(DEBUG_SPI, "End of partition[0] is "
206 "overlapping partition[1]. Adjusted.");
207 mem_size = reg_start - mem_start;
208 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
209 mem_start, mem_size);
210 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
211 reg_start, reg_size);
212 } else if ((reg_start < mem_start) &&
213 ((reg_start + reg_size) > mem_start)) {
214 /* Guarantee that the register partition doesn't overlap the
215 * memory partition */
216 wl1271_debug(DEBUG_SPI, "End of partition[1] is"
217 " overlapping partition[0]. Adjusted.");
218 reg_size = mem_start - reg_start;
219 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
220 mem_start, mem_size);
221 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
222 reg_start, reg_size);
225 partition[0].start = mem_start;
226 partition[0].size = mem_size;
227 partition[1].start = reg_start;
228 partition[1].size = reg_size;
230 wl->physical_mem_addr = mem_start;
231 wl->physical_reg_addr = reg_start;
233 wl->virtual_mem_addr = 0;
234 wl->virtual_reg_addr = mem_size;
236 t.tx_buf = cmd;
237 t.len = cmd_len;
238 spi_message_add_tail(&t, &m);
240 spi_sync(wl->spi, &m);
242 kfree(cmd);
244 return 0;
247 void wl1271_spi_read(struct wl1271 *wl, int addr, void *buf,
248 size_t len, bool fixed)
250 struct spi_transfer t[3];
251 struct spi_message m;
252 u8 *busy_buf;
253 u32 *cmd;
255 cmd = &wl->buffer_cmd;
256 busy_buf = wl->buffer_busyword;
258 *cmd = 0;
259 *cmd |= WSPI_CMD_READ;
260 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
261 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
263 if (fixed)
264 *cmd |= WSPI_CMD_FIXED;
266 spi_message_init(&m);
267 memset(t, 0, sizeof(t));
269 t[0].tx_buf = cmd;
270 t[0].len = 4;
271 spi_message_add_tail(&t[0], &m);
273 /* Busy and non busy words read */
274 t[1].rx_buf = busy_buf;
275 t[1].len = WL1271_BUSY_WORD_LEN;
276 spi_message_add_tail(&t[1], &m);
278 t[2].rx_buf = buf;
279 t[2].len = len;
280 spi_message_add_tail(&t[2], &m);
282 spi_sync(wl->spi, &m);
284 /* FIXME: check busy words */
286 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
287 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
290 void wl1271_spi_write(struct wl1271 *wl, int addr, void *buf,
291 size_t len, bool fixed)
293 struct spi_transfer t[2];
294 struct spi_message m;
295 u32 *cmd;
297 cmd = &wl->buffer_cmd;
299 *cmd = 0;
300 *cmd |= WSPI_CMD_WRITE;
301 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
302 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
304 if (fixed)
305 *cmd |= WSPI_CMD_FIXED;
307 spi_message_init(&m);
308 memset(t, 0, sizeof(t));
310 t[0].tx_buf = cmd;
311 t[0].len = sizeof(*cmd);
312 spi_message_add_tail(&t[0], &m);
314 t[1].tx_buf = buf;
315 t[1].len = len;
316 spi_message_add_tail(&t[1], &m);
318 spi_sync(wl->spi, &m);
320 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
321 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
324 void wl1271_spi_mem_read(struct wl1271 *wl, int addr, void *buf,
325 size_t len)
327 int physical;
329 physical = wl1271_translate_mem_addr(wl, addr);
331 wl1271_spi_read(wl, physical, buf, len, false);
334 void wl1271_spi_mem_write(struct wl1271 *wl, int addr, void *buf,
335 size_t len)
337 int physical;
339 physical = wl1271_translate_mem_addr(wl, addr);
341 wl1271_spi_write(wl, physical, buf, len, false);
344 void wl1271_spi_reg_read(struct wl1271 *wl, int addr, void *buf, size_t len,
345 bool fixed)
347 int physical;
349 physical = wl1271_translate_reg_addr(wl, addr);
351 wl1271_spi_read(wl, physical, buf, len, fixed);
354 void wl1271_spi_reg_write(struct wl1271 *wl, int addr, void *buf, size_t len,
355 bool fixed)
357 int physical;
359 physical = wl1271_translate_reg_addr(wl, addr);
361 wl1271_spi_write(wl, physical, buf, len, fixed);
364 u32 wl1271_mem_read32(struct wl1271 *wl, int addr)
366 return wl1271_read32(wl, wl1271_translate_mem_addr(wl, addr));
369 void wl1271_mem_write32(struct wl1271 *wl, int addr, u32 val)
371 wl1271_write32(wl, wl1271_translate_mem_addr(wl, addr), val);
374 u32 wl1271_reg_read32(struct wl1271 *wl, int addr)
376 return wl1271_read32(wl, wl1271_translate_reg_addr(wl, addr));
379 void wl1271_reg_write32(struct wl1271 *wl, int addr, u32 val)
381 wl1271_write32(wl, wl1271_translate_reg_addr(wl, addr), val);