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. |
| 4 | * |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 5 | * Based on CAAM driver in drivers/crypto/caam in Linux |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include "fsl_sec.h" |
| 12 | #include "jr.h" |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 13 | #include "jobdesc.h" |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 14 | #include "desc_constr.h" |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 15 | #include <time.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame^] | 16 | #include <asm/cache.h> |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 17 | #ifdef CONFIG_FSL_CORENET |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame^] | 18 | #include <asm/cache.h> |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 19 | #include <asm/fsl_pamu.h> |
| 20 | #endif |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 21 | |
| 22 | #define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1)) |
| 23 | #define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size)) |
| 24 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 25 | uint32_t sec_offset[CONFIG_SYS_FSL_MAX_NUM_OF_SEC] = { |
| 26 | 0, |
York Sun | 4119aee | 2016-11-15 18:44:22 -0800 | [diff] [blame] | 27 | #if defined(CONFIG_ARCH_C29X) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 28 | CONFIG_SYS_FSL_SEC_IDX_OFFSET, |
| 29 | 2 * CONFIG_SYS_FSL_SEC_IDX_OFFSET |
| 30 | #endif |
| 31 | }; |
| 32 | |
| 33 | #define SEC_ADDR(idx) \ |
| 34 | ((CONFIG_SYS_FSL_SEC_ADDR + sec_offset[idx])) |
| 35 | |
| 36 | #define SEC_JR0_ADDR(idx) \ |
| 37 | (SEC_ADDR(idx) + \ |
| 38 | (CONFIG_SYS_FSL_JR0_OFFSET - CONFIG_SYS_FSL_SEC_OFFSET)) |
| 39 | |
| 40 | struct jobring jr0[CONFIG_SYS_FSL_MAX_NUM_OF_SEC]; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 41 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 42 | static inline void start_jr0(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 43 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 44 | ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 45 | u32 ctpr_ms = sec_in32(&sec->ctpr_ms); |
| 46 | u32 scfgr = sec_in32(&sec->scfgr); |
| 47 | |
| 48 | if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) { |
| 49 | /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or |
| 50 | * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1 |
| 51 | */ |
| 52 | if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) || |
xypron.glpk@gmx.de | 3ec0182 | 2017-04-15 16:37:54 +0200 | [diff] [blame] | 53 | (scfgr & SEC_SCFGR_VIRT_EN)) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 54 | sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0); |
| 55 | } else { |
| 56 | /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */ |
| 57 | if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) |
| 58 | sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0); |
| 59 | } |
| 60 | } |
| 61 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 62 | static inline void jr_reset_liodn(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 63 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 64 | ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 65 | sec_out32(&sec->jrliodnr[0].ls, 0); |
| 66 | } |
| 67 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 68 | static inline void jr_disable_irq(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 69 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 70 | struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 71 | uint32_t jrcfg = sec_in32(®s->jrcfg1); |
| 72 | |
| 73 | jrcfg = jrcfg | JR_INTMASK; |
| 74 | |
| 75 | sec_out32(®s->jrcfg1, jrcfg); |
| 76 | } |
| 77 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 78 | static void jr_initregs(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 79 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 80 | struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx); |
| 81 | struct jobring *jr = &jr0[sec_idx]; |
| 82 | phys_addr_t ip_base = virt_to_phys((void *)jr->input_ring); |
| 83 | phys_addr_t op_base = virt_to_phys((void *)jr->output_ring); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 84 | |
| 85 | #ifdef CONFIG_PHYS_64BIT |
| 86 | sec_out32(®s->irba_h, ip_base >> 32); |
| 87 | #else |
| 88 | sec_out32(®s->irba_h, 0x0); |
| 89 | #endif |
| 90 | sec_out32(®s->irba_l, (uint32_t)ip_base); |
| 91 | #ifdef CONFIG_PHYS_64BIT |
| 92 | sec_out32(®s->orba_h, op_base >> 32); |
| 93 | #else |
| 94 | sec_out32(®s->orba_h, 0x0); |
| 95 | #endif |
| 96 | sec_out32(®s->orba_l, (uint32_t)op_base); |
| 97 | sec_out32(®s->ors, JR_SIZE); |
| 98 | sec_out32(®s->irs, JR_SIZE); |
| 99 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 100 | if (!jr->irq) |
| 101 | jr_disable_irq(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 102 | } |
| 103 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 104 | static int jr_init(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 105 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 106 | struct jobring *jr = &jr0[sec_idx]; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 107 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 108 | memset(jr, 0, sizeof(struct jobring)); |
| 109 | |
| 110 | jr->jq_id = DEFAULT_JR_ID; |
| 111 | jr->irq = DEFAULT_IRQ; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 112 | |
| 113 | #ifdef CONFIG_FSL_CORENET |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 114 | jr->liodn = DEFAULT_JR_LIODN; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 115 | #endif |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 116 | jr->size = JR_SIZE; |
| 117 | jr->input_ring = (dma_addr_t *)memalign(ARCH_DMA_MINALIGN, |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 118 | JR_SIZE * sizeof(dma_addr_t)); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 119 | if (!jr->input_ring) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 120 | return -1; |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 121 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 122 | jr->op_size = roundup(JR_SIZE * sizeof(struct op_ring), |
| 123 | ARCH_DMA_MINALIGN); |
| 124 | jr->output_ring = |
| 125 | (struct op_ring *)memalign(ARCH_DMA_MINALIGN, jr->op_size); |
| 126 | if (!jr->output_ring) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 127 | return -1; |
| 128 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 129 | memset(jr->input_ring, 0, JR_SIZE * sizeof(dma_addr_t)); |
| 130 | memset(jr->output_ring, 0, jr->op_size); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 131 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 132 | start_jr0(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 133 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 134 | jr_initregs(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 139 | static int jr_sw_cleanup(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 140 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 141 | struct jobring *jr = &jr0[sec_idx]; |
| 142 | |
| 143 | jr->head = 0; |
| 144 | jr->tail = 0; |
| 145 | jr->read_idx = 0; |
| 146 | jr->write_idx = 0; |
| 147 | memset(jr->info, 0, sizeof(jr->info)); |
| 148 | memset(jr->input_ring, 0, jr->size * sizeof(dma_addr_t)); |
| 149 | memset(jr->output_ring, 0, jr->size * sizeof(struct op_ring)); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 154 | static int jr_hw_reset(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 155 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 156 | struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 157 | uint32_t timeout = 100000; |
| 158 | uint32_t jrint, jrcr; |
| 159 | |
| 160 | sec_out32(®s->jrcr, JRCR_RESET); |
| 161 | do { |
| 162 | jrint = sec_in32(®s->jrint); |
| 163 | } while (((jrint & JRINT_ERR_HALT_MASK) == |
| 164 | JRINT_ERR_HALT_INPROGRESS) && --timeout); |
| 165 | |
| 166 | jrint = sec_in32(®s->jrint); |
| 167 | if (((jrint & JRINT_ERR_HALT_MASK) != |
| 168 | JRINT_ERR_HALT_INPROGRESS) && timeout == 0) |
| 169 | return -1; |
| 170 | |
| 171 | timeout = 100000; |
| 172 | sec_out32(®s->jrcr, JRCR_RESET); |
| 173 | do { |
| 174 | jrcr = sec_in32(®s->jrcr); |
| 175 | } while ((jrcr & JRCR_RESET) && --timeout); |
| 176 | |
| 177 | if (timeout == 0) |
| 178 | return -1; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | /* -1 --- error, can't enqueue -- no space available */ |
| 184 | static int jr_enqueue(uint32_t *desc_addr, |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 185 | void (*callback)(uint32_t status, void *arg), |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 186 | void *arg, uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 187 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 188 | struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx); |
| 189 | struct jobring *jr = &jr0[sec_idx]; |
| 190 | int head = jr->head; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 191 | uint32_t desc_word; |
| 192 | int length = desc_len(desc_addr); |
| 193 | int i; |
| 194 | #ifdef CONFIG_PHYS_64BIT |
| 195 | uint32_t *addr_hi, *addr_lo; |
| 196 | #endif |
| 197 | |
| 198 | /* The descriptor must be submitted to SEC block as per endianness |
| 199 | * of the SEC Block. |
| 200 | * So, if the endianness of Core and SEC block is different, each word |
| 201 | * of the descriptor will be byte-swapped. |
| 202 | */ |
| 203 | for (i = 0; i < length; i++) { |
| 204 | desc_word = desc_addr[i]; |
| 205 | sec_out32((uint32_t *)&desc_addr[i], desc_word); |
| 206 | } |
| 207 | |
| 208 | phys_addr_t desc_phys_addr = virt_to_phys(desc_addr); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 209 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 210 | jr->info[head].desc_phys_addr = desc_phys_addr; |
| 211 | jr->info[head].callback = (void *)callback; |
| 212 | jr->info[head].arg = arg; |
| 213 | jr->info[head].op_done = 0; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 214 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 215 | unsigned long start = (unsigned long)&jr->info[head] & |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 216 | ~(ARCH_DMA_MINALIGN - 1); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 217 | unsigned long end = ALIGN((unsigned long)&jr->info[head] + |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 218 | sizeof(struct jr_info), ARCH_DMA_MINALIGN); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 219 | flush_dcache_range(start, end); |
| 220 | |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 221 | #ifdef CONFIG_PHYS_64BIT |
| 222 | /* Write the 64 bit Descriptor address on Input Ring. |
| 223 | * The 32 bit hign and low part of the address will |
| 224 | * depend on endianness of SEC block. |
| 225 | */ |
| 226 | #ifdef CONFIG_SYS_FSL_SEC_LE |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 227 | addr_lo = (uint32_t *)(&jr->input_ring[head]); |
| 228 | addr_hi = (uint32_t *)(&jr->input_ring[head]) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 229 | #elif defined(CONFIG_SYS_FSL_SEC_BE) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 230 | addr_hi = (uint32_t *)(&jr->input_ring[head]); |
| 231 | addr_lo = (uint32_t *)(&jr->input_ring[head]) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 232 | #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */ |
| 233 | |
| 234 | sec_out32(addr_hi, (uint32_t)(desc_phys_addr >> 32)); |
| 235 | sec_out32(addr_lo, (uint32_t)(desc_phys_addr)); |
| 236 | |
| 237 | #else |
| 238 | /* Write the 32 bit Descriptor address on Input Ring. */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 239 | sec_out32(&jr->input_ring[head], desc_phys_addr); |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 240 | #endif /* ifdef CONFIG_PHYS_64BIT */ |
| 241 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 242 | start = (unsigned long)&jr->input_ring[head] & ~(ARCH_DMA_MINALIGN - 1); |
| 243 | end = ALIGN((unsigned long)&jr->input_ring[head] + |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 244 | sizeof(dma_addr_t), ARCH_DMA_MINALIGN); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 245 | flush_dcache_range(start, end); |
| 246 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 247 | jr->head = (head + 1) & (jr->size - 1); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 248 | |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 249 | /* Invalidate output ring */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 250 | start = (unsigned long)jr->output_ring & |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 251 | ~(ARCH_DMA_MINALIGN - 1); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 252 | end = ALIGN((unsigned long)jr->output_ring + jr->op_size, |
| 253 | ARCH_DMA_MINALIGN); |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 254 | invalidate_dcache_range(start, end); |
| 255 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 256 | sec_out32(®s->irja, 1); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 261 | static int jr_dequeue(int sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 262 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 263 | struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx); |
| 264 | struct jobring *jr = &jr0[sec_idx]; |
| 265 | int head = jr->head; |
| 266 | int tail = jr->tail; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 267 | int idx, i, found; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 268 | void (*callback)(uint32_t status, void *arg); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 269 | void *arg = NULL; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 270 | #ifdef CONFIG_PHYS_64BIT |
| 271 | uint32_t *addr_hi, *addr_lo; |
| 272 | #else |
| 273 | uint32_t *addr; |
| 274 | #endif |
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 | while (sec_in32(®s->orsf) && CIRC_CNT(jr->head, jr->tail, |
| 277 | jr->size)) { |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 278 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 279 | found = 0; |
| 280 | |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 281 | phys_addr_t op_desc; |
| 282 | #ifdef CONFIG_PHYS_64BIT |
| 283 | /* Read the 64 bit Descriptor address from Output Ring. |
| 284 | * The 32 bit hign and low part of the address will |
| 285 | * depend on endianness of SEC block. |
| 286 | */ |
| 287 | #ifdef CONFIG_SYS_FSL_SEC_LE |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 288 | addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc); |
| 289 | addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 290 | #elif defined(CONFIG_SYS_FSL_SEC_BE) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 291 | addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc); |
| 292 | addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 293 | #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */ |
| 294 | |
| 295 | op_desc = ((u64)sec_in32(addr_hi) << 32) | |
| 296 | ((u64)sec_in32(addr_lo)); |
| 297 | |
| 298 | #else |
| 299 | /* Read the 32 bit Descriptor address from Output Ring. */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 300 | addr = (uint32_t *)&jr->output_ring[jr->tail].desc; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 301 | op_desc = sec_in32(addr); |
| 302 | #endif /* ifdef CONFIG_PHYS_64BIT */ |
| 303 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 304 | uint32_t status = sec_in32(&jr->output_ring[jr->tail].status); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 305 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 306 | for (i = 0; CIRC_CNT(head, tail + i, jr->size) >= 1; i++) { |
| 307 | idx = (tail + i) & (jr->size - 1); |
| 308 | if (op_desc == jr->info[idx].desc_phys_addr) { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 309 | found = 1; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /* Error condition if match not found */ |
| 315 | if (!found) |
| 316 | return -1; |
| 317 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 318 | jr->info[idx].op_done = 1; |
| 319 | callback = (void *)jr->info[idx].callback; |
| 320 | arg = jr->info[idx].arg; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 321 | |
| 322 | /* When the job on tail idx gets done, increment |
| 323 | * tail till the point where job completed out of oredr has |
| 324 | * been taken into account |
| 325 | */ |
| 326 | if (idx == tail) |
| 327 | do { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 328 | tail = (tail + 1) & (jr->size - 1); |
| 329 | } while (jr->info[tail].op_done); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 330 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 331 | jr->tail = tail; |
| 332 | jr->read_idx = (jr->read_idx + 1) & (jr->size - 1); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 333 | |
| 334 | sec_out32(®s->orjr, 1); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 335 | jr->info[idx].op_done = 0; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 336 | |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 337 | callback(status, arg); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 343 | static void desc_done(uint32_t status, void *arg) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 344 | { |
| 345 | struct result *x = arg; |
| 346 | x->status = status; |
Ruchika Gupta | 0009c8f | 2017-04-17 18:07:19 +0530 | [diff] [blame] | 347 | #ifndef CONFIG_SPL_BUILD |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 348 | caam_jr_strstatus(status); |
Ruchika Gupta | 0009c8f | 2017-04-17 18:07:19 +0530 | [diff] [blame] | 349 | #endif |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 350 | x->done = 1; |
| 351 | } |
| 352 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 353 | 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] | 354 | { |
| 355 | unsigned long long timeval = get_ticks(); |
| 356 | unsigned long long timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT); |
| 357 | struct result op; |
| 358 | int ret = 0; |
| 359 | |
gaurav rana | 0762150 | 2014-12-04 13:00:41 +0530 | [diff] [blame] | 360 | memset(&op, 0, sizeof(op)); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 361 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 362 | ret = jr_enqueue(desc, desc_done, &op, sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 363 | if (ret) { |
| 364 | debug("Error in SEC enq\n"); |
| 365 | ret = JQ_ENQ_ERR; |
| 366 | goto out; |
| 367 | } |
| 368 | |
| 369 | timeval = get_ticks(); |
| 370 | timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT); |
| 371 | while (op.done != 1) { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 372 | ret = jr_dequeue(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 373 | if (ret) { |
| 374 | debug("Error in SEC deq\n"); |
| 375 | ret = JQ_DEQ_ERR; |
| 376 | goto out; |
| 377 | } |
| 378 | |
| 379 | if ((get_ticks() - timeval) > timeout) { |
| 380 | debug("SEC Dequeue timed out\n"); |
| 381 | ret = JQ_DEQ_TO_ERR; |
| 382 | goto out; |
| 383 | } |
| 384 | } |
| 385 | |
Aneesh Bansal | 3ab29d7 | 2016-02-11 14:36:51 +0530 | [diff] [blame] | 386 | if (op.status) { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 387 | debug("Error %x\n", op.status); |
| 388 | ret = op.status; |
| 389 | } |
| 390 | out: |
| 391 | return ret; |
| 392 | } |
| 393 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 394 | int run_descriptor_jr(uint32_t *desc) |
| 395 | { |
| 396 | return run_descriptor_jr_idx(desc, 0); |
| 397 | } |
| 398 | |
| 399 | static inline int jr_reset_sec(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 400 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 401 | if (jr_hw_reset(sec_idx) < 0) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 402 | return -1; |
| 403 | |
| 404 | /* Clean up the jobring structure maintained by software */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 405 | jr_sw_cleanup(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 410 | int jr_reset(void) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 411 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 412 | return jr_reset_sec(0); |
| 413 | } |
| 414 | |
| 415 | static inline int sec_reset_idx(uint8_t sec_idx) |
| 416 | { |
| 417 | ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 418 | uint32_t mcfgr = sec_in32(&sec->mcfgr); |
| 419 | uint32_t timeout = 100000; |
| 420 | |
| 421 | mcfgr |= MCFGR_SWRST; |
| 422 | sec_out32(&sec->mcfgr, mcfgr); |
| 423 | |
| 424 | mcfgr |= MCFGR_DMA_RST; |
| 425 | sec_out32(&sec->mcfgr, mcfgr); |
| 426 | do { |
| 427 | mcfgr = sec_in32(&sec->mcfgr); |
| 428 | } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout); |
| 429 | |
| 430 | if (timeout == 0) |
| 431 | return -1; |
| 432 | |
| 433 | timeout = 100000; |
| 434 | do { |
| 435 | mcfgr = sec_in32(&sec->mcfgr); |
| 436 | } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout); |
| 437 | |
| 438 | if (timeout == 0) |
| 439 | return -1; |
| 440 | |
| 441 | return 0; |
| 442 | } |
Ruchika Gupta | 0009c8f | 2017-04-17 18:07:19 +0530 | [diff] [blame] | 443 | int sec_reset(void) |
| 444 | { |
| 445 | return sec_reset_idx(0); |
| 446 | } |
| 447 | #ifndef CONFIG_SPL_BUILD |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 448 | static int instantiate_rng(uint8_t sec_idx) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 449 | { |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 450 | u32 *desc; |
| 451 | u32 rdsta_val; |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 452 | int ret = 0, sh_idx, size; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 453 | ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 454 | struct rng4tst __iomem *rng = |
| 455 | (struct rng4tst __iomem *)&sec->rng; |
| 456 | |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 457 | desc = memalign(ARCH_DMA_MINALIGN, sizeof(uint32_t) * 6); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 458 | if (!desc) { |
| 459 | printf("cannot allocate RNG init descriptor memory\n"); |
| 460 | return -1; |
| 461 | } |
| 462 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 463 | for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { |
| 464 | /* |
| 465 | * If the corresponding bit is set, this state handle |
| 466 | * was initialized by somebody else, so it's left alone. |
| 467 | */ |
| 468 | rdsta_val = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK; |
| 469 | if (rdsta_val & (1 << sh_idx)) |
| 470 | continue; |
| 471 | |
| 472 | inline_cnstr_jobdesc_rng_instantiation(desc, sh_idx); |
| 473 | size = roundup(sizeof(uint32_t) * 6, ARCH_DMA_MINALIGN); |
| 474 | flush_dcache_range((unsigned long)desc, |
| 475 | (unsigned long)desc + size); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 476 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 477 | ret = run_descriptor_jr_idx(desc, sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 478 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 479 | if (ret) |
| 480 | printf("RNG: Instantiation failed with error 0x%x\n", |
| 481 | ret); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 482 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 483 | rdsta_val = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK; |
| 484 | if (!(rdsta_val & (1 << sh_idx))) { |
| 485 | free(desc); |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | memset(desc, 0, sizeof(uint32_t) * 6); |
| 490 | } |
| 491 | |
| 492 | free(desc); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 497 | static u8 get_rng_vid(uint8_t sec_idx) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 498 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 499 | ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 500 | u32 cha_vid = sec_in32(&sec->chavid_ls); |
| 501 | |
| 502 | return (cha_vid & SEC_CHAVID_RNG_LS_MASK) >> SEC_CHAVID_LS_RNG_SHIFT; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * By default, the TRNG runs for 200 clocks per sample; |
| 507 | * 1200 clocks per sample generates better entropy. |
| 508 | */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 509 | static void kick_trng(int ent_delay, uint8_t sec_idx) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 510 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 511 | ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 512 | struct rng4tst __iomem *rng = |
| 513 | (struct rng4tst __iomem *)&sec->rng; |
| 514 | u32 val; |
| 515 | |
| 516 | /* put RNG4 into program mode */ |
| 517 | sec_setbits32(&rng->rtmctl, RTMCTL_PRGM); |
| 518 | /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the |
| 519 | * length (in system clocks) of each Entropy sample taken |
| 520 | * */ |
| 521 | val = sec_in32(&rng->rtsdctl); |
| 522 | val = (val & ~RTSDCTL_ENT_DLY_MASK) | |
| 523 | (ent_delay << RTSDCTL_ENT_DLY_SHIFT); |
| 524 | sec_out32(&rng->rtsdctl, val); |
| 525 | /* min. freq. count, equal to 1/4 of the entropy sample length */ |
| 526 | sec_out32(&rng->rtfreqmin, ent_delay >> 2); |
Alex Porosanu | f8d6a7f | 2015-05-05 16:48:33 +0300 | [diff] [blame] | 527 | /* disable maximum frequency count */ |
| 528 | sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE); |
Alex Porosanu | befb5cb | 2015-05-05 16:48:35 +0300 | [diff] [blame] | 529 | /* |
| 530 | * select raw sampling in both entropy shifter |
| 531 | * and statistical checker |
| 532 | */ |
Aneesh Bansal | 1fa9c90 | 2015-12-08 13:54:30 +0530 | [diff] [blame] | 533 | sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 534 | /* put RNG4 into run mode */ |
Aneesh Bansal | 1fa9c90 | 2015-12-08 13:54:30 +0530 | [diff] [blame] | 535 | sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 536 | } |
| 537 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 538 | static int rng_init(uint8_t sec_idx) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 539 | { |
| 540 | int ret, ent_delay = RTSDCTL_ENT_DLY_MIN; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 541 | ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 542 | struct rng4tst __iomem *rng = |
| 543 | (struct rng4tst __iomem *)&sec->rng; |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 544 | u32 inst_handles; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 545 | |
| 546 | do { |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 547 | inst_handles = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK; |
| 548 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 549 | /* |
| 550 | * If either of the SH's were instantiated by somebody else |
| 551 | * then it is assumed that the entropy |
| 552 | * parameters are properly set and thus the function |
| 553 | * setting these (kick_trng(...)) is skipped. |
| 554 | * Also, if a handle was instantiated, do not change |
| 555 | * the TRNG parameters. |
| 556 | */ |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 557 | if (!inst_handles) { |
| 558 | kick_trng(ent_delay, sec_idx); |
| 559 | ent_delay += 400; |
| 560 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 561 | /* |
| 562 | * if instantiate_rng(...) fails, the loop will rerun |
| 563 | * and the kick_trng(...) function will modfiy the |
| 564 | * upper and lower limits of the entropy sampling |
| 565 | * interval, leading to a sucessful initialization of |
| 566 | * the RNG. |
| 567 | */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 568 | ret = instantiate_rng(sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 569 | } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX)); |
| 570 | if (ret) { |
| 571 | printf("RNG: Failed to instantiate RNG\n"); |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | /* Enable RDB bit so that RNG works faster */ |
| 576 | sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE); |
| 577 | |
| 578 | return ret; |
| 579 | } |
Ruchika Gupta | 0009c8f | 2017-04-17 18:07:19 +0530 | [diff] [blame] | 580 | #endif |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 581 | int sec_init_idx(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 582 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 583 | ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 584 | uint32_t mcr = sec_in32(&sec->mcfgr); |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 585 | int ret = 0; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 586 | |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 587 | #ifdef CONFIG_FSL_CORENET |
| 588 | uint32_t liodnr; |
| 589 | uint32_t liodn_ns; |
| 590 | uint32_t liodn_s; |
| 591 | #endif |
| 592 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 593 | if (!(sec_idx < CONFIG_SYS_FSL_MAX_NUM_OF_SEC)) { |
| 594 | printf("SEC initialization failed\n"); |
| 595 | return -1; |
| 596 | } |
| 597 | |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 598 | /* |
| 599 | * Modifying CAAM Read/Write Attributes |
York Sun | cbe8e1c | 2016-04-04 11:41:26 -0700 | [diff] [blame] | 600 | * For LS2080A |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 601 | * For AXI Write - Cacheable, Write Back, Write allocate |
| 602 | * For AXI Read - Cacheable, Read allocate |
York Sun | cbe8e1c | 2016-04-04 11:41:26 -0700 | [diff] [blame] | 603 | * Only For LS2080a, to solve CAAM coherency issues |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 604 | */ |
York Sun | 4ce6fbf | 2017-03-27 11:41:01 -0700 | [diff] [blame] | 605 | #ifdef CONFIG_ARCH_LS2080A |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 606 | mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0xb << MCFGR_AWCACHE_SHIFT); |
| 607 | mcr = (mcr & ~MCFGR_ARCACHE_MASK) | (0x6 << MCFGR_ARCACHE_SHIFT); |
| 608 | #else |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 609 | mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0x2 << MCFGR_AWCACHE_SHIFT); |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 610 | #endif |
| 611 | |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 612 | #ifdef CONFIG_PHYS_64BIT |
| 613 | mcr |= (1 << MCFGR_PS_SHIFT); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 614 | #endif |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 615 | sec_out32(&sec->mcfgr, mcr); |
| 616 | |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 617 | #ifdef CONFIG_FSL_CORENET |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 618 | #ifdef CONFIG_SPL_BUILD |
| 619 | /* |
| 620 | * For SPL Build, Set the Liodns in SEC JR0 for |
| 621 | * creating PAMU entries corresponding to these. |
| 622 | * For normal build, these are set in set_liodns(). |
| 623 | */ |
| 624 | liodn_ns = CONFIG_SPL_JR0_LIODN_NS & JRNSLIODN_MASK; |
| 625 | liodn_s = CONFIG_SPL_JR0_LIODN_S & JRSLIODN_MASK; |
| 626 | |
| 627 | liodnr = sec_in32(&sec->jrliodnr[0].ls) & |
| 628 | ~(JRNSLIODN_MASK | JRSLIODN_MASK); |
| 629 | liodnr = liodnr | |
| 630 | (liodn_ns << JRNSLIODN_SHIFT) | |
| 631 | (liodn_s << JRSLIODN_SHIFT); |
| 632 | sec_out32(&sec->jrliodnr[0].ls, liodnr); |
| 633 | #else |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 634 | liodnr = sec_in32(&sec->jrliodnr[0].ls); |
| 635 | liodn_ns = (liodnr & JRNSLIODN_MASK) >> JRNSLIODN_SHIFT; |
| 636 | liodn_s = (liodnr & JRSLIODN_MASK) >> JRSLIODN_SHIFT; |
| 637 | #endif |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 638 | #endif |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 639 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 640 | ret = jr_init(sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 641 | if (ret < 0) { |
| 642 | printf("SEC initialization failed\n"); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 643 | return -1; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 644 | } |
| 645 | |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 646 | #ifdef CONFIG_FSL_CORENET |
| 647 | ret = sec_config_pamu_table(liodn_ns, liodn_s); |
| 648 | if (ret < 0) |
| 649 | return -1; |
| 650 | |
| 651 | pamu_enable(); |
| 652 | #endif |
Ruchika Gupta | 0009c8f | 2017-04-17 18:07:19 +0530 | [diff] [blame] | 653 | #ifndef CONFIG_SPL_BUILD |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 654 | if (get_rng_vid(sec_idx) >= 4) { |
| 655 | if (rng_init(sec_idx) < 0) { |
| 656 | printf("SEC%u: RNG instantiation failed\n", sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 657 | return -1; |
| 658 | } |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 659 | printf("SEC%u: RNG instantiated\n", sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 660 | } |
Ruchika Gupta | 0009c8f | 2017-04-17 18:07:19 +0530 | [diff] [blame] | 661 | #endif |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 662 | return ret; |
| 663 | } |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 664 | |
| 665 | int sec_init(void) |
| 666 | { |
| 667 | return sec_init_idx(0); |
| 668 | } |