blob: a92857659102dd921cc2b6872d9a03c1f08ef273 [file] [log] [blame]
Haojian Zhuang602362d2017-06-01 12:15:14 +08001/*
Haojian Zhuang1b4b4122018-01-25 16:13:05 +08002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Haojian Zhuang602362d2017-06-01 12:15:14 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch_helpers.h>
8#include <arm_gic.h>
9#include <assert.h>
10#include <bl_common.h>
11#include <console.h>
12#include <debug.h>
13#include <delay_timer.h>
14#include <dw_ufs.h>
15#include <errno.h>
Isla Mitchelle3631462017-07-14 10:46:32 +010016#include <generic_delay_timer.h>
Haojian Zhuang602362d2017-06-01 12:15:14 +080017#include <gicv2.h>
18#include <hi3660.h>
19#include <mmio.h>
Haojian Zhuang602362d2017-06-01 12:15:14 +080020#include <platform.h>
21#include <platform_def.h>
22#include <string.h>
23#include <tbbr/tbbr_img_desc.h>
24#include <ufs.h>
25
26#include "../../bl1/bl1_private.h"
27#include "hikey960_def.h"
28#include "hikey960_private.h"
29
30enum {
31 BOOT_MODE_RECOVERY = 0,
32 BOOT_MODE_NORMAL,
33 BOOT_MODE_MASK = 1,
34};
35
36/*
37 * Declarations of linker defined symbols which will help us find the layout
38 * of trusted RAM
39 */
Haojian Zhuang602362d2017-06-01 12:15:14 +080040
41/* Data structure which holds the extents of the trusted RAM for BL1 */
42static meminfo_t bl1_tzram_layout;
43
44/******************************************************************************
45 * On a GICv2 system, the Group 1 secure interrupts are treated as Group 0
46 * interrupts.
47 *****************************************************************************/
48const unsigned int g0_interrupt_array[] = {
49 IRQ_SEC_PHY_TIMER,
50 IRQ_SEC_SGI_0
51};
52
53const gicv2_driver_data_t hikey960_gic_data = {
54 .gicd_base = GICD_REG_BASE,
55 .gicc_base = GICC_REG_BASE,
56 .g0_interrupt_num = ARRAY_SIZE(g0_interrupt_array),
57 .g0_interrupt_array = g0_interrupt_array,
58};
59
60meminfo_t *bl1_plat_sec_mem_layout(void)
61{
62 return &bl1_tzram_layout;
63}
64
Victor Chong2d9a42d2017-08-17 15:21:10 +090065/*******************************************************************************
66 * Function that takes a memory layout into which BL2 has been loaded and
67 * populates a new memory layout for BL2 that ensures that BL1's data sections
68 * resident in secure RAM are not visible to BL2.
69 ******************************************************************************/
70void bl1_init_bl2_mem_layout(const meminfo_t *bl1_mem_layout,
71 meminfo_t *bl2_mem_layout)
72{
73
74 assert(bl1_mem_layout != NULL);
75 assert(bl2_mem_layout != NULL);
76
77 /*
78 * Cannot remove BL1 RW data from the scope of memory visible to BL2
79 * like arm platforms because they overlap in hikey960
80 */
81 bl2_mem_layout->total_base = BL2_BASE;
82 bl2_mem_layout->total_size = NS_BL1U_LIMIT - BL2_BASE;
83
84 flush_dcache_range((unsigned long)bl2_mem_layout, sizeof(meminfo_t));
85}
Victor Chong2d9a42d2017-08-17 15:21:10 +090086
Haojian Zhuang602362d2017-06-01 12:15:14 +080087/*
88 * Perform any BL1 specific platform actions.
89 */
90void bl1_early_platform_setup(void)
91{
Haojian Zhuang602362d2017-06-01 12:15:14 +080092 unsigned int id, uart_base;
93
94 generic_delay_timer_init();
95 hikey960_read_boardid(&id);
96 if (id == 5300)
97 uart_base = PL011_UART5_BASE;
98 else
99 uart_base = PL011_UART6_BASE;
100 /* Initialize the console to provide early debug support */
101 console_init(uart_base, PL011_UART_CLK_IN_HZ, PL011_BAUDRATE);
102
103 /* Allow BL1 to see the whole Trusted RAM */
104 bl1_tzram_layout.total_base = BL1_RW_BASE;
105 bl1_tzram_layout.total_size = BL1_RW_SIZE;
106
Haojian Zhuang602362d2017-06-01 12:15:14 +0800107 INFO("BL1: 0x%lx - 0x%lx [size = %lu]\n", BL1_RAM_BASE, BL1_RAM_LIMIT,
Victor Chong2d9a42d2017-08-17 15:21:10 +0900108 BL1_RAM_LIMIT - BL1_RAM_BASE); /* bl1_size */
Haojian Zhuang602362d2017-06-01 12:15:14 +0800109}
110
111/*
112 * Perform the very early platform specific architecture setup here. At the
113 * moment this only does basic initialization. Later architectural setup
114 * (bl1_arch_setup()) does not do anything platform specific.
115 */
116void bl1_plat_arch_setup(void)
117{
118 hikey960_init_mmu_el3(bl1_tzram_layout.total_base,
119 bl1_tzram_layout.total_size,
120 BL1_RO_BASE,
121 BL1_RO_LIMIT,
Joel Hutton5cc3bc82018-03-21 11:40:57 +0000122 BL_COHERENT_RAM_BASE,
123 BL_COHERENT_RAM_END);
Haojian Zhuang602362d2017-06-01 12:15:14 +0800124}
125
Haojian Zhuang602362d2017-06-01 12:15:14 +0800126static void hikey960_ufs_reset(void)
127{
128 unsigned int data, mask;
129
130 mmio_write_32(CRG_PERDIS7_REG, 1 << 14);
131 mmio_clrbits_32(UFS_SYS_PHY_CLK_CTRL_REG, BIT_SYSCTRL_REF_CLOCK_EN);
132 do {
133 data = mmio_read_32(UFS_SYS_PHY_CLK_CTRL_REG);
134 } while (data & BIT_SYSCTRL_REF_CLOCK_EN);
135 /* use abb clk */
136 mmio_clrbits_32(UFS_SYS_UFS_SYSCTRL_REG, BIT_UFS_REFCLK_SRC_SE1);
137 mmio_clrbits_32(UFS_SYS_PHY_ISO_EN_REG, BIT_UFS_REFCLK_ISO_EN);
138 mmio_write_32(PCTRL_PERI_CTRL3_REG, (1 << 0) | (1 << 16));
139 mdelay(1);
140 mmio_write_32(CRG_PEREN7_REG, 1 << 14);
141 mmio_setbits_32(UFS_SYS_PHY_CLK_CTRL_REG, BIT_SYSCTRL_REF_CLOCK_EN);
142
143 mmio_write_32(CRG_PERRSTEN3_REG, PERI_UFS_BIT);
144 do {
145 data = mmio_read_32(CRG_PERRSTSTAT3_REG);
146 } while ((data & PERI_UFS_BIT) == 0);
147 mmio_setbits_32(UFS_SYS_PSW_POWER_CTRL_REG, BIT_UFS_PSW_MTCMOS_EN);
148 mdelay(1);
149 mmio_setbits_32(UFS_SYS_HC_LP_CTRL_REG, BIT_SYSCTRL_PWR_READY);
150 mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
151 MASK_UFS_DEVICE_RESET);
152 /* clear SC_DIV_UFS_PERIBUS */
153 mask = SC_DIV_UFS_PERIBUS << 16;
154 mmio_write_32(CRG_CLKDIV17_REG, mask);
155 /* set SC_DIV_UFSPHY_CFG(3) */
156 mask = SC_DIV_UFSPHY_CFG_MASK << 16;
157 data = SC_DIV_UFSPHY_CFG(3);
158 mmio_write_32(CRG_CLKDIV16_REG, mask | data);
159 data = mmio_read_32(UFS_SYS_PHY_CLK_CTRL_REG);
160 data &= ~MASK_SYSCTRL_CFG_CLOCK_FREQ;
161 data |= 0x39;
162 mmio_write_32(UFS_SYS_PHY_CLK_CTRL_REG, data);
163 mmio_clrbits_32(UFS_SYS_PHY_CLK_CTRL_REG, MASK_SYSCTRL_REF_CLOCK_SEL);
164 mmio_setbits_32(UFS_SYS_CLOCK_GATE_BYPASS_REG,
165 MASK_UFS_CLK_GATE_BYPASS);
166 mmio_setbits_32(UFS_SYS_UFS_SYSCTRL_REG, MASK_UFS_SYSCTRL_BYPASS);
167
168 mmio_setbits_32(UFS_SYS_PSW_CLK_CTRL_REG, BIT_SYSCTRL_PSW_CLK_EN);
169 mmio_clrbits_32(UFS_SYS_PSW_POWER_CTRL_REG, BIT_UFS_PSW_ISO_CTRL);
170 mmio_clrbits_32(UFS_SYS_PHY_ISO_EN_REG, BIT_UFS_PHY_ISO_CTRL);
171 mmio_clrbits_32(UFS_SYS_HC_LP_CTRL_REG, BIT_SYSCTRL_LP_ISOL_EN);
172 mmio_write_32(CRG_PERRSTDIS3_REG, PERI_ARST_UFS_BIT);
173 mmio_setbits_32(UFS_SYS_RESET_CTRL_EN_REG, BIT_SYSCTRL_LP_RESET_N);
174 mdelay(1);
175 mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
176 MASK_UFS_DEVICE_RESET | BIT_UFS_DEVICE_RESET);
177 mdelay(20);
178 mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
179 0x03300330);
180
181 mmio_write_32(CRG_PERRSTDIS3_REG, PERI_UFS_BIT);
182 do {
183 data = mmio_read_32(CRG_PERRSTSTAT3_REG);
184 } while (data & PERI_UFS_BIT);
185}
186
187static void hikey960_ufs_init(void)
188{
189 dw_ufs_params_t ufs_params;
190
191 memset(&ufs_params, 0, sizeof(ufs_params));
192 ufs_params.reg_base = UFS_REG_BASE;
193 ufs_params.desc_base = HIKEY960_UFS_DESC_BASE;
194 ufs_params.desc_size = HIKEY960_UFS_DESC_SIZE;
195
196 if ((ufs_params.flags & UFS_FLAGS_SKIPINIT) == 0)
197 hikey960_ufs_reset();
198 dw_ufs_init(&ufs_params);
199}
200
Haojian Zhuang602362d2017-06-01 12:15:14 +0800201/*
202 * Function which will perform any remaining platform-specific setup that can
203 * occur after the MMU and data cache have been enabled.
204 */
205void bl1_platform_setup(void)
206{
207 hikey960_clk_init();
208 hikey960_pmu_init();
209 hikey960_regulator_enable();
210 hikey960_tzc_init();
211 hikey960_peri_init();
212 hikey960_ufs_init();
213 hikey960_pinmux_init();
Kaihua Zhong39ff2ee2018-07-16 17:33:48 +0800214 hikey960_gpio_init();
Haojian Zhuang602362d2017-06-01 12:15:14 +0800215 hikey960_io_setup();
216}
217
218/*
219 * The following function checks if Firmware update is needed,
220 * by checking if TOC in FIP image is valid or not.
221 */
222unsigned int bl1_plat_get_next_image_id(void)
223{
224 unsigned int mode, ret;
225
226 mode = mmio_read_32(SCTRL_BAK_DATA0_REG);
227 switch (mode & BOOT_MODE_MASK) {
228 case BOOT_MODE_RECOVERY:
229 ret = NS_BL1U_IMAGE_ID;
230 break;
Haojian Zhuang602362d2017-06-01 12:15:14 +0800231 default:
232 WARN("Invalid boot mode is found:%d\n", mode);
233 panic();
234 }
235 return ret;
236}
237
238image_desc_t *bl1_plat_get_image_desc(unsigned int image_id)
239{
240 unsigned int index = 0;
241
242 while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) {
243 if (bl1_tbbr_image_descs[index].image_id == image_id)
244 return &bl1_tbbr_image_descs[index];
245 index++;
246 }
247
248 return NULL;
249}
250
251void bl1_plat_set_ep_info(unsigned int image_id,
252 entry_point_info_t *ep_info)
253{
254 unsigned int data = 0;
255 uintptr_t tmp = HIKEY960_NS_TMP_OFFSET;
256
Haojian Zhuang1b4b4122018-01-25 16:13:05 +0800257 if (image_id != NS_BL1U_IMAGE_ID)
258 panic();
Haojian Zhuang602362d2017-06-01 12:15:14 +0800259 /* Copy NS BL1U from 0x1AC1_8000 to 0x1AC9_8000 */
260 memcpy((void *)tmp, (void *)HIKEY960_NS_IMAGE_OFFSET,
261 NS_BL1U_SIZE);
262 memcpy((void *)NS_BL1U_BASE, (void *)tmp, NS_BL1U_SIZE);
263 inv_dcache_range(NS_BL1U_BASE, NS_BL1U_SIZE);
264 /* Initialize the GIC driver, cpu and distributor interfaces */
265 gicv2_driver_init(&hikey960_gic_data);
266 gicv2_distif_init();
267 gicv2_pcpu_distif_init();
268 gicv2_cpuif_enable();
269 /* CNTFRQ is read-only in EL1 */
270 write_cntfrq_el0(plat_get_syscnt_freq2());
271 data = read_cpacr_el1();
272 do {
273 data |= 3 << 20;
274 write_cpacr_el1(data);
275 data = read_cpacr_el1();
276 } while ((data & (3 << 20)) != (3 << 20));
277 INFO("cpacr_el1:0x%x\n", data);
278
279 ep_info->args.arg0 = 0xffff & read_mpidr();
280 ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
281 DISABLE_ALL_EXCEPTIONS);
282}