Skip to contentSkip to conversation

1: Adding rngb entry in imx6ull device tree

PATCH In review Kshitiz Varshney 4 participants 15 messages Sep 8, 2022 – Dec 21, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 1: Adding rngb entry in imx6ull device tree

From: Kshitiz <[email protected]>

Added entry for rngb in imx6ull device tree which is required for Random number generation in u-boot.

---
 arch/arm/dts/imx6ull.dtsi | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm/dts/imx6ull.dtsi +6 -0
index 46e7ad6bab..c5d5a5ab7b 100644
@@ -66,6 +66,12 @@
 				clocks = <&clks IMX6ULL_CLK_DCP_CLK>;
 				clock-names = "dcp";
 			};
+			rngb: rng@2284000 {
+				compatible = "fsl,imx6ull-rngb", "fsl,imx25-rngb";
+				reg = <0x02284000 0x4000>;
+				interrupts = <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>;
+				clocks = <&clks IMX6UL_CLK_DUMMY>;
+			};
 
 			iomuxc_snvs: iomuxc-snvs@2290000 {
 				compatible = "fsl,imx6ull-iomuxc-snvs";
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 3: Added dcp_rng driver device binding code

From: Kshitiz <[email protected]>

This commit manually binds dcp_rng device driver and initalizes it inside arch_misc_init() function.

---
 arch/arm/mach-imx/mx6/soc.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/arm/mach-imx/mx6/soc.c +16 -0
index 3e538754d9..9bf16119c2 100644
@@ -31,6 +31,8 @@
 #include <hang.h>
 #include <cpu_func.h>
 #include <env.h>
+#include<dm/device-internal.h>
+#include<dm/lists.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1005,6 +1007,20 @@ int arch_misc_init(void)
 		if (ret)
 			printf("Failed to initialize caam_jr: %d\n", ret);
 	}
+
+	if (IS_ENABLED(CONFIG_FSL_DCP_RNG)) {
+		struct udevice *dev;
+		int ret;
+
+		ret = device_bind_driver(NULL, "dcp_rng", "dcp_rng", NULL);
+		if (ret)
+			printf("Couldn't bind dcp rng driver (%d)\n", ret);
+
+		ret = uclass_get_device_by_driver(UCLASS_RNG, DM_DRIVER_GET(dcp_rng), &dev);
+		if (ret)
+			printf("Failed to initialize dcp rng: %d\n", ret);
+	}
+
 	setup_serial_number();
 	return 0;
 }
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v1] 3: Added dcp_rng driver device binding code

Hi Kshitiz,

+Ilias Apalodimas

Quoted 26 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit manually binds dcp_rng device driver and initalizes it inside arch_misc_init() function.

---
 arch/arm/mach-imx/mx6/soc.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/arm/mach-imx/mx6/soc.c +2 -0
index 3e538754d9..9bf16119c2 100644
@@ -31,6 +31,8 @@
 #include <hang.h>
 #include <cpu_func.h>
 #include <env.h>
+#include<dm/device-internal.h>
+#include<dm/lists.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 

arch/arm/mach-imx/mx6/soc.c int arch_misc_init(void) · line 1015

Quoted 9 lines
arch/arm/mach-imx/mx6/soc.c +6 -0
@@ -1005,6 +1007,20 @@ int arch_misc_init(void)
                if (ret)
                        printf("Failed to initialize caam_jr: %d\n", ret);
        }
+
+       if (IS_ENABLED(CONFIG_FSL_DCP_RNG)) {
+               struct udevice *dev;
+               int ret;
+
+               ret = device_bind_driver(NULL, "dcp_rng", "dcp_rng", NULL);

This needs to be in the device tree. This it the kind of madness I was warning about with Ilias, so I have copied him here.

We need to stop manually binding devices when they should be in the DT.

Quoted 12 lines
arch/arm/mach-imx/mx6/soc.c +8 -1
+               if (ret)
+                       printf("Couldn't bind dcp rng driver (%d)\n", ret);
+
+               ret = uclass_get_device_by_driver(UCLASS_RNG, DM_DRIVER_GET(dcp_rng), &dev);
+               if (ret)
+                       printf("Failed to initialize dcp rng: %d\n", ret);
+       }
+
        setup_serial_number();
        return 0;
 }
--
Quoted 2 lines

2.25.1

Regards,
SImon

#3 lore raw

Re: [PATCH v1] 3: Added dcp_rng driver device binding code

Quoted 14 lines · Simon Glass

Hi Kshitiz,

+Ilias Apalodimas

Quoted 8 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit manually binds dcp_rng device driver and initalizes it inside arch_misc_init() function.

CC the maintainer of RNG Sughosh Ganu <[email protected]>

Quoted 50 lines
Quoted 18 lines
---
  arch/arm/mach-imx/mx6/soc.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
arch/arm/mach-imx/mx6/soc.c +2 -0
index 3e538754d9..9bf16119c2 100644
@@ -31,6 +31,8 @@
  #include <hang.h>
  #include <cpu_func.h>
  #include <env.h>
+#include<dm/device-internal.h>
+#include<dm/lists.h>
 
  DECLARE_GLOBAL_DATA_PTR;
 

arch/arm/mach-imx/mx6/soc.c int arch_misc_init(void) · line 1015

Quoted 9 lines
arch/arm/mach-imx/mx6/soc.c +6 -0
@@ -1005,6 +1007,20 @@ int arch_misc_init(void)
                 if (ret)
                         printf("Failed to initialize caam_jr: %d\n", ret);
         }
+
+       if (IS_ENABLED(CONFIG_FSL_DCP_RNG)) {
+               struct udevice *dev;
+               int ret;
+
+               ret = device_bind_driver(NULL, "dcp_rng", "dcp_rng", NULL);

This needs to be in the device tree. This it the kind of madness I was warning about with Ilias, so I have copied him here.

We need to stop manually binding devices when they should be in the DT.

Quoted 12 lines
arch/arm/mach-imx/mx6/soc.c +8 -1
+               if (ret)
+                       printf("Couldn't bind dcp rng driver (%d)\n", ret);
+
+               ret = uclass_get_device_by_driver(UCLASS_RNG, DM_DRIVER_GET(dcp_rng), &dev);
+               if (ret)
+                       printf("Failed to initialize dcp rng: %d\n", ret);
+       }
+
         setup_serial_number();
         return 0;
  }
--
Quoted 2 lines

2.25.1

Regards,
SImon

#4 lore raw

RE: [EXT] Re: [PATCH v1] 3: Added dcp_rng driver device binding code

In reply to Simon Glass

Please find comment inline.

Regards,
Kshitiz

-----Original Message-----
From: Simon Glass <[email protected]>
Sent: Thursday, September 8, 2022 11:49 PM
To: Kshitiz Varshney <[email protected]>
Cc: U-Boot Mailing List <[email protected]>; Horia Geanta <[email protected]>; Pankaj Gupta <[email protected]>; Varun Sethi <[email protected]>; Gaurav Jain <[email protected]>; Rahul Kumar Yadav <[email protected]>; Vabhav Sharma <[email protected]>; Sahil Malhotra <[email protected]>; Ye Li <[email protected]>; Heinrich Schuchardt <[email protected]>; Stefano Babic <[email protected]>; Fabio Estevam <[email protected]>; Peng Fan <[email protected]>
Subject: [EXT] Re: [PATCH v1] 3: Added dcp_rng driver device binding code

Caution: EXT Email

Hi Kshitiz,

+Ilias Apalodimas

Quoted 27 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit manually binds dcp_rng device driver and initalizes it inside
arch_misc_init() function.

---
 arch/arm/mach-imx/mx6/soc.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/arm/mach-imx/mx6/soc.c +2 -0
index 3e538754d9..9bf16119c2 100644
@@ -31,6 +31,8 @@
 #include <hang.h>
 #include <cpu_func.h>
 #include <env.h>
+#include<dm/device-internal.h>
+#include<dm/lists.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 

arch/arm/mach-imx/mx6/soc.c int arch_misc_init(void) · line 1016

Quoted 10 lines
arch/arm/mach-imx/mx6/soc.c +7 -0
@@ -1005,6 +1007,20 @@ int arch_misc_init(void)
                if (ret)
                        printf("Failed to initialize caam_jr: %d\n", ret);
        }
+
+       if (IS_ENABLED(CONFIG_FSL_DCP_RNG)) {
+               struct udevice *dev;
+               int ret;
+
+               ret = device_bind_driver(NULL, "dcp_rng", "dcp_rng", 
+ NULL);

This needs to be in the device tree. This it the kind of madness I was warning about with Ilias, so I have copied him here.

We need to stop manually binding devices when they should be in the DT.

I have removed manual binding in next patch instead used u-boot device tree rngb entry for binding device automatically.

Quoted 13 lines
arch/arm/mach-imx/mx6/soc.c +9 -1
+               if (ret)
+                       printf("Couldn't bind dcp rng driver (%d)\n", 
+ ret);
+
+               ret = uclass_get_device_by_driver(UCLASS_RNG, DM_DRIVER_GET(dcp_rng), &dev);
+               if (ret)
+                       printf("Failed to initialize dcp rng: %d\n", ret);
+       }
+
        setup_serial_number();
        return 0;
 }
--
Quoted 2 lines

2.25.1

Regards,
SImon

#5 lore raw

PATCH 1/1 2: Uboot RNG Driver using Data Co-processor

In reply to Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit introduces Random number generator to uboot. It uses DCP driver for number generation.
RNG driver can be invoked by using below command on uboot prompt:-
rng <number of bytes>

---
 drivers/crypto/fsl/Kconfig   |  10 ++
 drivers/crypto/fsl/Makefile  |   1 +
 drivers/crypto/fsl/dcp_rng.c | 184 +++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+)
 create mode 100644 drivers/crypto/fsl/dcp_rng.c
drivers/crypto/fsl/Kconfig +10 -0
index 702d204a3d..da5955e31d 100644
@@ -96,3 +96,13 @@ config RNG_SELF_TEST
 	 must be run before running any RNG based crypto implementation.
 
 endif
+
+config FSL_DCP_RNG
+	bool "Enable Random Number Generator support"
+	depends on DM_RNG
+	default n
+	help
+	Enable support for the hardware based random number generator
+	module of the DCP.It uses the True Random Number Generator (TRNG)
+	and a Pseudo-Random Number Generator (PRNG) to achieve a true
+	randomness and cryptographic strength.
drivers/crypto/fsl/Makefile +1 -0
index 926300e2ab..c653208d23 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_FSL_CAAM) += jr.o fsl_hash.o jobdesc.o error.o
 obj-$(CONFIG_FSL_BLOB) += fsl_blob.o
 obj-$(CONFIG_RSA_FREESCALE_EXP) += fsl_rsa.o
 obj-$(CONFIG_FSL_CAAM_RNG) += rng.o
+obj-$(CONFIG_FSL_DCP_RNG) += dcp_rng.o
 obj-$(CONFIG_IMX_CAAM_MFG_PROT) += fsl_mfgprot.o
 obj-$(CONFIG_RNG_SELF_TEST) += rng_self_test.o
 obj-$(CONFIG_CMD_PROVISION_KEY) += fsl_aes.o tag_object.o
drivers/crypto/fsl/dcp_rng.c new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#6 lore raw

Re: [PATCH v1] 2: Uboot RNG Driver using Data Co-processor

Hi Kshitiz,

Quoted 8 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit introduces Random number generator to uboot. It uses DCP driver for number generation.
RNG driver can be invoked by using below command on uboot prompt:-
rng <number of bytes>

Quoted 8 lines
---
 drivers/crypto/fsl/Kconfig   |  10 ++
 drivers/crypto/fsl/Makefile  |   1 +
 drivers/crypto/fsl/dcp_rng.c | 184 +++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+)
 create mode 100644 drivers/crypto/fsl/dcp_rng.c

but please see below

Quoted 7 lines
drivers/crypto/fsl/Kconfig +0 -0
index 702d204a3d..da5955e31d 100644
@@ -96,3 +96,13 @@ config RNG_SELF_TEST
         must be run before running any RNG based crypto implementation.

drivers/crypto/fsl/Kconfig config RNG_SELF_TEST · line 106

Quoted 10 lines
drivers/crypto/fsl/Kconfig +8 -0
 
 endif
+
+config FSL_DCP_RNG
+       bool "Enable Random Number Generator support"
+       depends on DM_RNG
+       default n
+       help
+       Enable support for the hardware based random number generator
+       module of the DCP.It uses the True Random Number Generator (TRNG)

Space before It

Quoted 30 lines
drivers/crypto/fsl/Kconfig +2 -0
+       and a Pseudo-Random Number Generator (PRNG) to achieve a true
+       randomness and cryptographic strength.
drivers/crypto/fsl/Makefile +1 -0
index 926300e2ab..c653208d23 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_FSL_CAAM) += jr.o fsl_hash.o jobdesc.o error.o
 obj-$(CONFIG_FSL_BLOB) += fsl_blob.o
 obj-$(CONFIG_RSA_FREESCALE_EXP) += fsl_rsa.o
 obj-$(CONFIG_FSL_CAAM_RNG) += rng.o
+obj-$(CONFIG_FSL_DCP_RNG) += dcp_rng.o
 obj-$(CONFIG_IMX_CAAM_MFG_PROT) += fsl_mfgprot.o
 obj-$(CONFIG_RNG_SELF_TEST) += rng_self_test.o
 obj-$(CONFIG_CMD_PROVISION_KEY) += fsl_aes.o tag_object.o
drivers/crypto/fsl/dcp_rng.c new file +10 -0
new file mode 100644
index 0000000000..a797710c2e
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * RNG driver for Freescale RNGC
+ *
+ * Copyright (C) 2008-2012 Freescale Semiconductor, Inc.
+ * Copyright (C) 2017 Martin Kaiser <[email protected]>
+ *     Copyright 2022 NXP
+ *
+ * Based on RNGC driver in drivers/char/hw_random/imx-rngc.c in Linux
+ */

drivers/crypto/fsl/dcp_rng.c line 20

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+
+#include <asm/cache.h>
+#include <common.h>
+#include <cpu_func.h>
+#include <dm.h>
+#include <rng.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <asm/io.h>
+#include <dm/root.h>

Should be:

Quoted 27 lines
drivers/crypto/fsl/dcp_rng.c new file +25 -0
+#include <common.h>
+#include <cpu_func.h>
+#include <dm.h>
+#include <rng.h>
+#include <asm/cache.h>
+#include <asm/io.h>
+#include <dm/root.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
 
 
+
+#define DCP_RNG_MAX_FIFO_STORE_SIZE    4
+#define RNGC_VER_ID                    0x0000
+#define RNGC_COMMAND                   0x0004
+#define RNGC_CONTROL                   0x0008
+#define RNGC_STATUS                    0x000C
+#define RNGC_ERROR                     0x0010
+#define RNGC_FIFO                      0x0014
+
+/* the fields in the ver id register */
+#define RNGC_TYPE_SHIFT                        28
+
+/* the rng_type field */
+#define RNGC_TYPE_RNGB                 0x1
+#define RNGC_TYPE_RNGC                 0x2
+

drivers/crypto/fsl/dcp_rng.c line 46

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+#define RNGC_CMD_CLR_ERR               0x00000020
+#define RNGC_CMD_SEED                  0x00000002
+
+#define RNGC_CTRL_AUTO_SEED            0x00000010
+
+#define RNGC_STATUS_ERROR              0x00010000
+#define RNGC_STATUS_FIFO_LEVEL_MASK    0x00000f00
+#define RNGC_STATUS_FIFO_LEVEL_SHIFT   8
+#define RNGC_STATUS_SEED_DONE          0x00000020
+#define RNGC_STATUS_ST_DONE            0x00000010

Why all the leading zeroes?

drivers/crypto/fsl/dcp_rng.c line 52

Quoted 6 lines
drivers/crypto/fsl/dcp_rng.c new file +6 -0
+
+#define RNGC_ERROR_STATUS_STAT_ERR     0x00000008
+
+#define RNGC_TIMEOUT                   3000000U /* 3 sec */
+
+struct imx_rngc {

Normally the priv data should have a _priv suffix.

drivers/crypto/fsl/dcp_rng.c line 58

Quoted 6 lines
drivers/crypto/fsl/dcp_rng.c new file +6 -0
+       unsigned long base;
+};
+
+static int rngc_read(struct udevice *dev, void *data, size_t len)
+{
+       struct imx_rngc *rngc = dev_get_priv(dev);

Normally the var should be called priv

Quoted 58 lines
drivers/crypto/fsl/dcp_rng.c new file +58 -0
+       u8 buffer[DCP_RNG_MAX_FIFO_STORE_SIZE];
+       u32 status, level;
+       size_t size;
+
+       while (len) {
+               status = readl(rngc->base + RNGC_STATUS);
+
+               /* is there some error while reading this random number? */
+               if (status & RNGC_STATUS_ERROR)
+                       break;
+               /* how many random numbers are in FIFO? [0-16] */
+               level = (status & RNGC_STATUS_FIFO_LEVEL_MASK) >>
+                       RNGC_STATUS_FIFO_LEVEL_SHIFT;
+
+               if (level) {
+                       /* retrieve a random number from FIFO */
+                       *(u32 *)buffer = readl(rngc->base + RNGC_FIFO);
+                       size = min(len, sizeof(u32));
+                       memcpy(data, buffer, size);
+                       data += size;
+                       len -= size;
+               }
+       }
+
+       return len ? -EIO : 0;
+}
+
+static int rngc_init(struct imx_rngc *rngc)
+{
+       u32 cmd, ctrl, status, err_reg = 0;
+       unsigned long long timeval = 0;
+       unsigned long long timeout = RNGC_TIMEOUT;
+
+       /* clear error */
+       cmd = readl(rngc->base + RNGC_COMMAND);
+       writel(cmd | RNGC_CMD_CLR_ERR, rngc->base + RNGC_COMMAND);
+
+       /* create seed, repeat while there is some statistical error */
+       do {
+               /* seed creation */
+               cmd = readl(rngc->base + RNGC_COMMAND);
+               writel(cmd | RNGC_CMD_SEED, rngc->base + RNGC_COMMAND);
+
+               udelay(1);
+               timeval += 1;
+
+               status = readl(rngc->base + RNGC_STATUS);
+               err_reg = readl(rngc->base + RNGC_ERROR);
+
+               if (status & (RNGC_STATUS_SEED_DONE | RNGC_STATUS_ST_DONE))
+                       break;
+
+               if (timeval > timeout) {
+                       debug("rngc timed out\n");
+                       return -ETIMEDOUT;
+               }
+       } while (err_reg == RNGC_ERROR_STATUS_STAT_ERR);
+

drivers/crypto/fsl/dcp_rng.c line 126

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+       if (err_reg)
+               return -EIO;
+
+       /*
+        * enable automatic seeding, the rngc creates a new seed automatically
+        * after serving 2^20 random 160-bit words
+        */
+       ctrl = readl(rngc->base + RNGC_CONTROL);
+       ctrl |= RNGC_CTRL_AUTO_SEED;
+       writel(ctrl, rngc->base + RNGC_CONTROL);

setbits_le32(rngc->base + RNGC_CONTROL, RNGC_CTRL_AUTO_SEED);

Quoted 59 lines
drivers/crypto/fsl/dcp_rng.c new file +58 -1
+       return 0;
+}
+
+static int rngc_probe(struct udevice *dev)
+{
+       struct imx_rngc *rngc = dev_get_priv(dev);
+       fdt_addr_t addr;
+       u32 ver_id;
+       u8  rng_type;
+       int ret;
+
+       addr = dev_read_addr(dev);
+       if (addr == FDT_ADDR_T_NONE) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       rngc->base = addr;
+       ver_id = readl(rngc->base + RNGC_VER_ID);
+       rng_type = ver_id >> RNGC_TYPE_SHIFT;
+       /*
+        * This driver supports only RNGC and RNGB. (There's a different
+        * driver for RNGA.)
+        */
+       if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) {
+               ret = -ENODEV;
+               goto err;
+       }
+
+       ret = rngc_init(rngc);
+       if (ret)
+               goto err;
+
+       return 0;
+
+err:
+       printf("%s error = %d\n", __func__, ret);
+       return ret;
+}
+
+static const struct dm_rng_ops rngc_ops = {
+       .read = rngc_read,
+};
+
+static const struct udevice_id rngc_dt_ids[] = {
+       { .compatible = "fsl,imx25-rngb" },
+       { }
+};
+
+U_BOOT_DRIVER(dcp_rng) = {
+       .name = "dcp_rng",
+       .id = UCLASS_RNG,
+       .of_match = rngc_dt_ids,
+       .ops = &rngc_ops,
+       .probe = rngc_probe,
+       .priv_auto  = sizeof(struct imx_rngc),
+       .flags = DM_FLAG_ALLOC_PRIV_DMA,
+};
--
Quoted 2 lines

2.25.1

Regards,
Simon

#7 lore raw

Re: [PATCH v1] 2: Uboot RNG Driver using Data Co-processor

Quoted 12 lines · Simon Glass

Hi Kshitiz,

Quoted 9 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit introduces Random number generator to uboot. It uses DCP driver for number generation.
RNG driver can be invoked by using below command on uboot prompt:-
rng <number of bytes>

CC the maintainer for RNG Sughosh Ganu <[email protected]>.

Quoted 20 lines
Quoted 7 lines
---
  drivers/crypto/fsl/Kconfig   |  10 ++
  drivers/crypto/fsl/Makefile  |   1 +
  drivers/crypto/fsl/dcp_rng.c | 184 +++++++++++++++++++++++++++++++++++
  3 files changed, 195 insertions(+)

create mode 100644 drivers/crypto/fsl/dcp_rng.c

but please see below

Quoted 8 lines
drivers/crypto/fsl/Kconfig +0 -0
index 702d204a3d..da5955e31d 100644
@@ -96,3 +96,13 @@ config RNG_SELF_TEST
          must be run before running any RNG based crypto implementation.
 
Quoted 9 lines

drivers/crypto/fsl/Kconfig config RNG_SELF_TEST · line 106

Quoted 9 lines
drivers/crypto/fsl/Kconfig +8 -0
  endif
+
+config FSL_DCP_RNG
+       bool "Enable Random Number Generator support"
+       depends on DM_RNG
+       default n
+       help
+       Enable support for the hardware based random number generator
+       module of the DCP.It uses the True Random Number Generator (TRNG)

Please, use the same indentation as the other Kconfig entries in this
file: Add two space in each line of the help text.

Quoted 95 lines

Space before It

Quoted 30 lines
drivers/crypto/fsl/Kconfig +2 -0
+       and a Pseudo-Random Number Generator (PRNG) to achieve a true
+       randomness and cryptographic strength.
drivers/crypto/fsl/Makefile +1 -0
index 926300e2ab..c653208d23 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_FSL_CAAM) += jr.o fsl_hash.o jobdesc.o error.o
  obj-$(CONFIG_FSL_BLOB) += fsl_blob.o
  obj-$(CONFIG_RSA_FREESCALE_EXP) += fsl_rsa.o
  obj-$(CONFIG_FSL_CAAM_RNG) += rng.o
+obj-$(CONFIG_FSL_DCP_RNG) += dcp_rng.o
  obj-$(CONFIG_IMX_CAAM_MFG_PROT) += fsl_mfgprot.o
  obj-$(CONFIG_RNG_SELF_TEST) += rng_self_test.o
  obj-$(CONFIG_CMD_PROVISION_KEY) += fsl_aes.o tag_object.o
drivers/crypto/fsl/dcp_rng.c new file +10 -0
new file mode 100644
index 0000000000..a797710c2e
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * RNG driver for Freescale RNGC
+ *
+ * Copyright (C) 2008-2012 Freescale Semiconductor, Inc.
+ * Copyright (C) 2017 Martin Kaiser <[email protected]>
+ *     Copyright 2022 NXP
+ *
+ * Based on RNGC driver in drivers/char/hw_random/imx-rngc.c in Linux
+ */

drivers/crypto/fsl/dcp_rng.c line 20

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+
+#include <asm/cache.h>
+#include <common.h>
+#include <cpu_func.h>
+#include <dm.h>
+#include <rng.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <asm/io.h>
+#include <dm/root.h>

Should be:

Quoted 27 lines
drivers/crypto/fsl/dcp_rng.c new file +25 -0
+#include <common.h>
+#include <cpu_func.h>
+#include <dm.h>
+#include <rng.h>
+#include <asm/cache.h>
+#include <asm/io.h>
+#include <dm/root.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
 
 
+
+#define DCP_RNG_MAX_FIFO_STORE_SIZE    4
+#define RNGC_VER_ID                    0x0000
+#define RNGC_COMMAND                   0x0004
+#define RNGC_CONTROL                   0x0008
+#define RNGC_STATUS                    0x000C
+#define RNGC_ERROR                     0x0010
+#define RNGC_FIFO                      0x0014
+
+/* the fields in the ver id register */
+#define RNGC_TYPE_SHIFT                        28
+
+/* the rng_type field */
+#define RNGC_TYPE_RNGB                 0x1
+#define RNGC_TYPE_RNGC                 0x2
+

drivers/crypto/fsl/dcp_rng.c line 46

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+#define RNGC_CMD_CLR_ERR               0x00000020
+#define RNGC_CMD_SEED                  0x00000002
+
+#define RNGC_CTRL_AUTO_SEED            0x00000010
+
+#define RNGC_STATUS_ERROR              0x00010000
+#define RNGC_STATUS_FIFO_LEVEL_MASK    0x00000f00
+#define RNGC_STATUS_FIFO_LEVEL_SHIFT   8
+#define RNGC_STATUS_SEED_DONE          0x00000020
+#define RNGC_STATUS_ST_DONE            0x00000010

Why all the leading zeroes?

drivers/crypto/fsl/dcp_rng.c line 52

Quoted 6 lines
drivers/crypto/fsl/dcp_rng.c new file +6 -0
+
+#define RNGC_ERROR_STATUS_STAT_ERR     0x00000008
+
+#define RNGC_TIMEOUT                   3000000U /* 3 sec */
+
+struct imx_rngc {

Normally the priv data should have a _priv suffix.

Quoted 8 lines

drivers/crypto/fsl/dcp_rng.c line 58

Quoted 6 lines
drivers/crypto/fsl/dcp_rng.c new file +6 -0
+       unsigned long base;
+};
+
+static int rngc_read(struct udevice *dev, void *data, size_t len)
+{
+       struct imx_rngc *rngc = dev_get_priv(dev);

Normally the var should be called priv

This is described in doc/develop/codingstyle.rst line 192ff.

Quoted 38 lines
Quoted 38 lines
drivers/crypto/fsl/dcp_rng.c new file +37 -0
+       u8 buffer[DCP_RNG_MAX_FIFO_STORE_SIZE];
+       u32 status, level;
+       size_t size;
+
+       while (len) {
+               status = readl(rngc->base + RNGC_STATUS);
+
+               /* is there some error while reading this random number? */
+               if (status & RNGC_STATUS_ERROR)
+                       break;
+               /* how many random numbers are in FIFO? [0-16] */
+               level = (status & RNGC_STATUS_FIFO_LEVEL_MASK) >>
+                       RNGC_STATUS_FIFO_LEVEL_SHIFT;
+
+               if (level) {
+                       /* retrieve a random number from FIFO */
+                       *(u32 *)buffer = readl(rngc->base + RNGC_FIFO);
+                       size = min(len, sizeof(u32));
+                       memcpy(data, buffer, size);
+                       data += size;
+                       len -= size;
+               }
+       }
+
+       return len ? -EIO : 0;
+}
+
+static int rngc_init(struct imx_rngc *rngc)
+{
+       u32 cmd, ctrl, status, err_reg = 0;
+       unsigned long long timeval = 0;
+       unsigned long long timeout = RNGC_TIMEOUT;
+
+       /* clear error */
+       cmd = readl(rngc->base + RNGC_COMMAND);
+       writel(cmd | RNGC_CMD_CLR_ERR, rngc->base + RNGC_COMMAND);
+
Quoted 8 lines

drivers/crypto/fsl/dcp_rng.c line 103

Quoted 8 lines
drivers/crypto/fsl/dcp_rng.c new file +8 -0
+       /* create seed, repeat while there is some statistical error */
+       do {
+               /* seed creation */
+               cmd = readl(rngc->base + RNGC_COMMAND);
+               writel(cmd | RNGC_CMD_SEED, rngc->base + RNGC_COMMAND);
+
+               udelay(1);
+               timeval += 1;

As this loop can take rather long, should we call WATCHDOG_RESET() before and after the loop?

Otherwise looks good to me.

Quoted 90 lines
Quoted 13 lines
drivers/crypto/fsl/dcp_rng.c new file +13 -0
+
+               status = readl(rngc->base + RNGC_STATUS);
+               err_reg = readl(rngc->base + RNGC_ERROR);
+
+               if (status & (RNGC_STATUS_SEED_DONE | RNGC_STATUS_ST_DONE))
+                       break;
+
+               if (timeval > timeout) {
+                       debug("rngc timed out\n");
+                       return -ETIMEDOUT;
+               }
+       } while (err_reg == RNGC_ERROR_STATUS_STAT_ERR);
+

drivers/crypto/fsl/dcp_rng.c line 126

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+       if (err_reg)
+               return -EIO;
+
+       /*
+        * enable automatic seeding, the rngc creates a new seed automatically
+        * after serving 2^20 random 160-bit words
+        */
+       ctrl = readl(rngc->base + RNGC_CONTROL);
+       ctrl |= RNGC_CTRL_AUTO_SEED;
+       writel(ctrl, rngc->base + RNGC_CONTROL);

setbits_le32(rngc->base + RNGC_CONTROL, RNGC_CTRL_AUTO_SEED);

Quoted 59 lines
drivers/crypto/fsl/dcp_rng.c new file +58 -1
+       return 0;
+}
+
+static int rngc_probe(struct udevice *dev)
+{
+       struct imx_rngc *rngc = dev_get_priv(dev);
+       fdt_addr_t addr;
+       u32 ver_id;
+       u8  rng_type;
+       int ret;
+
+       addr = dev_read_addr(dev);
+       if (addr == FDT_ADDR_T_NONE) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       rngc->base = addr;
+       ver_id = readl(rngc->base + RNGC_VER_ID);
+       rng_type = ver_id >> RNGC_TYPE_SHIFT;
+       /*
+        * This driver supports only RNGC and RNGB. (There's a different
+        * driver for RNGA.)
+        */
+       if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) {
+               ret = -ENODEV;
+               goto err;
+       }
+
+       ret = rngc_init(rngc);
+       if (ret)
+               goto err;
+
+       return 0;
+
+err:
+       printf("%s error = %d\n", __func__, ret);
+       return ret;
+}
+
+static const struct dm_rng_ops rngc_ops = {
+       .read = rngc_read,
+};
+
+static const struct udevice_id rngc_dt_ids[] = {
+       { .compatible = "fsl,imx25-rngb" },
+       { }
+};
+
+U_BOOT_DRIVER(dcp_rng) = {
+       .name = "dcp_rng",
+       .id = UCLASS_RNG,
+       .of_match = rngc_dt_ids,
+       .ops = &rngc_ops,
+       .probe = rngc_probe,
+       .priv_auto  = sizeof(struct imx_rngc),
+       .flags = DM_FLAG_ALLOC_PRIV_DMA,
+};
--
Quoted 2 lines

2.25.1

Regards,
Simon

author Acked-by Heinrich Schuchardt
#8 lore raw

RE: [EXT] Re: [PATCH v1] 2: Uboot RNG Driver using Data Co-processor

Please see the comment inline.

Regards,
Kshitiz

-----Original Message-----
From: Heinrich Schuchardt <[email protected]>
Sent: Friday, September 9, 2022 1:00 AM
To: Kshitiz Varshney <[email protected]>
Cc: U-Boot Mailing List <[email protected]>; Horia Geanta <[email protected]>; Pankaj Gupta <[email protected]>; Varun Sethi <[email protected]>; Gaurav Jain <[email protected]>; Rahul Kumar Yadav <[email protected]>; Vabhav Sharma <[email protected]>; Sahil Malhotra <[email protected]>; Ye Li <[email protected]>; Stefano Babic <[email protected]>; Fabio Estevam <[email protected]>; Peng Fan <[email protected]>; Sughosh Ganu <[email protected]>; Simon Glass <[email protected]>
Subject: [EXT] Re: [PATCH v1] 2: Uboot RNG Driver using Data Co-processor

Caution: EXT Email

Quoted 12 lines · Simon Glass

Hi Kshitiz,

Quoted 9 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit introduces Random number generator to uboot. It uses DCP driver for number generation.
RNG driver can be invoked by using below command on uboot prompt:-
rng <number of bytes>

CC the maintainer for RNG Sughosh Ganu <[email protected]>.

Will take care of this in next patch.

Quoted 20 lines
Quoted 7 lines
---
  drivers/crypto/fsl/Kconfig   |  10 ++
  drivers/crypto/fsl/Makefile  |   1 +
  drivers/crypto/fsl/dcp_rng.c | 184 +++++++++++++++++++++++++++++++++++
  3 files changed, 195 insertions(+)

create mode 100644 drivers/crypto/fsl/dcp_rng.c

but please see below

Quoted 8 lines
drivers/crypto/fsl/Kconfig +0 -0
index 702d204a3d..da5955e31d 100644
@@ -96,3 +96,13 @@ config RNG_SELF_TEST
          must be run before running any RNG based crypto implementation.
 
Quoted 10 lines

drivers/crypto/fsl/Kconfig config RNG_SELF_TEST

Quoted 10 lines
drivers/crypto/fsl/Kconfig +9 -0
  endif
+
+config FSL_DCP_RNG
+       bool "Enable Random Number Generator support"
+       depends on DM_RNG
+       default n
+       help
+       Enable support for the hardware based random number generator
+       module of the DCP.It uses the True Random Number Generator 
+(TRNG)

Please, use the same indentation as the other Kconfig entries in this
file: Add two space in each line of the help text.

Done

Quoted 94 lines

Space before It

Quoted 29 lines
drivers/crypto/fsl/Kconfig +2 -0
+       and a Pseudo-Random Number Generator (PRNG) to achieve a true
+       randomness and cryptographic strength.
(quoted diff) +0 -0

b/drivers/crypto/fsl/Makefile index 926300e2ab..c653208d23 100644

drivers/crypto/fsl/Makefile +1 -1
@@ -7,6 +7,7 @@ obj-$(CONFIG_FSL_CAAM) += jr.o fsl_hash.o jobdesc.o error.o
  obj-$(CONFIG_FSL_BLOB) += fsl_blob.o
  obj-$(CONFIG_RSA_FREESCALE_EXP) += fsl_rsa.o
  obj-$(CONFIG_FSL_CAAM_RNG) += rng.o
+obj-$(CONFIG_FSL_DCP_RNG) += dcp_rng.o
  obj-$(CONFIG_IMX_CAAM_MFG_PROT) += fsl_mfgprot.o
  obj-$(CONFIG_RNG_SELF_TEST) += rng_self_test.o
  obj-$(CONFIG_CMD_PROVISION_KEY) += fsl_aes.o tag_object.o diff 
--git a/drivers/crypto/fsl/dcp_rng.c b/drivers/crypto/fsl/dcp_rng.c 

new file mode 100644 index 0000000000..a797710c2e

drivers/crypto/fsl/dcp_rng.c new file +10 -0
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * RNG driver for Freescale RNGC
+ *
+ * Copyright (C) 2008-2012 Freescale Semiconductor, Inc.
+ * Copyright (C) 2017 Martin Kaiser <[email protected]>
+ *     Copyright 2022 NXP
+ *
+ * Based on RNGC driver in drivers/char/hw_random/imx-rngc.c in 
+Linux  */

drivers/crypto/fsl/dcp_rng.c line 20

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+
+#include <asm/cache.h>
+#include <common.h>
+#include <cpu_func.h>
+#include <dm.h>
+#include <rng.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <asm/io.h>
+#include <dm/root.h>

Should be:

Quoted 27 lines
drivers/crypto/fsl/dcp_rng.c new file +25 -0
+#include <common.h>
+#include <cpu_func.h>
+#include <dm.h>
+#include <rng.h>
+#include <asm/cache.h>
+#include <asm/io.h>
+#include <dm/root.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
 
 
+
+#define DCP_RNG_MAX_FIFO_STORE_SIZE    4
+#define RNGC_VER_ID                    0x0000
+#define RNGC_COMMAND                   0x0004
+#define RNGC_CONTROL                   0x0008
+#define RNGC_STATUS                    0x000C
+#define RNGC_ERROR                     0x0010
+#define RNGC_FIFO                      0x0014
+
+/* the fields in the ver id register */
+#define RNGC_TYPE_SHIFT                        28
+
+/* the rng_type field */
+#define RNGC_TYPE_RNGB                 0x1
+#define RNGC_TYPE_RNGC                 0x2
+

drivers/crypto/fsl/dcp_rng.c line 46

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+#define RNGC_CMD_CLR_ERR               0x00000020
+#define RNGC_CMD_SEED                  0x00000002
+
+#define RNGC_CTRL_AUTO_SEED            0x00000010
+
+#define RNGC_STATUS_ERROR              0x00010000
+#define RNGC_STATUS_FIFO_LEVEL_MASK    0x00000f00
+#define RNGC_STATUS_FIFO_LEVEL_SHIFT   8
+#define RNGC_STATUS_SEED_DONE          0x00000020
+#define RNGC_STATUS_ST_DONE            0x00000010

Why all the leading zeroes?

drivers/crypto/fsl/dcp_rng.c line 52

Quoted 6 lines
drivers/crypto/fsl/dcp_rng.c new file +6 -0
+
+#define RNGC_ERROR_STATUS_STAT_ERR     0x00000008
+
+#define RNGC_TIMEOUT                   3000000U /* 3 sec */
+
+struct imx_rngc {

Normally the priv data should have a _priv suffix.

Quoted 7 lines

drivers/crypto/fsl/dcp_rng.c line 58

Quoted 5 lines
drivers/crypto/fsl/dcp_rng.c new file +5 -0
+       unsigned long base;
+};
+
+static int rngc_read(struct udevice *dev, void *data, size_t len) {
+       struct imx_rngc *rngc = dev_get_priv(dev);

Normally the var should be called priv

This is described in doc/develop/codingstyle.rst line 192ff.

Quoted 38 lines
Quoted 38 lines
drivers/crypto/fsl/dcp_rng.c new file +37 -0
+       u8 buffer[DCP_RNG_MAX_FIFO_STORE_SIZE];
+       u32 status, level;
+       size_t size;
+
+       while (len) {
+               status = readl(rngc->base + RNGC_STATUS);
+
+               /* is there some error while reading this random number? */
+               if (status & RNGC_STATUS_ERROR)
+                       break;
+               /* how many random numbers are in FIFO? [0-16] */
+               level = (status & RNGC_STATUS_FIFO_LEVEL_MASK) >>
+                       RNGC_STATUS_FIFO_LEVEL_SHIFT;
+
+               if (level) {
+                       /* retrieve a random number from FIFO */
+                       *(u32 *)buffer = readl(rngc->base + RNGC_FIFO);
+                       size = min(len, sizeof(u32));
+                       memcpy(data, buffer, size);
+                       data += size;
+                       len -= size;
+               }
+       }
+
+       return len ? -EIO : 0;
+}
+
+static int rngc_init(struct imx_rngc *rngc) {
+       u32 cmd, ctrl, status, err_reg = 0;
+       unsigned long long timeval = 0;
+       unsigned long long timeout = RNGC_TIMEOUT;
+
+       /* clear error */
+       cmd = readl(rngc->base + RNGC_COMMAND);
+       writel(cmd | RNGC_CMD_CLR_ERR, rngc->base + RNGC_COMMAND);
+
+       /* create seed, repeat while there is some statistical error */
Quoted 8 lines

drivers/crypto/fsl/dcp_rng.c line 103

Quoted 8 lines
drivers/crypto/fsl/dcp_rng.c new file +8 -0
+       do {
+               /* seed creation */
+               cmd = readl(rngc->base + RNGC_COMMAND);
+               writel(cmd | RNGC_CMD_SEED, rngc->base + 
+ RNGC_COMMAND);
+
+               udelay(1);
+               timeval += 1;

As this loop can take rather long, should we call WATCHDOG_RESET() before and after the loop?

Otherwise looks good to me.

This is in reference with imx-rngc.c in kernel code.
See this link for more info:- https://elixir.bootlin.com/linux/latest/source/drivers/char/hw_random/imx-rngc.c

Quoted 88 lines
Quoted 13 lines
drivers/crypto/fsl/dcp_rng.c new file +13 -0
+
+               status = readl(rngc->base + RNGC_STATUS);
+               err_reg = readl(rngc->base + RNGC_ERROR);
+
+               if (status & (RNGC_STATUS_SEED_DONE | RNGC_STATUS_ST_DONE))
+                       break;
+
+               if (timeval > timeout) {
+                       debug("rngc timed out\n");
+                       return -ETIMEDOUT;
+               }
+       } while (err_reg == RNGC_ERROR_STATUS_STAT_ERR);
+

drivers/crypto/fsl/dcp_rng.c line 126

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+       if (err_reg)
+               return -EIO;
+
+       /*
+        * enable automatic seeding, the rngc creates a new seed automatically
+        * after serving 2^20 random 160-bit words
+        */
+       ctrl = readl(rngc->base + RNGC_CONTROL);
+       ctrl |= RNGC_CTRL_AUTO_SEED;
+       writel(ctrl, rngc->base + RNGC_CONTROL);

setbits_le32(rngc->base + RNGC_CONTROL, RNGC_CTRL_AUTO_SEED);

Quoted 57 lines
drivers/crypto/fsl/dcp_rng.c new file +56 -1
+       return 0;
+}
+
+static int rngc_probe(struct udevice *dev) {
+       struct imx_rngc *rngc = dev_get_priv(dev);
+       fdt_addr_t addr;
+       u32 ver_id;
+       u8  rng_type;
+       int ret;
+
+       addr = dev_read_addr(dev);
+       if (addr == FDT_ADDR_T_NONE) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       rngc->base = addr;
+       ver_id = readl(rngc->base + RNGC_VER_ID);
+       rng_type = ver_id >> RNGC_TYPE_SHIFT;
+       /*
+        * This driver supports only RNGC and RNGB. (There's a different
+        * driver for RNGA.)
+        */
+       if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) {
+               ret = -ENODEV;
+               goto err;
+       }
+
+       ret = rngc_init(rngc);
+       if (ret)
+               goto err;
+
+       return 0;
+
+err:
+       printf("%s error = %d\n", __func__, ret);
+       return ret;
+}
+
+static const struct dm_rng_ops rngc_ops = {
+       .read = rngc_read,
+};
+
+static const struct udevice_id rngc_dt_ids[] = {
+       { .compatible = "fsl,imx25-rngb" },
+       { }
+};
+
+U_BOOT_DRIVER(dcp_rng) = {
+       .name = "dcp_rng",
+       .id = UCLASS_RNG,
+       .of_match = rngc_dt_ids,
+       .ops = &rngc_ops,
+       .probe = rngc_probe,
+       .priv_auto  = sizeof(struct imx_rngc),
+       .flags = DM_FLAG_ALLOC_PRIV_DMA, };
--
Quoted 2 lines

2.25.1

Regards,
Simon

author Reviewed-by Simon Glass
#9 lore raw

RE: [EXT] Re: [PATCH v1] 2: Uboot RNG Driver using Data Co-processor

In reply to Simon Glass

Regards,

Kshitiz

-----Original Message-----
From: Simon Glass <[email protected]>
Sent: Thursday, September 8, 2022 11:49 PM
To: Kshitiz Varshney <[email protected]>
Cc: U-Boot Mailing List <[email protected]>; Horia Geanta <[email protected]>; Pankaj Gupta <[email protected]>; Varun Sethi <[email protected]>; Gaurav Jain <[email protected]>; Rahul Kumar Yadav <[email protected]>; Vabhav Sharma <[email protected]>; Sahil Malhotra <[email protected]>; Ye Li <[email protected]>; Heinrich Schuchardt <[email protected]>; Stefano Babic <[email protected]>; Fabio Estevam <[email protected]>; Peng Fan <[email protected]>
Subject: [EXT] Re: [PATCH v1] 2: Uboot RNG Driver using Data Co-processor

Caution: EXT Email

Hi Kshitiz,

Quoted 10 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit introduces Random number generator to uboot. It uses DCP driver for number generation.
RNG driver can be invoked by using below command on uboot prompt:-
rng <number of bytes>

Quoted 7 lines
---
 drivers/crypto/fsl/Kconfig   |  10 ++
 drivers/crypto/fsl/Makefile  |   1 +
 drivers/crypto/fsl/dcp_rng.c | 184 
+++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+)
 create mode 100644 drivers/crypto/fsl/dcp_rng.c

but please see below

Quoted 8 lines
drivers/crypto/fsl/Kconfig +0 -0
index 702d204a3d..da5955e31d 100644
@@ -96,3 +96,13 @@ config RNG_SELF_TEST
         must be run before running any RNG based crypto implementation.
 

drivers/crypto/fsl/Kconfig config RNG_SELF_TEST · line 107

Quoted 10 lines
drivers/crypto/fsl/Kconfig +9 -0
 endif
+
+config FSL_DCP_RNG
+       bool "Enable Random Number Generator support"
+       depends on DM_RNG
+       default n
+       help
+       Enable support for the hardware based random number generator
+       module of the DCP.It uses the True Random Number Generator 
+(TRNG)

Space before It

Quoted 30 lines
drivers/crypto/fsl/Kconfig +2 -0
+       and a Pseudo-Random Number Generator (PRNG) to achieve a true
+       randomness and cryptographic strength.
drivers/crypto/fsl/Makefile +0 -0
index 926300e2ab..c653208d23 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_FSL_CAAM) += jr.o fsl_hash.o jobdesc.o 

error.o
obj-$(CONFIG_FSL_BLOB) += fsl_blob.o obj-$(CONFIG_RSA_FREESCALE_EXP) += fsl_rsa.o obj-$(CONFIG_FSL_CAAM_RNG) += rng.o
+obj-$(CONFIG_FSL_DCP_RNG) += dcp_rng.o
obj-$(CONFIG_IMX_CAAM_MFG_PROT) += fsl_mfgprot.o obj-$(CONFIG_RNG_SELF_TEST) += rng_self_test.o obj-$(CONFIG_CMD_PROVISION_KEY) += fsl_aes.o tag_object.o diff --git
a/drivers/crypto/fsl/dcp_rng.c b/drivers/crypto/fsl/dcp_rng.c new file mode 100644 index 0000000000..a797710c2e

drivers/crypto/fsl/dcp_rng.c new file +10 -0
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * RNG driver for Freescale RNGC
+ *
+ * Copyright (C) 2008-2012 Freescale Semiconductor, Inc.
+ * Copyright (C) 2017 Martin Kaiser <[email protected]>
+ *     Copyright 2022 NXP
+ *
+ * Based on RNGC driver in drivers/char/hw_random/imx-rngc.c in Linux  
+*/

drivers/crypto/fsl/dcp_rng.c line 20

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+
+#include <asm/cache.h>
+#include <common.h>
+#include <cpu_func.h>
+#include <dm.h>
+#include <rng.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <asm/io.h>
+#include <dm/root.h>

Should be:

drivers/crypto/fsl/dcp_rng.c

Quoted 9 lines
drivers/crypto/fsl/dcp_rng.c new file +9 -0
+#include <common.h>
+#include <cpu_func.h>
+#include <dm.h>
+#include <rng.h>
+#include <asm/cache.h>
+#include <asm/io.h>
+#include <dm/root.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>

Taken care of this in next patch.

Quoted 16 lines
drivers/crypto/fsl/dcp_rng.c new file +16 -0
+
+#define DCP_RNG_MAX_FIFO_STORE_SIZE    4
+#define RNGC_VER_ID                    0x0000
+#define RNGC_COMMAND                   0x0004
+#define RNGC_CONTROL                   0x0008
+#define RNGC_STATUS                    0x000C
+#define RNGC_ERROR                     0x0010
+#define RNGC_FIFO                      0x0014
+
+/* the fields in the ver id register */
+#define RNGC_TYPE_SHIFT                        28
+
+/* the rng_type field */
+#define RNGC_TYPE_RNGB                 0x1
+#define RNGC_TYPE_RNGC                 0x2
+

drivers/crypto/fsl/dcp_rng.c line 46

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+#define RNGC_CMD_CLR_ERR               0x00000020
+#define RNGC_CMD_SEED                  0x00000002
+
+#define RNGC_CTRL_AUTO_SEED            0x00000010
+
+#define RNGC_STATUS_ERROR              0x00010000
+#define RNGC_STATUS_FIFO_LEVEL_MASK    0x00000f00
+#define RNGC_STATUS_FIFO_LEVEL_SHIFT   8
+#define RNGC_STATUS_SEED_DONE          0x00000020
+#define RNGC_STATUS_ST_DONE            0x00000010

Why all the leading zeroes?
Removed all the leading zeroes in next patch.

drivers/crypto/fsl/dcp_rng.c line 52

Quoted 6 lines
drivers/crypto/fsl/dcp_rng.c new file +6 -0
+
+#define RNGC_ERROR_STATUS_STAT_ERR     0x00000008
+
+#define RNGC_TIMEOUT                   3000000U /* 3 sec */
+
+struct imx_rngc {

Normally the priv data should have a _priv suffix. Done

drivers/crypto/fsl/dcp_rng.c line 58

Quoted 5 lines
drivers/crypto/fsl/dcp_rng.c new file +5 -0
+       unsigned long base;
+};
+
+static int rngc_read(struct udevice *dev, void *data, size_t len) {
+       struct imx_rngc *rngc = dev_get_priv(dev);

Normally the var should be called priv
Done

Quoted 58 lines
drivers/crypto/fsl/dcp_rng.c new file +58 -0
+       u8 buffer[DCP_RNG_MAX_FIFO_STORE_SIZE];
+       u32 status, level;
+       size_t size;
+
+       while (len) {
+               status = readl(rngc->base + RNGC_STATUS);
+
+               /* is there some error while reading this random number? */
+               if (status & RNGC_STATUS_ERROR)
+                       break;
+               /* how many random numbers are in FIFO? [0-16] */
+               level = (status & RNGC_STATUS_FIFO_LEVEL_MASK) >>
+                       RNGC_STATUS_FIFO_LEVEL_SHIFT;
+
+               if (level) {
+                       /* retrieve a random number from FIFO */
+                       *(u32 *)buffer = readl(rngc->base + RNGC_FIFO);
+                       size = min(len, sizeof(u32));
+                       memcpy(data, buffer, size);
+                       data += size;
+                       len -= size;
+               }
+       }
+
+       return len ? -EIO : 0;
+}
+
+static int rngc_init(struct imx_rngc *rngc) {
+       u32 cmd, ctrl, status, err_reg = 0;
+       unsigned long long timeval = 0;
+       unsigned long long timeout = RNGC_TIMEOUT;
+
+       /* clear error */
+       cmd = readl(rngc->base + RNGC_COMMAND);
+       writel(cmd | RNGC_CMD_CLR_ERR, rngc->base + RNGC_COMMAND);
+
+       /* create seed, repeat while there is some statistical error */
+       do {
+               /* seed creation */
+               cmd = readl(rngc->base + RNGC_COMMAND);
+               writel(cmd | RNGC_CMD_SEED, rngc->base + 
+ RNGC_COMMAND);
+
+               udelay(1);
+               timeval += 1;
+
+               status = readl(rngc->base + RNGC_STATUS);
+               err_reg = readl(rngc->base + RNGC_ERROR);
+
+               if (status & (RNGC_STATUS_SEED_DONE | RNGC_STATUS_ST_DONE))
+                       break;
+
+               if (timeval > timeout) {
+                       debug("rngc timed out\n");
+                       return -ETIMEDOUT;
+               }
+       } while (err_reg == RNGC_ERROR_STATUS_STAT_ERR);
+

drivers/crypto/fsl/dcp_rng.c line 126

Quoted 10 lines
drivers/crypto/fsl/dcp_rng.c new file +10 -0
+       if (err_reg)
+               return -EIO;
+
+       /*
+        * enable automatic seeding, the rngc creates a new seed automatically
+        * after serving 2^20 random 160-bit words
+        */
+       ctrl = readl(rngc->base + RNGC_CONTROL);
+       ctrl |= RNGC_CTRL_AUTO_SEED;
+       writel(ctrl, rngc->base + RNGC_CONTROL);

setbits_le32(rngc->base + RNGC_CONTROL, RNGC_CTRL_AUTO_SEED);

Quoted 57 lines
drivers/crypto/fsl/dcp_rng.c new file +56 -1
+       return 0;
+}
+
+static int rngc_probe(struct udevice *dev) {
+       struct imx_rngc *rngc = dev_get_priv(dev);
+       fdt_addr_t addr;
+       u32 ver_id;
+       u8  rng_type;
+       int ret;
+
+       addr = dev_read_addr(dev);
+       if (addr == FDT_ADDR_T_NONE) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       rngc->base = addr;
+       ver_id = readl(rngc->base + RNGC_VER_ID);
+       rng_type = ver_id >> RNGC_TYPE_SHIFT;
+       /*
+        * This driver supports only RNGC and RNGB. (There's a different
+        * driver for RNGA.)
+        */
+       if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) {
+               ret = -ENODEV;
+               goto err;
+       }
+
+       ret = rngc_init(rngc);
+       if (ret)
+               goto err;
+
+       return 0;
+
+err:
+       printf("%s error = %d\n", __func__, ret);
+       return ret;
+}
+
+static const struct dm_rng_ops rngc_ops = {
+       .read = rngc_read,
+};
+
+static const struct udevice_id rngc_dt_ids[] = {
+       { .compatible = "fsl,imx25-rngb" },
+       { }
+};
+
+U_BOOT_DRIVER(dcp_rng) = {
+       .name = "dcp_rng",
+       .id = UCLASS_RNG,
+       .of_match = rngc_dt_ids,
+       .ops = &rngc_ops,
+       .probe = rngc_probe,
+       .priv_auto  = sizeof(struct imx_rngc),
+       .flags = DM_FLAG_ALLOC_PRIV_DMA, };
--
Quoted 2 lines

2.25.1

Regards,
Simon

#10 lore raw

PATCH 1/1 4: Added configs required for dcp_rng driver

In reply to Kshitiz Varshney

From: Kshitiz <[email protected]>

This commit adds configs required for using dcp_rng driver in imx6ull defconfig files.

---
 configs/mx6ull_14x14_evk_defconfig        | 4 ++++
 configs/mx6ull_14x14_evk_emmc_defconfig   | 4 ++++
 configs/mx6ull_14x14_evk_nand_defconfig   | 4 ++++
 configs/mx6ull_14x14_evk_optee_defconfig  | 4 ++++
 configs/mx6ull_14x14_evk_plugin_defconfig | 4 ++++
 configs/mx6ull_14x14_evk_qspi1_defconfig  | 4 ++++
 configs/mx6ull_9x9_evk_defconfig          | 5 +++++
 configs/mx6ull_9x9_evk_plugin_defconfig   | 4 ++++
 configs/mx6ull_9x9_evk_qspi1_defconfig    | 4 ++++
 9 files changed, 37 insertions(+)
configs/mx6ull_14x14_evk_defconfig +4 -0
index cdcd496edd..79964b3ccf 100644
@@ -99,3 +99,7 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
configs/mx6ull_14x14_evk_emmc_defconfig +4 -0
index d94085e13b..f4bcc40544 100644
@@ -99,3 +99,7 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
configs/mx6ull_14x14_evk_nand_defconfig +4 -0
index 16900e04f0..1931293bc6 100644
@@ -106,3 +106,7 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
configs/mx6ull_14x14_evk_optee_defconfig +4 -0
index 2bb3d44fa8..ba1c8e02a4 100644
@@ -100,3 +100,7 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
configs/mx6ull_14x14_evk_plugin_defconfig +4 -0
index baab9c2ee8..9c1354981e 100644
@@ -100,3 +100,7 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
configs/mx6ull_14x14_evk_qspi1_defconfig +4 -0
index f1fe8b4000..13a1eec75e 100644
@@ -102,3 +102,7 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
configs/mx6ull_9x9_evk_defconfig +5 -0
index 66eacc55fa..3f73c5e697 100644
@@ -103,3 +103,8 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
configs/mx6ull_9x9_evk_plugin_defconfig +4 -0
index 7eace52b5c..41468f2d38 100644
@@ -104,3 +104,7 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
configs/mx6ull_9x9_evk_qspi1_defconfig +4 -0
index f145b65e6c..f1446ef8e0 100644
@@ -106,3 +106,7 @@ CONFIG_FASTBOOT_BUF_ADDR=0x83800000
 CONFIG_FASTBOOT_BUF_SIZE=0x40000000
 CONFIG_FASTBOOT_FLASH=y
 CONFIG_EFI_PARTITION=y
+CONFIG_ARCH_MISC_INIT=y
+CONFIG_DM_RNG=y
+CONFIG_CMD_RNG=y
+CONFIG_FSL_DCP_RNG=y
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#11 lore raw
In reply to Kshitiz Varshney
Quoted 1 line · Kshitiz Varshney

From: Kshitiz <[email protected]>

If you use format-patch HEAD~3, it will add numbers to the patches as
expected: [PATCH 1/3].

For patch series we typically generate a cover-letter.

Quoted 15 lines

Added entry for rngb in imx6ull device tree which is required for Random number generation in u-boot.

---
  arch/arm/dts/imx6ull.dtsi | 6 ++++++
  1 file changed, 6 insertions(+)
arch/arm/dts/imx6ull.dtsi +0 -0
index 46e7ad6bab..c5d5a5ab7b 100644

arch/arm/dts/imx6ull.dtsi line 74

Quoted 9 lines
arch/arm/dts/imx6ull.dtsi +6 -0
@@ -66,6 +66,12 @@
  				clocks = <&clks IMX6ULL_CLK_DCP_CLK>;
  				clock-names = "dcp";
  			};
+			rngb: rng@2284000 {
+				compatible = "fsl,imx6ull-rngb", "fsl,imx25-rngb";
+				reg = <0x02284000 0x4000>;
+				interrupts = <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>;
+				clocks = <&clks IMX6UL_CLK_DUMMY>;
+			};

This seems to match Linux' arch/arm/boot/dts/imx6ull.dtsi and Documentation/devicetree/bindings/rng/imx-rng.yaml.

Quoted 3 lines
arch/arm/dts/imx6ull.dtsi +0 -0
  			iomuxc_snvs: iomuxc-snvs@2290000 {
  				compatible = "fsl,imx6ull-iomuxc-snvs";
author Acked-by Heinrich Schuchardt
#12 lore raw

-----Original Message-----
From: Heinrich Schuchardt <[email protected]>
Sent: Friday, September 9, 2022 1:10 AM
To: Kshitiz Varshney <[email protected]>
Cc: Stefano Babic <[email protected]>; Fabio Estevam <[email protected]>; Peng Fan <[email protected]>; Sughosh Ganu <[email protected]>; [email protected]; Ye Li <[email protected]>; Gaurav Jain <[email protected]>; Rahul Kumar Yadav <[email protected]>; Vabhav Sharma <[email protected]>; Varun Sethi <[email protected]>; Sahil Malhotra <[email protected]>; Pankaj Gupta <[email protected]>; Horia Geanta <[email protected]>
Subject: [EXT] Re: [PATCH v1] 1: Adding rngb entry in imx6ull device tree

Caution: EXT Email

Quoted 1 line · Kshitiz Varshney

From: Kshitiz <[email protected]>

If you use format-patch HEAD~3, it will add numbers to the patches as
expected: [PATCH 1/3].

For patch series we typically generate a cover-letter.

Will take care in next patch set.

Quoted 15 lines

Added entry for rngb in imx6ull device tree which is required for Random number generation in u-boot.

---
  arch/arm/dts/imx6ull.dtsi | 6 ++++++
  1 file changed, 6 insertions(+)
arch/arm/dts/imx6ull.dtsi +0 -0
index 46e7ad6bab..c5d5a5ab7b 100644

arch/arm/dts/imx6ull.dtsi line 74

Quoted 9 lines
arch/arm/dts/imx6ull.dtsi +6 -0
@@ -66,6 +66,12 @@
                              clocks = <&clks IMX6ULL_CLK_DCP_CLK>;
                              clock-names = "dcp";
                      };
+                     rngb: rng@2284000 {
+                             compatible = "fsl,imx6ull-rngb", "fsl,imx25-rngb";
+                             reg = <0x02284000 0x4000>;
+                             interrupts = <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>;
+                             clocks = <&clks IMX6UL_CLK_DUMMY>;
+                     };

This seems to match Linux' arch/arm/boot/dts/imx6ull.dtsi and Documentation/devicetree/bindings/rng/imx-rng.yaml.

As, this node entry is already present in upstream uboot. Hence, removed this patch from new patchset.

Quoted 3 lines
arch/arm/dts/imx6ull.dtsi +0 -0
                      iomuxc_snvs: iomuxc-snvs@2290000 {
                              compatible = "fsl,imx6ull-iomuxc-snvs";
#13 lore raw
In reply to Kshitiz Varshney
Quoted 4 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

Added entry for rngb in imx6ull device tree which is required for Random number generation in u-boot.

Please sync with linux dts, not directly add a node to dtsi

Regards,
Peng.

Quoted 24 lines
---
  arch/arm/dts/imx6ull.dtsi | 6 ++++++
  1 file changed, 6 insertions(+)
arch/arm/dts/imx6ull.dtsi +6 -0
index 46e7ad6bab..c5d5a5ab7b 100644
@@ -66,6 +66,12 @@
  				clocks = <&clks IMX6ULL_CLK_DCP_CLK>;
  				clock-names = "dcp";
  			};
+			rngb: rng@2284000 {
+				compatible = "fsl,imx6ull-rngb", "fsl,imx25-rngb";
+				reg = <0x02284000 0x4000>;
+				interrupts = <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>;
+				clocks = <&clks IMX6UL_CLK_DUMMY>;
+			};
 
  			iomuxc_snvs: iomuxc-snvs@2290000 {
  				compatible = "fsl,imx6ull-iomuxc-snvs";
#14 lore raw

-----Original Message-----
From: Peng Fan (OSS) <[email protected]>
Sent: Monday, September 12, 2022 1:01 PM
To: Kshitiz Varshney <[email protected]>; [email protected]; Horia Geanta <[email protected]>; Pankaj Gupta <[email protected]>; Varun Sethi <[email protected]>; Gaurav Jain <[email protected]>; Rahul Kumar Yadav <[email protected]>; Vabhav Sharma <[email protected]>; Sahil Malhotra <[email protected]>; Ye Li <[email protected]>; Heinrich Schuchardt <[email protected]>
Cc: Stefano Babic <[email protected]>; Fabio Estevam <[email protected]>; Peng Fan <[email protected]>
Subject: Re: [PATCH v1] 1: Adding rngb entry in imx6ull device tree

Quoted 4 lines · Kshitiz Varshney

From: Kshitiz <[email protected]>

Added entry for rngb in imx6ull device tree which is required for Random number generation in u-boot.

Please sync with linux dts, not directly add a node to dtsi

Regards,
Peng.

I have removed this patch from new version of patches as this entry was already present in upstream u-boot.

Quoted 24 lines
---
  arch/arm/dts/imx6ull.dtsi | 6 ++++++
  1 file changed, 6 insertions(+)
arch/arm/dts/imx6ull.dtsi +6 -0
index 46e7ad6bab..c5d5a5ab7b 100644
@@ -66,6 +66,12 @@
  				clocks = <&clks IMX6ULL_CLK_DCP_CLK>;
  				clock-names = "dcp";
  			};
+			rngb: rng@2284000 {
+				compatible = "fsl,imx6ull-rngb", "fsl,imx25-rngb";
+				reg = <0x02284000 0x4000>;
+				interrupts = <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>;
+				clocks = <&clks IMX6UL_CLK_DUMMY>;
+			};
 
  			iomuxc_snvs: iomuxc-snvs@2290000 {
  				compatible = "fsl,imx6ull-iomuxc-snvs";