1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2022 Microsoft
4  *
5  * Driver for the NXP LX2160A-series Security Monitor (SecMon).
6  */
7 
8 #include <drivers/ls_sec_mon.h>
9 #include <io.h>
10 #include <kernel/boot.h>
11 #include <kernel/dt.h>
12 #include <libfdt.h>
13 #include <mm/core_memprot.h>
14 #include <util.h>
15 
16 /**
17  * struct ls_sec_mon_registers - Memory map of the SecMon registers.
18  * hplr;		HP Lock Register.
19  * @hpcomr:		HP Command Register.
20  * @rsvd0:		Reserved.
21  * @hpsicr:		HP Security Interrupt Control Register.
22  * @hpsvcr:		HP Security Violation Control Register.
23  * @hpsr:		HP Status Register.
24  * @hpsvsr:		HP Security Violation Status Register.
25  * @hphacivr:		HP High Assurance Counter IV Register.
26  * @hphacr:		HP High Assurance Counter Register.
27  * @rsvd1[0x4]:		Reserved.
28  * @lplr:		LP Lock Register.
29  * @lpcr:		LP Control Register.
30  * @lpmkcr:		LP Master Key Control Register.
31  * @lpsvcr:		LP Security Violation Control Register.
32  * @rsvd2:		Reserved.
33  * @lptdcr:		LP Tamper Detectors Configuration.
34  * @lpsr:		LP Status Register.
35  * @rsvd3[0x3]:		Reserved.
36  * @lpsmcmr:		LP Secure Monotonic Counter MSB Register.
37  * @lpsmclr:		LP Secure Monotonic Counter LSB Register.
38  * @lppgdr:		LP Power Glitch Detector Register.
39  * @rsvd4:		Reserved.
40  * @lpzmkr[0x8]:	LP Zeroizable Master Key Registers.
41  * @lpgpr[0x4]:		LP General Purpose Registers.
42  * @rsvd5[0x2d2]:	Reserved.
43  * @hpvidr1:		HP Version ID Register 1.
44  * @hpvidr2:		HP Version ID Register 2.
45  */
46 static struct ls_sec_mon_registers {
47 	uint32_t hplr;			/* 0x000 */
48 	uint32_t hpcomr;		/* 0x004 */
49 	uint32_t rsvd0;			/* 0x008 */
50 	uint32_t hpsicr;		/* 0x00C */
51 	uint32_t hpsvcr;		/* 0x010 */
52 	uint32_t hpsr;			/* 0x014 */
53 	uint32_t hpsvsr;		/* 0x018 */
54 	uint32_t hphacivr;		/* 0x01C */
55 	uint32_t hphacr;		/* 0x020 */
56 	uint32_t rsvd1[0x4];		/* 0x024 */
57 	uint32_t lplr;			/* 0x034 */
58 	uint32_t lpcr;			/* 0x038 */
59 	uint32_t lpmkcr;		/* 0x03C */
60 	uint32_t lpsvcr;		/* 0x040 */
61 	uint32_t rsvd2;			/* 0x044 */
62 	uint32_t lptdcr;		/* 0x048 */
63 	uint32_t lpsr;			/* 0x04C */
64 	uint32_t rsvd3[0x3];		/* 0x050 */
65 	uint32_t lpsmcmr;		/* 0x05C */
66 	uint32_t lpsmclr;		/* 0x060 */
67 	uint32_t lppgdr;		/* 0x064 */
68 	uint32_t rsvd4;			/* 0x068 */
69 	uint32_t lpzmkr[0x8];		/* 0x06C */
70 	uint32_t lpgpr[0x4];		/* 0x090 */
71 	uint32_t rsvd5[0x2d2];		/* 0x0B0 */
72 	uint32_t hpvidr1;		/* 0xBF8 */
73 	uint32_t hpvidr2;		/* 0xBFC */
74 } *sec_mon_regs;
75 
76 /**
77  * ls_sec_mon_init() - Initialize the SecMon driver and assign the sec_mon_regs
78  *		       pointer to the SecMon base address detailed in the device
79  *		       tree.
80  *
81  * Return:	0 if successful or > 0 on error.
82  */
ls_sec_mon_init(void)83 static TEE_Result ls_sec_mon_init(void)
84 {
85 	void *fdt = NULL;
86 	size_t size = 0;
87 	uint32_t node = 0;
88 	vaddr_t ctrl_base = 0;
89 
90 	fdt = get_embedded_dt();
91 	if (!fdt) {
92 		EMSG("Unable to find the device tree");
93 		return TEE_ERROR_ITEM_NOT_FOUND;
94 	}
95 
96 	node = fdt_node_offset_by_compatible(fdt, node, "fsl,lx2160a-sec-mon");
97 	if (node <= 0) {
98 		EMSG("Unable to find the SecMon device tree node");
99 		return TEE_ERROR_ITEM_NOT_FOUND;
100 	}
101 
102 	if (dt_map_dev(fdt, node, &ctrl_base, &size, DT_MAP_AUTO) < 0) {
103 		EMSG("Unable to get the SecMon virtual address");
104 		return TEE_ERROR_GENERIC;
105 	}
106 
107 	sec_mon_regs = (struct ls_sec_mon_registers *)ctrl_base;
108 
109 	return TEE_SUCCESS;
110 }
111 
ls_sec_mon_read(struct ls_sec_mon_data * data)112 TEE_Result ls_sec_mon_read(struct ls_sec_mon_data *data)
113 {
114 	if (!sec_mon_regs) {
115 		EMSG("SecMon driver is not initialized");
116 		return TEE_ERROR_GENERIC;
117 	}
118 
119 	if (!data) {
120 		EMSG("Given buffer is uninitialized");
121 		return TEE_ERROR_BAD_PARAMETERS;
122 	}
123 
124 	data->hplr = io_read32((vaddr_t)&sec_mon_regs->hplr);
125 	data->hpcomr = io_read32((vaddr_t)&sec_mon_regs->hpcomr);
126 	data->hpsicr = io_read32((vaddr_t)&sec_mon_regs->hpsicr);
127 	data->hpsvcr = io_read32((vaddr_t)&sec_mon_regs->hpsvcr);
128 	data->hpsr = io_read32((vaddr_t)&sec_mon_regs->hpsr);
129 	data->hpsvsr = io_read32((vaddr_t)&sec_mon_regs->hpsvsr);
130 	data->hphacivr = io_read32((vaddr_t)&sec_mon_regs->hphacivr);
131 	data->hphacr = io_read32((vaddr_t)&sec_mon_regs->hphacr);
132 	data->lplr = io_read32((vaddr_t)&sec_mon_regs->lplr);
133 	data->lpcr = io_read32((vaddr_t)&sec_mon_regs->lpcr);
134 	data->lpmkcr = io_read32((vaddr_t)&sec_mon_regs->lpmkcr);
135 	data->lpsvcr = io_read32((vaddr_t)&sec_mon_regs->lpsvcr);
136 	data->lptdcr = io_read32((vaddr_t)&sec_mon_regs->lptdcr);
137 	data->lpsr = io_read32((vaddr_t)&sec_mon_regs->lpsr);
138 	data->lpsmcmr = io_read32((vaddr_t)&sec_mon_regs->lpsmcmr);
139 	data->lpsmclr = io_read32((vaddr_t)&sec_mon_regs->lpsmclr);
140 	data->lppgdr = io_read32((vaddr_t)&sec_mon_regs->lppgdr);
141 	data->hpvidr1 = io_read32((vaddr_t)&sec_mon_regs->hpvidr1);
142 	data->hpvidr2 = io_read32((vaddr_t)&sec_mon_regs->hpvidr2);
143 
144 	for (uint32_t i = 0; i < ARRAY_SIZE(data->lpzmkr); ++i)
145 		data->lpzmkr[i] = io_read32((vaddr_t)&sec_mon_regs->lpzmkr[i]);
146 
147 	for (uint32_t i = 0; i < ARRAY_SIZE(data->lpgpr); ++i)
148 		data->lpgpr[i] = io_read32((vaddr_t)&sec_mon_regs->lpgpr[i]);
149 
150 	return TEE_SUCCESS;
151 }
152 
ls_sec_mon_status(void)153 TEE_Result ls_sec_mon_status(void)
154 {
155 	if (!sec_mon_regs)
156 		return TEE_ERROR_GENERIC;
157 
158 	return TEE_SUCCESS;
159 }
160 
161 driver_init(ls_sec_mon_init);
162