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
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.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
)
47 struct spi_transfer t
;
50 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
52 wl1271_error("could not allocate cmd for spi reset");
56 memset(&t
, 0, sizeof(t
));
59 memset(cmd
, 0xff, WSPI_INIT_CMD_LEN
);
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
;
76 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
78 wl1271_error("could not allocate cmd for spi init");
82 memset(crc
, 0, sizeof(crc
));
83 memset(&t
, 0, sizeof(t
));
87 * Set WSPI_INIT_COMMAND
88 * the data is being send from the MSB to LSB
92 cmd
[1] = WSPI_INIT_CMD_START
| WSPI_INIT_CMD_TX
;
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
;
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
;
112 cmd
[4] |= crc7(0, crc
, WSPI_INIT_CMD_CRC_LEN
) << 1;
113 cmd
[4] |= WSPI_INIT_CMD_END
;
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
137 * ...+----+--> mem_start
138 * VIRTUAL address ... | |
139 * space ... | | [PART_0]
141 * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
145 * part_size <--+----+... | | {unused area)
148 * part_size | | ... | |
149 * + <--+----+... ...+----+--> reg_start
153 * ...+----+--> reg_start + reg_size
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
;
168 cmd_len
= sizeof(u32
) + 2 * sizeof(struct wl1271_partition
);
169 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
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
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
;
238 spi_message_add_tail(&t
, &m
);
240 spi_sync(wl
->spi
, &m
);
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
;
255 cmd
= &wl
->buffer_cmd
;
256 busy_buf
= wl
->buffer_busyword
;
259 *cmd
|= WSPI_CMD_READ
;
260 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
261 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
264 *cmd
|= WSPI_CMD_FIXED
;
266 spi_message_init(&m
);
267 memset(t
, 0, sizeof(t
));
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
);
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
;
297 cmd
= &wl
->buffer_cmd
;
300 *cmd
|= WSPI_CMD_WRITE
;
301 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
302 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
305 *cmd
|= WSPI_CMD_FIXED
;
307 spi_message_init(&m
);
308 memset(t
, 0, sizeof(t
));
311 t
[0].len
= sizeof(*cmd
);
312 spi_message_add_tail(&t
[0], &m
);
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
,
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
,
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
,
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
,
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
);