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