blob: 85a3dac79603763ec681c5eae0f43f15be99399e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Guptab9eebfa2014-10-15 11:35:30 +05302/*
3 * Copyright 2008-2014 Freescale Semiconductor, Inc.
Gaurav Jain4556cf82022-03-24 11:50:25 +05304 * Copyright 2018, 2021 NXP
Ruchika Guptab9eebfa2014-10-15 11:35:30 +05305 *
Ruchika Guptab9eebfa2014-10-15 11:35:30 +05306 * Based on CAAM driver in drivers/crypto/caam in Linux
7 */
8
9#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070010#include <cpu_func.h>
Michael Walleb980f9e2020-06-27 22:58:52 +020011#include <linux/kernel.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053013#include <malloc.h>
Gaurav Jaincb5d0412022-03-24 11:50:33 +053014#include <power-domain.h>
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053015#include "jr.h"
Ruchika Guptac5de15c2014-10-07 15:46:20 +053016#include "jobdesc.h"
Aneesh Bansalf59e69c2015-10-29 22:58:03 +053017#include "desc_constr.h"
Simon Glass6887c5b2019-11-14 12:57:26 -070018#include <time.h>
Simon Glass90526e92020-05-10 11:39:56 -060019#include <asm/cache.h>
Aneesh Bansalf698e9f2016-01-22 17:05:59 +053020#ifdef CONFIG_FSL_CORENET
Simon Glass90526e92020-05-10 11:39:56 -060021#include <asm/cache.h>
Aneesh Bansalf698e9f2016-01-22 17:05:59 +053022#include <asm/fsl_pamu.h>
23#endif
Gaurav Jain4556cf82022-03-24 11:50:25 +053024#include <dm.h>
Michael Walleea95f212020-06-27 22:58:53 +020025#include <dm/lists.h>
Gaurav Jain4556cf82022-03-24 11:50:25 +053026#include <dm/root.h>
27#include <dm/device-internal.h>
Franck LENORMAND68a905d2021-03-25 17:30:22 +080028#include <linux/delay.h>
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053029
30#define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1))
31#define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size))
32
Alex Porosanu76394c92016-04-29 15:18:00 +030033uint32_t sec_offset[CONFIG_SYS_FSL_MAX_NUM_OF_SEC] = {
34 0,
York Sun4fd64742016-11-15 18:44:22 -080035#if defined(CONFIG_ARCH_C29X)
Alex Porosanu76394c92016-04-29 15:18:00 +030036 CONFIG_SYS_FSL_SEC_IDX_OFFSET,
37 2 * CONFIG_SYS_FSL_SEC_IDX_OFFSET
38#endif
39};
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053040
Gaurav Jain4556cf82022-03-24 11:50:25 +053041#if CONFIG_IS_ENABLED(DM)
42struct udevice *caam_dev;
43#else
Alex Porosanu76394c92016-04-29 15:18:00 +030044#define SEC_ADDR(idx) \
Aymen Sghaierdde92e22021-03-25 17:30:26 +080045 (ulong)((CONFIG_SYS_FSL_SEC_ADDR + sec_offset[idx]))
Alex Porosanu76394c92016-04-29 15:18:00 +030046
47#define SEC_JR0_ADDR(idx) \
Aymen Sghaierdde92e22021-03-25 17:30:26 +080048 (ulong)(SEC_ADDR(idx) + \
Alex Porosanu76394c92016-04-29 15:18:00 +030049 (CONFIG_SYS_FSL_JR0_OFFSET - CONFIG_SYS_FSL_SEC_OFFSET))
Gaurav Jain4556cf82022-03-24 11:50:25 +053050struct caam_regs caam_st;
51#endif
Alex Porosanu76394c92016-04-29 15:18:00 +030052
Gaurav Jain4556cf82022-03-24 11:50:25 +053053static inline u32 jr_start_reg(u8 jrid)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053054{
Gaurav Jain4556cf82022-03-24 11:50:25 +053055 return (1 << jrid);
56}
57
58static inline void start_jr(struct caam_regs *caam)
59{
60 ccsr_sec_t *sec = caam->sec;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053061 u32 ctpr_ms = sec_in32(&sec->ctpr_ms);
62 u32 scfgr = sec_in32(&sec->scfgr);
Gaurav Jain4556cf82022-03-24 11:50:25 +053063 u32 jrstart = jr_start_reg(caam->jrid);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053064
65 if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) {
66 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
67 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1
68 */
69 if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) ||
xypron.glpk@gmx.ded1710562017-04-15 16:37:54 +020070 (scfgr & SEC_SCFGR_VIRT_EN))
Gaurav Jain4556cf82022-03-24 11:50:25 +053071 sec_out32(&sec->jrstartr, jrstart);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053072 } else {
73 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
74 if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR)
Gaurav Jain4556cf82022-03-24 11:50:25 +053075 sec_out32(&sec->jrstartr, jrstart);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053076 }
77}
78
Gaurav Jain4556cf82022-03-24 11:50:25 +053079static inline void jr_disable_irq(struct jr_regs *regs)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053080{
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053081 uint32_t jrcfg = sec_in32(&regs->jrcfg1);
82
83 jrcfg = jrcfg | JR_INTMASK;
84
85 sec_out32(&regs->jrcfg1, jrcfg);
86}
87
Gaurav Jain4556cf82022-03-24 11:50:25 +053088static void jr_initregs(uint8_t sec_idx, struct caam_regs *caam)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053089{
Gaurav Jain4556cf82022-03-24 11:50:25 +053090 struct jr_regs *regs = caam->regs;
91 struct jobring *jr = &caam->jr[sec_idx];
Ye Li2ff17d22021-03-25 17:30:36 +080092 caam_dma_addr_t ip_base = virt_to_phys((void *)jr->input_ring);
93 caam_dma_addr_t op_base = virt_to_phys((void *)jr->output_ring);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053094
Ye Li2ff17d22021-03-25 17:30:36 +080095#ifdef CONFIG_CAAM_64BIT
Ruchika Guptab9eebfa2014-10-15 11:35:30 +053096 sec_out32(&regs->irba_h, ip_base >> 32);
97#else
98 sec_out32(&regs->irba_h, 0x0);
99#endif
100 sec_out32(&regs->irba_l, (uint32_t)ip_base);
Ye Li2ff17d22021-03-25 17:30:36 +0800101#ifdef CONFIG_CAAM_64BIT
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530102 sec_out32(&regs->orba_h, op_base >> 32);
103#else
104 sec_out32(&regs->orba_h, 0x0);
105#endif
106 sec_out32(&regs->orba_l, (uint32_t)op_base);
107 sec_out32(&regs->ors, JR_SIZE);
108 sec_out32(&regs->irs, JR_SIZE);
109
Alex Porosanu76394c92016-04-29 15:18:00 +0300110 if (!jr->irq)
Gaurav Jain4556cf82022-03-24 11:50:25 +0530111 jr_disable_irq(regs);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530112}
113
Gaurav Jain4556cf82022-03-24 11:50:25 +0530114static int jr_init(uint8_t sec_idx, struct caam_regs *caam)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530115{
Gaurav Jain4556cf82022-03-24 11:50:25 +0530116 struct jobring *jr = &caam->jr[sec_idx];
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530117#if CONFIG_IS_ENABLED(OF_CONTROL)
118 ofnode scu_node = ofnode_by_compatible(ofnode_null(), "fsl,imx8-mu");
119#endif
Alex Porosanu76394c92016-04-29 15:18:00 +0300120 memset(jr, 0, sizeof(struct jobring));
121
Gaurav Jain4556cf82022-03-24 11:50:25 +0530122 jr->jq_id = caam->jrid;
Alex Porosanu76394c92016-04-29 15:18:00 +0300123 jr->irq = DEFAULT_IRQ;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530124
125#ifdef CONFIG_FSL_CORENET
Alex Porosanu76394c92016-04-29 15:18:00 +0300126 jr->liodn = DEFAULT_JR_LIODN;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530127#endif
Alex Porosanu76394c92016-04-29 15:18:00 +0300128 jr->size = JR_SIZE;
Ye Li2ff17d22021-03-25 17:30:36 +0800129 jr->input_ring = (caam_dma_addr_t *)memalign(ARCH_DMA_MINALIGN,
130 JR_SIZE * sizeof(caam_dma_addr_t));
Alex Porosanu76394c92016-04-29 15:18:00 +0300131 if (!jr->input_ring)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530132 return -1;
Ruchika Gupta7f4736b2016-01-22 16:12:55 +0530133
Alex Porosanu76394c92016-04-29 15:18:00 +0300134 jr->op_size = roundup(JR_SIZE * sizeof(struct op_ring),
135 ARCH_DMA_MINALIGN);
136 jr->output_ring =
137 (struct op_ring *)memalign(ARCH_DMA_MINALIGN, jr->op_size);
138 if (!jr->output_ring)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530139 return -1;
140
Ye Li2ff17d22021-03-25 17:30:36 +0800141 memset(jr->input_ring, 0, JR_SIZE * sizeof(caam_dma_addr_t));
Alex Porosanu76394c92016-04-29 15:18:00 +0300142 memset(jr->output_ring, 0, jr->op_size);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530143
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530144#if CONFIG_IS_ENABLED(OF_CONTROL)
145 if (!ofnode_valid(scu_node))
146#endif
Gaurav Jain4556cf82022-03-24 11:50:25 +0530147 start_jr(caam);
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530148
Gaurav Jain4556cf82022-03-24 11:50:25 +0530149 jr_initregs(sec_idx, caam);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530150
151 return 0;
152}
153
154/* -1 --- error, can't enqueue -- no space available */
155static int jr_enqueue(uint32_t *desc_addr,
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530156 void (*callback)(uint32_t status, void *arg),
Gaurav Jain4556cf82022-03-24 11:50:25 +0530157 void *arg, uint8_t sec_idx, struct caam_regs *caam)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530158{
Gaurav Jain4556cf82022-03-24 11:50:25 +0530159 struct jr_regs *regs = caam->regs;
160 struct jobring *jr = &caam->jr[sec_idx];
Alex Porosanu76394c92016-04-29 15:18:00 +0300161 int head = jr->head;
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530162 uint32_t desc_word;
163 int length = desc_len(desc_addr);
164 int i;
Ye Li2ff17d22021-03-25 17:30:36 +0800165#ifdef CONFIG_CAAM_64BIT
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530166 uint32_t *addr_hi, *addr_lo;
167#endif
168
169 /* The descriptor must be submitted to SEC block as per endianness
170 * of the SEC Block.
171 * So, if the endianness of Core and SEC block is different, each word
172 * of the descriptor will be byte-swapped.
173 */
174 for (i = 0; i < length; i++) {
175 desc_word = desc_addr[i];
176 sec_out32((uint32_t *)&desc_addr[i], desc_word);
177 }
178
Ye Li2ff17d22021-03-25 17:30:36 +0800179 caam_dma_addr_t desc_phys_addr = virt_to_phys(desc_addr);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530180
Alex Porosanu76394c92016-04-29 15:18:00 +0300181 jr->info[head].desc_phys_addr = desc_phys_addr;
182 jr->info[head].callback = (void *)callback;
183 jr->info[head].arg = arg;
184 jr->info[head].op_done = 0;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530185
Alex Porosanu76394c92016-04-29 15:18:00 +0300186 unsigned long start = (unsigned long)&jr->info[head] &
Raul Cardenas02000202015-02-27 11:22:06 -0600187 ~(ARCH_DMA_MINALIGN - 1);
Alex Porosanu76394c92016-04-29 15:18:00 +0300188 unsigned long end = ALIGN((unsigned long)&jr->info[head] +
Ruchika Gupta7f4736b2016-01-22 16:12:55 +0530189 sizeof(struct jr_info), ARCH_DMA_MINALIGN);
Raul Cardenas02000202015-02-27 11:22:06 -0600190 flush_dcache_range(start, end);
191
Ye Li2ff17d22021-03-25 17:30:36 +0800192#ifdef CONFIG_CAAM_64BIT
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530193 /* Write the 64 bit Descriptor address on Input Ring.
194 * The 32 bit hign and low part of the address will
195 * depend on endianness of SEC block.
196 */
197#ifdef CONFIG_SYS_FSL_SEC_LE
Alex Porosanu76394c92016-04-29 15:18:00 +0300198 addr_lo = (uint32_t *)(&jr->input_ring[head]);
199 addr_hi = (uint32_t *)(&jr->input_ring[head]) + 1;
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530200#elif defined(CONFIG_SYS_FSL_SEC_BE)
Alex Porosanu76394c92016-04-29 15:18:00 +0300201 addr_hi = (uint32_t *)(&jr->input_ring[head]);
202 addr_lo = (uint32_t *)(&jr->input_ring[head]) + 1;
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530203#endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
204
205 sec_out32(addr_hi, (uint32_t)(desc_phys_addr >> 32));
206 sec_out32(addr_lo, (uint32_t)(desc_phys_addr));
207
208#else
209 /* Write the 32 bit Descriptor address on Input Ring. */
Alex Porosanu76394c92016-04-29 15:18:00 +0300210 sec_out32(&jr->input_ring[head], desc_phys_addr);
Ye Li2ff17d22021-03-25 17:30:36 +0800211#endif /* ifdef CONFIG_CAAM_64BIT */
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530212
Alex Porosanu76394c92016-04-29 15:18:00 +0300213 start = (unsigned long)&jr->input_ring[head] & ~(ARCH_DMA_MINALIGN - 1);
214 end = ALIGN((unsigned long)&jr->input_ring[head] +
Ye Li2ff17d22021-03-25 17:30:36 +0800215 sizeof(caam_dma_addr_t), ARCH_DMA_MINALIGN);
Raul Cardenas02000202015-02-27 11:22:06 -0600216 flush_dcache_range(start, end);
217
Alex Porosanu76394c92016-04-29 15:18:00 +0300218 jr->head = (head + 1) & (jr->size - 1);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530219
Ruchika Gupta7f4736b2016-01-22 16:12:55 +0530220 /* Invalidate output ring */
Alex Porosanu76394c92016-04-29 15:18:00 +0300221 start = (unsigned long)jr->output_ring &
Ruchika Gupta7f4736b2016-01-22 16:12:55 +0530222 ~(ARCH_DMA_MINALIGN - 1);
Alex Porosanu76394c92016-04-29 15:18:00 +0300223 end = ALIGN((unsigned long)jr->output_ring + jr->op_size,
224 ARCH_DMA_MINALIGN);
Ruchika Gupta7f4736b2016-01-22 16:12:55 +0530225 invalidate_dcache_range(start, end);
226
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530227 sec_out32(&regs->irja, 1);
228
229 return 0;
230}
231
Gaurav Jain4556cf82022-03-24 11:50:25 +0530232static int jr_dequeue(int sec_idx, struct caam_regs *caam)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530233{
Gaurav Jain4556cf82022-03-24 11:50:25 +0530234 struct jr_regs *regs = caam->regs;
235 struct jobring *jr = &caam->jr[sec_idx];
Alex Porosanu76394c92016-04-29 15:18:00 +0300236 int head = jr->head;
237 int tail = jr->tail;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530238 int idx, i, found;
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530239 void (*callback)(uint32_t status, void *arg);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530240 void *arg = NULL;
Ye Li2ff17d22021-03-25 17:30:36 +0800241#ifdef CONFIG_CAAM_64BIT
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530242 uint32_t *addr_hi, *addr_lo;
243#else
244 uint32_t *addr;
245#endif
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530246
Alex Porosanu76394c92016-04-29 15:18:00 +0300247 while (sec_in32(&regs->orsf) && CIRC_CNT(jr->head, jr->tail,
248 jr->size)) {
Raul Cardenas02000202015-02-27 11:22:06 -0600249
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530250 found = 0;
251
Ye Li2ff17d22021-03-25 17:30:36 +0800252 caam_dma_addr_t op_desc;
253 #ifdef CONFIG_CAAM_64BIT
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530254 /* Read the 64 bit Descriptor address from Output Ring.
255 * The 32 bit hign and low part of the address will
256 * depend on endianness of SEC block.
257 */
258 #ifdef CONFIG_SYS_FSL_SEC_LE
Alex Porosanu76394c92016-04-29 15:18:00 +0300259 addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc);
260 addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1;
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530261 #elif defined(CONFIG_SYS_FSL_SEC_BE)
Alex Porosanu76394c92016-04-29 15:18:00 +0300262 addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc);
263 addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1;
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530264 #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
265
266 op_desc = ((u64)sec_in32(addr_hi) << 32) |
267 ((u64)sec_in32(addr_lo));
268
269 #else
270 /* Read the 32 bit Descriptor address from Output Ring. */
Alex Porosanu76394c92016-04-29 15:18:00 +0300271 addr = (uint32_t *)&jr->output_ring[jr->tail].desc;
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530272 op_desc = sec_in32(addr);
Ye Li2ff17d22021-03-25 17:30:36 +0800273 #endif /* ifdef CONFIG_CAAM_64BIT */
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530274
Alex Porosanu76394c92016-04-29 15:18:00 +0300275 uint32_t status = sec_in32(&jr->output_ring[jr->tail].status);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530276
Alex Porosanu76394c92016-04-29 15:18:00 +0300277 for (i = 0; CIRC_CNT(head, tail + i, jr->size) >= 1; i++) {
278 idx = (tail + i) & (jr->size - 1);
279 if (op_desc == jr->info[idx].desc_phys_addr) {
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530280 found = 1;
281 break;
282 }
283 }
284
285 /* Error condition if match not found */
286 if (!found)
287 return -1;
288
Alex Porosanu76394c92016-04-29 15:18:00 +0300289 jr->info[idx].op_done = 1;
290 callback = (void *)jr->info[idx].callback;
291 arg = jr->info[idx].arg;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530292
293 /* When the job on tail idx gets done, increment
294 * tail till the point where job completed out of oredr has
295 * been taken into account
296 */
297 if (idx == tail)
298 do {
Alex Porosanu76394c92016-04-29 15:18:00 +0300299 tail = (tail + 1) & (jr->size - 1);
300 } while (jr->info[tail].op_done);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530301
Alex Porosanu76394c92016-04-29 15:18:00 +0300302 jr->tail = tail;
303 jr->read_idx = (jr->read_idx + 1) & (jr->size - 1);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530304
305 sec_out32(&regs->orjr, 1);
Alex Porosanu76394c92016-04-29 15:18:00 +0300306 jr->info[idx].op_done = 0;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530307
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530308 callback(status, arg);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530309 }
310
311 return 0;
312}
313
Aneesh Bansalf59e69c2015-10-29 22:58:03 +0530314static void desc_done(uint32_t status, void *arg)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530315{
316 struct result *x = arg;
317 x->status = status;
318 caam_jr_strstatus(status);
319 x->done = 1;
320}
321
Alex Porosanu76394c92016-04-29 15:18:00 +0300322static inline int run_descriptor_jr_idx(uint32_t *desc, uint8_t sec_idx)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530323{
Gaurav Jain4556cf82022-03-24 11:50:25 +0530324 struct caam_regs *caam;
325#if CONFIG_IS_ENABLED(DM)
326 caam = dev_get_priv(caam_dev);
327#else
328 caam = &caam_st;
329#endif
Franck LENORMAND68a905d2021-03-25 17:30:22 +0800330 unsigned long long timeval = 0;
331 unsigned long long timeout = CONFIG_USEC_DEQ_TIMEOUT;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530332 struct result op;
333 int ret = 0;
334
gaurav rana851c9db2014-12-04 13:00:41 +0530335 memset(&op, 0, sizeof(op));
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530336
Gaurav Jain4556cf82022-03-24 11:50:25 +0530337 ret = jr_enqueue(desc, desc_done, &op, sec_idx, caam);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530338 if (ret) {
339 debug("Error in SEC enq\n");
340 ret = JQ_ENQ_ERR;
341 goto out;
342 }
343
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530344 while (op.done != 1) {
Franck LENORMAND68a905d2021-03-25 17:30:22 +0800345 udelay(1);
346 timeval += 1;
347
Gaurav Jain4556cf82022-03-24 11:50:25 +0530348 ret = jr_dequeue(sec_idx, caam);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530349 if (ret) {
350 debug("Error in SEC deq\n");
351 ret = JQ_DEQ_ERR;
352 goto out;
353 }
354
Franck LENORMAND68a905d2021-03-25 17:30:22 +0800355 if (timeval > timeout) {
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530356 debug("SEC Dequeue timed out\n");
357 ret = JQ_DEQ_TO_ERR;
358 goto out;
359 }
360 }
361
Aneesh Bansal6178e952016-02-11 14:36:51 +0530362 if (op.status) {
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530363 debug("Error %x\n", op.status);
364 ret = op.status;
365 }
366out:
367 return ret;
368}
369
Alex Porosanu76394c92016-04-29 15:18:00 +0300370int run_descriptor_jr(uint32_t *desc)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530371{
Alex Porosanu76394c92016-04-29 15:18:00 +0300372 return run_descriptor_jr_idx(desc, 0);
373}
374
Gaurav Jain4556cf82022-03-24 11:50:25 +0530375static int jr_sw_cleanup(uint8_t sec_idx, struct caam_regs *caam)
376{
377 struct jobring *jr = &caam->jr[sec_idx];
378
379 jr->head = 0;
380 jr->tail = 0;
381 jr->read_idx = 0;
382 jr->write_idx = 0;
383 memset(jr->info, 0, sizeof(jr->info));
384 memset(jr->input_ring, 0, jr->size * sizeof(caam_dma_addr_t));
385 memset(jr->output_ring, 0, jr->size * sizeof(struct op_ring));
386
387 return 0;
388}
389
390static int jr_hw_reset(struct jr_regs *regs)
391{
392 uint32_t timeout = 100000;
393 uint32_t jrint, jrcr;
394
395 sec_out32(&regs->jrcr, JRCR_RESET);
396 do {
397 jrint = sec_in32(&regs->jrint);
398 } while (((jrint & JRINT_ERR_HALT_MASK) ==
399 JRINT_ERR_HALT_INPROGRESS) && --timeout);
400
401 jrint = sec_in32(&regs->jrint);
402 if (((jrint & JRINT_ERR_HALT_MASK) !=
403 JRINT_ERR_HALT_INPROGRESS) && timeout == 0)
404 return -1;
405
406 timeout = 100000;
407 sec_out32(&regs->jrcr, JRCR_RESET);
408 do {
409 jrcr = sec_in32(&regs->jrcr);
410 } while ((jrcr & JRCR_RESET) && --timeout);
411
412 if (timeout == 0)
413 return -1;
414
415 return 0;
416}
417
Alex Porosanu76394c92016-04-29 15:18:00 +0300418static inline int jr_reset_sec(uint8_t sec_idx)
419{
Gaurav Jain4556cf82022-03-24 11:50:25 +0530420 struct caam_regs *caam;
421#if CONFIG_IS_ENABLED(DM)
422 caam = dev_get_priv(caam_dev);
423#else
424 caam = &caam_st;
425#endif
426 if (jr_hw_reset(caam->regs) < 0)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530427 return -1;
428
429 /* Clean up the jobring structure maintained by software */
Gaurav Jain4556cf82022-03-24 11:50:25 +0530430 jr_sw_cleanup(sec_idx, caam);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530431
432 return 0;
433}
434
Alex Porosanu76394c92016-04-29 15:18:00 +0300435int jr_reset(void)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530436{
Alex Porosanu76394c92016-04-29 15:18:00 +0300437 return jr_reset_sec(0);
438}
439
Gaurav Jain4556cf82022-03-24 11:50:25 +0530440int sec_reset(void)
Alex Porosanu76394c92016-04-29 15:18:00 +0300441{
Gaurav Jain4556cf82022-03-24 11:50:25 +0530442 struct caam_regs *caam;
443#if CONFIG_IS_ENABLED(DM)
444 caam = dev_get_priv(caam_dev);
445#else
446 caam = &caam_st;
447#endif
448 ccsr_sec_t *sec = caam->sec;
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530449 uint32_t mcfgr = sec_in32(&sec->mcfgr);
450 uint32_t timeout = 100000;
451
452 mcfgr |= MCFGR_SWRST;
453 sec_out32(&sec->mcfgr, mcfgr);
454
455 mcfgr |= MCFGR_DMA_RST;
456 sec_out32(&sec->mcfgr, mcfgr);
457 do {
458 mcfgr = sec_in32(&sec->mcfgr);
459 } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout);
460
461 if (timeout == 0)
462 return -1;
463
464 timeout = 100000;
465 do {
466 mcfgr = sec_in32(&sec->mcfgr);
467 } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout);
468
469 if (timeout == 0)
470 return -1;
471
472 return 0;
473}
Gaurav Jain4556cf82022-03-24 11:50:25 +0530474
Michael Walleb980f9e2020-06-27 22:58:52 +0200475static int deinstantiate_rng(u8 sec_idx, int state_handle_mask)
476{
477 u32 *desc;
478 int sh_idx, ret = 0;
479 int desc_size = ALIGN(sizeof(u32) * 2, ARCH_DMA_MINALIGN);
480
481 desc = memalign(ARCH_DMA_MINALIGN, desc_size);
482 if (!desc) {
483 debug("cannot allocate RNG init descriptor memory\n");
484 return -ENOMEM;
485 }
486
487 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
488 /*
489 * If the corresponding bit is set, then it means the state
490 * handle was initialized by us, and thus it needs to be
491 * deinitialized as well
492 */
493
494 if (state_handle_mask & RDSTA_IF(sh_idx)) {
495 /*
496 * Create the descriptor for deinstantating this state
497 * handle.
498 */
499 inline_cnstr_jobdesc_rng_deinstantiation(desc, sh_idx);
500 flush_dcache_range((unsigned long)desc,
501 (unsigned long)desc + desc_size);
502
503 ret = run_descriptor_jr_idx(desc, sec_idx);
504 if (ret) {
505 printf("SEC%u: RNG4 SH%d deinstantiation failed with error 0x%x\n",
506 sec_idx, sh_idx, ret);
507 ret = -EIO;
508 break;
509 }
510
511 printf("SEC%u: Deinstantiated RNG4 SH%d\n",
512 sec_idx, sh_idx);
513 }
514 }
515
516 free(desc);
517 return ret;
518}
519
Gaurav Jain4556cf82022-03-24 11:50:25 +0530520static int instantiate_rng(uint8_t sec_idx, ccsr_sec_t *sec, int gen_sk)
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530521{
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530522 u32 *desc;
523 u32 rdsta_val;
Lukas Auerdfaec762018-01-25 14:11:17 +0100524 int ret = 0, sh_idx, size;
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530525 struct rng4tst __iomem *rng =
526 (struct rng4tst __iomem *)&sec->rng;
527
Raul Cardenas02000202015-02-27 11:22:06 -0600528 desc = memalign(ARCH_DMA_MINALIGN, sizeof(uint32_t) * 6);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530529 if (!desc) {
530 printf("cannot allocate RNG init descriptor memory\n");
531 return -1;
532 }
533
Lukas Auerdfaec762018-01-25 14:11:17 +0100534 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
535 /*
536 * If the corresponding bit is set, this state handle
537 * was initialized by somebody else, so it's left alone.
538 */
Michael Walleb980f9e2020-06-27 22:58:52 +0200539 rdsta_val = sec_in32(&rng->rdsta);
540 if (rdsta_val & (RDSTA_IF(sh_idx))) {
541 if (rdsta_val & RDSTA_PR(sh_idx))
542 continue;
543
544 printf("SEC%u: RNG4 SH%d was instantiated w/o prediction resistance. Tearing it down\n",
545 sec_idx, sh_idx);
546
547 ret = deinstantiate_rng(sec_idx, RDSTA_IF(sh_idx));
548 if (ret)
549 break;
550 }
Raul Cardenas02000202015-02-27 11:22:06 -0600551
Michael Wallec269a972020-06-27 22:58:51 +0200552 inline_cnstr_jobdesc_rng_instantiation(desc, sh_idx, gen_sk);
Lukas Auerdfaec762018-01-25 14:11:17 +0100553 size = roundup(sizeof(uint32_t) * 6, ARCH_DMA_MINALIGN);
554 flush_dcache_range((unsigned long)desc,
555 (unsigned long)desc + size);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530556
Lukas Auerdfaec762018-01-25 14:11:17 +0100557 ret = run_descriptor_jr_idx(desc, sec_idx);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530558
Lukas Auerdfaec762018-01-25 14:11:17 +0100559 if (ret)
Michael Walle9b86bf22020-06-27 22:58:48 +0200560 printf("SEC%u: RNG4 SH%d instantiation failed with error 0x%x\n",
561 sec_idx, sh_idx, ret);
Lukas Auerdfaec762018-01-25 14:11:17 +0100562
Michael Walleb980f9e2020-06-27 22:58:52 +0200563 rdsta_val = sec_in32(&rng->rdsta);
564 if (!(rdsta_val & RDSTA_IF(sh_idx))) {
Lukas Auerdfaec762018-01-25 14:11:17 +0100565 free(desc);
566 return -1;
567 }
568
569 memset(desc, 0, sizeof(uint32_t) * 6);
570 }
571
572 free(desc);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530573
574 return ret;
575}
576
Gaurav Jain4556cf82022-03-24 11:50:25 +0530577static u8 get_rng_vid(ccsr_sec_t *sec)
Alex Porosanu76394c92016-04-29 15:18:00 +0300578{
Michael Walle0dc59612020-06-27 22:58:50 +0200579 u8 vid;
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530580
Michael Walle0dc59612020-06-27 22:58:50 +0200581 if (caam_get_era() < 10) {
582 vid = (sec_in32(&sec->chavid_ls) & SEC_CHAVID_RNG_LS_MASK)
583 >> SEC_CHAVID_LS_RNG_SHIFT;
584 } else {
585 vid = (sec_in32(&sec->vreg.rng) & CHA_VER_VID_MASK)
586 >> CHA_VER_VID_SHIFT;
587 }
588
589 return vid;
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530590}
591
592/*
593 * By default, the TRNG runs for 200 clocks per sample;
594 * 1200 clocks per sample generates better entropy.
595 */
Gaurav Jain4556cf82022-03-24 11:50:25 +0530596static void kick_trng(int ent_delay, ccsr_sec_t *sec)
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530597{
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530598 struct rng4tst __iomem *rng =
599 (struct rng4tst __iomem *)&sec->rng;
600 u32 val;
601
602 /* put RNG4 into program mode */
603 sec_setbits32(&rng->rtmctl, RTMCTL_PRGM);
604 /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the
605 * length (in system clocks) of each Entropy sample taken
606 * */
607 val = sec_in32(&rng->rtsdctl);
608 val = (val & ~RTSDCTL_ENT_DLY_MASK) |
609 (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
610 sec_out32(&rng->rtsdctl, val);
611 /* min. freq. count, equal to 1/4 of the entropy sample length */
612 sec_out32(&rng->rtfreqmin, ent_delay >> 2);
Alex Porosanu026a3f12015-05-05 16:48:33 +0300613 /* disable maximum frequency count */
614 sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE);
Alex Porosanuc4065512015-05-05 16:48:35 +0300615 /*
616 * select raw sampling in both entropy shifter
617 * and statistical checker
618 */
Aneesh Bansal3a4800a2015-12-08 13:54:30 +0530619 sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530620 /* put RNG4 into run mode */
Aneesh Bansal3a4800a2015-12-08 13:54:30 +0530621 sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530622}
623
Gaurav Jain4556cf82022-03-24 11:50:25 +0530624static int rng_init(uint8_t sec_idx, ccsr_sec_t *sec)
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530625{
Gaurav Jain0c45c772022-04-15 16:40:49 +0530626 int ret, gen_sk, ent_delay = RTSDCTL_ENT_DLY;
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530627 struct rng4tst __iomem *rng =
628 (struct rng4tst __iomem *)&sec->rng;
Lukas Auerdfaec762018-01-25 14:11:17 +0100629 u32 inst_handles;
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530630
Michael Wallec269a972020-06-27 22:58:51 +0200631 gen_sk = !(sec_in32(&rng->rdsta) & RDSTA_SKVN);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530632 do {
Michael Walleb980f9e2020-06-27 22:58:52 +0200633 inst_handles = sec_in32(&rng->rdsta) & RDSTA_MASK;
Lukas Auerdfaec762018-01-25 14:11:17 +0100634
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530635 /*
636 * If either of the SH's were instantiated by somebody else
637 * then it is assumed that the entropy
638 * parameters are properly set and thus the function
639 * setting these (kick_trng(...)) is skipped.
640 * Also, if a handle was instantiated, do not change
641 * the TRNG parameters.
642 */
Lukas Auerdfaec762018-01-25 14:11:17 +0100643 if (!inst_handles) {
Gaurav Jain4556cf82022-03-24 11:50:25 +0530644 kick_trng(ent_delay, sec);
Lukas Auerdfaec762018-01-25 14:11:17 +0100645 ent_delay += 400;
646 }
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530647 /*
648 * if instantiate_rng(...) fails, the loop will rerun
649 * and the kick_trng(...) function will modfiy the
650 * upper and lower limits of the entropy sampling
651 * interval, leading to a sucessful initialization of
652 * the RNG.
653 */
Gaurav Jain4556cf82022-03-24 11:50:25 +0530654 ret = instantiate_rng(sec_idx, sec, gen_sk);
Gaurav Jain0c45c772022-04-15 16:40:49 +0530655 /*
656 * entropy delay is calculated via self-test method.
657 * self-test are run across different volatge, temp.
658 * if worst case value for ent_dly is identified,
659 * loop can be skipped for that platform.
660 */
661 if (IS_ENABLED(CONFIG_MX6SX))
662 break;
663
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530664 } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
665 if (ret) {
Michael Walle9b86bf22020-06-27 22:58:48 +0200666 printf("SEC%u: Failed to instantiate RNG\n", sec_idx);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530667 return ret;
668 }
669
670 /* Enable RDB bit so that RNG works faster */
671 sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE);
672
673 return ret;
674}
Gaurav Jain4556cf82022-03-24 11:50:25 +0530675
Alex Porosanu76394c92016-04-29 15:18:00 +0300676int sec_init_idx(uint8_t sec_idx)
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530677{
horia.geanta@freescale.com3ef24122015-07-08 17:24:57 +0300678 int ret = 0;
Gaurav Jain4556cf82022-03-24 11:50:25 +0530679 struct caam_regs *caam;
680#if CONFIG_IS_ENABLED(DM)
681 if (!caam_dev) {
682 printf("caam_jr: caam not found\n");
683 return -1;
684 }
685 caam = dev_get_priv(caam_dev);
686#else
687 caam_st.sec = (void *)SEC_ADDR(sec_idx);
688 caam_st.regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
689 caam_st.jrid = 0;
690 caam = &caam_st;
691#endif
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530692#if CONFIG_IS_ENABLED(OF_CONTROL)
693 ofnode scu_node = ofnode_by_compatible(ofnode_null(), "fsl,imx8-mu");
694
695 if (ofnode_valid(scu_node))
696 goto init;
697#endif
698
Gaurav Jain4556cf82022-03-24 11:50:25 +0530699 ccsr_sec_t *sec = caam->sec;
700 uint32_t mcr = sec_in32(&sec->mcfgr);
701#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_IMX8M)
702 uint32_t jrdid_ms = 0;
703#endif
Aneesh Bansalf698e9f2016-01-22 17:05:59 +0530704#ifdef CONFIG_FSL_CORENET
705 uint32_t liodnr;
706 uint32_t liodn_ns;
707 uint32_t liodn_s;
708#endif
709
Alex Porosanu76394c92016-04-29 15:18:00 +0300710 if (!(sec_idx < CONFIG_SYS_FSL_MAX_NUM_OF_SEC)) {
Michael Walle9b86bf22020-06-27 22:58:48 +0200711 printf("SEC%u: initialization failed\n", sec_idx);
Alex Porosanu76394c92016-04-29 15:18:00 +0300712 return -1;
713 }
714
Saksham Jain8a6f83d2016-03-23 16:24:42 +0530715 /*
716 * Modifying CAAM Read/Write Attributes
York Sun3c1d2182016-04-04 11:41:26 -0700717 * For LS2080A
Saksham Jain8a6f83d2016-03-23 16:24:42 +0530718 * For AXI Write - Cacheable, Write Back, Write allocate
719 * For AXI Read - Cacheable, Read allocate
York Sun3c1d2182016-04-04 11:41:26 -0700720 * Only For LS2080a, to solve CAAM coherency issues
Saksham Jain8a6f83d2016-03-23 16:24:42 +0530721 */
York Sun4a3ab192017-03-27 11:41:01 -0700722#ifdef CONFIG_ARCH_LS2080A
Saksham Jain8a6f83d2016-03-23 16:24:42 +0530723 mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0xb << MCFGR_AWCACHE_SHIFT);
724 mcr = (mcr & ~MCFGR_ARCACHE_MASK) | (0x6 << MCFGR_ARCACHE_SHIFT);
725#else
horia.geanta@freescale.com3ef24122015-07-08 17:24:57 +0300726 mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0x2 << MCFGR_AWCACHE_SHIFT);
Saksham Jain8a6f83d2016-03-23 16:24:42 +0530727#endif
728
Ye Li2ff17d22021-03-25 17:30:36 +0800729#ifdef CONFIG_CAAM_64BIT
horia.geanta@freescale.com3ef24122015-07-08 17:24:57 +0300730 mcr |= (1 << MCFGR_PS_SHIFT);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530731#endif
horia.geanta@freescale.com3ef24122015-07-08 17:24:57 +0300732 sec_out32(&sec->mcfgr, mcr);
Gaurav Jain4556cf82022-03-24 11:50:25 +0530733#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_IMX8M)
734 jrdid_ms = JRDID_MS_TZ_OWN | JRDID_MS_PRIM_TZ | JRDID_MS_PRIM_DID;
735 sec_out32(&sec->jrliodnr[caam->jrid].ms, jrdid_ms);
736#endif
737 jr_reset();
horia.geanta@freescale.com3ef24122015-07-08 17:24:57 +0300738
Aneesh Bansalf698e9f2016-01-22 17:05:59 +0530739#ifdef CONFIG_FSL_CORENET
Sumit Garg8f013972016-07-14 12:27:51 -0400740#ifdef CONFIG_SPL_BUILD
741 /*
742 * For SPL Build, Set the Liodns in SEC JR0 for
743 * creating PAMU entries corresponding to these.
744 * For normal build, these are set in set_liodns().
745 */
746 liodn_ns = CONFIG_SPL_JR0_LIODN_NS & JRNSLIODN_MASK;
747 liodn_s = CONFIG_SPL_JR0_LIODN_S & JRSLIODN_MASK;
748
Gaurav Jain4556cf82022-03-24 11:50:25 +0530749 liodnr = sec_in32(&sec->jrliodnr[caam->jrid].ls) &
Sumit Garg8f013972016-07-14 12:27:51 -0400750 ~(JRNSLIODN_MASK | JRSLIODN_MASK);
751 liodnr = liodnr |
752 (liodn_ns << JRNSLIODN_SHIFT) |
753 (liodn_s << JRSLIODN_SHIFT);
Gaurav Jain4556cf82022-03-24 11:50:25 +0530754 sec_out32(&sec->jrliodnr[caam->jrid].ls, liodnr);
Sumit Garg8f013972016-07-14 12:27:51 -0400755#else
Gaurav Jain4556cf82022-03-24 11:50:25 +0530756 liodnr = sec_in32(&sec->jrliodnr[caam->jrid].ls);
Aneesh Bansalf698e9f2016-01-22 17:05:59 +0530757 liodn_ns = (liodnr & JRNSLIODN_MASK) >> JRNSLIODN_SHIFT;
758 liodn_s = (liodnr & JRSLIODN_MASK) >> JRSLIODN_SHIFT;
759#endif
Sumit Garg8f013972016-07-14 12:27:51 -0400760#endif
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530761#if CONFIG_IS_ENABLED(OF_CONTROL)
762init:
763#endif
Gaurav Jain4556cf82022-03-24 11:50:25 +0530764 ret = jr_init(sec_idx, caam);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530765 if (ret < 0) {
Michael Walle9b86bf22020-06-27 22:58:48 +0200766 printf("SEC%u: initialization failed\n", sec_idx);
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530767 return -1;
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530768 }
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530769#if CONFIG_IS_ENABLED(OF_CONTROL)
770 if (ofnode_valid(scu_node))
771 return ret;
772#endif
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530773
Aneesh Bansalf698e9f2016-01-22 17:05:59 +0530774#ifdef CONFIG_FSL_CORENET
775 ret = sec_config_pamu_table(liodn_ns, liodn_s);
776 if (ret < 0)
777 return -1;
778
779 pamu_enable();
780#endif
Gaurav Jain4556cf82022-03-24 11:50:25 +0530781
782 if (get_rng_vid(caam->sec) >= 4) {
783 if (rng_init(sec_idx, caam->sec) < 0) {
Michael Walle9b86bf22020-06-27 22:58:48 +0200784 printf("SEC%u: RNG instantiation failed\n", sec_idx);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530785 return -1;
786 }
Michael Walleea95f212020-06-27 22:58:53 +0200787
788 if (IS_ENABLED(CONFIG_DM_RNG)) {
789 ret = device_bind_driver(NULL, "caam-rng", "caam-rng",
790 NULL);
791 if (ret)
792 printf("Couldn't bind rng driver (%d)\n", ret);
793 }
794
Michael Walle9b86bf22020-06-27 22:58:48 +0200795 printf("SEC%u: RNG instantiated\n", sec_idx);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530796 }
Ruchika Guptab9eebfa2014-10-15 11:35:30 +0530797 return ret;
798}
Alex Porosanu76394c92016-04-29 15:18:00 +0300799
800int sec_init(void)
801{
802 return sec_init_idx(0);
803}
Gaurav Jain4556cf82022-03-24 11:50:25 +0530804
805#if CONFIG_IS_ENABLED(DM)
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530806static int jr_power_on(ofnode node)
807{
808#if CONFIG_IS_ENABLED(POWER_DOMAIN)
809 struct udevice __maybe_unused jr_dev;
810 struct power_domain pd;
811
812 dev_set_ofnode(&jr_dev, node);
813
814 /* Power on Job Ring before access it */
815 if (!power_domain_get(&jr_dev, &pd)) {
816 if (power_domain_on(&pd))
817 return -EINVAL;
818 }
819#endif
820 return 0;
821}
822
Gaurav Jain4556cf82022-03-24 11:50:25 +0530823static int caam_jr_ioctl(struct udevice *dev, unsigned long request, void *buf)
824{
825 if (request != CAAM_JR_RUN_DESC)
826 return -ENOSYS;
827
828 return run_descriptor_jr(buf);
829}
830
831static int caam_jr_probe(struct udevice *dev)
832{
833 struct caam_regs *caam = dev_get_priv(dev);
834 fdt_addr_t addr;
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530835 ofnode node, scu_node;
Gaurav Jain4556cf82022-03-24 11:50:25 +0530836 unsigned int jr_node = 0;
837
838 caam_dev = dev;
839
840 addr = dev_read_addr(dev);
841 if (addr == FDT_ADDR_T_NONE) {
842 printf("caam_jr: crypto not found\n");
843 return -EINVAL;
844 }
845 caam->sec = (ccsr_sec_t *)(uintptr_t)addr;
846 caam->regs = (struct jr_regs *)caam->sec;
847
848 /* Check for enabled job ring node */
849 ofnode_for_each_subnode(node, dev_ofnode(dev)) {
850 if (!ofnode_is_available(node))
851 continue;
852
853 jr_node = ofnode_read_u32_default(node, "reg", -1);
854 if (jr_node > 0) {
855 caam->regs = (struct jr_regs *)((ulong)caam->sec + jr_node);
856 while (!(jr_node & 0x0F))
857 jr_node = jr_node >> 4;
858
859 caam->jrid = jr_node - 1;
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530860 scu_node = ofnode_by_compatible(ofnode_null(), "fsl,imx8-mu");
861 if (ofnode_valid(scu_node)) {
862 if (jr_power_on(node))
863 return -EINVAL;
864 }
Gaurav Jain4556cf82022-03-24 11:50:25 +0530865 break;
866 }
867 }
868
869 if (sec_init())
870 printf("\nsec_init failed!\n");
871
872 return 0;
873}
874
875static int caam_jr_bind(struct udevice *dev)
876{
877 return 0;
878}
879
880static const struct misc_ops caam_jr_ops = {
881 .ioctl = caam_jr_ioctl,
882};
883
884static const struct udevice_id caam_jr_match[] = {
885 { .compatible = "fsl,sec-v4.0" },
886 { }
887};
888
889U_BOOT_DRIVER(caam_jr) = {
890 .name = "caam_jr",
891 .id = UCLASS_MISC,
892 .of_match = caam_jr_match,
893 .ops = &caam_jr_ops,
894 .bind = caam_jr_bind,
895 .probe = caam_jr_probe,
896 .priv_auto = sizeof(struct caam_regs),
897};
898#endif