blob: 61a0852b4ee7b08719a46b7b6c030b7e8d60d4b7 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
Kalle Valo80301cd2009-06-12 14:17:39 +03002 * This file is part of wl1251
Kalle Valo2f01a1f2009-04-29 23:33:31 +03003 *
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
Kalle Valo13674112009-06-12 14:17:25 +030028#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030029#include "reg.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030030#include "wl1251_spi.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030031
Bob Copeland08d9f5722009-08-07 13:33:11 +030032static void wl1251_spi_reset(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033{
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) {
Kalle Valo80301cd2009-06-12 14:17:39 +030040 wl1251_error("could not allocate cmd for spi reset");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030041 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
Kalle Valo80301cd2009-06-12 14:17:39 +030055 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030056}
57
Bob Copeland08d9f5722009-08-07 13:33:11 +030058static void wl1251_spi_init(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030059{
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) {
Kalle Valo80301cd2009-06-12 14:17:39 +030066 wl1251_error("could not allocate cmd for spi init");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030067 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
Kalle Valo80301cd2009-06-12 14:17:39 +0300109 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300110}
111
Bob Copeland08d9f5722009-08-07 13:33:11 +0300112static void wl1251_spi_reset_wake(struct wl1251 *wl)
113{
114 wl1251_spi_reset(wl);
115 wl1251_spi_init(wl);
116}
117
118
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300119/* Set the SPI partitions to access the chip addresses
120 *
121 * There are two VIRTUAL (SPI) partitions (the memory partition and the
122 * registers partition), which are mapped to two different areas of the
123 * PHYSICAL (hardware) memory. This function also makes other checks to
124 * ensure that the partitions are not overlapping. In the diagram below, the
125 * memory partition comes before the register partition, but the opposite is
126 * also supported.
127 *
128 * PHYSICAL address
129 * space
130 *
131 * | |
132 * ...+----+--> mem_start
133 * VIRTUAL address ... | |
134 * space ... | | [PART_0]
135 * ... | |
136 * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
137 * | | ... | |
138 * |MEM | ... | |
139 * | | ... | |
140 * part_size <--+----+... | | {unused area)
141 * | | ... | |
142 * |REG | ... | |
143 * part_size | | ... | |
144 * + <--+----+... ...+----+--> reg_start
145 * reg_size ... | |
146 * ... | | [PART_1]
147 * ... | |
148 * ...+----+--> reg_start + reg_size
149 * | |
150 *
151 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300152int wl1251_set_partition(struct wl1251 *wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300153 u32 mem_start, u32 mem_size,
154 u32 reg_start, u32 reg_size)
155{
Kalle Valo80301cd2009-06-12 14:17:39 +0300156 struct wl1251_partition *partition;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300157 struct spi_transfer t;
158 struct spi_message m;
Kalle Valo8d47cdb2009-06-12 14:14:41 +0300159 size_t len, cmd_len;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300160 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300161 int addr;
162
Kalle Valo80301cd2009-06-12 14:17:39 +0300163 cmd_len = sizeof(u32) + 2 * sizeof(struct wl1251_partition);
Kalle Valo8d47cdb2009-06-12 14:14:41 +0300164 cmd = kzalloc(cmd_len, GFP_KERNEL);
165 if (!cmd)
166 return -ENOMEM;
167
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300168 spi_message_init(&m);
169 memset(&t, 0, sizeof(t));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300170
Kalle Valo80301cd2009-06-12 14:17:39 +0300171 partition = (struct wl1251_partition *) (cmd + 1);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300172 addr = HW_ACCESS_PART0_SIZE_ADDR;
Kalle Valo80301cd2009-06-12 14:17:39 +0300173 len = 2 * sizeof(struct wl1251_partition);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300174
175 *cmd |= WSPI_CMD_WRITE;
176 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
177 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
178
Kalle Valo80301cd2009-06-12 14:17:39 +0300179 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300180 mem_start, mem_size);
Kalle Valo80301cd2009-06-12 14:17:39 +0300181 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300182 reg_start, reg_size);
183
184 /* Make sure that the two partitions together don't exceed the
185 * address range */
186 if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300187 wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300188 " address range. Truncating partition[0].");
189 mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
Kalle Valo80301cd2009-06-12 14:17:39 +0300190 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300191 mem_start, mem_size);
Kalle Valo80301cd2009-06-12 14:17:39 +0300192 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300193 reg_start, reg_size);
194 }
195
196 if ((mem_start < reg_start) &&
197 ((mem_start + mem_size) > reg_start)) {
198 /* Guarantee that the memory partition doesn't overlap the
199 * registers partition */
Kalle Valo80301cd2009-06-12 14:17:39 +0300200 wl1251_debug(DEBUG_SPI, "End of partition[0] is "
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300201 "overlapping partition[1]. Adjusted.");
202 mem_size = reg_start - mem_start;
Kalle Valo80301cd2009-06-12 14:17:39 +0300203 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300204 mem_start, mem_size);
Kalle Valo80301cd2009-06-12 14:17:39 +0300205 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300206 reg_start, reg_size);
207 } else if ((reg_start < mem_start) &&
208 ((reg_start + reg_size) > mem_start)) {
209 /* Guarantee that the register partition doesn't overlap the
210 * memory partition */
Kalle Valo80301cd2009-06-12 14:17:39 +0300211 wl1251_debug(DEBUG_SPI, "End of partition[1] is"
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300212 " overlapping partition[0]. Adjusted.");
213 reg_size = mem_start - reg_start;
Kalle Valo80301cd2009-06-12 14:17:39 +0300214 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300215 mem_start, mem_size);
Kalle Valo80301cd2009-06-12 14:17:39 +0300216 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300217 reg_start, reg_size);
218 }
219
220 partition[0].start = mem_start;
221 partition[0].size = mem_size;
222 partition[1].start = reg_start;
223 partition[1].size = reg_size;
224
225 wl->physical_mem_addr = mem_start;
226 wl->physical_reg_addr = reg_start;
227
228 wl->virtual_mem_addr = 0;
229 wl->virtual_reg_addr = mem_size;
230
Kalle Valo8d47cdb2009-06-12 14:14:41 +0300231 t.tx_buf = cmd;
232 t.len = cmd_len;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300233 spi_message_add_tail(&t, &m);
234
235 spi_sync(wl->spi, &m);
Kalle Valo8d47cdb2009-06-12 14:14:41 +0300236
237 kfree(cmd);
238
239 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300240}
241
Bob Copeland08d9f5722009-08-07 13:33:11 +0300242static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
243 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300244{
245 struct spi_transfer t[3];
246 struct spi_message m;
Kalle Valo5262c122009-06-12 14:14:55 +0300247 u8 *busy_buf;
Kalle Valo56343a32009-06-12 14:14:47 +0300248 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300249
Kalle Valo56343a32009-06-12 14:14:47 +0300250 cmd = &wl->buffer_cmd;
Kalle Valo5262c122009-06-12 14:14:55 +0300251 busy_buf = wl->buffer_busyword;
Kalle Valo56343a32009-06-12 14:14:47 +0300252
253 *cmd = 0;
254 *cmd |= WSPI_CMD_READ;
255 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
256 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300257
258 spi_message_init(&m);
259 memset(t, 0, sizeof(t));
260
Kalle Valo56343a32009-06-12 14:14:47 +0300261 t[0].tx_buf = cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300262 t[0].len = 4;
263 spi_message_add_tail(&t[0], &m);
264
265 /* Busy and non busy words read */
266 t[1].rx_buf = busy_buf;
Kalle Valo80301cd2009-06-12 14:17:39 +0300267 t[1].len = WL1251_BUSY_WORD_LEN;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300268 spi_message_add_tail(&t[1], &m);
269
270 t[2].rx_buf = buf;
271 t[2].len = len;
272 spi_message_add_tail(&t[2], &m);
273
274 spi_sync(wl->spi, &m);
275
276 /* FIXME: check busy words */
277
Kalle Valo80301cd2009-06-12 14:17:39 +0300278 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
279 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300280}
281
Bob Copeland08d9f5722009-08-07 13:33:11 +0300282static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
283 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300284{
285 struct spi_transfer t[2];
286 struct spi_message m;
Kalle Valo56343a32009-06-12 14:14:47 +0300287 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300288
Kalle Valo56343a32009-06-12 14:14:47 +0300289 cmd = &wl->buffer_cmd;
290
291 *cmd = 0;
292 *cmd |= WSPI_CMD_WRITE;
293 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
294 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300295
296 spi_message_init(&m);
297 memset(t, 0, sizeof(t));
298
Kalle Valo56343a32009-06-12 14:14:47 +0300299 t[0].tx_buf = cmd;
300 t[0].len = sizeof(*cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300301 spi_message_add_tail(&t[0], &m);
302
303 t[1].tx_buf = buf;
304 t[1].len = len;
305 spi_message_add_tail(&t[1], &m);
306
307 spi_sync(wl->spi, &m);
308
Kalle Valo80301cd2009-06-12 14:17:39 +0300309 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
310 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300311}
Bob Copeland08d9f5722009-08-07 13:33:11 +0300312
313const struct wl1251_if_operations wl1251_spi_ops = {
314 .read = wl1251_spi_read,
315 .write = wl1251_spi_write,
316 .reset = wl1251_spi_reset_wake,
317};