wl1271_io.c 6.5 KB

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