xref: /openbmc/u-boot/include/fsl_sec.h (revision ff255e83)
1 /*
2  * Common internal memory map for some Freescale SoCs
3  *
4  * Copyright 2014 Freescale Semiconductor, Inc.
5  *
6  */
7 
8 #ifndef __FSL_SEC_H
9 #define __FSL_SEC_H
10 
11 #include <common.h>
12 #include <asm/io.h>
13 
14 #ifdef CONFIG_SYS_FSL_SEC_LE
15 #define sec_in32(a)       in_le32(a)
16 #define sec_out32(a, v)   out_le32(a, v)
17 #define sec_in16(a)       in_le16(a)
18 #define sec_clrbits32     clrbits_le32
19 #define sec_setbits32     setbits_le32
20 #elif defined(CONFIG_SYS_FSL_SEC_BE)
21 #define sec_in32(a)       in_be32(a)
22 #define sec_out32(a, v)   out_be32(a, v)
23 #define sec_in16(a)       in_be16(a)
24 #define sec_clrbits32     clrbits_be32
25 #define sec_setbits32     setbits_be32
26 #else
27 #error Neither CONFIG_SYS_FSL_SEC_LE nor CONFIG_SYS_FSL_SEC_BE is defined
28 #endif
29 
30 /* Security Engine Block (MS = Most Sig., LS = Least Sig.) */
31 #if CONFIG_SYS_FSL_SEC_COMPAT >= 4
32 /* RNG4 TRNG test registers */
33 struct rng4tst {
34 #define RTMCTL_PRGM 0x00010000	/* 1 -> program mode, 0 -> run mode */
35 	u32 rtmctl;		/* misc. control register */
36 	u32 rtscmisc;		/* statistical check misc. register */
37 	u32 rtpkrrng;		/* poker range register */
38 #define RTSDCTL_ENT_DLY_MIN	1200
39 #define RTSDCTL_ENT_DLY_MAX	12800
40 	union {
41 		u32 rtpkrmax;	/* PRGM=1: poker max. limit register */
42 		u32 rtpkrsq;	/* PRGM=0: poker square calc. result register */
43 	};
44 #define RTSDCTL_ENT_DLY_SHIFT 16
45 #define RTSDCTL_ENT_DLY_MASK (0xffff << RTSDCTL_ENT_DLY_SHIFT)
46 	u32 rtsdctl;		/* seed control register */
47 	union {
48 		u32 rtsblim;	/* PRGM=1: sparse bit limit register */
49 		u32 rttotsam;	/* PRGM=0: total samples register */
50 	};
51 	u32 rtfreqmin;		/* frequency count min. limit register */
52 	union {
53 		u32 rtfreqmax;	/* PRGM=1: freq. count max. limit register */
54 		u32 rtfreqcnt;	/* PRGM=0: freq. count register */
55 	};
56 	u32 rsvd1[40];
57 #define RNG_STATE0_HANDLE_INSTANTIATED	0x00000001
58 	u32 rdsta;		/*RNG DRNG Status Register*/
59 	u32 rsvd2[15];
60 };
61 
62 typedef struct ccsr_sec {
63 	u32	res0;
64 	u32	mcfgr;		/* Master CFG Register */
65 	u8	res1[0x4];
66 	u32	scfgr;
67 	struct {
68 		u32	ms;	/* Job Ring LIODN Register, MS */
69 		u32	ls;	/* Job Ring LIODN Register, LS */
70 	} jrliodnr[4];
71 	u8	res2[0x2c];
72 	u32	jrstartr;	/* Job Ring Start Register */
73 	struct {
74 		u32	ms;	/* RTIC LIODN Register, MS */
75 		u32	ls;	/* RTIC LIODN Register, LS */
76 	} rticliodnr[4];
77 	u8	res3[0x1c];
78 	u32	decorr;		/* DECO Request Register */
79 	struct {
80 		u32	ms;	/* DECO LIODN Register, MS */
81 		u32	ls;	/* DECO LIODN Register, LS */
82 	} decoliodnr[8];
83 	u8	res4[0x40];
84 	u32	dar;		/* DECO Avail Register */
85 	u32	drr;		/* DECO Reset Register */
86 	u8	res5[0x4d8];
87 	struct rng4tst rng;	/* RNG Registers */
88 	u8	res11[0x8a0];
89 	u32	crnr_ms;	/* CHA Revision Number Register, MS */
90 	u32	crnr_ls;	/* CHA Revision Number Register, LS */
91 	u32	ctpr_ms;	/* Compile Time Parameters Register, MS */
92 	u32	ctpr_ls;	/* Compile Time Parameters Register, LS */
93 	u8	res6[0x10];
94 	u32	far_ms;		/* Fault Address Register, MS */
95 	u32	far_ls;		/* Fault Address Register, LS */
96 	u32	falr;		/* Fault Address LIODN Register */
97 	u32	fadr;		/* Fault Address Detail Register */
98 	u8	res7[0x4];
99 	u32	csta;		/* CAAM Status Register */
100 	u8	res8[0x8];
101 	u32	rvid;		/* Run Time Integrity Checking Version ID Reg.*/
102 	u32	ccbvid;		/* CHA Cluster Block Version ID Register */
103 	u32	chavid_ms;	/* CHA Version ID Register, MS */
104 	u32	chavid_ls;	/* CHA Version ID Register, LS */
105 	u32	chanum_ms;	/* CHA Number Register, MS */
106 	u32	chanum_ls;	/* CHA Number Register, LS */
107 	u32	secvid_ms;	/* SEC Version ID Register, MS */
108 	u32	secvid_ls;	/* SEC Version ID Register, LS */
109 	u8	res9[0x6020];
110 	u32	qilcr_ms;	/* Queue Interface LIODN CFG Register, MS */
111 	u32	qilcr_ls;	/* Queue Interface LIODN CFG Register, LS */
112 	u8	res10[0x8fd8];
113 } ccsr_sec_t;
114 
115 #define SEC_CTPR_MS_AXI_LIODN		0x08000000
116 #define SEC_CTPR_MS_QI			0x02000000
117 #define SEC_CTPR_MS_VIRT_EN_INCL	0x00000001
118 #define SEC_CTPR_MS_VIRT_EN_POR		0x00000002
119 #define SEC_RVID_MA			0x0f000000
120 #define SEC_CHANUM_MS_JRNUM_MASK	0xf0000000
121 #define SEC_CHANUM_MS_JRNUM_SHIFT	28
122 #define SEC_CHANUM_MS_DECONUM_MASK	0x0f000000
123 #define SEC_CHANUM_MS_DECONUM_SHIFT	24
124 #define SEC_SECVID_MS_IPID_MASK	0xffff0000
125 #define SEC_SECVID_MS_IPID_SHIFT	16
126 #define SEC_SECVID_MS_MAJ_REV_MASK	0x0000ff00
127 #define SEC_SECVID_MS_MAJ_REV_SHIFT	8
128 #define SEC_CCBVID_ERA_MASK		0xff000000
129 #define SEC_CCBVID_ERA_SHIFT		24
130 #define SEC_SCFGR_RDBENABLE		0x00000400
131 #define SEC_SCFGR_VIRT_EN		0x00008000
132 #define SEC_CHAVID_LS_RNG_SHIFT		16
133 #define SEC_CHAVID_RNG_LS_MASK		0x000f0000
134 
135 #define CONFIG_JRSTARTR_JR0		0x00000001
136 
137 struct jr_regs {
138 #if defined(CONFIG_SYS_FSL_SEC_LE) && !defined(CONFIG_MX6)
139 	u32 irba_l;
140 	u32 irba_h;
141 #else
142 	u32 irba_h;
143 	u32 irba_l;
144 #endif
145 	u32 rsvd1;
146 	u32 irs;
147 	u32 rsvd2;
148 	u32 irsa;
149 	u32 rsvd3;
150 	u32 irja;
151 #if defined(CONFIG_SYS_FSL_SEC_LE) && !defined(CONFIG_MX6)
152 	u32 orba_l;
153 	u32 orba_h;
154 #else
155 	u32 orba_h;
156 	u32 orba_l;
157 #endif
158 	u32 rsvd4;
159 	u32 ors;
160 	u32 rsvd5;
161 	u32 orjr;
162 	u32 rsvd6;
163 	u32 orsf;
164 	u32 rsvd7;
165 	u32 jrsta;
166 	u32 rsvd8;
167 	u32 jrint;
168 	u32 jrcfg0;
169 	u32 jrcfg1;
170 	u32 rsvd9;
171 	u32 irri;
172 	u32 rsvd10;
173 	u32 orwi;
174 	u32 rsvd11;
175 	u32 jrcr;
176 };
177 
178 /*
179  * Scatter Gather Entry - Specifies the the Scatter Gather Format
180  * related information
181  */
182 struct sg_entry {
183 #if defined(CONFIG_SYS_FSL_SEC_LE) && !defined(CONFIG_MX6)
184 	uint32_t addr_lo;	/* Memory Address - lo */
185 	uint16_t addr_hi;	/* Memory Address of start of buffer - hi */
186 	uint16_t reserved_zero;
187 #else
188 	uint16_t reserved_zero;
189 	uint16_t addr_hi;	/* Memory Address of start of buffer - hi */
190 	uint32_t addr_lo;	/* Memory Address - lo */
191 #endif
192 
193 	uint32_t len_flag;	/* Length of the data in the frame */
194 #define SG_ENTRY_LENGTH_MASK	0x3FFFFFFF
195 #define SG_ENTRY_EXTENSION_BIT	0x80000000
196 #define SG_ENTRY_FINAL_BIT	0x40000000
197 	uint32_t bpid_offset;
198 #define SG_ENTRY_BPID_MASK	0x00FF0000
199 #define SG_ENTRY_BPID_SHIFT	16
200 #define SG_ENTRY_OFFSET_MASK	0x00001FFF
201 #define SG_ENTRY_OFFSET_SHIFT	0
202 };
203 
204 #ifdef CONFIG_MX6
205 /* CAAM Job Ring 0 Registers */
206 /* Secure Memory Partition Owner register */
207 #define SMCSJR_PO		(3 << 6)
208 /* JR Allocation Error */
209 #define SMCSJR_AERR		(3 << 12)
210 /* Secure memory partition 0 page 0 owner register */
211 #define CAAM_SMPO_0		CONFIG_SYS_FSL_SEC_ADDR + 0x1FBC
212 /* Secure memory command register */
213 #define CAAM_SMCJR0		CONFIG_SYS_FSL_SEC_ADDR + 0x10f4
214 /* Secure memory command status register */
215 #define CAAM_SMCSJR0		CONFIG_SYS_FSL_SEC_ADDR + 0x10fc
216 /* Secure memory access permissions register */
217 #define CAAM_SMAPJR0(y)	(CONFIG_SYS_FSL_SEC_ADDR + 0x1104 + y*16)
218 /* Secure memory access group 2 register */
219 #define CAAM_SMAG2JR0(y)	(CONFIG_SYS_FSL_SEC_ADDR + 0x1108 + y*16)
220 /* Secure memory access group 1 register */
221 #define CAAM_SMAG1JR0(y)	(CONFIG_SYS_FSL_SEC_ADDR + 0x110C + y*16)
222 
223 /* Commands and macros for secure memory */
224 #define CMD_PAGE_ALLOC		0x1
225 #define CMD_PAGE_DEALLOC	0x2
226 #define CMD_PART_DEALLOC	0x3
227 #define CMD_INQUIRY		0x5
228 #define CMD_COMPLETE		(3 << 14)
229 #define PAGE_AVAILABLE		0
230 #define PAGE_OWNED		(3 << 6)
231 #define PAGE(x)			(x << 16)
232 #define PARTITION(x)		(x << 8)
233 #define PARTITION_OWNER(x)	(0x3 << (x*2))
234 
235 /* Address of secure 4kbyte pages */
236 #define SEC_MEM_PAGE0		CAAM_ARB_BASE_ADDR
237 #define SEC_MEM_PAGE1		(CAAM_ARB_BASE_ADDR + 0x1000)
238 #define SEC_MEM_PAGE2		(CAAM_ARB_BASE_ADDR + 0x2000)
239 #define SEC_MEM_PAGE3		(CAAM_ARB_BASE_ADDR + 0x3000)
240 
241 #define JR_MID			2               /* Matches ROM configuration */
242 #define KS_G1			(1 << JR_MID)   /* CAAM only */
243 #define PERM			0x0000B008      /* Clear on release, lock SMAP
244 						 * lock SMAG group 1 Blob */
245 
246 #define BLOB_SIZE(x)       (x + 32 + 16) /* Blob buffer size */
247 
248 /* HAB WRAPPED KEY header */
249 #define WRP_HDR_SIZE		0x08
250 #define HDR_TAG			0x81
251 #define HDR_PAR			0x41
252 /* HAB WRAPPED KEY Data */
253 #define HAB_MOD			0x66
254 #define HAB_ALG			0x55
255 #define HAB_FLG			0x00
256 
257 /* Partition and Page IDs */
258 #define PARTITION_1	1
259 #define PAGE_1			1
260 
261 #define ERROR_IN_PAGE_ALLOC	1
262 #define ECONSTRJDESC   -1
263 
264 #endif
265 
266 int sec_init(void);
267 
268 /* blob_dek:
269  * Encapsulates the src in a secure blob and stores it dst
270  * @src: reference to the plaintext
271  * @dst: reference to the output adrress
272  * @len: size in bytes of src
273  * @return: 0 on success, error otherwise
274  */
275 int blob_dek(const u8 *src, u8 *dst, u8 len);
276 
277 #endif
278 
279 #endif /* __FSL_SEC_H */
280