Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 1 | /* |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 2 | * This file is part of wl1251 |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 3 | * |
| 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 Valo | 1367411 | 2009-06-12 14:17:25 +0300 | [diff] [blame] | 28 | #include "wl1251.h" |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 29 | #include "reg.h" |
Kalle Valo | ef2f8d4 | 2009-06-12 14:17:19 +0300 | [diff] [blame] | 30 | #include "wl1251_spi.h" |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 31 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame^] | 32 | static void wl1251_spi_reset(struct wl1251 *wl) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 33 | { |
| 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 40 | wl1251_error("could not allocate cmd for spi reset"); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 41 | 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 55 | wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 56 | } |
| 57 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame^] | 58 | static void wl1251_spi_init(struct wl1251 *wl) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 59 | { |
| 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 66 | wl1251_error("could not allocate cmd for spi init"); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 67 | 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 109 | wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 110 | } |
| 111 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame^] | 112 | static void wl1251_spi_reset_wake(struct wl1251 *wl) |
| 113 | { |
| 114 | wl1251_spi_reset(wl); |
| 115 | wl1251_spi_init(wl); |
| 116 | } |
| 117 | |
| 118 | |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 119 | /* 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 152 | int wl1251_set_partition(struct wl1251 *wl, |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 153 | u32 mem_start, u32 mem_size, |
| 154 | u32 reg_start, u32 reg_size) |
| 155 | { |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 156 | struct wl1251_partition *partition; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 157 | struct spi_transfer t; |
| 158 | struct spi_message m; |
Kalle Valo | 8d47cdb | 2009-06-12 14:14:41 +0300 | [diff] [blame] | 159 | size_t len, cmd_len; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 160 | u32 *cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 161 | int addr; |
| 162 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 163 | cmd_len = sizeof(u32) + 2 * sizeof(struct wl1251_partition); |
Kalle Valo | 8d47cdb | 2009-06-12 14:14:41 +0300 | [diff] [blame] | 164 | cmd = kzalloc(cmd_len, GFP_KERNEL); |
| 165 | if (!cmd) |
| 166 | return -ENOMEM; |
| 167 | |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 168 | spi_message_init(&m); |
| 169 | memset(&t, 0, sizeof(t)); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 170 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 171 | partition = (struct wl1251_partition *) (cmd + 1); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 172 | addr = HW_ACCESS_PART0_SIZE_ADDR; |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 173 | len = 2 * sizeof(struct wl1251_partition); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 174 | |
| 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 179 | wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X", |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 180 | mem_start, mem_size); |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 181 | wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X", |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 182 | 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 187 | wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual" |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 188 | " address range. Truncating partition[0]."); |
| 189 | mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size; |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 190 | wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X", |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 191 | mem_start, mem_size); |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 192 | wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X", |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 193 | 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 200 | wl1251_debug(DEBUG_SPI, "End of partition[0] is " |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 201 | "overlapping partition[1]. Adjusted."); |
| 202 | mem_size = reg_start - mem_start; |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 203 | wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X", |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 204 | mem_start, mem_size); |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 205 | wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X", |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 206 | 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 211 | wl1251_debug(DEBUG_SPI, "End of partition[1] is" |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 212 | " overlapping partition[0]. Adjusted."); |
| 213 | reg_size = mem_start - reg_start; |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 214 | wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X", |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 215 | mem_start, mem_size); |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 216 | wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X", |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 217 | 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 Valo | 8d47cdb | 2009-06-12 14:14:41 +0300 | [diff] [blame] | 231 | t.tx_buf = cmd; |
| 232 | t.len = cmd_len; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 233 | spi_message_add_tail(&t, &m); |
| 234 | |
| 235 | spi_sync(wl->spi, &m); |
Kalle Valo | 8d47cdb | 2009-06-12 14:14:41 +0300 | [diff] [blame] | 236 | |
| 237 | kfree(cmd); |
| 238 | |
| 239 | return 0; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 240 | } |
| 241 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame^] | 242 | static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, |
| 243 | size_t len) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 244 | { |
| 245 | struct spi_transfer t[3]; |
| 246 | struct spi_message m; |
Kalle Valo | 5262c12 | 2009-06-12 14:14:55 +0300 | [diff] [blame] | 247 | u8 *busy_buf; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 248 | u32 *cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 249 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 250 | cmd = &wl->buffer_cmd; |
Kalle Valo | 5262c12 | 2009-06-12 14:14:55 +0300 | [diff] [blame] | 251 | busy_buf = wl->buffer_busyword; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 252 | |
| 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 Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 257 | |
| 258 | spi_message_init(&m); |
| 259 | memset(t, 0, sizeof(t)); |
| 260 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 261 | t[0].tx_buf = cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 262 | 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 267 | t[1].len = WL1251_BUSY_WORD_LEN; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 268 | 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 278 | wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
| 279 | wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 280 | } |
| 281 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame^] | 282 | static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, |
| 283 | size_t len) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 284 | { |
| 285 | struct spi_transfer t[2]; |
| 286 | struct spi_message m; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 287 | u32 *cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 288 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 289 | 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 Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 295 | |
| 296 | spi_message_init(&m); |
| 297 | memset(t, 0, sizeof(t)); |
| 298 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 299 | t[0].tx_buf = cmd; |
| 300 | t[0].len = sizeof(*cmd); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 301 | 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 Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 309 | wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
| 310 | wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 311 | } |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame^] | 312 | |
| 313 | const struct wl1251_if_operations wl1251_spi_ops = { |
| 314 | .read = wl1251_spi_read, |
| 315 | .write = wl1251_spi_write, |
| 316 | .reset = wl1251_spi_reset_wake, |
| 317 | }; |