wl1271_io.c 6.3 KB

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