Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 1 | /* |
| 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 Paasikivi | 54f7e50 | 2010-02-22 08:38:22 +0200 | [diff] [blame^] | 34 | void wl1271_disable_interrupts(struct wl1271 *wl) |
| 35 | { |
| 36 | wl1271_spi_disable_interrupts(wl); |
| 37 | } |
| 38 | |
| 39 | void wl1271_enable_interrupts(struct wl1271 *wl) |
| 40 | { |
| 41 | wl1271_spi_enable_interrupts(wl); |
| 42 | } |
| 43 | |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 44 | static int wl1271_translate_addr(struct wl1271 *wl, int addr) |
| 45 | { |
| 46 | /* |
| 47 | * To translate, first check to which window of addresses the |
| 48 | * particular address belongs. Then subtract the starting address |
| 49 | * of that window from the address. Then, add offset of the |
| 50 | * translated region. |
| 51 | * |
| 52 | * The translated regions occur next to each other in physical device |
| 53 | * memory, so just add the sizes of the preceeding address regions to |
| 54 | * get the offset to the new region. |
| 55 | * |
| 56 | * Currently, only the two first regions are addressed, and the |
| 57 | * assumption is that all addresses will fall into either of those |
| 58 | * two. |
| 59 | */ |
| 60 | if ((addr >= wl->part.reg.start) && |
| 61 | (addr < wl->part.reg.start + wl->part.reg.size)) |
| 62 | return addr - wl->part.reg.start + wl->part.mem.size; |
| 63 | else |
| 64 | return addr - wl->part.mem.start; |
| 65 | } |
| 66 | |
| 67 | /* Set the SPI partitions to access the chip addresses |
| 68 | * |
| 69 | * To simplify driver code, a fixed (virtual) memory map is defined for |
| 70 | * register and memory addresses. Because in the chipset, in different stages |
| 71 | * of operation, those addresses will move around, an address translation |
| 72 | * mechanism is required. |
| 73 | * |
| 74 | * There are four partitions (three memory and one register partition), |
| 75 | * which are mapped to two different areas of the hardware memory. |
| 76 | * |
| 77 | * Virtual address |
| 78 | * space |
| 79 | * |
| 80 | * | | |
| 81 | * ...+----+--> mem.start |
| 82 | * Physical address ... | | |
| 83 | * space ... | | [PART_0] |
| 84 | * ... | | |
| 85 | * 00000000 <--+----+... ...+----+--> mem.start + mem.size |
| 86 | * | | ... | | |
| 87 | * |MEM | ... | | |
| 88 | * | | ... | | |
| 89 | * mem.size <--+----+... | | {unused area) |
| 90 | * | | ... | | |
| 91 | * |REG | ... | | |
| 92 | * mem.size | | ... | | |
| 93 | * + <--+----+... ...+----+--> reg.start |
| 94 | * reg.size | | ... | | |
| 95 | * |MEM2| ... | | [PART_1] |
| 96 | * | | ... | | |
| 97 | * ...+----+--> reg.start + reg.size |
| 98 | * | | |
| 99 | * |
| 100 | */ |
| 101 | int wl1271_set_partition(struct wl1271 *wl, |
| 102 | struct wl1271_partition_set *p) |
| 103 | { |
| 104 | /* copy partition info */ |
| 105 | memcpy(&wl->part, p, sizeof(*p)); |
| 106 | |
| 107 | wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X", |
| 108 | p->mem.start, p->mem.size); |
| 109 | wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X", |
| 110 | p->reg.start, p->reg.size); |
| 111 | wl1271_debug(DEBUG_SPI, "mem2_start %08X mem2_size %08X", |
| 112 | p->mem2.start, p->mem2.size); |
| 113 | wl1271_debug(DEBUG_SPI, "mem3_start %08X mem3_size %08X", |
| 114 | p->mem3.start, p->mem3.size); |
| 115 | |
| 116 | /* write partition info to the chipset */ |
| 117 | wl1271_raw_write32(wl, HW_PART0_START_ADDR, p->mem.start); |
| 118 | wl1271_raw_write32(wl, HW_PART0_SIZE_ADDR, p->mem.size); |
| 119 | wl1271_raw_write32(wl, HW_PART1_START_ADDR, p->reg.start); |
| 120 | wl1271_raw_write32(wl, HW_PART1_SIZE_ADDR, p->reg.size); |
| 121 | wl1271_raw_write32(wl, HW_PART2_START_ADDR, p->mem2.start); |
| 122 | wl1271_raw_write32(wl, HW_PART2_SIZE_ADDR, p->mem2.size); |
| 123 | wl1271_raw_write32(wl, HW_PART3_START_ADDR, p->mem3.start); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Teemu Paasikivi | 9b28072 | 2010-02-18 13:25:56 +0200 | [diff] [blame] | 128 | void wl1271_io_reset(struct wl1271 *wl) |
| 129 | { |
| 130 | wl1271_spi_reset(wl); |
| 131 | } |
| 132 | |
| 133 | void wl1271_io_init(struct wl1271 *wl) |
| 134 | { |
| 135 | wl1271_spi_init(wl); |
| 136 | } |
| 137 | |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 138 | void wl1271_raw_write(struct wl1271 *wl, int addr, void *buf, |
| 139 | size_t len, bool fixed) |
| 140 | { |
| 141 | wl1271_spi_raw_write(wl, addr, buf, len, fixed); |
| 142 | } |
| 143 | |
| 144 | void wl1271_raw_read(struct wl1271 *wl, int addr, void *buf, |
| 145 | size_t len, bool fixed) |
| 146 | { |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 147 | wl1271_spi_raw_read(wl, addr, buf, len, fixed); |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 150 | void wl1271_read(struct wl1271 *wl, int addr, void *buf, size_t len, |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 151 | bool fixed) |
| 152 | { |
| 153 | int physical; |
| 154 | |
| 155 | physical = wl1271_translate_addr(wl, addr); |
| 156 | |
| 157 | wl1271_spi_raw_read(wl, physical, buf, len, fixed); |
| 158 | } |
| 159 | |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 160 | void wl1271_write(struct wl1271 *wl, int addr, void *buf, size_t len, |
| 161 | bool fixed) |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 162 | { |
| 163 | int physical; |
| 164 | |
| 165 | physical = wl1271_translate_addr(wl, addr); |
| 166 | |
| 167 | wl1271_spi_raw_write(wl, physical, buf, len, fixed); |
| 168 | } |
| 169 | |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 170 | u32 wl1271_read32(struct wl1271 *wl, int addr) |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 171 | { |
| 172 | return wl1271_raw_read32(wl, wl1271_translate_addr(wl, addr)); |
| 173 | } |
| 174 | |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 175 | void wl1271_write32(struct wl1271 *wl, int addr, u32 val) |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 176 | { |
| 177 | wl1271_raw_write32(wl, wl1271_translate_addr(wl, addr), val); |
| 178 | } |
| 179 | |
| 180 | void wl1271_top_reg_write(struct wl1271 *wl, int addr, u16 val) |
| 181 | { |
| 182 | /* write address >> 1 + 0x30000 to OCP_POR_CTR */ |
| 183 | addr = (addr >> 1) + 0x30000; |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 184 | wl1271_write32(wl, OCP_POR_CTR, addr); |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 185 | |
| 186 | /* write value to OCP_POR_WDATA */ |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 187 | wl1271_write32(wl, OCP_DATA_WRITE, val); |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 188 | |
| 189 | /* write 1 to OCP_CMD */ |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 190 | wl1271_write32(wl, OCP_CMD, OCP_CMD_WRITE); |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | u16 wl1271_top_reg_read(struct wl1271 *wl, int addr) |
| 194 | { |
| 195 | u32 val; |
| 196 | int timeout = OCP_CMD_LOOP; |
| 197 | |
| 198 | /* write address >> 1 + 0x30000 to OCP_POR_CTR */ |
| 199 | addr = (addr >> 1) + 0x30000; |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 200 | wl1271_write32(wl, OCP_POR_CTR, addr); |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 201 | |
| 202 | /* write 2 to OCP_CMD */ |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 203 | wl1271_write32(wl, OCP_CMD, OCP_CMD_READ); |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 204 | |
| 205 | /* poll for data ready */ |
| 206 | do { |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 207 | val = wl1271_read32(wl, OCP_DATA_READ); |
Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 208 | } while (!(val & OCP_READY_MASK) && --timeout); |
| 209 | |
| 210 | if (!timeout) { |
| 211 | wl1271_warning("Top register access timed out."); |
| 212 | return 0xffff; |
| 213 | } |
| 214 | |
| 215 | /* check data status and return if OK */ |
| 216 | if ((val & OCP_STATUS_MASK) == OCP_STATUS_OK) |
| 217 | return val & 0xffff; |
| 218 | else { |
| 219 | wl1271_warning("Top register access returned error."); |
| 220 | return 0xffff; |
| 221 | } |
| 222 | } |
| 223 | |