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