1e3525114SXing Zheng /*
2*7f0b2e78SHeiko Stuebner * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
3e3525114SXing Zheng *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5e3525114SXing Zheng */
6e3525114SXing Zheng
7e3525114SXing Zheng #include <assert.h>
809d40e0eSAntonio Nino Diaz
909d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1009d40e0eSAntonio Nino Diaz #include <common/debug.h>
1109d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
1209d40e0eSAntonio Nino Diaz
13e3525114SXing Zheng #include <plat_private.h>
14e3525114SXing Zheng #include <secure.h>
15e3525114SXing Zheng #include <soc.h>
16e3525114SXing Zheng
sgrf_ddr_rgn_global_bypass(uint32_t bypass)17e3525114SXing Zheng static void sgrf_ddr_rgn_global_bypass(uint32_t bypass)
18e3525114SXing Zheng {
19e3525114SXing Zheng if (bypass)
20e3525114SXing Zheng /* set bypass (non-secure regions) for whole ddr regions */
21e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
22e3525114SXing Zheng SGRF_DDR_RGN_BYPS);
23e3525114SXing Zheng else
24e3525114SXing Zheng /* cancel bypass for whole ddr regions */
25e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
26e3525114SXing Zheng SGRF_DDR_RGN_NO_BYPS);
27e3525114SXing Zheng }
28e3525114SXing Zheng
29e3525114SXing Zheng /**
30e3525114SXing Zheng * There are 8 + 1 regions for DDR secure control:
31e3525114SXing Zheng * DDR_RGN_0 ~ DDR_RGN_7: Per DDR_RGNs grain size is 1MB
32e3525114SXing Zheng * DDR_RGN_X - the memories of exclude DDR_RGN_0 ~ DDR_RGN_7
33e3525114SXing Zheng *
34e3525114SXing Zheng * DDR_RGN_0 - start address of the RGN0
35e3525114SXing Zheng * DDR_RGN_8 - end address of the RGN0
36e3525114SXing Zheng * DDR_RGN_1 - start address of the RGN1
37e3525114SXing Zheng * DDR_RGN_9 - end address of the RGN1
38e3525114SXing Zheng * ...
39e3525114SXing Zheng * DDR_RGN_7 - start address of the RGN7
40e3525114SXing Zheng * DDR_RGN_15 - end address of the RGN7
41e3525114SXing Zheng * DDR_RGN_16 - bit 0 ~ 7 is bitmap for RGN0~7 secure,0: disable, 1: enable
42e3525114SXing Zheng * bit 8 is setting for RGNx, the rest of the memory and region
43e3525114SXing Zheng * which excludes RGN0~7, 0: disable, 1: enable
44e3525114SXing Zheng * bit 9, the global secure configuration via bypass, 0: disable
45e3525114SXing Zheng * bypass, 1: enable bypass
46e3525114SXing Zheng *
47e3525114SXing Zheng * @rgn - the DDR regions 0 ~ 7 which are can be configured.
48*7f0b2e78SHeiko Stuebner * @st - start address to set as secure
49*7f0b2e78SHeiko Stuebner * @sz - length of area to set as secure
50e3525114SXing Zheng * The @st_mb and @ed_mb indicate the start and end addresses for which to set
51e3525114SXing Zheng * the security, and the unit is megabyte. When the st_mb == 0, ed_mb == 0, the
52e3525114SXing Zheng * address range 0x0 ~ 0xfffff is secure.
53e3525114SXing Zheng *
54e3525114SXing Zheng * For example, if we would like to set the range [0, 32MB) is security via
55e3525114SXing Zheng * DDR_RGN0, then rgn == 0, st_mb == 0, ed_mb == 31.
56e3525114SXing Zheng */
sgrf_ddr_rgn_config(uint32_t rgn,uintptr_t st,size_t sz)57e3525114SXing Zheng static void sgrf_ddr_rgn_config(uint32_t rgn,
58*7f0b2e78SHeiko Stuebner uintptr_t st, size_t sz)
59e3525114SXing Zheng {
60*7f0b2e78SHeiko Stuebner uintptr_t ed = st + sz;
61e3525114SXing Zheng uintptr_t st_mb, ed_mb;
62e3525114SXing Zheng
63e3525114SXing Zheng assert(rgn <= 7);
64e3525114SXing Zheng assert(st < ed);
65e3525114SXing Zheng
66e3525114SXing Zheng /* check aligned 1MB */
67e3525114SXing Zheng assert(st % SIZE_M(1) == 0);
68e3525114SXing Zheng assert(ed % SIZE_M(1) == 0);
69e3525114SXing Zheng
70e3525114SXing Zheng st_mb = st / SIZE_M(1);
71e3525114SXing Zheng ed_mb = ed / SIZE_M(1);
72e3525114SXing Zheng
73e3525114SXing Zheng /* set ddr region addr start */
74e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(rgn),
75e3525114SXing Zheng BITS_WITH_WMASK(st_mb, SGRF_DDR_RGN_0_16_WMSK, 0));
76e3525114SXing Zheng
77e3525114SXing Zheng /* set ddr region addr end */
78e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(rgn + 8),
79e3525114SXing Zheng BITS_WITH_WMASK((ed_mb - 1), SGRF_DDR_RGN_0_16_WMSK, 0));
80e3525114SXing Zheng
81e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
82e3525114SXing Zheng BIT_WITH_WMSK(rgn));
83e3525114SXing Zheng }
84e3525114SXing Zheng
secure_watchdog_gate(void)855b886432SDerek Basehore void secure_watchdog_gate(void)
86e3525114SXing Zheng {
87e3525114SXing Zheng /**
88e3525114SXing Zheng * Disable CA53 and CM0 wdt pclk
89e3525114SXing Zheng * BIT[8]: ca53 wdt pclk, 0: enable 1: disable
90e3525114SXing Zheng * BIT[10]: cm0 wdt pclk, 0: enable 1: disable
91e3525114SXing Zheng */
92e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3),
93e3525114SXing Zheng BIT_WITH_WMSK(PCLK_WDT_CA53_GATE_SHIFT) |
94e3525114SXing Zheng BIT_WITH_WMSK(PCLK_WDT_CM0_GATE_SHIFT));
95e3525114SXing Zheng }
96e3525114SXing Zheng
secure_watchdog_ungate(void)975b886432SDerek Basehore __pmusramfunc void secure_watchdog_ungate(void)
98e3525114SXing Zheng {
99e3525114SXing Zheng /**
100e3525114SXing Zheng * Enable CA53 and CM0 wdt pclk
101e3525114SXing Zheng * BIT[8]: ca53 wdt pclk, 0: enable 1: disable
102e3525114SXing Zheng * BIT[10]: cm0 wdt pclk, 0: enable 1: disable
103e3525114SXing Zheng */
104e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3),
105e3525114SXing Zheng WMSK_BIT(PCLK_WDT_CA53_GATE_SHIFT) |
106e3525114SXing Zheng WMSK_BIT(PCLK_WDT_CM0_GATE_SHIFT));
107e3525114SXing Zheng }
108e3525114SXing Zheng
sram_secure_timer_init(void)109a7bb3388SLin Huang __pmusramfunc void sram_secure_timer_init(void)
110a7bb3388SLin Huang {
111a7bb3388SLin Huang mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT0, 0xffffffff);
112a7bb3388SLin Huang mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT1, 0xffffffff);
113a7bb3388SLin Huang
114a7bb3388SLin Huang mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
115a7bb3388SLin Huang mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
116a7bb3388SLin Huang
117a7bb3388SLin Huang /* auto reload & enable the timer */
118a7bb3388SLin Huang mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_CONTROL_REG,
119a7bb3388SLin Huang TIMER_EN | TIMER_FMODE);
120a7bb3388SLin Huang }
121a7bb3388SLin Huang
secure_timer_init(void)122e3525114SXing Zheng void secure_timer_init(void)
123e3525114SXing Zheng {
124e3525114SXing Zheng mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT0, 0xffffffff);
125e3525114SXing Zheng mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT1, 0xffffffff);
126e3525114SXing Zheng
127e3525114SXing Zheng mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
128e3525114SXing Zheng mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
129e3525114SXing Zheng
130e3525114SXing Zheng /* auto reload & enable the timer */
131e3525114SXing Zheng mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_CONTROL_REG,
132e3525114SXing Zheng TIMER_EN | TIMER_FMODE);
133e3525114SXing Zheng }
134e3525114SXing Zheng
secure_sgrf_init(void)135e3525114SXing Zheng void secure_sgrf_init(void)
136e3525114SXing Zheng {
137e3525114SXing Zheng /* security config for master */
138e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SOC_CON(5),
139e3525114SXing Zheng REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
140e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6),
141e3525114SXing Zheng REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
142e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SOC_CON(7),
143e3525114SXing Zheng REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
144e3525114SXing Zheng
145e3525114SXing Zheng /* security config for slave */
146e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_PMU_SLV_CON0_1(0),
147e3525114SXing Zheng SGRF_PMU_SLV_S_CFGED |
148e3525114SXing Zheng SGRF_PMU_SLV_CRYPTO1_NS);
149e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_PMU_SLV_CON0_1(1),
150e3525114SXing Zheng SGRF_SLV_S_WMSK | SGRF_PMUSRAM_S);
151e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(0),
152e3525114SXing Zheng SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
153e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(1),
154e3525114SXing Zheng SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
155e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(2),
156e3525114SXing Zheng SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
157e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(3),
158e3525114SXing Zheng SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
159e3525114SXing Zheng mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(4),
160ccdc044aSXing Zheng SGRF_SLV_S_WMSK | SGRF_INTSRAM_S);
161e3525114SXing Zheng }
162e3525114SXing Zheng
secure_sgrf_ddr_rgn_init(void)163e3525114SXing Zheng void secure_sgrf_ddr_rgn_init(void)
164e3525114SXing Zheng {
165e3525114SXing Zheng sgrf_ddr_rgn_config(0, TZRAM_BASE, TZRAM_SIZE);
166e3525114SXing Zheng sgrf_ddr_rgn_global_bypass(0);
167e3525114SXing Zheng }
168