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