Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008-2014 Freescale Semiconductor, Inc. |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 4 | * Copyright 2018, 2021 NXP |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 5 | * |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 6 | * Based on CAAM driver in drivers/crypto/caam in Linux |
| 7 | */ |
| 8 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 9 | #include <config.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 13 | #include <malloc.h> |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 14 | #include <power-domain.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 15 | #include "jr.h" |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 16 | #include "jobdesc.h" |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 17 | #include "desc_constr.h" |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 18 | #include <time.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 19 | #include <asm/cache.h> |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 20 | #ifdef CONFIG_FSL_CORENET |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 21 | #include <asm/cache.h> |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 22 | #include <asm/fsl_pamu.h> |
| 23 | #endif |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 24 | #include <dm.h> |
Michael Walle | b258eb2 | 2020-06-27 22:58:53 +0200 | [diff] [blame] | 25 | #include <dm/lists.h> |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 26 | #include <dm/root.h> |
| 27 | #include <dm/device-internal.h> |
Franck LENORMAND | 7181278 | 2021-03-25 17:30:22 +0800 | [diff] [blame] | 28 | #include <linux/delay.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 29 | |
| 30 | #define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1)) |
| 31 | #define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size)) |
| 32 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 33 | uint32_t sec_offset[CONFIG_SYS_FSL_MAX_NUM_OF_SEC] = { |
| 34 | 0, |
York Sun | 4119aee | 2016-11-15 18:44:22 -0800 | [diff] [blame] | 35 | #if defined(CONFIG_ARCH_C29X) |
Tom Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 36 | CFG_SYS_FSL_SEC_IDX_OFFSET, |
| 37 | 2 * CFG_SYS_FSL_SEC_IDX_OFFSET |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 38 | #endif |
| 39 | }; |
| 40 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 41 | #if CONFIG_IS_ENABLED(DM) |
| 42 | struct udevice *caam_dev; |
| 43 | #else |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 44 | #define SEC_ADDR(idx) \ |
Tom Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 45 | (ulong)((CFG_SYS_FSL_SEC_ADDR + sec_offset[idx])) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 46 | |
| 47 | #define SEC_JR0_ADDR(idx) \ |
Aymen Sghaier | 1536fd8 | 2021-03-25 17:30:26 +0800 | [diff] [blame] | 48 | (ulong)(SEC_ADDR(idx) + \ |
Tom Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 49 | (CFG_SYS_FSL_JR0_OFFSET - CFG_SYS_FSL_SEC_OFFSET)) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 50 | struct caam_regs caam_st; |
| 51 | #endif |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 52 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 53 | static inline u32 jr_start_reg(u8 jrid) |
| 54 | { |
| 55 | return (1 << jrid); |
| 56 | } |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 57 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 58 | static inline void start_jr(struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 59 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 60 | ccsr_sec_t *sec = caam->sec; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 61 | u32 ctpr_ms = sec_in32(&sec->ctpr_ms); |
| 62 | u32 scfgr = sec_in32(&sec->scfgr); |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 63 | u32 jrstart = jr_start_reg(caam->jrid); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 64 | |
| 65 | if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) { |
| 66 | /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or |
| 67 | * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1 |
| 68 | */ |
| 69 | if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) || |
xypron.glpk@gmx.de | 3ec0182 | 2017-04-15 16:37:54 +0200 | [diff] [blame] | 70 | (scfgr & SEC_SCFGR_VIRT_EN)) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 71 | sec_out32(&sec->jrstartr, jrstart); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 72 | } else { |
| 73 | /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */ |
| 74 | if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 75 | sec_out32(&sec->jrstartr, jrstart); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 79 | static inline void jr_disable_irq(struct jr_regs *regs) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 80 | { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 81 | uint32_t jrcfg = sec_in32(®s->jrcfg1); |
| 82 | |
| 83 | jrcfg = jrcfg | JR_INTMASK; |
| 84 | |
| 85 | sec_out32(®s->jrcfg1, jrcfg); |
| 86 | } |
| 87 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 88 | static void jr_initregs(uint8_t sec_idx, struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 89 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 90 | struct jr_regs *regs = caam->regs; |
| 91 | struct jobring *jr = &caam->jr[sec_idx]; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 92 | caam_dma_addr_t ip_base = virt_to_phys((void *)jr->input_ring); |
| 93 | caam_dma_addr_t op_base = virt_to_phys((void *)jr->output_ring); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 94 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 95 | #ifdef CONFIG_CAAM_64BIT |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 96 | sec_out32(®s->irba_h, ip_base >> 32); |
| 97 | #else |
| 98 | sec_out32(®s->irba_h, 0x0); |
| 99 | #endif |
| 100 | sec_out32(®s->irba_l, (uint32_t)ip_base); |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 101 | #ifdef CONFIG_CAAM_64BIT |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 102 | sec_out32(®s->orba_h, op_base >> 32); |
| 103 | #else |
| 104 | sec_out32(®s->orba_h, 0x0); |
| 105 | #endif |
| 106 | sec_out32(®s->orba_l, (uint32_t)op_base); |
| 107 | sec_out32(®s->ors, JR_SIZE); |
| 108 | sec_out32(®s->irs, JR_SIZE); |
| 109 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 110 | if (!jr->irq) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 111 | jr_disable_irq(regs); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 112 | } |
| 113 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 114 | static int jr_init(uint8_t sec_idx, struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 115 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 116 | struct jobring *jr = &caam->jr[sec_idx]; |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 117 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 118 | ofnode scu_node = ofnode_by_compatible(ofnode_null(), "fsl,imx8-mu"); |
| 119 | #endif |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 120 | memset(jr, 0, sizeof(struct jobring)); |
| 121 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 122 | jr->jq_id = caam->jrid; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 123 | jr->irq = DEFAULT_IRQ; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 124 | |
| 125 | #ifdef CONFIG_FSL_CORENET |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 126 | jr->liodn = DEFAULT_JR_LIODN; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 127 | #endif |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 128 | jr->size = JR_SIZE; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 129 | jr->input_ring = (caam_dma_addr_t *)memalign(ARCH_DMA_MINALIGN, |
| 130 | JR_SIZE * sizeof(caam_dma_addr_t)); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 131 | if (!jr->input_ring) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 132 | return -1; |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 133 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 134 | jr->op_size = roundup(JR_SIZE * sizeof(struct op_ring), |
| 135 | ARCH_DMA_MINALIGN); |
| 136 | jr->output_ring = |
| 137 | (struct op_ring *)memalign(ARCH_DMA_MINALIGN, jr->op_size); |
| 138 | if (!jr->output_ring) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 139 | return -1; |
| 140 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 141 | memset(jr->input_ring, 0, JR_SIZE * sizeof(caam_dma_addr_t)); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 142 | memset(jr->output_ring, 0, jr->op_size); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 143 | |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 144 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 145 | if (!ofnode_valid(scu_node)) |
| 146 | #endif |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 147 | start_jr(caam); |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 148 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 149 | jr_initregs(sec_idx, caam); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* -1 --- error, can't enqueue -- no space available */ |
| 155 | static int jr_enqueue(uint32_t *desc_addr, |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 156 | void (*callback)(uint32_t status, void *arg), |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 157 | void *arg, uint8_t sec_idx, struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 158 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 159 | struct jr_regs *regs = caam->regs; |
| 160 | struct jobring *jr = &caam->jr[sec_idx]; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 161 | int head = jr->head; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 162 | uint32_t desc_word; |
| 163 | int length = desc_len(desc_addr); |
| 164 | int i; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 165 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 166 | uint32_t *addr_hi, *addr_lo; |
| 167 | #endif |
| 168 | |
| 169 | /* The descriptor must be submitted to SEC block as per endianness |
| 170 | * of the SEC Block. |
| 171 | * So, if the endianness of Core and SEC block is different, each word |
| 172 | * of the descriptor will be byte-swapped. |
| 173 | */ |
| 174 | for (i = 0; i < length; i++) { |
| 175 | desc_word = desc_addr[i]; |
| 176 | sec_out32((uint32_t *)&desc_addr[i], desc_word); |
| 177 | } |
| 178 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 179 | caam_dma_addr_t desc_phys_addr = virt_to_phys(desc_addr); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 180 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 181 | jr->info[head].desc_phys_addr = desc_phys_addr; |
| 182 | jr->info[head].callback = (void *)callback; |
| 183 | jr->info[head].arg = arg; |
| 184 | jr->info[head].op_done = 0; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 185 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 186 | unsigned long start = (unsigned long)&jr->info[head] & |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 187 | ~(ARCH_DMA_MINALIGN - 1); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 188 | unsigned long end = ALIGN((unsigned long)&jr->info[head] + |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 189 | sizeof(struct jr_info), ARCH_DMA_MINALIGN); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 190 | flush_dcache_range(start, end); |
| 191 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 192 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 193 | /* Write the 64 bit Descriptor address on Input Ring. |
| 194 | * The 32 bit hign and low part of the address will |
| 195 | * depend on endianness of SEC block. |
| 196 | */ |
| 197 | #ifdef CONFIG_SYS_FSL_SEC_LE |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 198 | addr_lo = (uint32_t *)(&jr->input_ring[head]); |
| 199 | addr_hi = (uint32_t *)(&jr->input_ring[head]) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 200 | #elif defined(CONFIG_SYS_FSL_SEC_BE) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 201 | addr_hi = (uint32_t *)(&jr->input_ring[head]); |
| 202 | addr_lo = (uint32_t *)(&jr->input_ring[head]) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 203 | #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */ |
| 204 | |
| 205 | sec_out32(addr_hi, (uint32_t)(desc_phys_addr >> 32)); |
| 206 | sec_out32(addr_lo, (uint32_t)(desc_phys_addr)); |
| 207 | |
| 208 | #else |
| 209 | /* Write the 32 bit Descriptor address on Input Ring. */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 210 | sec_out32(&jr->input_ring[head], desc_phys_addr); |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 211 | #endif /* ifdef CONFIG_CAAM_64BIT */ |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 212 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 213 | start = (unsigned long)&jr->input_ring[head] & ~(ARCH_DMA_MINALIGN - 1); |
| 214 | end = ALIGN((unsigned long)&jr->input_ring[head] + |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 215 | sizeof(caam_dma_addr_t), ARCH_DMA_MINALIGN); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 216 | flush_dcache_range(start, end); |
| 217 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 218 | jr->head = (head + 1) & (jr->size - 1); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 219 | |
| 220 | sec_out32(®s->irja, 1); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 225 | static int jr_dequeue(int sec_idx, struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 226 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 227 | struct jr_regs *regs = caam->regs; |
| 228 | struct jobring *jr = &caam->jr[sec_idx]; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 229 | int head = jr->head; |
| 230 | int tail = jr->tail; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 231 | int idx, i, found; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 232 | void (*callback)(uint32_t status, void *arg); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 233 | void *arg = NULL; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 234 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 235 | uint32_t *addr_hi, *addr_lo; |
| 236 | #else |
| 237 | uint32_t *addr; |
| 238 | #endif |
Olaf Baehring | 6092e8b | 2025-05-21 08:03:40 -0300 | [diff] [blame^] | 239 | unsigned long start, end; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 240 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 241 | while (sec_in32(®s->orsf) && CIRC_CNT(jr->head, jr->tail, |
| 242 | jr->size)) { |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 243 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 244 | found = 0; |
| 245 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 246 | caam_dma_addr_t op_desc; |
Olaf Baehring | 6092e8b | 2025-05-21 08:03:40 -0300 | [diff] [blame^] | 247 | |
| 248 | /* Invalidate output ring */ |
| 249 | start = (unsigned long)jr->output_ring & ~(ARCH_DMA_MINALIGN - 1); |
| 250 | end = ALIGN((unsigned long)jr->output_ring + jr->op_size, ARCH_DMA_MINALIGN); |
| 251 | invalidate_dcache_range(start, end); |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 252 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 253 | /* Read the 64 bit Descriptor address from Output Ring. |
| 254 | * The 32 bit hign and low part of the address will |
| 255 | * depend on endianness of SEC block. |
| 256 | */ |
| 257 | #ifdef CONFIG_SYS_FSL_SEC_LE |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 258 | addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc); |
| 259 | addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 260 | #elif defined(CONFIG_SYS_FSL_SEC_BE) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 261 | addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc); |
| 262 | addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 263 | #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */ |
| 264 | |
| 265 | op_desc = ((u64)sec_in32(addr_hi) << 32) | |
| 266 | ((u64)sec_in32(addr_lo)); |
| 267 | |
| 268 | #else |
| 269 | /* Read the 32 bit Descriptor address from Output Ring. */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 270 | addr = (uint32_t *)&jr->output_ring[jr->tail].desc; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 271 | op_desc = sec_in32(addr); |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 272 | #endif /* ifdef CONFIG_CAAM_64BIT */ |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 273 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 274 | uint32_t status = sec_in32(&jr->output_ring[jr->tail].status); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 275 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 276 | for (i = 0; CIRC_CNT(head, tail + i, jr->size) >= 1; i++) { |
| 277 | idx = (tail + i) & (jr->size - 1); |
| 278 | if (op_desc == jr->info[idx].desc_phys_addr) { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 279 | found = 1; |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* Error condition if match not found */ |
Olaf Baehring | 6092e8b | 2025-05-21 08:03:40 -0300 | [diff] [blame^] | 285 | if (!found) { |
| 286 | int slots_full = sec_in32(®s->orsf); |
| 287 | |
| 288 | jr->tail = (jr->tail + slots_full) & (jr->size - 1); |
| 289 | sec_out32(®s->orjr, slots_full); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 290 | return -1; |
Olaf Baehring | 6092e8b | 2025-05-21 08:03:40 -0300 | [diff] [blame^] | 291 | } |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 292 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 293 | jr->info[idx].op_done = 1; |
| 294 | callback = (void *)jr->info[idx].callback; |
| 295 | arg = jr->info[idx].arg; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 296 | |
| 297 | /* When the job on tail idx gets done, increment |
| 298 | * tail till the point where job completed out of oredr has |
| 299 | * been taken into account |
| 300 | */ |
| 301 | if (idx == tail) |
| 302 | do { |
Olaf Baehring | 6092e8b | 2025-05-21 08:03:40 -0300 | [diff] [blame^] | 303 | jr->info[tail].op_done = 0; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 304 | tail = (tail + 1) & (jr->size - 1); |
| 305 | } while (jr->info[tail].op_done); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 306 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 307 | jr->tail = tail; |
Olaf Baehring | 6092e8b | 2025-05-21 08:03:40 -0300 | [diff] [blame^] | 308 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 309 | |
| 310 | sec_out32(®s->orjr, 1); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 311 | |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 312 | callback(status, arg); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 318 | static void desc_done(uint32_t status, void *arg) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 319 | { |
| 320 | struct result *x = arg; |
| 321 | x->status = status; |
| 322 | caam_jr_strstatus(status); |
| 323 | x->done = 1; |
| 324 | } |
| 325 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 326 | static inline int run_descriptor_jr_idx(uint32_t *desc, uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 327 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 328 | struct caam_regs *caam; |
| 329 | #if CONFIG_IS_ENABLED(DM) |
| 330 | caam = dev_get_priv(caam_dev); |
| 331 | #else |
| 332 | caam = &caam_st; |
| 333 | #endif |
Franck LENORMAND | 7181278 | 2021-03-25 17:30:22 +0800 | [diff] [blame] | 334 | unsigned long long timeval = 0; |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 335 | unsigned long long timeout = CFG_USEC_DEQ_TIMEOUT; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 336 | struct result op; |
| 337 | int ret = 0; |
| 338 | |
gaurav rana | 0762150 | 2014-12-04 13:00:41 +0530 | [diff] [blame] | 339 | memset(&op, 0, sizeof(op)); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 340 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 341 | ret = jr_enqueue(desc, desc_done, &op, sec_idx, caam); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 342 | if (ret) { |
| 343 | debug("Error in SEC enq\n"); |
| 344 | ret = JQ_ENQ_ERR; |
| 345 | goto out; |
| 346 | } |
| 347 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 348 | while (op.done != 1) { |
Franck LENORMAND | 7181278 | 2021-03-25 17:30:22 +0800 | [diff] [blame] | 349 | udelay(1); |
| 350 | timeval += 1; |
| 351 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 352 | ret = jr_dequeue(sec_idx, caam); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 353 | if (ret) { |
| 354 | debug("Error in SEC deq\n"); |
| 355 | ret = JQ_DEQ_ERR; |
| 356 | goto out; |
| 357 | } |
| 358 | |
Franck LENORMAND | 7181278 | 2021-03-25 17:30:22 +0800 | [diff] [blame] | 359 | if (timeval > timeout) { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 360 | debug("SEC Dequeue timed out\n"); |
| 361 | ret = JQ_DEQ_TO_ERR; |
| 362 | goto out; |
| 363 | } |
| 364 | } |
| 365 | |
Aneesh Bansal | 3ab29d7 | 2016-02-11 14:36:51 +0530 | [diff] [blame] | 366 | if (op.status) { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 367 | debug("Error %x\n", op.status); |
| 368 | ret = op.status; |
| 369 | } |
| 370 | out: |
| 371 | return ret; |
| 372 | } |
| 373 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 374 | int run_descriptor_jr(uint32_t *desc) |
| 375 | { |
| 376 | return run_descriptor_jr_idx(desc, 0); |
| 377 | } |
| 378 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 379 | static int jr_sw_cleanup(uint8_t sec_idx, struct caam_regs *caam) |
| 380 | { |
| 381 | struct jobring *jr = &caam->jr[sec_idx]; |
| 382 | |
| 383 | jr->head = 0; |
| 384 | jr->tail = 0; |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 385 | jr->write_idx = 0; |
| 386 | memset(jr->info, 0, sizeof(jr->info)); |
| 387 | memset(jr->input_ring, 0, jr->size * sizeof(caam_dma_addr_t)); |
| 388 | memset(jr->output_ring, 0, jr->size * sizeof(struct op_ring)); |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int jr_hw_reset(struct jr_regs *regs) |
| 394 | { |
| 395 | uint32_t timeout = 100000; |
| 396 | uint32_t jrint, jrcr; |
| 397 | |
| 398 | sec_out32(®s->jrcr, JRCR_RESET); |
| 399 | do { |
| 400 | jrint = sec_in32(®s->jrint); |
| 401 | } while (((jrint & JRINT_ERR_HALT_MASK) == |
| 402 | JRINT_ERR_HALT_INPROGRESS) && --timeout); |
| 403 | |
| 404 | jrint = sec_in32(®s->jrint); |
| 405 | if (((jrint & JRINT_ERR_HALT_MASK) != |
| 406 | JRINT_ERR_HALT_INPROGRESS) && timeout == 0) |
| 407 | return -1; |
| 408 | |
| 409 | timeout = 100000; |
| 410 | sec_out32(®s->jrcr, JRCR_RESET); |
| 411 | do { |
| 412 | jrcr = sec_in32(®s->jrcr); |
| 413 | } while ((jrcr & JRCR_RESET) && --timeout); |
| 414 | |
| 415 | if (timeout == 0) |
| 416 | return -1; |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 421 | static inline int jr_reset_sec(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 422 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 423 | struct caam_regs *caam; |
| 424 | #if CONFIG_IS_ENABLED(DM) |
| 425 | caam = dev_get_priv(caam_dev); |
| 426 | #else |
| 427 | caam = &caam_st; |
| 428 | #endif |
| 429 | if (jr_hw_reset(caam->regs) < 0) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 430 | return -1; |
| 431 | |
| 432 | /* Clean up the jobring structure maintained by software */ |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 433 | jr_sw_cleanup(sec_idx, caam); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 438 | int jr_reset(void) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 439 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 440 | return jr_reset_sec(0); |
| 441 | } |
| 442 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 443 | int sec_reset(void) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 444 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 445 | struct caam_regs *caam; |
| 446 | #if CONFIG_IS_ENABLED(DM) |
| 447 | caam = dev_get_priv(caam_dev); |
| 448 | #else |
| 449 | caam = &caam_st; |
| 450 | #endif |
| 451 | ccsr_sec_t *sec = caam->sec; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 452 | uint32_t mcfgr = sec_in32(&sec->mcfgr); |
| 453 | uint32_t timeout = 100000; |
| 454 | |
| 455 | mcfgr |= MCFGR_SWRST; |
| 456 | sec_out32(&sec->mcfgr, mcfgr); |
| 457 | |
| 458 | mcfgr |= MCFGR_DMA_RST; |
| 459 | sec_out32(&sec->mcfgr, mcfgr); |
| 460 | do { |
| 461 | mcfgr = sec_in32(&sec->mcfgr); |
| 462 | } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout); |
| 463 | |
| 464 | if (timeout == 0) |
| 465 | return -1; |
| 466 | |
| 467 | timeout = 100000; |
| 468 | do { |
| 469 | mcfgr = sec_in32(&sec->mcfgr); |
| 470 | } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout); |
| 471 | |
| 472 | if (timeout == 0) |
| 473 | return -1; |
| 474 | |
| 475 | return 0; |
| 476 | } |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 477 | |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 478 | static int deinstantiate_rng(u8 sec_idx, int state_handle_mask) |
| 479 | { |
| 480 | u32 *desc; |
| 481 | int sh_idx, ret = 0; |
| 482 | int desc_size = ALIGN(sizeof(u32) * 2, ARCH_DMA_MINALIGN); |
| 483 | |
| 484 | desc = memalign(ARCH_DMA_MINALIGN, desc_size); |
| 485 | if (!desc) { |
| 486 | debug("cannot allocate RNG init descriptor memory\n"); |
| 487 | return -ENOMEM; |
| 488 | } |
| 489 | |
| 490 | for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { |
| 491 | /* |
| 492 | * If the corresponding bit is set, then it means the state |
| 493 | * handle was initialized by us, and thus it needs to be |
| 494 | * deinitialized as well |
| 495 | */ |
| 496 | |
| 497 | if (state_handle_mask & RDSTA_IF(sh_idx)) { |
| 498 | /* |
| 499 | * Create the descriptor for deinstantating this state |
| 500 | * handle. |
| 501 | */ |
| 502 | inline_cnstr_jobdesc_rng_deinstantiation(desc, sh_idx); |
| 503 | flush_dcache_range((unsigned long)desc, |
| 504 | (unsigned long)desc + desc_size); |
| 505 | |
| 506 | ret = run_descriptor_jr_idx(desc, sec_idx); |
| 507 | if (ret) { |
| 508 | printf("SEC%u: RNG4 SH%d deinstantiation failed with error 0x%x\n", |
| 509 | sec_idx, sh_idx, ret); |
| 510 | ret = -EIO; |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | printf("SEC%u: Deinstantiated RNG4 SH%d\n", |
| 515 | sec_idx, sh_idx); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | free(desc); |
| 520 | return ret; |
| 521 | } |
| 522 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 523 | static int instantiate_rng(uint8_t sec_idx, ccsr_sec_t *sec, int gen_sk) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 524 | { |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 525 | u32 *desc; |
| 526 | u32 rdsta_val; |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 527 | int ret = 0, sh_idx, size; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 528 | struct rng4tst __iomem *rng = |
| 529 | (struct rng4tst __iomem *)&sec->rng; |
| 530 | |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 531 | desc = memalign(ARCH_DMA_MINALIGN, sizeof(uint32_t) * 6); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 532 | if (!desc) { |
| 533 | printf("cannot allocate RNG init descriptor memory\n"); |
| 534 | return -1; |
| 535 | } |
| 536 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 537 | for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { |
| 538 | /* |
| 539 | * If the corresponding bit is set, this state handle |
| 540 | * was initialized by somebody else, so it's left alone. |
| 541 | */ |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 542 | rdsta_val = sec_in32(&rng->rdsta); |
| 543 | if (rdsta_val & (RDSTA_IF(sh_idx))) { |
| 544 | if (rdsta_val & RDSTA_PR(sh_idx)) |
| 545 | continue; |
| 546 | |
| 547 | printf("SEC%u: RNG4 SH%d was instantiated w/o prediction resistance. Tearing it down\n", |
| 548 | sec_idx, sh_idx); |
| 549 | |
| 550 | ret = deinstantiate_rng(sec_idx, RDSTA_IF(sh_idx)); |
| 551 | if (ret) |
| 552 | break; |
| 553 | } |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 554 | |
Michael Walle | 602cc8d | 2020-06-27 22:58:51 +0200 | [diff] [blame] | 555 | inline_cnstr_jobdesc_rng_instantiation(desc, sh_idx, gen_sk); |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 556 | size = roundup(sizeof(uint32_t) * 6, ARCH_DMA_MINALIGN); |
| 557 | flush_dcache_range((unsigned long)desc, |
| 558 | (unsigned long)desc + size); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 559 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 560 | ret = run_descriptor_jr_idx(desc, sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 561 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 562 | if (ret) |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 563 | printf("SEC%u: RNG4 SH%d instantiation failed with error 0x%x\n", |
| 564 | sec_idx, sh_idx, ret); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 565 | |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 566 | rdsta_val = sec_in32(&rng->rdsta); |
| 567 | if (!(rdsta_val & RDSTA_IF(sh_idx))) { |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 568 | free(desc); |
| 569 | return -1; |
| 570 | } |
| 571 | |
| 572 | memset(desc, 0, sizeof(uint32_t) * 6); |
| 573 | } |
| 574 | |
| 575 | free(desc); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 580 | static u8 get_rng_vid(ccsr_sec_t *sec) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 581 | { |
Michael Walle | a83fa18 | 2020-06-27 22:58:50 +0200 | [diff] [blame] | 582 | u8 vid; |
| 583 | |
| 584 | if (caam_get_era() < 10) { |
| 585 | vid = (sec_in32(&sec->chavid_ls) & SEC_CHAVID_RNG_LS_MASK) |
| 586 | >> SEC_CHAVID_LS_RNG_SHIFT; |
| 587 | } else { |
| 588 | vid = (sec_in32(&sec->vreg.rng) & CHA_VER_VID_MASK) |
| 589 | >> CHA_VER_VID_SHIFT; |
| 590 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 591 | |
Michael Walle | a83fa18 | 2020-06-27 22:58:50 +0200 | [diff] [blame] | 592 | return vid; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* |
| 596 | * By default, the TRNG runs for 200 clocks per sample; |
| 597 | * 1200 clocks per sample generates better entropy. |
| 598 | */ |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 599 | static void kick_trng(int ent_delay, ccsr_sec_t *sec) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 600 | { |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 601 | struct rng4tst __iomem *rng = |
| 602 | (struct rng4tst __iomem *)&sec->rng; |
| 603 | u32 val; |
| 604 | |
| 605 | /* put RNG4 into program mode */ |
| 606 | sec_setbits32(&rng->rtmctl, RTMCTL_PRGM); |
| 607 | /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the |
| 608 | * length (in system clocks) of each Entropy sample taken |
| 609 | * */ |
| 610 | val = sec_in32(&rng->rtsdctl); |
| 611 | val = (val & ~RTSDCTL_ENT_DLY_MASK) | |
| 612 | (ent_delay << RTSDCTL_ENT_DLY_SHIFT); |
| 613 | sec_out32(&rng->rtsdctl, val); |
| 614 | /* min. freq. count, equal to 1/4 of the entropy sample length */ |
| 615 | sec_out32(&rng->rtfreqmin, ent_delay >> 2); |
Alex Porosanu | f8d6a7f | 2015-05-05 16:48:33 +0300 | [diff] [blame] | 616 | /* disable maximum frequency count */ |
| 617 | sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE); |
Alex Porosanu | befb5cb | 2015-05-05 16:48:35 +0300 | [diff] [blame] | 618 | /* |
| 619 | * select raw sampling in both entropy shifter |
| 620 | * and statistical checker |
| 621 | */ |
Aneesh Bansal | 1fa9c90 | 2015-12-08 13:54:30 +0530 | [diff] [blame] | 622 | sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 623 | /* put RNG4 into run mode */ |
Aneesh Bansal | 1fa9c90 | 2015-12-08 13:54:30 +0530 | [diff] [blame] | 624 | sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 625 | } |
| 626 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 627 | static int rng_init(uint8_t sec_idx, ccsr_sec_t *sec) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 628 | { |
Gaurav Jain | b8192ad | 2022-04-15 16:40:49 +0530 | [diff] [blame] | 629 | int ret, gen_sk, ent_delay = RTSDCTL_ENT_DLY; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 630 | struct rng4tst __iomem *rng = |
| 631 | (struct rng4tst __iomem *)&sec->rng; |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 632 | u32 inst_handles; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 633 | |
Michael Walle | 602cc8d | 2020-06-27 22:58:51 +0200 | [diff] [blame] | 634 | gen_sk = !(sec_in32(&rng->rdsta) & RDSTA_SKVN); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 635 | do { |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 636 | inst_handles = sec_in32(&rng->rdsta) & RDSTA_MASK; |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 637 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 638 | /* |
| 639 | * If either of the SH's were instantiated by somebody else |
| 640 | * then it is assumed that the entropy |
| 641 | * parameters are properly set and thus the function |
| 642 | * setting these (kick_trng(...)) is skipped. |
| 643 | * Also, if a handle was instantiated, do not change |
| 644 | * the TRNG parameters. |
| 645 | */ |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 646 | if (!inst_handles) { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 647 | kick_trng(ent_delay, sec); |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 648 | ent_delay += 400; |
| 649 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 650 | /* |
| 651 | * if instantiate_rng(...) fails, the loop will rerun |
| 652 | * and the kick_trng(...) function will modfiy the |
| 653 | * upper and lower limits of the entropy sampling |
| 654 | * interval, leading to a sucessful initialization of |
| 655 | * the RNG. |
| 656 | */ |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 657 | ret = instantiate_rng(sec_idx, sec, gen_sk); |
Gaurav Jain | b8192ad | 2022-04-15 16:40:49 +0530 | [diff] [blame] | 658 | /* |
| 659 | * entropy delay is calculated via self-test method. |
Heinrich Schuchardt | b72dfb2 | 2024-12-11 17:31:54 +0100 | [diff] [blame] | 660 | * self-test are run across different voltage, temp. |
Gaurav Jain | b8192ad | 2022-04-15 16:40:49 +0530 | [diff] [blame] | 661 | * if worst case value for ent_dly is identified, |
| 662 | * loop can be skipped for that platform. |
| 663 | */ |
| 664 | if (IS_ENABLED(CONFIG_MX6SX)) |
| 665 | break; |
| 666 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 667 | } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX)); |
| 668 | if (ret) { |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 669 | printf("SEC%u: Failed to instantiate RNG\n", sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | /* Enable RDB bit so that RNG works faster */ |
| 674 | sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE); |
| 675 | |
| 676 | return ret; |
| 677 | } |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 678 | |
Emanuele Ghidoli | 04a0402 | 2024-03-28 11:30:12 +0100 | [diff] [blame] | 679 | #if CONFIG_IS_ENABLED(FSL_CAAM_JR_NTZ_ACCESS) |
| 680 | static void jr_setown_non_trusted(ccsr_sec_t *sec) |
| 681 | { |
| 682 | u32 jrown_ns; |
| 683 | int i; |
| 684 | |
| 685 | /* Set ownership of job rings to non-TrustZone mode */ |
| 686 | for (i = 0; i < ARRAY_SIZE(sec->jrliodnr); i++) { |
| 687 | jrown_ns = sec_in32(&sec->jrliodnr[i].ms); |
| 688 | jrown_ns |= JROWN_NS | JRMID_NS; |
| 689 | sec_out32(&sec->jrliodnr[i].ms, jrown_ns); |
| 690 | } |
| 691 | } |
| 692 | #endif |
| 693 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 694 | int sec_init_idx(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 695 | { |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 696 | int ret = 0; |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 697 | struct caam_regs *caam; |
| 698 | #if CONFIG_IS_ENABLED(DM) |
| 699 | if (!caam_dev) { |
| 700 | printf("caam_jr: caam not found\n"); |
| 701 | return -1; |
| 702 | } |
| 703 | caam = dev_get_priv(caam_dev); |
| 704 | #else |
| 705 | caam_st.sec = (void *)SEC_ADDR(sec_idx); |
| 706 | caam_st.regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx); |
| 707 | caam_st.jrid = 0; |
| 708 | caam = &caam_st; |
| 709 | #endif |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 710 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 711 | ofnode scu_node = ofnode_by_compatible(ofnode_null(), "fsl,imx8-mu"); |
| 712 | |
| 713 | if (ofnode_valid(scu_node)) |
| 714 | goto init; |
| 715 | #endif |
| 716 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 717 | ccsr_sec_t *sec = caam->sec; |
| 718 | uint32_t mcr = sec_in32(&sec->mcfgr); |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 719 | #if defined(CONFIG_XPL_BUILD) && defined(CONFIG_IMX8M) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 720 | uint32_t jrdid_ms = 0; |
| 721 | #endif |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 722 | #ifdef CONFIG_FSL_CORENET |
| 723 | uint32_t liodnr; |
| 724 | uint32_t liodn_ns; |
| 725 | uint32_t liodn_s; |
| 726 | #endif |
| 727 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 728 | if (!(sec_idx < CONFIG_SYS_FSL_MAX_NUM_OF_SEC)) { |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 729 | printf("SEC%u: initialization failed\n", sec_idx); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 730 | return -1; |
| 731 | } |
| 732 | |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 733 | /* |
| 734 | * Modifying CAAM Read/Write Attributes |
York Sun | cbe8e1c | 2016-04-04 11:41:26 -0700 | [diff] [blame] | 735 | * For LS2080A |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 736 | * For AXI Write - Cacheable, Write Back, Write allocate |
| 737 | * For AXI Read - Cacheable, Read allocate |
York Sun | cbe8e1c | 2016-04-04 11:41:26 -0700 | [diff] [blame] | 738 | * Only For LS2080a, to solve CAAM coherency issues |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 739 | */ |
York Sun | 4ce6fbf | 2017-03-27 11:41:01 -0700 | [diff] [blame] | 740 | #ifdef CONFIG_ARCH_LS2080A |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 741 | mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0xb << MCFGR_AWCACHE_SHIFT); |
| 742 | mcr = (mcr & ~MCFGR_ARCACHE_MASK) | (0x6 << MCFGR_ARCACHE_SHIFT); |
| 743 | #else |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 744 | mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0x2 << MCFGR_AWCACHE_SHIFT); |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 745 | #endif |
| 746 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 747 | #ifdef CONFIG_CAAM_64BIT |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 748 | mcr |= (1 << MCFGR_PS_SHIFT); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 749 | #endif |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 750 | sec_out32(&sec->mcfgr, mcr); |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 751 | #if defined(CONFIG_XPL_BUILD) && defined(CONFIG_IMX8M) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 752 | jrdid_ms = JRDID_MS_TZ_OWN | JRDID_MS_PRIM_TZ | JRDID_MS_PRIM_DID; |
| 753 | sec_out32(&sec->jrliodnr[caam->jrid].ms, jrdid_ms); |
| 754 | #endif |
| 755 | jr_reset(); |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 756 | |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 757 | #ifdef CONFIG_FSL_CORENET |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 758 | #ifdef CONFIG_XPL_BUILD |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 759 | /* |
| 760 | * For SPL Build, Set the Liodns in SEC JR0 for |
| 761 | * creating PAMU entries corresponding to these. |
| 762 | * For normal build, these are set in set_liodns(). |
| 763 | */ |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 764 | liodn_ns = CFG_SPL_JR0_LIODN_NS & JRNSLIODN_MASK; |
| 765 | liodn_s = CFG_SPL_JR0_LIODN_S & JRSLIODN_MASK; |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 766 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 767 | liodnr = sec_in32(&sec->jrliodnr[caam->jrid].ls) & |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 768 | ~(JRNSLIODN_MASK | JRSLIODN_MASK); |
| 769 | liodnr = liodnr | |
| 770 | (liodn_ns << JRNSLIODN_SHIFT) | |
| 771 | (liodn_s << JRSLIODN_SHIFT); |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 772 | sec_out32(&sec->jrliodnr[caam->jrid].ls, liodnr); |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 773 | #else |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 774 | liodnr = sec_in32(&sec->jrliodnr[caam->jrid].ls); |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 775 | liodn_ns = (liodnr & JRNSLIODN_MASK) >> JRNSLIODN_SHIFT; |
| 776 | liodn_s = (liodnr & JRSLIODN_MASK) >> JRSLIODN_SHIFT; |
| 777 | #endif |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 778 | #endif |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 779 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 780 | init: |
| 781 | #endif |
Emanuele Ghidoli | 04a0402 | 2024-03-28 11:30:12 +0100 | [diff] [blame] | 782 | #if CONFIG_IS_ENABLED(FSL_CAAM_JR_NTZ_ACCESS) |
| 783 | jr_setown_non_trusted(sec); |
| 784 | #endif |
| 785 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 786 | ret = jr_init(sec_idx, caam); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 787 | if (ret < 0) { |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 788 | printf("SEC%u: initialization failed\n", sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 789 | return -1; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 790 | } |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 791 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Gaurav Jain | 332d4f9 | 2022-04-22 16:38:34 +0530 | [diff] [blame] | 792 | if (ofnode_valid(scu_node)) { |
Marek Vasut | 0d871e7 | 2024-04-26 01:02:07 +0200 | [diff] [blame] | 793 | if (CONFIG_IS_ENABLED(DM_RNG)) { |
Gaurav Jain | 332d4f9 | 2022-04-22 16:38:34 +0530 | [diff] [blame] | 794 | ret = device_bind_driver(NULL, "caam-rng", "caam-rng", NULL); |
| 795 | if (ret) |
| 796 | printf("Couldn't bind rng driver (%d)\n", ret); |
| 797 | } |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 798 | return ret; |
Gaurav Jain | 332d4f9 | 2022-04-22 16:38:34 +0530 | [diff] [blame] | 799 | } |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 800 | #endif |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 801 | |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 802 | #ifdef CONFIG_FSL_CORENET |
| 803 | ret = sec_config_pamu_table(liodn_ns, liodn_s); |
| 804 | if (ret < 0) |
| 805 | return -1; |
| 806 | |
| 807 | pamu_enable(); |
| 808 | #endif |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 809 | |
| 810 | if (get_rng_vid(caam->sec) >= 4) { |
| 811 | if (rng_init(sec_idx, caam->sec) < 0) { |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 812 | printf("SEC%u: RNG instantiation failed\n", sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 813 | return -1; |
| 814 | } |
Michael Walle | b258eb2 | 2020-06-27 22:58:53 +0200 | [diff] [blame] | 815 | |
Marek Vasut | 0d871e7 | 2024-04-26 01:02:07 +0200 | [diff] [blame] | 816 | if (CONFIG_IS_ENABLED(DM_RNG)) { |
Michael Walle | b258eb2 | 2020-06-27 22:58:53 +0200 | [diff] [blame] | 817 | ret = device_bind_driver(NULL, "caam-rng", "caam-rng", |
| 818 | NULL); |
| 819 | if (ret) |
| 820 | printf("Couldn't bind rng driver (%d)\n", ret); |
| 821 | } |
| 822 | |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 823 | printf("SEC%u: RNG instantiated\n", sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 824 | } |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 825 | return ret; |
| 826 | } |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 827 | |
| 828 | int sec_init(void) |
| 829 | { |
| 830 | return sec_init_idx(0); |
| 831 | } |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 832 | |
| 833 | #if CONFIG_IS_ENABLED(DM) |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 834 | static int jr_power_on(ofnode node) |
| 835 | { |
| 836 | #if CONFIG_IS_ENABLED(POWER_DOMAIN) |
| 837 | struct udevice __maybe_unused jr_dev; |
| 838 | struct power_domain pd; |
| 839 | |
| 840 | dev_set_ofnode(&jr_dev, node); |
| 841 | |
| 842 | /* Power on Job Ring before access it */ |
| 843 | if (!power_domain_get(&jr_dev, &pd)) { |
| 844 | if (power_domain_on(&pd)) |
| 845 | return -EINVAL; |
| 846 | } |
| 847 | #endif |
| 848 | return 0; |
| 849 | } |
| 850 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 851 | static int caam_jr_ioctl(struct udevice *dev, unsigned long request, void *buf) |
| 852 | { |
| 853 | if (request != CAAM_JR_RUN_DESC) |
| 854 | return -ENOSYS; |
| 855 | |
| 856 | return run_descriptor_jr(buf); |
| 857 | } |
| 858 | |
| 859 | static int caam_jr_probe(struct udevice *dev) |
| 860 | { |
| 861 | struct caam_regs *caam = dev_get_priv(dev); |
| 862 | fdt_addr_t addr; |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 863 | ofnode node, scu_node; |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 864 | unsigned int jr_node = 0; |
| 865 | |
| 866 | caam_dev = dev; |
| 867 | |
| 868 | addr = dev_read_addr(dev); |
| 869 | if (addr == FDT_ADDR_T_NONE) { |
| 870 | printf("caam_jr: crypto not found\n"); |
| 871 | return -EINVAL; |
| 872 | } |
| 873 | caam->sec = (ccsr_sec_t *)(uintptr_t)addr; |
| 874 | caam->regs = (struct jr_regs *)caam->sec; |
| 875 | |
| 876 | /* Check for enabled job ring node */ |
| 877 | ofnode_for_each_subnode(node, dev_ofnode(dev)) { |
Simon Glass | 2e4938b | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 878 | if (!ofnode_is_enabled(node)) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 879 | continue; |
| 880 | |
| 881 | jr_node = ofnode_read_u32_default(node, "reg", -1); |
| 882 | if (jr_node > 0) { |
| 883 | caam->regs = (struct jr_regs *)((ulong)caam->sec + jr_node); |
| 884 | while (!(jr_node & 0x0F)) |
| 885 | jr_node = jr_node >> 4; |
| 886 | |
| 887 | caam->jrid = jr_node - 1; |
Gaurav Jain | db4dd6a | 2022-03-24 11:50:33 +0530 | [diff] [blame] | 888 | scu_node = ofnode_by_compatible(ofnode_null(), "fsl,imx8-mu"); |
| 889 | if (ofnode_valid(scu_node)) { |
| 890 | if (jr_power_on(node)) |
| 891 | return -EINVAL; |
| 892 | } |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame] | 893 | break; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (sec_init()) |
| 898 | printf("\nsec_init failed!\n"); |
| 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | static int caam_jr_bind(struct udevice *dev) |
| 904 | { |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | static const struct misc_ops caam_jr_ops = { |
| 909 | .ioctl = caam_jr_ioctl, |
| 910 | }; |
| 911 | |
| 912 | static const struct udevice_id caam_jr_match[] = { |
| 913 | { .compatible = "fsl,sec-v4.0" }, |
| 914 | { } |
| 915 | }; |
| 916 | |
| 917 | U_BOOT_DRIVER(caam_jr) = { |
| 918 | .name = "caam_jr", |
| 919 | .id = UCLASS_MISC, |
| 920 | .of_match = caam_jr_match, |
| 921 | .ops = &caam_jr_ops, |
| 922 | .bind = caam_jr_bind, |
| 923 | .probe = caam_jr_probe, |
| 924 | .priv_auto = sizeof(struct caam_regs), |
| 925 | }; |
| 926 | #endif |