blob: aa84f2cee0aba7f6530469f190649c32b617fce0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Guptaac1b2692014-10-15 11:35:30 +05302/*
3 * Copyright 2008-2014 Freescale Semiconductor, Inc.
4 *
Ruchika Guptaac1b2692014-10-15 11:35:30 +05305 * Based on CAAM driver in drivers/crypto/caam in Linux
6 */
7
8#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053010#include <malloc.h>
11#include "fsl_sec.h"
12#include "jr.h"
Ruchika Gupta4345a572014-10-07 15:46:20 +053013#include "jobdesc.h"
Aneesh Bansal43421822015-10-29 22:58:03 +053014#include "desc_constr.h"
Simon Glass45c78902019-11-14 12:57:26 -070015#include <time.h>
Aneesh Bansal4b636c32016-01-22 17:05:59 +053016#ifdef CONFIG_FSL_CORENET
17#include <asm/fsl_pamu.h>
18#endif
Ruchika Guptaac1b2692014-10-15 11:35:30 +053019
20#define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1))
21#define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size))
22
Alex Porosanu7703d1e2016-04-29 15:18:00 +030023uint32_t sec_offset[CONFIG_SYS_FSL_MAX_NUM_OF_SEC] = {
24 0,
York Sun4119aee2016-11-15 18:44:22 -080025#if defined(CONFIG_ARCH_C29X)
Alex Porosanu7703d1e2016-04-29 15:18:00 +030026 CONFIG_SYS_FSL_SEC_IDX_OFFSET,
27 2 * CONFIG_SYS_FSL_SEC_IDX_OFFSET
28#endif
29};
30
31#define SEC_ADDR(idx) \
32 ((CONFIG_SYS_FSL_SEC_ADDR + sec_offset[idx]))
33
34#define SEC_JR0_ADDR(idx) \
35 (SEC_ADDR(idx) + \
36 (CONFIG_SYS_FSL_JR0_OFFSET - CONFIG_SYS_FSL_SEC_OFFSET))
37
38struct jobring jr0[CONFIG_SYS_FSL_MAX_NUM_OF_SEC];
Ruchika Guptaac1b2692014-10-15 11:35:30 +053039
Alex Porosanu7703d1e2016-04-29 15:18:00 +030040static inline void start_jr0(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +053041{
Alex Porosanu7703d1e2016-04-29 15:18:00 +030042 ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +053043 u32 ctpr_ms = sec_in32(&sec->ctpr_ms);
44 u32 scfgr = sec_in32(&sec->scfgr);
45
46 if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) {
47 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
48 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1
49 */
50 if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) ||
xypron.glpk@gmx.de3ec01822017-04-15 16:37:54 +020051 (scfgr & SEC_SCFGR_VIRT_EN))
Ruchika Guptaac1b2692014-10-15 11:35:30 +053052 sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0);
53 } else {
54 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
55 if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR)
56 sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0);
57 }
58}
59
Alex Porosanu7703d1e2016-04-29 15:18:00 +030060static inline void jr_reset_liodn(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +053061{
Alex Porosanu7703d1e2016-04-29 15:18:00 +030062 ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +053063 sec_out32(&sec->jrliodnr[0].ls, 0);
64}
65
Alex Porosanu7703d1e2016-04-29 15:18:00 +030066static inline void jr_disable_irq(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +053067{
Alex Porosanu7703d1e2016-04-29 15:18:00 +030068 struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +053069 uint32_t jrcfg = sec_in32(&regs->jrcfg1);
70
71 jrcfg = jrcfg | JR_INTMASK;
72
73 sec_out32(&regs->jrcfg1, jrcfg);
74}
75
Alex Porosanu7703d1e2016-04-29 15:18:00 +030076static void jr_initregs(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +053077{
Alex Porosanu7703d1e2016-04-29 15:18:00 +030078 struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
79 struct jobring *jr = &jr0[sec_idx];
80 phys_addr_t ip_base = virt_to_phys((void *)jr->input_ring);
81 phys_addr_t op_base = virt_to_phys((void *)jr->output_ring);
Ruchika Guptaac1b2692014-10-15 11:35:30 +053082
83#ifdef CONFIG_PHYS_64BIT
84 sec_out32(&regs->irba_h, ip_base >> 32);
85#else
86 sec_out32(&regs->irba_h, 0x0);
87#endif
88 sec_out32(&regs->irba_l, (uint32_t)ip_base);
89#ifdef CONFIG_PHYS_64BIT
90 sec_out32(&regs->orba_h, op_base >> 32);
91#else
92 sec_out32(&regs->orba_h, 0x0);
93#endif
94 sec_out32(&regs->orba_l, (uint32_t)op_base);
95 sec_out32(&regs->ors, JR_SIZE);
96 sec_out32(&regs->irs, JR_SIZE);
97
Alex Porosanu7703d1e2016-04-29 15:18:00 +030098 if (!jr->irq)
99 jr_disable_irq(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530100}
101
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300102static int jr_init(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530103{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300104 struct jobring *jr = &jr0[sec_idx];
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530105
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300106 memset(jr, 0, sizeof(struct jobring));
107
108 jr->jq_id = DEFAULT_JR_ID;
109 jr->irq = DEFAULT_IRQ;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530110
111#ifdef CONFIG_FSL_CORENET
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300112 jr->liodn = DEFAULT_JR_LIODN;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530113#endif
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300114 jr->size = JR_SIZE;
115 jr->input_ring = (dma_addr_t *)memalign(ARCH_DMA_MINALIGN,
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600116 JR_SIZE * sizeof(dma_addr_t));
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300117 if (!jr->input_ring)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530118 return -1;
Ruchika Guptad2180332016-01-22 16:12:55 +0530119
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300120 jr->op_size = roundup(JR_SIZE * sizeof(struct op_ring),
121 ARCH_DMA_MINALIGN);
122 jr->output_ring =
123 (struct op_ring *)memalign(ARCH_DMA_MINALIGN, jr->op_size);
124 if (!jr->output_ring)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530125 return -1;
126
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300127 memset(jr->input_ring, 0, JR_SIZE * sizeof(dma_addr_t));
128 memset(jr->output_ring, 0, jr->op_size);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530129
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300130 start_jr0(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530131
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300132 jr_initregs(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530133
134 return 0;
135}
136
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300137static int jr_sw_cleanup(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530138{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300139 struct jobring *jr = &jr0[sec_idx];
140
141 jr->head = 0;
142 jr->tail = 0;
143 jr->read_idx = 0;
144 jr->write_idx = 0;
145 memset(jr->info, 0, sizeof(jr->info));
146 memset(jr->input_ring, 0, jr->size * sizeof(dma_addr_t));
147 memset(jr->output_ring, 0, jr->size * sizeof(struct op_ring));
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530148
149 return 0;
150}
151
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300152static int jr_hw_reset(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530153{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300154 struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530155 uint32_t timeout = 100000;
156 uint32_t jrint, jrcr;
157
158 sec_out32(&regs->jrcr, JRCR_RESET);
159 do {
160 jrint = sec_in32(&regs->jrint);
161 } while (((jrint & JRINT_ERR_HALT_MASK) ==
162 JRINT_ERR_HALT_INPROGRESS) && --timeout);
163
164 jrint = sec_in32(&regs->jrint);
165 if (((jrint & JRINT_ERR_HALT_MASK) !=
166 JRINT_ERR_HALT_INPROGRESS) && timeout == 0)
167 return -1;
168
169 timeout = 100000;
170 sec_out32(&regs->jrcr, JRCR_RESET);
171 do {
172 jrcr = sec_in32(&regs->jrcr);
173 } while ((jrcr & JRCR_RESET) && --timeout);
174
175 if (timeout == 0)
176 return -1;
177
178 return 0;
179}
180
181/* -1 --- error, can't enqueue -- no space available */
182static int jr_enqueue(uint32_t *desc_addr,
Aneesh Bansal43421822015-10-29 22:58:03 +0530183 void (*callback)(uint32_t status, void *arg),
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300184 void *arg, uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530185{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300186 struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
187 struct jobring *jr = &jr0[sec_idx];
188 int head = jr->head;
Aneesh Bansal43421822015-10-29 22:58:03 +0530189 uint32_t desc_word;
190 int length = desc_len(desc_addr);
191 int i;
192#ifdef CONFIG_PHYS_64BIT
193 uint32_t *addr_hi, *addr_lo;
194#endif
195
196 /* The descriptor must be submitted to SEC block as per endianness
197 * of the SEC Block.
198 * So, if the endianness of Core and SEC block is different, each word
199 * of the descriptor will be byte-swapped.
200 */
201 for (i = 0; i < length; i++) {
202 desc_word = desc_addr[i];
203 sec_out32((uint32_t *)&desc_addr[i], desc_word);
204 }
205
206 phys_addr_t desc_phys_addr = virt_to_phys(desc_addr);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530207
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300208 jr->info[head].desc_phys_addr = desc_phys_addr;
209 jr->info[head].callback = (void *)callback;
210 jr->info[head].arg = arg;
211 jr->info[head].op_done = 0;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530212
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300213 unsigned long start = (unsigned long)&jr->info[head] &
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600214 ~(ARCH_DMA_MINALIGN - 1);
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300215 unsigned long end = ALIGN((unsigned long)&jr->info[head] +
Ruchika Guptad2180332016-01-22 16:12:55 +0530216 sizeof(struct jr_info), ARCH_DMA_MINALIGN);
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600217 flush_dcache_range(start, end);
218
Aneesh Bansal43421822015-10-29 22:58:03 +0530219#ifdef CONFIG_PHYS_64BIT
220 /* Write the 64 bit Descriptor address on Input Ring.
221 * The 32 bit hign and low part of the address will
222 * depend on endianness of SEC block.
223 */
224#ifdef CONFIG_SYS_FSL_SEC_LE
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300225 addr_lo = (uint32_t *)(&jr->input_ring[head]);
226 addr_hi = (uint32_t *)(&jr->input_ring[head]) + 1;
Aneesh Bansal43421822015-10-29 22:58:03 +0530227#elif defined(CONFIG_SYS_FSL_SEC_BE)
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300228 addr_hi = (uint32_t *)(&jr->input_ring[head]);
229 addr_lo = (uint32_t *)(&jr->input_ring[head]) + 1;
Aneesh Bansal43421822015-10-29 22:58:03 +0530230#endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
231
232 sec_out32(addr_hi, (uint32_t)(desc_phys_addr >> 32));
233 sec_out32(addr_lo, (uint32_t)(desc_phys_addr));
234
235#else
236 /* Write the 32 bit Descriptor address on Input Ring. */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300237 sec_out32(&jr->input_ring[head], desc_phys_addr);
Aneesh Bansal43421822015-10-29 22:58:03 +0530238#endif /* ifdef CONFIG_PHYS_64BIT */
239
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300240 start = (unsigned long)&jr->input_ring[head] & ~(ARCH_DMA_MINALIGN - 1);
241 end = ALIGN((unsigned long)&jr->input_ring[head] +
Ruchika Guptad2180332016-01-22 16:12:55 +0530242 sizeof(dma_addr_t), ARCH_DMA_MINALIGN);
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600243 flush_dcache_range(start, end);
244
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300245 jr->head = (head + 1) & (jr->size - 1);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530246
Ruchika Guptad2180332016-01-22 16:12:55 +0530247 /* Invalidate output ring */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300248 start = (unsigned long)jr->output_ring &
Ruchika Guptad2180332016-01-22 16:12:55 +0530249 ~(ARCH_DMA_MINALIGN - 1);
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300250 end = ALIGN((unsigned long)jr->output_ring + jr->op_size,
251 ARCH_DMA_MINALIGN);
Ruchika Guptad2180332016-01-22 16:12:55 +0530252 invalidate_dcache_range(start, end);
253
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530254 sec_out32(&regs->irja, 1);
255
256 return 0;
257}
258
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300259static int jr_dequeue(int sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530260{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300261 struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
262 struct jobring *jr = &jr0[sec_idx];
263 int head = jr->head;
264 int tail = jr->tail;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530265 int idx, i, found;
Aneesh Bansal43421822015-10-29 22:58:03 +0530266 void (*callback)(uint32_t status, void *arg);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530267 void *arg = NULL;
Aneesh Bansal43421822015-10-29 22:58:03 +0530268#ifdef CONFIG_PHYS_64BIT
269 uint32_t *addr_hi, *addr_lo;
270#else
271 uint32_t *addr;
272#endif
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530273
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300274 while (sec_in32(&regs->orsf) && CIRC_CNT(jr->head, jr->tail,
275 jr->size)) {
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600276
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530277 found = 0;
278
Aneesh Bansal43421822015-10-29 22:58:03 +0530279 phys_addr_t op_desc;
280 #ifdef CONFIG_PHYS_64BIT
281 /* Read the 64 bit Descriptor address from Output Ring.
282 * The 32 bit hign and low part of the address will
283 * depend on endianness of SEC block.
284 */
285 #ifdef CONFIG_SYS_FSL_SEC_LE
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300286 addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc);
287 addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1;
Aneesh Bansal43421822015-10-29 22:58:03 +0530288 #elif defined(CONFIG_SYS_FSL_SEC_BE)
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300289 addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc);
290 addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1;
Aneesh Bansal43421822015-10-29 22:58:03 +0530291 #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
292
293 op_desc = ((u64)sec_in32(addr_hi) << 32) |
294 ((u64)sec_in32(addr_lo));
295
296 #else
297 /* Read the 32 bit Descriptor address from Output Ring. */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300298 addr = (uint32_t *)&jr->output_ring[jr->tail].desc;
Aneesh Bansal43421822015-10-29 22:58:03 +0530299 op_desc = sec_in32(addr);
300 #endif /* ifdef CONFIG_PHYS_64BIT */
301
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300302 uint32_t status = sec_in32(&jr->output_ring[jr->tail].status);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530303
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300304 for (i = 0; CIRC_CNT(head, tail + i, jr->size) >= 1; i++) {
305 idx = (tail + i) & (jr->size - 1);
306 if (op_desc == jr->info[idx].desc_phys_addr) {
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530307 found = 1;
308 break;
309 }
310 }
311
312 /* Error condition if match not found */
313 if (!found)
314 return -1;
315
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300316 jr->info[idx].op_done = 1;
317 callback = (void *)jr->info[idx].callback;
318 arg = jr->info[idx].arg;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530319
320 /* When the job on tail idx gets done, increment
321 * tail till the point where job completed out of oredr has
322 * been taken into account
323 */
324 if (idx == tail)
325 do {
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300326 tail = (tail + 1) & (jr->size - 1);
327 } while (jr->info[tail].op_done);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530328
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300329 jr->tail = tail;
330 jr->read_idx = (jr->read_idx + 1) & (jr->size - 1);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530331
332 sec_out32(&regs->orjr, 1);
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300333 jr->info[idx].op_done = 0;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530334
Aneesh Bansal43421822015-10-29 22:58:03 +0530335 callback(status, arg);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530336 }
337
338 return 0;
339}
340
Aneesh Bansal43421822015-10-29 22:58:03 +0530341static void desc_done(uint32_t status, void *arg)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530342{
343 struct result *x = arg;
344 x->status = status;
Ruchika Gupta0009c8f2017-04-17 18:07:19 +0530345#ifndef CONFIG_SPL_BUILD
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530346 caam_jr_strstatus(status);
Ruchika Gupta0009c8f2017-04-17 18:07:19 +0530347#endif
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530348 x->done = 1;
349}
350
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300351static inline int run_descriptor_jr_idx(uint32_t *desc, uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530352{
353 unsigned long long timeval = get_ticks();
354 unsigned long long timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT);
355 struct result op;
356 int ret = 0;
357
gaurav rana07621502014-12-04 13:00:41 +0530358 memset(&op, 0, sizeof(op));
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530359
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300360 ret = jr_enqueue(desc, desc_done, &op, sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530361 if (ret) {
362 debug("Error in SEC enq\n");
363 ret = JQ_ENQ_ERR;
364 goto out;
365 }
366
367 timeval = get_ticks();
368 timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT);
369 while (op.done != 1) {
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300370 ret = jr_dequeue(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530371 if (ret) {
372 debug("Error in SEC deq\n");
373 ret = JQ_DEQ_ERR;
374 goto out;
375 }
376
377 if ((get_ticks() - timeval) > timeout) {
378 debug("SEC Dequeue timed out\n");
379 ret = JQ_DEQ_TO_ERR;
380 goto out;
381 }
382 }
383
Aneesh Bansal3ab29d72016-02-11 14:36:51 +0530384 if (op.status) {
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530385 debug("Error %x\n", op.status);
386 ret = op.status;
387 }
388out:
389 return ret;
390}
391
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300392int run_descriptor_jr(uint32_t *desc)
393{
394 return run_descriptor_jr_idx(desc, 0);
395}
396
397static inline int jr_reset_sec(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530398{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300399 if (jr_hw_reset(sec_idx) < 0)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530400 return -1;
401
402 /* Clean up the jobring structure maintained by software */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300403 jr_sw_cleanup(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530404
405 return 0;
406}
407
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300408int jr_reset(void)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530409{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300410 return jr_reset_sec(0);
411}
412
413static inline int sec_reset_idx(uint8_t sec_idx)
414{
415 ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530416 uint32_t mcfgr = sec_in32(&sec->mcfgr);
417 uint32_t timeout = 100000;
418
419 mcfgr |= MCFGR_SWRST;
420 sec_out32(&sec->mcfgr, mcfgr);
421
422 mcfgr |= MCFGR_DMA_RST;
423 sec_out32(&sec->mcfgr, mcfgr);
424 do {
425 mcfgr = sec_in32(&sec->mcfgr);
426 } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout);
427
428 if (timeout == 0)
429 return -1;
430
431 timeout = 100000;
432 do {
433 mcfgr = sec_in32(&sec->mcfgr);
434 } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout);
435
436 if (timeout == 0)
437 return -1;
438
439 return 0;
440}
Ruchika Gupta0009c8f2017-04-17 18:07:19 +0530441int sec_reset(void)
442{
443 return sec_reset_idx(0);
444}
445#ifndef CONFIG_SPL_BUILD
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300446static int instantiate_rng(uint8_t sec_idx)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530447{
Ruchika Gupta4345a572014-10-07 15:46:20 +0530448 u32 *desc;
449 u32 rdsta_val;
Lukas Aueraed8eac2018-01-25 14:11:17 +0100450 int ret = 0, sh_idx, size;
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300451 ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530452 struct rng4tst __iomem *rng =
453 (struct rng4tst __iomem *)&sec->rng;
454
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600455 desc = memalign(ARCH_DMA_MINALIGN, sizeof(uint32_t) * 6);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530456 if (!desc) {
457 printf("cannot allocate RNG init descriptor memory\n");
458 return -1;
459 }
460
Lukas Aueraed8eac2018-01-25 14:11:17 +0100461 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
462 /*
463 * If the corresponding bit is set, this state handle
464 * was initialized by somebody else, so it's left alone.
465 */
466 rdsta_val = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK;
467 if (rdsta_val & (1 << sh_idx))
468 continue;
469
470 inline_cnstr_jobdesc_rng_instantiation(desc, sh_idx);
471 size = roundup(sizeof(uint32_t) * 6, ARCH_DMA_MINALIGN);
472 flush_dcache_range((unsigned long)desc,
473 (unsigned long)desc + size);
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600474
Lukas Aueraed8eac2018-01-25 14:11:17 +0100475 ret = run_descriptor_jr_idx(desc, sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530476
Lukas Aueraed8eac2018-01-25 14:11:17 +0100477 if (ret)
478 printf("RNG: Instantiation failed with error 0x%x\n",
479 ret);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530480
Lukas Aueraed8eac2018-01-25 14:11:17 +0100481 rdsta_val = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK;
482 if (!(rdsta_val & (1 << sh_idx))) {
483 free(desc);
484 return -1;
485 }
486
487 memset(desc, 0, sizeof(uint32_t) * 6);
488 }
489
490 free(desc);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530491
492 return ret;
493}
494
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300495static u8 get_rng_vid(uint8_t sec_idx)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530496{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300497 ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530498 u32 cha_vid = sec_in32(&sec->chavid_ls);
499
500 return (cha_vid & SEC_CHAVID_RNG_LS_MASK) >> SEC_CHAVID_LS_RNG_SHIFT;
501}
502
503/*
504 * By default, the TRNG runs for 200 clocks per sample;
505 * 1200 clocks per sample generates better entropy.
506 */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300507static void kick_trng(int ent_delay, uint8_t sec_idx)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530508{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300509 ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530510 struct rng4tst __iomem *rng =
511 (struct rng4tst __iomem *)&sec->rng;
512 u32 val;
513
514 /* put RNG4 into program mode */
515 sec_setbits32(&rng->rtmctl, RTMCTL_PRGM);
516 /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the
517 * length (in system clocks) of each Entropy sample taken
518 * */
519 val = sec_in32(&rng->rtsdctl);
520 val = (val & ~RTSDCTL_ENT_DLY_MASK) |
521 (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
522 sec_out32(&rng->rtsdctl, val);
523 /* min. freq. count, equal to 1/4 of the entropy sample length */
524 sec_out32(&rng->rtfreqmin, ent_delay >> 2);
Alex Porosanuf8d6a7f2015-05-05 16:48:33 +0300525 /* disable maximum frequency count */
526 sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE);
Alex Porosanubefb5cb2015-05-05 16:48:35 +0300527 /*
528 * select raw sampling in both entropy shifter
529 * and statistical checker
530 */
Aneesh Bansal1fa9c902015-12-08 13:54:30 +0530531 sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530532 /* put RNG4 into run mode */
Aneesh Bansal1fa9c902015-12-08 13:54:30 +0530533 sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530534}
535
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300536static int rng_init(uint8_t sec_idx)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530537{
538 int ret, ent_delay = RTSDCTL_ENT_DLY_MIN;
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300539 ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530540 struct rng4tst __iomem *rng =
541 (struct rng4tst __iomem *)&sec->rng;
Lukas Aueraed8eac2018-01-25 14:11:17 +0100542 u32 inst_handles;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530543
544 do {
Lukas Aueraed8eac2018-01-25 14:11:17 +0100545 inst_handles = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK;
546
Ruchika Gupta4345a572014-10-07 15:46:20 +0530547 /*
548 * If either of the SH's were instantiated by somebody else
549 * then it is assumed that the entropy
550 * parameters are properly set and thus the function
551 * setting these (kick_trng(...)) is skipped.
552 * Also, if a handle was instantiated, do not change
553 * the TRNG parameters.
554 */
Lukas Aueraed8eac2018-01-25 14:11:17 +0100555 if (!inst_handles) {
556 kick_trng(ent_delay, sec_idx);
557 ent_delay += 400;
558 }
Ruchika Gupta4345a572014-10-07 15:46:20 +0530559 /*
560 * if instantiate_rng(...) fails, the loop will rerun
561 * and the kick_trng(...) function will modfiy the
562 * upper and lower limits of the entropy sampling
563 * interval, leading to a sucessful initialization of
564 * the RNG.
565 */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300566 ret = instantiate_rng(sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530567 } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
568 if (ret) {
569 printf("RNG: Failed to instantiate RNG\n");
570 return ret;
571 }
572
573 /* Enable RDB bit so that RNG works faster */
574 sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE);
575
576 return ret;
577}
Ruchika Gupta0009c8f2017-04-17 18:07:19 +0530578#endif
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300579int sec_init_idx(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530580{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300581 ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530582 uint32_t mcr = sec_in32(&sec->mcfgr);
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300583 int ret = 0;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530584
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530585#ifdef CONFIG_FSL_CORENET
586 uint32_t liodnr;
587 uint32_t liodn_ns;
588 uint32_t liodn_s;
589#endif
590
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300591 if (!(sec_idx < CONFIG_SYS_FSL_MAX_NUM_OF_SEC)) {
592 printf("SEC initialization failed\n");
593 return -1;
594 }
595
Saksham Jain0c19cea2016-03-23 16:24:42 +0530596 /*
597 * Modifying CAAM Read/Write Attributes
York Suncbe8e1c2016-04-04 11:41:26 -0700598 * For LS2080A
Saksham Jain0c19cea2016-03-23 16:24:42 +0530599 * For AXI Write - Cacheable, Write Back, Write allocate
600 * For AXI Read - Cacheable, Read allocate
York Suncbe8e1c2016-04-04 11:41:26 -0700601 * Only For LS2080a, to solve CAAM coherency issues
Saksham Jain0c19cea2016-03-23 16:24:42 +0530602 */
York Sun4ce6fbf2017-03-27 11:41:01 -0700603#ifdef CONFIG_ARCH_LS2080A
Saksham Jain0c19cea2016-03-23 16:24:42 +0530604 mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0xb << MCFGR_AWCACHE_SHIFT);
605 mcr = (mcr & ~MCFGR_ARCACHE_MASK) | (0x6 << MCFGR_ARCACHE_SHIFT);
606#else
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300607 mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0x2 << MCFGR_AWCACHE_SHIFT);
Saksham Jain0c19cea2016-03-23 16:24:42 +0530608#endif
609
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300610#ifdef CONFIG_PHYS_64BIT
611 mcr |= (1 << MCFGR_PS_SHIFT);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530612#endif
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300613 sec_out32(&sec->mcfgr, mcr);
614
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530615#ifdef CONFIG_FSL_CORENET
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400616#ifdef CONFIG_SPL_BUILD
617 /*
618 * For SPL Build, Set the Liodns in SEC JR0 for
619 * creating PAMU entries corresponding to these.
620 * For normal build, these are set in set_liodns().
621 */
622 liodn_ns = CONFIG_SPL_JR0_LIODN_NS & JRNSLIODN_MASK;
623 liodn_s = CONFIG_SPL_JR0_LIODN_S & JRSLIODN_MASK;
624
625 liodnr = sec_in32(&sec->jrliodnr[0].ls) &
626 ~(JRNSLIODN_MASK | JRSLIODN_MASK);
627 liodnr = liodnr |
628 (liodn_ns << JRNSLIODN_SHIFT) |
629 (liodn_s << JRSLIODN_SHIFT);
630 sec_out32(&sec->jrliodnr[0].ls, liodnr);
631#else
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530632 liodnr = sec_in32(&sec->jrliodnr[0].ls);
633 liodn_ns = (liodnr & JRNSLIODN_MASK) >> JRNSLIODN_SHIFT;
634 liodn_s = (liodnr & JRSLIODN_MASK) >> JRSLIODN_SHIFT;
635#endif
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400636#endif
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530637
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300638 ret = jr_init(sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530639 if (ret < 0) {
640 printf("SEC initialization failed\n");
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530641 return -1;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530642 }
643
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530644#ifdef CONFIG_FSL_CORENET
645 ret = sec_config_pamu_table(liodn_ns, liodn_s);
646 if (ret < 0)
647 return -1;
648
649 pamu_enable();
650#endif
Ruchika Gupta0009c8f2017-04-17 18:07:19 +0530651#ifndef CONFIG_SPL_BUILD
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300652 if (get_rng_vid(sec_idx) >= 4) {
653 if (rng_init(sec_idx) < 0) {
654 printf("SEC%u: RNG instantiation failed\n", sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530655 return -1;
656 }
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300657 printf("SEC%u: RNG instantiated\n", sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530658 }
Ruchika Gupta0009c8f2017-04-17 18:07:19 +0530659#endif
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530660 return ret;
661}
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300662
663int sec_init(void)
664{
665 return sec_init_idx(0);
666}