1 // SPDX-License-Identifier: GPL-2.0+
2 /* * CAAM control-plane driver backend
3 * Controller-level driver, kernel property detection, initialization
5 * Copyright 2008-2012 Freescale Semiconductor, Inc.
6 * Copyright 2018-2019 NXP
9 #include <linux/device.h>
10 #include <linux/of_address.h>
11 #include <linux/of_irq.h>
12 #include <linux/sys_soc.h>
18 #include "desc_constr.h"
22 EXPORT_SYMBOL(caam_dpaa2);
29 * Descriptor to instantiate RNG State Handle 0 in normal mode and
30 * load the JDKEK, TDKEK and TDSK registers
32 static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
34 u32 *jump_cmd, op_flags;
36 init_job_desc(desc, 0);
38 op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
39 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT;
41 /* INIT RNG in non-test mode */
42 append_operation(desc, op_flags);
44 if (!handle && do_sk) {
46 * For SH0, Secure Keys must be generated as well
50 jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
51 set_jump_tgt_here(desc, jump_cmd);
54 * load 1 to clear written reg:
55 * resets the done interrrupt and returns the RNG to idle.
57 append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
59 /* Initialize State Handle */
60 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
64 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
67 /* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
68 static void build_deinstantiation_desc(u32 *desc, int handle)
70 init_job_desc(desc, 0);
72 /* Uninstantiate State Handle 0 */
73 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
74 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
76 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
80 * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
81 * the software (no JR/QI used).
82 * @ctrldev - pointer to device
83 * @status - descriptor status, after being run
85 * Return: - 0 if no error occurred
86 * - -ENODEV if the DECO couldn't be acquired
87 * - -EAGAIN if an error occurred while executing the descriptor
89 static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
92 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
93 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
94 struct caam_deco __iomem *deco = ctrlpriv->deco;
95 unsigned int timeout = 100000;
96 u32 deco_dbg_reg, deco_state, flags;
100 if (ctrlpriv->virt_en == 1 ||
102 * Apparently on i.MX8MQ it doesn't matter if virt_en == 1
103 * and the following steps should be performed regardless
105 of_machine_is_compatible("fsl,imx8mq")) {
106 clrsetbits_32(&ctrl->deco_rsr, 0, DECORSR_JR0);
108 while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) &&
115 clrsetbits_32(&ctrl->deco_rq, 0, DECORR_RQD0ENABLE);
117 while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) &&
122 dev_err(ctrldev, "failed to acquire DECO 0\n");
123 clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
127 for (i = 0; i < desc_len(desc); i++)
128 wr_reg32(&deco->descbuf[i], caam32_to_cpu(*(desc + i)));
130 flags = DECO_JQCR_WHL;
132 * If the descriptor length is longer than 4 words, then the
133 * FOUR bit in JRCTRL register must be set.
135 if (desc_len(desc) >= 4)
136 flags |= DECO_JQCR_FOUR;
138 /* Instruct the DECO to execute it */
139 clrsetbits_32(&deco->jr_ctl_hi, 0, flags);
143 deco_dbg_reg = rd_reg32(&deco->desc_dbg);
145 if (ctrlpriv->era < 10)
146 deco_state = (deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) >>
147 DESC_DBG_DECO_STAT_SHIFT;
149 deco_state = (rd_reg32(&deco->dbg_exec) &
150 DESC_DER_DECO_STAT_MASK) >>
151 DESC_DER_DECO_STAT_SHIFT;
154 * If an error occured in the descriptor, then
155 * the DECO status field will be set to 0x0D
157 if (deco_state == DECO_STAT_HOST_ERR)
161 } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
163 *status = rd_reg32(&deco->op_status_hi) &
164 DECO_OP_STATUS_HI_ERR_MASK;
166 if (ctrlpriv->virt_en == 1)
167 clrsetbits_32(&ctrl->deco_rsr, DECORSR_JR0, 0);
169 /* Mark the DECO as free */
170 clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
179 * deinstantiate_rng - builds and executes a descriptor on DECO0,
180 * which deinitializes the RNG block.
181 * @ctrldev - pointer to device
182 * @state_handle_mask - bitmask containing the instantiation status
183 * for the RNG4 state handles which exist in
184 * the RNG4 block: 1 if it's been instantiated
186 * Return: - 0 if no error occurred
187 * - -ENOMEM if there isn't enough memory to allocate the descriptor
188 * - -ENODEV if DECO0 couldn't be acquired
189 * - -EAGAIN if an error occurred when executing the descriptor
191 static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
196 desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
200 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
202 * If the corresponding bit is set, then it means the state
203 * handle was initialized by us, and thus it needs to be
204 * deinitialized as well
206 if ((1 << sh_idx) & state_handle_mask) {
208 * Create the descriptor for deinstantating this state
211 build_deinstantiation_desc(desc, sh_idx);
213 /* Try to run it through DECO0 */
214 ret = run_descriptor_deco0(ctrldev, desc, &status);
217 (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
219 "Failed to deinstantiate RNG4 SH%d\n",
223 dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
232 static void devm_deinstantiate_rng(void *data)
234 struct device *ctrldev = data;
235 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
238 * De-initialize RNG state handles initialized by this driver.
239 * In case of SoCs with Management Complex, RNG is managed by MC f/w.
241 if (ctrlpriv->rng4_sh_init)
242 deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
246 * instantiate_rng - builds and executes a descriptor on DECO0,
247 * which initializes the RNG block.
248 * @ctrldev - pointer to device
249 * @state_handle_mask - bitmask containing the instantiation status
250 * for the RNG4 state handles which exist in
251 * the RNG4 block: 1 if it's been instantiated
252 * by an external entry, 0 otherwise.
253 * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
254 * Caution: this can be done only once; if the keys need to be
255 * regenerated, a POR is required
257 * Return: - 0 if no error occurred
258 * - -ENOMEM if there isn't enough memory to allocate the descriptor
259 * - -ENODEV if DECO0 couldn't be acquired
260 * - -EAGAIN if an error occurred when executing the descriptor
261 * f.i. there was a RNG hardware error due to not "good enough"
262 * entropy being aquired.
264 static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
267 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
268 struct caam_ctrl __iomem *ctrl;
269 u32 *desc, status = 0, rdsta_val;
272 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
273 desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
277 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
279 * If the corresponding bit is set, this state handle
280 * was initialized by somebody else, so it's left alone.
282 if ((1 << sh_idx) & state_handle_mask)
285 /* Create the descriptor for instantiating RNG State Handle */
286 build_instantiation_desc(desc, sh_idx, gen_sk);
288 /* Try to run it through DECO0 */
289 ret = run_descriptor_deco0(ctrldev, desc, &status);
292 * If ret is not 0, or descriptor status is not 0, then
293 * something went wrong. No need to try the next state
294 * handle (if available), bail out here.
295 * Also, if for some reason, the State Handle didn't get
296 * instantiated although the descriptor has finished
297 * without any error (HW optimizations for later
298 * CAAM eras), then try again.
303 rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK;
304 if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) ||
305 !(rdsta_val & (1 << sh_idx))) {
310 dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
311 /* Clear the contents before recreating the descriptor */
312 memset(desc, 0x00, CAAM_CMD_SZ * 7);
318 ret = devm_add_action_or_reset(ctrldev, devm_deinstantiate_rng,
325 * kick_trng - sets the various parameters for enabling the initialization
326 * of the RNG4 block in CAAM
327 * @pdev - pointer to the platform device
328 * @ent_delay - Defines the length (in system clocks) of each entropy sample.
330 static void kick_trng(struct platform_device *pdev, int ent_delay)
332 struct device *ctrldev = &pdev->dev;
333 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
334 struct caam_ctrl __iomem *ctrl;
335 struct rng4tst __iomem *r4tst;
338 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
339 r4tst = &ctrl->r4tst[0];
341 /* put RNG4 into program mode */
342 clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM);
345 * Performance-wise, it does not make sense to
346 * set the delay to a value that is lower
347 * than the last one that worked (i.e. the state handles
348 * were instantiated properly. Thus, instead of wasting
349 * time trying to set the values controlling the sample
350 * frequency, the function simply returns.
352 val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
353 >> RTSDCTL_ENT_DLY_SHIFT;
354 if (ent_delay <= val)
357 val = rd_reg32(&r4tst->rtsdctl);
358 val = (val & ~RTSDCTL_ENT_DLY_MASK) |
359 (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
360 wr_reg32(&r4tst->rtsdctl, val);
361 /* min. freq. count, equal to 1/4 of the entropy sample length */
362 wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
363 /* disable maximum frequency count */
364 wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE);
365 /* read the control register */
366 val = rd_reg32(&r4tst->rtmctl);
369 * select raw sampling in both entropy shifter
370 * and statistical checker; ; put RNG4 into run mode
372 clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM, RTMCTL_SAMP_MODE_RAW_ES_SC);
375 static int caam_get_era_from_hw(struct caam_ctrl __iomem *ctrl)
377 static const struct {
401 ccbvid = rd_reg32(&ctrl->perfmon.ccb_id);
402 era = (ccbvid & CCBVID_ERA_MASK) >> CCBVID_ERA_SHIFT;
403 if (era) /* This is '0' prior to CAAM ERA-6 */
406 id_ms = rd_reg32(&ctrl->perfmon.caam_id_ms);
407 ip_id = (id_ms & SECVID_MS_IPID_MASK) >> SECVID_MS_IPID_SHIFT;
408 maj_rev = (id_ms & SECVID_MS_MAJ_REV_MASK) >> SECVID_MS_MAJ_REV_SHIFT;
410 for (i = 0; i < ARRAY_SIZE(id); i++)
411 if (id[i].ip_id == ip_id && id[i].maj_rev == maj_rev)
418 * caam_get_era() - Return the ERA of the SEC on SoC, based
419 * on "sec-era" optional property in the DTS. This property is updated
421 * In case this property is not passed an attempt to retrieve the CAAM
422 * era via register reads will be made.
424 static int caam_get_era(struct caam_ctrl __iomem *ctrl)
426 struct device_node *caam_node;
430 caam_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
431 ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop);
432 of_node_put(caam_node);
437 return caam_get_era_from_hw(ctrl);
441 * ERRATA: imx6 devices (imx6D, imx6Q, imx6DL, imx6S, imx6DP and imx6QP)
442 * have an issue wherein AXI bus transactions may not occur in the correct
443 * order. This isn't a problem running single descriptors, but can be if
444 * running multiple concurrent descriptors. Reworking the driver to throttle
445 * to single requests is impractical, thus the workaround is to limit the AXI
446 * pipeline to a depth of 1 (from it's default of 4) to preclude this situation
449 static void handle_imx6_err005766(u32 *mcr)
451 if (of_machine_is_compatible("fsl,imx6q") ||
452 of_machine_is_compatible("fsl,imx6dl") ||
453 of_machine_is_compatible("fsl,imx6qp"))
454 clrsetbits_32(mcr, MCFGR_AXIPIPE_MASK,
455 1 << MCFGR_AXIPIPE_SHIFT);
458 static const struct of_device_id caam_match[] = {
460 .compatible = "fsl,sec-v4.0",
463 .compatible = "fsl,sec4.0",
467 MODULE_DEVICE_TABLE(of, caam_match);
469 struct caam_imx_data {
470 const struct clk_bulk_data *clks;
474 static const struct clk_bulk_data caam_imx6_clks[] = {
478 { .id = "emi_slow" },
481 static const struct caam_imx_data caam_imx6_data = {
482 .clks = caam_imx6_clks,
483 .num_clks = ARRAY_SIZE(caam_imx6_clks),
486 static const struct clk_bulk_data caam_imx7_clks[] = {
491 static const struct caam_imx_data caam_imx7_data = {
492 .clks = caam_imx7_clks,
493 .num_clks = ARRAY_SIZE(caam_imx7_clks),
496 static const struct clk_bulk_data caam_imx6ul_clks[] = {
502 static const struct caam_imx_data caam_imx6ul_data = {
503 .clks = caam_imx6ul_clks,
504 .num_clks = ARRAY_SIZE(caam_imx6ul_clks),
507 static const struct soc_device_attribute caam_imx_soc_table[] = {
508 { .soc_id = "i.MX6UL", .data = &caam_imx6ul_data },
509 { .soc_id = "i.MX6*", .data = &caam_imx6_data },
510 { .soc_id = "i.MX7*", .data = &caam_imx7_data },
511 { .soc_id = "i.MX8MQ", .data = &caam_imx7_data },
512 { .family = "Freescale i.MX" },
516 static void disable_clocks(void *data)
518 struct caam_drv_private *ctrlpriv = data;
520 clk_bulk_disable_unprepare(ctrlpriv->num_clks, ctrlpriv->clks);
523 static int init_clocks(struct device *dev, const struct caam_imx_data *data)
525 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
528 ctrlpriv->num_clks = data->num_clks;
529 ctrlpriv->clks = devm_kmemdup(dev, data->clks,
530 data->num_clks * sizeof(data->clks[0]),
535 ret = devm_clk_bulk_get(dev, ctrlpriv->num_clks, ctrlpriv->clks);
538 "Failed to request all necessary clocks\n");
542 ret = clk_bulk_prepare_enable(ctrlpriv->num_clks, ctrlpriv->clks);
545 "Failed to prepare/enable all necessary clocks\n");
549 return devm_add_action_or_reset(dev, disable_clocks, ctrlpriv);
552 #ifdef CONFIG_DEBUG_FS
553 static void caam_remove_debugfs(void *root)
555 debugfs_remove_recursive(root);
559 /* Probe routine for CAAM top (controller) level */
560 static int caam_probe(struct platform_device *pdev)
562 int ret, ring, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
564 const struct soc_device_attribute *imx_soc_match;
566 struct device_node *nprop, *np;
567 struct caam_ctrl __iomem *ctrl;
568 struct caam_drv_private *ctrlpriv;
569 #ifdef CONFIG_DEBUG_FS
570 struct caam_perfmon *perfmon;
571 struct dentry *dfs_root;
573 u32 scfgr, comp_params;
576 int BLOCK_OFFSET = 0;
578 ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
583 dev_set_drvdata(dev, ctrlpriv);
584 nprop = pdev->dev.of_node;
586 imx_soc_match = soc_device_match(caam_imx_soc_table);
587 caam_imx = (bool)imx_soc_match;
590 if (!imx_soc_match->data) {
591 dev_err(dev, "No clock data provided for i.MX SoC");
595 ret = init_clocks(dev, imx_soc_match->data);
601 /* Get configuration properties from device tree */
602 /* First, get register page */
603 ctrl = devm_of_iomap(dev, nprop, 0, NULL);
604 ret = PTR_ERR_OR_ZERO(ctrl);
606 dev_err(dev, "caam: of_iomap() failed\n");
610 caam_little_end = !(bool)(rd_reg32(&ctrl->perfmon.status) &
611 (CSTA_PLEND | CSTA_ALT_PLEND));
612 comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
613 if (comp_params & CTPR_MS_PS && rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR)
614 caam_ptr_sz = sizeof(u64);
616 caam_ptr_sz = sizeof(u32);
617 caam_dpaa2 = !!(comp_params & CTPR_MS_DPAA2);
618 ctrlpriv->qi_present = !!(comp_params & CTPR_MS_QI_MASK);
620 #ifdef CONFIG_CAAM_QI
621 /* If (DPAA 1.x) QI present, check whether dependencies are available */
622 if (ctrlpriv->qi_present && !caam_dpaa2) {
623 ret = qman_is_probed();
625 return -EPROBE_DEFER;
626 } else if (ret < 0) {
627 dev_err(dev, "failing probe due to qman probe error\n");
631 ret = qman_portals_probed();
633 return -EPROBE_DEFER;
634 } else if (ret < 0) {
635 dev_err(dev, "failing probe due to qman portals probe error\n");
641 /* Allocating the BLOCK_OFFSET based on the supported page size on
644 pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
646 BLOCK_OFFSET = PG_SIZE_4K;
648 BLOCK_OFFSET = PG_SIZE_64K;
650 ctrlpriv->ctrl = (struct caam_ctrl __iomem __force *)ctrl;
651 ctrlpriv->assure = (struct caam_assurance __iomem __force *)
652 ((__force uint8_t *)ctrl +
653 BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
655 ctrlpriv->deco = (struct caam_deco __iomem __force *)
656 ((__force uint8_t *)ctrl +
657 BLOCK_OFFSET * DECO_BLOCK_NUMBER
660 /* Get the IRQ of the controller (for security violations only) */
661 ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
664 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
665 * long pointers in master configuration register.
666 * In case of SoCs with Management Complex, MC f/w performs
669 np = of_find_compatible_node(NULL, NULL, "fsl,qoriq-mc");
670 ctrlpriv->mc_en = !!np;
673 if (!ctrlpriv->mc_en)
674 clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK | MCFGR_LONG_PTR,
675 MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF |
676 MCFGR_WDENABLE | MCFGR_LARGE_BURST |
677 (sizeof(dma_addr_t) == sizeof(u64) ?
678 MCFGR_LONG_PTR : 0));
680 handle_imx6_err005766(&ctrl->mcr);
683 * Read the Compile Time paramters and SCFGR to determine
684 * if Virtualization is enabled for this platform
686 scfgr = rd_reg32(&ctrl->scfgr);
688 ctrlpriv->virt_en = 0;
689 if (comp_params & CTPR_MS_VIRT_EN_INCL) {
690 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
691 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
693 if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
694 (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
695 (scfgr & SCFGR_VIRT_EN)))
696 ctrlpriv->virt_en = 1;
698 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
699 if (comp_params & CTPR_MS_VIRT_EN_POR)
700 ctrlpriv->virt_en = 1;
703 if (ctrlpriv->virt_en == 1)
704 clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
705 JRSTART_JR1_START | JRSTART_JR2_START |
708 ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev));
710 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
714 ctrlpriv->era = caam_get_era(ctrl);
715 ctrlpriv->domain = iommu_get_domain_for_dev(dev);
717 #ifdef CONFIG_DEBUG_FS
719 * FIXME: needs better naming distinction, as some amalgamation of
720 * "caam" and nprop->full_name. The OF name isn't distinctive,
721 * but does separate instances
723 perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
725 dfs_root = debugfs_create_dir(dev_name(dev), NULL);
726 ret = devm_add_action_or_reset(dev, caam_remove_debugfs, dfs_root);
730 ctrlpriv->ctl = debugfs_create_dir("ctl", dfs_root);
733 /* Check to see if (DPAA 1.x) QI present. If so, enable */
734 if (ctrlpriv->qi_present && !caam_dpaa2) {
735 ctrlpriv->qi = (struct caam_queue_if __iomem __force *)
736 ((__force uint8_t *)ctrl +
737 BLOCK_OFFSET * QI_BLOCK_NUMBER
739 /* This is all that's required to physically enable QI */
740 wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
742 /* If QMAN driver is present, init CAAM-QI backend */
743 #ifdef CONFIG_CAAM_QI
744 ret = caam_qi_init(pdev);
746 dev_err(dev, "caam qi i/f init failed: %d\n", ret);
751 for_each_available_child_of_node(nprop, np)
752 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
753 of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
754 ctrlpriv->jr[ring] = (struct caam_job_ring __iomem __force *)
755 ((__force uint8_t *)ctrl +
756 (ring + JR_BLOCK_NUMBER) *
759 ctrlpriv->total_jobrs++;
763 /* If no QI and no rings specified, quit and go home */
764 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
765 dev_err(dev, "no queues configured, terminating\n");
769 if (ctrlpriv->era < 10)
770 rng_vid = (rd_reg32(&ctrl->perfmon.cha_id_ls) &
771 CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT;
773 rng_vid = (rd_reg32(&ctrl->vreg.rng) & CHA_VER_VID_MASK) >>
777 * If SEC has RNG version >= 4 and RNG state handle has not been
778 * already instantiated, do RNG instantiation
779 * In case of SoCs with Management Complex, RNG is managed by MC f/w.
781 if (!ctrlpriv->mc_en && rng_vid >= 4) {
782 ctrlpriv->rng4_sh_init =
783 rd_reg32(&ctrl->r4tst[0].rdsta);
785 * If the secure keys (TDKEK, JDKEK, TDSK), were already
786 * generated, signal this to the function that is instantiating
787 * the state handles. An error would occur if RNG4 attempts
788 * to regenerate these keys before the next POR.
790 gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
791 ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
794 rd_reg32(&ctrl->r4tst[0].rdsta) &
797 * If either SH were instantiated by somebody else
798 * (e.g. u-boot) then it is assumed that the entropy
799 * parameters are properly set and thus the function
800 * setting these (kick_trng(...)) is skipped.
801 * Also, if a handle was instantiated, do not change
802 * the TRNG parameters.
804 if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
806 "Entropy delay = %u\n",
808 kick_trng(pdev, ent_delay);
812 * if instantiate_rng(...) fails, the loop will rerun
813 * and the kick_trng(...) function will modfiy the
814 * upper and lower limits of the entropy sampling
815 * interval, leading to a sucessful initialization of
818 ret = instantiate_rng(dev, inst_handles,
822 * if here, the loop will rerun,
823 * so don't hog the CPU
826 } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
828 dev_err(dev, "failed to instantiate RNG");
832 * Set handles init'ed by this module as the complement of the
833 * already initialized ones
835 ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
837 /* Enable RDB bit so that RNG works faster */
838 clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
841 /* NOTE: RTIC detection ought to go here, around Si time */
843 caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 |
844 (u64)rd_reg32(&ctrl->perfmon.caam_id_ls);
846 /* Report "alive" for developer to see */
847 dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
849 dev_info(dev, "job rings = %d, qi = %d\n",
850 ctrlpriv->total_jobrs, ctrlpriv->qi_present);
852 #ifdef CONFIG_DEBUG_FS
853 debugfs_create_file("rq_dequeued", S_IRUSR | S_IRGRP | S_IROTH,
854 ctrlpriv->ctl, &perfmon->req_dequeued,
856 debugfs_create_file("ob_rq_encrypted", S_IRUSR | S_IRGRP | S_IROTH,
857 ctrlpriv->ctl, &perfmon->ob_enc_req,
859 debugfs_create_file("ib_rq_decrypted", S_IRUSR | S_IRGRP | S_IROTH,
860 ctrlpriv->ctl, &perfmon->ib_dec_req,
862 debugfs_create_file("ob_bytes_encrypted", S_IRUSR | S_IRGRP | S_IROTH,
863 ctrlpriv->ctl, &perfmon->ob_enc_bytes,
865 debugfs_create_file("ob_bytes_protected", S_IRUSR | S_IRGRP | S_IROTH,
866 ctrlpriv->ctl, &perfmon->ob_prot_bytes,
868 debugfs_create_file("ib_bytes_decrypted", S_IRUSR | S_IRGRP | S_IROTH,
869 ctrlpriv->ctl, &perfmon->ib_dec_bytes,
871 debugfs_create_file("ib_bytes_validated", S_IRUSR | S_IRGRP | S_IROTH,
872 ctrlpriv->ctl, &perfmon->ib_valid_bytes,
875 /* Controller level - global status values */
876 debugfs_create_file("fault_addr", S_IRUSR | S_IRGRP | S_IROTH,
877 ctrlpriv->ctl, &perfmon->faultaddr,
879 debugfs_create_file("fault_detail", S_IRUSR | S_IRGRP | S_IROTH,
880 ctrlpriv->ctl, &perfmon->faultdetail,
882 debugfs_create_file("fault_status", S_IRUSR | S_IRGRP | S_IROTH,
883 ctrlpriv->ctl, &perfmon->status,
886 /* Internal covering keys (useful in non-secure mode only) */
887 ctrlpriv->ctl_kek_wrap.data = (__force void *)&ctrlpriv->ctrl->kek[0];
888 ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
889 debugfs_create_blob("kek", S_IRUSR | S_IRGRP | S_IROTH, ctrlpriv->ctl,
890 &ctrlpriv->ctl_kek_wrap);
892 ctrlpriv->ctl_tkek_wrap.data = (__force void *)&ctrlpriv->ctrl->tkek[0];
893 ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
894 debugfs_create_blob("tkek", S_IRUSR | S_IRGRP | S_IROTH, ctrlpriv->ctl,
895 &ctrlpriv->ctl_tkek_wrap);
897 ctrlpriv->ctl_tdsk_wrap.data = (__force void *)&ctrlpriv->ctrl->tdsk[0];
898 ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
899 debugfs_create_blob("tdsk", S_IRUSR | S_IRGRP | S_IROTH, ctrlpriv->ctl,
900 &ctrlpriv->ctl_tdsk_wrap);
903 ret = devm_of_platform_populate(dev);
905 dev_err(dev, "JR platform devices creation error\n");
910 static struct platform_driver caam_driver = {
913 .of_match_table = caam_match,
918 module_platform_driver(caam_driver);
920 MODULE_LICENSE("GPL");
921 MODULE_DESCRIPTION("FSL CAAM request backend");
922 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");