hwrng: npcm: add NPCM RNG driver support
Subsystems (4)
Tomer Maimon
maintainerv2 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.
Addressed comments from:.
- Daniel Thompson, Milton Miller II : https://patchwork.ozlabs.org/patch/1154598/
Changes since version 1:
- Define timout in real-world units.
- Using readl_poll_timeout in rng_read function.
- Honor wait parameter in rng_read function.
- Using local variable instead of #ifndef.
- Remove probe print.
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 | 203 ++++++++++++++++++ 4 files changed, 234 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
Tomer Maimon
maintainerPATCH v2 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
@@ -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
Re: [PATCH v2 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).
Signed-off-by Tomer Maimon <[email protected]>--- .../bindings/rng/nuvoton,npcm-rng.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txtDocumentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +0
-0
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.
There are pending unreplied review comments about this property (my own as it happens):
https://patchwork.kernel.org/patch/11119371/
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
Tomer Maimon
maintainerRe: [PATCH v2 1/2] dt-binding: hwrng: add NPCM RNG documentation
Hi Daniel,
Sorry but I have probably miss it, thanks a lot for your comment
Quoted 31 lines · Daniel Thompson
Quoted 8 lines · Tomer Maimon
Added device tree binding documentation for Nuvoton BMC NPCM Random Number Generator (RNG).
Signed-off-by Tomer Maimon <[email protected]>--- .../bindings/rng/nuvoton,npcm-rng.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txtReview
Quoted 2 lines
(quoted diff) +0
-0
b/Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txtDocumentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt line 5
Quoted 10 lines
new file mode 100644
index 000000000000..a697b4425fb3Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +5
-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 theregisters.
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt
Quoted 5 lines
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +5
-0+ +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 to1000.
Quoted 5 lines
There are pending unreplied review comments about this property (my own as it happens):
https://patchwork.kernel.org/patch/11119371/No, there isn't different SoCs.
we had checked the quality of the hwrng and the results we got are set as default.
we been asked from one of our client to have a dynamic quality, they will like to be more strict when using the hwrng.
is it problematic to add it?
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?
Quoted 13 lines
Daniel.
Quoted 10 lines
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +7
-1+ +Example: + +rng: rng@f000b000 { + compatible = "nuvoton,npcm750-rng"; + reg = <0xf000b000 0x8>; +}; --2.18.0
Re: [PATCH v2 1/2] dt-binding: hwrng: add NPCM RNG documentation
Quoted 42 lines · Tomer Maimon
Hi Daniel,
Sorry but I have probably miss it, thanks a lot for your comment
Quoted 35 lines · Daniel Thompson
Quoted 8 lines · Tomer Maimon
Added device tree binding documentation for Nuvoton BMC NPCM Random Number Generator (RNG).
Signed-off-by Tomer Maimon <[email protected]>--- .../bindings/rng/nuvoton,npcm-rng.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txtReview
Quoted 2 lines
(quoted diff) +0
-0
b/Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txtDocumentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt line 5
Quoted 10 lines
new file mode 100644
index 000000000000..a697b4425fb3Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +5
-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 theregisters.
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt
Quoted 5 lines
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +5
-0+ +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 to1000.
There are pending unreplied review comments about this property (my own as it happens):
https://patchwork.kernel.org/patch/11119371/
Quoted 6 lines
Quoted 1 line
No, there isn't different SoCs.
we had checked the quality of the hwrng and the results we got are set as default.
we been asked from one of our client to have a dynamic quality, they will like to be more strict when using the hwrng.
is it problematic to add it?
It's a slightly grey area but in general the role of devicetree is to describe the hardware. This parameter is not doing that.
If you view the quality assessment of this RNG to be a user preference it is better set the quality to zero which is what the vast majority of hwrng devices do. When the driver sets the quality to zero then the kernel does not stir the entropy pool automatically... instead it relies on the userspace rngd to do that. If the user wants the kernel to stir the pool automatically then the quality can be set using the default_quality kernel parameter.
Daniel.
Quoted 20 lines
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?
Quoted 13 lines
Daniel.
Quoted 10 lines
Documentation/devicetree/bindings/rng/nuvoton,npcm-rng.txt new file +7
-1+ +Example: + +rng: rng@f000b000 { + compatible = "nuvoton,npcm750-rng"; + reg = <0xf000b000 0x8>; +}; --2.18.0
Tomer Maimon
maintainerPATCH v2 2/2 hwrng: npcm: add NPCM RNG driver
In reply to Tomer MaimonAdd 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 | 203 ++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/char/hw_random/npcm-rng.c
drivers/char/hw_random/Kconfig +13 -0
@@ -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
@@ -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 +203 -0
203 lines not on this page · raw message
Signature · 1 line
2.18.0
Re: [PATCH v2 2/2] hwrng: npcm: add NPCM RNG driver
Quoted 118 lines · Tomer Maimon
Add Nuvoton NPCM BMC Random Number Generator(RNG) driver.
Signed-off-by Tomer Maimon <[email protected]>--- drivers/char/hw_random/Kconfig | 13 ++ drivers/char/hw_random/Makefile | 1 + drivers/char/hw_random/npcm-rng.c | 203 ++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/char/hw_random/npcm-rng.cdrivers/char/hw_random/Kconfig +13
-0@@ -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_RANDOMdrivers/char/hw_random/Makefile +1
-0@@ -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.odrivers/char/hw_random/npcm-rng.c new file +69
-0@@ -0,0 +1,203 @@ +// 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_USEC 20000 +#define NPCM_RNG_POLL_USEC 1000 + +#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 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 ; +int ready; + +pm_runtime_get_sync ((struct device *)priv->rng.priv); + +while (max >=sizeof (u32)) { + ready =readl (priv->base +NPCM_RNGCS_REG ) & +NPCM_RNG_DATA_VALID ; +if (!ready) {
drivers/char/hw_random/npcm-rng.c line 79
Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10
-0+if (wait) { +if (readl_poll_timeout (priv->base +NPCM_RNGCS_REG , + ready, + ready &NPCM_RNG_DATA_VALID , +NPCM_RNG_POLL_USEC , +NPCM_RNG_TIMEOUT_USEC )) +break ; + }else { +break ; + }
This would read easier without breaking on the else clause:
if (!wait) break; if (readl_poll_timeout(...)) break;
Quoted 40 lines
drivers/char/hw_random/npcm-rng.c new file +40
-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; +bool pm_dis = false; +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 + pm_dis = true; + 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))
drivers/char/hw_random/npcm-rng.c line 129
Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10
-0+ priv->rng.quality =1000 ; + else + priv->rng.quality = quality; + +writel (NPCM_RNG_M1ROSEL , priv->base +NPCM_RNGMODE_REG ); +if (pm_dis) +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 );
This still doesn't seem right and its not simply because pm_dis is an obfuscated way to write IS_ENABLED(CONFIG_PM).
I'd like to understand why the call to pm_runtime_get_sync() isn't resulting in the device resume callback running... it is simply because the hwrng_register() happens before the pm_runtime_enable() ?
Daniel.
Quoted 77 lines
drivers/char/hw_random/npcm-rng.c new file +74
-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); + +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
Re: [PATCH v2 2/2] hwrng: npcm: add NPCM RNG driver
In reply to Tomer MaimonQuoted 2 lines · Tomer Maimon
drivers/char/hw_random/npcm-rng.c new file +2
-0+#define NPCM_RNG_TIMEOUT_USEC 20000 +#define NPCM_RNG_POLL_USEC 1000
...
Quoted 34 lines
drivers/char/hw_random/npcm-rng.c new file +23
-0+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 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 ; +int ready; + +pm_runtime_get_sync ((struct device *)priv->rng.priv); + +while (max >=sizeof (u32)) { + ready =readl (priv->base +NPCM_RNGCS_REG ) & +NPCM_RNG_DATA_VALID ;
drivers/char/hw_random/npcm-rng.c line 78
Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10
-0+if (!ready) { +if (wait) { +if (readl_poll_timeout (priv->base +NPCM_RNGCS_REG , + ready, + ready &NPCM_RNG_DATA_VALID , +NPCM_RNG_POLL_USEC , +NPCM_RNG_TIMEOUT_USEC )) +break ; + }else { +break ;
This break is too far from the condition and deeply nested to follow.
And looking further, readl_poll_timeout will read and check the condition before calling usleep, so the the initial readl and check is redundant
Rearrange to make wait determine if you call readl_poll_timeout or readl / compare / break.
Quoted 28 lines
drivers/char/hw_random/npcm-rng.c new file +28
-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; +bool pm_dis = false; +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 );
drivers/char/hw_random/npcm-rng.c line 116
Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10
-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 + pm_dis = true; + priv->rng.init = npcm_rng_init; + priv->rng.cleanup = npcm_rng_cleanup; +#endif
if you move this down you can use one if (ENABLED_CONFIG_PM) {}
drivers/char/hw_random/npcm-rng.c line 129
Quoted 13 lines
drivers/char/hw_random/npcm-rng.c new file +13
-0+ 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 ); +if (pm_dis) +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 );
wait ... if we know the whole value here, why read/modify/write the value
in the init and cleanup hook? Save the io read and write the known value
... define the value to be written for clarity between enable/disable if
needed
drivers/char/hw_random/npcm-rng.c line 134
Quoted 5 lines
drivers/char/hw_random/npcm-rng.c new file +5
-0+ + ret =devm_hwrng_register (&pdev->dev, &priv->rng); +if (ret) { +dev_err (&pdev->dev,"Failed to register rng device: %d\n" , + ret);
need to disable if CONFIG_PM ?
drivers/char/hw_random/npcm-rng.c line 138
Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4
-0+return ret; + } + +dev_set_drvdata (&pdev->dev, priv);
This should probably be before the register.
drivers/char/hw_random/npcm-rng.c line 139
Quoted 1 line
drivers/char/hw_random/npcm-rng.c new file +1
-0+pm_runtime_set_autosuspend_delay (&pdev->dev,100 );
So every 100ms power off, and if userspace does a read we will poll every 1ms for upto 20ms.
If userspace says try once a second with -ENODELAY so no wait, it never gets data.
Oh, and yes, rngd sets non-blocking, polls the descriptors, and falls back to slow expensive software if no hardware says it has data ready.
drivers/char/hw_random/npcm-rng.c line 150
Quoted 11 lines
drivers/char/hw_random/npcm-rng.c new file +11
-0+pm_runtime_use_autosuspend (&pdev->dev); +pm_runtime_enable (&pdev->dev); + +return 0 ; +} + +static int npcm_rng_remove (struct platform_device *pdev) +{ +struct npcm_rng *priv =platform_get_drvdata (pdev); + +hwrng_unregister (&priv->rng);
you did devm register, but call unregister directly?
Quoted 32 lines
drivers/char/hw_random/npcm-rng.c new file +32
-0+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[] = {
drivers/char/hw_random/npcm-rng.c line 192
Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10
-0+ { .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 ,
module_platform_driver will set owner, remove it here.
Quoted 15 lines
drivers/char/hw_random/npcm-rng.c new file +11
-0+ .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
milton
Re: [PATCH v2 2/2] hwrng: npcm: add NPCM RNG driver
Quoted 136 lines · Milton Miller II
Quoted 2 lines · Tomer Maimon
drivers/char/hw_random/npcm-rng.c new file +2
-0+#define NPCM_RNG_TIMEOUT_USEC 20000 +#define NPCM_RNG_POLL_USEC 1000...
Quoted 34 lines
drivers/char/hw_random/npcm-rng.c new file +23
-0+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 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 ; +int ready; + +pm_runtime_get_sync ((struct device *)priv->rng.priv); + +while (max >=sizeof (u32)) { + ready =readl (priv->base +NPCM_RNGCS_REG ) & +NPCM_RNG_DATA_VALID ;drivers/char/hw_random/npcm-rng.c line 78
Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10
-0+if (!ready) { +if (wait) { +if (readl_poll_timeout (priv->base +NPCM_RNGCS_REG , + ready, + ready &NPCM_RNG_DATA_VALID , +NPCM_RNG_POLL_USEC , +NPCM_RNG_TIMEOUT_USEC )) +break ; + }else { +break ;This break is too far from the condition and deeply nested to follow.
And looking further,
readl_poll_timeoutwill read and check the condition before calling usleep, so the the initial readl and check is redundantRearrange to make wait determine if you call
readl_poll_timeoutor readl / compare / break.Quoted 28 lines
drivers/char/hw_random/npcm-rng.c new file +28
-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; +bool pm_dis = false; +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 );drivers/char/hw_random/npcm-rng.c line 116
Quoted 10 lines
drivers/char/hw_random/npcm-rng.c new file +10
-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 + pm_dis = true; + priv->rng.init = npcm_rng_init; + priv->rng.cleanup = npcm_rng_cleanup; +#endif if you move this down you can use one if (
ENABLED_CONFIG_PM) {}drivers/char/hw_random/npcm-rng.c line 129
Quoted 13 lines
drivers/char/hw_random/npcm-rng.c new file +13
-0+ 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 ); +if (pm_dis) +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 );wait ... if we know the whole value here, why read/modify/write the value
in the init and cleanup hook? Save the io read and write the known value
... define the value to be written for clarity between enable/disable if
neededdrivers/char/hw_random/npcm-rng.c line 134
Quoted 5 lines
drivers/char/hw_random/npcm-rng.c new file +5
-0+ + ret =devm_hwrng_register (&pdev->dev, &priv->rng); +if (ret) { +dev_err (&pdev->dev,"Failed to register rng device: %d\n" , + ret);need to disable if
CONFIG_PM?drivers/char/hw_random/npcm-rng.c line 138
Quoted 4 lines
drivers/char/hw_random/npcm-rng.c new file +4
-0+return ret; + } + +dev_set_drvdata (&pdev->dev, priv);This should probably be before the register.
Quoted 7 lines
drivers/char/hw_random/npcm-rng.c line 139
Quoted 1 line
drivers/char/hw_random/npcm-rng.c new file +1
-0+pm_runtime_set_autosuspend_delay (&pdev->dev,100 );So every 100ms power off, and if userspace does a read we will poll every 1ms for upto 20ms.
If userspace says try once a second with
-ENODELAYso no wait, it never gets data.
I didn't follow this.
In the time before the device is suspended it should have generated data and this can be sent to the userspace. Providing the suspend delay is longer than the buffer size of the hardware then there won't necessarily be performance problems because the device is "full" when it is suspended.
Of course if the hardware loses state when it is suspended then the driver would need extra code on the PM paths to preserve the data...
Daniel.