blob: a4d3cfe96d658e4317204c9608320f890dbab00d [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2018 MediaTek Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*/
23
24#include <arch.h>
25#include <arch/arm64/mmu.h>
26#include <arch/ops.h>
27#include <assert.h>
28#include <debug.h>
29#include <dev/timer/arm_generic.h>
30#include <dev/uart.h>
31#include <err.h>
32#include <kernel/vm.h>
33#include <platform.h>
34#include <platform/mt8518.h>
35#include <platform/mt_gic_v3.h>
36#include <platform/pll.h>
37#include <platform/dramc_api.h>
38#include <lib/mempool.h>
39#if WITH_MTK_PMIC_WRAP_AND_PMIC
40#include <platform/pmic_wrap_init.h>
41#include <platform/pmic.h>
42#endif
43#if WITH_PMIC_MT6395
44#include <platform/pmic_6395.h>
45#endif
46#if WITH_VCORE_PWM_BUCK
47#include <platform/pwm-buck.h>
48#endif
49#if WITH_VCORE_I2C_BUCK
50#include <platform/rt5748.h>
51#endif
52#ifdef MTK_TINYSYS_SCP_SUPPORT
53#include <platform/mt_scp.h>
54#endif
55#ifdef WITH_BOOTMODE_HANDSHAKE
56#include <platform/usb_toolhandler.h>
57#endif
58#include <platform/pwm.h>
59
60#if WITH_KERNEL_VM
61#define L2C_MAPPING_IDX 0
62#define PERIPHERAL_MAPPING_IDX 1
63#define ICVERSION_MAPPING_IDX 2
64#define GIC_MAPPING_IDX 3
65#define SRAM_MAPPING_IDX 4
66#define DRAM_MAPPING_IDX 5
67
68#include <platform/mtk_wdt.h>
69
70/* initial memory mappings. parsed by start.S */
71struct mmu_initial_mapping mmu_initial_mappings[] = {
72 {
73 .phys = MEMORY_BASE_PHYS,
74 .virt = MEMORY_BASE_VIRT,
75 .size = MEMORY_APERTURE_SIZE,
76 .flags = 0,
77 .name = "l2c"
78 },
79 {
80 .phys = PERIPHERAL_BASE_PHYS,
81 .virt = PERIPHERAL_BASE_VIRT,
82 .size = PERIPHERAL_BASE_SIZE,
83 .flags = MMU_INITIAL_MAPPING_FLAG_DEVICE,
84 .name = "peripherals"
85 },
86 {
87 .phys = VERSION_BASE_PHYS,
88 .virt = VERSION_BASE_VIRT,
89 .size = VERSION_BASE_SIZE,
90 .flags = MMU_INITIAL_MAPPING_FLAG_DEVICE,
91 .name = "icversion"
92 },
93 {
94 .phys = GIC_BASE_PHYS,
95 .virt = GIC_BASE_VIRT,
96 .size = GIC_BASE_SIZE,
97 .flags = MMU_INITIAL_MAPPING_FLAG_DEVICE,
98 .name = "gic"
99 },
100 /* reserved for internal sram */
101 { 0 },
102 /* reserved for dram */
103 { 0 },
104 /* null entry to terminate the list */
105 { 0 }
106};
107
108static pmm_arena_t arena = {
109 .name = "sdram",
110 .base = SRAM_BASE_PHYS,
111 .size = SRAM_BASE_SIZE,
112 .flags = PMM_ARENA_FLAG_KMAP,
113};
114
115/* only enable el1 dcache */
116static void dcache_enable(void)
117{
118 uint32_t sctlr;
119
120 asm volatile("mrs %0, sctlr_el1" : "=r" (sctlr) : : "cc");
121 asm volatile("msr sctlr_el1, %0" : : "r" (sctlr | (1 << 2)) : "cc");
122 asm volatile("isb");
123}
124
125uint32_t lk_dram_sz = 0x10000000; //get_dram_size();;
126void *dram_map(paddr_t pa)
127{
128 paddr_t dram_phy = DRAM_BASE_PHY;
129
130 if (pa >= dram_phy && pa <= (dram_phy + lk_dram_sz - 1)) {
131 return (void *)(DRAM_BASE_VIRT + (pa - dram_phy));
132 }
133
134 return NULL;
135}
136
137#endif /* WITH_KERNEL_VM */
138
139void platform_early_init(void)
140{
141 uart_init_early();
142
143 /* initialize the interrupt controller */
144 arm_gic_init();
145
146 arm_generic_timer_init(ARM_GENERIC_TIMER_PHYSICAL_INT, 13000000);
147
148 mtk_wdt_init();
149
150#if WITH_KERNEL_VM
151 arch_disable_cache(DCACHE);
152#endif
153
154#if !(FPGA_PLATFORM)
155 mt_pll_init();
156#endif
157
158 pwm_init();
159
160#if WITH_MTK_PMIC_WRAP_AND_PMIC
161 pwrap_init();
162 pmic_init();
163#endif
164
165#if WITH_PMIC_MT6395
166 pmic_init_mt6395();
167#endif
168
169#if WITH_VCORE_PWM_BUCK
170 pwm_buck_init();
171#endif
172
173#if WITH_VCORE_I2C_BUCK
174 rt5748_init();
175#endif
176
177#if !(FPGA_PLATFORM)
178 /* mt_pll_post_init should be invoked after pmic_init */
179 mt_pll_post_init();
180#endif
181
182 /* check DDR-reserve mode */
183 check_ddr_reserve_status();
184
185#if !(FPGA_PLATFORM)
186 mt_mem_init();
187#endif
188
189#if WITH_KERNEL_VM
190 dcache_enable();
191
192/* mapping dram to cacheable memory */
193#if FPGA_PLATFORM
194 arch_mmu_map(DRAM_BASE_VIRT, DRAM_BASE_PHY, 0x10000000 >> PAGE_SIZE_SHIFT, 0);
195#else
196 arch_mmu_map(DRAM_BASE_VIRT, DRAM_BASE_PHY, 0x20000000 >> PAGE_SIZE_SHIFT, 0);
197#endif
198 /* add DRAM to mmu_initial_mappings for physical-to-virtual translation */
199 mmu_initial_mappings[DRAM_MAPPING_IDX].phys = DRAM_BASE_PHY;
200 mmu_initial_mappings[DRAM_MAPPING_IDX].virt = DRAM_BASE_VIRT;
201#if FPGA_PLATFORM
202 mmu_initial_mappings[DRAM_MAPPING_IDX].size = 0x10000000;
203#else
204 mmu_initial_mappings[DRAM_MAPPING_IDX].size = 0x20000000;//get_dram_size();
205#endif
206 mmu_initial_mappings[DRAM_MAPPING_IDX].flags = 0;
207 mmu_initial_mappings[DRAM_MAPPING_IDX].name = "dram";
208
209 /* mapping internel sram to cacheable memory */
210 arch_mmu_map(SRAM_BASE_VIRT, SRAM_BASE_PHYS, SRAM_BASE_SIZE >> PAGE_SIZE_SHIFT, 0);
211 /* add intrenal sram to mmu_initial_mappings for heap */
212 mmu_initial_mappings[SRAM_MAPPING_IDX].phys = SRAM_BASE_PHYS;
213 mmu_initial_mappings[SRAM_MAPPING_IDX].virt = SRAM_BASE_VIRT;
214 mmu_initial_mappings[SRAM_MAPPING_IDX].size = SRAM_BASE_SIZE;
215 mmu_initial_mappings[SRAM_MAPPING_IDX].flags = 0;
216 mmu_initial_mappings[SRAM_MAPPING_IDX].name = "sram";
217
218 pmm_add_arena(&arena);
219#endif
220 /* set all gpio smt enabled */
221 writel(0xffff, GPIO_BASE + 0Xa00);
222 writel(0xffff, GPIO_BASE + 0xa10);
223 writel(0xffff, GPIO_BASE + 0xa20);
224 writel(0xffff, GPIO_BASE + 0xa30);
225}
226
227void platform_init(void)
228{
229 int ret;
230 ret = mempool_init((void *)CACHED_MEMPOOL_ADDR, CACHED_MEMPOOL_SIZE,
231 MEMPOOL_CACHE);
232 if (ret != NO_ERROR)
233 platform_halt(HALT_ACTION_REBOOT, HALT_REASON_SW_PANIC);
234
235}
236
237extern __WEAK bool plat_fixup_hook(void* bootimg_dtb_load, ...);
238bool plat_fixup_hook(void* bootimg_dtb_load, ...)
239{
240#ifdef MTK_TINYSYS_SCP_SUPPORT
241 if (load_scpsys() < 0)
242 dprintf(ALWAYS, "load scp fail\n");
243#endif
244
245#ifdef WITH_BOOTMODE_HANDSHAKE
246 bool ret= tool_usb_handshake( bootimg_dtb_load );
247 return ret;
248#endif
249
250 return true;
251}
252
253/* Initialization context in start.S before switching from EL3 to EL1.
254 * Note data/bss segment NOT initialized, i.e. No assumption on global variable initialization.*/
255extern void mt_clear_sec_pol_ctl_en(void);
256void platform_el3_init(void)
257{
258 mt_gic_el3_setup();
259}