gwin.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (C) 2018 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. /* GWIN unit device driver for Marvell AP810 SoC */
  8. #include <inttypes.h>
  9. #include <stdint.h>
  10. #include <common/debug.h>
  11. #include <drivers/marvell/gwin.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. #define WIN_TARGET_MASK (0xF)
  22. #define WIN_TARGET_SHIFT (0x8)
  23. #define WIN_TARGET(tgt) (((tgt) & WIN_TARGET_MASK) \
  24. << WIN_TARGET_SHIFT)
  25. /* Bits[43:26] of the physical address are the window base,
  26. * which is aligned to 64MB
  27. */
  28. #define ADDRESS_RSHIFT (26)
  29. #define ADDRESS_LSHIFT (10)
  30. #define GWIN_ALIGNMENT_64M (0x4000000)
  31. /* AP registers */
  32. #define GWIN_CR_OFFSET(ap, win) (MVEBU_GWIN_BASE(ap) + 0x0 + \
  33. (0x10 * (win)))
  34. #define GWIN_ALR_OFFSET(ap, win) (MVEBU_GWIN_BASE(ap) + 0x8 + \
  35. (0x10 * (win)))
  36. #define GWIN_AHR_OFFSET(ap, win) (MVEBU_GWIN_BASE(ap) + 0xc + \
  37. (0x10 * (win)))
  38. #define CCU_GRU_CR_OFFSET(ap) (MVEBU_CCU_GRU_BASE(ap))
  39. #define CCR_GRU_CR_GWIN_MBYPASS (1 << 1)
  40. static void gwin_check(struct addr_map_win *win)
  41. {
  42. /* The base is always 64M aligned */
  43. if (IS_NOT_ALIGN(win->base_addr, GWIN_ALIGNMENT_64M)) {
  44. win->base_addr &= ~(GWIN_ALIGNMENT_64M - 1);
  45. NOTICE("%s: Align the base address to 0x%" PRIx64 "\n",
  46. __func__, win->base_addr);
  47. }
  48. /* size parameter validity check */
  49. if (IS_NOT_ALIGN(win->win_size, GWIN_ALIGNMENT_64M)) {
  50. win->win_size = ALIGN_UP(win->win_size, GWIN_ALIGNMENT_64M);
  51. NOTICE("%s: Aligning window size to 0x%" PRIx64 "\n",
  52. __func__, win->win_size);
  53. }
  54. }
  55. static void gwin_enable_window(int ap_index, struct addr_map_win *win,
  56. uint32_t win_num)
  57. {
  58. uint32_t alr, ahr;
  59. uint64_t end_addr;
  60. if ((win->target_id & WIN_TARGET_MASK) != win->target_id) {
  61. ERROR("target ID = %d, is invalid\n", win->target_id);
  62. return;
  63. }
  64. /* calculate 64bit end-address */
  65. end_addr = (win->base_addr + win->win_size - 1);
  66. alr = (uint32_t)((win->base_addr >> ADDRESS_RSHIFT) << ADDRESS_LSHIFT);
  67. ahr = (uint32_t)((end_addr >> ADDRESS_RSHIFT) << ADDRESS_LSHIFT);
  68. /* write start address and end address for GWIN */
  69. mmio_write_32(GWIN_ALR_OFFSET(ap_index, win_num), alr);
  70. mmio_write_32(GWIN_AHR_OFFSET(ap_index, win_num), ahr);
  71. /* write the target ID and enable the window */
  72. mmio_write_32(GWIN_CR_OFFSET(ap_index, win_num),
  73. WIN_TARGET(win->target_id) | WIN_ENABLE_BIT);
  74. }
  75. static void gwin_disable_window(int ap_index, uint32_t win_num)
  76. {
  77. uint32_t win_reg;
  78. win_reg = mmio_read_32(GWIN_CR_OFFSET(ap_index, win_num));
  79. win_reg &= ~WIN_ENABLE_BIT;
  80. mmio_write_32(GWIN_CR_OFFSET(ap_index, win_num), win_reg);
  81. }
  82. /* Insert/Remove temporary window for using the out-of reset default
  83. * CPx base address to access the CP configuration space prior to
  84. * the further base address update in accordance with address mapping
  85. * design.
  86. *
  87. * NOTE: Use the same window array for insertion and removal of
  88. * temporary windows.
  89. */
  90. void gwin_temp_win_insert(int ap_index, struct addr_map_win *win, int size)
  91. {
  92. uint32_t win_id;
  93. for (int i = 0; i < size; i++) {
  94. win_id = MVEBU_GWIN_MAX_WINS - i - 1;
  95. gwin_check(win);
  96. gwin_enable_window(ap_index, win, win_id);
  97. win++;
  98. }
  99. }
  100. /*
  101. * NOTE: Use the same window array for insertion and removal of
  102. * temporary windows.
  103. */
  104. void gwin_temp_win_remove(int ap_index, struct addr_map_win *win, int size)
  105. {
  106. uint32_t win_id;
  107. for (int i = 0; i < size; i++) {
  108. uint64_t base;
  109. uint32_t target;
  110. win_id = MVEBU_GWIN_MAX_WINS - i - 1;
  111. target = mmio_read_32(GWIN_CR_OFFSET(ap_index, win_id));
  112. target >>= WIN_TARGET_SHIFT;
  113. target &= WIN_TARGET_MASK;
  114. base = mmio_read_32(GWIN_ALR_OFFSET(ap_index, win_id));
  115. base >>= ADDRESS_LSHIFT;
  116. base <<= ADDRESS_RSHIFT;
  117. if (win->target_id != target) {
  118. ERROR("%s: Trying to remove bad window-%d!\n",
  119. __func__, win_id);
  120. continue;
  121. }
  122. gwin_disable_window(ap_index, win_id);
  123. win++;
  124. }
  125. }
  126. #ifdef DEBUG_ADDR_MAP
  127. static void dump_gwin(int ap_index)
  128. {
  129. uint32_t win_num;
  130. /* Dump all GWIN windows */
  131. printf("\tbank target start end\n");
  132. printf("\t----------------------------------------------------\n");
  133. for (win_num = 0; win_num < MVEBU_GWIN_MAX_WINS; win_num++) {
  134. uint32_t cr;
  135. uint64_t alr, ahr;
  136. cr = mmio_read_32(GWIN_CR_OFFSET(ap_index, win_num));
  137. /* Window enabled */
  138. if (cr & WIN_ENABLE_BIT) {
  139. alr = mmio_read_32(GWIN_ALR_OFFSET(ap_index, win_num));
  140. alr = (alr >> ADDRESS_LSHIFT) << ADDRESS_RSHIFT;
  141. ahr = mmio_read_32(GWIN_AHR_OFFSET(ap_index, win_num));
  142. ahr = (ahr >> ADDRESS_LSHIFT) << ADDRESS_RSHIFT;
  143. printf("\tgwin %d 0x%016" PRIx64 " 0x%016" PRIx64 "\n",
  144. (cr >> 8) & 0xF, alr, ahr);
  145. }
  146. }
  147. }
  148. #endif
  149. int init_gwin(int ap_index)
  150. {
  151. struct addr_map_win *win;
  152. uint32_t win_id;
  153. uint32_t win_count;
  154. uint32_t win_reg;
  155. INFO("Initializing GWIN Address decoding\n");
  156. /* Get the array of the windows and its size */
  157. marvell_get_gwin_memory_map(ap_index, &win, &win_count);
  158. if (win_count <= 0) {
  159. INFO("no windows configurations found\n");
  160. return 0;
  161. }
  162. if (win_count > MVEBU_GWIN_MAX_WINS) {
  163. ERROR("number of windows is bigger than %d\n",
  164. MVEBU_GWIN_MAX_WINS);
  165. return 0;
  166. }
  167. /* disable all windows */
  168. for (win_id = 0; win_id < MVEBU_GWIN_MAX_WINS; win_id++)
  169. gwin_disable_window(ap_index, win_id);
  170. /* enable relevant windows */
  171. for (win_id = 0; win_id < win_count; win_id++, win++) {
  172. gwin_check(win);
  173. gwin_enable_window(ap_index, win, win_id);
  174. }
  175. /* GWIN Miss feature has not verified, therefore any access towards
  176. * remote AP should be accompanied with proper configuration to
  177. * GWIN registers group and therefore the GWIN Miss feature
  178. * should be set into Bypass mode, need to make sure all GWIN regions
  179. * are defined correctly that will assure no GWIN miss occurrence
  180. * JIRA-AURORA2-1630
  181. */
  182. INFO("Update GWIN miss bypass\n");
  183. win_reg = mmio_read_32(CCU_GRU_CR_OFFSET(ap_index));
  184. win_reg |= CCR_GRU_CR_GWIN_MBYPASS;
  185. mmio_write_32(CCU_GRU_CR_OFFSET(ap_index), win_reg);
  186. #ifdef DEBUG_ADDR_MAP
  187. dump_gwin(ap_index);
  188. #endif
  189. INFO("Done GWIN Address decoding Initializing\n");
  190. return 0;
  191. }