xref: /optee_os/core/drivers/imx_caam.c (revision 9f34db38245c9b3a4e6e7e63eb78a75e23ab2da3)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2019 Bryan O'Donoghue
4  * Copyright 2019, 2023 NXP
5  *
6  * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
7  */
8 
9 #include <initcall.h>
10 #include <io.h>
11 #include <mm/core_memprot.h>
12 
13 struct imx_caam_job_ring {
14 	uint32_t			jrmidr_ms;
15 	uint32_t			jrmidr_ls;
16 };
17 
18 #define CAAM_NUM_JOB_RINGS		4
19 
20 /* CAAM ownersip definition bits */
21 #define JROWN_NS			BIT(3)
22 #define JROWN_MID			0x01
23 
24 /* A basic sub-set of the CAAM */
25 struct imx_caam_ctrl {
26 	uint32_t			res0;
27 	uint32_t			mcfgr;
28 	uint32_t			res1;
29 	uint32_t			scfgr;
30 	struct imx_caam_job_ring	jr[CAAM_NUM_JOB_RINGS];
31 };
32 
33 register_phys_mem_pgdir(MEM_AREA_IO_SEC, CAAM_BASE, CORE_MMU_PGDIR_SIZE);
34 
35 static TEE_Result init_caam(void)
36 {
37 	struct imx_caam_ctrl *caam;
38 	uint32_t reg;
39 	int i;
40 
41 	caam = (struct imx_caam_ctrl *)
42 		core_mmu_get_va(CAAM_BASE, MEM_AREA_IO_SEC,
43 				sizeof(struct imx_caam_ctrl));
44 	if (!caam)
45 		return TEE_ERROR_GENERIC;
46 
47 	/*
48 	 * Set job-ring ownership to non-secure by default.
49 	 * A Linux kernel that runs after OP-TEE will run in normal-world
50 	 * so we want to enable that kernel to have total ownership of the
51 	 * CAAM job-rings.
52 	 *
53 	 * It is possible to use CAAM job-rings inside of OP-TEE i.e. in
54 	 * secure world code but, to do that OP-TEE and kernel should agree
55 	 * via a DTB which job-rings are owned by OP-TEE and which are
56 	 * owned by Kernel, something that the OP-TEE CAAM driver should
57 	 * set up.
58 	 *
59 	 * This code below simply sets a default for the case where no
60 	 * runtime OP-TEE CAAM code will be run
61 	 */
62 	for (i = 0; i < CAAM_NUM_JOB_RINGS; i++) {
63 		reg = io_read32((vaddr_t)&caam->jr[i].jrmidr_ms);
64 		reg |= JROWN_NS | JROWN_MID;
65 		io_write32((vaddr_t)&caam->jr[i].jrmidr_ms, reg);
66 	}
67 
68 	return TEE_SUCCESS;
69 }
70 
71 driver_init(init_caam);
72