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