1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2017-2019, 2021 NXP
4  *
5  * Brief   CAAM Configuration.
6  */
7 #include <caam_common.h>
8 #include <caam_hal_cfg.h>
9 #include <caam_hal_ctrl.h>
10 #include <caam_hal_jr.h>
11 #include <caam_jr.h>
12 #include <config.h>
13 #include <kernel/boot.h>
14 #include <mm/core_memprot.h>
15 #include <registers/jr_regs.h>
16 
caam_hal_cfg_get_conf(struct caam_jrcfg * jrcfg)17 enum caam_status caam_hal_cfg_get_conf(struct caam_jrcfg *jrcfg)
18 {
19 	enum caam_status retstatus = CAAM_FAILURE;
20 	vaddr_t ctrl_base = 0;
21 	void *fdt = NULL;
22 
23 	fdt = get_dt();
24 
25 	/*
26 	 * First get the CAAM Controller base address from the DTB,
27 	 * if DTB present and if the CAAM Controller defined in it.
28 	 */
29 	if (fdt)
30 		caam_hal_cfg_get_ctrl_dt(fdt, &ctrl_base);
31 
32 	if (!ctrl_base) {
33 		ctrl_base = (vaddr_t)core_mmu_add_mapping(MEM_AREA_IO_SEC,
34 							  CAAM_BASE, CAAM_SIZE);
35 		if (!ctrl_base) {
36 			EMSG("Unable to map CAAM Registers");
37 			goto exit_get_conf;
38 		}
39 	}
40 
41 	jrcfg->base = ctrl_base;
42 
43 	/*
44 	 * Next get the Job Ring reserved for the Secure environment
45 	 * into the DTB. If nothing reserved use the default hard coded
46 	 * value.
47 	 */
48 	if (fdt)
49 		caam_hal_cfg_get_jobring_dt(fdt, jrcfg);
50 
51 	if (!jrcfg->offset) {
52 		jrcfg->offset = (CFG_JR_INDEX + 1) * JRX_BLOCK_SIZE;
53 		jrcfg->it_num = CFG_JR_INT;
54 
55 		if (IS_ENABLED(CFG_NXP_CAAM_RUNTIME_JR) &&
56 		    !is_embedded_dt(fdt)) {
57 			if (fdt) {
58 				/* Ensure Secure Job Ring is secure in DTB */
59 				caam_hal_cfg_disable_jobring_dt(fdt, jrcfg);
60 			}
61 		}
62 	}
63 
64 	jrcfg->nb_jobs = NB_JOBS_QUEUE;
65 
66 	retstatus = CAAM_NO_ERROR;
67 
68 exit_get_conf:
69 	HAL_TRACE("HAL CFG Get CAAM config ret (0x%x)\n", retstatus);
70 	return retstatus;
71 }
72 
caam_hal_cfg_setup_nsjobring(struct caam_jrcfg * jrcfg)73 void __weak caam_hal_cfg_setup_nsjobring(struct caam_jrcfg *jrcfg)
74 {
75 	enum caam_status status = CAAM_FAILURE;
76 	paddr_t jr_offset = 0;
77 	uint8_t jrnum = 0;
78 
79 	for (jrnum = caam_hal_ctrl_jrnum(jrcfg->base); jrnum; jrnum--) {
80 		jr_offset = jrnum * JRX_BLOCK_SIZE;
81 
82 #ifdef CFG_NXP_CAAM_RUNTIME_JR
83 		/*
84 		 * When the Cryptographic driver is enabled, keep the
85 		 * Secure Job Ring don't release it.
86 		 * But save the configuration to restore it when
87 		 * device reset after suspend.
88 		 */
89 		if (jr_offset == jrcfg->offset) {
90 			caam_hal_jr_prepare_backup(jrcfg->base, jr_offset);
91 			continue;
92 		}
93 #endif
94 		status = caam_hal_jr_setowner(jrcfg->base, jr_offset,
95 					      JROWN_ARM_NS);
96 		if (status == CAAM_NO_ERROR)
97 			caam_hal_jr_prepare_backup(jrcfg->base, jr_offset);
98 	}
99 }
100