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