xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/secure/secure.c (revision e3525114394324b7d4be104ccba24f0ca8ca8c6b)
1*e3525114SXing Zheng /*
2*e3525114SXing Zheng  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3*e3525114SXing Zheng  *
4*e3525114SXing Zheng  * Redistribution and use in source and binary forms, with or without
5*e3525114SXing Zheng  * modification, are permitted provided that the following conditions are met:
6*e3525114SXing Zheng  *
7*e3525114SXing Zheng  * Redistributions of source code must retain the above copyright notice, this
8*e3525114SXing Zheng  * list of conditions and the following disclaimer.
9*e3525114SXing Zheng  *
10*e3525114SXing Zheng  * Redistributions in binary form must reproduce the above copyright notice,
11*e3525114SXing Zheng  * this list of conditions and the following disclaimer in the documentation
12*e3525114SXing Zheng  * and/or other materials provided with the distribution.
13*e3525114SXing Zheng  *
14*e3525114SXing Zheng  * Neither the name of ARM nor the names of its contributors may be used
15*e3525114SXing Zheng  * to endorse or promote products derived from this software without specific
16*e3525114SXing Zheng  * prior written permission.
17*e3525114SXing Zheng  *
18*e3525114SXing Zheng  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*e3525114SXing Zheng  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*e3525114SXing Zheng  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*e3525114SXing Zheng  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*e3525114SXing Zheng  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*e3525114SXing Zheng  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*e3525114SXing Zheng  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*e3525114SXing Zheng  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*e3525114SXing Zheng  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*e3525114SXing Zheng  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*e3525114SXing Zheng  * POSSIBILITY OF SUCH DAMAGE.
29*e3525114SXing Zheng  */
30*e3525114SXing Zheng 
31*e3525114SXing Zheng #include <arch_helpers.h>
32*e3525114SXing Zheng #include <assert.h>
33*e3525114SXing Zheng #include <debug.h>
34*e3525114SXing Zheng #include <delay_timer.h>
35*e3525114SXing Zheng #include <plat_private.h>
36*e3525114SXing Zheng #include <secure.h>
37*e3525114SXing Zheng #include <soc.h>
38*e3525114SXing Zheng 
39*e3525114SXing Zheng static void sgrf_ddr_rgn_global_bypass(uint32_t bypass)
40*e3525114SXing Zheng {
41*e3525114SXing Zheng 	if (bypass)
42*e3525114SXing Zheng 		/* set bypass (non-secure regions) for whole ddr regions */
43*e3525114SXing Zheng 		mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
44*e3525114SXing Zheng 			      SGRF_DDR_RGN_BYPS);
45*e3525114SXing Zheng 	else
46*e3525114SXing Zheng 		/* cancel bypass for whole ddr regions */
47*e3525114SXing Zheng 		mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
48*e3525114SXing Zheng 			      SGRF_DDR_RGN_NO_BYPS);
49*e3525114SXing Zheng }
50*e3525114SXing Zheng 
51*e3525114SXing Zheng /**
52*e3525114SXing Zheng  * There are 8 + 1 regions for DDR secure control:
53*e3525114SXing Zheng  * DDR_RGN_0 ~ DDR_RGN_7: Per DDR_RGNs grain size is 1MB
54*e3525114SXing Zheng  * DDR_RGN_X - the memories of exclude DDR_RGN_0 ~ DDR_RGN_7
55*e3525114SXing Zheng  *
56*e3525114SXing Zheng  * DDR_RGN_0 - start address of the RGN0
57*e3525114SXing Zheng  * DDR_RGN_8 - end address of the RGN0
58*e3525114SXing Zheng  * DDR_RGN_1 - start address of the RGN1
59*e3525114SXing Zheng  * DDR_RGN_9 - end address of the RGN1
60*e3525114SXing Zheng  * ...
61*e3525114SXing Zheng  * DDR_RGN_7 - start address of the RGN7
62*e3525114SXing Zheng  * DDR_RGN_15 - end address of the RGN7
63*e3525114SXing Zheng  * DDR_RGN_16 - bit 0 ~ 7 is bitmap for RGN0~7 secure,0: disable, 1: enable
64*e3525114SXing Zheng  *              bit 8 is setting for RGNx, the rest of the memory and region
65*e3525114SXing Zheng  *                which excludes RGN0~7, 0: disable, 1: enable
66*e3525114SXing Zheng  *              bit 9, the global secure configuration via bypass, 0: disable
67*e3525114SXing Zheng  *                bypass, 1: enable bypass
68*e3525114SXing Zheng  *
69*e3525114SXing Zheng  * @rgn - the DDR regions 0 ~ 7 which are can be configured.
70*e3525114SXing Zheng  * The @st_mb and @ed_mb indicate the start and end addresses for which to set
71*e3525114SXing Zheng  * the security, and the unit is megabyte. When the st_mb == 0, ed_mb == 0, the
72*e3525114SXing Zheng  * address range 0x0 ~ 0xfffff is secure.
73*e3525114SXing Zheng  *
74*e3525114SXing Zheng  * For example, if we would like to set the range [0, 32MB) is security via
75*e3525114SXing Zheng  * DDR_RGN0, then rgn == 0, st_mb == 0, ed_mb == 31.
76*e3525114SXing Zheng  */
77*e3525114SXing Zheng static void sgrf_ddr_rgn_config(uint32_t rgn,
78*e3525114SXing Zheng 				uintptr_t st, uintptr_t ed)
79*e3525114SXing Zheng {
80*e3525114SXing Zheng 	uintptr_t st_mb, ed_mb;
81*e3525114SXing Zheng 
82*e3525114SXing Zheng 	assert(rgn <= 7);
83*e3525114SXing Zheng 	assert(st < ed);
84*e3525114SXing Zheng 
85*e3525114SXing Zheng 	/* check aligned 1MB */
86*e3525114SXing Zheng 	assert(st % SIZE_M(1) == 0);
87*e3525114SXing Zheng 	assert(ed % SIZE_M(1) == 0);
88*e3525114SXing Zheng 
89*e3525114SXing Zheng 	st_mb = st / SIZE_M(1);
90*e3525114SXing Zheng 	ed_mb = ed / SIZE_M(1);
91*e3525114SXing Zheng 
92*e3525114SXing Zheng 	/* set ddr region addr start */
93*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(rgn),
94*e3525114SXing Zheng 		      BITS_WITH_WMASK(st_mb, SGRF_DDR_RGN_0_16_WMSK, 0));
95*e3525114SXing Zheng 
96*e3525114SXing Zheng 	/* set ddr region addr end */
97*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(rgn + 8),
98*e3525114SXing Zheng 		      BITS_WITH_WMASK((ed_mb - 1), SGRF_DDR_RGN_0_16_WMSK, 0));
99*e3525114SXing Zheng 
100*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
101*e3525114SXing Zheng 		      BIT_WITH_WMSK(rgn));
102*e3525114SXing Zheng }
103*e3525114SXing Zheng 
104*e3525114SXing Zheng void secure_watchdog_disable(void)
105*e3525114SXing Zheng {
106*e3525114SXing Zheng 	/**
107*e3525114SXing Zheng 	 * Disable CA53 and CM0 wdt pclk
108*e3525114SXing Zheng 	 * BIT[8]: ca53 wdt pclk, 0: enable 1: disable
109*e3525114SXing Zheng 	 * BIT[10]: cm0 wdt pclk, 0: enable 1: disable
110*e3525114SXing Zheng 	 */
111*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3),
112*e3525114SXing Zheng 		      BIT_WITH_WMSK(PCLK_WDT_CA53_GATE_SHIFT) |
113*e3525114SXing Zheng 		      BIT_WITH_WMSK(PCLK_WDT_CM0_GATE_SHIFT));
114*e3525114SXing Zheng }
115*e3525114SXing Zheng 
116*e3525114SXing Zheng void secure_watchdog_enable(void)
117*e3525114SXing Zheng {
118*e3525114SXing Zheng 	/**
119*e3525114SXing Zheng 	 * Enable CA53 and CM0 wdt pclk
120*e3525114SXing Zheng 	 * BIT[8]: ca53 wdt pclk, 0: enable 1: disable
121*e3525114SXing Zheng 	 * BIT[10]: cm0 wdt pclk, 0: enable 1: disable
122*e3525114SXing Zheng 	 */
123*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3),
124*e3525114SXing Zheng 		      WMSK_BIT(PCLK_WDT_CA53_GATE_SHIFT) |
125*e3525114SXing Zheng 		      WMSK_BIT(PCLK_WDT_CM0_GATE_SHIFT));
126*e3525114SXing Zheng }
127*e3525114SXing Zheng 
128*e3525114SXing Zheng void secure_timer_init(void)
129*e3525114SXing Zheng {
130*e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT0, 0xffffffff);
131*e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT1, 0xffffffff);
132*e3525114SXing Zheng 
133*e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
134*e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
135*e3525114SXing Zheng 
136*e3525114SXing Zheng 	/* auto reload & enable the timer */
137*e3525114SXing Zheng 	mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_CONTROL_REG,
138*e3525114SXing Zheng 		      TIMER_EN | TIMER_FMODE);
139*e3525114SXing Zheng }
140*e3525114SXing Zheng 
141*e3525114SXing Zheng void secure_sgrf_init(void)
142*e3525114SXing Zheng {
143*e3525114SXing Zheng 	/* security config for master */
144*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(5),
145*e3525114SXing Zheng 		      REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
146*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6),
147*e3525114SXing Zheng 		      REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
148*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(7),
149*e3525114SXing Zheng 		      REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
150*e3525114SXing Zheng 
151*e3525114SXing Zheng 	/* security config for slave */
152*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_SLV_CON0_1(0),
153*e3525114SXing Zheng 		      SGRF_PMU_SLV_S_CFGED |
154*e3525114SXing Zheng 		      SGRF_PMU_SLV_CRYPTO1_NS);
155*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_SLV_CON0_1(1),
156*e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_PMUSRAM_S);
157*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(0),
158*e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
159*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(1),
160*e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
161*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(2),
162*e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
163*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(3),
164*e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
165*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(4),
166*e3525114SXing Zheng 		      SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
167*e3525114SXing Zheng }
168*e3525114SXing Zheng 
169*e3525114SXing Zheng void secure_sgrf_ddr_rgn_init(void)
170*e3525114SXing Zheng {
171*e3525114SXing Zheng 	sgrf_ddr_rgn_config(0, TZRAM_BASE, TZRAM_SIZE);
172*e3525114SXing Zheng 	sgrf_ddr_rgn_global_bypass(0);
173*e3525114SXing Zheng }
174