io_win.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2018 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. /* IO Window unit device driver for Marvell AP807, AP807 and AP810 SoCs */
  8. #include <inttypes.h>
  9. #include <stdint.h>
  10. #include <common/debug.h>
  11. #include <drivers/marvell/io_win.h>
  12. #include <lib/mmio.h>
  13. #include <armada_common.h>
  14. #include <mvebu.h>
  15. #include <mvebu_def.h>
  16. #if LOG_LEVEL >= LOG_LEVEL_INFO
  17. #define DEBUG_ADDR_MAP
  18. #endif
  19. /* common defines */
  20. #define WIN_ENABLE_BIT (0x1)
  21. /* Physical address of the base of the window = {Addr[19:0],20`h0} */
  22. #define ADDRESS_SHIFT (20 - 4)
  23. #define ADDRESS_MASK (0xFFFFFFF0)
  24. #define IO_WIN_ALIGNMENT_1M (0x100000)
  25. #define IO_WIN_ALIGNMENT_64K (0x10000)
  26. /* AP registers */
  27. #define IO_WIN_ALR_OFFSET(ap, win) (MVEBU_IO_WIN_BASE(ap) + 0x0 + \
  28. (0x10 * win))
  29. #define IO_WIN_AHR_OFFSET(ap, win) (MVEBU_IO_WIN_BASE(ap) + 0x8 + \
  30. (0x10 * win))
  31. #define IO_WIN_CR_OFFSET(ap, win) (MVEBU_IO_WIN_BASE(ap) + 0xC + \
  32. (0x10 * win))
  33. /* For storage of CR, ALR, AHR abd GCR */
  34. static uint32_t io_win_regs_save[MVEBU_IO_WIN_MAX_WINS * 3 + 1];
  35. static void io_win_check(struct addr_map_win *win)
  36. {
  37. /* for IO The base is always 1M aligned */
  38. /* check if address is aligned to 1M */
  39. if (IS_NOT_ALIGN(win->base_addr, IO_WIN_ALIGNMENT_1M)) {
  40. win->base_addr = ALIGN_UP(win->base_addr, IO_WIN_ALIGNMENT_1M);
  41. NOTICE("%s: Align up the base address to 0x%" PRIx64 "\n",
  42. __func__, win->base_addr);
  43. }
  44. /* size parameter validity check */
  45. if (IS_NOT_ALIGN(win->win_size, IO_WIN_ALIGNMENT_1M)) {
  46. win->win_size = ALIGN_UP(win->win_size, IO_WIN_ALIGNMENT_1M);
  47. NOTICE("%s: Aligning size to 0x%" PRIx64 "\n",
  48. __func__, win->win_size);
  49. }
  50. }
  51. static void io_win_enable_window(int ap_index, struct addr_map_win *win,
  52. uint32_t win_num)
  53. {
  54. uint32_t alr, ahr;
  55. uint64_t end_addr;
  56. if (win->target_id < 0 || win->target_id >= MVEBU_IO_WIN_MAX_WINS) {
  57. ERROR("target ID = %d, is invalid\n", win->target_id);
  58. return;
  59. }
  60. if ((win_num == 0) || (win_num > MVEBU_IO_WIN_MAX_WINS)) {
  61. ERROR("Enabling wrong IOW window %d!\n", win_num);
  62. return;
  63. }
  64. /* calculate the end-address */
  65. end_addr = (win->base_addr + win->win_size - 1);
  66. alr = (uint32_t)((win->base_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
  67. alr |= WIN_ENABLE_BIT;
  68. ahr = (uint32_t)((end_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
  69. /* write start address and end address for IO window */
  70. mmio_write_32(IO_WIN_ALR_OFFSET(ap_index, win_num), alr);
  71. mmio_write_32(IO_WIN_AHR_OFFSET(ap_index, win_num), ahr);
  72. /* write window target */
  73. mmio_write_32(IO_WIN_CR_OFFSET(ap_index, win_num), win->target_id);
  74. }
  75. static void io_win_disable_window(int ap_index, uint32_t win_num)
  76. {
  77. uint32_t win_reg;
  78. if ((win_num == 0) || (win_num > MVEBU_IO_WIN_MAX_WINS)) {
  79. ERROR("Disabling wrong IOW window %d!\n", win_num);
  80. return;
  81. }
  82. win_reg = mmio_read_32(IO_WIN_ALR_OFFSET(ap_index, win_num));
  83. win_reg &= ~WIN_ENABLE_BIT;
  84. mmio_write_32(IO_WIN_ALR_OFFSET(ap_index, win_num), win_reg);
  85. }
  86. /* Insert/Remove temporary window for using the out-of reset default
  87. * CPx base address to access the CP configuration space prior to
  88. * the further base address update in accordance with address mapping
  89. * design.
  90. *
  91. * NOTE: Use the same window array for insertion and removal of
  92. * temporary windows.
  93. */
  94. void iow_temp_win_insert(int ap_index, struct addr_map_win *win, int size)
  95. {
  96. uint32_t win_id;
  97. for (int i = 0; i < size; i++) {
  98. win_id = MVEBU_IO_WIN_MAX_WINS - i - 1;
  99. io_win_check(win);
  100. io_win_enable_window(ap_index, win, win_id);
  101. win++;
  102. }
  103. }
  104. /*
  105. * NOTE: Use the same window array for insertion and removal of
  106. * temporary windows.
  107. */
  108. void iow_temp_win_remove(int ap_index, struct addr_map_win *win, int size)
  109. {
  110. uint32_t win_id;
  111. /* Start from the last window and do not touch Win0 */
  112. for (int i = 0; i < size; i++) {
  113. uint64_t base;
  114. uint32_t target;
  115. win_id = MVEBU_IO_WIN_MAX_WINS - i - 1;
  116. target = mmio_read_32(IO_WIN_CR_OFFSET(ap_index, win_id));
  117. base = mmio_read_32(IO_WIN_ALR_OFFSET(ap_index, win_id));
  118. base &= ~WIN_ENABLE_BIT;
  119. base <<= ADDRESS_SHIFT;
  120. if ((win->target_id != target) || (win->base_addr != base)) {
  121. ERROR("%s: Trying to remove bad window-%d!\n",
  122. __func__, win_id);
  123. continue;
  124. }
  125. io_win_disable_window(ap_index, win_id);
  126. win++;
  127. }
  128. }
  129. #ifdef DEBUG_ADDR_MAP
  130. static void dump_io_win(int ap_index)
  131. {
  132. uint32_t trgt_id, win_id;
  133. uint32_t alr, ahr;
  134. uint64_t start, end;
  135. /* Dump all IO windows */
  136. printf("\tbank target start end\n");
  137. printf("\t----------------------------------------------------\n");
  138. for (win_id = 0; win_id < MVEBU_IO_WIN_MAX_WINS; win_id++) {
  139. alr = mmio_read_32(IO_WIN_ALR_OFFSET(ap_index, win_id));
  140. if (alr & WIN_ENABLE_BIT) {
  141. alr &= ~WIN_ENABLE_BIT;
  142. ahr = mmio_read_32(IO_WIN_AHR_OFFSET(ap_index, win_id));
  143. trgt_id = mmio_read_32(IO_WIN_CR_OFFSET(ap_index,
  144. win_id));
  145. start = ((uint64_t)alr << ADDRESS_SHIFT);
  146. end = (((uint64_t)ahr + 0x10) << ADDRESS_SHIFT);
  147. printf("\tio-win %d 0x%016" PRIx64 " 0x%016" PRIx64 "\n",
  148. trgt_id, start, end);
  149. }
  150. }
  151. printf("\tio-win gcr is %x\n",
  152. mmio_read_32(MVEBU_IO_WIN_BASE(ap_index) +
  153. MVEBU_IO_WIN_GCR_OFFSET));
  154. }
  155. #endif
  156. static void iow_save_win_range(int ap_id, int win_first, int win_last,
  157. uint32_t *buffer)
  158. {
  159. int win_id, idx;
  160. /* Save IOW */
  161. for (idx = 0, win_id = win_first; win_id <= win_last; win_id++) {
  162. buffer[idx++] = mmio_read_32(IO_WIN_CR_OFFSET(ap_id, win_id));
  163. buffer[idx++] = mmio_read_32(IO_WIN_ALR_OFFSET(ap_id, win_id));
  164. buffer[idx++] = mmio_read_32(IO_WIN_AHR_OFFSET(ap_id, win_id));
  165. }
  166. buffer[idx] = mmio_read_32(MVEBU_IO_WIN_BASE(ap_id) +
  167. MVEBU_IO_WIN_GCR_OFFSET);
  168. }
  169. static void iow_restore_win_range(int ap_id, int win_first, int win_last,
  170. uint32_t *buffer)
  171. {
  172. int win_id, idx;
  173. /* Restore IOW */
  174. for (idx = 0, win_id = win_first; win_id <= win_last; win_id++) {
  175. mmio_write_32(IO_WIN_CR_OFFSET(ap_id, win_id), buffer[idx++]);
  176. mmio_write_32(IO_WIN_ALR_OFFSET(ap_id, win_id), buffer[idx++]);
  177. mmio_write_32(IO_WIN_AHR_OFFSET(ap_id, win_id), buffer[idx++]);
  178. }
  179. mmio_write_32(MVEBU_IO_WIN_BASE(ap_id) + MVEBU_IO_WIN_GCR_OFFSET,
  180. buffer[idx++]);
  181. }
  182. void iow_save_win_all(int ap_id)
  183. {
  184. iow_save_win_range(ap_id, 0, MVEBU_IO_WIN_MAX_WINS - 1,
  185. io_win_regs_save);
  186. }
  187. void iow_restore_win_all(int ap_id)
  188. {
  189. iow_restore_win_range(ap_id, 0, MVEBU_IO_WIN_MAX_WINS - 1,
  190. io_win_regs_save);
  191. }
  192. int init_io_win(int ap_index)
  193. {
  194. struct addr_map_win *win;
  195. uint32_t win_id, win_reg;
  196. uint32_t win_count;
  197. INFO("Initializing IO WIN Address decoding\n");
  198. /* Get the array of the windows and its size */
  199. marvell_get_io_win_memory_map(ap_index, &win, &win_count);
  200. if (win_count <= 0)
  201. INFO("no windows configurations found\n");
  202. if (win_count > MVEBU_IO_WIN_MAX_WINS) {
  203. INFO("number of windows is bigger than %d\n",
  204. MVEBU_IO_WIN_MAX_WINS);
  205. return 0;
  206. }
  207. /* Get the default target id to set the GCR */
  208. win_reg = marvell_get_io_win_gcr_target(ap_index);
  209. mmio_write_32(MVEBU_IO_WIN_BASE(ap_index) + MVEBU_IO_WIN_GCR_OFFSET,
  210. win_reg);
  211. /* disable all IO windows */
  212. for (win_id = 1; win_id < MVEBU_IO_WIN_MAX_WINS; win_id++)
  213. io_win_disable_window(ap_index, win_id);
  214. /* enable relevant windows, starting from win_id = 1 because
  215. * index 0 dedicated for BootROM
  216. */
  217. for (win_id = 1; win_id <= win_count; win_id++, win++) {
  218. io_win_check(win);
  219. io_win_enable_window(ap_index, win, win_id);
  220. }
  221. #ifdef DEBUG_ADDR_MAP
  222. dump_io_win(ap_index);
  223. #endif
  224. INFO("Done IO WIN Address decoding Initializing\n");
  225. return 0;
  226. }