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