plat_bl31_setup.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2023, Aspeed Technology Inc.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <arch.h>
  8. #include <common/debug.h>
  9. #include <common/desc_image_load.h>
  10. #include <drivers/arm/gicv3.h>
  11. #include <drivers/console.h>
  12. #include <drivers/ti/uart/uart_16550.h>
  13. #include <lib/mmio.h>
  14. #include <lib/xlat_tables/xlat_tables_v2.h>
  15. #include <plat/common/platform.h>
  16. #include <platform_def.h>
  17. static console_t console;
  18. static entry_point_info_t bl32_ep_info;
  19. static entry_point_info_t bl33_ep_info;
  20. static uintptr_t rdistif_base_addrs[PLATFORM_CORE_COUNT];
  21. static unsigned int plat_mpidr_to_core_pos(u_register_t mpidr)
  22. {
  23. /* to workaround the return type mismatch */
  24. return plat_core_pos_by_mpidr(mpidr);
  25. }
  26. static const gicv3_driver_data_t plat_gic_data = {
  27. .gicd_base = GICD_BASE,
  28. .gicr_base = GICR_BASE,
  29. .rdistif_num = PLATFORM_CORE_COUNT,
  30. .rdistif_base_addrs = rdistif_base_addrs,
  31. .mpidr_to_core_pos = plat_mpidr_to_core_pos,
  32. };
  33. static const mmap_region_t plat_mmap[] = {
  34. MAP_REGION_FLAT(GICD_BASE, GICD_SIZE,
  35. MT_DEVICE | MT_RW | MT_SECURE),
  36. MAP_REGION_FLAT(GICR_BASE, GICR_SIZE,
  37. MT_DEVICE | MT_RW | MT_SECURE),
  38. MAP_REGION_FLAT(UART_BASE, PAGE_SIZE,
  39. MT_DEVICE | MT_RW | MT_SECURE),
  40. MAP_REGION_FLAT(SCU_CPU_BASE, PAGE_SIZE,
  41. MT_DEVICE | MT_RW | MT_SECURE),
  42. { 0 }
  43. };
  44. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  45. u_register_t arg2, u_register_t arg3)
  46. {
  47. console_16550_register(CONSOLE_UART_BASE, CONSOLE_UART_CLKIN_HZ,
  48. CONSOLE_UART_BAUDRATE, &console);
  49. console_set_scope(&console, CONSOLE_FLAG_BOOT | CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH);
  50. SET_PARAM_HEAD(&bl32_ep_info, PARAM_EP, VERSION_2, 0);
  51. bl32_ep_info.pc = BL32_BASE;
  52. SET_SECURITY_STATE(bl32_ep_info.h.attr, SECURE);
  53. SET_PARAM_HEAD(&bl33_ep_info, PARAM_EP, VERSION_2, 0);
  54. bl33_ep_info.pc = mmio_read_64(SCU_CPU_SMP_EP0);
  55. bl33_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  56. SET_SECURITY_STATE(bl33_ep_info.h.attr, NON_SECURE);
  57. }
  58. void bl31_plat_arch_setup(void)
  59. {
  60. mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
  61. BL_CODE_END - BL_CODE_BASE,
  62. MT_CODE | MT_SECURE);
  63. mmap_add_region(BL_CODE_END, BL_CODE_END,
  64. BL_END - BL_CODE_END,
  65. MT_RW_DATA | MT_SECURE);
  66. #if USE_COHERENT_MEM
  67. mmap_add_region(BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_BASE,
  68. BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
  69. MT_DEVICE | MT_RW | MT_SECURE);
  70. #endif
  71. mmap_add_region(BL32_BASE, BL32_BASE, BL32_SIZE,
  72. MT_MEMORY | MT_RW);
  73. mmap_add(plat_mmap);
  74. init_xlat_tables();
  75. enable_mmu_el3(0);
  76. }
  77. void bl31_platform_setup(void)
  78. {
  79. gicv3_driver_init(&plat_gic_data);
  80. gicv3_distif_init();
  81. gicv3_rdistif_init(plat_my_core_pos());
  82. gicv3_cpuif_enable(plat_my_core_pos());
  83. }
  84. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
  85. {
  86. entry_point_info_t *ep_info;
  87. ep_info = (type == NON_SECURE) ? &bl33_ep_info : &bl32_ep_info;
  88. if (!ep_info->pc) {
  89. return NULL;
  90. }
  91. return ep_info;
  92. }
  93. /*
  94. * Clock divider/multiplier configuration struct.
  95. * For H-PLL and M-PLL the formula is
  96. * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
  97. * M - Numerator
  98. * N - Denumerator
  99. * P - Post Divider
  100. * They have the same layout in their control register.
  101. *
  102. */
  103. union plat_pll_reg {
  104. uint32_t w;
  105. struct {
  106. uint16_t m : 13; /* bit[12:0] */
  107. uint8_t n : 6; /* bit[18:13] */
  108. uint8_t p : 4; /* bit[22:19] */
  109. uint8_t off : 1; /* bit[23] */
  110. uint8_t bypass : 1; /* bit[24] */
  111. uint8_t reset : 1; /* bit[25] */
  112. uint8_t reserved : 6; /* bit[31:26] */
  113. } b;
  114. };
  115. static uint32_t plat_get_pll_rate(int pll_idx)
  116. {
  117. union plat_pll_reg pll_reg;
  118. uint32_t mul = 1, div = 1;
  119. uint32_t rate = 0;
  120. switch (pll_idx) {
  121. case PLAT_CLK_HPLL:
  122. pll_reg.w = mmio_read_32(SCU_CPU_HPLL);
  123. break;
  124. case PLAT_CLK_DPLL:
  125. pll_reg.w = mmio_read_32(SCU_CPU_DPLL);
  126. break;
  127. case PLAT_CLK_MPLL:
  128. pll_reg.w = mmio_read_32(SCU_CPU_MPLL);
  129. break;
  130. default:
  131. ERROR("%s: invalid PSP clock source (%d)\n", __func__, pll_idx);
  132. return -EINVAL;
  133. }
  134. if (pll_idx == PLAT_CLK_HPLL && ((mmio_read_32(SCU_CPU_HW_STRAP1) & GENMASK(3, 2)) != 0U)) {
  135. switch ((mmio_read_32(SCU_CPU_HW_STRAP1) & GENMASK(3, 2)) >> 2) {
  136. case 1U:
  137. rate = 1900000000;
  138. break;
  139. case 2U:
  140. rate = 1800000000;
  141. break;
  142. case 3U:
  143. rate = 1700000000;
  144. break;
  145. default:
  146. rate = 2000000000;
  147. break;
  148. }
  149. } else {
  150. if (pll_reg.b.bypass == 0U) {
  151. if (pll_idx == PLAT_CLK_MPLL) {
  152. /* F = 25Mhz * [M / (n + 1)] / (p + 1) */
  153. mul = (pll_reg.b.m) / ((pll_reg.b.n + 1));
  154. div = (pll_reg.b.p + 1);
  155. } else {
  156. /* F = 25Mhz * [(M + 2) / 2 * (n + 1)] / (p + 1) */
  157. mul = (pll_reg.b.m + 1) / ((pll_reg.b.n + 1) * 2);
  158. div = (pll_reg.b.p + 1);
  159. }
  160. }
  161. rate = ((CLKIN_25M * mul) / div);
  162. }
  163. return rate;
  164. }
  165. unsigned int plat_get_syscnt_freq2(void)
  166. {
  167. if (mmio_read_32(SCU_CPU_HW_STRAP1) & BIT(4)) {
  168. return plat_get_pll_rate(PLAT_CLK_HPLL);
  169. } else {
  170. return plat_get_pll_rate(PLAT_CLK_MPLL);
  171. }
  172. }