io.c 6.8 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/spi/spi.h>
  26. #include <linux/interrupt.h>
  27. #include "wlcore.h"
  28. #include "debug.h"
  29. #include "wl12xx_80211.h"
  30. #include "io.h"
  31. #include "tx.h"
  32. #define OCP_CMD_LOOP 32
  33. #define OCP_CMD_WRITE 0x1
  34. #define OCP_CMD_READ 0x2
  35. #define OCP_READY_MASK BIT(18)
  36. #define OCP_STATUS_MASK (BIT(16) | BIT(17))
  37. #define OCP_STATUS_NO_RESP 0x00000
  38. #define OCP_STATUS_OK 0x10000
  39. #define OCP_STATUS_REQ_FAILED 0x20000
  40. #define OCP_STATUS_RESP_ERROR 0x30000
  41. bool wl1271_set_block_size(struct wl1271 *wl)
  42. {
  43. if (wl->if_ops->set_block_size) {
  44. wl->if_ops->set_block_size(wl->dev, WL12XX_BUS_BLOCK_SIZE);
  45. return true;
  46. }
  47. return false;
  48. }
  49. void wl1271_disable_interrupts(struct wl1271 *wl)
  50. {
  51. disable_irq(wl->irq);
  52. }
  53. void wl1271_enable_interrupts(struct wl1271 *wl)
  54. {
  55. enable_irq(wl->irq);
  56. }
  57. int wlcore_translate_addr(struct wl1271 *wl, int addr)
  58. {
  59. struct wlcore_partition_set *part = &wl->curr_part;
  60. /*
  61. * To translate, first check to which window of addresses the
  62. * particular address belongs. Then subtract the starting address
  63. * of that window from the address. Then, add offset of the
  64. * translated region.
  65. *
  66. * The translated regions occur next to each other in physical device
  67. * memory, so just add the sizes of the preceding address regions to
  68. * get the offset to the new region.
  69. */
  70. if ((addr >= part->mem.start) &&
  71. (addr < part->mem.start + part->mem.size))
  72. return addr - part->mem.start;
  73. else if ((addr >= part->reg.start) &&
  74. (addr < part->reg.start + part->reg.size))
  75. return addr - part->reg.start + part->mem.size;
  76. else if ((addr >= part->mem2.start) &&
  77. (addr < part->mem2.start + part->mem2.size))
  78. return addr - part->mem2.start + part->mem.size +
  79. part->reg.size;
  80. else if ((addr >= part->mem3.start) &&
  81. (addr < part->mem3.start + part->mem3.size))
  82. return addr - part->mem3.start + part->mem.size +
  83. part->reg.size + part->mem2.size;
  84. WARN(1, "HW address 0x%x out of range", addr);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(wlcore_translate_addr);
  88. /* Set the partitions to access the chip addresses
  89. *
  90. * To simplify driver code, a fixed (virtual) memory map is defined for
  91. * register and memory addresses. Because in the chipset, in different stages
  92. * of operation, those addresses will move around, an address translation
  93. * mechanism is required.
  94. *
  95. * There are four partitions (three memory and one register partition),
  96. * which are mapped to two different areas of the hardware memory.
  97. *
  98. * Virtual address
  99. * space
  100. *
  101. * | |
  102. * ...+----+--> mem.start
  103. * Physical address ... | |
  104. * space ... | | [PART_0]
  105. * ... | |
  106. * 00000000 <--+----+... ...+----+--> mem.start + mem.size
  107. * | | ... | |
  108. * |MEM | ... | |
  109. * | | ... | |
  110. * mem.size <--+----+... | | {unused area)
  111. * | | ... | |
  112. * |REG | ... | |
  113. * mem.size | | ... | |
  114. * + <--+----+... ...+----+--> reg.start
  115. * reg.size | | ... | |
  116. * |MEM2| ... | | [PART_1]
  117. * | | ... | |
  118. * ...+----+--> reg.start + reg.size
  119. * | |
  120. *
  121. */
  122. void wlcore_set_partition(struct wl1271 *wl,
  123. const struct wlcore_partition_set *p)
  124. {
  125. /* copy partition info */
  126. memcpy(&wl->curr_part, p, sizeof(*p));
  127. wl1271_debug(DEBUG_IO, "mem_start %08X mem_size %08X",
  128. p->mem.start, p->mem.size);
  129. wl1271_debug(DEBUG_IO, "reg_start %08X reg_size %08X",
  130. p->reg.start, p->reg.size);
  131. wl1271_debug(DEBUG_IO, "mem2_start %08X mem2_size %08X",
  132. p->mem2.start, p->mem2.size);
  133. wl1271_debug(DEBUG_IO, "mem3_start %08X mem3_size %08X",
  134. p->mem3.start, p->mem3.size);
  135. wl1271_raw_write32(wl, HW_PART0_START_ADDR, p->mem.start);
  136. wl1271_raw_write32(wl, HW_PART0_SIZE_ADDR, p->mem.size);
  137. wl1271_raw_write32(wl, HW_PART1_START_ADDR, p->reg.start);
  138. wl1271_raw_write32(wl, HW_PART1_SIZE_ADDR, p->reg.size);
  139. wl1271_raw_write32(wl, HW_PART2_START_ADDR, p->mem2.start);
  140. wl1271_raw_write32(wl, HW_PART2_SIZE_ADDR, p->mem2.size);
  141. /*
  142. * We don't need the size of the last partition, as it is
  143. * automatically calculated based on the total memory size and
  144. * the sizes of the previous partitions.
  145. */
  146. wl1271_raw_write32(wl, HW_PART3_START_ADDR, p->mem3.start);
  147. }
  148. EXPORT_SYMBOL_GPL(wlcore_set_partition);
  149. void wlcore_select_partition(struct wl1271 *wl, u8 part)
  150. {
  151. wl1271_debug(DEBUG_IO, "setting partition %d", part);
  152. wlcore_set_partition(wl, &wl->ptable[part]);
  153. }
  154. EXPORT_SYMBOL_GPL(wlcore_select_partition);
  155. void wl1271_io_reset(struct wl1271 *wl)
  156. {
  157. if (wl->if_ops->reset)
  158. wl->if_ops->reset(wl->dev);
  159. }
  160. void wl1271_io_init(struct wl1271 *wl)
  161. {
  162. if (wl->if_ops->init)
  163. wl->if_ops->init(wl->dev);
  164. }
  165. void wl1271_top_reg_write(struct wl1271 *wl, int addr, u16 val)
  166. {
  167. /* write address >> 1 + 0x30000 to OCP_POR_CTR */
  168. addr = (addr >> 1) + 0x30000;
  169. wl1271_write32(wl, OCP_POR_CTR, addr);
  170. /* write value to OCP_POR_WDATA */
  171. wl1271_write32(wl, OCP_DATA_WRITE, val);
  172. /* write 1 to OCP_CMD */
  173. wl1271_write32(wl, OCP_CMD, OCP_CMD_WRITE);
  174. }
  175. u16 wl1271_top_reg_read(struct wl1271 *wl, int addr)
  176. {
  177. u32 val;
  178. int timeout = OCP_CMD_LOOP;
  179. /* write address >> 1 + 0x30000 to OCP_POR_CTR */
  180. addr = (addr >> 1) + 0x30000;
  181. wl1271_write32(wl, OCP_POR_CTR, addr);
  182. /* write 2 to OCP_CMD */
  183. wl1271_write32(wl, OCP_CMD, OCP_CMD_READ);
  184. /* poll for data ready */
  185. do {
  186. val = wl1271_read32(wl, OCP_DATA_READ);
  187. } while (!(val & OCP_READY_MASK) && --timeout);
  188. if (!timeout) {
  189. wl1271_warning("Top register access timed out.");
  190. return 0xffff;
  191. }
  192. /* check data status and return if OK */
  193. if ((val & OCP_STATUS_MASK) == OCP_STATUS_OK)
  194. return val & 0xffff;
  195. else {
  196. wl1271_warning("Top register access returned error.");
  197. return 0xffff;
  198. }
  199. }