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