Skip to contentSkip to conversation

hwrng: npcm: add NPCM RNG driver support

PATCH Superseded Tomer Maimon 3 participants 12 messages Aug 28, 2019 – Sep 10, 2019

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tomer Maimon

author maintainer
#0 lore raw

cover hwrng: npcm: add NPCM RNG driver support

This patch set adds Randon Number Generator (RNG) support for the Nuvoton NPCM Baseboard Management Controller (BMC).

The RNG driver we use power consumption when the RNG is not required.

The NPCM RNG driver tested on NPCM750 evaluation board.

Tomer Maimon (2):
dt-binding: hwrng: add NPCM RNG documentation
hwrng: npcm: add NPCM RNG driver

 .../bindings/rng/nuvoton,npcm-rng.txt         |  17 ++
 drivers/char/hw_random/Kconfig                |  13 ++
 drivers/char/hw_random/Makefile               |   1 +
 drivers/char/hw_random/npcm-rng.c             | 207 ++++++++++++++++++
 4 files changed, 238 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt
 create mode 100644 drivers/char/hw_random/npcm-rng.c
Signature · 1 line
2.18.0
author maintainer
#1 lore raw

PATCH 1/2 dt-binding: hwrng: add NPCM RNG documentation

Added device tree binding documentation for Nuvoton BMC NPCM Random Number Generator (RNG).

---
 .../bindings/rng/nuvoton,npcm-rng.txt           | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +17 -0
new file mode 100644
index 000000000000..a697b4425fb3
@@ -0,0 +1,17 @@
+NPCM SoC Random Number Generator
+
+Required properties:
+- compatible  : "nuvoton,npcm750-rng" for the NPCM7XX BMC.
+- reg         : Specifies physical base address and size of the registers.
+
+Optional property:
+- quality : estimated number of bits of true entropy per 1024 bits
+			read from the rng.
+			If this property is not defined, it defaults to 1000.
+
+Example:
+
+rng: rng@f000b000 {
+	compatible = "nuvoton,npcm750-rng";
+	reg = <0xf000b000 0x8>;
+};
Signature · 1 line
2.18.0
#2 lore raw

Re: [PATCH v1 1/2] dt-binding: hwrng: add NPCM RNG documentation

Quoted 15 lines · Tomer Maimon

Added device tree binding documentation for Nuvoton BMC NPCM Random Number Generator (RNG).

---
 .../bindings/rng/nuvoton,npcm-rng.txt           | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +0 -0
new file mode 100644
index 000000000000..a697b4425fb3

Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt line 10

Quoted 10 lines
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +10 -0
@@ -0,0 +1,17 @@
+NPCM SoC Random Number Generator
+
+Required properties:
+- compatible  : "nuvoton,npcm750-rng" for the NPCM7XX BMC.
+- reg         : Specifies physical base address and size of the registers.
+
+Optional property:
+- quality : estimated number of bits of true entropy per 1024 bits
+			read from the rng.
+			If this property is not defined, it defaults to 1000.

Having a controllable quality implies that the numeric quality of the peripheral changes when it is stamped out on different SoCs (otherwise the driver can confidently set the quality without needing any hint from the DT). Is that really true here?

Daniel.

Quoted 10 lines
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +7 -0
+
+Example:
+
+rng: rng@f000b000 {
+	compatible = "nuvoton,npcm750-rng";
+	reg = <0xf000b000 0x8>;
+};
Signature · 1 line
2.18.0
author maintainer
#3 lore raw

PATCH 2/2 hwrng: npcm: add NPCM RNG driver

In reply to Tomer Maimon

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---
 drivers/char/hw_random/Kconfig    |  13 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/npcm-rng.c | 207 ++++++++++++++++++++++++++++++
 3 files changed, 221 insertions(+)
 create mode 100644 drivers/char/hw_random/npcm-rng.c
drivers/char/hw_random/Kconfig +13 -0
index 59f25286befe..87a1c30e7958 100644
@@ -440,6 +440,19 @@ config HW_RANDOM_OPTEE
 
 	  If unsure, say Y.
 
+config HW_RANDOM_NPCM
+	tristate "NPCM Random Number Generator support"
+	depends on ARCH_NPCM || COMPILE_TEST
+	default HW_RANDOM
+	help
+ 	  This driver provides support for the Random Number
+	  Generator hardware available in Nuvoton NPCM SoCs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called npcm-rng.
+
+ 	  If unsure, say Y.
+
 endif # HW_RANDOM
 
 config UML_RANDOM
drivers/char/hw_random/Makefile +1 -0
index 7c9ef4a7667f..17b6d4e6d591 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_HW_RANDOM_MTK)	+= mtk-rng.o
 obj-$(CONFIG_HW_RANDOM_S390) += s390-trng.o
 obj-$(CONFIG_HW_RANDOM_KEYSTONE) += ks-sa-rng.o
 obj-$(CONFIG_HW_RANDOM_OPTEE) += optee-rng.o
+obj-$(CONFIG_HW_RANDOM_NPCM) += npcm-rng.o
drivers/char/hw_random/npcm-rng.c new file +207 -0

207 lines not on this page · raw message

Signature · 1 line
2.18.0
#4 lore raw

Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver

Quoted 32 lines · Tomer Maimon

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---
 drivers/char/hw_random/Kconfig    |  13 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/npcm-rng.c | 207 ++++++++++++++++++++++++++++++
 3 files changed, 221 insertions(+)
 create mode 100644 drivers/char/hw_random/npcm-rng.c
drivers/char/hw_random/npcm-rng.c new file +16 -0
new file mode 100644
index 000000000000..5b4b1b6cb362
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Nuvoton Technology corporation.
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/init.h>
+#include <linux/random.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/of_irq.h>
+#include <linux/pm_runtime.h>
+

drivers/char/hw_random/npcm-rng.c line 26

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+#define NPCM_RNGCS_REG		0x00	/* Control and status register */
+#define NPCM_RNGD_REG		0x04	/* Data register */
+#define NPCM_RNGMODE_REG	0x08	/* Mode register */
+
+#define NPCM_RNG_CLK_SET_25MHZ	GENMASK(4, 3) /* 20-25 MHz */
+#define NPCM_RNG_DATA_VALID	BIT(1)
+#define NPCM_RNG_ENABLE		BIT(0)
+#define NPCM_RNG_M1ROSEL	BIT(1)
+
+#define NPCM_RNG_TIMEOUT_POLL	20

Might be better to define this in real-world units (such as milliseconds) since the timeout is effectively the longest time the hardware can take to generate 4 bytes.

Quoted 36 lines
drivers/char/hw_random/npcm-rng.c new file +36 -0
+
+#define to_npcm_rng(p)	container_of(p, struct npcm_rng, rng)
+
+struct npcm_rng {
+	void __iomem *base;
+	struct hwrng rng;
+};
+
+static int npcm_rng_init(struct hwrng *rng)
+{
+	struct npcm_rng *priv = to_npcm_rng(rng);
+	u32 val;
+
+	val = readl(priv->base + NPCM_RNGCS_REG);
+	val |= NPCM_RNG_ENABLE;
+	writel(val, priv->base + NPCM_RNGCS_REG);
+
+	return 0;
+}
+
+static void npcm_rng_cleanup(struct hwrng *rng)
+{
+	struct npcm_rng *priv = to_npcm_rng(rng);
+	u32 val;
+
+	val = readl(priv->base + NPCM_RNGCS_REG);
+	val &= ~NPCM_RNG_ENABLE;
+	writel(val, priv->base + NPCM_RNGCS_REG);
+}
+
+static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+	struct npcm_rng *priv = to_npcm_rng(rng);
+	int timeout_cnt = 0;
+	int ready;
+

drivers/char/hw_random/npcm-rng.c line 72

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+	ready = readl(priv->base + NPCM_RNGCS_REG) & NPCM_RNG_DATA_VALID;
+	while ((ready == 0) && (timeout_cnt < NPCM_RNG_TIMEOUT_POLL)) {
+		usleep_range(500, 1000);
+		ready = readl(priv->base + NPCM_RNGCS_REG) &
+			NPCM_RNG_DATA_VALID;
+		timeout_cnt++;
+	}
+
+	return !!ready;
+}

This looks like an open-coded version of readl_poll_timeout()... better to use the library function.

Also the sleep looks a bit long to me. What is the generation rate of the peripheral? Most RNG drivers have short intervals between data generation so they use delays rather than sleeps (a.k.a. readl_poll_timeout_atomic() ).

drivers/char/hw_random/npcm-rng.c line 83

Quoted 11 lines
drivers/char/hw_random/npcm-rng.c new file +11 -0
+
+static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+	struct npcm_rng *priv = to_npcm_rng(rng);
+	int retval = 0;
+
+	pm_runtime_get_sync((struct device *)priv->rng.priv);
+
+	while (max >= sizeof(u32)) {
+		if (!npcm_rng_wait_ready(rng, wait))
+			break;

The code as currently written does not honour the wait parameter (e.g. it sleeps even when wait is false).

Quoted 38 lines
drivers/char/hw_random/npcm-rng.c new file +38 -0
+
+		*(u32 *)buf = readl(priv->base + NPCM_RNGD_REG);
+		retval += sizeof(u32);
+		buf += sizeof(u32);
+		max -= sizeof(u32);
+	}
+
+	pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
+	pm_runtime_put_sync_autosuspend((struct device *)priv->rng.priv);
+
+	return retval || !wait ? retval : -EIO;
+}
+
+static int npcm_rng_probe(struct platform_device *pdev)
+{
+	struct npcm_rng *priv;
+	struct resource *res;
+	u32 quality;
+	int ret;
+
+	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(priv->base))
+		return PTR_ERR(priv->base);
+
+	priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
+	priv->rng.init = npcm_rng_init;
+	priv->rng.cleanup = npcm_rng_cleanup;
+#endif
+	priv->rng.read = npcm_rng_read;
+	priv->rng.priv = (unsigned long)&pdev->dev;
+	if (of_property_read_u32(pdev->dev.of_node, "quality", &quality))
+		priv->rng.quality = 1000;

drivers/char/hw_random/npcm-rng.c line 131

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+	else
+		priv->rng.quality = quality;
+
+	writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
+#ifndef CONFIG_PM
+	writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);
+#else
+	writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
+	       priv->base + NPCM_RNGCS_REG);
+#endif

If this initialization was moved to npcm_rng_init() then there would be no need for the additional ifdefing. It would also get rid of the (potentially slow) readl calls on the PM wakeup path.

Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4 -0
+
+	ret = devm_hwrng_register(&pdev->dev, &priv->rng);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to register rng device: %d\n",

drivers/char/hw_random/npcm-rng.c line 145

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+			ret);
+		return ret;
+	}
+
+	dev_set_drvdata(&pdev->dev, priv);
+	pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
+	pm_runtime_use_autosuspend(&pdev->dev);
+	pm_runtime_enable(&pdev->dev);
+
+	dev_info(&pdev->dev, "Random Number Generator Probed\n");

Does the user need to know this every time we boot? There are lots of debug tools we can bring to bear if they are worried the device isn't probing.

Daniel.

Quoted 65 lines
drivers/char/hw_random/npcm-rng.c new file +62 -0
+
+	return 0;
+}
+
+static int npcm_rng_remove(struct platform_device *pdev)
+{
+	struct npcm_rng *priv = platform_get_drvdata(pdev);
+
+	hwrng_unregister(&priv->rng);
+	pm_runtime_disable(&pdev->dev);
+	pm_runtime_set_suspended(&pdev->dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int npcm_rng_runtime_suspend(struct device *dev)
+{
+	struct npcm_rng *priv = dev_get_drvdata(dev);
+
+	npcm_rng_cleanup(&priv->rng);
+
+	return 0;
+}
+
+static int npcm_rng_runtime_resume(struct device *dev)
+{
+	struct npcm_rng *priv = dev_get_drvdata(dev);
+
+	return npcm_rng_init(&priv->rng);
+}
+#endif
+
+static const struct dev_pm_ops npcm_rng_pm_ops = {
+	SET_RUNTIME_PM_OPS(npcm_rng_runtime_suspend,
+			   npcm_rng_runtime_resume, NULL)
+	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+				pm_runtime_force_resume)
+};
+
+static const struct of_device_id rng_dt_id[] = {
+	{ .compatible = "nuvoton,npcm750-rng",  },
+	{},
+};
+MODULE_DEVICE_TABLE(of, rng_dt_id);
+
+static struct platform_driver npcm_rng_driver = {
+	.driver = {
+		.name		= "npcm-rng",
+		.pm		= &npcm_rng_pm_ops,
+		.owner		= THIS_MODULE,
+		.of_match_table = of_match_ptr(rng_dt_id),
+	},
+	.probe		= npcm_rng_probe,
+	.remove		= npcm_rng_remove,
+};
+
+module_platform_driver(npcm_rng_driver);
+
+MODULE_DESCRIPTION("Nuvoton NPCM Random Number Generator Driver");
+MODULE_AUTHOR("Tomer Maimon <[email protected]>");
+MODULE_LICENSE("GPL v2");
Signature · 1 line
2.18.0
author maintainer
#5 lore raw

Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver

Hi Daniel,

appreciate your comments and sorry for the late reply

Quoted 100 lines · Daniel Thompson

Review

Quoted 11 lines · Tomer Maimon

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---
 drivers/char/hw_random/Kconfig    |  13 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/npcm-rng.c | 207 ++++++++++++++++++++++++++++++
 3 files changed, 221 insertions(+)
 create mode 100644 drivers/char/hw_random/npcm-rng.c
(quoted diff) +0 -0

b/drivers/char/hw_random/npcm-rng.c

Quoted 12 lines

new file mode 100644
index 000000000000..5b4b1b6cb362

drivers/char/hw_random/npcm-rng.c new file +7 -0
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Nuvoton Technology corporation.
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>

drivers/char/hw_random/npcm-rng.c line 17

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+#include <linux/init.h>
+#include <linux/random.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/of_irq.h>
+#include <linux/pm_runtime.h>
+
+#define NPCM_RNGCS_REG               0x00    /* Control and status

register */

drivers/char/hw_random/npcm-rng.c line 26

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+#define NPCM_RNGD_REG                0x04    /* Data register */
+#define NPCM_RNGMODE_REG     0x08    /* Mode register */
+
+#define NPCM_RNG_CLK_SET_25MHZ       GENMASK(4, 3) /* 20-25 MHz */
+#define NPCM_RNG_DATA_VALID  BIT(1)
+#define NPCM_RNG_ENABLE              BIT(0)
+#define NPCM_RNG_M1ROSEL     BIT(1)
+
+#define NPCM_RNG_TIMEOUT_POLL        20

Might be better to define this in real-world units (such as milliseconds) since the timeout is effectively the longest time the hardware can take to generate 4 bytes.

Quoted 36 lines
drivers/char/hw_random/npcm-rng.c new file +36 -0
+
+#define to_npcm_rng(p)       container_of(p, struct npcm_rng, rng)
+
+struct npcm_rng {
+     void __iomem *base;
+     struct hwrng rng;
+};
+
+static int npcm_rng_init(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val |= NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+
+     return 0;
+}
+
+static void npcm_rng_cleanup(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val &= ~NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+}
+
+static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int timeout_cnt = 0;
+     int ready;
+

drivers/char/hw_random/npcm-rng.c line 72

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+     ready = readl(priv->base + NPCM_RNGCS_REG) & NPCM_RNG_DATA_VALID;
+     while ((ready == 0) && (timeout_cnt < NPCM_RNG_TIMEOUT_POLL)) {
+             usleep_range(500, 1000);
+             ready = readl(priv->base + NPCM_RNGCS_REG) &
+                     NPCM_RNG_DATA_VALID;
+             timeout_cnt++;
+     }
+
+     return !!ready;
+}

This looks like an open-coded version of readl_poll_timeout()... better to use the library function.

Quoted 6 lines

Also the sleep looks a bit long to me. What is the generation rate of the peripheral? Most RNG drivers have short intervals between data generation so they use delays rather than sleeps (a.k.a. readl_poll_timeout_atomic() ).

the HWRNG generate byte of random data in a few milliseconds so it is

better to use the sleep command.

Quoted 62 lines

drivers/char/hw_random/npcm-rng.c line 74

Quoted 3 lines
drivers/char/hw_random/npcm-rng.c new file +2 -0
+
+static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, bool

wait)

drivers/char/hw_random/npcm-rng.c line 83

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int retval = 0;
+
+     pm_runtime_get_sync((struct device *)priv->rng.priv);
+
+     while (max >= sizeof(u32)) {
+             if (!npcm_rng_wait_ready(rng, wait))
+                     break;

The code as currently written does not honour the wait parameter (e.g. it sleeps even when wait is false).

Quoted 44 lines
drivers/char/hw_random/npcm-rng.c new file +44 -0
+
+             *(u32 *)buf = readl(priv->base + NPCM_RNGD_REG);
+             retval += sizeof(u32);
+             buf += sizeof(u32);
+             max -= sizeof(u32);
+     }
+
+     pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
+     pm_runtime_put_sync_autosuspend((struct device *)priv->rng.priv);
+
+     return retval || !wait ? retval : -EIO;
+}
+
+static int npcm_rng_probe(struct platform_device *pdev)
+{
+     struct npcm_rng *priv;
+     struct resource *res;
+     u32 quality;
+     int ret;
+
+     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+     if (!priv)
+             return -ENOMEM;
+
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+     priv->base = devm_ioremap_resource(&pdev->dev, res);
+     if (IS_ERR(priv->base))
+             return PTR_ERR(priv->base);
+
+     priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
+     priv->rng.init = npcm_rng_init;
+     priv->rng.cleanup = npcm_rng_cleanup;
+#endif
+     priv->rng.read = npcm_rng_read;
+     priv->rng.priv = (unsigned long)&pdev->dev;
+     if (of_property_read_u32(pdev->dev.of_node, "quality", &quality))
+             priv->rng.quality = 1000;
+     else
+             priv->rng.quality = quality;
+
+     writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
+#ifndef CONFIG_PM
+     writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);
Quoted 9 lines

drivers/char/hw_random/npcm-rng.c line 131

Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4 -0
+#else
+     writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
+            priv->base + NPCM_RNGCS_REG);
+#endif

If this initialization was moved to npcm_rng_init() then there would be no need for the additional ifdefing. It would also get rid of the (potentially slow) readl calls on the PM wakeup path.

But when the Kernel have PM configuration than the priv->rng.init is not set and
*add_early_randomness* function is called. for the *add_early_randomness* success
the hwrng need to enabled in the probe.

I will remove the ifdef and add local variable instead (as Milton Miller suggested)

Quoted 80 lines
Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4 -0
+
+     ret = devm_hwrng_register(&pdev->dev, &priv->rng);
+     if (ret) {
+             dev_err(&pdev->dev, "Failed to register rng device: %d\n",

drivers/char/hw_random/npcm-rng.c line 145

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+                     ret);
+             return ret;
+     }
+
+     dev_set_drvdata(&pdev->dev, priv);
+     pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
+     pm_runtime_use_autosuspend(&pdev->dev);
+     pm_runtime_enable(&pdev->dev);
+
+     dev_info(&pdev->dev, "Random Number Generator Probed\n");

Does the user need to know this every time we boot? There are lots of debug tools we can bring to bear if they are worried the device isn't probing.

Daniel.

Quoted 57 lines
drivers/char/hw_random/npcm-rng.c new file +57 -0
+
+     return 0;
+}
+
+static int npcm_rng_remove(struct platform_device *pdev)
+{
+     struct npcm_rng *priv = platform_get_drvdata(pdev);
+
+     hwrng_unregister(&priv->rng);
+     pm_runtime_disable(&pdev->dev);
+     pm_runtime_set_suspended(&pdev->dev);
+
+     return 0;
+}
+
+#ifdef CONFIG_PM
+static int npcm_rng_runtime_suspend(struct device *dev)
+{
+     struct npcm_rng *priv = dev_get_drvdata(dev);
+
+     npcm_rng_cleanup(&priv->rng);
+
+     return 0;
+}
+
+static int npcm_rng_runtime_resume(struct device *dev)
+{
+     struct npcm_rng *priv = dev_get_drvdata(dev);
+
+     return npcm_rng_init(&priv->rng);
+}
+#endif
+
+static const struct dev_pm_ops npcm_rng_pm_ops = {
+     SET_RUNTIME_PM_OPS(npcm_rng_runtime_suspend,
+                        npcm_rng_runtime_resume, NULL)
+     SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+                             pm_runtime_force_resume)
+};
+
+static const struct of_device_id rng_dt_id[] = {
+     { .compatible = "nuvoton,npcm750-rng",  },
+     {},
+};
+MODULE_DEVICE_TABLE(of, rng_dt_id);
+
+static struct platform_driver npcm_rng_driver = {
+     .driver = {
+             .name           = "npcm-rng",
+             .pm             = &npcm_rng_pm_ops,
+             .owner          = THIS_MODULE,
+             .of_match_table = of_match_ptr(rng_dt_id),
+     },
+     .probe          = npcm_rng_probe,
+     .remove         = npcm_rng_remove,
+};
+
Quoted 9 lines
Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +5 -1
+module_platform_driver(npcm_rng_driver);
+
+MODULE_DESCRIPTION("Nuvoton NPCM Random Number Generator Driver");
+MODULE_AUTHOR("Tomer Maimon <[email protected]>");
+MODULE_LICENSE("GPL v2");
--

2.18.0

Thanks a lot,

Tomer

#6 lore raw

Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver

Quoted 107 lines · Tomer Maimon

Hi Daniel,

appreciate your comments and sorry for the late reply

Quoted 100 lines · Daniel Thompson

Review

Quoted 11 lines · Tomer Maimon

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---
 drivers/char/hw_random/Kconfig    |  13 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/npcm-rng.c | 207 ++++++++++++++++++++++++++++++
 3 files changed, 221 insertions(+)
 create mode 100644 drivers/char/hw_random/npcm-rng.c
(quoted diff) +0 -0

b/drivers/char/hw_random/npcm-rng.c

Quoted 12 lines

new file mode 100644
index 000000000000..5b4b1b6cb362

drivers/char/hw_random/npcm-rng.c new file +7 -0
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Nuvoton Technology corporation.
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>

drivers/char/hw_random/npcm-rng.c line 17

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+#include <linux/init.h>
+#include <linux/random.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/of_irq.h>
+#include <linux/pm_runtime.h>
+
+#define NPCM_RNGCS_REG               0x00    /* Control and status

register */

drivers/char/hw_random/npcm-rng.c line 26

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+#define NPCM_RNGD_REG                0x04    /* Data register */
+#define NPCM_RNGMODE_REG     0x08    /* Mode register */
+
+#define NPCM_RNG_CLK_SET_25MHZ       GENMASK(4, 3) /* 20-25 MHz */
+#define NPCM_RNG_DATA_VALID  BIT(1)
+#define NPCM_RNG_ENABLE              BIT(0)
+#define NPCM_RNG_M1ROSEL     BIT(1)
+
+#define NPCM_RNG_TIMEOUT_POLL        20

Might be better to define this in real-world units (such as milliseconds) since the timeout is effectively the longest time the hardware can take to generate 4 bytes.

Quoted 36 lines
drivers/char/hw_random/npcm-rng.c new file +36 -0
+
+#define to_npcm_rng(p)       container_of(p, struct npcm_rng, rng)
+
+struct npcm_rng {
+     void __iomem *base;
+     struct hwrng rng;
+};
+
+static int npcm_rng_init(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val |= NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+
+     return 0;
+}
+
+static void npcm_rng_cleanup(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val &= ~NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+}
+
+static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int timeout_cnt = 0;
+     int ready;
+

drivers/char/hw_random/npcm-rng.c line 72

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+     ready = readl(priv->base + NPCM_RNGCS_REG) & NPCM_RNG_DATA_VALID;
+     while ((ready == 0) && (timeout_cnt < NPCM_RNG_TIMEOUT_POLL)) {
+             usleep_range(500, 1000);
+             ready = readl(priv->base + NPCM_RNGCS_REG) &
+                     NPCM_RNG_DATA_VALID;
+             timeout_cnt++;
+     }
+
+     return !!ready;
+}

This looks like an open-coded version of readl_poll_timeout()... better to use the library function.

Quoted 7 lines
Quoted 4 lines

Also the sleep looks a bit long to me. What is the generation rate of the peripheral? Most RNG drivers have short intervals between data generation so they use delays rather than sleeps (a.k.a. readl_poll_timeout_atomic() ).

the HWRNG generate byte of random data in a few milliseconds so it is better to use the sleep command.

That's fine, just use readl_poll_timeout() then.

Quoted 71 lines
Quoted 61 lines

drivers/char/hw_random/npcm-rng.c line 74

Quoted 2 lines
drivers/char/hw_random/npcm-rng.c new file +2 -0
+
+static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, bool

wait)

drivers/char/hw_random/npcm-rng.c line 83

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int retval = 0;
+
+     pm_runtime_get_sync((struct device *)priv->rng.priv);
+
+     while (max >= sizeof(u32)) {
+             if (!npcm_rng_wait_ready(rng, wait))
+                     break;

The code as currently written does not honour the wait parameter (e.g. it sleeps even when wait is false).

Quoted 44 lines
drivers/char/hw_random/npcm-rng.c new file +44 -0
+
+             *(u32 *)buf = readl(priv->base + NPCM_RNGD_REG);
+             retval += sizeof(u32);
+             buf += sizeof(u32);
+             max -= sizeof(u32);
+     }
+
+     pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
+     pm_runtime_put_sync_autosuspend((struct device *)priv->rng.priv);
+
+     return retval || !wait ? retval : -EIO;
+}
+
+static int npcm_rng_probe(struct platform_device *pdev)
+{
+     struct npcm_rng *priv;
+     struct resource *res;
+     u32 quality;
+     int ret;
+
+     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+     if (!priv)
+             return -ENOMEM;
+
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+     priv->base = devm_ioremap_resource(&pdev->dev, res);
+     if (IS_ERR(priv->base))
+             return PTR_ERR(priv->base);
+
+     priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
+     priv->rng.init = npcm_rng_init;
+     priv->rng.cleanup = npcm_rng_cleanup;
+#endif
+     priv->rng.read = npcm_rng_read;
+     priv->rng.priv = (unsigned long)&pdev->dev;
+     if (of_property_read_u32(pdev->dev.of_node, "quality", &quality))
+             priv->rng.quality = 1000;
+     else
+             priv->rng.quality = quality;
+
+     writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
+#ifndef CONFIG_PM
+     writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);
Quoted 9 lines

drivers/char/hw_random/npcm-rng.c line 131

Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4 -0
+#else
+     writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
+            priv->base + NPCM_RNGCS_REG);
+#endif

If this initialization was moved to npcm_rng_init() then there would be no need for the additional ifdefing. It would also get rid of the (potentially slow) readl calls on the PM wakeup path.

Quoted 5 lines

But when the Kernel have PM configuration than the priv->rng.init is not set and
*add_early_randomness* function is called. for the *add_early_randomness* success
the hwrng need to enabled in the probe.

Sorry but I don't understand this reply.

When CONFIG_PM is enabled then the probe function does not currently set NPCM_RNG_ENABLE; instead is relies on npcm_rng_init() being called by the PM logic (as part of pm_runtime_get_sync() ).

Given the code *already* relies on npcm_rng_init() being called by the PM logic why does it matter if additional init is put there?

Daniel.

author maintainer
#7 lore raw

Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver

Hi Daniel,

Thanks for your prompt reply,

Quoted 202 lines · Daniel Thompson
Quoted 5 lines · Tomer Maimon

Hi Daniel,

appreciate your comments and sorry for the late reply

On Thu, 29 Aug 2019 at 13:47, Daniel Thompson <

[email protected]>

Quoted 10 lines
Quoted 8 lines · wrote:
Quoted 7 lines · Tomer Maimon

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---
 drivers/char/hw_random/Kconfig    |  13 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/npcm-rng.c | 207

++++++++++++++++++++++++++++++

Quoted 71 lines
Quoted 71 lines

Review

Quoted 4 lines
 3 files changed, 221 insertions(+)
 create mode 100644 drivers/char/hw_random/npcm-rng.c
(quoted diff) +0 -0

b/drivers/char/hw_random/npcm-rng.c

Quoted 12 lines

new file mode 100644
index 000000000000..5b4b1b6cb362

drivers/char/hw_random/npcm-rng.c new file +7 -0
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Nuvoton Technology corporation.
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>

drivers/char/hw_random/npcm-rng.c line 17

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+#include <linux/init.h>
+#include <linux/random.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/of_irq.h>
+#include <linux/pm_runtime.h>
+
+#define NPCM_RNGCS_REG               0x00    /* Control and status

register */

drivers/char/hw_random/npcm-rng.c line 26

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+#define NPCM_RNGD_REG                0x04    /* Data register */
+#define NPCM_RNGMODE_REG     0x08    /* Mode register */
+
+#define NPCM_RNG_CLK_SET_25MHZ       GENMASK(4, 3) /* 20-25 MHz */
+#define NPCM_RNG_DATA_VALID  BIT(1)
+#define NPCM_RNG_ENABLE              BIT(0)
+#define NPCM_RNG_M1ROSEL     BIT(1)
+
+#define NPCM_RNG_TIMEOUT_POLL        20

Might be better to define this in real-world units (such as milliseconds) since the timeout is effectively the longest time the hardware can take to generate 4 bytes.

Quoted 29 lines
drivers/char/hw_random/npcm-rng.c new file +29 -0
+
+#define to_npcm_rng(p)       container_of(p, struct npcm_rng, rng)
+
+struct npcm_rng {
+     void __iomem *base;
+     struct hwrng rng;
+};
+
+static int npcm_rng_init(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val |= NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+
+     return 0;
+}
+
+static void npcm_rng_cleanup(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val &= ~NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+}
Quoted 8 lines
Quoted 8 lines

drivers/char/hw_random/npcm-rng.c line 66

Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +8 -0
+
+static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int timeout_cnt = 0;
+     int ready;
+
+     ready = readl(priv->base + NPCM_RNGCS_REG) &

NPCM_RNG_DATA_VALID;

Quoted 13 lines
Quoted 13 lines

drivers/char/hw_random/npcm-rng.c line 72

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+     while ((ready == 0) && (timeout_cnt < NPCM_RNG_TIMEOUT_POLL)) {
+             usleep_range(500, 1000);
+             ready = readl(priv->base + NPCM_RNGCS_REG) &
+                     NPCM_RNG_DATA_VALID;
+             timeout_cnt++;
+     }
+
+     return !!ready;
+}

This looks like an open-coded version of readl_poll_timeout()... better to use the library function.

Quoted 7 lines
Quoted 4 lines

Also the sleep looks a bit long to me. What is the generation rate of the peripheral? Most RNG drivers have short intervals between data generation so they use delays rather than sleeps (a.k.a. readl_poll_timeout_atomic() ).

the HWRNG generate byte of random data in a few milliseconds so it is better to use the sleep command.

That's fine, just use readl_poll_timeout() then.

Quoted 2 lines
Quoted 2 lines

drivers/char/hw_random/npcm-rng.c line 74

Quoted 2 lines
drivers/char/hw_random/npcm-rng.c new file +2 -0
+
+static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max,

bool

Quoted 15 lines
Quoted 15 lines

wait)

drivers/char/hw_random/npcm-rng.c line 83

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int retval = 0;
+
+     pm_runtime_get_sync((struct device *)priv->rng.priv);
+
+     while (max >= sizeof(u32)) {
+             if (!npcm_rng_wait_ready(rng, wait))
+                     break;

The code as currently written does not honour the wait parameter (e.g. it sleeps even when wait is false).

Quoted 9 lines
Quoted 9 lines

drivers/char/hw_random/npcm-rng.c

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+
+             *(u32 *)buf = readl(priv->base + NPCM_RNGD_REG);
+             retval += sizeof(u32);
+             buf += sizeof(u32);
+             max -= sizeof(u32);
+     }
+
+     pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
+     pm_runtime_put_sync_autosuspend((struct device

*)priv->rng.priv);

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/char/hw_random/npcm-rng.c new file +20 -0
+
+     return retval || !wait ? retval : -EIO;
+}
+
+static int npcm_rng_probe(struct platform_device *pdev)
+{
+     struct npcm_rng *priv;
+     struct resource *res;
+     u32 quality;
+     int ret;
+
+     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+     if (!priv)
+             return -ENOMEM;
+
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+     priv->base = devm_ioremap_resource(&pdev->dev, res);
+     if (IS_ERR(priv->base))
+             return PTR_ERR(priv->base);
+
Quoted 8 lines
Quoted 8 lines

drivers/char/hw_random/npcm-rng.c

Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +8 -0
+     priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
+     priv->rng.init = npcm_rng_init;
+     priv->rng.cleanup = npcm_rng_cleanup;
+#endif
+     priv->rng.read = npcm_rng_read;
+     priv->rng.priv = (unsigned long)&pdev->dev;
+     if (of_property_read_u32(pdev->dev.of_node, "quality",

&quality))

Quoted 17 lines
Quoted 7 lines
Quoted 7 lines
drivers/char/hw_random/npcm-rng.c new file +7 -0
+             priv->rng.quality = 1000;
+     else
+             priv->rng.quality = quality;
+
+     writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
+#ifndef CONFIG_PM
+     writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);
Quoted 9 lines

drivers/char/hw_random/npcm-rng.c line 131

Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4 -0
+#else
+     writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
+            priv->base + NPCM_RNGCS_REG);
+#endif

If this initialization was moved to npcm_rng_init() then there would be no need for the additional ifdefing. It would also get rid of the (potentially slow) readl calls on the PM wakeup path.

Quoted 5 lines

But when the Kernel have PM configuration than the priv->rng.init is not set and
*add_early_randomness* function is called. for the *add_early_randomness* success
the hwrng need to enabled in the probe.

Quoted 5 lines

Sorry but I don't understand this reply.

When CONFIG_PM is enabled then the probe function does not currently set NPCM_RNG_ENABLE; instead is relies on npcm_rng_init() being called by

Sorry maybe I miss understood, but when the CONFIG_PM enabled so the NPCM_RNG_ENABLE sets (the code use ifndef and not ifdef) *#ifndef CONFIG_PM*

       writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);

#else (*CONFIG_PM enabled*)
writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
priv->base + NPCM_RNGCS_REG);
#endif

And the hwrng needed to be enabled to run *add_early_randomness *function successfully.

If the hwrng driver will relay on PM logic to enable the hwrng will be disable when *add_early_randomness *function is called.

the PM logic (as part of pm_runtime_get_sync() ).

Quoted 7 lines

Given the code *already* relies on npcm_rng_init() being called by the PM logic why does it matter if additional init is put there?

Daniel.

Thanks,

Tomer

#8 lore raw

Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver

Quoted 226 lines · Tomer Maimon

Hi Daniel,

Thanks for your prompt reply,

Quoted 202 lines · Daniel Thompson
Quoted 5 lines · Tomer Maimon

Hi Daniel,

appreciate your comments and sorry for the late reply

On Thu, 29 Aug 2019 at 13:47, Daniel Thompson <

[email protected]>

Quoted 10 lines
Quoted 8 lines · wrote:
Quoted 7 lines · Tomer Maimon

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---
 drivers/char/hw_random/Kconfig    |  13 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/npcm-rng.c | 207

++++++++++++++++++++++++++++++

Quoted 71 lines
Quoted 71 lines

Review

Quoted 4 lines
 3 files changed, 221 insertions(+)
 create mode 100644 drivers/char/hw_random/npcm-rng.c
(quoted diff) +0 -0

b/drivers/char/hw_random/npcm-rng.c

Quoted 12 lines

new file mode 100644
index 000000000000..5b4b1b6cb362

drivers/char/hw_random/npcm-rng.c new file +7 -0
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Nuvoton Technology corporation.
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>

drivers/char/hw_random/npcm-rng.c line 17

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+#include <linux/init.h>
+#include <linux/random.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/of_irq.h>
+#include <linux/pm_runtime.h>
+
+#define NPCM_RNGCS_REG               0x00    /* Control and status

register */

drivers/char/hw_random/npcm-rng.c line 26

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+#define NPCM_RNGD_REG                0x04    /* Data register */
+#define NPCM_RNGMODE_REG     0x08    /* Mode register */
+
+#define NPCM_RNG_CLK_SET_25MHZ       GENMASK(4, 3) /* 20-25 MHz */
+#define NPCM_RNG_DATA_VALID  BIT(1)
+#define NPCM_RNG_ENABLE              BIT(0)
+#define NPCM_RNG_M1ROSEL     BIT(1)
+
+#define NPCM_RNG_TIMEOUT_POLL        20

Might be better to define this in real-world units (such as milliseconds) since the timeout is effectively the longest time the hardware can take to generate 4 bytes.

Quoted 29 lines
drivers/char/hw_random/npcm-rng.c new file +29 -0
+
+#define to_npcm_rng(p)       container_of(p, struct npcm_rng, rng)
+
+struct npcm_rng {
+     void __iomem *base;
+     struct hwrng rng;
+};
+
+static int npcm_rng_init(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val |= NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+
+     return 0;
+}
+
+static void npcm_rng_cleanup(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val &= ~NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+}
Quoted 8 lines
Quoted 8 lines

drivers/char/hw_random/npcm-rng.c line 66

Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +8 -0
+
+static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int timeout_cnt = 0;
+     int ready;
+
+     ready = readl(priv->base + NPCM_RNGCS_REG) &

NPCM_RNG_DATA_VALID;

Quoted 13 lines
Quoted 13 lines

drivers/char/hw_random/npcm-rng.c line 72

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+     while ((ready == 0) && (timeout_cnt < NPCM_RNG_TIMEOUT_POLL)) {
+             usleep_range(500, 1000);
+             ready = readl(priv->base + NPCM_RNGCS_REG) &
+                     NPCM_RNG_DATA_VALID;
+             timeout_cnt++;
+     }
+
+     return !!ready;
+}

This looks like an open-coded version of readl_poll_timeout()... better to use the library function.

Quoted 7 lines
Quoted 4 lines

Also the sleep looks a bit long to me. What is the generation rate of the peripheral? Most RNG drivers have short intervals between data generation so they use delays rather than sleeps (a.k.a. readl_poll_timeout_atomic() ).

the HWRNG generate byte of random data in a few milliseconds so it is better to use the sleep command.

That's fine, just use readl_poll_timeout() then.

Quoted 2 lines
Quoted 2 lines

drivers/char/hw_random/npcm-rng.c line 74

Quoted 2 lines
drivers/char/hw_random/npcm-rng.c new file +2 -0
+
+static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max,

bool

Quoted 15 lines
Quoted 15 lines

wait)

drivers/char/hw_random/npcm-rng.c line 83

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int retval = 0;
+
+     pm_runtime_get_sync((struct device *)priv->rng.priv);
+
+     while (max >= sizeof(u32)) {
+             if (!npcm_rng_wait_ready(rng, wait))
+                     break;

The code as currently written does not honour the wait parameter (e.g. it sleeps even when wait is false).

Quoted 9 lines
Quoted 9 lines

drivers/char/hw_random/npcm-rng.c

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+
+             *(u32 *)buf = readl(priv->base + NPCM_RNGD_REG);
+             retval += sizeof(u32);
+             buf += sizeof(u32);
+             max -= sizeof(u32);
+     }
+
+     pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
+     pm_runtime_put_sync_autosuspend((struct device

*)priv->rng.priv);

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/char/hw_random/npcm-rng.c new file +20 -0
+
+     return retval || !wait ? retval : -EIO;
+}
+
+static int npcm_rng_probe(struct platform_device *pdev)
+{
+     struct npcm_rng *priv;
+     struct resource *res;
+     u32 quality;
+     int ret;
+
+     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+     if (!priv)
+             return -ENOMEM;
+
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+     priv->base = devm_ioremap_resource(&pdev->dev, res);
+     if (IS_ERR(priv->base))
+             return PTR_ERR(priv->base);
+
Quoted 8 lines
Quoted 8 lines

drivers/char/hw_random/npcm-rng.c

Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +8 -0
+     priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
+     priv->rng.init = npcm_rng_init;
+     priv->rng.cleanup = npcm_rng_cleanup;
+#endif
+     priv->rng.read = npcm_rng_read;
+     priv->rng.priv = (unsigned long)&pdev->dev;
+     if (of_property_read_u32(pdev->dev.of_node, "quality",

&quality))

Quoted 17 lines
Quoted 7 lines
Quoted 7 lines
drivers/char/hw_random/npcm-rng.c new file +7 -0
+             priv->rng.quality = 1000;
+     else
+             priv->rng.quality = quality;
+
+     writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
+#ifndef CONFIG_PM
+     writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);
Quoted 9 lines

drivers/char/hw_random/npcm-rng.c line 131

Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4 -0
+#else
+     writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
+            priv->base + NPCM_RNGCS_REG);
+#endif

If this initialization was moved to npcm_rng_init() then there would be no need for the additional ifdefing. It would also get rid of the (potentially slow) readl calls on the PM wakeup path.

Quoted 5 lines

But when the Kernel have PM configuration than the priv->rng.init is not set and
*add_early_randomness* function is called. for the *add_early_randomness* success
the hwrng need to enabled in the probe.

Quoted 5 lines

Sorry but I don't understand this reply.

When CONFIG_PM is enabled then the probe function does not currently set NPCM_RNG_ENABLE; instead is relies on npcm_rng_init() being called by

Sorry maybe I miss understood, but when the CONFIG_PM enabled so the NPCM_RNG_ENABLE sets (the code use ifndef and not ifdef) *#ifndef CONFIG_PM*

       writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);

#else (*CONFIG_PM enabled*)
writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
priv->base + NPCM_RNGCS_REG);
#endif

Quoted 7 lines

And the hwrng needed to be enabled to run *add_early_randomness *function successfully.

If the hwrng driver will relay on PM logic to enable the hwrng will be disable when *add_early_randomness *function is called.

the PM logic (as part of pm_runtime_get_sync() ).

I ended up reading my mail out of order and replied to the v2 patch.

The question is *why* the driver doesn't resume properly when adding early randomness! I think it is because the hwrng_register() is being called before PM runtime is enabled.

Daniel.

author maintainer
#9 lore raw

Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver

Quoted 247 lines · Daniel Thompson
Quoted 47 lines · Tomer Maimon

Hi Daniel,

Thanks for your prompt reply,

On Mon, 9 Sep 2019 at 18:10, Daniel Thompson <[email protected]

Quoted 37 lines · wrote:
Quoted 5 lines · Tomer Maimon

Hi Daniel,

appreciate your comments and sorry for the late reply

On Thu, 29 Aug 2019 at 13:47, Daniel Thompson <

[email protected]>

Quoted 10 lines
Quoted 8 lines · wrote:
Quoted 7 lines · Tomer Maimon

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---
 drivers/char/hw_random/Kconfig    |  13 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/npcm-rng.c | 207

++++++++++++++++++++++++++++++

Quoted 19 lines
Quoted 19 lines

Review

Quoted 4 lines
 3 files changed, 221 insertions(+)
 create mode 100644 drivers/char/hw_random/npcm-rng.c
(quoted diff) +0 -0

b/drivers/char/hw_random/npcm-rng.c

Quoted 14 lines

new file mode 100644
index 000000000000..5b4b1b6cb362

drivers/char/hw_random/npcm-rng.c new file +9 -0
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Nuvoton Technology corporation.
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/init.h>
+#include <linux/random.h>
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/char/hw_random/npcm-rng.c

Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +8 -0
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/of_irq.h>
+#include <linux/pm_runtime.h>
+
+#define NPCM_RNGCS_REG               0x00    /* Control and

status

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

register */

drivers/char/hw_random/npcm-rng.c

Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4 -0
+#define NPCM_RNGD_REG                0x04    /* Data register */
+#define NPCM_RNGMODE_REG     0x08    /* Mode register */
+
+#define NPCM_RNG_CLK_SET_25MHZ       GENMASK(4, 3) /* 20-25 MHz

*/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/char/hw_random/npcm-rng.c line 26

Quoted 5 lines
drivers/char/hw_random/npcm-rng.c new file +5 -0
+#define NPCM_RNG_DATA_VALID  BIT(1)
+#define NPCM_RNG_ENABLE              BIT(0)
+#define NPCM_RNG_M1ROSEL     BIT(1)
+
+#define NPCM_RNG_TIMEOUT_POLL        20

Might be better to define this in real-world units (such as milliseconds) since the timeout is effectively the longest time the hardware can take to generate 4 bytes.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/char/hw_random/npcm-rng.c line 28

Quoted 2 lines
drivers/char/hw_random/npcm-rng.c new file +2 -0
+
+#define to_npcm_rng(p)       container_of(p, struct npcm_rng,

rng)

Quoted 36 lines
Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
drivers/char/hw_random/npcm-rng.c new file +27 -0
+
+struct npcm_rng {
+     void __iomem *base;
+     struct hwrng rng;
+};
+
+static int npcm_rng_init(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val |= NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+
+     return 0;
+}
+
+static void npcm_rng_cleanup(struct hwrng *rng)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     u32 val;
+
+     val = readl(priv->base + NPCM_RNGCS_REG);
+     val &= ~NPCM_RNG_ENABLE;
+     writel(val, priv->base + NPCM_RNGCS_REG);
+}
Quoted 8 lines
Quoted 8 lines

drivers/char/hw_random/npcm-rng.c line 66

Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +8 -0
+
+static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int timeout_cnt = 0;
+     int ready;
+
+     ready = readl(priv->base + NPCM_RNGCS_REG) &

NPCM_RNG_DATA_VALID;

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/char/hw_random/npcm-rng.c line 64

Quoted 1 line
drivers/char/hw_random/npcm-rng.c new file +1 -0
+     while ((ready == 0) && (timeout_cnt <

NPCM_RNG_TIMEOUT_POLL)) {

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/char/hw_random/npcm-rng.c line 72

Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +8 -0
+             usleep_range(500, 1000);
+             ready = readl(priv->base + NPCM_RNGCS_REG) &
+                     NPCM_RNG_DATA_VALID;
+             timeout_cnt++;
+     }
+
+     return !!ready;
+}

This looks like an open-coded version of readl_poll_timeout()...

better

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

to use the library function.

Also the sleep looks a bit long to me. What is the generation rate

of

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

the peripheral? Most RNG drivers have short intervals between data generation so they use delays rather than sleeps (a.k.a. readl_poll_timeout_atomic() ).

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

the HWRNG generate byte of random data in a few milliseconds so it is better to use the sleep command.

That's fine, just use readl_poll_timeout() then.

Quoted 2 lines
Quoted 2 lines

drivers/char/hw_random/npcm-rng.c line 74

Quoted 2 lines
drivers/char/hw_random/npcm-rng.c new file +2 -0
+
+static int npcm_rng_read(struct hwrng *rng, void *buf, size_t

max,

Quoted 13 lines
Quoted 13 lines

bool

Quoted 12 lines
Quoted 12 lines

wait)

drivers/char/hw_random/npcm-rng.c line 83

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+{
+     struct npcm_rng *priv = to_npcm_rng(rng);
+     int retval = 0;
+
+     pm_runtime_get_sync((struct device *)priv->rng.priv);
+
+     while (max >= sizeof(u32)) {
+             if (!npcm_rng_wait_ready(rng, wait))
+                     break;

The code as currently written does not honour the wait parameter

(e.g.

Quoted 42 lines
Quoted 42 lines
Quoted 12 lines
Quoted 12 lines

it sleeps even when wait is false).

drivers/char/hw_random/npcm-rng.c

Quoted 9 lines
drivers/char/hw_random/npcm-rng.c new file +9 -0
+
+             *(u32 *)buf = readl(priv->base + NPCM_RNGD_REG);
+             retval += sizeof(u32);
+             buf += sizeof(u32);
+             max -= sizeof(u32);
+     }
+
+     pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
+     pm_runtime_put_sync_autosuspend((struct device

*)priv->rng.priv);

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/char/hw_random/npcm-rng.c new file +20 -0
+
+     return retval || !wait ? retval : -EIO;
+}
+
+static int npcm_rng_probe(struct platform_device *pdev)
+{
+     struct npcm_rng *priv;
+     struct resource *res;
+     u32 quality;
+     int ret;
+
+     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+     if (!priv)
+             return -ENOMEM;
+
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+     priv->base = devm_ioremap_resource(&pdev->dev, res);
+     if (IS_ERR(priv->base))
+             return PTR_ERR(priv->base);
+
Quoted 8 lines
Quoted 8 lines

drivers/char/hw_random/npcm-rng.c

Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +8 -0
+     priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
+     priv->rng.init = npcm_rng_init;
+     priv->rng.cleanup = npcm_rng_cleanup;
+#endif
+     priv->rng.read = npcm_rng_read;
+     priv->rng.priv = (unsigned long)&pdev->dev;
+     if (of_property_read_u32(pdev->dev.of_node, "quality",

&quality))

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/char/hw_random/npcm-rng.c

Quoted 7 lines
drivers/char/hw_random/npcm-rng.c new file +7 -0
+             priv->rng.quality = 1000;
+     else
+             priv->rng.quality = quality;
+
+     writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
+#ifndef CONFIG_PM
+     writel(NPCM_RNG_CLK_SET_25MHZ, priv->base +

NPCM_RNGCS_REG);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/char/hw_random/npcm-rng.c line 131

Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4 -0
+#else
+     writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
+            priv->base + NPCM_RNGCS_REG);
+#endif

If this initialization was moved to npcm_rng_init() then there

would be

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

no need for the additional ifdefing. It would also get rid of the (potentially slow) readl calls on the PM wakeup path.

But when the Kernel have PM configuration than the priv->rng.init is

not

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

set and
*add_early_randomness* function is called. for the

*add_early_randomness*

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

success
the hwrng need to enabled in the probe.

Sorry but I don't understand this reply.

When CONFIG_PM is enabled then the probe function does not currently

set

Quoted 18 lines
Quoted 2 lines

NPCM_RNG_ENABLE; instead is relies on npcm_rng_init() being called by

Sorry maybe I miss understood, but when the CONFIG_PM enabled so the NPCM_RNG_ENABLE sets (the code use ifndef and not ifdef) *#ifndef CONFIG_PM*

       writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);

#else (*CONFIG_PM enabled*)
writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
priv->base + NPCM_RNGCS_REG);
#endif

And the hwrng needed to be enabled to run *add_early_randomness *function successfully.

If the hwrng driver will relay on PM logic to enable the hwrng will be disable when *add_early_randomness *function is called.

Quoted 9 lines
Quoted 1 line

the PM logic (as part of pm_runtime_get_sync() ).

I ended up reading my mail out of order and replied to the v2 patch.

The question is *why* the driver doesn't resume properly when adding early randomness! I think it is because the hwrng_register() is being called before PM runtime is enabled.

probably, but I am not sure it will be right to enable the PM runtime before hwrng_register() is called.

I will double check it.

Quoted 3 lines

Daniel.

Thanks,

Tomer

#10 lore raw

Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver

In reply to Tomer Maimon

On August 28th around 11:28AM in some timezone, Tomer Maimon wrote

Quoted 106 lines

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---

drivers/char/hw_random/Kconfig    |  13 ++
drivers/char/hw_random/Makefile   |   1 +
drivers/char/hw_random/npcm-rng.c | 207
++++++++++++++++++++++++++++++
3 files changed, 221 insertions(+)

create mode 100644 drivers/char/hw_random/npcm-rng.c

drivers/char/hw_random/Kconfig +13 -0
index 59f25286befe..87a1c30e7958 100644
@@ -440,6 +440,19 @@ config HW_RANDOM_OPTEE
 
 	  If unsure, say Y.
 
+config HW_RANDOM_NPCM
+	tristate "NPCM Random Number Generator support"
+	depends on ARCH_NPCM || COMPILE_TEST
+	default HW_RANDOM
+	help
+ 	  This driver provides support for the Random Number
+	  Generator hardware available in Nuvoton NPCM SoCs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called npcm-rng.
+
+ 	  If unsure, say Y.
+

endif # HW_RANDOM

config UML_RANDOM

drivers/char/hw_random/Makefile +0 -0
index 7c9ef4a7667f..17b6d4e6d591 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_HW_RANDOM_MTK)	+= mtk-rng.o

obj-$(CONFIG_HW_RANDOM_S390) += s390-trng.o
obj-$(CONFIG_HW_RANDOM_KEYSTONE) += ks-sa-rng.o
obj-$(CONFIG_HW_RANDOM_OPTEE) += optee-rng.o

drivers/char/hw_random/Makefile +1 -0
+obj-$(CONFIG_HW_RANDOM_NPCM) += npcm-rng.o
drivers/char/hw_random/npcm-rng.c new file +53 -0
new file mode 100644
index 000000000000..5b4b1b6cb362
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Nuvoton Technology corporation.
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/init.h>
+#include <linux/random.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/of_irq.h>
+#include <linux/pm_runtime.h>
+
+#define NPCM_RNGCS_REG		0x00	/* Control and status register */
+#define NPCM_RNGD_REG		0x04	/* Data register */
+#define NPCM_RNGMODE_REG	0x08	/* Mode register */
+
+#define NPCM_RNG_CLK_SET_25MHZ	GENMASK(4, 3) /* 20-25 MHz */
+#define NPCM_RNG_DATA_VALID	BIT(1)
+#define NPCM_RNG_ENABLE		BIT(0)
+#define NPCM_RNG_M1ROSEL	BIT(1)
+
+#define NPCM_RNG_TIMEOUT_POLL	20
+
+#define to_npcm_rng(p)	container_of(p, struct npcm_rng, rng)
+
+struct npcm_rng {
+	void __iomem *base;
+	struct hwrng rng;
+};
+
+static int npcm_rng_init(struct hwrng *rng)
+{
+	struct npcm_rng *priv = to_npcm_rng(rng);
+	u32 val;
+
+	val = readl(priv->base + NPCM_RNGCS_REG);
+	val |= NPCM_RNG_ENABLE;
+	writel(val, priv->base + NPCM_RNGCS_REG);
+
+	return 0;
+}
+
+static void npcm_rng_cleanup(struct hwrng *rng)
+{
+	struct npcm_rng *priv = to_npcm_rng(rng);
+	u32 val;
+
+	val = readl(priv->base + NPCM_RNGCS_REG);
+	val &= ~NPCM_RNG_ENABLE;

drivers/char/hw_random/npcm-rng.c line 63

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+	writel(val, priv->base + NPCM_RNGCS_REG);
+}
+
+static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+	struct npcm_rng *priv = to_npcm_rng(rng);
+	int timeout_cnt = 0;
+	int ready;
+
+	ready = readl(priv->base + NPCM_RNGCS_REG) & NPCM_RNG_DATA_VALID;

You should honor the wait paramter here.

Quoted 22 lines
drivers/char/hw_random/npcm-rng.c new file +11 -0
+	while ((ready == 0) && (timeout_cnt < NPCM_RNG_TIMEOUT_POLL)) {
+		usleep_range(500, 1000);
+		ready = readl(priv->base + NPCM_RNGCS_REG) &
+			NPCM_RNG_DATA_VALID;
+		timeout_cnt++;
+	}
+
+	return !!ready;
+}
+
+static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max,

bool wait)

drivers/char/hw_random/npcm-rng.c new file +10 -0
+{
+	struct npcm_rng *priv = to_npcm_rng(rng);
+	int retval = 0;
+
+	pm_runtime_get_sync((struct device *)priv->rng.priv);
+
+	while (max >= sizeof(u32)) {
+		if (!npcm_rng_wait_ready(rng, wait))
+			break;
+

drivers/char/hw_random/npcm-rng.c line 94

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+		*(u32 *)buf = readl(priv->base + NPCM_RNGD_REG);
+		retval += sizeof(u32);
+		buf += sizeof(u32);
+		max -= sizeof(u32);
+	}
+
+	pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
+	pm_runtime_put_sync_autosuspend((struct device *)priv->rng.priv);
+
+	return retval || !wait ? retval : -EIO;

So you are doing pm get/put around each rng data sample.

Have you characterized the time to enable to the time to get a sample and compared to the pm runtime sync parameters?

Do you get any data if you set non-blocking wait above?

Quoted 12 lines
drivers/char/hw_random/npcm-rng.c new file +12 -0
+}
+
+static int npcm_rng_probe(struct platform_device *pdev)
+{
+	struct npcm_rng *priv;
+	struct resource *res;
+	u32 quality;
+	int ret;
+
+	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;

drivers/char/hw_random/npcm-rng.c line 116

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(priv->base))
+		return PTR_ERR(priv->base);
+
+	priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
+	priv->rng.init = npcm_rng_init;
+	priv->rng.cleanup = npcm_rng_cleanup;

so npcm_rng_init and npcm_rng_cleanup are unused if !CONFIG_PM. No warnings?

Quoted 5 lines
drivers/char/hw_random/npcm-rng.c new file +5 -0
+#endif
+	priv->rng.read = npcm_rng_read;
+	priv->rng.priv = (unsigned long)&pdev->dev;
+	if (of_property_read_u32(pdev->dev.of_node, "quality", &quality))
+		priv->rng.quality = 1000;

drivers/char/hw_random/npcm-rng.c line 131

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+	else
+		priv->rng.quality = quality;
+
+	writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
+#ifndef CONFIG_PM
+	writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);
+#else
+	writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
+	       priv->base + NPCM_RNGCS_REG);
+#endif

I am assuming these are safe to always set and the clock will bein range?

Did you test without CONFIG_PM ? Looks like the ifndev should be ifdef otherwise the enable will never be set.

Can you use a local variable for this value that is chosen by the config instead of ifdef'ing the code?

Quoted 80 lines
drivers/char/hw_random/npcm-rng.c new file +76 -0
+
+	ret = devm_hwrng_register(&pdev->dev, &priv->rng);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to register rng device: %d\n",
+			ret);
+		return ret;
+	}
+
+	dev_set_drvdata(&pdev->dev, priv);
+	pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
+	pm_runtime_use_autosuspend(&pdev->dev);
+	pm_runtime_enable(&pdev->dev);
+
+	dev_info(&pdev->dev, "Random Number Generator Probed\n");
+
+	return 0;
+}
+
+static int npcm_rng_remove(struct platform_device *pdev)
+{
+	struct npcm_rng *priv = platform_get_drvdata(pdev);
+
+	hwrng_unregister(&priv->rng);
+	pm_runtime_disable(&pdev->dev);
+	pm_runtime_set_suspended(&pdev->dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int npcm_rng_runtime_suspend(struct device *dev)
+{
+	struct npcm_rng *priv = dev_get_drvdata(dev);
+
+	npcm_rng_cleanup(&priv->rng);
+
+	return 0;
+}
+
+static int npcm_rng_runtime_resume(struct device *dev)
+{
+	struct npcm_rng *priv = dev_get_drvdata(dev);
+
+	return npcm_rng_init(&priv->rng);
+}
+#endif
+
+static const struct dev_pm_ops npcm_rng_pm_ops = {
+	SET_RUNTIME_PM_OPS(npcm_rng_runtime_suspend,
+			   npcm_rng_runtime_resume, NULL)
+	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+				pm_runtime_force_resume)
+};
+
+static const struct of_device_id rng_dt_id[] = {
+	{ .compatible = "nuvoton,npcm750-rng",  },
+	{},
+};
+MODULE_DEVICE_TABLE(of, rng_dt_id);
+
+static struct platform_driver npcm_rng_driver = {
+	.driver = {
+		.name		= "npcm-rng",
+		.pm		= &npcm_rng_pm_ops,
+		.owner		= THIS_MODULE,
+		.of_match_table = of_match_ptr(rng_dt_id),
+	},
+	.probe		= npcm_rng_probe,
+	.remove		= npcm_rng_remove,
+};
+
+module_platform_driver(npcm_rng_driver);
+
+MODULE_DESCRIPTION("Nuvoton NPCM Random Number Generator Driver");
+MODULE_AUTHOR("Tomer Maimon <[email protected]>");
+MODULE_LICENSE("GPL v2");
Signature · 1 line
2.18.0
author maintainer
#11 lore raw

Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver

Hi Milton,

appreciate your comments and sorry for the late reply

Quoted 148 lines · Milton Miller II

On August 28th around 11:28AM in some timezone, Tomer Maimon wrote

Quoted 60 lines

Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.

---

drivers/char/hw_random/Kconfig    |  13 ++
drivers/char/hw_random/Makefile   |   1 +
drivers/char/hw_random/npcm-rng.c | 207
++++++++++++++++++++++++++++++
3 files changed, 221 insertions(+)

create mode 100644 drivers/char/hw_random/npcm-rng.c

drivers/char/hw_random/Kconfig +13 -0
index 59f25286befe..87a1c30e7958 100644
@@ -440,6 +440,19 @@ config HW_RANDOM_OPTEE
 
        If unsure, say Y.
 
+config HW_RANDOM_NPCM
+      tristate "NPCM Random Number Generator support"
+      depends on ARCH_NPCM || COMPILE_TEST
+      default HW_RANDOM
+      help
+        This driver provides support for the Random Number
+        Generator hardware available in Nuvoton NPCM SoCs.
+
+        To compile this driver as a module, choose M here: the
+        module will be called npcm-rng.
+
+        If unsure, say Y.
+

endif # HW_RANDOM

config UML_RANDOM

drivers/char/hw_random/Makefile +0 -0
index 7c9ef4a7667f..17b6d4e6d591 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_HW_RANDOM_MTK)  += mtk-rng.o

obj-$(CONFIG_HW_RANDOM_S390) += s390-trng.o
obj-$(CONFIG_HW_RANDOM_KEYSTONE) += ks-sa-rng.o
obj-$(CONFIG_HW_RANDOM_OPTEE) += optee-rng.o

drivers/char/hw_random/Makefile +1 -0
+obj-$(CONFIG_HW_RANDOM_NPCM) += npcm-rng.o
drivers/char/hw_random/npcm-rng.c new file +7 -0
new file mode 100644
index 000000000000..5b4b1b6cb362
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Nuvoton Technology corporation.
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>

drivers/char/hw_random/npcm-rng.c line 17

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+#include <linux/init.h>
+#include <linux/random.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/of_irq.h>
+#include <linux/pm_runtime.h>
+
+#define NPCM_RNGCS_REG                0x00    /* Control and status

register */

Quoted 36 lines
drivers/char/hw_random/npcm-rng.c new file +36 -0
+#define NPCM_RNGD_REG         0x04    /* Data register */
+#define NPCM_RNGMODE_REG      0x08    /* Mode register */
+
+#define NPCM_RNG_CLK_SET_25MHZ        GENMASK(4, 3) /* 20-25 MHz */
+#define NPCM_RNG_DATA_VALID   BIT(1)
+#define NPCM_RNG_ENABLE               BIT(0)
+#define NPCM_RNG_M1ROSEL      BIT(1)
+
+#define NPCM_RNG_TIMEOUT_POLL 20
+
+#define to_npcm_rng(p)        container_of(p, struct npcm_rng, rng)
+
+struct npcm_rng {
+      void __iomem *base;
+      struct hwrng rng;
+};
+
+static int npcm_rng_init(struct hwrng *rng)
+{
+      struct npcm_rng *priv = to_npcm_rng(rng);
+      u32 val;
+
+      val = readl(priv->base + NPCM_RNGCS_REG);
+      val |= NPCM_RNG_ENABLE;
+      writel(val, priv->base + NPCM_RNGCS_REG);
+
+      return 0;
+}
+
+static void npcm_rng_cleanup(struct hwrng *rng)
+{
+      struct npcm_rng *priv = to_npcm_rng(rng);
+      u32 val;
+
+      val = readl(priv->base + NPCM_RNGCS_REG);
+      val &= ~NPCM_RNG_ENABLE;

drivers/char/hw_random/npcm-rng.c line 63

Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+      writel(val, priv->base + NPCM_RNGCS_REG);
+}
+
+static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+      struct npcm_rng *priv = to_npcm_rng(rng);
+      int timeout_cnt = 0;
+      int ready;
+
+      ready = readl(priv->base + NPCM_RNGCS_REG) & NPCM_RNG_DATA_VALID;

You should honor the wait paramter here.

Quoted 26 lines
drivers/char/hw_random/npcm-rng.c new file +11 -0
+      while ((ready == 0) && (timeout_cnt < NPCM_RNG_TIMEOUT_POLL)) {
+              usleep_range(500, 1000);
+              ready = readl(priv->base + NPCM_RNGCS_REG) &
+                      NPCM_RNG_DATA_VALID;
+              timeout_cnt++;
+      }
+
+      return !!ready;
+}
+
+static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max,

bool wait)

drivers/char/hw_random/npcm-rng.c new file +14 -0
+{
+      struct npcm_rng *priv = to_npcm_rng(rng);
+      int retval = 0;
+
+      pm_runtime_get_sync((struct device *)priv->rng.priv);
+
+      while (max >= sizeof(u32)) {
+              if (!npcm_rng_wait_ready(rng, wait))
+                      break;
+
+              *(u32 *)buf = readl(priv->base + NPCM_RNGD_REG);
+              retval += sizeof(u32);
+              buf += sizeof(u32);
+              max -= sizeof(u32);
Quoted 8 lines

drivers/char/hw_random/npcm-rng.c line 94

Quoted 6 lines
drivers/char/hw_random/npcm-rng.c new file +6 -0
+      }
+
+      pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
+      pm_runtime_put_sync_autosuspend((struct device *)priv->rng.priv);
+
+      return retval || !wait ? retval : -EIO;

So you are doing pm get/put around each rng data sample.

Do you mean every read? from the core it minimum 32 bytes. only when *add_early_randomness* function is called the read is 16 bytes.

Quoted 2 lines

Have you characterized the time to enable to the time to get a sample

Quoted 2 lines

and compared to the pm runtime sync parameters?

Enabling the hwrng module take much less than generating the random data, especially when the read is minimum 16 bytes

Quoted 2 lines

Do you get any data if you set non-blocking wait above?

yes, if the user need only one byte. but if he will need more than a byte it will fail.
Do you think I should remove this option because the core read minimum of 16 bytes?

Quoted 19 lines
Quoted 19 lines
drivers/char/hw_random/npcm-rng.c new file +17 -0
+}
+
+static int npcm_rng_probe(struct platform_device *pdev)
+{
+      struct npcm_rng *priv;
+      struct resource *res;
+      u32 quality;
+      int ret;
+
+      priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+      if (!priv)
+              return -ENOMEM;
+
+      res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+      priv->base = devm_ioremap_resource(&pdev->dev, res);
+      if (IS_ERR(priv->base))
+              return PTR_ERR(priv->base);
Quoted 9 lines

drivers/char/hw_random/npcm-rng.c line 116

Quoted 5 lines
drivers/char/hw_random/npcm-rng.c new file +5 -0
+
+      priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
+      priv->rng.init = npcm_rng_init;
+      priv->rng.cleanup = npcm_rng_cleanup;

so npcm_rng_init and npcm_rng_cleanup are unused if !CONFIG_PM. No warnings?

No

Quoted 11 lines
Quoted 11 lines
drivers/char/hw_random/npcm-rng.c new file +10 -0
+#endif
+      priv->rng.read = npcm_rng_read;
+      priv->rng.priv = (unsigned long)&pdev->dev;
+      if (of_property_read_u32(pdev->dev.of_node, "quality", &quality))
+              priv->rng.quality = 1000;
+      else
+              priv->rng.quality = quality;
+
+      writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
+#ifndef CONFIG_PM
Quoted 9 lines

drivers/char/hw_random/npcm-rng.c line 131

Quoted 5 lines
drivers/char/hw_random/npcm-rng.c new file +5 -0
+      writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);
+#else
+      writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
+             priv->base + NPCM_RNGCS_REG);
+#endif

I am assuming these are safe to always set and the clock will bein range?

Yes

Quoted 4 lines

Did you test without CONFIG_PM ? Looks like the ifndev should be ifdef otherwise the enable will never be set.

Yes, I have test it.
The hwrng should be enabled when the PM configuration is set before the *add_early_randomness* function is called in the hwrng core.

Quoted 78 lines

Can you use a local variable for this value that is chosen by the config instead of ifdef'ing the code?

Quoted 72 lines
drivers/char/hw_random/npcm-rng.c new file +72 -0
+
+      ret = devm_hwrng_register(&pdev->dev, &priv->rng);
+      if (ret) {
+              dev_err(&pdev->dev, "Failed to register rng device: %d\n",
+                      ret);
+              return ret;
+      }
+
+      dev_set_drvdata(&pdev->dev, priv);
+      pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
+      pm_runtime_use_autosuspend(&pdev->dev);
+      pm_runtime_enable(&pdev->dev);
+
+      dev_info(&pdev->dev, "Random Number Generator Probed\n");
+
+      return 0;
+}
+
+static int npcm_rng_remove(struct platform_device *pdev)
+{
+      struct npcm_rng *priv = platform_get_drvdata(pdev);
+
+      hwrng_unregister(&priv->rng);
+      pm_runtime_disable(&pdev->dev);
+      pm_runtime_set_suspended(&pdev->dev);
+
+      return 0;
+}
+
+#ifdef CONFIG_PM
+static int npcm_rng_runtime_suspend(struct device *dev)
+{
+      struct npcm_rng *priv = dev_get_drvdata(dev);
+
+      npcm_rng_cleanup(&priv->rng);
+
+      return 0;
+}
+
+static int npcm_rng_runtime_resume(struct device *dev)
+{
+      struct npcm_rng *priv = dev_get_drvdata(dev);
+
+      return npcm_rng_init(&priv->rng);
+}
+#endif
+
+static const struct dev_pm_ops npcm_rng_pm_ops = {
+      SET_RUNTIME_PM_OPS(npcm_rng_runtime_suspend,
+                         npcm_rng_runtime_resume, NULL)
+      SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+                              pm_runtime_force_resume)
+};
+
+static const struct of_device_id rng_dt_id[] = {
+      { .compatible = "nuvoton,npcm750-rng",  },
+      {},
+};
+MODULE_DEVICE_TABLE(of, rng_dt_id);
+
+static struct platform_driver npcm_rng_driver = {
+      .driver = {
+              .name           = "npcm-rng",
+              .pm             = &npcm_rng_pm_ops,
+              .owner          = THIS_MODULE,
+              .of_match_table = of_match_ptr(rng_dt_id),
+      },
+      .probe          = npcm_rng_probe,
+      .remove         = npcm_rng_remove,
+};
+
+module_platform_driver(npcm_rng_driver);
Quoted 10 lines
Quoted 8 lines
drivers/char/hw_random/npcm-rng.c new file +4 -1
+
+MODULE_DESCRIPTION("Nuvoton NPCM Random Number Generator Driver");
+MODULE_AUTHOR("Tomer Maimon <[email protected]>");
+MODULE_LICENSE("GPL v2");
--

2.18.0

Thanks a lot,

Tomer