blob: d00f7ed7f21c0d8096732b4b1b287c5d1c62d082 [file] [log] [blame]
Teemu Paasikivi521a5b22010-02-18 13:25:54 +02001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2008-2010 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@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/platform_device.h>
26#include <linux/crc7.h>
27#include <linux/spi/spi.h>
28
29#include "wl1271.h"
30#include "wl12xx_80211.h"
31#include "wl1271_spi.h"
32#include "wl1271_io.h"
33
Teemu Paasikivi8197b712010-02-22 08:38:23 +020034struct device *wl1271_wl_to_dev(struct wl1271 *wl)
35{
36 return wl->if_ops->dev(wl);
37}
38
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020039void wl1271_disable_interrupts(struct wl1271 *wl)
40{
Teemu Paasikivi8197b712010-02-22 08:38:23 +020041 wl->if_ops->disable_irq(wl);
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020042}
43
44void wl1271_enable_interrupts(struct wl1271 *wl)
45{
Teemu Paasikivi8197b712010-02-22 08:38:23 +020046 wl->if_ops->enable_irq(wl);
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020047}
48
Teemu Paasikivi521a5b22010-02-18 13:25:54 +020049static int wl1271_translate_addr(struct wl1271 *wl, int addr)
50{
51 /*
52 * To translate, first check to which window of addresses the
53 * particular address belongs. Then subtract the starting address
54 * of that window from the address. Then, add offset of the
55 * translated region.
56 *
57 * The translated regions occur next to each other in physical device
58 * memory, so just add the sizes of the preceeding address regions to
59 * get the offset to the new region.
60 *
61 * Currently, only the two first regions are addressed, and the
62 * assumption is that all addresses will fall into either of those
63 * two.
64 */
65 if ((addr >= wl->part.reg.start) &&
66 (addr < wl->part.reg.start + wl->part.reg.size))
67 return addr - wl->part.reg.start + wl->part.mem.size;
68 else
69 return addr - wl->part.mem.start;
70}
71
72/* Set the SPI partitions to access the chip addresses
73 *
74 * To simplify driver code, a fixed (virtual) memory map is defined for
75 * register and memory addresses. Because in the chipset, in different stages
76 * of operation, those addresses will move around, an address translation
77 * mechanism is required.
78 *
79 * There are four partitions (three memory and one register partition),
80 * which are mapped to two different areas of the hardware memory.
81 *
82 * Virtual address
83 * space
84 *
85 * | |
86 * ...+----+--> mem.start
87 * Physical address ... | |
88 * space ... | | [PART_0]
89 * ... | |
90 * 00000000 <--+----+... ...+----+--> mem.start + mem.size
91 * | | ... | |
92 * |MEM | ... | |
93 * | | ... | |
94 * mem.size <--+----+... | | {unused area)
95 * | | ... | |
96 * |REG | ... | |
97 * mem.size | | ... | |
98 * + <--+----+... ...+----+--> reg.start
99 * reg.size | | ... | |
100 * |MEM2| ... | | [PART_1]
101 * | | ... | |
102 * ...+----+--> reg.start + reg.size
103 * | |
104 *
105 */
106int wl1271_set_partition(struct wl1271 *wl,
107 struct wl1271_partition_set *p)
108{
109 /* copy partition info */
110 memcpy(&wl->part, p, sizeof(*p));
111
112 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
113 p->mem.start, p->mem.size);
114 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
115 p->reg.start, p->reg.size);
116 wl1271_debug(DEBUG_SPI, "mem2_start %08X mem2_size %08X",
117 p->mem2.start, p->mem2.size);
118 wl1271_debug(DEBUG_SPI, "mem3_start %08X mem3_size %08X",
119 p->mem3.start, p->mem3.size);
120
121 /* write partition info to the chipset */
122 wl1271_raw_write32(wl, HW_PART0_START_ADDR, p->mem.start);
123 wl1271_raw_write32(wl, HW_PART0_SIZE_ADDR, p->mem.size);
124 wl1271_raw_write32(wl, HW_PART1_START_ADDR, p->reg.start);
125 wl1271_raw_write32(wl, HW_PART1_SIZE_ADDR, p->reg.size);
126 wl1271_raw_write32(wl, HW_PART2_START_ADDR, p->mem2.start);
127 wl1271_raw_write32(wl, HW_PART2_SIZE_ADDR, p->mem2.size);
128 wl1271_raw_write32(wl, HW_PART3_START_ADDR, p->mem3.start);
129
130 return 0;
131}
132
Teemu Paasikivi9b280722010-02-18 13:25:56 +0200133void wl1271_io_reset(struct wl1271 *wl)
134{
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200135 wl->if_ops->reset(wl);
Teemu Paasikivi9b280722010-02-18 13:25:56 +0200136}
137
138void wl1271_io_init(struct wl1271 *wl)
139{
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200140 wl->if_ops->init(wl);
Teemu Paasikivi9b280722010-02-18 13:25:56 +0200141}
142
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200143void wl1271_raw_write(struct wl1271 *wl, int addr, void *buf,
144 size_t len, bool fixed)
145{
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200146 wl->if_ops->write(wl, addr, buf, len, fixed);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200147}
148
149void wl1271_raw_read(struct wl1271 *wl, int addr, void *buf,
150 size_t len, bool fixed)
151{
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200152 wl->if_ops->read(wl, addr, buf, len, fixed);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200153}
154
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200155void wl1271_read(struct wl1271 *wl, int addr, void *buf, size_t len,
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200156 bool fixed)
157{
158 int physical;
159
160 physical = wl1271_translate_addr(wl, addr);
161
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200162 wl1271_raw_read(wl, physical, buf, len, fixed);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200163}
164
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200165void wl1271_write(struct wl1271 *wl, int addr, void *buf, size_t len,
166 bool fixed)
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200167{
168 int physical;
169
170 physical = wl1271_translate_addr(wl, addr);
171
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200172 wl1271_raw_write(wl, physical, buf, len, fixed);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200173}
174
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200175u32 wl1271_read32(struct wl1271 *wl, int addr)
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200176{
177 return wl1271_raw_read32(wl, wl1271_translate_addr(wl, addr));
178}
179
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200180void wl1271_write32(struct wl1271 *wl, int addr, u32 val)
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200181{
182 wl1271_raw_write32(wl, wl1271_translate_addr(wl, addr), val);
183}
184
185void wl1271_top_reg_write(struct wl1271 *wl, int addr, u16 val)
186{
187 /* write address >> 1 + 0x30000 to OCP_POR_CTR */
188 addr = (addr >> 1) + 0x30000;
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200189 wl1271_write32(wl, OCP_POR_CTR, addr);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200190
191 /* write value to OCP_POR_WDATA */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200192 wl1271_write32(wl, OCP_DATA_WRITE, val);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200193
194 /* write 1 to OCP_CMD */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200195 wl1271_write32(wl, OCP_CMD, OCP_CMD_WRITE);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200196}
197
198u16 wl1271_top_reg_read(struct wl1271 *wl, int addr)
199{
200 u32 val;
201 int timeout = OCP_CMD_LOOP;
202
203 /* write address >> 1 + 0x30000 to OCP_POR_CTR */
204 addr = (addr >> 1) + 0x30000;
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200205 wl1271_write32(wl, OCP_POR_CTR, addr);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200206
207 /* write 2 to OCP_CMD */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200208 wl1271_write32(wl, OCP_CMD, OCP_CMD_READ);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200209
210 /* poll for data ready */
211 do {
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200212 val = wl1271_read32(wl, OCP_DATA_READ);
Teemu Paasikivi521a5b22010-02-18 13:25:54 +0200213 } while (!(val & OCP_READY_MASK) && --timeout);
214
215 if (!timeout) {
216 wl1271_warning("Top register access timed out.");
217 return 0xffff;
218 }
219
220 /* check data status and return if OK */
221 if ((val & OCP_STATUS_MASK) == OCP_STATUS_OK)
222 return val & 0xffff;
223 else {
224 wl1271_warning("Top register access returned error.");
225 return 0xffff;
226 }
227}
228