]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/wl12xx/wl1251_spi.c
wl1251: separate bus i/o code into io.c
[net-next-2.6.git] / drivers / net / wireless / wl12xx / wl1251_spi.c
CommitLineData
2f01a1f5 1/*
80301cdc 2 * This file is part of wl1251
2f01a1f5
KV
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 <linux/module.h>
25#include <linux/crc7.h>
26#include <linux/spi/spi.h>
27
13674118 28#include "wl1251.h"
2f01a1f5 29#include "reg.h"
ef2f8d45 30#include "wl1251_spi.h"
2f01a1f5 31
80301cdc 32void wl1251_spi_reset(struct wl1251 *wl)
2f01a1f5
KV
33{
34 u8 *cmd;
35 struct spi_transfer t;
36 struct spi_message m;
37
38 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
39 if (!cmd) {
80301cdc 40 wl1251_error("could not allocate cmd for spi reset");
2f01a1f5
KV
41 return;
42 }
43
44 memset(&t, 0, sizeof(t));
45 spi_message_init(&m);
46
47 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
48
49 t.tx_buf = cmd;
50 t.len = WSPI_INIT_CMD_LEN;
51 spi_message_add_tail(&t, &m);
52
53 spi_sync(wl->spi, &m);
54
80301cdc 55 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
2f01a1f5
KV
56}
57
80301cdc 58void wl1251_spi_init(struct wl1251 *wl)
2f01a1f5
KV
59{
60 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
61 struct spi_transfer t;
62 struct spi_message m;
63
64 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
65 if (!cmd) {
80301cdc 66 wl1251_error("could not allocate cmd for spi init");
2f01a1f5
KV
67 return;
68 }
69
70 memset(crc, 0, sizeof(crc));
71 memset(&t, 0, sizeof(t));
72 spi_message_init(&m);
73
74 /*
75 * Set WSPI_INIT_COMMAND
76 * the data is being send from the MSB to LSB
77 */
78 cmd[2] = 0xff;
79 cmd[3] = 0xff;
80 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
81 cmd[0] = 0;
82 cmd[7] = 0;
83 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
84 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
85
86 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
87 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
88 else
89 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
90
91 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
92 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
93
94 crc[0] = cmd[1];
95 crc[1] = cmd[0];
96 crc[2] = cmd[7];
97 crc[3] = cmd[6];
98 crc[4] = cmd[5];
99
100 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
101 cmd[4] |= WSPI_INIT_CMD_END;
102
103 t.tx_buf = cmd;
104 t.len = WSPI_INIT_CMD_LEN;
105 spi_message_add_tail(&t, &m);
106
107 spi_sync(wl->spi, &m);
108
80301cdc 109 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
2f01a1f5
KV
110}
111
112/* Set the SPI partitions to access the chip addresses
113 *
114 * There are two VIRTUAL (SPI) partitions (the memory partition and the
115 * registers partition), which are mapped to two different areas of the
116 * PHYSICAL (hardware) memory. This function also makes other checks to
117 * ensure that the partitions are not overlapping. In the diagram below, the
118 * memory partition comes before the register partition, but the opposite is
119 * also supported.
120 *
121 * PHYSICAL address
122 * space
123 *
124 * | |
125 * ...+----+--> mem_start
126 * VIRTUAL address ... | |
127 * space ... | | [PART_0]
128 * ... | |
129 * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
130 * | | ... | |
131 * |MEM | ... | |
132 * | | ... | |
133 * part_size <--+----+... | | {unused area)
134 * | | ... | |
135 * |REG | ... | |
136 * part_size | | ... | |
137 * + <--+----+... ...+----+--> reg_start
138 * reg_size ... | |
139 * ... | | [PART_1]
140 * ... | |
141 * ...+----+--> reg_start + reg_size
142 * | |
143 *
144 */
80301cdc 145int wl1251_set_partition(struct wl1251 *wl,
2f01a1f5
KV
146 u32 mem_start, u32 mem_size,
147 u32 reg_start, u32 reg_size)
148{
80301cdc 149 struct wl1251_partition *partition;
2f01a1f5
KV
150 struct spi_transfer t;
151 struct spi_message m;
8d47cdb6 152 size_t len, cmd_len;
2f01a1f5 153 u32 *cmd;
2f01a1f5
KV
154 int addr;
155
80301cdc 156 cmd_len = sizeof(u32) + 2 * sizeof(struct wl1251_partition);
8d47cdb6
KV
157 cmd = kzalloc(cmd_len, GFP_KERNEL);
158 if (!cmd)
159 return -ENOMEM;
160
2f01a1f5
KV
161 spi_message_init(&m);
162 memset(&t, 0, sizeof(t));
2f01a1f5 163
80301cdc 164 partition = (struct wl1251_partition *) (cmd + 1);
2f01a1f5 165 addr = HW_ACCESS_PART0_SIZE_ADDR;
80301cdc 166 len = 2 * sizeof(struct wl1251_partition);
2f01a1f5
KV
167
168 *cmd |= WSPI_CMD_WRITE;
169 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
170 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
171
80301cdc 172 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
2f01a1f5 173 mem_start, mem_size);
80301cdc 174 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
2f01a1f5
KV
175 reg_start, reg_size);
176
177 /* Make sure that the two partitions together don't exceed the
178 * address range */
179 if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
80301cdc 180 wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
2f01a1f5
KV
181 " address range. Truncating partition[0].");
182 mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
80301cdc 183 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
2f01a1f5 184 mem_start, mem_size);
80301cdc 185 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
2f01a1f5
KV
186 reg_start, reg_size);
187 }
188
189 if ((mem_start < reg_start) &&
190 ((mem_start + mem_size) > reg_start)) {
191 /* Guarantee that the memory partition doesn't overlap the
192 * registers partition */
80301cdc 193 wl1251_debug(DEBUG_SPI, "End of partition[0] is "
2f01a1f5
KV
194 "overlapping partition[1]. Adjusted.");
195 mem_size = reg_start - mem_start;
80301cdc 196 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
2f01a1f5 197 mem_start, mem_size);
80301cdc 198 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
2f01a1f5
KV
199 reg_start, reg_size);
200 } else if ((reg_start < mem_start) &&
201 ((reg_start + reg_size) > mem_start)) {
202 /* Guarantee that the register partition doesn't overlap the
203 * memory partition */
80301cdc 204 wl1251_debug(DEBUG_SPI, "End of partition[1] is"
2f01a1f5
KV
205 " overlapping partition[0]. Adjusted.");
206 reg_size = mem_start - reg_start;
80301cdc 207 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
2f01a1f5 208 mem_start, mem_size);
80301cdc 209 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
2f01a1f5
KV
210 reg_start, reg_size);
211 }
212
213 partition[0].start = mem_start;
214 partition[0].size = mem_size;
215 partition[1].start = reg_start;
216 partition[1].size = reg_size;
217
218 wl->physical_mem_addr = mem_start;
219 wl->physical_reg_addr = reg_start;
220
221 wl->virtual_mem_addr = 0;
222 wl->virtual_reg_addr = mem_size;
223
8d47cdb6
KV
224 t.tx_buf = cmd;
225 t.len = cmd_len;
2f01a1f5
KV
226 spi_message_add_tail(&t, &m);
227
228 spi_sync(wl->spi, &m);
8d47cdb6
KV
229
230 kfree(cmd);
231
232 return 0;
2f01a1f5
KV
233}
234
b8010790 235void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, size_t len)
2f01a1f5
KV
236{
237 struct spi_transfer t[3];
238 struct spi_message m;
5262c12d 239 u8 *busy_buf;
56343a3c 240 u32 *cmd;
2f01a1f5 241
56343a3c 242 cmd = &wl->buffer_cmd;
5262c12d 243 busy_buf = wl->buffer_busyword;
56343a3c
KV
244
245 *cmd = 0;
246 *cmd |= WSPI_CMD_READ;
247 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
248 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
2f01a1f5
KV
249
250 spi_message_init(&m);
251 memset(t, 0, sizeof(t));
252
56343a3c 253 t[0].tx_buf = cmd;
2f01a1f5
KV
254 t[0].len = 4;
255 spi_message_add_tail(&t[0], &m);
256
257 /* Busy and non busy words read */
258 t[1].rx_buf = busy_buf;
80301cdc 259 t[1].len = WL1251_BUSY_WORD_LEN;
2f01a1f5
KV
260 spi_message_add_tail(&t[1], &m);
261
262 t[2].rx_buf = buf;
263 t[2].len = len;
264 spi_message_add_tail(&t[2], &m);
265
266 spi_sync(wl->spi, &m);
267
268 /* FIXME: check busy words */
269
80301cdc
KV
270 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
271 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
2f01a1f5
KV
272}
273
b8010790 274void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, size_t len)
2f01a1f5
KV
275{
276 struct spi_transfer t[2];
277 struct spi_message m;
56343a3c 278 u32 *cmd;
2f01a1f5 279
56343a3c
KV
280 cmd = &wl->buffer_cmd;
281
282 *cmd = 0;
283 *cmd |= WSPI_CMD_WRITE;
284 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
285 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
2f01a1f5
KV
286
287 spi_message_init(&m);
288 memset(t, 0, sizeof(t));
289
56343a3c
KV
290 t[0].tx_buf = cmd;
291 t[0].len = sizeof(*cmd);
2f01a1f5
KV
292 spi_message_add_tail(&t[0], &m);
293
294 t[1].tx_buf = buf;
295 t[1].len = len;
296 spi_message_add_tail(&t[1], &m);
297
298 spi_sync(wl->spi, &m);
299
80301cdc
KV
300 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
301 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
2f01a1f5 302}