Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of wl1271 |
| 3 | * |
| 4 | * Copyright (C) 2008-2009 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 | |
| 33 | static int wl1271_translate_reg_addr(struct wl1271 *wl, int addr) |
| 34 | { |
| 35 | return addr - wl->physical_reg_addr + wl->virtual_reg_addr; |
| 36 | } |
| 37 | |
| 38 | static int wl1271_translate_mem_addr(struct wl1271 *wl, int addr) |
| 39 | { |
| 40 | return addr - wl->physical_mem_addr + wl->virtual_mem_addr; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void wl1271_spi_reset(struct wl1271 *wl) |
| 45 | { |
| 46 | u8 *cmd; |
| 47 | struct spi_transfer t; |
| 48 | struct spi_message m; |
| 49 | |
| 50 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 51 | if (!cmd) { |
| 52 | wl1271_error("could not allocate cmd for spi reset"); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | memset(&t, 0, sizeof(t)); |
| 57 | spi_message_init(&m); |
| 58 | |
| 59 | memset(cmd, 0xff, WSPI_INIT_CMD_LEN); |
| 60 | |
| 61 | t.tx_buf = cmd; |
| 62 | t.len = WSPI_INIT_CMD_LEN; |
| 63 | spi_message_add_tail(&t, &m); |
| 64 | |
| 65 | spi_sync(wl->spi, &m); |
| 66 | |
| 67 | wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); |
| 68 | } |
| 69 | |
| 70 | void wl1271_spi_init(struct wl1271 *wl) |
| 71 | { |
| 72 | u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; |
| 73 | struct spi_transfer t; |
| 74 | struct spi_message m; |
| 75 | |
| 76 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 77 | if (!cmd) { |
| 78 | wl1271_error("could not allocate cmd for spi init"); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | memset(crc, 0, sizeof(crc)); |
| 83 | memset(&t, 0, sizeof(t)); |
| 84 | spi_message_init(&m); |
| 85 | |
| 86 | /* |
| 87 | * Set WSPI_INIT_COMMAND |
| 88 | * the data is being send from the MSB to LSB |
| 89 | */ |
| 90 | cmd[2] = 0xff; |
| 91 | cmd[3] = 0xff; |
| 92 | cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; |
| 93 | cmd[0] = 0; |
| 94 | cmd[7] = 0; |
| 95 | cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; |
| 96 | cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; |
| 97 | |
| 98 | if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) |
| 99 | cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; |
| 100 | else |
| 101 | cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; |
| 102 | |
| 103 | cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS |
| 104 | | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; |
| 105 | |
| 106 | crc[0] = cmd[1]; |
| 107 | crc[1] = cmd[0]; |
| 108 | crc[2] = cmd[7]; |
| 109 | crc[3] = cmd[6]; |
| 110 | crc[4] = cmd[5]; |
| 111 | |
| 112 | cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; |
| 113 | cmd[4] |= WSPI_INIT_CMD_END; |
| 114 | |
| 115 | t.tx_buf = cmd; |
| 116 | t.len = WSPI_INIT_CMD_LEN; |
| 117 | spi_message_add_tail(&t, &m); |
| 118 | |
| 119 | spi_sync(wl->spi, &m); |
| 120 | |
| 121 | wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
| 122 | } |
| 123 | |
| 124 | /* Set the SPI partitions to access the chip addresses |
| 125 | * |
| 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 |
| 131 | * also supported. |
| 132 | * |
| 133 | * PHYSICAL address |
| 134 | * space |
| 135 | * |
| 136 | * | | |
| 137 | * ...+----+--> mem_start |
| 138 | * VIRTUAL address ... | | |
| 139 | * space ... | | [PART_0] |
| 140 | * ... | | |
| 141 | * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size |
| 142 | * | | ... | | |
| 143 | * |MEM | ... | | |
| 144 | * | | ... | | |
| 145 | * part_size <--+----+... | | {unused area) |
| 146 | * | | ... | | |
| 147 | * |REG | ... | | |
| 148 | * part_size | | ... | | |
| 149 | * + <--+----+... ...+----+--> reg_start |
| 150 | * reg_size ... | | |
| 151 | * ... | | [PART_1] |
| 152 | * ... | | |
| 153 | * ...+----+--> reg_start + reg_size |
| 154 | * | | |
| 155 | * |
| 156 | */ |
| 157 | int wl1271_set_partition(struct wl1271 *wl, |
| 158 | u32 mem_start, u32 mem_size, |
| 159 | u32 reg_start, u32 reg_size) |
| 160 | { |
| 161 | struct wl1271_partition *partition; |
| 162 | struct spi_transfer t; |
| 163 | struct spi_message m; |
| 164 | size_t len, cmd_len; |
| 165 | u32 *cmd; |
| 166 | int addr; |
| 167 | |
| 168 | cmd_len = sizeof(u32) + 2 * sizeof(struct wl1271_partition); |
| 169 | cmd = kzalloc(cmd_len, GFP_KERNEL); |
| 170 | if (!cmd) |
| 171 | return -ENOMEM; |
| 172 | |
| 173 | spi_message_init(&m); |
| 174 | memset(&t, 0, sizeof(t)); |
| 175 | |
| 176 | partition = (struct wl1271_partition *) (cmd + 1); |
| 177 | addr = HW_ACCESS_PART0_SIZE_ADDR; |
| 178 | len = 2 * sizeof(struct wl1271_partition); |
| 179 | |
| 180 | *cmd |= WSPI_CMD_WRITE; |
| 181 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 182 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
| 183 | |
| 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); |
| 188 | |
| 189 | /* Make sure that the two partitions together don't exceed the |
| 190 | * address range */ |
| 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); |
| 199 | } |
| 200 | |
| 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); |
| 223 | } |
| 224 | |
| 225 | partition[0].start = mem_start; |
| 226 | partition[0].size = mem_size; |
| 227 | partition[1].start = reg_start; |
| 228 | partition[1].size = reg_size; |
| 229 | |
| 230 | wl->physical_mem_addr = mem_start; |
| 231 | wl->physical_reg_addr = reg_start; |
| 232 | |
| 233 | wl->virtual_mem_addr = 0; |
| 234 | wl->virtual_reg_addr = mem_size; |
| 235 | |
| 236 | t.tx_buf = cmd; |
| 237 | t.len = cmd_len; |
| 238 | spi_message_add_tail(&t, &m); |
| 239 | |
| 240 | spi_sync(wl->spi, &m); |
| 241 | |
| 242 | kfree(cmd); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 247 | #define WL1271_BUSY_WORD_TIMEOUT 1000 |
| 248 | |
| 249 | void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len) |
| 250 | { |
| 251 | struct spi_transfer t[1]; |
| 252 | struct spi_message m; |
| 253 | u32 *busy_buf; |
| 254 | int num_busy_bytes = 0; |
| 255 | |
| 256 | wl1271_info("spi read BUSY!"); |
| 257 | |
| 258 | /* |
| 259 | * Look for the non-busy word in the read buffer, and if found, |
| 260 | * read in the remaining data into the buffer. |
| 261 | */ |
| 262 | busy_buf = (u32 *)buf; |
| 263 | for (; (u32)busy_buf < (u32)buf + len; busy_buf++) { |
| 264 | num_busy_bytes += sizeof(u32); |
| 265 | if (*busy_buf & 0x1) { |
| 266 | spi_message_init(&m); |
| 267 | memset(t, 0, sizeof(t)); |
| 268 | memmove(buf, busy_buf, len - num_busy_bytes); |
| 269 | t[0].rx_buf = buf + (len - num_busy_bytes); |
| 270 | t[0].len = num_busy_bytes; |
| 271 | spi_message_add_tail(&t[0], &m); |
| 272 | spi_sync(wl->spi, &m); |
| 273 | return; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Read further busy words from SPI until a non-busy word is |
| 279 | * encountered, then read the data itself into the buffer. |
| 280 | */ |
| 281 | wl1271_info("spi read BUSY-polling needed!"); |
| 282 | |
| 283 | num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT; |
| 284 | busy_buf = wl->buffer_busyword; |
| 285 | while (num_busy_bytes) { |
| 286 | num_busy_bytes--; |
| 287 | spi_message_init(&m); |
| 288 | memset(t, 0, sizeof(t)); |
| 289 | t[0].rx_buf = busy_buf; |
| 290 | t[0].len = sizeof(u32); |
| 291 | spi_message_add_tail(&t[0], &m); |
| 292 | spi_sync(wl->spi, &m); |
| 293 | |
| 294 | if (*busy_buf & 0x1) { |
| 295 | spi_message_init(&m); |
| 296 | memset(t, 0, sizeof(t)); |
| 297 | t[0].rx_buf = buf; |
| 298 | t[0].len = len; |
| 299 | spi_message_add_tail(&t[0], &m); |
| 300 | spi_sync(wl->spi, &m); |
| 301 | return; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /* The SPI bus is unresponsive, the read failed. */ |
| 306 | memset(buf, 0, len); |
| 307 | wl1271_error("SPI read busy-word timeout!\n"); |
| 308 | } |
| 309 | |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 310 | void wl1271_spi_read(struct wl1271 *wl, int addr, void *buf, |
| 311 | size_t len, bool fixed) |
| 312 | { |
| 313 | struct spi_transfer t[3]; |
| 314 | struct spi_message m; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 315 | u32 *busy_buf; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 316 | u32 *cmd; |
| 317 | |
| 318 | cmd = &wl->buffer_cmd; |
| 319 | busy_buf = wl->buffer_busyword; |
| 320 | |
| 321 | *cmd = 0; |
| 322 | *cmd |= WSPI_CMD_READ; |
| 323 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 324 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
| 325 | |
| 326 | if (fixed) |
| 327 | *cmd |= WSPI_CMD_FIXED; |
| 328 | |
| 329 | spi_message_init(&m); |
| 330 | memset(t, 0, sizeof(t)); |
| 331 | |
| 332 | t[0].tx_buf = cmd; |
| 333 | t[0].len = 4; |
| 334 | spi_message_add_tail(&t[0], &m); |
| 335 | |
| 336 | /* Busy and non busy words read */ |
| 337 | t[1].rx_buf = busy_buf; |
| 338 | t[1].len = WL1271_BUSY_WORD_LEN; |
| 339 | spi_message_add_tail(&t[1], &m); |
| 340 | |
| 341 | t[2].rx_buf = buf; |
| 342 | t[2].len = len; |
| 343 | spi_message_add_tail(&t[2], &m); |
| 344 | |
| 345 | spi_sync(wl->spi, &m); |
| 346 | |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 347 | /* Check busy words */ |
| 348 | if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1)) |
| 349 | wl1271_spi_read_busy(wl, buf, len); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 350 | |
| 351 | wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
| 352 | wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); |
| 353 | } |
| 354 | |
| 355 | void wl1271_spi_write(struct wl1271 *wl, int addr, void *buf, |
| 356 | size_t len, bool fixed) |
| 357 | { |
| 358 | struct spi_transfer t[2]; |
| 359 | struct spi_message m; |
| 360 | u32 *cmd; |
| 361 | |
| 362 | cmd = &wl->buffer_cmd; |
| 363 | |
| 364 | *cmd = 0; |
| 365 | *cmd |= WSPI_CMD_WRITE; |
| 366 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 367 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
| 368 | |
| 369 | if (fixed) |
| 370 | *cmd |= WSPI_CMD_FIXED; |
| 371 | |
| 372 | spi_message_init(&m); |
| 373 | memset(t, 0, sizeof(t)); |
| 374 | |
| 375 | t[0].tx_buf = cmd; |
| 376 | t[0].len = sizeof(*cmd); |
| 377 | spi_message_add_tail(&t[0], &m); |
| 378 | |
| 379 | t[1].tx_buf = buf; |
| 380 | t[1].len = len; |
| 381 | spi_message_add_tail(&t[1], &m); |
| 382 | |
| 383 | spi_sync(wl->spi, &m); |
| 384 | |
| 385 | wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
| 386 | wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); |
| 387 | } |
| 388 | |
| 389 | void wl1271_spi_mem_read(struct wl1271 *wl, int addr, void *buf, |
| 390 | size_t len) |
| 391 | { |
| 392 | int physical; |
| 393 | |
| 394 | physical = wl1271_translate_mem_addr(wl, addr); |
| 395 | |
| 396 | wl1271_spi_read(wl, physical, buf, len, false); |
| 397 | } |
| 398 | |
| 399 | void wl1271_spi_mem_write(struct wl1271 *wl, int addr, void *buf, |
| 400 | size_t len) |
| 401 | { |
| 402 | int physical; |
| 403 | |
| 404 | physical = wl1271_translate_mem_addr(wl, addr); |
| 405 | |
| 406 | wl1271_spi_write(wl, physical, buf, len, false); |
| 407 | } |
| 408 | |
| 409 | void wl1271_spi_reg_read(struct wl1271 *wl, int addr, void *buf, size_t len, |
| 410 | bool fixed) |
| 411 | { |
| 412 | int physical; |
| 413 | |
| 414 | physical = wl1271_translate_reg_addr(wl, addr); |
| 415 | |
| 416 | wl1271_spi_read(wl, physical, buf, len, fixed); |
| 417 | } |
| 418 | |
| 419 | void wl1271_spi_reg_write(struct wl1271 *wl, int addr, void *buf, size_t len, |
| 420 | bool fixed) |
| 421 | { |
| 422 | int physical; |
| 423 | |
| 424 | physical = wl1271_translate_reg_addr(wl, addr); |
| 425 | |
| 426 | wl1271_spi_write(wl, physical, buf, len, fixed); |
| 427 | } |
| 428 | |
| 429 | u32 wl1271_mem_read32(struct wl1271 *wl, int addr) |
| 430 | { |
| 431 | return wl1271_read32(wl, wl1271_translate_mem_addr(wl, addr)); |
| 432 | } |
| 433 | |
| 434 | void wl1271_mem_write32(struct wl1271 *wl, int addr, u32 val) |
| 435 | { |
| 436 | wl1271_write32(wl, wl1271_translate_mem_addr(wl, addr), val); |
| 437 | } |
| 438 | |
| 439 | u32 wl1271_reg_read32(struct wl1271 *wl, int addr) |
| 440 | { |
| 441 | return wl1271_read32(wl, wl1271_translate_reg_addr(wl, addr)); |
| 442 | } |
| 443 | |
| 444 | void wl1271_reg_write32(struct wl1271 *wl, int addr, u32 val) |
| 445 | { |
| 446 | wl1271_write32(wl, wl1271_translate_reg_addr(wl, addr), val); |
| 447 | } |