crypto: caam: Fix build warnings pointer casting
[platform/kernel/u-boot.git] / include / fsl_sec.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Common internal memory map for some Freescale SoCs
4  *
5  * Copyright 2014 Freescale Semiconductor, Inc.
6  * Copyright 2018 NXP
7  */
8
9 #ifndef __FSL_SEC_H
10 #define __FSL_SEC_H
11
12 #include <common.h>
13 #include <asm/io.h>
14
15 #ifdef CONFIG_SYS_FSL_SEC_LE
16 #define sec_in32(a)       in_le32((ulong *)(ulong)a)
17 #define sec_out32(a, v)   out_le32((ulong *)(ulong)a, v)
18 #define sec_in16(a)       in_le16(a)
19 #define sec_clrbits32     clrbits_le32
20 #define sec_setbits32     setbits_le32
21 #elif defined(CONFIG_SYS_FSL_SEC_BE)
22 #define sec_in32(a)       in_be32(a)
23 #define sec_out32(a, v)   out_be32(a, v)
24 #define sec_in16(a)       in_be16(a)
25 #define sec_clrbits32     clrbits_be32
26 #define sec_setbits32     setbits_be32
27 #elif defined(CONFIG_SYS_FSL_HAS_SEC)
28 #error Neither CONFIG_SYS_FSL_SEC_LE nor CONFIG_SYS_FSL_SEC_BE is defined
29 #endif
30
31 /* Security Engine Block (MS = Most Sig., LS = Least Sig.) */
32 #if CONFIG_SYS_FSL_SEC_COMPAT >= 4
33 /* RNG4 TRNG test registers */
34 struct rng4tst {
35 #define RTMCTL_PRGM 0x00010000  /* 1 -> program mode, 0 -> run mode */
36 #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC     0 /* use von Neumann data in
37                                                     both entropy shifter and
38                                                     statistical checker */
39 #define RTMCTL_SAMP_MODE_RAW_ES_SC             1 /* use raw data in both
40                                                     entropy shifter and
41                                                     statistical checker */
42 #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_RAW_SC 2 /* use von Neumann data in
43                                                     entropy shifter, raw data
44                                                     in statistical checker */
45 #define RTMCTL_SAMP_MODE_INVALID               3 /* invalid combination */
46         u32 rtmctl;             /* misc. control register */
47         u32 rtscmisc;           /* statistical check misc. register */
48         u32 rtpkrrng;           /* poker range register */
49 #define RTSDCTL_ENT_DLY_MIN     3200
50 #define RTSDCTL_ENT_DLY_MAX     12800
51         union {
52                 u32 rtpkrmax;   /* PRGM=1: poker max. limit register */
53                 u32 rtpkrsq;    /* PRGM=0: poker square calc. result register */
54         };
55 #define RTSDCTL_ENT_DLY_SHIFT 16
56 #define RTSDCTL_ENT_DLY_MASK (0xffff << RTSDCTL_ENT_DLY_SHIFT)
57         u32 rtsdctl;            /* seed control register */
58         union {
59                 u32 rtsblim;    /* PRGM=1: sparse bit limit register */
60                 u32 rttotsam;   /* PRGM=0: total samples register */
61         };
62         u32 rtfreqmin;          /* frequency count min. limit register */
63 #define RTFRQMAX_DISABLE       (1 << 20)
64         union {
65                 u32 rtfreqmax;  /* PRGM=1: freq. count max. limit register */
66                 u32 rtfreqcnt;  /* PRGM=0: freq. count register */
67         };
68         u32 rsvd1[40];
69 #define RDSTA_IF(idx) (0x00000001 << (idx))
70 #define RDSTA_PR(idx) (0x00000010 << (idx))
71 #define RDSTA_MASK (RDSTA_PR(1) | RDSTA_PR(0) | RDSTA_IF(1) | RDSTA_IF(0))
72 #define RDSTA_SKVN 0x40000000
73         u32 rdsta;              /*RNG DRNG Status Register*/
74         u32 rsvd2[15];
75 };
76
77 /* Version registers (Era 10+) */
78 struct version_regs {
79         u32 crca;       /* CRCA_VERSION */
80         u32 afha;       /* AFHA_VERSION */
81         u32 kfha;       /* KFHA_VERSION */
82         u32 pkha;       /* PKHA_VERSION */
83         u32 aesa;       /* AESA_VERSION */
84         u32 mdha;       /* MDHA_VERSION */
85         u32 desa;       /* DESA_VERSION */
86         u32 snw8a;      /* SNW8A_VERSION */
87         u32 snw9a;      /* SNW9A_VERSION */
88         u32 zuce;       /* ZUCE_VERSION */
89         u32 zuca;       /* ZUCA_VERSION */
90         u32 ccha;       /* CCHA_VERSION */
91         u32 ptha;       /* PTHA_VERSION */
92         u32 rng;        /* RNG_VERSION */
93         u32 trng;       /* TRNG_VERSION */
94         u32 aaha;       /* AAHA_VERSION */
95         u32 rsvd[10];
96         u32 sr;         /* SR_VERSION */
97         u32 dma;        /* DMA_VERSION */
98         u32 ai;         /* AI_VERSION */
99         u32 qi;         /* QI_VERSION */
100         u32 jr;         /* JR_VERSION */
101         u32 deco;       /* DECO_VERSION */
102 };
103
104 #define CHA_VER_NUM_MASK        0x000000ff
105 #define CHA_VER_MISC_SHIFT      8
106 #define CHA_VER_MISC_MASK       0x0000ff00
107 #define CHA_VER_REV_SHIFT       16
108 #define CHA_VER_REV_MASK        0x00ff0000
109 #define CHA_VER_VID_SHIFT       24
110 #define CHA_VER_VID_MASK        0xff000000
111
112 typedef struct ccsr_sec {
113         u32     res0;
114         u32     mcfgr;          /* Master CFG Register */
115         u8      res1[0x4];
116         u32     scfgr;
117         struct {
118                 u32     ms;     /* Job Ring LIODN Register, MS */
119                 u32     ls;     /* Job Ring LIODN Register, LS */
120         } jrliodnr[4];
121         u8      res2[0x2c];
122         u32     jrstartr;       /* Job Ring Start Register */
123         struct {
124                 u32     ms;     /* RTIC LIODN Register, MS */
125                 u32     ls;     /* RTIC LIODN Register, LS */
126         } rticliodnr[4];
127         u8      res3[0x1c];
128         u32     decorr;         /* DECO Request Register */
129         struct {
130                 u32     ms;     /* DECO LIODN Register, MS */
131                 u32     ls;     /* DECO LIODN Register, LS */
132         } decoliodnr[16];
133         u32     dar;            /* DECO Avail Register */
134         u32     drr;            /* DECO Reset Register */
135         u8      res5[0x4d8];
136         struct rng4tst rng;     /* RNG Registers */
137         u8      res6[0x780];
138         struct version_regs vreg; /* version registers since era 10 */
139         u8      res7[0xa0];
140         u32     crnr_ms;        /* CHA Revision Number Register, MS */
141         u32     crnr_ls;        /* CHA Revision Number Register, LS */
142         u32     ctpr_ms;        /* Compile Time Parameters Register, MS */
143         u32     ctpr_ls;        /* Compile Time Parameters Register, LS */
144         u8      res8[0x10];
145         u32     far_ms;         /* Fault Address Register, MS */
146         u32     far_ls;         /* Fault Address Register, LS */
147         u32     falr;           /* Fault Address LIODN Register */
148         u32     fadr;           /* Fault Address Detail Register */
149         u8      res9[0x4];
150         u32     csta;           /* CAAM Status Register */
151         u32     smpart;         /* Secure Memory Partition Parameters */
152         u32     smvid;          /* Secure Memory Version ID */
153         u32     rvid;           /* Run Time Integrity Checking Version ID Reg.*/
154         u32     ccbvid;         /* CHA Cluster Block Version ID Register */
155         u32     chavid_ms;      /* CHA Version ID Register, MS */
156         u32     chavid_ls;      /* CHA Version ID Register, LS */
157         u32     chanum_ms;      /* CHA Number Register, MS */
158         u32     chanum_ls;      /* CHA Number Register, LS */
159         u32     secvid_ms;      /* SEC Version ID Register, MS */
160         u32     secvid_ls;      /* SEC Version ID Register, LS */
161 #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3)
162         u8      res10[0x6f020];
163 #else
164         u8      res10[0x6020];
165 #endif
166         u32     qilcr_ms;       /* Queue Interface LIODN CFG Register, MS */
167         u32     qilcr_ls;       /* Queue Interface LIODN CFG Register, LS */
168 #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3)
169         u8      res11[0x8ffd8];
170 #else
171         u8      res11[0x8fd8];
172 #endif
173 } ccsr_sec_t;
174
175 #define SEC_CTPR_MS_AXI_LIODN           0x08000000
176 #define SEC_CTPR_MS_QI                  0x02000000
177 #define SEC_CTPR_MS_VIRT_EN_INCL        0x00000001
178 #define SEC_CTPR_MS_VIRT_EN_POR         0x00000002
179 #define SEC_RVID_MA                     0x0f000000
180 #define SEC_CHANUM_MS_JRNUM_MASK        0xf0000000
181 #define SEC_CHANUM_MS_JRNUM_SHIFT       28
182 #define SEC_CHANUM_MS_DECONUM_MASK      0x0f000000
183 #define SEC_CHANUM_MS_DECONUM_SHIFT     24
184 #define SEC_SECVID_MS_IPID_MASK 0xffff0000
185 #define SEC_SECVID_MS_IPID_SHIFT        16
186 #define SEC_SECVID_MS_MAJ_REV_MASK      0x0000ff00
187 #define SEC_SECVID_MS_MAJ_REV_SHIFT     8
188 #define SEC_CCBVID_ERA_MASK             0xff000000
189 #define SEC_CCBVID_ERA_SHIFT            24
190 #define SEC_SCFGR_RDBENABLE             0x00000400
191 #define SEC_SCFGR_VIRT_EN               0x00008000
192 #define SEC_CHAVID_LS_RNG_SHIFT         16
193 #define SEC_CHAVID_RNG_LS_MASK          0x000f0000
194
195 #define CONFIG_JRSTARTR_JR0             0x00000001
196
197 struct jr_regs {
198 #if defined(CONFIG_SYS_FSL_SEC_LE) && \
199         !(defined(CONFIG_MX6) || defined(CONFIG_MX7) || \
200           defined(CONFIG_MX7ULP) || defined(CONFIG_IMX8M))
201         u32 irba_l;
202         u32 irba_h;
203 #else
204         u32 irba_h;
205         u32 irba_l;
206 #endif
207         u32 rsvd1;
208         u32 irs;
209         u32 rsvd2;
210         u32 irsa;
211         u32 rsvd3;
212         u32 irja;
213 #if defined(CONFIG_SYS_FSL_SEC_LE) && \
214         !(defined(CONFIG_MX6) || defined(CONFIG_MX7) || \
215           defined(CONFIG_MX7ULP) || defined(CONFIG_IMX8M))
216         u32 orba_l;
217         u32 orba_h;
218 #else
219         u32 orba_h;
220         u32 orba_l;
221 #endif
222         u32 rsvd4;
223         u32 ors;
224         u32 rsvd5;
225         u32 orjr;
226         u32 rsvd6;
227         u32 orsf;
228         u32 rsvd7;
229         u32 jrsta;
230         u32 rsvd8;
231         u32 jrint;
232         u32 jrcfg0;
233         u32 jrcfg1;
234         u32 rsvd9;
235         u32 irri;
236         u32 rsvd10;
237         u32 orwi;
238         u32 rsvd11;
239         u32 jrcr;
240 };
241
242 /*
243  * Scatter Gather Entry - Specifies the the Scatter Gather Format
244  * related information
245  */
246 struct sg_entry {
247 #if defined(CONFIG_SYS_FSL_SEC_LE) && \
248         !(defined(CONFIG_MX6) || defined(CONFIG_MX7) || \
249           defined(CONFIG_MX7ULP) || defined(CONFIG_IMX8M))
250         uint32_t addr_lo;       /* Memory Address - lo */
251         uint32_t addr_hi;       /* Memory Address of start of buffer - hi */
252 #else
253         uint32_t addr_hi;       /* Memory Address of start of buffer - hi */
254         uint32_t addr_lo;       /* Memory Address - lo */
255 #endif
256
257         uint32_t len_flag;      /* Length of the data in the frame */
258 #define SG_ENTRY_LENGTH_MASK    0x3FFFFFFF
259 #define SG_ENTRY_EXTENSION_BIT  0x80000000
260 #define SG_ENTRY_FINAL_BIT      0x40000000
261         uint32_t bpid_offset;
262 #define SG_ENTRY_BPID_MASK      0x00FF0000
263 #define SG_ENTRY_BPID_SHIFT     16
264 #define SG_ENTRY_OFFSET_MASK    0x00001FFF
265 #define SG_ENTRY_OFFSET_SHIFT   0
266 };
267
268 #define BLOB_SIZE(x)            ((x) + 32 + 16) /* Blob buffer size */
269
270 #if defined(CONFIG_MX6) || defined(CONFIG_MX7) || \
271         defined(CONFIG_MX7ULP) || defined(CONFIG_IMX8M)
272 /* Job Ring Base Address */
273 #define JR_BASE_ADDR(x) (CONFIG_SYS_FSL_SEC_ADDR + 0x1000 * (x + 1))
274 /* Secure Memory Offset varies accross versions */
275 #define SM_V1_OFFSET 0x0f4
276 #define SM_V2_OFFSET 0xa00
277 /*Secure Memory Versioning */
278 #define SMVID_V2 0x20105
279 #define SM_VERSION(x)  (x < SMVID_V2 ? 1 : 2)
280 #define SM_OFFSET(x)  (x == 1 ? SM_V1_OFFSET : SM_V2_OFFSET)
281 /* CAAM Job Ring 0 Registers */
282 /* Secure Memory Partition Owner register */
283 #define SMCSJR_PO               (3 << 6)
284 /* JR Allocation Error */
285 #define SMCSJR_AERR             (3 << 12)
286 /* Secure memory partition 0 page 0 owner register */
287 #define CAAM_SMPO_0         (CONFIG_SYS_FSL_SEC_ADDR + 0x1FBC)
288 /* Secure memory command register */
289 #define CAAM_SMCJR(v, jr)   (JR_BASE_ADDR(jr) + SM_OFFSET(v) + SM_CMD(v))
290 /* Secure memory command status register */
291 #define CAAM_SMCSJR(v, jr)  (JR_BASE_ADDR(jr) + SM_OFFSET(v) + SM_STATUS(v))
292 /* Secure memory access permissions register */
293 #define CAAM_SMAPJR(v, jr, y) \
294         (JR_BASE_ADDR(jr) + SM_OFFSET(v) + SM_PERM(v) + y * 16)
295 /* Secure memory access group 2 register */
296 #define CAAM_SMAG2JR(v, jr, y) \
297         (JR_BASE_ADDR(jr) + SM_OFFSET(v) + SM_GROUP2(v) + y * 16)
298 /* Secure memory access group 1 register */
299 #define CAAM_SMAG1JR(v, jr, y)  \
300         (JR_BASE_ADDR(jr) + SM_OFFSET(v) + SM_GROUP1(v) + y * 16)
301
302 /* Commands and macros for secure memory */
303 #define SM_CMD(v)               (v == 1 ? 0x0 : 0x1E4)
304 #define SM_STATUS(v)            (v == 1 ? 0x8 : 0x1EC)
305 #define SM_PERM(v)              (v == 1 ?  0x10 : 0x4)
306 #define SM_GROUP2(v)            (v == 1 ? 0x14 : 0x8)
307 #define SM_GROUP1(v)            (v == 1 ? 0x18 : 0xC)
308 #define CMD_PAGE_ALLOC          0x1
309 #define CMD_PAGE_DEALLOC        0x2
310 #define CMD_PART_DEALLOC        0x3
311 #define CMD_INQUIRY             0x5
312 #define CMD_COMPLETE            (3 << 14)
313 #define PAGE_AVAILABLE          0
314 #define PAGE_OWNED              (3 << 6)
315 #define PAGE(x)                 (x << 16)
316 #define PARTITION(x)            (x << 8)
317 #define PARTITION_OWNER(x)      (0x3 << (x*2))
318
319 /* Address of secure 4kbyte pages */
320 #define SEC_MEM_PAGE0           CAAM_ARB_BASE_ADDR
321 #define SEC_MEM_PAGE1           (CAAM_ARB_BASE_ADDR + 0x1000)
322 #define SEC_MEM_PAGE2           (CAAM_ARB_BASE_ADDR + 0x2000)
323 #define SEC_MEM_PAGE3           (CAAM_ARB_BASE_ADDR + 0x3000)
324
325 #define JR_MID                  2               /* Matches ROM configuration */
326 #define KS_G1                   (1 << JR_MID)   /* CAAM only */
327 #define PERM                    0x0000B008      /* Clear on release, lock SMAP
328                                                  * lock SMAG group 1 Blob */
329
330 /* HAB WRAPPED KEY header */
331 #define WRP_HDR_SIZE            0x08
332 #define HDR_TAG                 0x81
333 #define HDR_PAR                 0x41
334 /* HAB WRAPPED KEY Data */
335 #define HAB_MOD                 0x66
336 #define HAB_ALG                 0x55
337 #define HAB_FLG                 0x00
338
339 /* Partition and Page IDs */
340 #define PARTITION_1     1
341 #define PAGE_1                  1
342
343 #define ERROR_IN_PAGE_ALLOC     1
344 #define ECONSTRJDESC   -1
345
346 #endif
347
348 #define FSL_CAAM_MP_PUBK_BYTES              64
349 #define FSL_CAAM_MP_PRVK_BYTES              32
350 #define FSL_CAAM_MP_MES_DGST_BYTES          32
351
352 #define FSL_CAAM_ORSR_JRa_OFFSET        0x102c
353 #define FSL_CAAM_MAX_JR_SIZE            4
354
355 /* blob_dek:
356  * Encapsulates the src in a secure blob and stores it dst
357  * @src: reference to the plaintext
358  * @dst: reference to the output adrress
359  * @len: size in bytes of src
360  * @return: 0 on success, error otherwise
361  */
362 int blob_dek(const u8 *src, u8 *dst, u8 len);
363
364 int gen_mppubk(u8 *dst);
365
366 int sign_mppubk(const u8 *m, int data_size, u8 *dgst, u8 *c, u8 *d);
367
368 #if defined(CONFIG_ARCH_C29X)
369 int sec_init_idx(uint8_t);
370 #endif
371 int sec_init(void);
372
373 u8 caam_get_era(void);
374 #endif
375
376 #endif /* __FSL_SEC_H */