Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / drivers / crypto / caam / ctrl.c
CommitLineData
618b5dc4 1// SPDX-License-Identifier: GPL-2.0+
fb4562b2 2/* * CAAM control-plane driver backend
8e8ec596
KP
3 * Controller-level driver, kernel property detection, initialization
4 *
281922a1 5 * Copyright 2008-2012 Freescale Semiconductor, Inc.
ae1dd17d 6 * Copyright 2018-2019, 2023 NXP
8e8ec596
KP
7 */
8
4776d381 9#include <linux/device.h>
5af50730
RH
10#include <linux/of_address.h>
11#include <linux/of_irq.h>
b0cc7491 12#include <linux/platform_device.h>
c056d910 13#include <linux/sys_soc.h>
358ba762 14#include <linux/fsl/mc.h>
5af50730 15
8e8ec596 16#include "compat.h"
abd98754 17#include "debugfs.h"
8e8ec596
KP
18#include "regs.h"
19#include "intern.h"
20#include "jr.h"
281922a1 21#include "desc_constr.h"
1ac6b731 22#include "ctrl.h"
8e8ec596 23
297b9ceb
HG
24bool caam_dpaa2;
25EXPORT_SYMBOL(caam_dpaa2);
261ea058 26
67c2315d
HG
27#ifdef CONFIG_CAAM_QI
28#include "qi.h"
29#endif
30
281922a1
KP
31/*
32 * Descriptor to instantiate RNG State Handle 0 in normal mode and
33 * load the JDKEK, TDKEK and TDSK registers
34 */
1005bccd 35static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
281922a1 36{
1005bccd 37 u32 *jump_cmd, op_flags;
281922a1
KP
38
39 init_job_desc(desc, 0);
40
1005bccd 41 op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
358ba762
AS
42 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT |
43 OP_ALG_PR_ON;
1005bccd 44
281922a1 45 /* INIT RNG in non-test mode */
1005bccd 46 append_operation(desc, op_flags);
281922a1 47
1005bccd
AP
48 if (!handle && do_sk) {
49 /*
50 * For SH0, Secure Keys must be generated as well
51 */
281922a1 52
1005bccd
AP
53 /* wait for done */
54 jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
55 set_jump_tgt_here(desc, jump_cmd);
281922a1 56
1005bccd
AP
57 /*
58 * load 1 to clear written reg:
24c7bf08 59 * resets the done interrupt and returns the RNG to idle.
1005bccd
AP
60 */
61 append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
62
63 /* Initialize State Handle */
64 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
65 OP_ALG_AAI_RNG4_SK);
66 }
281922a1 67
d5e4e999 68 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
281922a1 69}
281922a1 70
b1f996e0 71/* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
1005bccd 72static void build_deinstantiation_desc(u32 *desc, int handle)
b1f996e0
AP
73{
74 init_job_desc(desc, 0);
281922a1 75
b1f996e0 76 /* Uninstantiate State Handle 0 */
281922a1 77 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
1005bccd 78 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
b1f996e0
AP
79
80 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
281922a1
KP
81}
82
9b5c33b1 83#ifdef CONFIG_OF
271e3830
PG
84static const struct of_device_id imx8m_machine_match[] = {
85 { .compatible = "fsl,imx8mm", },
86 { .compatible = "fsl,imx8mn", },
87 { .compatible = "fsl,imx8mp", },
88 { .compatible = "fsl,imx8mq", },
89 { .compatible = "fsl,imx8ulp", },
90 { }
91};
9b5c33b1 92#endif
271e3830 93
04cddbfe
AP
94/*
95 * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
96 * the software (no JR/QI used).
97 * @ctrldev - pointer to device
1005bccd
AP
98 * @status - descriptor status, after being run
99 *
04cddbfe
AP
100 * Return: - 0 if no error occurred
101 * - -ENODEV if the DECO couldn't be acquired
102 * - -EAGAIN if an error occurred while executing the descriptor
103 */
1005bccd
AP
104static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
105 u32 *status)
281922a1 106{
997ad290 107 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
fb4562b2
NNL
108 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
109 struct caam_deco __iomem *deco = ctrlpriv->deco;
997ad290 110 unsigned int timeout = 100000;
d239b10d 111 u32 deco_dbg_reg, deco_state, flags;
b1f996e0 112 int i;
997ad290 113
17157c90 114
a6727055
AS
115 if (ctrlpriv->virt_en == 1 ||
116 /*
7e2b89fb 117 * Apparently on i.MX8M{Q,M,N,P} it doesn't matter if virt_en == 1
a6727055
AS
118 * and the following steps should be performed regardless
119 */
271e3830 120 of_match_node(imx8m_machine_match, of_root)) {
261ea058 121 clrsetbits_32(&ctrl->deco_rsr, 0, DECORSR_JR0);
17157c90 122
fb4562b2 123 while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) &&
8f1da7b9
HG
124 --timeout)
125 cpu_relax();
126
127 timeout = 100000;
128 }
17157c90 129
261ea058 130 clrsetbits_32(&ctrl->deco_rq, 0, DECORR_RQD0ENABLE);
997ad290 131
fb4562b2 132 while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) &&
997ad290
RG
133 --timeout)
134 cpu_relax();
135
136 if (!timeout) {
137 dev_err(ctrldev, "failed to acquire DECO 0\n");
261ea058 138 clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
04cddbfe 139 return -ENODEV;
281922a1
KP
140 }
141
997ad290 142 for (i = 0; i < desc_len(desc); i++)
261ea058 143 wr_reg32(&deco->descbuf[i], caam32_to_cpu(*(desc + i)));
281922a1 144
04cddbfe
AP
145 flags = DECO_JQCR_WHL;
146 /*
147 * If the descriptor length is longer than 4 words, then the
148 * FOUR bit in JRCTRL register must be set.
149 */
150 if (desc_len(desc) >= 4)
151 flags |= DECO_JQCR_FOUR;
152
153 /* Instruct the DECO to execute it */
261ea058 154 clrsetbits_32(&deco->jr_ctl_hi, 0, flags);
997ad290
RG
155
156 timeout = 10000000;
84cf4827 157 do {
fb4562b2 158 deco_dbg_reg = rd_reg32(&deco->desc_dbg);
d239b10d
HG
159
160 if (ctrlpriv->era < 10)
161 deco_state = (deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) >>
162 DESC_DBG_DECO_STAT_SHIFT;
163 else
164 deco_state = (rd_reg32(&deco->dbg_exec) &
165 DESC_DER_DECO_STAT_MASK) >>
166 DESC_DER_DECO_STAT_SHIFT;
167
84cf4827 168 /*
24c7bf08 169 * If an error occurred in the descriptor, then
84cf4827
AP
170 * the DECO status field will be set to 0x0D
171 */
d239b10d 172 if (deco_state == DECO_STAT_HOST_ERR)
84cf4827 173 break;
d239b10d 174
997ad290 175 cpu_relax();
84cf4827 176 } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
281922a1 177
fb4562b2 178 *status = rd_reg32(&deco->op_status_hi) &
1005bccd 179 DECO_OP_STATUS_HI_ERR_MASK;
997ad290 180
17157c90 181 if (ctrlpriv->virt_en == 1)
261ea058 182 clrsetbits_32(&ctrl->deco_rsr, DECORSR_JR0, 0);
17157c90 183
04cddbfe 184 /* Mark the DECO as free */
261ea058 185 clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
04cddbfe
AP
186
187 if (!timeout)
188 return -EAGAIN;
189
190 return 0;
191}
192
e57acaf0
AS
193/*
194 * deinstantiate_rng - builds and executes a descriptor on DECO0,
195 * which deinitializes the RNG block.
196 * @ctrldev - pointer to device
197 * @state_handle_mask - bitmask containing the instantiation status
198 * for the RNG4 state handles which exist in
199 * the RNG4 block: 1 if it's been instantiated
200 *
201 * Return: - 0 if no error occurred
202 * - -ENOMEM if there isn't enough memory to allocate the descriptor
203 * - -ENODEV if DECO0 couldn't be acquired
204 * - -EAGAIN if an error occurred when executing the descriptor
205 */
206static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
207{
208 u32 *desc, status;
209 int sh_idx, ret = 0;
210
199354d7 211 desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
e57acaf0
AS
212 if (!desc)
213 return -ENOMEM;
214
215 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
216 /*
217 * If the corresponding bit is set, then it means the state
218 * handle was initialized by us, and thus it needs to be
219 * deinitialized as well
220 */
221 if ((1 << sh_idx) & state_handle_mask) {
222 /*
223 * Create the descriptor for deinstantating this state
224 * handle
225 */
226 build_deinstantiation_desc(desc, sh_idx);
227
228 /* Try to run it through DECO0 */
229 ret = run_descriptor_deco0(ctrldev, desc, &status);
230
231 if (ret ||
232 (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
233 dev_err(ctrldev,
234 "Failed to deinstantiate RNG4 SH%d\n",
235 sh_idx);
236 break;
237 }
238 dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
239 }
240 }
241
242 kfree(desc);
243
244 return ret;
245}
246
247static void devm_deinstantiate_rng(void *data)
248{
249 struct device *ctrldev = data;
250 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
251
252 /*
253 * De-initialize RNG state handles initialized by this driver.
254 * In case of SoCs with Management Complex, RNG is managed by MC f/w.
255 */
256 if (ctrlpriv->rng4_sh_init)
257 deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
258}
259
04cddbfe
AP
260/*
261 * instantiate_rng - builds and executes a descriptor on DECO0,
262 * which initializes the RNG block.
263 * @ctrldev - pointer to device
1005bccd
AP
264 * @state_handle_mask - bitmask containing the instantiation status
265 * for the RNG4 state handles which exist in
266 * the RNG4 block: 1 if it's been instantiated
267 * by an external entry, 0 otherwise.
268 * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
269 * Caution: this can be done only once; if the keys need to be
270 * regenerated, a POR is required
271 *
04cddbfe
AP
272 * Return: - 0 if no error occurred
273 * - -ENOMEM if there isn't enough memory to allocate the descriptor
274 * - -ENODEV if DECO0 couldn't be acquired
275 * - -EAGAIN if an error occurred when executing the descriptor
276 * f.i. there was a RNG hardware error due to not "good enough"
24c7bf08 277 * entropy being acquired.
04cddbfe 278 */
1005bccd
AP
279static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
280 int gen_sk)
04cddbfe 281{
1005bccd 282 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
fb4562b2 283 struct caam_ctrl __iomem *ctrl;
62743a41 284 u32 *desc, status = 0, rdsta_val;
1005bccd
AP
285 int ret = 0, sh_idx;
286
fb4562b2 287 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
199354d7 288 desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
04cddbfe
AP
289 if (!desc)
290 return -ENOMEM;
04cddbfe 291
1005bccd 292 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
358ba762
AS
293 const u32 rdsta_if = RDSTA_IF0 << sh_idx;
294 const u32 rdsta_pr = RDSTA_PR0 << sh_idx;
295 const u32 rdsta_mask = rdsta_if | rdsta_pr;
9c19fb86
CJ
296
297 /* Clear the contents before using the descriptor */
298 memset(desc, 0x00, CAAM_CMD_SZ * 7);
299
1005bccd
AP
300 /*
301 * If the corresponding bit is set, this state handle
302 * was initialized by somebody else, so it's left alone.
303 */
358ba762
AS
304 if (rdsta_if & state_handle_mask) {
305 if (rdsta_pr & state_handle_mask)
306 continue;
307
308 dev_info(ctrldev,
309 "RNG4 SH%d was previously instantiated without prediction resistance. Tearing it down\n",
310 sh_idx);
311
312 ret = deinstantiate_rng(ctrldev, rdsta_if);
313 if (ret)
314 break;
315 }
1005bccd
AP
316
317 /* Create the descriptor for instantiating RNG State Handle */
318 build_instantiation_desc(desc, sh_idx, gen_sk);
319
320 /* Try to run it through DECO0 */
321 ret = run_descriptor_deco0(ctrldev, desc, &status);
322
323 /*
324 * If ret is not 0, or descriptor status is not 0, then
325 * something went wrong. No need to try the next state
326 * handle (if available), bail out here.
327 * Also, if for some reason, the State Handle didn't get
328 * instantiated although the descriptor has finished
329 * without any error (HW optimizations for later
330 * CAAM eras), then try again.
331 */
225ece3e
HG
332 if (ret)
333 break;
334
358ba762 335 rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_MASK;
62743a41 336 if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) ||
358ba762 337 (rdsta_val & rdsta_mask) != rdsta_mask) {
1005bccd 338 ret = -EAGAIN;
1005bccd 339 break;
225ece3e
HG
340 }
341
1005bccd 342 dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
1005bccd 343 }
04cddbfe 344
997ad290 345 kfree(desc);
04cddbfe 346
3ec25b43
AS
347 if (ret)
348 return ret;
b1f996e0 349
3ec25b43 350 return devm_add_action_or_reset(ctrldev, devm_deinstantiate_rng, ctrldev);
b1f996e0
AP
351}
352
281922a1 353/*
84cf4827
AP
354 * kick_trng - sets the various parameters for enabling the initialization
355 * of the RNG4 block in CAAM
da2f2a03 356 * @dev - pointer to the controller device
84cf4827 357 * @ent_delay - Defines the length (in system clocks) of each entropy sample.
281922a1 358 */
da2f2a03 359static void kick_trng(struct device *dev, int ent_delay)
281922a1 360{
da2f2a03 361 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
fb4562b2 362 struct caam_ctrl __iomem *ctrl;
281922a1 363 struct rng4tst __iomem *r4tst;
1abc8966 364 u32 val, rtsdctl;
281922a1 365
fb4562b2
NNL
366 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
367 r4tst = &ctrl->r4tst[0];
281922a1 368
551ce72a
AS
369 /*
370 * Setting both RTMCTL:PRGM and RTMCTL:TRNG_ACC causes TRNG to
371 * properly invalidate the entropy in the entropy register and
372 * force re-generation.
373 */
374 clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM | RTMCTL_ACC);
84cf4827
AP
375
376 /*
377 * Performance-wise, it does not make sense to
378 * set the delay to a value that is lower
379 * than the last one that worked (i.e. the state handles
1abc8966 380 * were instantiated properly).
84cf4827 381 */
1abc8966
MA
382 rtsdctl = rd_reg32(&r4tst->rtsdctl);
383 val = (rtsdctl & RTSDCTL_ENT_DLY_MASK) >> RTSDCTL_ENT_DLY_SHIFT;
384 if (ent_delay > val) {
385 val = ent_delay;
386 /* min. freq. count, equal to 1/4 of the entropy sample length */
387 wr_reg32(&r4tst->rtfrqmin, val >> 2);
83874b8e
HX
388 /* disable maximum frequency count */
389 wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE);
1abc8966
MA
390 }
391
392 wr_reg32(&r4tst->rtsdctl, (val << RTSDCTL_ENT_DLY_SHIFT) |
393 RTSDCTL_SAMP_SIZE_VAL);
394
395 /*
396 * To avoid reprogramming the self-test parameters over and over again,
397 * use RTSDCTL[SAMP_SIZE] as an indicator.
398 */
399 if ((rtsdctl & RTSDCTL_SAMP_SIZE_MASK) != RTSDCTL_SAMP_SIZE_VAL) {
400 wr_reg32(&r4tst->rtscmisc, (2 << 16) | 32);
401 wr_reg32(&r4tst->rtpkrrng, 570);
402 wr_reg32(&r4tst->rtpkrmax, 1600);
403 wr_reg32(&r4tst->rtscml, (122 << 16) | 317);
404 wr_reg32(&r4tst->rtscrl[0], (80 << 16) | 107);
405 wr_reg32(&r4tst->rtscrl[1], (57 << 16) | 62);
406 wr_reg32(&r4tst->rtscrl[2], (39 << 16) | 39);
407 wr_reg32(&r4tst->rtscrl[3], (27 << 16) | 26);
408 wr_reg32(&r4tst->rtscrl[4], (19 << 16) | 18);
409 wr_reg32(&r4tst->rtscrl[5], (18 << 16) | 17);
410 }
411
e5ffbfc1
AP
412 /*
413 * select raw sampling in both entropy shifter
8439e94f 414 * and statistical checker; ; put RNG4 into run mode
e5ffbfc1 415 */
551ce72a
AS
416 clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM | RTMCTL_ACC,
417 RTMCTL_SAMP_MODE_RAW_ES_SC);
281922a1
KP
418}
419
ae1dd17d 420static int caam_get_era_from_hw(struct caam_perfmon __iomem *perfmon)
654f2b93
FE
421{
422 static const struct {
423 u16 ip_id;
424 u8 maj_rev;
425 u8 era;
426 } id[] = {
427 {0x0A10, 1, 1},
428 {0x0A10, 2, 2},
429 {0x0A12, 1, 3},
430 {0x0A14, 1, 3},
431 {0x0A14, 2, 4},
432 {0x0A16, 1, 4},
433 {0x0A10, 3, 4},
434 {0x0A11, 1, 4},
435 {0x0A18, 1, 4},
436 {0x0A11, 2, 5},
437 {0x0A12, 2, 5},
438 {0x0A13, 1, 5},
439 {0x0A1C, 1, 5}
440 };
441 u32 ccbvid, id_ms;
442 u8 maj_rev, era;
443 u16 ip_id;
444 int i;
445
ae1dd17d 446 ccbvid = rd_reg32(&perfmon->ccb_id);
654f2b93
FE
447 era = (ccbvid & CCBVID_ERA_MASK) >> CCBVID_ERA_SHIFT;
448 if (era) /* This is '0' prior to CAAM ERA-6 */
449 return era;
450
ae1dd17d 451 id_ms = rd_reg32(&perfmon->caam_id_ms);
654f2b93
FE
452 ip_id = (id_ms & SECVID_MS_IPID_MASK) >> SECVID_MS_IPID_SHIFT;
453 maj_rev = (id_ms & SECVID_MS_MAJ_REV_MASK) >> SECVID_MS_MAJ_REV_SHIFT;
454
455 for (i = 0; i < ARRAY_SIZE(id); i++)
456 if (id[i].ip_id == ip_id && id[i].maj_rev == maj_rev)
457 return id[i].era;
458
459 return -ENOTSUPP;
460}
461
82c2f960
AP
462/**
463 * caam_get_era() - Return the ERA of the SEC on SoC, based
654f2b93
FE
464 * on "sec-era" optional property in the DTS. This property is updated
465 * by u-boot.
466 * In case this property is not passed an attempt to retrieve the CAAM
467 * era via register reads will be made.
319936bf 468 *
ae1dd17d 469 * @perfmon: Performance Monitor Registers
319936bf 470 */
ae1dd17d 471static int caam_get_era(struct caam_perfmon __iomem *perfmon)
82c2f960 472{
883619a9 473 struct device_node *caam_node;
e27513eb
AP
474 int ret;
475 u32 prop;
476
477 caam_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
478 ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop);
479 of_node_put(caam_node);
82c2f960 480
654f2b93
FE
481 if (!ret)
482 return prop;
483 else
ae1dd17d 484 return caam_get_era_from_hw(perfmon);
82c2f960 485}
82c2f960 486
33d69455 487/*
4fa0b1f9 488 * ERRATA: imx6 devices (imx6D, imx6Q, imx6DL, imx6S, imx6DP and imx6QP)
33d69455
IP
489 * have an issue wherein AXI bus transactions may not occur in the correct
490 * order. This isn't a problem running single descriptors, but can be if
491 * running multiple concurrent descriptors. Reworking the driver to throttle
492 * to single requests is impractical, thus the workaround is to limit the AXI
493 * pipeline to a depth of 1 (from it's default of 4) to preclude this situation
494 * from occurring.
495 */
864c2d57 496static void handle_imx6_err005766(u32 __iomem *mcr)
33d69455
IP
497{
498 if (of_machine_is_compatible("fsl,imx6q") ||
499 of_machine_is_compatible("fsl,imx6dl") ||
500 of_machine_is_compatible("fsl,imx6qp"))
501 clrsetbits_32(mcr, MCFGR_AXIPIPE_MASK,
502 1 << MCFGR_AXIPIPE_SHIFT);
503}
504
ec360607
HG
505static const struct of_device_id caam_match[] = {
506 {
507 .compatible = "fsl,sec-v4.0",
508 },
509 {
510 .compatible = "fsl,sec4.0",
511 },
512 {},
513};
514MODULE_DEVICE_TABLE(of, caam_match);
515
51e002e9 516struct caam_imx_data {
61444368 517 bool page0_access;
51e002e9
AS
518 const struct clk_bulk_data *clks;
519 int num_clks;
520};
521
522static const struct clk_bulk_data caam_imx6_clks[] = {
523 { .id = "ipg" },
524 { .id = "mem" },
525 { .id = "aclk" },
526 { .id = "emi_slow" },
527};
528
529static const struct caam_imx_data caam_imx6_data = {
61444368 530 .page0_access = true,
51e002e9
AS
531 .clks = caam_imx6_clks,
532 .num_clks = ARRAY_SIZE(caam_imx6_clks),
533};
534
535static const struct clk_bulk_data caam_imx7_clks[] = {
536 { .id = "ipg" },
537 { .id = "aclk" },
538};
539
540static const struct caam_imx_data caam_imx7_data = {
61444368 541 .page0_access = true,
51e002e9
AS
542 .clks = caam_imx7_clks,
543 .num_clks = ARRAY_SIZE(caam_imx7_clks),
544};
545
546static const struct clk_bulk_data caam_imx6ul_clks[] = {
547 { .id = "ipg" },
548 { .id = "mem" },
549 { .id = "aclk" },
550};
551
552static const struct caam_imx_data caam_imx6ul_data = {
61444368 553 .page0_access = true,
51e002e9
AS
554 .clks = caam_imx6ul_clks,
555 .num_clks = ARRAY_SIZE(caam_imx6ul_clks),
556};
557
58e5b015
AS
558static const struct clk_bulk_data caam_vf610_clks[] = {
559 { .id = "ipg" },
560};
561
562static const struct caam_imx_data caam_vf610_data = {
61444368 563 .page0_access = true,
58e5b015
AS
564 .clks = caam_vf610_clks,
565 .num_clks = ARRAY_SIZE(caam_vf610_clks),
566};
567
d2835701
PG
568static const struct caam_imx_data caam_imx8ulp_data;
569
51e002e9
AS
570static const struct soc_device_attribute caam_imx_soc_table[] = {
571 { .soc_id = "i.MX6UL", .data = &caam_imx6ul_data },
572 { .soc_id = "i.MX6*", .data = &caam_imx6_data },
573 { .soc_id = "i.MX7*", .data = &caam_imx7_data },
2a2fbf20 574 { .soc_id = "i.MX8M*", .data = &caam_imx7_data },
d2835701 575 { .soc_id = "i.MX8ULP", .data = &caam_imx8ulp_data },
61bb8db6 576 { .soc_id = "i.MX8QM", .data = &caam_imx8ulp_data },
58e5b015 577 { .soc_id = "VF*", .data = &caam_vf610_data },
51e002e9
AS
578 { .family = "Freescale i.MX" },
579 { /* sentinel */ }
580};
581
582static void disable_clocks(void *data)
583{
584 struct caam_drv_private *ctrlpriv = data;
585
586 clk_bulk_disable_unprepare(ctrlpriv->num_clks, ctrlpriv->clks);
587}
588
589static int init_clocks(struct device *dev, const struct caam_imx_data *data)
590{
591 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
592 int ret;
593
594 ctrlpriv->num_clks = data->num_clks;
595 ctrlpriv->clks = devm_kmemdup(dev, data->clks,
596 data->num_clks * sizeof(data->clks[0]),
597 GFP_KERNEL);
598 if (!ctrlpriv->clks)
599 return -ENOMEM;
600
601 ret = devm_clk_bulk_get(dev, ctrlpriv->num_clks, ctrlpriv->clks);
602 if (ret) {
603 dev_err(dev,
604 "Failed to request all necessary clocks\n");
605 return ret;
606 }
607
608 ret = clk_bulk_prepare_enable(ctrlpriv->num_clks, ctrlpriv->clks);
609 if (ret) {
610 dev_err(dev,
611 "Failed to prepare/enable all necessary clocks\n");
612 return ret;
613 }
614
615 return devm_add_action_or_reset(dev, disable_clocks, ctrlpriv);
616}
617
eceb5daf
AS
618static void caam_remove_debugfs(void *root)
619{
620 debugfs_remove_recursive(root);
621}
eceb5daf 622
358ba762
AS
623#ifdef CONFIG_FSL_MC_BUS
624static bool check_version(struct fsl_mc_version *mc_version, u32 major,
625 u32 minor, u32 revision)
626{
627 if (mc_version->major > major)
628 return true;
629
630 if (mc_version->major == major) {
631 if (mc_version->minor > minor)
632 return true;
633
634 if (mc_version->minor == minor &&
635 mc_version->revision > revision)
636 return true;
637 }
638
639 return false;
640}
641#endif
642
4ee4cdad
FE
643static bool needs_entropy_delay_adjustment(void)
644{
645 if (of_machine_is_compatible("fsl,imx6sx"))
646 return true;
647 return false;
648}
649
da2f2a03
HG
650static int caam_ctrl_rng_init(struct device *dev)
651{
652 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
653 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
654 int ret, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
655 u8 rng_vid;
656
657 if (ctrlpriv->era < 10) {
658 struct caam_perfmon __iomem *perfmon;
659
660 perfmon = ctrlpriv->total_jobrs ?
661 (struct caam_perfmon __iomem *)&ctrlpriv->jr[0]->perfmon :
662 (struct caam_perfmon __iomem *)&ctrl->perfmon;
663
664 rng_vid = (rd_reg32(&perfmon->cha_id_ls) &
665 CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT;
666 } else {
667 struct version_regs __iomem *vreg;
668
669 vreg = ctrlpriv->total_jobrs ?
670 (struct version_regs __iomem *)&ctrlpriv->jr[0]->vreg :
671 (struct version_regs __iomem *)&ctrl->vreg;
672
673 rng_vid = (rd_reg32(&vreg->rng) & CHA_VER_VID_MASK) >>
674 CHA_VER_VID_SHIFT;
675 }
676
677 /*
678 * If SEC has RNG version >= 4 and RNG state handle has not been
679 * already instantiated, do RNG instantiation
680 * In case of SoCs with Management Complex, RNG is managed by MC f/w.
681 */
682 if (!(ctrlpriv->mc_en && ctrlpriv->pr_support) && rng_vid >= 4) {
683 ctrlpriv->rng4_sh_init =
684 rd_reg32(&ctrl->r4tst[0].rdsta);
685 /*
686 * If the secure keys (TDKEK, JDKEK, TDSK), were already
687 * generated, signal this to the function that is instantiating
688 * the state handles. An error would occur if RNG4 attempts
689 * to regenerate these keys before the next POR.
690 */
691 gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
692 ctrlpriv->rng4_sh_init &= RDSTA_MASK;
693 do {
694 int inst_handles =
695 rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_MASK;
696 /*
697 * If either SH were instantiated by somebody else
698 * (e.g. u-boot) then it is assumed that the entropy
699 * parameters are properly set and thus the function
700 * setting these (kick_trng(...)) is skipped.
701 * Also, if a handle was instantiated, do not change
702 * the TRNG parameters.
703 */
704 if (needs_entropy_delay_adjustment())
705 ent_delay = 12000;
706 if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
707 dev_info(dev,
708 "Entropy delay = %u\n",
709 ent_delay);
710 kick_trng(dev, ent_delay);
711 ent_delay += 400;
712 }
713 /*
714 * if instantiate_rng(...) fails, the loop will rerun
715 * and the kick_trng(...) function will modify the
716 * upper and lower limits of the entropy sampling
717 * interval, leading to a successful initialization of
718 * the RNG.
719 */
720 ret = instantiate_rng(dev, inst_handles,
721 gen_sk);
722 /*
723 * Entropy delay is determined via TRNG characterization.
724 * TRNG characterization is run across different voltages
725 * and temperatures.
726 * If worst case value for ent_dly is identified,
727 * the loop can be skipped for that platform.
728 */
729 if (needs_entropy_delay_adjustment())
730 break;
731 if (ret == -EAGAIN)
732 /*
733 * if here, the loop will rerun,
734 * so don't hog the CPU
735 */
736 cpu_relax();
737 } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
738 if (ret) {
739 dev_err(dev, "failed to instantiate RNG");
740 return ret;
741 }
742 /*
743 * Set handles initialized by this module as the complement of
744 * the already initialized ones
745 */
746 ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_MASK;
747
748 /* Enable RDB bit so that RNG works faster */
749 clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
750 }
751
752 return 0;
753}
754
322d7475
HG
755/* Indicate if the internal state of the CAAM is lost during PM */
756static int caam_off_during_pm(void)
757{
758 bool not_off_during_pm = of_machine_is_compatible("fsl,imx6q") ||
759 of_machine_is_compatible("fsl,imx6qp") ||
760 of_machine_is_compatible("fsl,imx6dl");
761
762 return not_off_during_pm ? 0 : 1;
763}
764
765static void caam_state_save(struct device *dev)
766{
767 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
768 struct caam_ctl_state *state = &ctrlpriv->state;
769 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
770 u32 deco_inst, jr_inst;
771 int i;
772
773 state->mcr = rd_reg32(&ctrl->mcr);
774 state->scfgr = rd_reg32(&ctrl->scfgr);
775
776 deco_inst = (rd_reg32(&ctrl->perfmon.cha_num_ms) &
777 CHA_ID_MS_DECO_MASK) >> CHA_ID_MS_DECO_SHIFT;
778 for (i = 0; i < deco_inst; i++) {
779 state->deco_mid[i].liodn_ms =
780 rd_reg32(&ctrl->deco_mid[i].liodn_ms);
781 state->deco_mid[i].liodn_ls =
782 rd_reg32(&ctrl->deco_mid[i].liodn_ls);
783 }
784
785 jr_inst = (rd_reg32(&ctrl->perfmon.cha_num_ms) &
786 CHA_ID_MS_JR_MASK) >> CHA_ID_MS_JR_SHIFT;
787 for (i = 0; i < jr_inst; i++) {
788 state->jr_mid[i].liodn_ms =
789 rd_reg32(&ctrl->jr_mid[i].liodn_ms);
790 state->jr_mid[i].liodn_ls =
791 rd_reg32(&ctrl->jr_mid[i].liodn_ls);
792 }
793}
794
795static void caam_state_restore(const struct device *dev)
796{
797 const struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
798 const struct caam_ctl_state *state = &ctrlpriv->state;
799 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
800 u32 deco_inst, jr_inst;
801 int i;
802
803 wr_reg32(&ctrl->mcr, state->mcr);
804 wr_reg32(&ctrl->scfgr, state->scfgr);
805
806 deco_inst = (rd_reg32(&ctrl->perfmon.cha_num_ms) &
807 CHA_ID_MS_DECO_MASK) >> CHA_ID_MS_DECO_SHIFT;
808 for (i = 0; i < deco_inst; i++) {
809 wr_reg32(&ctrl->deco_mid[i].liodn_ms,
810 state->deco_mid[i].liodn_ms);
811 wr_reg32(&ctrl->deco_mid[i].liodn_ls,
812 state->deco_mid[i].liodn_ls);
813 }
814
815 jr_inst = (rd_reg32(&ctrl->perfmon.cha_num_ms) &
816 CHA_ID_MS_JR_MASK) >> CHA_ID_MS_JR_SHIFT;
817 for (i = 0; i < jr_inst; i++) {
818 wr_reg32(&ctrl->jr_mid[i].liodn_ms,
819 state->jr_mid[i].liodn_ms);
820 wr_reg32(&ctrl->jr_mid[i].liodn_ls,
821 state->jr_mid[i].liodn_ls);
822 }
823
824 if (ctrlpriv->virt_en == 1)
825 clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
826 JRSTART_JR1_START | JRSTART_JR2_START |
827 JRSTART_JR3_START);
828}
829
830static int caam_ctrl_suspend(struct device *dev)
831{
832 const struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
833
834 if (ctrlpriv->caam_off_during_pm && !ctrlpriv->optee_en)
835 caam_state_save(dev);
836
837 return 0;
838}
839
840static int caam_ctrl_resume(struct device *dev)
841{
842 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
843 int ret = 0;
844
845 if (ctrlpriv->caam_off_during_pm && !ctrlpriv->optee_en) {
846 caam_state_restore(dev);
847
848 /* HW and rng will be reset so deinstantiation can be removed */
849 devm_remove_action(dev, devm_deinstantiate_rng, dev);
850 ret = caam_ctrl_rng_init(dev);
851 }
852
853 return ret;
854}
855
b52c8c72 856static DEFINE_SIMPLE_DEV_PM_OPS(caam_ctrl_pm_ops, caam_ctrl_suspend, caam_ctrl_resume);
322d7475 857
8e8ec596 858/* Probe routine for CAAM top (controller) level */
2930d497 859static int caam_probe(struct platform_device *pdev)
8e8ec596 860{
da2f2a03 861 int ret, ring;
82c2f960 862 u64 caam_id;
51e002e9 863 const struct soc_device_attribute *imx_soc_match;
8e8ec596
KP
864 struct device *dev;
865 struct device_node *nprop, *np;
866 struct caam_ctrl __iomem *ctrl;
8e8ec596 867 struct caam_drv_private *ctrlpriv;
ae1dd17d 868 struct caam_perfmon __iomem *perfmon;
eceb5daf 869 struct dentry *dfs_root;
17157c90 870 u32 scfgr, comp_params;
fb4562b2
NNL
871 int pg_size;
872 int BLOCK_OFFSET = 0;
0489929f 873 bool reg_access = true;
61444368 874 const struct caam_imx_data *imx_soc_data;
8e8ec596 875
9c4f9733 876 ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
8e8ec596
KP
877 if (!ctrlpriv)
878 return -ENOMEM;
879
880 dev = &pdev->dev;
881 dev_set_drvdata(dev, ctrlpriv);
8e8ec596
KP
882 nprop = pdev->dev.of_node;
883
796114f5 884 imx_soc_match = soc_device_match(caam_imx_soc_table);
271e3830
PG
885 if (!imx_soc_match && of_match_node(imx8m_machine_match, of_root))
886 return -EPROBE_DEFER;
887
796114f5
AS
888 caam_imx = (bool)imx_soc_match;
889
322d7475
HG
890 ctrlpriv->caam_off_during_pm = caam_imx && caam_off_during_pm();
891
796114f5 892 if (imx_soc_match) {
0489929f
HG
893 /*
894 * Until Layerscape and i.MX OP-TEE get in sync,
895 * only i.MX OP-TEE use cases disallow access to
896 * caam page 0 (controller) registers.
897 */
898 np = of_find_compatible_node(NULL, NULL, "linaro,optee-tz");
899 ctrlpriv->optee_en = !!np;
900 of_node_put(np);
901
902 reg_access = !ctrlpriv->optee_en;
903
796114f5
AS
904 if (!imx_soc_match->data) {
905 dev_err(dev, "No clock data provided for i.MX SoC");
906 return -EINVAL;
907 }
908
61444368
PG
909 imx_soc_data = imx_soc_match->data;
910 reg_access = reg_access && imx_soc_data->page0_access;
911 /*
912 * CAAM clocks cannot be controlled from kernel.
913 */
914 if (!imx_soc_data->num_clks)
915 goto iomap_ctrl;
916
796114f5
AS
917 ret = init_clocks(dev, imx_soc_match->data);
918 if (ret)
919 return ret;
920 }
921
61444368 922iomap_ctrl:
176435ad
HG
923 /* Get configuration properties from device tree */
924 /* First, get register page */
66e93b28
AS
925 ctrl = devm_of_iomap(dev, nprop, 0, NULL);
926 ret = PTR_ERR_OR_ZERO(ctrl);
927 if (ret) {
176435ad 928 dev_err(dev, "caam: of_iomap() failed\n");
66e93b28 929 return ret;
176435ad
HG
930 }
931
ae1dd17d
HG
932 ring = 0;
933 for_each_available_child_of_node(nprop, np)
934 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
935 of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
936 u32 reg;
937
938 if (of_property_read_u32_index(np, "reg", 0, &reg)) {
939 dev_err(dev, "%s read reg property error\n",
940 np->full_name);
941 continue;
942 }
943
944 ctrlpriv->jr[ring] = (struct caam_job_ring __iomem __force *)
945 ((__force uint8_t *)ctrl + reg);
946
947 ctrlpriv->total_jobrs++;
948 ring++;
949 }
950
951 /*
952 * Wherever possible, instead of accessing registers from the global page,
953 * use the alias registers in the first (cf. DT nodes order)
954 * job ring's page.
955 */
956 perfmon = ring ? (struct caam_perfmon __iomem *)&ctrlpriv->jr[0]->perfmon :
957 (struct caam_perfmon __iomem *)&ctrl->perfmon;
958
959 caam_little_end = !(bool)(rd_reg32(&perfmon->status) &
176435ad 960 (CSTA_PLEND | CSTA_ALT_PLEND));
ae1dd17d 961 comp_params = rd_reg32(&perfmon->comp_parms_ms);
0489929f
HG
962 if (reg_access && comp_params & CTPR_MS_PS &&
963 rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR)
a1cf573e
AS
964 caam_ptr_sz = sizeof(u64);
965 else
966 caam_ptr_sz = sizeof(u32);
176435ad
HG
967 caam_dpaa2 = !!(comp_params & CTPR_MS_DPAA2);
968 ctrlpriv->qi_present = !!(comp_params & CTPR_MS_QI_MASK);
969
970#ifdef CONFIG_CAAM_QI
971 /* If (DPAA 1.x) QI present, check whether dependencies are available */
972 if (ctrlpriv->qi_present && !caam_dpaa2) {
973 ret = qman_is_probed();
974 if (!ret) {
66e93b28 975 return -EPROBE_DEFER;
176435ad
HG
976 } else if (ret < 0) {
977 dev_err(dev, "failing probe due to qman probe error\n");
66e93b28 978 return -ENODEV;
176435ad
HG
979 }
980
981 ret = qman_portals_probed();
982 if (!ret) {
66e93b28 983 return -EPROBE_DEFER;
176435ad
HG
984 } else if (ret < 0) {
985 dev_err(dev, "failing probe due to qman portals probe error\n");
66e93b28 986 return -ENODEV;
176435ad
HG
987 }
988 }
989#endif
990
fb4562b2
NNL
991 /* Allocating the BLOCK_OFFSET based on the supported page size on
992 * the platform
993 */
176435ad 994 pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
fb4562b2
NNL
995 if (pg_size == 0)
996 BLOCK_OFFSET = PG_SIZE_4K;
997 else
998 BLOCK_OFFSET = PG_SIZE_64K;
999
8439e94f
HG
1000 ctrlpriv->ctrl = (struct caam_ctrl __iomem __force *)ctrl;
1001 ctrlpriv->assure = (struct caam_assurance __iomem __force *)
1002 ((__force uint8_t *)ctrl +
fb4562b2
NNL
1003 BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
1004 );
8439e94f
HG
1005 ctrlpriv->deco = (struct caam_deco __iomem __force *)
1006 ((__force uint8_t *)ctrl +
fb4562b2
NNL
1007 BLOCK_OFFSET * DECO_BLOCK_NUMBER
1008 );
8e8ec596
KP
1009
1010 /* Get the IRQ of the controller (for security violations only) */
f7578496 1011 ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
358ba762
AS
1012 np = of_find_compatible_node(NULL, NULL, "fsl,qoriq-mc");
1013 ctrlpriv->mc_en = !!np;
1014 of_node_put(np);
1015
1016#ifdef CONFIG_FSL_MC_BUS
1017 if (ctrlpriv->mc_en) {
1018 struct fsl_mc_version *mc_version;
1019
1020 mc_version = fsl_mc_get_version();
1021 if (mc_version)
da2f2a03
HG
1022 ctrlpriv->pr_support = check_version(mc_version, 10, 20,
1023 0);
358ba762
AS
1024 else
1025 return -EPROBE_DEFER;
1026 }
1027#endif
8e8ec596 1028
0489929f
HG
1029 if (!reg_access)
1030 goto set_dma_mask;
1031
8e8ec596
KP
1032 /*
1033 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
297b9ceb 1034 * long pointers in master configuration register.
06d44c91 1035 * In case of SoCs with Management Complex, MC f/w performs
297b9ceb 1036 * the configuration.
8e8ec596 1037 */
06d44c91 1038 if (!ctrlpriv->mc_en)
7278fa25 1039 clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK,
297b9ceb 1040 MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF |
7278fa25 1041 MCFGR_WDENABLE | MCFGR_LARGE_BURST);
8e8ec596 1042
33d69455
IP
1043 handle_imx6_err005766(&ctrl->mcr);
1044
17157c90 1045 /*
24c7bf08
HS
1046 * Read the Compile Time parameters and SCFGR to determine
1047 * if virtualization is enabled for this platform
17157c90 1048 */
fb4562b2 1049 scfgr = rd_reg32(&ctrl->scfgr);
17157c90
RG
1050
1051 ctrlpriv->virt_en = 0;
1052 if (comp_params & CTPR_MS_VIRT_EN_INCL) {
1053 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
1054 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
1055 */
1056 if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
1057 (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
1058 (scfgr & SCFGR_VIRT_EN)))
1059 ctrlpriv->virt_en = 1;
1060 } else {
1061 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
1062 if (comp_params & CTPR_MS_VIRT_EN_POR)
1063 ctrlpriv->virt_en = 1;
1064 }
1065
1066 if (ctrlpriv->virt_en == 1)
261ea058
HG
1067 clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
1068 JRSTART_JR1_START | JRSTART_JR2_START |
1069 JRSTART_JR3_START);
17157c90 1070
0489929f 1071set_dma_mask:
70c0cda2 1072 ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev));
b3b5fce7
HG
1073 if (ret) {
1074 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
66e93b28 1075 return ret;
b3b5fce7 1076 }
8e8ec596 1077
ae1dd17d 1078 ctrlpriv->era = caam_get_era(perfmon);
b2b2ee35 1079 ctrlpriv->domain = iommu_get_domain_for_dev(dev);
9fe712df 1080
eceb5daf 1081 dfs_root = debugfs_create_dir(dev_name(dev), NULL);
abd98754
HG
1082 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1083 ret = devm_add_action_or_reset(dev, caam_remove_debugfs,
1084 dfs_root);
1085 if (ret)
1086 return ret;
1087 }
eceb5daf 1088
ae1dd17d 1089 caam_debugfs_init(ctrlpriv, perfmon, dfs_root);
c6dc0609 1090
297b9ceb 1091 /* Check to see if (DPAA 1.x) QI present. If so, enable */
297b9ceb 1092 if (ctrlpriv->qi_present && !caam_dpaa2) {
8439e94f
HG
1093 ctrlpriv->qi = (struct caam_queue_if __iomem __force *)
1094 ((__force uint8_t *)ctrl +
fb4562b2
NNL
1095 BLOCK_OFFSET * QI_BLOCK_NUMBER
1096 );
8e8ec596 1097 /* This is all that's required to physically enable QI */
fb4562b2 1098 wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
67c2315d
HG
1099
1100 /* If QMAN driver is present, init CAAM-QI backend */
1101#ifdef CONFIG_CAAM_QI
1102 ret = caam_qi_init(pdev);
1103 if (ret)
1104 dev_err(dev, "caam qi i/f init failed: %d\n", ret);
1105#endif
8e8ec596
KP
1106 }
1107
1108 /* If no QI and no rings specified, quit and go home */
1109 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
1110 dev_err(dev, "no queues configured, terminating\n");
1a1c4f00 1111 return -ENOMEM;
8e8ec596
KP
1112 }
1113
ae1dd17d 1114 comp_params = rd_reg32(&perfmon->comp_parms_ls);
7a0e7d52
AF
1115 ctrlpriv->blob_present = !!(comp_params & CTPR_LS_BLOB);
1116
1117 /*
1118 * Some SoCs like the LS1028A (non-E) indicate CTPR_LS_BLOB support,
1119 * but fail when actually using it due to missing AES support, so
1120 * check both here.
1121 */
1122 if (ctrlpriv->era < 10) {
7a0e7d52 1123 ctrlpriv->blob_present = ctrlpriv->blob_present &&
ae1dd17d 1124 (rd_reg32(&perfmon->cha_num_ls) & CHA_ID_LS_AES_MASK);
7a0e7d52 1125 } else {
ae1dd17d
HG
1126 struct version_regs __iomem *vreg;
1127
1128 vreg = ctrlpriv->total_jobrs ?
1129 (struct version_regs __iomem *)&ctrlpriv->jr[0]->vreg :
1130 (struct version_regs __iomem *)&ctrl->vreg;
1131
7a0e7d52 1132 ctrlpriv->blob_present = ctrlpriv->blob_present &&
ae1dd17d 1133 (rd_reg32(&vreg->aesa) & CHA_VER_MISC_AES_NUM_MASK);
7a0e7d52 1134 }
986dfbcf 1135
da2f2a03
HG
1136 if (reg_access) {
1137 ret = caam_ctrl_rng_init(dev);
1138 if (ret)
1a1c4f00 1139 return ret;
281922a1
KP
1140 }
1141
ae1dd17d
HG
1142 caam_id = (u64)rd_reg32(&perfmon->caam_id_ms) << 32 |
1143 (u64)rd_reg32(&perfmon->caam_id_ls);
82c2f960 1144
8e8ec596 1145 /* Report "alive" for developer to see */
82c2f960 1146 dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
9fe712df 1147 ctrlpriv->era);
06d44c91
HG
1148 dev_info(dev, "job rings = %d, qi = %d\n",
1149 ctrlpriv->total_jobrs, ctrlpriv->qi_present);
8e8ec596 1150
51d13aaf
AS
1151 ret = devm_of_platform_populate(dev);
1152 if (ret)
1153 dev_err(dev, "JR platform devices creation error\n");
1154
1155 return ret;
8e8ec596
KP
1156}
1157
2930d497 1158static struct platform_driver caam_driver = {
8e8ec596
KP
1159 .driver = {
1160 .name = "caam",
8e8ec596 1161 .of_match_table = caam_match,
b52c8c72 1162 .pm = pm_ptr(&caam_ctrl_pm_ops),
8e8ec596
KP
1163 },
1164 .probe = caam_probe,
8e8ec596
KP
1165};
1166
741e8c2d 1167module_platform_driver(caam_driver);
8e8ec596
KP
1168
1169MODULE_LICENSE("GPL");
1170MODULE_DESCRIPTION("FSL CAAM request backend");
1171MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");