blob: 810398742523cf2cd5a822b50686d52a5adce444 [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.
Gaurav Jaine31dab82022-03-24 11:50:25 +05304 * Copyright 2018, 2021 NXP
Ruchika Guptaac1b2692014-10-15 11:35:30 +05305 *
Ruchika Guptaac1b2692014-10-15 11:35:30 +05306 * Based on CAAM driver in drivers/crypto/caam in Linux
7 */
8
9#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Michael Wallee692a002020-06-27 22:58:52 +020011#include <linux/kernel.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053013#include <malloc.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053014#include "jr.h"
Ruchika Gupta4345a572014-10-07 15:46:20 +053015#include "jobdesc.h"
Aneesh Bansal43421822015-10-29 22:58:03 +053016#include "desc_constr.h"
Simon Glass45c78902019-11-14 12:57:26 -070017#include <time.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <asm/cache.h>
Aneesh Bansal4b636c32016-01-22 17:05:59 +053019#ifdef CONFIG_FSL_CORENET
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Aneesh Bansal4b636c32016-01-22 17:05:59 +053021#include <asm/fsl_pamu.h>
22#endif
Gaurav Jaine31dab82022-03-24 11:50:25 +053023#include <dm.h>
Michael Walleb258eb22020-06-27 22:58:53 +020024#include <dm/lists.h>
Gaurav Jaine31dab82022-03-24 11:50:25 +053025#include <dm/root.h>
26#include <dm/device-internal.h>
Franck LENORMAND71812782021-03-25 17:30:22 +080027#include <linux/delay.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053028
29#define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1))
30#define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size))
31
Alex Porosanu7703d1e2016-04-29 15:18:00 +030032uint32_t sec_offset[CONFIG_SYS_FSL_MAX_NUM_OF_SEC] = {
33 0,
York Sun4119aee2016-11-15 18:44:22 -080034#if defined(CONFIG_ARCH_C29X)
Alex Porosanu7703d1e2016-04-29 15:18:00 +030035 CONFIG_SYS_FSL_SEC_IDX_OFFSET,
36 2 * CONFIG_SYS_FSL_SEC_IDX_OFFSET
37#endif
38};
39
Gaurav Jaine31dab82022-03-24 11:50:25 +053040#if CONFIG_IS_ENABLED(DM)
41struct udevice *caam_dev;
42#else
Alex Porosanu7703d1e2016-04-29 15:18:00 +030043#define SEC_ADDR(idx) \
Aymen Sghaier1536fd82021-03-25 17:30:26 +080044 (ulong)((CONFIG_SYS_FSL_SEC_ADDR + sec_offset[idx]))
Alex Porosanu7703d1e2016-04-29 15:18:00 +030045
46#define SEC_JR0_ADDR(idx) \
Aymen Sghaier1536fd82021-03-25 17:30:26 +080047 (ulong)(SEC_ADDR(idx) + \
Alex Porosanu7703d1e2016-04-29 15:18:00 +030048 (CONFIG_SYS_FSL_JR0_OFFSET - CONFIG_SYS_FSL_SEC_OFFSET))
Gaurav Jaine31dab82022-03-24 11:50:25 +053049struct caam_regs caam_st;
50#endif
Alex Porosanu7703d1e2016-04-29 15:18:00 +030051
Gaurav Jaine31dab82022-03-24 11:50:25 +053052static inline u32 jr_start_reg(u8 jrid)
53{
54 return (1 << jrid);
55}
Ruchika Guptaac1b2692014-10-15 11:35:30 +053056
Gaurav Jaine31dab82022-03-24 11:50:25 +053057static inline void start_jr(struct caam_regs *caam)
Ruchika Guptaac1b2692014-10-15 11:35:30 +053058{
Gaurav Jaine31dab82022-03-24 11:50:25 +053059 ccsr_sec_t *sec = caam->sec;
Ruchika Guptaac1b2692014-10-15 11:35:30 +053060 u32 ctpr_ms = sec_in32(&sec->ctpr_ms);
61 u32 scfgr = sec_in32(&sec->scfgr);
Gaurav Jaine31dab82022-03-24 11:50:25 +053062 u32 jrstart = jr_start_reg(caam->jrid);
Ruchika Guptaac1b2692014-10-15 11:35:30 +053063
64 if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) {
65 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
66 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1
67 */
68 if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) ||
xypron.glpk@gmx.de3ec01822017-04-15 16:37:54 +020069 (scfgr & SEC_SCFGR_VIRT_EN))
Gaurav Jaine31dab82022-03-24 11:50:25 +053070 sec_out32(&sec->jrstartr, jrstart);
Ruchika Guptaac1b2692014-10-15 11:35:30 +053071 } else {
72 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
73 if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR)
Gaurav Jaine31dab82022-03-24 11:50:25 +053074 sec_out32(&sec->jrstartr, jrstart);
Ruchika Guptaac1b2692014-10-15 11:35:30 +053075 }
76}
77
Gaurav Jaine31dab82022-03-24 11:50:25 +053078static inline void jr_disable_irq(struct jr_regs *regs)
Ruchika Guptaac1b2692014-10-15 11:35:30 +053079{
Ruchika Guptaac1b2692014-10-15 11:35:30 +053080 uint32_t jrcfg = sec_in32(&regs->jrcfg1);
81
82 jrcfg = jrcfg | JR_INTMASK;
83
84 sec_out32(&regs->jrcfg1, jrcfg);
85}
86
Gaurav Jaine31dab82022-03-24 11:50:25 +053087static void jr_initregs(uint8_t sec_idx, struct caam_regs *caam)
Ruchika Guptaac1b2692014-10-15 11:35:30 +053088{
Gaurav Jaine31dab82022-03-24 11:50:25 +053089 struct jr_regs *regs = caam->regs;
90 struct jobring *jr = &caam->jr[sec_idx];
Ye Li3c3e9a12021-03-25 17:30:36 +080091 caam_dma_addr_t ip_base = virt_to_phys((void *)jr->input_ring);
92 caam_dma_addr_t op_base = virt_to_phys((void *)jr->output_ring);
Ruchika Guptaac1b2692014-10-15 11:35:30 +053093
Ye Li3c3e9a12021-03-25 17:30:36 +080094#ifdef CONFIG_CAAM_64BIT
Ruchika Guptaac1b2692014-10-15 11:35:30 +053095 sec_out32(&regs->irba_h, ip_base >> 32);
96#else
97 sec_out32(&regs->irba_h, 0x0);
98#endif
99 sec_out32(&regs->irba_l, (uint32_t)ip_base);
Ye Li3c3e9a12021-03-25 17:30:36 +0800100#ifdef CONFIG_CAAM_64BIT
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530101 sec_out32(&regs->orba_h, op_base >> 32);
102#else
103 sec_out32(&regs->orba_h, 0x0);
104#endif
105 sec_out32(&regs->orba_l, (uint32_t)op_base);
106 sec_out32(&regs->ors, JR_SIZE);
107 sec_out32(&regs->irs, JR_SIZE);
108
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300109 if (!jr->irq)
Gaurav Jaine31dab82022-03-24 11:50:25 +0530110 jr_disable_irq(regs);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530111}
112
Gaurav Jaine31dab82022-03-24 11:50:25 +0530113static int jr_init(uint8_t sec_idx, struct caam_regs *caam)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530114{
Gaurav Jaine31dab82022-03-24 11:50:25 +0530115 struct jobring *jr = &caam->jr[sec_idx];
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530116
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300117 memset(jr, 0, sizeof(struct jobring));
118
Gaurav Jaine31dab82022-03-24 11:50:25 +0530119 jr->jq_id = caam->jrid;
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300120 jr->irq = DEFAULT_IRQ;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530121
122#ifdef CONFIG_FSL_CORENET
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300123 jr->liodn = DEFAULT_JR_LIODN;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530124#endif
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300125 jr->size = JR_SIZE;
Ye Li3c3e9a12021-03-25 17:30:36 +0800126 jr->input_ring = (caam_dma_addr_t *)memalign(ARCH_DMA_MINALIGN,
127 JR_SIZE * sizeof(caam_dma_addr_t));
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300128 if (!jr->input_ring)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530129 return -1;
Ruchika Guptad2180332016-01-22 16:12:55 +0530130
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300131 jr->op_size = roundup(JR_SIZE * sizeof(struct op_ring),
132 ARCH_DMA_MINALIGN);
133 jr->output_ring =
134 (struct op_ring *)memalign(ARCH_DMA_MINALIGN, jr->op_size);
135 if (!jr->output_ring)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530136 return -1;
137
Ye Li3c3e9a12021-03-25 17:30:36 +0800138 memset(jr->input_ring, 0, JR_SIZE * sizeof(caam_dma_addr_t));
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300139 memset(jr->output_ring, 0, jr->op_size);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530140
Gaurav Jaine31dab82022-03-24 11:50:25 +0530141 start_jr(caam);
142 jr_initregs(sec_idx, caam);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530143
144 return 0;
145}
146
147/* -1 --- error, can't enqueue -- no space available */
148static int jr_enqueue(uint32_t *desc_addr,
Aneesh Bansal43421822015-10-29 22:58:03 +0530149 void (*callback)(uint32_t status, void *arg),
Gaurav Jaine31dab82022-03-24 11:50:25 +0530150 void *arg, uint8_t sec_idx, struct caam_regs *caam)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530151{
Gaurav Jaine31dab82022-03-24 11:50:25 +0530152 struct jr_regs *regs = caam->regs;
153 struct jobring *jr = &caam->jr[sec_idx];
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300154 int head = jr->head;
Aneesh Bansal43421822015-10-29 22:58:03 +0530155 uint32_t desc_word;
156 int length = desc_len(desc_addr);
157 int i;
Ye Li3c3e9a12021-03-25 17:30:36 +0800158#ifdef CONFIG_CAAM_64BIT
Aneesh Bansal43421822015-10-29 22:58:03 +0530159 uint32_t *addr_hi, *addr_lo;
160#endif
161
162 /* The descriptor must be submitted to SEC block as per endianness
163 * of the SEC Block.
164 * So, if the endianness of Core and SEC block is different, each word
165 * of the descriptor will be byte-swapped.
166 */
167 for (i = 0; i < length; i++) {
168 desc_word = desc_addr[i];
169 sec_out32((uint32_t *)&desc_addr[i], desc_word);
170 }
171
Ye Li3c3e9a12021-03-25 17:30:36 +0800172 caam_dma_addr_t desc_phys_addr = virt_to_phys(desc_addr);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530173
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300174 jr->info[head].desc_phys_addr = desc_phys_addr;
175 jr->info[head].callback = (void *)callback;
176 jr->info[head].arg = arg;
177 jr->info[head].op_done = 0;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530178
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300179 unsigned long start = (unsigned long)&jr->info[head] &
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600180 ~(ARCH_DMA_MINALIGN - 1);
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300181 unsigned long end = ALIGN((unsigned long)&jr->info[head] +
Ruchika Guptad2180332016-01-22 16:12:55 +0530182 sizeof(struct jr_info), ARCH_DMA_MINALIGN);
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600183 flush_dcache_range(start, end);
184
Ye Li3c3e9a12021-03-25 17:30:36 +0800185#ifdef CONFIG_CAAM_64BIT
Aneesh Bansal43421822015-10-29 22:58:03 +0530186 /* Write the 64 bit Descriptor address on Input Ring.
187 * The 32 bit hign and low part of the address will
188 * depend on endianness of SEC block.
189 */
190#ifdef CONFIG_SYS_FSL_SEC_LE
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300191 addr_lo = (uint32_t *)(&jr->input_ring[head]);
192 addr_hi = (uint32_t *)(&jr->input_ring[head]) + 1;
Aneesh Bansal43421822015-10-29 22:58:03 +0530193#elif defined(CONFIG_SYS_FSL_SEC_BE)
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300194 addr_hi = (uint32_t *)(&jr->input_ring[head]);
195 addr_lo = (uint32_t *)(&jr->input_ring[head]) + 1;
Aneesh Bansal43421822015-10-29 22:58:03 +0530196#endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
197
198 sec_out32(addr_hi, (uint32_t)(desc_phys_addr >> 32));
199 sec_out32(addr_lo, (uint32_t)(desc_phys_addr));
200
201#else
202 /* Write the 32 bit Descriptor address on Input Ring. */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300203 sec_out32(&jr->input_ring[head], desc_phys_addr);
Ye Li3c3e9a12021-03-25 17:30:36 +0800204#endif /* ifdef CONFIG_CAAM_64BIT */
Aneesh Bansal43421822015-10-29 22:58:03 +0530205
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300206 start = (unsigned long)&jr->input_ring[head] & ~(ARCH_DMA_MINALIGN - 1);
207 end = ALIGN((unsigned long)&jr->input_ring[head] +
Ye Li3c3e9a12021-03-25 17:30:36 +0800208 sizeof(caam_dma_addr_t), ARCH_DMA_MINALIGN);
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600209 flush_dcache_range(start, end);
210
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300211 jr->head = (head + 1) & (jr->size - 1);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530212
Ruchika Guptad2180332016-01-22 16:12:55 +0530213 /* Invalidate output ring */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300214 start = (unsigned long)jr->output_ring &
Ruchika Guptad2180332016-01-22 16:12:55 +0530215 ~(ARCH_DMA_MINALIGN - 1);
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300216 end = ALIGN((unsigned long)jr->output_ring + jr->op_size,
217 ARCH_DMA_MINALIGN);
Ruchika Guptad2180332016-01-22 16:12:55 +0530218 invalidate_dcache_range(start, end);
219
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530220 sec_out32(&regs->irja, 1);
221
222 return 0;
223}
224
Gaurav Jaine31dab82022-03-24 11:50:25 +0530225static int jr_dequeue(int sec_idx, struct caam_regs *caam)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530226{
Gaurav Jaine31dab82022-03-24 11:50:25 +0530227 struct jr_regs *regs = caam->regs;
228 struct jobring *jr = &caam->jr[sec_idx];
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300229 int head = jr->head;
230 int tail = jr->tail;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530231 int idx, i, found;
Aneesh Bansal43421822015-10-29 22:58:03 +0530232 void (*callback)(uint32_t status, void *arg);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530233 void *arg = NULL;
Ye Li3c3e9a12021-03-25 17:30:36 +0800234#ifdef CONFIG_CAAM_64BIT
Aneesh Bansal43421822015-10-29 22:58:03 +0530235 uint32_t *addr_hi, *addr_lo;
236#else
237 uint32_t *addr;
238#endif
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530239
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300240 while (sec_in32(&regs->orsf) && CIRC_CNT(jr->head, jr->tail,
241 jr->size)) {
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600242
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530243 found = 0;
244
Ye Li3c3e9a12021-03-25 17:30:36 +0800245 caam_dma_addr_t op_desc;
246 #ifdef CONFIG_CAAM_64BIT
Aneesh Bansal43421822015-10-29 22:58:03 +0530247 /* Read the 64 bit Descriptor address from Output Ring.
248 * The 32 bit hign and low part of the address will
249 * depend on endianness of SEC block.
250 */
251 #ifdef CONFIG_SYS_FSL_SEC_LE
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300252 addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc);
253 addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1;
Aneesh Bansal43421822015-10-29 22:58:03 +0530254 #elif defined(CONFIG_SYS_FSL_SEC_BE)
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300255 addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc);
256 addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1;
Aneesh Bansal43421822015-10-29 22:58:03 +0530257 #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
258
259 op_desc = ((u64)sec_in32(addr_hi) << 32) |
260 ((u64)sec_in32(addr_lo));
261
262 #else
263 /* Read the 32 bit Descriptor address from Output Ring. */
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300264 addr = (uint32_t *)&jr->output_ring[jr->tail].desc;
Aneesh Bansal43421822015-10-29 22:58:03 +0530265 op_desc = sec_in32(addr);
Ye Li3c3e9a12021-03-25 17:30:36 +0800266 #endif /* ifdef CONFIG_CAAM_64BIT */
Aneesh Bansal43421822015-10-29 22:58:03 +0530267
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300268 uint32_t status = sec_in32(&jr->output_ring[jr->tail].status);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530269
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300270 for (i = 0; CIRC_CNT(head, tail + i, jr->size) >= 1; i++) {
271 idx = (tail + i) & (jr->size - 1);
272 if (op_desc == jr->info[idx].desc_phys_addr) {
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530273 found = 1;
274 break;
275 }
276 }
277
278 /* Error condition if match not found */
279 if (!found)
280 return -1;
281
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300282 jr->info[idx].op_done = 1;
283 callback = (void *)jr->info[idx].callback;
284 arg = jr->info[idx].arg;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530285
286 /* When the job on tail idx gets done, increment
287 * tail till the point where job completed out of oredr has
288 * been taken into account
289 */
290 if (idx == tail)
291 do {
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300292 tail = (tail + 1) & (jr->size - 1);
293 } while (jr->info[tail].op_done);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530294
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300295 jr->tail = tail;
296 jr->read_idx = (jr->read_idx + 1) & (jr->size - 1);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530297
298 sec_out32(&regs->orjr, 1);
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300299 jr->info[idx].op_done = 0;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530300
Aneesh Bansal43421822015-10-29 22:58:03 +0530301 callback(status, arg);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530302 }
303
304 return 0;
305}
306
Aneesh Bansal43421822015-10-29 22:58:03 +0530307static void desc_done(uint32_t status, void *arg)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530308{
309 struct result *x = arg;
310 x->status = status;
311 caam_jr_strstatus(status);
312 x->done = 1;
313}
314
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300315static inline int run_descriptor_jr_idx(uint32_t *desc, uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530316{
Gaurav Jaine31dab82022-03-24 11:50:25 +0530317 struct caam_regs *caam;
318#if CONFIG_IS_ENABLED(DM)
319 caam = dev_get_priv(caam_dev);
320#else
321 caam = &caam_st;
322#endif
Franck LENORMAND71812782021-03-25 17:30:22 +0800323 unsigned long long timeval = 0;
324 unsigned long long timeout = CONFIG_USEC_DEQ_TIMEOUT;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530325 struct result op;
326 int ret = 0;
327
gaurav rana07621502014-12-04 13:00:41 +0530328 memset(&op, 0, sizeof(op));
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530329
Gaurav Jaine31dab82022-03-24 11:50:25 +0530330 ret = jr_enqueue(desc, desc_done, &op, sec_idx, caam);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530331 if (ret) {
332 debug("Error in SEC enq\n");
333 ret = JQ_ENQ_ERR;
334 goto out;
335 }
336
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530337 while (op.done != 1) {
Franck LENORMAND71812782021-03-25 17:30:22 +0800338 udelay(1);
339 timeval += 1;
340
Gaurav Jaine31dab82022-03-24 11:50:25 +0530341 ret = jr_dequeue(sec_idx, caam);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530342 if (ret) {
343 debug("Error in SEC deq\n");
344 ret = JQ_DEQ_ERR;
345 goto out;
346 }
347
Franck LENORMAND71812782021-03-25 17:30:22 +0800348 if (timeval > timeout) {
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530349 debug("SEC Dequeue timed out\n");
350 ret = JQ_DEQ_TO_ERR;
351 goto out;
352 }
353 }
354
Aneesh Bansal3ab29d72016-02-11 14:36:51 +0530355 if (op.status) {
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530356 debug("Error %x\n", op.status);
357 ret = op.status;
358 }
359out:
360 return ret;
361}
362
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300363int run_descriptor_jr(uint32_t *desc)
364{
365 return run_descriptor_jr_idx(desc, 0);
366}
367
Gaurav Jaine31dab82022-03-24 11:50:25 +0530368static int jr_sw_cleanup(uint8_t sec_idx, struct caam_regs *caam)
369{
370 struct jobring *jr = &caam->jr[sec_idx];
371
372 jr->head = 0;
373 jr->tail = 0;
374 jr->read_idx = 0;
375 jr->write_idx = 0;
376 memset(jr->info, 0, sizeof(jr->info));
377 memset(jr->input_ring, 0, jr->size * sizeof(caam_dma_addr_t));
378 memset(jr->output_ring, 0, jr->size * sizeof(struct op_ring));
379
380 return 0;
381}
382
383static int jr_hw_reset(struct jr_regs *regs)
384{
385 uint32_t timeout = 100000;
386 uint32_t jrint, jrcr;
387
388 sec_out32(&regs->jrcr, JRCR_RESET);
389 do {
390 jrint = sec_in32(&regs->jrint);
391 } while (((jrint & JRINT_ERR_HALT_MASK) ==
392 JRINT_ERR_HALT_INPROGRESS) && --timeout);
393
394 jrint = sec_in32(&regs->jrint);
395 if (((jrint & JRINT_ERR_HALT_MASK) !=
396 JRINT_ERR_HALT_INPROGRESS) && timeout == 0)
397 return -1;
398
399 timeout = 100000;
400 sec_out32(&regs->jrcr, JRCR_RESET);
401 do {
402 jrcr = sec_in32(&regs->jrcr);
403 } while ((jrcr & JRCR_RESET) && --timeout);
404
405 if (timeout == 0)
406 return -1;
407
408 return 0;
409}
410
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300411static inline int jr_reset_sec(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530412{
Gaurav Jaine31dab82022-03-24 11:50:25 +0530413 struct caam_regs *caam;
414#if CONFIG_IS_ENABLED(DM)
415 caam = dev_get_priv(caam_dev);
416#else
417 caam = &caam_st;
418#endif
419 if (jr_hw_reset(caam->regs) < 0)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530420 return -1;
421
422 /* Clean up the jobring structure maintained by software */
Gaurav Jaine31dab82022-03-24 11:50:25 +0530423 jr_sw_cleanup(sec_idx, caam);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530424
425 return 0;
426}
427
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300428int jr_reset(void)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530429{
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300430 return jr_reset_sec(0);
431}
432
Gaurav Jaine31dab82022-03-24 11:50:25 +0530433int sec_reset(void)
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300434{
Gaurav Jaine31dab82022-03-24 11:50:25 +0530435 struct caam_regs *caam;
436#if CONFIG_IS_ENABLED(DM)
437 caam = dev_get_priv(caam_dev);
438#else
439 caam = &caam_st;
440#endif
441 ccsr_sec_t *sec = caam->sec;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530442 uint32_t mcfgr = sec_in32(&sec->mcfgr);
443 uint32_t timeout = 100000;
444
445 mcfgr |= MCFGR_SWRST;
446 sec_out32(&sec->mcfgr, mcfgr);
447
448 mcfgr |= MCFGR_DMA_RST;
449 sec_out32(&sec->mcfgr, mcfgr);
450 do {
451 mcfgr = sec_in32(&sec->mcfgr);
452 } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout);
453
454 if (timeout == 0)
455 return -1;
456
457 timeout = 100000;
458 do {
459 mcfgr = sec_in32(&sec->mcfgr);
460 } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout);
461
462 if (timeout == 0)
463 return -1;
464
465 return 0;
466}
Gaurav Jaine31dab82022-03-24 11:50:25 +0530467
Michael Wallee692a002020-06-27 22:58:52 +0200468static int deinstantiate_rng(u8 sec_idx, int state_handle_mask)
469{
470 u32 *desc;
471 int sh_idx, ret = 0;
472 int desc_size = ALIGN(sizeof(u32) * 2, ARCH_DMA_MINALIGN);
473
474 desc = memalign(ARCH_DMA_MINALIGN, desc_size);
475 if (!desc) {
476 debug("cannot allocate RNG init descriptor memory\n");
477 return -ENOMEM;
478 }
479
480 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
481 /*
482 * If the corresponding bit is set, then it means the state
483 * handle was initialized by us, and thus it needs to be
484 * deinitialized as well
485 */
486
487 if (state_handle_mask & RDSTA_IF(sh_idx)) {
488 /*
489 * Create the descriptor for deinstantating this state
490 * handle.
491 */
492 inline_cnstr_jobdesc_rng_deinstantiation(desc, sh_idx);
493 flush_dcache_range((unsigned long)desc,
494 (unsigned long)desc + desc_size);
495
496 ret = run_descriptor_jr_idx(desc, sec_idx);
497 if (ret) {
498 printf("SEC%u: RNG4 SH%d deinstantiation failed with error 0x%x\n",
499 sec_idx, sh_idx, ret);
500 ret = -EIO;
501 break;
502 }
503
504 printf("SEC%u: Deinstantiated RNG4 SH%d\n",
505 sec_idx, sh_idx);
506 }
507 }
508
509 free(desc);
510 return ret;
511}
512
Gaurav Jaine31dab82022-03-24 11:50:25 +0530513static int instantiate_rng(uint8_t sec_idx, ccsr_sec_t *sec, int gen_sk)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530514{
Ruchika Gupta4345a572014-10-07 15:46:20 +0530515 u32 *desc;
516 u32 rdsta_val;
Lukas Aueraed8eac2018-01-25 14:11:17 +0100517 int ret = 0, sh_idx, size;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530518 struct rng4tst __iomem *rng =
519 (struct rng4tst __iomem *)&sec->rng;
520
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600521 desc = memalign(ARCH_DMA_MINALIGN, sizeof(uint32_t) * 6);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530522 if (!desc) {
523 printf("cannot allocate RNG init descriptor memory\n");
524 return -1;
525 }
526
Lukas Aueraed8eac2018-01-25 14:11:17 +0100527 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
528 /*
529 * If the corresponding bit is set, this state handle
530 * was initialized by somebody else, so it's left alone.
531 */
Michael Wallee692a002020-06-27 22:58:52 +0200532 rdsta_val = sec_in32(&rng->rdsta);
533 if (rdsta_val & (RDSTA_IF(sh_idx))) {
534 if (rdsta_val & RDSTA_PR(sh_idx))
535 continue;
536
537 printf("SEC%u: RNG4 SH%d was instantiated w/o prediction resistance. Tearing it down\n",
538 sec_idx, sh_idx);
539
540 ret = deinstantiate_rng(sec_idx, RDSTA_IF(sh_idx));
541 if (ret)
542 break;
543 }
Lukas Aueraed8eac2018-01-25 14:11:17 +0100544
Michael Walle602cc8d2020-06-27 22:58:51 +0200545 inline_cnstr_jobdesc_rng_instantiation(desc, sh_idx, gen_sk);
Lukas Aueraed8eac2018-01-25 14:11:17 +0100546 size = roundup(sizeof(uint32_t) * 6, ARCH_DMA_MINALIGN);
547 flush_dcache_range((unsigned long)desc,
548 (unsigned long)desc + size);
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600549
Lukas Aueraed8eac2018-01-25 14:11:17 +0100550 ret = run_descriptor_jr_idx(desc, sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530551
Lukas Aueraed8eac2018-01-25 14:11:17 +0100552 if (ret)
Michael Walle73e3f572020-06-27 22:58:48 +0200553 printf("SEC%u: RNG4 SH%d instantiation failed with error 0x%x\n",
554 sec_idx, sh_idx, ret);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530555
Michael Wallee692a002020-06-27 22:58:52 +0200556 rdsta_val = sec_in32(&rng->rdsta);
557 if (!(rdsta_val & RDSTA_IF(sh_idx))) {
Lukas Aueraed8eac2018-01-25 14:11:17 +0100558 free(desc);
559 return -1;
560 }
561
562 memset(desc, 0, sizeof(uint32_t) * 6);
563 }
564
565 free(desc);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530566
567 return ret;
568}
569
Gaurav Jaine31dab82022-03-24 11:50:25 +0530570static u8 get_rng_vid(ccsr_sec_t *sec)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530571{
Michael Wallea83fa182020-06-27 22:58:50 +0200572 u8 vid;
573
574 if (caam_get_era() < 10) {
575 vid = (sec_in32(&sec->chavid_ls) & SEC_CHAVID_RNG_LS_MASK)
576 >> SEC_CHAVID_LS_RNG_SHIFT;
577 } else {
578 vid = (sec_in32(&sec->vreg.rng) & CHA_VER_VID_MASK)
579 >> CHA_VER_VID_SHIFT;
580 }
Ruchika Gupta4345a572014-10-07 15:46:20 +0530581
Michael Wallea83fa182020-06-27 22:58:50 +0200582 return vid;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530583}
584
585/*
586 * By default, the TRNG runs for 200 clocks per sample;
587 * 1200 clocks per sample generates better entropy.
588 */
Gaurav Jaine31dab82022-03-24 11:50:25 +0530589static void kick_trng(int ent_delay, ccsr_sec_t *sec)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530590{
Ruchika Gupta4345a572014-10-07 15:46:20 +0530591 struct rng4tst __iomem *rng =
592 (struct rng4tst __iomem *)&sec->rng;
593 u32 val;
594
595 /* put RNG4 into program mode */
596 sec_setbits32(&rng->rtmctl, RTMCTL_PRGM);
597 /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the
598 * length (in system clocks) of each Entropy sample taken
599 * */
600 val = sec_in32(&rng->rtsdctl);
601 val = (val & ~RTSDCTL_ENT_DLY_MASK) |
602 (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
603 sec_out32(&rng->rtsdctl, val);
604 /* min. freq. count, equal to 1/4 of the entropy sample length */
605 sec_out32(&rng->rtfreqmin, ent_delay >> 2);
Alex Porosanuf8d6a7f2015-05-05 16:48:33 +0300606 /* disable maximum frequency count */
607 sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE);
Alex Porosanubefb5cb2015-05-05 16:48:35 +0300608 /*
609 * select raw sampling in both entropy shifter
610 * and statistical checker
611 */
Aneesh Bansal1fa9c902015-12-08 13:54:30 +0530612 sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530613 /* put RNG4 into run mode */
Aneesh Bansal1fa9c902015-12-08 13:54:30 +0530614 sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530615}
616
Gaurav Jaine31dab82022-03-24 11:50:25 +0530617static int rng_init(uint8_t sec_idx, ccsr_sec_t *sec)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530618{
Michael Walle602cc8d2020-06-27 22:58:51 +0200619 int ret, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530620 struct rng4tst __iomem *rng =
621 (struct rng4tst __iomem *)&sec->rng;
Lukas Aueraed8eac2018-01-25 14:11:17 +0100622 u32 inst_handles;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530623
Michael Walle602cc8d2020-06-27 22:58:51 +0200624 gen_sk = !(sec_in32(&rng->rdsta) & RDSTA_SKVN);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530625 do {
Michael Wallee692a002020-06-27 22:58:52 +0200626 inst_handles = sec_in32(&rng->rdsta) & RDSTA_MASK;
Lukas Aueraed8eac2018-01-25 14:11:17 +0100627
Ruchika Gupta4345a572014-10-07 15:46:20 +0530628 /*
629 * If either of the SH's were instantiated by somebody else
630 * then it is assumed that the entropy
631 * parameters are properly set and thus the function
632 * setting these (kick_trng(...)) is skipped.
633 * Also, if a handle was instantiated, do not change
634 * the TRNG parameters.
635 */
Lukas Aueraed8eac2018-01-25 14:11:17 +0100636 if (!inst_handles) {
Gaurav Jaine31dab82022-03-24 11:50:25 +0530637 kick_trng(ent_delay, sec);
Lukas Aueraed8eac2018-01-25 14:11:17 +0100638 ent_delay += 400;
639 }
Ruchika Gupta4345a572014-10-07 15:46:20 +0530640 /*
641 * if instantiate_rng(...) fails, the loop will rerun
642 * and the kick_trng(...) function will modfiy the
643 * upper and lower limits of the entropy sampling
644 * interval, leading to a sucessful initialization of
645 * the RNG.
646 */
Gaurav Jaine31dab82022-03-24 11:50:25 +0530647 ret = instantiate_rng(sec_idx, sec, gen_sk);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530648 } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
649 if (ret) {
Michael Walle73e3f572020-06-27 22:58:48 +0200650 printf("SEC%u: Failed to instantiate RNG\n", sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530651 return ret;
652 }
653
654 /* Enable RDB bit so that RNG works faster */
655 sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE);
656
657 return ret;
658}
Gaurav Jaine31dab82022-03-24 11:50:25 +0530659
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300660int sec_init_idx(uint8_t sec_idx)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530661{
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300662 int ret = 0;
Gaurav Jaine31dab82022-03-24 11:50:25 +0530663 struct caam_regs *caam;
664#if CONFIG_IS_ENABLED(DM)
665 if (!caam_dev) {
666 printf("caam_jr: caam not found\n");
667 return -1;
668 }
669 caam = dev_get_priv(caam_dev);
670#else
671 caam_st.sec = (void *)SEC_ADDR(sec_idx);
672 caam_st.regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
673 caam_st.jrid = 0;
674 caam = &caam_st;
675#endif
676 ccsr_sec_t *sec = caam->sec;
677 uint32_t mcr = sec_in32(&sec->mcfgr);
678#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_IMX8M)
679 uint32_t jrdid_ms = 0;
680#endif
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530681#ifdef CONFIG_FSL_CORENET
682 uint32_t liodnr;
683 uint32_t liodn_ns;
684 uint32_t liodn_s;
685#endif
686
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300687 if (!(sec_idx < CONFIG_SYS_FSL_MAX_NUM_OF_SEC)) {
Michael Walle73e3f572020-06-27 22:58:48 +0200688 printf("SEC%u: initialization failed\n", sec_idx);
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300689 return -1;
690 }
691
Saksham Jain0c19cea2016-03-23 16:24:42 +0530692 /*
693 * Modifying CAAM Read/Write Attributes
York Suncbe8e1c2016-04-04 11:41:26 -0700694 * For LS2080A
Saksham Jain0c19cea2016-03-23 16:24:42 +0530695 * For AXI Write - Cacheable, Write Back, Write allocate
696 * For AXI Read - Cacheable, Read allocate
York Suncbe8e1c2016-04-04 11:41:26 -0700697 * Only For LS2080a, to solve CAAM coherency issues
Saksham Jain0c19cea2016-03-23 16:24:42 +0530698 */
York Sun4ce6fbf2017-03-27 11:41:01 -0700699#ifdef CONFIG_ARCH_LS2080A
Saksham Jain0c19cea2016-03-23 16:24:42 +0530700 mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0xb << MCFGR_AWCACHE_SHIFT);
701 mcr = (mcr & ~MCFGR_ARCACHE_MASK) | (0x6 << MCFGR_ARCACHE_SHIFT);
702#else
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300703 mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0x2 << MCFGR_AWCACHE_SHIFT);
Saksham Jain0c19cea2016-03-23 16:24:42 +0530704#endif
705
Ye Li3c3e9a12021-03-25 17:30:36 +0800706#ifdef CONFIG_CAAM_64BIT
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300707 mcr |= (1 << MCFGR_PS_SHIFT);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530708#endif
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300709 sec_out32(&sec->mcfgr, mcr);
Gaurav Jaine31dab82022-03-24 11:50:25 +0530710#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_IMX8M)
711 jrdid_ms = JRDID_MS_TZ_OWN | JRDID_MS_PRIM_TZ | JRDID_MS_PRIM_DID;
712 sec_out32(&sec->jrliodnr[caam->jrid].ms, jrdid_ms);
713#endif
714 jr_reset();
horia.geanta@freescale.com66e26aa2015-07-08 17:24:57 +0300715
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530716#ifdef CONFIG_FSL_CORENET
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400717#ifdef CONFIG_SPL_BUILD
718 /*
719 * For SPL Build, Set the Liodns in SEC JR0 for
720 * creating PAMU entries corresponding to these.
721 * For normal build, these are set in set_liodns().
722 */
723 liodn_ns = CONFIG_SPL_JR0_LIODN_NS & JRNSLIODN_MASK;
724 liodn_s = CONFIG_SPL_JR0_LIODN_S & JRSLIODN_MASK;
725
Gaurav Jaine31dab82022-03-24 11:50:25 +0530726 liodnr = sec_in32(&sec->jrliodnr[caam->jrid].ls) &
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400727 ~(JRNSLIODN_MASK | JRSLIODN_MASK);
728 liodnr = liodnr |
729 (liodn_ns << JRNSLIODN_SHIFT) |
730 (liodn_s << JRSLIODN_SHIFT);
Gaurav Jaine31dab82022-03-24 11:50:25 +0530731 sec_out32(&sec->jrliodnr[caam->jrid].ls, liodnr);
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400732#else
Gaurav Jaine31dab82022-03-24 11:50:25 +0530733 liodnr = sec_in32(&sec->jrliodnr[caam->jrid].ls);
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530734 liodn_ns = (liodnr & JRNSLIODN_MASK) >> JRNSLIODN_SHIFT;
735 liodn_s = (liodnr & JRSLIODN_MASK) >> JRSLIODN_SHIFT;
736#endif
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400737#endif
Gaurav Jaine31dab82022-03-24 11:50:25 +0530738 ret = jr_init(sec_idx, caam);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530739 if (ret < 0) {
Michael Walle73e3f572020-06-27 22:58:48 +0200740 printf("SEC%u: initialization failed\n", sec_idx);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530741 return -1;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530742 }
743
Aneesh Bansal4b636c32016-01-22 17:05:59 +0530744#ifdef CONFIG_FSL_CORENET
745 ret = sec_config_pamu_table(liodn_ns, liodn_s);
746 if (ret < 0)
747 return -1;
748
749 pamu_enable();
750#endif
Gaurav Jaine31dab82022-03-24 11:50:25 +0530751
752 if (get_rng_vid(caam->sec) >= 4) {
753 if (rng_init(sec_idx, caam->sec) < 0) {
Michael Walle73e3f572020-06-27 22:58:48 +0200754 printf("SEC%u: RNG instantiation failed\n", sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530755 return -1;
756 }
Michael Walleb258eb22020-06-27 22:58:53 +0200757
758 if (IS_ENABLED(CONFIG_DM_RNG)) {
759 ret = device_bind_driver(NULL, "caam-rng", "caam-rng",
760 NULL);
761 if (ret)
762 printf("Couldn't bind rng driver (%d)\n", ret);
763 }
764
Michael Walle73e3f572020-06-27 22:58:48 +0200765 printf("SEC%u: RNG instantiated\n", sec_idx);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530766 }
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530767 return ret;
768}
Alex Porosanu7703d1e2016-04-29 15:18:00 +0300769
770int sec_init(void)
771{
772 return sec_init_idx(0);
773}
Gaurav Jaine31dab82022-03-24 11:50:25 +0530774
775#if CONFIG_IS_ENABLED(DM)
776static int caam_jr_ioctl(struct udevice *dev, unsigned long request, void *buf)
777{
778 if (request != CAAM_JR_RUN_DESC)
779 return -ENOSYS;
780
781 return run_descriptor_jr(buf);
782}
783
784static int caam_jr_probe(struct udevice *dev)
785{
786 struct caam_regs *caam = dev_get_priv(dev);
787 fdt_addr_t addr;
788 ofnode node;
789 unsigned int jr_node = 0;
790
791 caam_dev = dev;
792
793 addr = dev_read_addr(dev);
794 if (addr == FDT_ADDR_T_NONE) {
795 printf("caam_jr: crypto not found\n");
796 return -EINVAL;
797 }
798 caam->sec = (ccsr_sec_t *)(uintptr_t)addr;
799 caam->regs = (struct jr_regs *)caam->sec;
800
801 /* Check for enabled job ring node */
802 ofnode_for_each_subnode(node, dev_ofnode(dev)) {
803 if (!ofnode_is_available(node))
804 continue;
805
806 jr_node = ofnode_read_u32_default(node, "reg", -1);
807 if (jr_node > 0) {
808 caam->regs = (struct jr_regs *)((ulong)caam->sec + jr_node);
809 while (!(jr_node & 0x0F))
810 jr_node = jr_node >> 4;
811
812 caam->jrid = jr_node - 1;
813 break;
814 }
815 }
816
817 if (sec_init())
818 printf("\nsec_init failed!\n");
819
820 return 0;
821}
822
823static int caam_jr_bind(struct udevice *dev)
824{
825 return 0;
826}
827
828static const struct misc_ops caam_jr_ops = {
829 .ioctl = caam_jr_ioctl,
830};
831
832static const struct udevice_id caam_jr_match[] = {
833 { .compatible = "fsl,sec-v4.0" },
834 { }
835};
836
837U_BOOT_DRIVER(caam_jr) = {
838 .name = "caam_jr",
839 .id = UCLASS_MISC,
840 .of_match = caam_jr_match,
841 .ops = &caam_jr_ops,
842 .bind = caam_jr_bind,
843 .probe = caam_jr_probe,
844 .priv_auto = sizeof(struct caam_regs),
845};
846#endif