iob.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2016 - 2018 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. /* IOW unit device driver for Marvell CP110 and CP115 SoCs */
  8. #include <inttypes.h>
  9. #include <stdint.h>
  10. #include <arch_helpers.h>
  11. #include <common/debug.h>
  12. #include <drivers/marvell/iob.h>
  13. #include <lib/mmio.h>
  14. #include <armada_common.h>
  15. #include <mvebu.h>
  16. #include <mvebu_def.h>
  17. #if LOG_LEVEL >= LOG_LEVEL_INFO
  18. #define DEBUG_ADDR_MAP
  19. #endif
  20. #define MVEBU_IOB_OFFSET (0x190000)
  21. #define MVEBU_IOB_MAX_WINS 16
  22. /* common defines */
  23. #define WIN_ENABLE_BIT (0x1)
  24. /* Physical address of the base of the window = {AddrLow[19:0],20`h0} */
  25. #define ADDRESS_SHIFT (20 - 4)
  26. #define ADDRESS_MASK (0xFFFFFFF0)
  27. #define IOB_WIN_ALIGNMENT (0x100000)
  28. /* IOB registers */
  29. #define IOB_WIN_CR_OFFSET(win) (iob_base + 0x0 + (0x20 * win))
  30. #define IOB_TARGET_ID_OFFSET (8)
  31. #define IOB_TARGET_ID_MASK (0xF)
  32. #define IOB_WIN_SCR_OFFSET(win) (iob_base + 0x4 + (0x20 * win))
  33. #define IOB_WIN_ENA_CTRL_WRITE_SECURE (0x1)
  34. #define IOB_WIN_ENA_CTRL_READ_SECURE (0x2)
  35. #define IOB_WIN_ENA_WRITE_SECURE (0x4)
  36. #define IOB_WIN_ENA_READ_SECURE (0x8)
  37. #define IOB_WIN_ALR_OFFSET(win) (iob_base + 0x8 + (0x20 * win))
  38. #define IOB_WIN_AHR_OFFSET(win) (iob_base + 0xC + (0x20 * win))
  39. #define IOB_WIN_DIOB_CR_OFFSET(win) (iob_base + 0x10 + (0x20 * win))
  40. #define IOB_WIN_XOR0_DIOB_EN BIT(0)
  41. #define IOB_WIN_XOR1_DIOB_EN BIT(1)
  42. uintptr_t iob_base;
  43. static void iob_win_check(struct addr_map_win *win, uint32_t win_num)
  44. {
  45. /* check if address is aligned to the size */
  46. if (IS_NOT_ALIGN(win->base_addr, IOB_WIN_ALIGNMENT)) {
  47. win->base_addr = ALIGN_UP(win->base_addr, IOB_WIN_ALIGNMENT);
  48. ERROR("Window %d: base address unaligned to 0x%x\n",
  49. win_num, IOB_WIN_ALIGNMENT);
  50. printf("Align up the base address to 0x%" PRIx64 "\n",
  51. win->base_addr);
  52. }
  53. /* size parameter validity check */
  54. if (IS_NOT_ALIGN(win->win_size, IOB_WIN_ALIGNMENT)) {
  55. win->win_size = ALIGN_UP(win->win_size, IOB_WIN_ALIGNMENT);
  56. ERROR("Window %d: window size unaligned to 0x%x\n", win_num,
  57. IOB_WIN_ALIGNMENT);
  58. printf("Aligning size to 0x%" PRIx64 "\n", win->win_size);
  59. }
  60. }
  61. static void iob_enable_win(struct addr_map_win *win, uint32_t win_id)
  62. {
  63. uint32_t iob_win_reg;
  64. uint32_t alr, ahr;
  65. uint64_t end_addr;
  66. uint32_t reg_en;
  67. /* move XOR (DMA) to use WIN1 which is used for PCI-EP address space */
  68. reg_en = IOB_WIN_XOR0_DIOB_EN | IOB_WIN_XOR1_DIOB_EN;
  69. iob_win_reg = mmio_read_32(IOB_WIN_DIOB_CR_OFFSET(0));
  70. iob_win_reg &= ~reg_en;
  71. mmio_write_32(IOB_WIN_DIOB_CR_OFFSET(0), iob_win_reg);
  72. iob_win_reg = mmio_read_32(IOB_WIN_DIOB_CR_OFFSET(1));
  73. iob_win_reg |= reg_en;
  74. mmio_write_32(IOB_WIN_DIOB_CR_OFFSET(1), iob_win_reg);
  75. end_addr = (win->base_addr + win->win_size - 1);
  76. alr = (uint32_t)((win->base_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
  77. ahr = (uint32_t)((end_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
  78. mmio_write_32(IOB_WIN_ALR_OFFSET(win_id), alr);
  79. mmio_write_32(IOB_WIN_AHR_OFFSET(win_id), ahr);
  80. iob_win_reg = WIN_ENABLE_BIT;
  81. iob_win_reg |= (win->target_id & IOB_TARGET_ID_MASK)
  82. << IOB_TARGET_ID_OFFSET;
  83. mmio_write_32(IOB_WIN_CR_OFFSET(win_id), iob_win_reg);
  84. }
  85. #ifdef DEBUG_ADDR_MAP
  86. static void dump_iob(void)
  87. {
  88. uint32_t win_id, win_cr, alr, ahr;
  89. uint8_t target_id;
  90. uint64_t start, end;
  91. char *iob_target_name[IOB_MAX_TID] = {
  92. "CFG ", "MCI0 ", "PEX1 ", "PEX2 ",
  93. "PEX0 ", "NAND ", "RUNIT", "MCI1 " };
  94. /* Dump all IOB windows */
  95. printf("bank id target start end\n");
  96. printf("----------------------------------------------------\n");
  97. for (win_id = 0; win_id < MVEBU_IOB_MAX_WINS; win_id++) {
  98. win_cr = mmio_read_32(IOB_WIN_CR_OFFSET(win_id));
  99. if (win_cr & WIN_ENABLE_BIT) {
  100. target_id = (win_cr >> IOB_TARGET_ID_OFFSET) &
  101. IOB_TARGET_ID_MASK;
  102. alr = mmio_read_32(IOB_WIN_ALR_OFFSET(win_id));
  103. start = ((uint64_t)alr << ADDRESS_SHIFT);
  104. if (win_id != 0) {
  105. ahr = mmio_read_32(IOB_WIN_AHR_OFFSET(win_id));
  106. end = (((uint64_t)ahr + 0x10) << ADDRESS_SHIFT);
  107. } else {
  108. /* Window #0 size is hardcoded to 16MB, as it's
  109. * reserved for CP configuration space.
  110. */
  111. end = start + (16 << 20);
  112. }
  113. printf("iob %02d %s 0x%016" PRIx64 " 0x%016" PRIx64 "\n",
  114. win_id, iob_target_name[target_id],
  115. start, end);
  116. }
  117. }
  118. }
  119. #endif
  120. void iob_cfg_space_update(int ap_idx, int cp_idx, uintptr_t base,
  121. uintptr_t new_base)
  122. {
  123. debug_enter();
  124. iob_base = base + MVEBU_IOB_OFFSET;
  125. NOTICE("Change the base address of AP%d-CP%d to %lx\n",
  126. ap_idx, cp_idx, new_base);
  127. mmio_write_32(IOB_WIN_ALR_OFFSET(0), new_base >> ADDRESS_SHIFT);
  128. iob_base = new_base + MVEBU_IOB_OFFSET;
  129. /* Make sure the address was configured by the CPU before
  130. * any possible access to the CP.
  131. */
  132. dsb();
  133. debug_exit();
  134. }
  135. int init_iob(uintptr_t base)
  136. {
  137. struct addr_map_win *win;
  138. uint32_t win_id, win_reg;
  139. uint32_t win_count;
  140. INFO("Initializing IOB Address decoding\n");
  141. /* Get the base address of the address decoding MBUS */
  142. iob_base = base + MVEBU_IOB_OFFSET;
  143. /* Get the array of the windows and fill the map data */
  144. marvell_get_iob_memory_map(&win, &win_count, base);
  145. if (win_count <= 0) {
  146. INFO("no windows configurations found\n");
  147. return 0;
  148. } else if (win_count > (MVEBU_IOB_MAX_WINS - 1)) {
  149. ERROR("IOB mem map array > than max available windows (%d)\n",
  150. MVEBU_IOB_MAX_WINS);
  151. win_count = MVEBU_IOB_MAX_WINS;
  152. }
  153. /* disable all IOB windows, start from win_id = 1
  154. * because can't disable internal register window
  155. */
  156. for (win_id = 1; win_id < MVEBU_IOB_MAX_WINS; win_id++) {
  157. win_reg = mmio_read_32(IOB_WIN_CR_OFFSET(win_id));
  158. win_reg &= ~WIN_ENABLE_BIT;
  159. mmio_write_32(IOB_WIN_CR_OFFSET(win_id), win_reg);
  160. win_reg = ~IOB_WIN_ENA_CTRL_WRITE_SECURE;
  161. win_reg &= ~IOB_WIN_ENA_CTRL_READ_SECURE;
  162. win_reg &= ~IOB_WIN_ENA_WRITE_SECURE;
  163. win_reg &= ~IOB_WIN_ENA_READ_SECURE;
  164. mmio_write_32(IOB_WIN_SCR_OFFSET(win_id), win_reg);
  165. }
  166. for (win_id = 1; win_id < win_count + 1; win_id++, win++) {
  167. iob_win_check(win, win_id);
  168. iob_enable_win(win, win_id);
  169. }
  170. #ifdef DEBUG_ADDR_MAP
  171. dump_iob();
  172. #endif
  173. INFO("Done IOB Address decoding Initializing\n");
  174. return 0;
  175. }