xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/secure/secure.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1e3525114SXing Zheng /*
2e3525114SXing Zheng  * Copyright (c) 2016, 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>
8*09d40e0eSAntonio Nino Diaz 
9*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
10*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
11*09d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
12*09d40e0eSAntonio Nino Diaz 
13e3525114SXing Zheng #include <plat_private.h>
14e3525114SXing Zheng #include <secure.h>
15e3525114SXing Zheng #include <soc.h>
16e3525114SXing Zheng 
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.
48e3525114SXing Zheng  * The @st_mb and @ed_mb indicate the start and end addresses for which to set
49e3525114SXing Zheng  * the security, and the unit is megabyte. When the st_mb == 0, ed_mb == 0, the
50e3525114SXing Zheng  * address range 0x0 ~ 0xfffff is secure.
51e3525114SXing Zheng  *
52e3525114SXing Zheng  * For example, if we would like to set the range [0, 32MB) is security via
53e3525114SXing Zheng  * DDR_RGN0, then rgn == 0, st_mb == 0, ed_mb == 31.
54e3525114SXing Zheng  */
55e3525114SXing Zheng static void sgrf_ddr_rgn_config(uint32_t rgn,
56e3525114SXing Zheng 				uintptr_t st, uintptr_t ed)
57e3525114SXing Zheng {
58e3525114SXing Zheng 	uintptr_t st_mb, ed_mb;
59e3525114SXing Zheng 
60e3525114SXing Zheng 	assert(rgn <= 7);
61e3525114SXing Zheng 	assert(st < ed);
62e3525114SXing Zheng 
63e3525114SXing Zheng 	/* check aligned 1MB */
64e3525114SXing Zheng 	assert(st % SIZE_M(1) == 0);
65e3525114SXing Zheng 	assert(ed % SIZE_M(1) == 0);
66e3525114SXing Zheng 
67e3525114SXing Zheng 	st_mb = st / SIZE_M(1);
68e3525114SXing Zheng 	ed_mb = ed / SIZE_M(1);
69e3525114SXing Zheng 
70e3525114SXing Zheng 	/* set ddr region addr start */
71e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(rgn),
72e3525114SXing Zheng 		      BITS_WITH_WMASK(st_mb, SGRF_DDR_RGN_0_16_WMSK, 0));
73e3525114SXing Zheng 
74e3525114SXing Zheng 	/* set ddr region addr end */
75e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(rgn + 8),
76e3525114SXing Zheng 		      BITS_WITH_WMASK((ed_mb - 1), SGRF_DDR_RGN_0_16_WMSK, 0));
77e3525114SXing Zheng 
78e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
79e3525114SXing Zheng 		      BIT_WITH_WMSK(rgn));
80e3525114SXing Zheng }
81e3525114SXing Zheng 
825b886432SDerek Basehore void secure_watchdog_gate(void)
83e3525114SXing Zheng {
84e3525114SXing Zheng 	/**
85e3525114SXing Zheng 	 * Disable CA53 and CM0 wdt pclk
86e3525114SXing Zheng 	 * BIT[8]: ca53 wdt pclk, 0: enable 1: disable
87e3525114SXing Zheng 	 * BIT[10]: cm0 wdt pclk, 0: enable 1: disable
88e3525114SXing Zheng 	 */
89e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3),
90e3525114SXing Zheng 		      BIT_WITH_WMSK(PCLK_WDT_CA53_GATE_SHIFT) |
91e3525114SXing Zheng 		      BIT_WITH_WMSK(PCLK_WDT_CM0_GATE_SHIFT));
92e3525114SXing Zheng }
93e3525114SXing Zheng 
945b886432SDerek Basehore __pmusramfunc void secure_watchdog_ungate(void)
95e3525114SXing Zheng {
96e3525114SXing Zheng 	/**
97e3525114SXing Zheng 	 * Enable CA53 and CM0 wdt pclk
98e3525114SXing Zheng 	 * BIT[8]: ca53 wdt pclk, 0: enable 1: disable
99e3525114SXing Zheng 	 * BIT[10]: cm0 wdt pclk, 0: enable 1: disable
100e3525114SXing Zheng 	 */
101e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3),
102e3525114SXing Zheng 		      WMSK_BIT(PCLK_WDT_CA53_GATE_SHIFT) |
103e3525114SXing Zheng 		      WMSK_BIT(PCLK_WDT_CM0_GATE_SHIFT));
104e3525114SXing Zheng }
105e3525114SXing Zheng 
106a7bb3388SLin Huang __pmusramfunc void sram_secure_timer_init(void)
107a7bb3388SLin Huang {
108a7bb3388SLin Huang 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT0, 0xffffffff);
109a7bb3388SLin Huang 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT1, 0xffffffff);
110a7bb3388SLin Huang 
111a7bb3388SLin Huang 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
112a7bb3388SLin Huang 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
113a7bb3388SLin Huang 
114a7bb3388SLin Huang 	/* auto reload & enable the timer */
115a7bb3388SLin Huang 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_CONTROL_REG,
116a7bb3388SLin Huang 		      TIMER_EN | TIMER_FMODE);
117a7bb3388SLin Huang }
118a7bb3388SLin Huang 
119e3525114SXing Zheng void secure_timer_init(void)
120e3525114SXing Zheng {
121e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT0, 0xffffffff);
122e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT1, 0xffffffff);
123e3525114SXing Zheng 
124e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
125e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
126e3525114SXing Zheng 
127e3525114SXing Zheng 	/* auto reload & enable the timer */
128e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_CONTROL_REG,
129e3525114SXing Zheng 		      TIMER_EN | TIMER_FMODE);
130e3525114SXing Zheng }
131e3525114SXing Zheng 
132e3525114SXing Zheng void secure_sgrf_init(void)
133e3525114SXing Zheng {
134e3525114SXing Zheng 	/* security config for master */
135e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(5),
136e3525114SXing Zheng 		      REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
137e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6),
138e3525114SXing Zheng 		      REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
139e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(7),
140e3525114SXing Zheng 		      REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
141e3525114SXing Zheng 
142e3525114SXing Zheng 	/* security config for slave */
143e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_SLV_CON0_1(0),
144e3525114SXing Zheng 		      SGRF_PMU_SLV_S_CFGED |
145e3525114SXing Zheng 		      SGRF_PMU_SLV_CRYPTO1_NS);
146e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_SLV_CON0_1(1),
147e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_PMUSRAM_S);
148e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(0),
149e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
150e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(1),
151e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
152e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(2),
153e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
154e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(3),
155e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
156e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(4),
157ccdc044aSXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_INTSRAM_S);
158e3525114SXing Zheng }
159e3525114SXing Zheng 
160e3525114SXing Zheng void secure_sgrf_ddr_rgn_init(void)
161e3525114SXing Zheng {
162e3525114SXing Zheng 	sgrf_ddr_rgn_config(0, TZRAM_BASE, TZRAM_SIZE);
163e3525114SXing Zheng 	sgrf_ddr_rgn_global_bypass(0);
164e3525114SXing Zheng }
165