185f199b7SChia-Wei Wang /*
285f199b7SChia-Wei Wang * Copyright (c) 2023, Aspeed Technology Inc.
385f199b7SChia-Wei Wang *
485f199b7SChia-Wei Wang * SPDX-License-Identifier: BSD-3-Clause
585f199b7SChia-Wei Wang */
685f199b7SChia-Wei Wang
7e3d1bbdbSKevin Chen #include <errno.h>
885f199b7SChia-Wei Wang #include <arch.h>
985f199b7SChia-Wei Wang #include <common/debug.h>
1085f199b7SChia-Wei Wang #include <common/desc_image_load.h>
1185f199b7SChia-Wei Wang #include <drivers/arm/gicv3.h>
1285f199b7SChia-Wei Wang #include <drivers/console.h>
1385f199b7SChia-Wei Wang #include <drivers/ti/uart/uart_16550.h>
14564e073cSChia-Wei Wang #include <lib/mmio.h>
1585f199b7SChia-Wei Wang #include <lib/xlat_tables/xlat_tables_v2.h>
1685f199b7SChia-Wei Wang #include <plat/common/platform.h>
1785f199b7SChia-Wei Wang #include <platform_def.h>
1885f199b7SChia-Wei Wang
1985f199b7SChia-Wei Wang static console_t console;
2085f199b7SChia-Wei Wang
2185f199b7SChia-Wei Wang static entry_point_info_t bl32_ep_info;
2285f199b7SChia-Wei Wang static entry_point_info_t bl33_ep_info;
2385f199b7SChia-Wei Wang
2485f199b7SChia-Wei Wang static uintptr_t rdistif_base_addrs[PLATFORM_CORE_COUNT];
2585f199b7SChia-Wei Wang
plat_mpidr_to_core_pos(u_register_t mpidr)2685f199b7SChia-Wei Wang static unsigned int plat_mpidr_to_core_pos(u_register_t mpidr)
2785f199b7SChia-Wei Wang {
2885f199b7SChia-Wei Wang /* to workaround the return type mismatch */
2985f199b7SChia-Wei Wang return plat_core_pos_by_mpidr(mpidr);
3085f199b7SChia-Wei Wang }
3185f199b7SChia-Wei Wang
3285f199b7SChia-Wei Wang static const gicv3_driver_data_t plat_gic_data = {
3385f199b7SChia-Wei Wang .gicd_base = GICD_BASE,
3485f199b7SChia-Wei Wang .gicr_base = GICR_BASE,
3585f199b7SChia-Wei Wang .rdistif_num = PLATFORM_CORE_COUNT,
3685f199b7SChia-Wei Wang .rdistif_base_addrs = rdistif_base_addrs,
3785f199b7SChia-Wei Wang .mpidr_to_core_pos = plat_mpidr_to_core_pos,
3885f199b7SChia-Wei Wang };
3985f199b7SChia-Wei Wang
4085f199b7SChia-Wei Wang static const mmap_region_t plat_mmap[] = {
4185f199b7SChia-Wei Wang MAP_REGION_FLAT(GICD_BASE, GICD_SIZE,
4285f199b7SChia-Wei Wang MT_DEVICE | MT_RW | MT_SECURE),
4385f199b7SChia-Wei Wang MAP_REGION_FLAT(GICR_BASE, GICR_SIZE,
4485f199b7SChia-Wei Wang MT_DEVICE | MT_RW | MT_SECURE),
4585f199b7SChia-Wei Wang MAP_REGION_FLAT(UART_BASE, PAGE_SIZE,
4685f199b7SChia-Wei Wang MT_DEVICE | MT_RW | MT_SECURE),
4785f199b7SChia-Wei Wang MAP_REGION_FLAT(SCU_CPU_BASE, PAGE_SIZE,
4885f199b7SChia-Wei Wang MT_DEVICE | MT_RW | MT_SECURE),
4985f199b7SChia-Wei Wang { 0 }
5085f199b7SChia-Wei Wang };
5185f199b7SChia-Wei Wang
bl31_early_platform_setup2(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)5285f199b7SChia-Wei Wang void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
5385f199b7SChia-Wei Wang u_register_t arg2, u_register_t arg3)
5485f199b7SChia-Wei Wang {
5585f199b7SChia-Wei Wang console_16550_register(CONSOLE_UART_BASE, CONSOLE_UART_CLKIN_HZ,
5685f199b7SChia-Wei Wang CONSOLE_UART_BAUDRATE, &console);
5785f199b7SChia-Wei Wang
5885f199b7SChia-Wei Wang console_set_scope(&console, CONSOLE_FLAG_BOOT | CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH);
5985f199b7SChia-Wei Wang
60564e073cSChia-Wei Wang SET_PARAM_HEAD(&bl32_ep_info, PARAM_EP, VERSION_2, 0);
61564e073cSChia-Wei Wang bl32_ep_info.pc = BL32_BASE;
62564e073cSChia-Wei Wang SET_SECURITY_STATE(bl32_ep_info.h.attr, SECURE);
63564e073cSChia-Wei Wang
64564e073cSChia-Wei Wang SET_PARAM_HEAD(&bl33_ep_info, PARAM_EP, VERSION_2, 0);
65564e073cSChia-Wei Wang bl33_ep_info.pc = mmio_read_64(SCU_CPU_SMP_EP0);
66564e073cSChia-Wei Wang bl33_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
67564e073cSChia-Wei Wang SET_SECURITY_STATE(bl33_ep_info.h.attr, NON_SECURE);
6885f199b7SChia-Wei Wang }
6985f199b7SChia-Wei Wang
bl31_plat_arch_setup(void)7085f199b7SChia-Wei Wang void bl31_plat_arch_setup(void)
7185f199b7SChia-Wei Wang {
7285f199b7SChia-Wei Wang mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
7385f199b7SChia-Wei Wang BL_CODE_END - BL_CODE_BASE,
7485f199b7SChia-Wei Wang MT_CODE | MT_SECURE);
7585f199b7SChia-Wei Wang
7685f199b7SChia-Wei Wang mmap_add_region(BL_CODE_END, BL_CODE_END,
7785f199b7SChia-Wei Wang BL_END - BL_CODE_END,
7885f199b7SChia-Wei Wang MT_RW_DATA | MT_SECURE);
7985f199b7SChia-Wei Wang
80cef2e925SChia-Wei Wang #if USE_COHERENT_MEM
81cef2e925SChia-Wei Wang mmap_add_region(BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_BASE,
82cef2e925SChia-Wei Wang BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
83cef2e925SChia-Wei Wang MT_DEVICE | MT_RW | MT_SECURE);
84cef2e925SChia-Wei Wang #endif
85cef2e925SChia-Wei Wang
8685f199b7SChia-Wei Wang mmap_add_region(BL32_BASE, BL32_BASE, BL32_SIZE,
8785f199b7SChia-Wei Wang MT_MEMORY | MT_RW);
8885f199b7SChia-Wei Wang
8985f199b7SChia-Wei Wang mmap_add(plat_mmap);
9085f199b7SChia-Wei Wang
9185f199b7SChia-Wei Wang init_xlat_tables();
9285f199b7SChia-Wei Wang
9385f199b7SChia-Wei Wang enable_mmu_el3(0);
9485f199b7SChia-Wei Wang }
9585f199b7SChia-Wei Wang
bl31_platform_setup(void)9685f199b7SChia-Wei Wang void bl31_platform_setup(void)
9785f199b7SChia-Wei Wang {
9885f199b7SChia-Wei Wang gicv3_driver_init(&plat_gic_data);
9985f199b7SChia-Wei Wang gicv3_distif_init();
10085f199b7SChia-Wei Wang gicv3_rdistif_init(plat_my_core_pos());
10185f199b7SChia-Wei Wang gicv3_cpuif_enable(plat_my_core_pos());
10285f199b7SChia-Wei Wang }
10385f199b7SChia-Wei Wang
bl31_plat_get_next_image_ep_info(uint32_t type)10485f199b7SChia-Wei Wang entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
10585f199b7SChia-Wei Wang {
10685f199b7SChia-Wei Wang entry_point_info_t *ep_info;
10785f199b7SChia-Wei Wang
10885f199b7SChia-Wei Wang ep_info = (type == NON_SECURE) ? &bl33_ep_info : &bl32_ep_info;
10985f199b7SChia-Wei Wang
11085f199b7SChia-Wei Wang if (!ep_info->pc) {
11185f199b7SChia-Wei Wang return NULL;
11285f199b7SChia-Wei Wang }
11385f199b7SChia-Wei Wang
11485f199b7SChia-Wei Wang return ep_info;
11585f199b7SChia-Wei Wang }
116e3d1bbdbSKevin Chen
117e3d1bbdbSKevin Chen /*
118e3d1bbdbSKevin Chen * Clock divider/multiplier configuration struct.
119e3d1bbdbSKevin Chen * For H-PLL and M-PLL the formula is
120e3d1bbdbSKevin Chen * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
121e3d1bbdbSKevin Chen * M - Numerator
122e3d1bbdbSKevin Chen * N - Denumerator
123e3d1bbdbSKevin Chen * P - Post Divider
124e3d1bbdbSKevin Chen * They have the same layout in their control register.
125e3d1bbdbSKevin Chen *
126e3d1bbdbSKevin Chen */
127e3d1bbdbSKevin Chen union plat_pll_reg {
128e3d1bbdbSKevin Chen uint32_t w;
129e3d1bbdbSKevin Chen struct {
130e3d1bbdbSKevin Chen uint16_t m : 13; /* bit[12:0] */
131e3d1bbdbSKevin Chen uint8_t n : 6; /* bit[18:13] */
132e3d1bbdbSKevin Chen uint8_t p : 4; /* bit[22:19] */
133e3d1bbdbSKevin Chen uint8_t off : 1; /* bit[23] */
134e3d1bbdbSKevin Chen uint8_t bypass : 1; /* bit[24] */
135e3d1bbdbSKevin Chen uint8_t reset : 1; /* bit[25] */
136e3d1bbdbSKevin Chen uint8_t reserved : 6; /* bit[31:26] */
137e3d1bbdbSKevin Chen } b;
138e3d1bbdbSKevin Chen };
139e3d1bbdbSKevin Chen
plat_get_pll_rate(int pll_idx)140e3d1bbdbSKevin Chen static uint32_t plat_get_pll_rate(int pll_idx)
141e3d1bbdbSKevin Chen {
142e3d1bbdbSKevin Chen union plat_pll_reg pll_reg;
143e3d1bbdbSKevin Chen uint32_t mul = 1, div = 1;
144e3d1bbdbSKevin Chen uint32_t rate = 0;
145e3d1bbdbSKevin Chen
146e3d1bbdbSKevin Chen switch (pll_idx) {
147e3d1bbdbSKevin Chen case PLAT_CLK_HPLL:
148e3d1bbdbSKevin Chen pll_reg.w = mmio_read_32(SCU_CPU_HPLL);
149e3d1bbdbSKevin Chen break;
150e3d1bbdbSKevin Chen case PLAT_CLK_DPLL:
151e3d1bbdbSKevin Chen pll_reg.w = mmio_read_32(SCU_CPU_DPLL);
152e3d1bbdbSKevin Chen break;
153e3d1bbdbSKevin Chen case PLAT_CLK_MPLL:
154e3d1bbdbSKevin Chen pll_reg.w = mmio_read_32(SCU_CPU_MPLL);
155e3d1bbdbSKevin Chen break;
156e3d1bbdbSKevin Chen default:
157e3d1bbdbSKevin Chen ERROR("%s: invalid PSP clock source (%d)\n", __func__, pll_idx);
158e3d1bbdbSKevin Chen return -EINVAL;
159e3d1bbdbSKevin Chen }
160e3d1bbdbSKevin Chen
161e3d1bbdbSKevin Chen if (pll_idx == PLAT_CLK_HPLL && ((mmio_read_32(SCU_CPU_HW_STRAP1) & GENMASK(3, 2)) != 0U)) {
162e3d1bbdbSKevin Chen switch ((mmio_read_32(SCU_CPU_HW_STRAP1) & GENMASK(3, 2)) >> 2) {
163e3d1bbdbSKevin Chen case 1U:
164e3d1bbdbSKevin Chen rate = 1900000000;
165e3d1bbdbSKevin Chen break;
166e3d1bbdbSKevin Chen case 2U:
167e3d1bbdbSKevin Chen rate = 1800000000;
168e3d1bbdbSKevin Chen break;
169e3d1bbdbSKevin Chen case 3U:
170e3d1bbdbSKevin Chen rate = 1700000000;
171e3d1bbdbSKevin Chen break;
172e3d1bbdbSKevin Chen default:
173e3d1bbdbSKevin Chen rate = 2000000000;
174e3d1bbdbSKevin Chen break;
175e3d1bbdbSKevin Chen }
176e3d1bbdbSKevin Chen } else {
177*aa096222SKevin Chen if (pll_reg.b.bypass == 0U) {
178e3d1bbdbSKevin Chen if (pll_idx == PLAT_CLK_MPLL) {
179e3d1bbdbSKevin Chen /* F = 25Mhz * [M / (n + 1)] / (p + 1) */
180e3d1bbdbSKevin Chen mul = (pll_reg.b.m) / ((pll_reg.b.n + 1));
181e3d1bbdbSKevin Chen div = (pll_reg.b.p + 1);
182e3d1bbdbSKevin Chen } else {
183e3d1bbdbSKevin Chen /* F = 25Mhz * [(M + 2) / 2 * (n + 1)] / (p + 1) */
184e3d1bbdbSKevin Chen mul = (pll_reg.b.m + 1) / ((pll_reg.b.n + 1) * 2);
185e3d1bbdbSKevin Chen div = (pll_reg.b.p + 1);
186e3d1bbdbSKevin Chen }
187e3d1bbdbSKevin Chen }
188e3d1bbdbSKevin Chen
189e3d1bbdbSKevin Chen rate = ((CLKIN_25M * mul) / div);
190e3d1bbdbSKevin Chen }
191e3d1bbdbSKevin Chen
192e3d1bbdbSKevin Chen return rate;
193e3d1bbdbSKevin Chen }
194e3d1bbdbSKevin Chen
plat_get_syscnt_freq2(void)195e3d1bbdbSKevin Chen unsigned int plat_get_syscnt_freq2(void)
196e3d1bbdbSKevin Chen {
197e3d1bbdbSKevin Chen if (mmio_read_32(SCU_CPU_HW_STRAP1) & BIT(4)) {
198e3d1bbdbSKevin Chen return plat_get_pll_rate(PLAT_CLK_HPLL);
199e3d1bbdbSKevin Chen } else {
200e3d1bbdbSKevin Chen return plat_get_pll_rate(PLAT_CLK_MPLL);
201e3d1bbdbSKevin Chen }
202e3d1bbdbSKevin Chen }
203