blob: 031a3bac95e561eb44d3b6c2429b86b9d0940c72 [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
Kalle Valo80301cd2009-06-12 14:17:39 +030032void 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
Kalle Valo80301cd2009-06-12 14:17:39 +030058void 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
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 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300145int wl1251_set_partition(struct wl1251 *wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300146 u32 mem_start, u32 mem_size,
147 u32 reg_start, u32 reg_size)
148{
Kalle Valo80301cd2009-06-12 14:17:39 +0300149 struct wl1251_partition *partition;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300150 struct spi_transfer t;
151 struct spi_message m;
Kalle Valo8d47cdb2009-06-12 14:14:41 +0300152 size_t len, cmd_len;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300153 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300154 int addr;
155
Kalle Valo80301cd2009-06-12 14:17:39 +0300156 cmd_len = sizeof(u32) + 2 * sizeof(struct wl1251_partition);
Kalle Valo8d47cdb2009-06-12 14:14:41 +0300157 cmd = kzalloc(cmd_len, GFP_KERNEL);
158 if (!cmd)
159 return -ENOMEM;
160
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300161 spi_message_init(&m);
162 memset(&t, 0, sizeof(t));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300163
Kalle Valo80301cd2009-06-12 14:17:39 +0300164 partition = (struct wl1251_partition *) (cmd + 1);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300165 addr = HW_ACCESS_PART0_SIZE_ADDR;
Kalle Valo80301cd2009-06-12 14:17:39 +0300166 len = 2 * sizeof(struct wl1251_partition);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300167
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
Kalle Valo80301cd2009-06-12 14:17:39 +0300172 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300173 mem_start, mem_size);
Kalle Valo80301cd2009-06-12 14:17:39 +0300174 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300175 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) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300180 wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300181 " address range. Truncating partition[0].");
182 mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
Kalle Valo80301cd2009-06-12 14:17:39 +0300183 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300184 mem_start, mem_size);
Kalle Valo80301cd2009-06-12 14:17:39 +0300185 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300186 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 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300193 wl1251_debug(DEBUG_SPI, "End of partition[0] is "
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300194 "overlapping partition[1]. Adjusted.");
195 mem_size = reg_start - mem_start;
Kalle Valo80301cd2009-06-12 14:17:39 +0300196 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300197 mem_start, mem_size);
Kalle Valo80301cd2009-06-12 14:17:39 +0300198 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300199 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 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300204 wl1251_debug(DEBUG_SPI, "End of partition[1] is"
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300205 " overlapping partition[0]. Adjusted.");
206 reg_size = mem_start - reg_start;
Kalle Valo80301cd2009-06-12 14:17:39 +0300207 wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300208 mem_start, mem_size);
Kalle Valo80301cd2009-06-12 14:17:39 +0300209 wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300210 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
Kalle Valo8d47cdb2009-06-12 14:14:41 +0300224 t.tx_buf = cmd;
225 t.len = cmd_len;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300226 spi_message_add_tail(&t, &m);
227
228 spi_sync(wl->spi, &m);
Kalle Valo8d47cdb2009-06-12 14:14:41 +0300229
230 kfree(cmd);
231
232 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300233}
234
Kalle Valob8010792009-08-07 13:32:48 +0300235void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300236{
237 struct spi_transfer t[3];
238 struct spi_message m;
Kalle Valo5262c122009-06-12 14:14:55 +0300239 u8 *busy_buf;
Kalle Valo56343a32009-06-12 14:14:47 +0300240 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300241
Kalle Valo56343a32009-06-12 14:14:47 +0300242 cmd = &wl->buffer_cmd;
Kalle Valo5262c122009-06-12 14:14:55 +0300243 busy_buf = wl->buffer_busyword;
Kalle Valo56343a32009-06-12 14:14:47 +0300244
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;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300249
250 spi_message_init(&m);
251 memset(t, 0, sizeof(t));
252
Kalle Valo56343a32009-06-12 14:14:47 +0300253 t[0].tx_buf = cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300254 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;
Kalle Valo80301cd2009-06-12 14:17:39 +0300259 t[1].len = WL1251_BUSY_WORD_LEN;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300260 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
Kalle Valo80301cd2009-06-12 14:17:39 +0300270 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
271 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300272}
273
Kalle Valob8010792009-08-07 13:32:48 +0300274void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300275{
276 struct spi_transfer t[2];
277 struct spi_message m;
Kalle Valo56343a32009-06-12 14:14:47 +0300278 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300279
Kalle Valo56343a32009-06-12 14:14:47 +0300280 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;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300286
287 spi_message_init(&m);
288 memset(t, 0, sizeof(t));
289
Kalle Valo56343a32009-06-12 14:14:47 +0300290 t[0].tx_buf = cmd;
291 t[0].len = sizeof(*cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300292 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
Kalle Valo80301cd2009-06-12 14:17:39 +0300300 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
301 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300302}