wl1271_io.c 6.5 KB

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