xref: /openbmc/u-boot/drivers/crypto/fsl/sec.c (revision 2f3f477b)
1 /*
2  * Copyright 2014 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <libfdt.h>
9 #include <fdt_support.h>
10 #if CONFIG_SYS_FSL_SEC_COMPAT == 2 || CONFIG_SYS_FSL_SEC_COMPAT >= 4
11 #include <fsl_sec.h>
12 #endif
13 
14 /*
15  * update crypto node properties to a specified revision of the SEC
16  * called with sec_rev == 0 if not on an E processor
17  */
18 #if CONFIG_SYS_FSL_SEC_COMPAT == 2 /* SEC 2.x/3.x */
19 void fdt_fixup_crypto_node(void *blob, int sec_rev)
20 {
21 	static const struct sec_rev_prop {
22 		u32 sec_rev;
23 		u32 num_channels;
24 		u32 channel_fifo_len;
25 		u32 exec_units_mask;
26 		u32 descriptor_types_mask;
27 	} sec_rev_prop_list[] = {
28 		{ 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
29 		{ 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
30 		{ 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
31 		{ 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
32 		{ 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
33 		{ 0x0301, 4, 24, 0xbfe, 0x03ab0ebf }, /* SEC 3.1 */
34 		{ 0x0303, 4, 24, 0x97c, 0x03a30abf }, /* SEC 3.3 */
35 	};
36 	static char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
37 				   sizeof("fsl,secX.Y")];
38 	int crypto_node, sec_idx, err;
39 	char *p;
40 	u32 val;
41 
42 	/* locate crypto node based on lowest common compatible */
43 	crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
44 	if (crypto_node == -FDT_ERR_NOTFOUND)
45 		return;
46 
47 	/* delete it if not on an E-processor */
48 	if (crypto_node > 0 && !sec_rev) {
49 		fdt_del_node(blob, crypto_node);
50 		return;
51 	}
52 
53 	/* else we got called for possible uprev */
54 	for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
55 		if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
56 			break;
57 
58 	if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
59 		puts("warning: unknown SEC revision number\n");
60 		return;
61 	}
62 
63 	err = fdt_setprop_u32(blob, crypto_node, "fsl,num-channels",
64 			      sec_rev_prop_list[sec_idx].num_channels);
65 	if (err < 0)
66 		printf("WARNING: could not set crypto property: %s\n",
67 		       fdt_strerror(err));
68 
69 	err = fdt_setprop_u32(blob, crypto_node, "fsl,descriptor-types-mask",
70 			      sec_rev_prop_list[sec_idx].descriptor_types_mask);
71 	if (err < 0)
72 		printf("WARNING: could not set crypto property: %s\n",
73 		       fdt_strerror(err));
74 
75 	err = fdt_setprop_u32(blob, crypto_node, "fsl,exec-units-mask",
76 			      sec_rev_prop_list[sec_idx].exec_units_mask);
77 	if (err < 0)
78 		printf("WARNING: could not set crypto property: %s\n",
79 		       fdt_strerror(err));
80 
81 	err = fdt_setprop_u32(blob, crypto_node, "fsl,channel-fifo-len",
82 			      sec_rev_prop_list[sec_idx].channel_fifo_len);
83 	if (err < 0)
84 		printf("WARNING: could not set crypto property: %s\n",
85 		       fdt_strerror(err));
86 
87 	val = 0;
88 	while (sec_idx >= 0) {
89 		p = compat_strlist + val;
90 		val += sprintf(p, "fsl,sec%d.%d",
91 			(sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
92 			sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
93 		sec_idx--;
94 	}
95 	err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist,
96 			  val);
97 	if (err < 0)
98 		printf("WARNING: could not set crypto property: %s\n",
99 		       fdt_strerror(err));
100 }
101 #elif CONFIG_SYS_FSL_SEC_COMPAT >= 4  /* SEC4 */
102 static u8 caam_get_era(void)
103 {
104 	static const struct {
105 		u16 ip_id;
106 		u8 maj_rev;
107 		u8 era;
108 	} caam_eras[] = {
109 		{0x0A10, 1, 1},
110 		{0x0A10, 2, 2},
111 		{0x0A12, 1, 3},
112 		{0x0A14, 1, 3},
113 		{0x0A14, 2, 4},
114 		{0x0A16, 1, 4},
115 		{0x0A10, 3, 4},
116 		{0x0A11, 1, 4},
117 		{0x0A18, 1, 4},
118 		{0x0A11, 2, 5},
119 		{0x0A12, 2, 5},
120 		{0x0A13, 1, 5},
121 		{0x0A1C, 1, 5}
122 	};
123 
124 	ccsr_sec_t __iomem *sec = (void __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
125 	u32 secvid_ms = sec_in32(&sec->secvid_ms);
126 	u32 ccbvid = sec_in32(&sec->ccbvid);
127 	u16 ip_id = (secvid_ms & SEC_SECVID_MS_IPID_MASK) >>
128 				SEC_SECVID_MS_IPID_SHIFT;
129 	u8 maj_rev = (secvid_ms & SEC_SECVID_MS_MAJ_REV_MASK) >>
130 				SEC_SECVID_MS_MAJ_REV_SHIFT;
131 	u8 era = (ccbvid & SEC_CCBVID_ERA_MASK) >> SEC_CCBVID_ERA_SHIFT;
132 
133 	int i;
134 
135 	if (era)	/* This is '0' prior to CAAM ERA-6 */
136 		return era;
137 
138 	for (i = 0; i < ARRAY_SIZE(caam_eras); i++)
139 		if (caam_eras[i].ip_id == ip_id &&
140 		    caam_eras[i].maj_rev == maj_rev)
141 			return caam_eras[i].era;
142 
143 	return 0;
144 }
145 
146 static void fdt_fixup_crypto_era(void *blob, u32 era)
147 {
148 	int err;
149 	int crypto_node;
150 
151 	crypto_node = fdt_path_offset(blob, "crypto");
152 	if (crypto_node < 0) {
153 		printf("WARNING: Missing crypto node\n");
154 		return;
155 	}
156 
157 	err = fdt_setprop_u32(blob, crypto_node, "fsl,sec-era", era);
158 	if (err < 0) {
159 		printf("ERROR: could not set fsl,sec-era property: %s\n",
160 		       fdt_strerror(err));
161 	}
162 }
163 
164 void fdt_fixup_crypto_node(void *blob, int sec_rev)
165 {
166 	u8 era;
167 
168 	if (!sec_rev) {
169 		fdt_del_node_and_alias(blob, "crypto");
170 		return;
171 	}
172 
173 	/* Add SEC ERA information in compatible */
174 	era = caam_get_era();
175 	if (era) {
176 		fdt_fixup_crypto_era(blob, era);
177 	} else {
178 		printf("WARNING: Unable to get ERA for CAAM rev: %d\n",
179 		       sec_rev);
180 	}
181 }
182 #endif
183