summaryrefslogtreecommitdiffstats
path: root/drivers/misc/fsl_sec_mon.c
blob: e9f351dc3aa468424cebb34d5a11aeb4438f4dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2015 Freescale Semiconductor, Inc.
 */

#include <common.h>
#include <fsl_sec_mon.h>

static u32 get_sec_mon_state(void)
{
	struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
						(CONFIG_SYS_SEC_MON_ADDR);
	return sec_mon_in32(&sec_mon_regs->hp_stat) & HPSR_SSM_ST_MASK;
}

static int set_sec_mon_state_non_sec(void)
{
	u32 sts;
	int timeout = 10;
	struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
						(CONFIG_SYS_SEC_MON_ADDR);

	sts = get_sec_mon_state();

	switch (sts) {
	/*
	 * If initial state is check or Non-Secure, then set the Software
	 * Security Violation Bit and transition to Non-Secure State.
	 */
	case HPSR_SSM_ST_CHECK:
		printf("SEC_MON state transitioning to Non Secure.\n");
		sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_SV);

		/* polling loop till SEC_MON is in Non Secure state */
		while (timeout) {
			sts = get_sec_mon_state();

			if ((sts & HPSR_SSM_ST_MASK) ==
				HPSR_SSM_ST_NON_SECURE)
				break;

			udelay(10);
			timeout--;
		}

		if (timeout == 0) {
			printf("SEC_MON state transition timeout.\n");
			return -1;
		}
		break;

	/*
	 * If initial state is Trusted, Secure or Soft-Fail, then first set
	 * the Software Security Violation Bit and transition to Soft-Fail
	 * State.
	 */
	case HPSR_SSM_ST_TRUST:
	case HPSR_SSM_ST_SECURE:
	case HPSR_SSM_ST_SOFT_FAIL:
		printf("SEC_MON state transitioning to Soft Fail.\n");
		sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_SV);

		/* polling loop till SEC_MON is in Soft-Fail state */
		while (timeout) {
			sts = get_sec_mon_state();

			if ((sts & HPSR_SSM_ST_MASK) ==
				HPSR_SSM_ST_SOFT_FAIL)
				break;

			udelay(10);
			timeout--;
		}

		if (timeout == 0) {
			printf("SEC_MON state transition timeout.\n");
			return -1;
		}

		timeout = 10;

		/*
		 * If SSM Soft Fail to Non-Secure State Transition
		 * disable is not set, then set SSM_ST bit and
		 * transition to Non-Secure State.
		 */
		if ((sec_mon_in32(&sec_mon_regs->hp_com) &
			HPCOMR_SSM_SFNS_DIS) == 0) {
			printf("SEC_MON state transitioning to Non Secure.\n");
			sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SSM_ST);

			/* polling loop till SEC_MON is in Non Secure*/
			while (timeout) {
				sts = get_sec_mon_state();

				if ((sts & HPSR_SSM_ST_MASK) ==
					HPSR_SSM_ST_NON_SECURE)
					break;

				udelay(10);
				timeout--;
			}

			if (timeout == 0) {
				printf("SEC_MON state transition timeout.\n");
				return -1;
			}
		}
		break;
	default:
		printf("SEC_MON already in Non Secure state.\n");
		return 0;
	}
	return 0;
}

static int set_sec_mon_state_soft_fail(void)
{
	u32 sts;
	int timeout = 10;
	struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
						(CONFIG_SYS_SEC_MON_ADDR);

	printf("SEC_MON state transitioning to Soft Fail.\n");
	sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_FSV);

	/* polling loop till SEC_MON is in Soft-Fail state */
	while (timeout) {
		sts = get_sec_mon_state();

		if ((sts & HPSR_SSM_ST_MASK) ==
			HPSR_SSM_ST_SOFT_FAIL)
			break;

		udelay(10);
		timeout--;
	}

	if (timeout == 0) {
		printf("SEC_MON state transition timeout.\n");
		return -1;
	}
	return 0;
}

int set_sec_mon_state(u32 state)
{
	int ret = -1;

	switch (state) {
	case HPSR_SSM_ST_NON_SECURE:
		ret = set_sec_mon_state_non_sec();
		break;
	case HPSR_SSM_ST_SOFT_FAIL:
		ret = set_sec_mon_state_soft_fail();
		break;
	default:
		printf("SEC_MON state transition not supported.\n");
		return 0;
	}

	return ret;
}