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