xref: /rk3399_rockchip-uboot/drivers/misc/fsl_sec_mon.c (revision 711b534120c0a5f73cdb9a25eb91f9aa0c5e09ab)
1fe78378dSgaurav rana /*
2fe78378dSgaurav rana  * Copyright 2015 Freescale Semiconductor, Inc.
3fe78378dSgaurav rana  *
4fe78378dSgaurav rana  * SPDX-License-Identifier:	GPL-2.0+
5fe78378dSgaurav rana  */
6fe78378dSgaurav rana 
7fe78378dSgaurav rana #include <common.h>
8fe78378dSgaurav rana #include <fsl_sec_mon.h>
9fe78378dSgaurav rana 
get_sec_mon_state(void)10*b259732dSSumit Garg static u32 get_sec_mon_state(void)
11fe78378dSgaurav rana {
12fe78378dSgaurav rana 	struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
13fe78378dSgaurav rana 						(CONFIG_SYS_SEC_MON_ADDR);
14*b259732dSSumit Garg 	return sec_mon_in32(&sec_mon_regs->hp_stat) & HPSR_SSM_ST_MASK;
15*b259732dSSumit Garg }
16*b259732dSSumit Garg 
set_sec_mon_state_non_sec(void)17*b259732dSSumit Garg static int set_sec_mon_state_non_sec(void)
18*b259732dSSumit Garg {
19*b259732dSSumit Garg 	u32 sts;
20fe78378dSgaurav rana 	int timeout = 10;
21*b259732dSSumit Garg 	struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
22*b259732dSSumit Garg 						(CONFIG_SYS_SEC_MON_ADDR);
23fe78378dSgaurav rana 
24*b259732dSSumit Garg 	sts = get_sec_mon_state();
25*b259732dSSumit Garg 
26*b259732dSSumit Garg 	switch (sts) {
27*b259732dSSumit Garg 	/*
28*b259732dSSumit Garg 	 * If initial state is check or Non-Secure, then set the Software
29*b259732dSSumit Garg 	 * Security Violation Bit and transition to Non-Secure State.
30*b259732dSSumit Garg 	 */
31*b259732dSSumit Garg 	case HPSR_SSM_ST_CHECK:
32*b259732dSSumit Garg 		printf("SEC_MON state transitioning to Non Secure.\n");
33*b259732dSSumit Garg 		sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_SV);
34*b259732dSSumit Garg 
35*b259732dSSumit Garg 		/* polling loop till SEC_MON is in Non Secure state */
36*b259732dSSumit Garg 		while (timeout) {
37*b259732dSSumit Garg 			sts = get_sec_mon_state();
38*b259732dSSumit Garg 
39*b259732dSSumit Garg 			if ((sts & HPSR_SSM_ST_MASK) ==
40*b259732dSSumit Garg 				HPSR_SSM_ST_NON_SECURE)
41*b259732dSSumit Garg 				break;
42*b259732dSSumit Garg 
43*b259732dSSumit Garg 			udelay(10);
44*b259732dSSumit Garg 			timeout--;
45*b259732dSSumit Garg 		}
46*b259732dSSumit Garg 
47*b259732dSSumit Garg 		if (timeout == 0) {
48*b259732dSSumit Garg 			printf("SEC_MON state transition timeout.\n");
49fe78378dSgaurav rana 			return -1;
50*b259732dSSumit Garg 		}
51*b259732dSSumit Garg 		break;
52fe78378dSgaurav rana 
53*b259732dSSumit Garg 	/*
54*b259732dSSumit Garg 	 * If initial state is Trusted, Secure or Soft-Fail, then first set
55*b259732dSSumit Garg 	 * the Software Security Violation Bit and transition to Soft-Fail
56*b259732dSSumit Garg 	 * State.
57*b259732dSSumit Garg 	 */
58*b259732dSSumit Garg 	case HPSR_SSM_ST_TRUST:
59*b259732dSSumit Garg 	case HPSR_SSM_ST_SECURE:
60*b259732dSSumit Garg 	case HPSR_SSM_ST_SOFT_FAIL:
61fe78378dSgaurav rana 		printf("SEC_MON state transitioning to Soft Fail.\n");
62fe78378dSgaurav rana 		sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_SV);
63fe78378dSgaurav rana 
64*b259732dSSumit Garg 		/* polling loop till SEC_MON is in Soft-Fail state */
65fe78378dSgaurav rana 		while (timeout) {
66*b259732dSSumit Garg 			sts = get_sec_mon_state();
67fe78378dSgaurav rana 
68fe78378dSgaurav rana 			if ((sts & HPSR_SSM_ST_MASK) ==
69fe78378dSgaurav rana 				HPSR_SSM_ST_SOFT_FAIL)
70fe78378dSgaurav rana 				break;
71fe78378dSgaurav rana 
72fe78378dSgaurav rana 			udelay(10);
73fe78378dSgaurav rana 			timeout--;
74fe78378dSgaurav rana 		}
75fe78378dSgaurav rana 
76fe78378dSgaurav rana 		if (timeout == 0) {
77fe78378dSgaurav rana 			printf("SEC_MON state transition timeout.\n");
78fe78378dSgaurav rana 			return -1;
79fe78378dSgaurav rana 		}
80fe78378dSgaurav rana 
81fe78378dSgaurav rana 		timeout = 10;
82fe78378dSgaurav rana 
83*b259732dSSumit Garg 		/*
84*b259732dSSumit Garg 		 * If SSM Soft Fail to Non-Secure State Transition
85*b259732dSSumit Garg 		 * disable is not set, then set SSM_ST bit and
86*b259732dSSumit Garg 		 * transition to Non-Secure State.
87*b259732dSSumit Garg 		 */
88*b259732dSSumit Garg 		if ((sec_mon_in32(&sec_mon_regs->hp_com) &
89*b259732dSSumit Garg 			HPCOMR_SSM_SFNS_DIS) == 0) {
90fe78378dSgaurav rana 			printf("SEC_MON state transitioning to Non Secure.\n");
91fe78378dSgaurav rana 			sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SSM_ST);
92fe78378dSgaurav rana 
93*b259732dSSumit Garg 			/* polling loop till SEC_MON is in Non Secure*/
94fe78378dSgaurav rana 			while (timeout) {
95*b259732dSSumit Garg 				sts = get_sec_mon_state();
96fe78378dSgaurav rana 
97fe78378dSgaurav rana 				if ((sts & HPSR_SSM_ST_MASK) ==
98fe78378dSgaurav rana 					HPSR_SSM_ST_NON_SECURE)
99fe78378dSgaurav rana 					break;
100fe78378dSgaurav rana 
101fe78378dSgaurav rana 				udelay(10);
102fe78378dSgaurav rana 				timeout--;
103fe78378dSgaurav rana 			}
104fe78378dSgaurav rana 
105fe78378dSgaurav rana 			if (timeout == 0) {
106fe78378dSgaurav rana 				printf("SEC_MON state transition timeout.\n");
107fe78378dSgaurav rana 				return -1;
108fe78378dSgaurav rana 			}
109fe78378dSgaurav rana 		}
110fe78378dSgaurav rana 		break;
111fe78378dSgaurav rana 	default:
112*b259732dSSumit Garg 		printf("SEC_MON already in Non Secure state.\n");
113fe78378dSgaurav rana 		return 0;
114fe78378dSgaurav rana 	}
115*b259732dSSumit Garg 	return 0;
116*b259732dSSumit Garg }
117*b259732dSSumit Garg 
set_sec_mon_state_soft_fail(void)118*b259732dSSumit Garg static int set_sec_mon_state_soft_fail(void)
119*b259732dSSumit Garg {
120*b259732dSSumit Garg 	u32 sts;
121*b259732dSSumit Garg 	int timeout = 10;
122*b259732dSSumit Garg 	struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
123*b259732dSSumit Garg 						(CONFIG_SYS_SEC_MON_ADDR);
124*b259732dSSumit Garg 
125*b259732dSSumit Garg 	printf("SEC_MON state transitioning to Soft Fail.\n");
126*b259732dSSumit Garg 	sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_FSV);
127*b259732dSSumit Garg 
128*b259732dSSumit Garg 	/* polling loop till SEC_MON is in Soft-Fail state */
129*b259732dSSumit Garg 	while (timeout) {
130*b259732dSSumit Garg 		sts = get_sec_mon_state();
131*b259732dSSumit Garg 
132*b259732dSSumit Garg 		if ((sts & HPSR_SSM_ST_MASK) ==
133*b259732dSSumit Garg 			HPSR_SSM_ST_SOFT_FAIL)
134*b259732dSSumit Garg 			break;
135*b259732dSSumit Garg 
136*b259732dSSumit Garg 		udelay(10);
137*b259732dSSumit Garg 		timeout--;
138*b259732dSSumit Garg 	}
139*b259732dSSumit Garg 
140*b259732dSSumit Garg 	if (timeout == 0) {
141*b259732dSSumit Garg 		printf("SEC_MON state transition timeout.\n");
142*b259732dSSumit Garg 		return -1;
143*b259732dSSumit Garg 	}
144*b259732dSSumit Garg 	return 0;
145*b259732dSSumit Garg }
146*b259732dSSumit Garg 
set_sec_mon_state(u32 state)147*b259732dSSumit Garg int set_sec_mon_state(u32 state)
148*b259732dSSumit Garg {
149*b259732dSSumit Garg 	int ret = -1;
150*b259732dSSumit Garg 
151*b259732dSSumit Garg 	switch (state) {
152*b259732dSSumit Garg 	case HPSR_SSM_ST_NON_SECURE:
153*b259732dSSumit Garg 		ret = set_sec_mon_state_non_sec();
154*b259732dSSumit Garg 		break;
155*b259732dSSumit Garg 	case HPSR_SSM_ST_SOFT_FAIL:
156*b259732dSSumit Garg 		ret = set_sec_mon_state_soft_fail();
157*b259732dSSumit Garg 		break;
158*b259732dSSumit Garg 	default:
159*b259732dSSumit Garg 		printf("SEC_MON state transition not supported.\n");
160*b259732dSSumit Garg 		return 0;
161*b259732dSSumit Garg 	}
162*b259732dSSumit Garg 
163*b259732dSSumit Garg 	return ret;
164*b259732dSSumit Garg }
165