Skip to contentSkip to conversation

Add Ingenic JZ4780 hardware RNG driver

PATCH Superseded PrasannaKumar Muralidharan 5 participants 12 messages Aug 17, 2016 – Aug 19, 2016

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add Ingenic JZ4780 hardware RNG driver

This patch adds support for hardware random number generator present in JZ4780 SoC.

---
 .../devicetree/bindings/rng/ingenic,jz4780-rng.txt |  12 +++
 MAINTAINERS                                        |   5 +
 arch/mips/boot/dts/ingenic/jz4780.dtsi             |   7 +-
 drivers/char/hw_random/Kconfig                     |  14 +++
 drivers/char/hw_random/Makefile                    |   1 +
 drivers/char/hw_random/jz4780-rng.c                | 105 +++++++++++++++++++++
 6 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt
 create mode 100644 drivers/char/hw_random/jz4780-rng.c
Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt new file +12 -0
new file mode 100644
index 0000000..03abf56
@@ -0,0 +1,12 @@
+Ingenic jz4780 RNG driver
+
+Required properties:
+- compatible : Should be "ingenic,jz4780-rng"
+- reg : Specifies base physical address and size of the registers.
+
+Example:
+
+rng: rng@100000D8 {
+	compatible = "ingenic,jz4780-rng";
+	reg = <0x100000D8 0x8>;
+};
MAINTAINERS +5 -0
index 08e9efe..c0c66eb 100644
@@ -6002,6 +6002,11 @@ M:	Zubair Lutfullah Kakakhel <[email protected]>
 S:	Maintained
 F:	drivers/dma/dma-jz4780.c
 
+INGENIC JZ4780 HW RNG Driver
+M:	PrasannaKumar Muralidharan <[email protected]>
+S:	Maintained
+F:	drivers/char/hw_random/jz4780-rng.c
+
 INTEGRITY MEASUREMENT ARCHITECTURE (IMA)
 M:	Mimi Zohar <[email protected]>
 M:	Dmitry Kasatkin <[email protected]>
arch/mips/boot/dts/ingenic/jz4780.dtsi +6 -1
index b868b42..f11d139 100644
@@ -36,7 +36,7 @@
 
 	cgu: jz4780-cgu@10000000 {
 		compatible = "ingenic,jz4780-cgu";
-		reg = <0x10000000 0x100>;
+		reg = <0x10000000 0xD8>;
 
 		clocks = <&ext>, <&rtc>;
 		clock-names = "ext", "rtc";
@@ -44,6 +44,11 @@
 		#clock-cells = <1>;
 	};
 
+	rng: jz4780-rng@100000D8 {
+		compatible = "ingenic,jz4780-rng";
+		reg = <0x100000D8 0x8>;
+	};
+
 	uart0: serial@10030000 {
 		compatible = "ingenic,jz4780-uart";
 		reg = <0x10030000 0x100>;
drivers/char/hw_random/Kconfig +14 -0
index 56ad5a59..c336fe8 100644
@@ -294,6 +294,20 @@ config HW_RANDOM_POWERNV
 
 	  If unsure, say Y.
 
+config HW_RANDOM_JZ4780
+	tristate "JZ4780 HW random number generator support"
+	depends on MACH_INGENIC
+	depends on HAS_IOMEM
+	default HW_RANDOM
+	---help---
+	  This driver provides kernel-side support for the Random Number
+	  Generator hardware found on JZ4780 SOCs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called jz4780-rng.
+
+	  If unsure, say Y.
+
 config HW_RANDOM_EXYNOS
 	tristate "EXYNOS HW random number generator support"
 	depends on ARCH_EXYNOS || COMPILE_TEST
drivers/char/hw_random/Makefile +1 -0
index 04bb0b0..a155066 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
 obj-$(CONFIG_HW_RANDOM_EXYNOS)	+= exynos-rng.o
 obj-$(CONFIG_HW_RANDOM_HISI)	+= hisi-rng.o
+obj-$(CONFIG_HW_RANDOM_JZ4780)	+= jz4780-rng.o
 obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
 obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
drivers/char/hw_random/jz4780-rng.c new file +105 -0

105 lines not on this page · raw message

Signature · 1 line
2.5.0
#1 lore raw
Quoted 23 lines · PrasannaKumar Muralidharan

This patch adds support for hardware random number generator present in JZ4780 SoC.

---
 .../devicetree/bindings/rng/ingenic,jz4780-rng.txt |  12 +++
 MAINTAINERS                                        |   5 +
 arch/mips/boot/dts/ingenic/jz4780.dtsi             |   7 +-
 drivers/char/hw_random/Kconfig                     |  14 +++
 drivers/char/hw_random/Makefile                    |   1 +
 drivers/char/hw_random/jz4780-rng.c                | 105 +++++++++++++++++++++
 6 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt
 create mode 100644 drivers/char/hw_random/jz4780-rng.c
Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt new file +2 -0
new file mode 100644
index 0000000..03abf56
@@ -0,0 +1,12 @@
+Ingenic jz4780 RNG driver
+

Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt line 12

Quoted 10 lines
Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt new file +10 -0
+Required properties:
+- compatible : Should be "ingenic,jz4780-rng"
+- reg : Specifies base physical address and size of the registers.
+
+Example:
+
+rng: rng@100000D8 {
+	compatible = "ingenic,jz4780-rng";
+	reg = <0x100000D8 0x8>;
+};

Device tree bindings should be a separate patch. See Documentation/devicetree/bindings/submitting-patches.txt .

Quoted 30 lines
MAINTAINERS +5 -0
index 08e9efe..c0c66eb 100644
@@ -6002,6 +6002,11 @@ M:	Zubair Lutfullah Kakakhel <[email protected]>
 S:	Maintained
 F:	drivers/dma/dma-jz4780.c
 
+INGENIC JZ4780 HW RNG Driver
+M:	PrasannaKumar Muralidharan <[email protected]>
+S:	Maintained
+F:	drivers/char/hw_random/jz4780-rng.c
+
 INTEGRITY MEASUREMENT ARCHITECTURE (IMA)
 M:	Mimi Zohar <[email protected]>
 M:	Dmitry Kasatkin <[email protected]>
arch/mips/boot/dts/ingenic/jz4780.dtsi +1 -1
index b868b42..f11d139 100644
@@ -36,7 +36,7 @@
 
 	cgu: jz4780-cgu@10000000 {
 		compatible = "ingenic,jz4780-cgu";
-		reg = <0x10000000 0x100>;
+		reg = <0x10000000 0xD8>;
 		clocks = <&ext>, <&rtc>;
 		clock-names = "ext", "rtc";
@@ -44,6 +44,11 @@
 		#clock-cells = <1>;

arch/mips/boot/dts/ingenic/jz4780.dtsi line 54

Quoted 10 lines
arch/mips/boot/dts/ingenic/jz4780.dtsi +5 -0
 	};
 
+	rng: jz4780-rng@100000D8 {
+		compatible = "ingenic,jz4780-rng";
+		reg = <0x100000D8 0x8>;
+	};
+
 	uart0: serial@10030000 {
 		compatible = "ingenic,jz4780-uart";
 		reg = <0x10030000 0x100>;

Updates to device tree files are also normally sent as a separate patch later in the series than the driver itself (at least they are in arm land).

Quoted 12 lines
drivers/char/hw_random/Kconfig +4 -0
index 56ad5a59..c336fe8 100644
@@ -294,6 +294,20 @@ config HW_RANDOM_POWERNV
 
 	  If unsure, say Y.
 
+config HW_RANDOM_JZ4780
+	tristate "JZ4780 HW random number generator support"
+	depends on MACH_INGENIC
+	depends on HAS_IOMEM

drivers/char/hw_random/Kconfig config HW_RANDOM_POWERNV · line 310

Quoted 10 lines
drivers/char/hw_random/Kconfig +10 -0
+	default HW_RANDOM
+	---help---
+	  This driver provides kernel-side support for the Random Number
+	  Generator hardware found on JZ4780 SOCs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called jz4780-rng.
+
+	  If unsure, say Y.
+

Shouldn't this be inserted either in alphabetic order (which not applicable for this file) or at the end of the file?

drivers/char/hw_random/Makefile obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o · line 29

Quoted 12 lines
drivers/char/hw_random/Kconfig +0 -0
 config HW_RANDOM_EXYNOS
 	tristate "EXYNOS HW random number generator support"
 	depends on ARCH_EXYNOS || COMPILE_TEST
drivers/char/hw_random/Makefile +1 -0
index 04bb0b0..a155066 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
 obj-$(CONFIG_HW_RANDOM_EXYNOS)	+= exynos-rng.o
 obj-$(CONFIG_HW_RANDOM_HISI)	+= hisi-rng.o
+obj-$(CONFIG_HW_RANDOM_JZ4780)	+= jz4780-rng.o

This looks like it inserts into the makefile in a gratuitously different order than the one in the KConfig file.

Quoted 62 lines
drivers/char/hw_random/Makefile +0 -0
 obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
 obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
drivers/char/hw_random/jz4780-rng.c new file +53 -0
new file mode 100644
index 0000000..c9d2cde
@@ -0,0 +1,105 @@
+/*
+ * jz4780-rng.c - Random Number Generator driver for J4780
+ *
+ * Copyright 2016 (C) PrasannaKumar Muralidharan <[email protected]>
+ *
+ * This file is licensed under  the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include <linux/hw_random.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>
+
+#define REG_RNG_CTRL	0x0
+#define REG_RNG_DATA	0x4
+
+struct jz4780_rng {
+	struct device *dev;
+	struct hwrng rng;
+	void __iomem *mem;
+};
+
+static u32 jz4780_rng_readl(struct jz4780_rng *rng, u32 offset)
+{
+	return readl(rng->mem + offset);
+}
+
+static void jz4780_rng_writel(struct jz4780_rng *rng, u32 val, u32 offset)
+{
+	writel(val, rng->mem + offset);
+}
+
+static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+	struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng,
+							rng);
+	u32 *data = buf;
+	*data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
+	return 4;
+}
+
+static int jz4780_rng_probe(struct platform_device *pdev)
+{
+	struct jz4780_rng *jz4780_rng;
+	struct resource *res;
+	resource_size_t size;
+	int ret;
+

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

Quoted 10 lines
drivers/char/hw_random/jz4780-rng.c new file +10 -0
+	jz4780_rng = devm_kzalloc(&pdev->dev, sizeof(struct jz4780_rng),
+					GFP_KERNEL);
+	if (!jz4780_rng)
+		return -ENOMEM;
+
+	jz4780_rng->dev = &pdev->dev;
+	jz4780_rng->rng.name = "jz4780";
+	jz4780_rng->rng.read = jz4780_rng_read;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

If platform_get_resource() returns NULL I think this code will crash.

Quoted 8 lines
drivers/char/hw_random/jz4780-rng.c new file +8 -0
+	size = resource_size(res);
+
+	jz4780_rng->mem = devm_ioremap(&pdev->dev, res->start, size);
+	if (IS_ERR(jz4780_rng->mem))
+		return PTR_ERR(jz4780_rng->mem);
+
+	platform_set_drvdata(pdev, jz4780_rng);
+	jz4780_rng_writel(jz4780_rng, 1, REG_RNG_CTRL);

drivers/char/hw_random/jz4780-rng.c line 82

Quoted 10 lines
drivers/char/hw_random/jz4780-rng.c new file +10 -0
+	ret = hwrng_register(&jz4780_rng->rng);
+	return ret;
+}
+
+static int jz4780_rng_remove(struct platform_device *pdev)
+{
+	struct jz4780_rng *jz4780_rng = platform_get_drvdata(pdev);
+
+	jz4780_rng_writel(jz4780_rng, 0, REG_RNG_CTRL);
+	hwrng_unregister(&jz4780_rng->rng);

These two are in same order as probe() function. I would expect them to be reversed (so hardware cannot be shutdown until we have unregistered it).

Quoted 24 lines
drivers/char/hw_random/jz4780-rng.c new file +23 -0
+
+	return 0;
+}
+
+static const struct of_device_id jz4780_rng_dt_match[] = {
+	{ .compatible = "ingenic,jz4780-rng", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, jz4780_rng_dt_match);
+
+static struct platform_driver jz4780_rng_driver = {
+	.driver		= {
+		.name	= "jz4780-rng",
+		.of_match_table = jz4780_rng_dt_match,
+	},
+	.probe		= jz4780_rng_probe,
+	.remove		= jz4780_rng_remove,
+};
+module_platform_driver(jz4780_rng_driver);
+
+MODULE_DESCRIPTION("Ingenic JZ4780 H/W Random Number Generator driver");
+MODULE_AUTHOR("PrasannaKumar Muralidharan <[email protected]>");
+MODULE_LICENSE("GPL");
#2 lore raw
Quoted 34 lines · Daniel Thompson
Quoted 33 lines · PrasannaKumar Muralidharan

This patch adds support for hardware random number generator present in JZ4780 SoC.

---
 .../devicetree/bindings/rng/ingenic,jz4780-rng.txt |  12 +++
 MAINTAINERS                                        |   5 +
 arch/mips/boot/dts/ingenic/jz4780.dtsi             |   7 +-
 drivers/char/hw_random/Kconfig                     |  14 +++
 drivers/char/hw_random/Makefile                    |   1 +
 drivers/char/hw_random/jz4780-rng.c                | 105
+++++++++++++++++++++
 6 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100644
Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt
 create mode 100644 drivers/char/hw_random/jz4780-rng.c
Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt new file +8 -0
new file mode 100644
index 0000000..03abf56
@@ -0,0 +1,12 @@
+Ingenic jz4780 RNG driver
+
+Required properties:
+- compatible : Should be "ingenic,jz4780-rng"
+- reg : Specifies base physical address and size of the registers.
+
+Example:
+
Quoted 8 lines

Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt line 12

Quoted 4 lines
Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt new file +4 -0
+rng: rng@100000D8 {
+       compatible = "ingenic,jz4780-rng";
+       reg = <0x100000D8 0x8>;
+};

Device tree bindings should be a separate patch. See Documentation/devicetree/bindings/submitting-patches.txt .

Sure, Will submit it as a separate patch.

Quoted 34 lines
Quoted 34 lines
MAINTAINERS +0 -0
index 08e9efe..c0c66eb 100644
@@ -6002,6 +6002,11 @@ M:       Zubair Lutfullah Kakakhel

<[email protected]>
S: Maintained
F: drivers/dma/dma-jz4780.c

MAINTAINERS +5 -0
+INGENIC JZ4780 HW RNG Driver
+M:     PrasannaKumar Muralidharan <[email protected]>
+S:     Maintained
+F:     drivers/char/hw_random/jz4780-rng.c
+
 INTEGRITY MEASUREMENT ARCHITECTURE (IMA)
 M:     Mimi Zohar <[email protected]>
 M:     Dmitry Kasatkin <[email protected]>
arch/mips/boot/dts/ingenic/jz4780.dtsi +1 -1
index b868b42..f11d139 100644
@@ -36,7 +36,7 @@
 
        cgu: jz4780-cgu@10000000 {
                compatible = "ingenic,jz4780-cgu";
-               reg = <0x10000000 0x100>;
+               reg = <0x10000000 0xD8>;
                clocks = <&ext>, <&rtc>;
                clock-names = "ext", "rtc";
@@ -44,6 +44,11 @@
                #clock-cells = <1>;
        };
 
Quoted 12 lines

arch/mips/boot/dts/ingenic/jz4780.dtsi line 54

Quoted 8 lines
arch/mips/boot/dts/ingenic/jz4780.dtsi +5 -0
+       rng: jz4780-rng@100000D8 {
+               compatible = "ingenic,jz4780-rng";
+               reg = <0x100000D8 0x8>;
+       };
+
        uart0: serial@10030000 {
                compatible = "ingenic,jz4780-uart";
                reg = <0x10030000 0x100>;

Updates to device tree files are also normally sent as a separate patch later in the series than the driver itself (at least they are in arm land).

Will follow the convention.

Quoted 19 lines
Quoted 19 lines
drivers/char/hw_random/Kconfig +10 -0
index 56ad5a59..c336fe8 100644
@@ -294,6 +294,20 @@ config HW_RANDOM_POWERNV
 
          If unsure, say Y.
 
+config HW_RANDOM_JZ4780
+       tristate "JZ4780 HW random number generator support"
+       depends on MACH_INGENIC
+       depends on HAS_IOMEM
+       default HW_RANDOM
+       ---help---
+         This driver provides kernel-side support for the Random Number
+         Generator hardware found on JZ4780 SOCs.
+
+         To compile this driver as a module, choose M here: the
Quoted 8 lines

drivers/char/hw_random/Kconfig config HW_RANDOM_POWERNV · line 310

Quoted 4 lines
drivers/char/hw_random/Kconfig +4 -0
+         module will be called jz4780-rng.
+
+         If unsure, say Y.
+

Shouldn't this be inserted either in alphabetic order (which not applicable for this file) or at the end of the file?

I missed it. Will take into account while sending next revision.

Quoted 9 lines
Quoted 9 lines
drivers/char/hw_random/Kconfig +0 -0
 config HW_RANDOM_EXYNOS
        tristate "EXYNOS HW random number generator support"
        depends on ARCH_EXYNOS || COMPILE_TEST
drivers/char/hw_random/Makefile +0 -0
index 04bb0b0..a155066 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
Quoted 8 lines

drivers/char/hw_random/Makefile obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o · line 29

Quoted 4 lines
drivers/char/hw_random/Makefile +1 -0
 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
 obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
 obj-$(CONFIG_HW_RANDOM_HISI)   += hisi-rng.o
+obj-$(CONFIG_HW_RANDOM_JZ4780) += jz4780-rng.o

This looks like it inserts into the makefile in a gratuitously different order than the one in the KConfig file.

Will arrange both alphabetically. They will be in sync then.

Quoted 72 lines
Quoted 72 lines
drivers/char/hw_random/Makefile +0 -0
 obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
 obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
drivers/char/hw_random/jz4780-rng.c new file +4 -0
new file mode 100644
index 0000000..c9d2cde
@@ -0,0 +1,105 @@
+/*
+ * jz4780-rng.c - Random Number Generator driver for J4780
+ *
+ * Copyright 2016 (C) PrasannaKumar Muralidharan

<[email protected]>
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */

drivers/char/hw_random/jz4780-rng.c new file +24 -0
+
+#include <linux/hw_random.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>
+
+#define REG_RNG_CTRL   0x0
+#define REG_RNG_DATA   0x4
+
+struct jz4780_rng {
+       struct device *dev;
+       struct hwrng rng;
+       void __iomem *mem;
+};
+
+static u32 jz4780_rng_readl(struct jz4780_rng *rng, u32 offset)
+{
+       return readl(rng->mem + offset);
+}
+
+static void jz4780_rng_writel(struct jz4780_rng *rng, u32 val, u32

offset)

drivers/char/hw_random/jz4780-rng.c new file +5 -0
+{
+       writel(val, rng->mem + offset);
+}
+
+static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool

wait)

drivers/char/hw_random/jz4780-rng.c new file +2 -0
+{
+       struct jz4780_rng *jz4780_rng = container_of(rng, struct

jz4780_rng,

drivers/char/hw_random/jz4780-rng.c new file +18 -0
+                                                       rng);
+       u32 *data = buf;
+       *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
+       return 4;
+}
+
+static int jz4780_rng_probe(struct platform_device *pdev)
+{
+       struct jz4780_rng *jz4780_rng;
+       struct resource *res;
+       resource_size_t size;
+       int ret;
+
+       jz4780_rng = devm_kzalloc(&pdev->dev, sizeof(struct jz4780_rng),
+                                       GFP_KERNEL);
+       if (!jz4780_rng)
+               return -ENOMEM;
+
Quoted 8 lines

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

Quoted 5 lines
drivers/char/hw_random/jz4780-rng.c new file +5 -0
+       jz4780_rng->dev = &pdev->dev;
+       jz4780_rng->rng.name = "jz4780";
+       jz4780_rng->rng.read = jz4780_rng_read;
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

If platform_get_resource() returns NULL I think this code will crash.

Missed it. Will fix the issue.

Quoted 14 lines
Quoted 14 lines
drivers/char/hw_random/jz4780-rng.c new file +14 -0
+       size = resource_size(res);
+
+       jz4780_rng->mem = devm_ioremap(&pdev->dev, res->start, size);
+       if (IS_ERR(jz4780_rng->mem))
+               return PTR_ERR(jz4780_rng->mem);
+
+       platform_set_drvdata(pdev, jz4780_rng);
+       jz4780_rng_writel(jz4780_rng, 1, REG_RNG_CTRL);
+       ret = hwrng_register(&jz4780_rng->rng);
+       return ret;
+}
+
+static int jz4780_rng_remove(struct platform_device *pdev)
+{
Quoted 8 lines

drivers/char/hw_random/jz4780-rng.c line 82

Quoted 4 lines
drivers/char/hw_random/jz4780-rng.c new file +4 -0
+       struct jz4780_rng *jz4780_rng = platform_get_drvdata(pdev);
+
+       jz4780_rng_writel(jz4780_rng, 0, REG_RNG_CTRL);
+       hwrng_unregister(&jz4780_rng->rng);

These two are in same order as probe() function. I would expect them to be reversed (so hardware cannot be shutdown until we have unregistered it).

True. I will change it.

Quoted 16 lines
Quoted 16 lines
drivers/char/hw_random/jz4780-rng.c new file +16 -0
+
+       return 0;
+}
+
+static const struct of_device_id jz4780_rng_dt_match[] = {
+       { .compatible = "ingenic,jz4780-rng", },
+       { },
+};
+MODULE_DEVICE_TABLE(of, jz4780_rng_dt_match);
+
+static struct platform_driver jz4780_rng_driver = {
+       .driver         = {
+               .name   = "jz4780-rng",
+               .of_match_table = jz4780_rng_dt_match,
+       },
+       .probe          = jz4780_rng_probe,
Quoted 9 lines

drivers/char/hw_random/jz4780-rng.c line 105

Quoted 8 lines
drivers/char/hw_random/jz4780-rng.c new file +7 -0
+       .remove         = jz4780_rng_remove,
+};
+module_platform_driver(jz4780_rng_driver);
+
+MODULE_DESCRIPTION("Ingenic JZ4780 H/W Random Number Generator driver");
+MODULE_AUTHOR("PrasannaKumar Muralidharan <[email protected]>");
+MODULE_LICENSE("GPL");

Thanks for the review, appreciate your time.

#3 lore raw
In reply to PrasannaKumar Muralidharan

Hello

I have just some minor comments below

Quoted 13 lines
drivers/char/hw_random/jz4780-rng.c new file +7 -0
new file mode 100644
index 0000000..c9d2cde
@@ -0,0 +1,105 @@
+/*
+ * jz4780-rng.c - Random Number Generator driver for J4780
+ *
+ * Copyright 2016 (C) PrasannaKumar Muralidharan <[email protected]>
+ *
+ * This file is licensed under  the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any

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

Quoted 10 lines
drivers/char/hw_random/jz4780-rng.c new file +10 -0
+ * warranty of any kind, whether express or implied.
+ */
+
+#include <linux/hw_random.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>

You could sort them by alphabetical order

Quoted 18 lines
drivers/char/hw_random/jz4780-rng.c new file +18 -0
+
+#define REG_RNG_CTRL	0x0
+#define REG_RNG_DATA	0x4
+
+struct jz4780_rng {
+	struct device *dev;
+	struct hwrng rng;
+	void __iomem *mem;
+};
+
+static u32 jz4780_rng_readl(struct jz4780_rng *rng, u32 offset)
+{
+	return readl(rng->mem + offset);
+}
+
+static void jz4780_rng_writel(struct jz4780_rng *rng, u32 val, u32 offset)
+{
+	writel(val, rng->mem + offset);

drivers/char/hw_random/jz4780-rng.c line 45

Quoted 10 lines
drivers/char/hw_random/jz4780-rng.c new file +10 -0
+}
+
+static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+	struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng,
+							rng);
+	u32 *data = buf;
+	*data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
+	return 4;
+}

If max is less than 4, its bad

drivers/char/hw_random/jz4780-rng.c line 55

Quoted 10 lines
drivers/char/hw_random/jz4780-rng.c new file +10 -0
+
+static int jz4780_rng_probe(struct platform_device *pdev)
+{
+	struct jz4780_rng *jz4780_rng;
+	struct resource *res;
+	resource_size_t size;
+	int ret;
+
+	jz4780_rng = devm_kzalloc(&pdev->dev, sizeof(struct jz4780_rng),
+					GFP_KERNEL);

You could write sizeof(*js480_rng)

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

Quoted 11 lines
drivers/char/hw_random/jz4780-rng.c new file +11 -0
+	if (!jz4780_rng)
+		return -ENOMEM;
+
+	jz4780_rng->dev = &pdev->dev;
+	jz4780_rng->rng.name = "jz4780";
+	jz4780_rng->rng.read = jz4780_rng_read;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	size = resource_size(res);
+
+	jz4780_rng->mem = devm_ioremap(&pdev->dev, res->start, size);

You could save code by using devm_ioremap_resource (don't need size)

drivers/char/hw_random/jz4780-rng.c line 75

Quoted 9 lines
drivers/char/hw_random/jz4780-rng.c new file +9 -0
+	if (IS_ERR(jz4780_rng->mem))
+		return PTR_ERR(jz4780_rng->mem);
+
+	platform_set_drvdata(pdev, jz4780_rng);
+	jz4780_rng_writel(jz4780_rng, 1, REG_RNG_CTRL);
+	ret = hwrng_register(&jz4780_rng->rng);
+
+	return ret;
+}

You could write directly return hwrng_register(..)

Regards

LABBE Corentin

#4 lore raw
Quoted 1 line

I have just some minor comments below

Appreciate your review.

Quoted 17 lines
Quoted 17 lines
drivers/char/hw_random/jz4780-rng.c new file +11 -0
new file mode 100644
index 0000000..c9d2cde
@@ -0,0 +1,105 @@
+/*
+ * jz4780-rng.c - Random Number Generator driver for J4780
+ *
+ * Copyright 2016 (C) PrasannaKumar Muralidharan <[email protected]>
+ *
+ * This file is licensed under  the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include <linux/hw_random.h>
Quoted 8 lines

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

Quoted 6 lines
drivers/char/hw_random/jz4780-rng.c new file +6 -0
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>

You could sort them by alphabetical order

Sure, will do.

Quoted 22 lines
Quoted 22 lines
drivers/char/hw_random/jz4780-rng.c new file +22 -0
+
+#define REG_RNG_CTRL 0x0
+#define REG_RNG_DATA 0x4
+
+struct jz4780_rng {
+     struct device *dev;
+     struct hwrng rng;
+     void __iomem *mem;
+};
+
+static u32 jz4780_rng_readl(struct jz4780_rng *rng, u32 offset)
+{
+     return readl(rng->mem + offset);
+}
+
+static void jz4780_rng_writel(struct jz4780_rng *rng, u32 val, u32 offset)
+{
+     writel(val, rng->mem + offset);
+}
+
+static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
Quoted 8 lines

drivers/char/hw_random/jz4780-rng.c line 45

Quoted 6 lines
drivers/char/hw_random/jz4780-rng.c new file +6 -0
+     struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng,
+                                                     rng);
+     u32 *data = buf;
+     *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
+     return 4;
+}

If max is less than 4, its bad

Data will be 4 bytes.

Quoted 4 lines
Quoted 4 lines
drivers/char/hw_random/jz4780-rng.c new file +4 -0
+
+static int jz4780_rng_probe(struct platform_device *pdev)
+{
+     struct jz4780_rng *jz4780_rng;
Quoted 8 lines

drivers/char/hw_random/jz4780-rng.c line 55

Quoted 6 lines
drivers/char/hw_random/jz4780-rng.c new file +6 -0
+     struct resource *res;
+     resource_size_t size;
+     int ret;
+
+     jz4780_rng = devm_kzalloc(&pdev->dev, sizeof(struct jz4780_rng),
+                                     GFP_KERNEL);

You could write sizeof(*js480_rng)

Will do.

Quoted 4 lines
Quoted 4 lines
drivers/char/hw_random/jz4780-rng.c new file +4 -0
+     if (!jz4780_rng)
+             return -ENOMEM;
+
+     jz4780_rng->dev = &pdev->dev;
Quoted 8 lines

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

Quoted 7 lines
drivers/char/hw_random/jz4780-rng.c new file +7 -0
+     jz4780_rng->rng.name = "jz4780";
+     jz4780_rng->rng.read = jz4780_rng_read;
+
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+     size = resource_size(res);
+
+     jz4780_rng->mem = devm_ioremap(&pdev->dev, res->start, size);

You could save code by using devm_ioremap_resource (don't need size)

Will do.

Quoted 10 lines

drivers/char/hw_random/jz4780-rng.c line 75

Quoted 9 lines
drivers/char/hw_random/jz4780-rng.c new file +9 -0
+     if (IS_ERR(jz4780_rng->mem))
+             return PTR_ERR(jz4780_rng->mem);
+
+     platform_set_drvdata(pdev, jz4780_rng);
+     jz4780_rng_writel(jz4780_rng, 1, REG_RNG_CTRL);
+     ret = hwrng_register(&jz4780_rng->rng);
+
+     return ret;
+}

You could write directly return hwrng_register(..)

Will do.

#5 lore raw
Quoted 11 lines · PrasannaKumar Muralidharan
Quoted 10 lines

drivers/char/hw_random/jz4780-rng.c line 45

Quoted 8 lines
drivers/char/hw_random/jz4780-rng.c new file +8 -0
+static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+     struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng,
+                                                     rng);
+     u32 *data = buf;
+     *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
+     return 4;
+}

If max is less than 4, its bad

Quoted 2 lines

Data will be 4 bytes.

No, according to comment in include/linux/hw_random.h "drivers can fill up to max bytes of data" So you cannot write more than max bytes without risking buffer overflow.

And if max > 4, hwrng client need to recall your read function.
The better example I found is tpm_get_random() in drivers/char/tpm/tpm-interface.c for handling both problem.

Regards

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
Quoted 12 lines · LABBE Corentin
Quoted 11 lines · PrasannaKumar Muralidharan
Quoted 10 lines

drivers/char/hw_random/jz4780-rng.c line 45

Quoted 8 lines
drivers/char/hw_random/jz4780-rng.c new file +8 -0
+static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+     struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng,
+                                                     rng);
+     u32 *data = buf;
+     *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
+     return 4;
+}

If max is less than 4, its bad

Quoted 8 lines
Quoted 2 lines

Data will be 4 bytes.

No, according to comment in include/linux/hw_random.h "drivers can fill up to max bytes of data" So you cannot write more than max bytes without risking buffer overflow.

And if max > 4, hwrng client need to recall your read function.
The better example I found is tpm_get_random() in drivers/char/tpm/tpm-interface.c for handling both problem.

Right now the core code will never actually ask a RNG driver for <4 bytes so perhaps it would be better to update the comment in include/linux/hw_random.h !

For devices with 32-bit RNG registers the extra code to handle a special case that doesn't actually exist is a waste.

There are 14 drivers in drivers/char/hw_random that support the ->read() interface but only three of these actually support max == 1 (existing accepted behavior varies between return 0, return 2, return 4 and return -EIO).

Daniel.
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer Acked-by on v1 1/1
#7 lore raw
In reply to PrasannaKumar Muralidharan
Quoted 6 lines · PrasannaKumar Muralidharan

This patch adds support for hardware random number generator present in JZ4780 SoC.

---
 .../devicetree/bindings/rng/ingenic,jz4780-rng.txt |  12 +++
Quoted 8 lines
 MAINTAINERS                                        |   5 +
 arch/mips/boot/dts/ingenic/jz4780.dtsi             |   7 +-
 drivers/char/hw_random/Kconfig                     |  14 +++
 drivers/char/hw_random/Makefile                    |   1 +
 drivers/char/hw_random/jz4780-rng.c                | 105 +++++++++++++++++++++
 6 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt
 create mode 100644 drivers/char/hw_random/jz4780-rng.c
#8 lore raw
In reply to PrasannaKumar Muralidharan
Quoted 213 lines · PrasannaKumar Muralidharan

This patch adds support for hardware random number generator present in JZ4780 SoC.

---
 .../devicetree/bindings/rng/ingenic,jz4780-rng.txt |  12 +++
 MAINTAINERS                                        |   5 +
 arch/mips/boot/dts/ingenic/jz4780.dtsi             |   7 +-
 drivers/char/hw_random/Kconfig                     |  14 +++
 drivers/char/hw_random/Makefile                    |   1 +
 drivers/char/hw_random/jz4780-rng.c                | 105 +++++++++++++++++++++
 6 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt
 create mode 100644 drivers/char/hw_random/jz4780-rng.c
Documentation/devicetree/bindings/rng/ingenic,jz4780-rng.txt new file +12 -0
new file mode 100644
index 0000000..03abf56
@@ -0,0 +1,12 @@
+Ingenic jz4780 RNG driver
+
+Required properties:
+- compatible : Should be "ingenic,jz4780-rng"
+- reg : Specifies base physical address and size of the registers.
+
+Example:
+
+rng: rng@100000D8 {
+       compatible = "ingenic,jz4780-rng";
+       reg = <0x100000D8 0x8>;
+};
MAINTAINERS +5 -0
index 08e9efe..c0c66eb 100644
@@ -6002,6 +6002,11 @@ M:       Zubair Lutfullah Kakakhel <[email protected]>
 S:     Maintained
 F:     drivers/dma/dma-jz4780.c
 
+INGENIC JZ4780 HW RNG Driver
+M:     PrasannaKumar Muralidharan <[email protected]>
+S:     Maintained
+F:     drivers/char/hw_random/jz4780-rng.c
+
 INTEGRITY MEASUREMENT ARCHITECTURE (IMA)
 M:     Mimi Zohar <[email protected]>
 M:     Dmitry Kasatkin <[email protected]>
arch/mips/boot/dts/ingenic/jz4780.dtsi +6 -1
index b868b42..f11d139 100644
@@ -36,7 +36,7 @@
 
        cgu: jz4780-cgu@10000000 {
                compatible = "ingenic,jz4780-cgu";
-               reg = <0x10000000 0x100>;
+               reg = <0x10000000 0xD8>;
 
                clocks = <&ext>, <&rtc>;
                clock-names = "ext", "rtc";
@@ -44,6 +44,11 @@
                #clock-cells = <1>;
        };
 
+       rng: jz4780-rng@100000D8 {
+               compatible = "ingenic,jz4780-rng";
+               reg = <0x100000D8 0x8>;
+       };
+
        uart0: serial@10030000 {
                compatible = "ingenic,jz4780-uart";
                reg = <0x10030000 0x100>;
drivers/char/hw_random/Kconfig +14 -0
index 56ad5a59..c336fe8 100644
@@ -294,6 +294,20 @@ config HW_RANDOM_POWERNV
 
          If unsure, say Y.
 
+config HW_RANDOM_JZ4780
+       tristate "JZ4780 HW random number generator support"
+       depends on MACH_INGENIC
+       depends on HAS_IOMEM
+       default HW_RANDOM
+       ---help---
+         This driver provides kernel-side support for the Random Number
+         Generator hardware found on JZ4780 SOCs.
+
+         To compile this driver as a module, choose M here: the
+         module will be called jz4780-rng.
+
+         If unsure, say Y.
+
 config HW_RANDOM_EXYNOS
        tristate "EXYNOS HW random number generator support"
        depends on ARCH_EXYNOS || COMPILE_TEST
drivers/char/hw_random/Makefile +1 -0
index 04bb0b0..a155066 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
 obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
 obj-$(CONFIG_HW_RANDOM_HISI)   += hisi-rng.o
+obj-$(CONFIG_HW_RANDOM_JZ4780) += jz4780-rng.o
 obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
 obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
drivers/char/hw_random/jz4780-rng.c new file +96 -0
new file mode 100644
index 0000000..c9d2cde
@@ -0,0 +1,105 @@
+/*
+ * jz4780-rng.c - Random Number Generator driver for J4780
+ *
+ * Copyright 2016 (C) PrasannaKumar Muralidharan <[email protected]>
+ *
+ * This file is licensed under  the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include <linux/hw_random.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>
+
+#define REG_RNG_CTRL   0x0
+#define REG_RNG_DATA   0x4
+
+struct jz4780_rng {
+       struct device *dev;
+       struct hwrng rng;
+       void __iomem *mem;
+};
+
+static u32 jz4780_rng_readl(struct jz4780_rng *rng, u32 offset)
+{
+       return readl(rng->mem + offset);
+}
+
+static void jz4780_rng_writel(struct jz4780_rng *rng, u32 val, u32 offset)
+{
+       writel(val, rng->mem + offset);
+}
+
+static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+       struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng,
+                                                       rng);
+       u32 *data = buf;
+       *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
+       return 4;
+}
+
+static int jz4780_rng_probe(struct platform_device *pdev)
+{
+       struct jz4780_rng *jz4780_rng;
+       struct resource *res;
+       resource_size_t size;
+       int ret;
+
+       jz4780_rng = devm_kzalloc(&pdev->dev, sizeof(struct jz4780_rng),
+                                       GFP_KERNEL);
+       if (!jz4780_rng)
+               return -ENOMEM;
+
+       jz4780_rng->dev = &pdev->dev;
+       jz4780_rng->rng.name = "jz4780";
+       jz4780_rng->rng.read = jz4780_rng_read;
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       size = resource_size(res);
+
+       jz4780_rng->mem = devm_ioremap(&pdev->dev, res->start, size);
+       if (IS_ERR(jz4780_rng->mem))
+               return PTR_ERR(jz4780_rng->mem);
+
+       platform_set_drvdata(pdev, jz4780_rng);
+       jz4780_rng_writel(jz4780_rng, 1, REG_RNG_CTRL);
+       ret = hwrng_register(&jz4780_rng->rng);
+
+       return ret;
+}
+
+static int jz4780_rng_remove(struct platform_device *pdev)
+{
+       struct jz4780_rng *jz4780_rng = platform_get_drvdata(pdev);
+
+       jz4780_rng_writel(jz4780_rng, 0, REG_RNG_CTRL);
+       hwrng_unregister(&jz4780_rng->rng);
+
+       return 0;
+}
+
+static const struct of_device_id jz4780_rng_dt_match[] = {
+       { .compatible = "ingenic,jz4780-rng", },
+       { },
+};
+MODULE_DEVICE_TABLE(of, jz4780_rng_dt_match);
+
+static struct platform_driver jz4780_rng_driver = {
+       .driver         = {
+               .name   = "jz4780-rng",
+               .of_match_table = jz4780_rng_dt_match,

drivers/char/hw_random/jz4780-rng.c

Quoted 10 lines
drivers/char/hw_random/jz4780-rng.c new file +9 -1
+       },
+       .probe          = jz4780_rng_probe,
+       .remove         = jz4780_rng_remove,
+};
+module_platform_driver(jz4780_rng_driver);
+
+MODULE_DESCRIPTION("Ingenic JZ4780 H/W Random Number Generator driver");
+MODULE_AUTHOR("PrasannaKumar Muralidharan <[email protected]>");
+MODULE_LICENSE("GPL");
--

Please forgive my ignorance Prasanna...

For the JZ4780 I have, there are two registers in play. The first is the control register which enables/disables the RNG. The control register is named ERNG. The second register is the data register, and it produces the random stream. The data register is named RNG. ERNG is located at 0x100000D8 and RNG is located at 0x100000DC. This kind of confuses me because I don't see where 0x100000D8 is ever added to those values (maybe its in the descriptor?):

+#define REG_RNG_CTRL 0x0
+#define REG_RNG_DATA 0x4

Also, testing with a userland PoC for the device, you have to throttle reads from RNG register. If reads occur with a 0 delay, then the random value appears fixed. If the delay is too small, then you can watch random values being shifted-in in a barrel like fashion. Unfortunately, the manual did not discuss how long to wait for a value to be ready. I found spinning in a loop for 5000 was too small and witnessed the shifting; while spinning in a loop for 10000 avoided the shift observation. I don't what number of JIFFIES that translates to.

Finally, from looking at the native Ingenic driver (which was not very impressive), they enabled/disabled the RNG register on demand. There was also a [possible related] note in the manual about not applying VCC for over a second. I can only say "possibly related" because I was not sure if the register was part of the controller they were discussing. The userland PoC worked fine when enabling/disabling the RNG register. So I'm not sure about this (from jz4780_rng_probe):

+       platform_set_drvdata(pdev, jz4780_rng);
+       jz4780_rng_writel(jz4780_rng, 1, REG_RNG_CTRL);
+       ret = hwrng_register(&jz4780_rng->rng);

And this (from jz4780_rng_remove):

+ jz4780_rng_writel(jz4780_rng, 0, REG_RNG_CTRL);
+ hwrng_unregister(&jz4780_rng->rng);

Anyway, I hope that helps you avoid some land mines (if they are present).

Jeff

#9 lore raw

drivers/char/hw_random/jz4780-rng.c line 20

Quoted 12 lines

Please forgive my ignorance Prasanna...

For the JZ4780 I have, there are two registers in play. The first is the control register which enables/disables the RNG. The control register is named ERNG. The second register is the data register, and it produces the random stream. The data register is named RNG. ERNG is located at 0x100000D8 and RNG is located at 0x100000DC. This kind of confuses me because I don't see where 0x100000D8 is ever added to those values (maybe its in the descriptor?):

drivers/char/hw_random/jz4780-rng.c new file +2 -0
+#define REG_RNG_CTRL   0x0
+#define REG_RNG_DATA   0x4

The base address 0x100000D8 is defined in jz4780.dtsi file. REG_RNG_CTRL and REG_RNG_DATA are offsets. In jz4780_rng_readl and jz4780_rng_writel functions the ioremap'd base address is added with offset.

Quoted 8 lines

Also, testing with a userland PoC for the device, you have to throttle reads from RNG register. If reads occur with a 0 delay, then the random value appears fixed. If the delay is too small, then you can watch random values being shifted-in in a barrel like fashion. Unfortunately, the manual did not discuss how long to wait for a value to be ready. I found spinning in a loop for 5000 was too small and witnessed the shifting; while spinning in a loop for 10000 avoided the shift observation. I don't what number of JIFFIES that translates to.

I can calculate the speed and make sure the delay is met in the driver. Thanks a lot for providing this info.

Quoted 17 lines

Finally, from looking at the native Ingenic driver (which was not very impressive), they enabled/disabled the RNG register on demand. There was also a [possible related] note in the manual about not applying VCC for over a second. I can only say "possibly related" because I was not sure if the register was part of the controller they were discussing. The userland PoC worked fine when enabling/disabling the RNG register. So I'm not sure about this (from jz4780_rng_probe):

drivers/char/hw_random/jz4780-rng.c new file +3 -0
+       platform_set_drvdata(pdev, jz4780_rng);
+       jz4780_rng_writel(jz4780_rng, 1, REG_RNG_CTRL);
+       ret = hwrng_register(&jz4780_rng->rng);

And this (from jz4780_rng_remove):

drivers/char/hw_random/jz4780-rng.c new file +2 -0
+       jz4780_rng_writel(jz4780_rng, 0, REG_RNG_CTRL);
+       hwrng_unregister(&jz4780_rng->rng);
Quoted 1 line

Anyway, I hope that helps you avoid some land mines (if they are present).

Looking at JZ4780 Programmers manual I could not find any info about VCC. Can you point me to it?
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
In reply to PrasannaKumar Muralidharan
Quoted 6 lines · PrasannaKumar Muralidharan

This patch adds support for hardware random number generator present in JZ4780 SoC.

---
...

drivers/char/hw_random/jz4780-rng.c line 45

Quoted 8 lines
drivers/char/hw_random/jz4780-rng.c new file +8 -0
+static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+       struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng,
+                                                       rng);
+       u32 *data = buf;
+       *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
+       return 4;
+}

My bad, I should have spotted this earlier....

i686, x86_64 and some ARM will sometimes define a macro indicating unaligned data access is allowed. For example, see __ARM_FEATURE_UNALIGNED (cf.,
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0774f/chr1383660321827.html)
. MIPSEL does not define such a macro.

# MIPS ci20 creator with GCC 4.6
$ gcc -march=native -dM -E - </dev/null | grep -i align #define __BIGGEST_ALIGNMENT__ 8

If the MIPS CPU does not tolerate unaligned data access, then the following could SIGBUS:

drivers/char/hw_random/jz4780-rng.c line 43

Quoted 2 lines
drivers/char/hw_random/jz4780-rng.c new file +2 -0
+       u32 *data = buf;
+       *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);

If GCC emits code that uses the MIPS unaligned load and store instructions, then there's probably going to be a performance penalty.

Regardless of what the CPU tolerates, I believe unaligned data access is undefined behavior in C/C++. I believe you should memcpy the value into the buffer.

Jeff

#11 lore raw
Quoted 14 lines

__ARM_FEATURE_UNALIGNED (cf.,
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0774f/chr1383660321827.html)
. MIPSEL does not define such a macro.

# MIPS ci20 creator with GCC 4.6
$ gcc -march=native -dM -E - </dev/null | grep -i align #define __BIGGEST_ALIGNMENT__ 8

If the MIPS CPU does not tolerate unaligned data access, then the following could SIGBUS:

drivers/char/hw_random/jz4780-rng.c line 43

Quoted 2 lines
drivers/char/hw_random/jz4780-rng.c new file +2 -0
+       u32 *data = buf;
+       *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA);
Quoted 6 lines

If GCC emits code that uses the MIPS unaligned load and store instructions, then there's probably going to be a performance penalty.

Regardless of what the CPU tolerates, I believe unaligned data access is undefined behavior in C/C++. I believe you should memcpy the value into the buffer.

I am not sure whether this is required. 'buf' is part of a structure so I guess it is properly aligned by padding. Not sure though, will look about this.