xref: /optee_os/core/drivers/crypto/caam/caam_ctrl.c (revision 12fc37711783247b0d05fdc271ef007f4930767b)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2017-2021 NXP
4  *
5  * Brief   CAAM Global Controller.
6  */
7 #include <caam_acipher.h>
8 #include <caam_cipher.h>
9 #include <caam_common.h>
10 #include <caam_hal_cfg.h>
11 #include <caam_hal_clk.h>
12 #include <caam_hal_ctrl.h>
13 #include <caam_hash.h>
14 #include <caam_jr.h>
15 #include <caam_blob.h>
16 #include <caam_mp.h>
17 #include <caam_pwr.h>
18 #include <caam_rng.h>
19 #include <drivers/imx_snvs.h>
20 #include <initcall.h>
21 #include <kernel/panic.h>
22 #include <tee_api_types.h>
23 
24 /* Crypto driver initialization */
25 static TEE_Result crypto_driver_init(void)
26 {
27 	TEE_Result retresult = TEE_ERROR_GENERIC;
28 	enum caam_status retstatus = CAAM_FAILURE;
29 	struct caam_jrcfg jrcfg = {};
30 
31 	/* Enable the CAAM Clock */
32 	caam_hal_clk_enable(true);
33 
34 	/* Set OTP as master key if the platform is closed */
35 	if (snvs_is_device_closed()) {
36 		retresult = imx_snvs_set_master_otpmk();
37 		if (retresult && retresult != TEE_ERROR_NOT_IMPLEMENTED)
38 			goto exit_init;
39 	}
40 
41 	retstatus = caam_hal_cfg_get_conf(&jrcfg);
42 	if (retstatus != CAAM_NO_ERROR) {
43 		retresult = TEE_ERROR_NOT_SUPPORTED;
44 		goto exit_init;
45 	}
46 
47 	/* Initialize the CAAM Controller */
48 	caam_hal_ctrl_init(jrcfg.base);
49 
50 	/* Initialize the Job Ring to be used */
51 	retstatus = caam_jr_init(&jrcfg);
52 	if (retstatus != CAAM_NO_ERROR) {
53 		retresult = TEE_ERROR_GENERIC;
54 		goto exit_init;
55 	}
56 
57 	/* Initialize the RNG Module */
58 	retstatus = caam_rng_init(jrcfg.base);
59 	if (retstatus != CAAM_NO_ERROR) {
60 		retresult = TEE_ERROR_GENERIC;
61 		goto exit_init;
62 	}
63 
64 	/* Initialize the Hash Module */
65 	retstatus = caam_hash_init(&jrcfg);
66 	if (retstatus != CAAM_NO_ERROR) {
67 		retresult = TEE_ERROR_GENERIC;
68 		goto exit_init;
69 	}
70 
71 	/* Initialize the MATH Module */
72 	retstatus = caam_math_init(&jrcfg);
73 	if (retstatus != CAAM_NO_ERROR) {
74 		retresult = TEE_ERROR_GENERIC;
75 		goto exit_init;
76 	}
77 
78 	/* Initialize the RSA Module */
79 	retstatus = caam_rsa_init(&jrcfg);
80 	if (retstatus != CAAM_NO_ERROR) {
81 		retresult = TEE_ERROR_GENERIC;
82 		goto exit_init;
83 	}
84 
85 	/* Initialize the Cipher Module */
86 	retstatus = caam_cipher_init(jrcfg.base);
87 	if (retstatus != CAAM_NO_ERROR) {
88 		retresult = TEE_ERROR_GENERIC;
89 		goto exit_init;
90 	}
91 
92 	/* Initialize the HMAC Module */
93 	retstatus = caam_hmac_init(&jrcfg);
94 	if (retstatus != CAAM_NO_ERROR) {
95 		retresult = TEE_ERROR_GENERIC;
96 		goto exit_init;
97 	}
98 
99 	/* Initialize the BLOB Module */
100 	retstatus = caam_blob_mkvb_init(jrcfg.base);
101 	if (retstatus != CAAM_NO_ERROR) {
102 		retresult = TEE_ERROR_GENERIC;
103 		goto exit_init;
104 	}
105 
106 	/* Initialize the CMAC Module */
107 	retstatus = caam_cmac_init(jrcfg.base);
108 	if (retstatus != CAAM_NO_ERROR) {
109 		retresult = TEE_ERROR_GENERIC;
110 		goto exit_init;
111 	}
112 
113 	/* Initialize the ECC Module */
114 	retstatus = caam_ecc_init(&jrcfg);
115 	if (retstatus != CAAM_NO_ERROR) {
116 		retresult = TEE_ERROR_GENERIC;
117 		goto exit_init;
118 	}
119 
120 	/* Initialize the DH Module */
121 	retstatus = caam_dh_init(&jrcfg);
122 	if (retstatus != CAAM_NO_ERROR) {
123 		retresult = TEE_ERROR_GENERIC;
124 		goto exit_init;
125 	}
126 
127 	/* Initialize the DSA Module */
128 	retstatus = caam_dsa_init(&jrcfg);
129 	if (retstatus != CAAM_NO_ERROR) {
130 		retresult = TEE_ERROR_GENERIC;
131 		goto exit_init;
132 	}
133 
134 	/* Initialize the Manufacturing Protection Module */
135 	retstatus = caam_mp_init(jrcfg.base);
136 	if (retstatus != CAAM_NO_ERROR && retstatus != CAAM_NOT_SUPPORTED) {
137 		retresult = TEE_ERROR_GENERIC;
138 		goto exit_init;
139 	}
140 
141 	/* Everything is OK, register the Power Management handler */
142 	caam_pwr_init();
143 
144 	/*
145 	 * Configure Job Rings to NS World
146 	 * If the Driver Crypto is not used CFG_NXP_CAAM_RUNTIME_JR is not
147 	 * enable, hence relax the JR used for the CAAM configuration to
148 	 * the Non-Secure
149 	 */
150 	if (jrcfg.base)
151 		caam_hal_cfg_setup_nsjobring(&jrcfg);
152 
153 	retresult = TEE_SUCCESS;
154 exit_init:
155 	if (retresult != TEE_SUCCESS) {
156 		EMSG("CAAM Driver initialization (0x%" PRIx32 ")", retresult);
157 		panic();
158 	}
159 
160 	return retresult;
161 }
162 
163 early_init(crypto_driver_init);
164 
165 /* Crypto driver late initialization to complete on-going CAAM operations */
166 static TEE_Result init_caam_late(void)
167 {
168 	enum caam_status ret = CAAM_BUSY;
169 
170 	ret = caam_jr_complete();
171 
172 	if (ret != CAAM_NO_ERROR) {
173 		EMSG("CAAM initialization failed");
174 		panic();
175 	}
176 
177 	return TEE_SUCCESS;
178 }
179 
180 early_init_late(init_caam_late);
181