* [PATCH] [2.6.22] pasemi: hardware rng driver
@ 2007-04-25 20:45 Olof Johansson
2007-04-25 23:38 ` Arnd Bergmann
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Olof Johansson @ 2007-04-25 20:45 UTC (permalink / raw)
To: mb; +Cc: linuxppc-dev, egor
Driver for the on-chip hardware random number generator on PA Semi
PA6T-1682M.
Signed-off-by: Egor Martovetsky <egor@pasemi.com>
Signed-off-by: Olof Johansson <olof@lixom.net>
Index: powerpc/drivers/char/hw_random/Kconfig
===================================================================
--- powerpc.orig/drivers/char/hw_random/Kconfig
+++ powerpc/drivers/char/hw_random/Kconfig
@@ -91,3 +91,17 @@ config HW_RANDOM_OMAP
module will be called omap-rng.
If unsure, say Y.
+
+config HW_RANDOM_PASEMI
+ tristate "PA Semi HW Random Number Generator support"
+ depends on HW_RANDOM && PPC_PASEMI
+ default HW_RANDOM
+ ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on PA6T-1682M processor.
+
+ To compile this driver as a module, choose M here: the
+ module will be called pasemi-rng.
+
+ If unsure, say Y.
+
Index: powerpc/drivers/char/hw_random/Makefile
===================================================================
--- powerpc.orig/drivers/char/hw_random/Makefile
+++ powerpc/drivers/char/hw_random/Makefile
@@ -10,3 +10,4 @@ obj-$(CONFIG_HW_RANDOM_GEODE) += geode-r
obj-$(CONFIG_HW_RANDOM_VIA) += via-rng.o
obj-$(CONFIG_HW_RANDOM_IXP4XX) += ixp4xx-rng.o
obj-$(CONFIG_HW_RANDOM_OMAP) += omap-rng.o
+obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o
Index: powerpc/drivers/char/hw_random/pasemi-rng.c
===================================================================
--- /dev/null
+++ powerpc/drivers/char/hw_random/pasemi-rng.c
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2006-2007 PA Semi, Inc
+ *
+ * Maintained by: Olof Johansson <olof@lixom.net>
+ *
+ * Driver for the PWRficient onchip rng
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <asm/of_platform.h>
+#include <asm/io.h>
+
+#define SDCRNG_CTL_REG 0x00
+#define SDCRNG_CTL_FVLD_M 0x0000f000
+#define SDCRNG_CTL_FVLD_S 12
+#define SDCRNG_CTL_KSZ 0x00000800
+#define SDCRNG_CTL_RSRC_CRG 0x00000010
+#define SDCRNG_CTL_RSRC_RRG 0x00000000
+#define SDCRNG_CTL_CE 0x00000004
+#define SDCRNG_CTL_RE 0x00000002
+#define SDCRNG_CTL_DR 0x00000001
+#define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
+#define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
+#define SDCRNG_VAL_REG 0x20
+
+#define MODULE_NAME "pasemi_rng"
+
+static void __iomem *rng_regs;
+
+static int pasemi_rng_data_present(struct hwrng *rng)
+{
+ return (in_le32(rng_regs + SDCRNG_CTL_REG)
+ & SDCRNG_CTL_FVLD_M) ? 1 : 0;
+}
+
+static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
+{
+ *data = in_le32(rng_regs + SDCRNG_VAL_REG);
+ return 4;
+}
+
+static int pasemi_rng_init(struct hwrng *rng)
+{
+ u32 ctl;
+
+ ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
+ out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
+ out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
+
+ return 0;
+}
+
+static void pasemi_rng_cleanup(struct hwrng *rng)
+{
+ u32 ctl;
+
+ ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
+ out_le32(rng_regs + SDCRNG_CTL_REG,
+ in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
+}
+
+static struct hwrng pasemi_rng = {
+ .name = MODULE_NAME,
+ .init = pasemi_rng_init,
+ .cleanup = pasemi_rng_cleanup,
+ .data_present = pasemi_rng_data_present,
+ .data_read = pasemi_rng_data_read,
+};
+
+static int __devinit rng_probe(struct of_device *ofdev,
+ const struct of_device_id *match)
+{
+ struct device_node *rng_np;
+ struct resource res;
+ int err = 0;
+
+ rng_np = of_find_compatible_node(NULL, "rng", "1682m-rng");
+ if (!rng_np)
+ return -ENODEV;
+
+ err = of_address_to_resource(rng_np, 0, &res);
+ of_node_put(rng_np);
+
+ if (err)
+ return -EINVAL;
+
+ if (!rng_regs)
+ rng_regs = ioremap(res.start, 0x100);
+
+ if (!rng_regs)
+ return -EPERM;
+
+ printk(KERN_INFO "Registering PA Semi RNG\n");
+
+ return hwrng_register(&pasemi_rng);
+}
+
+static int rng_remove(struct of_device *dev)
+{
+ iounmap(rng_regs);
+ hwrng_unregister(&pasemi_rng);
+
+ return 0;
+}
+
+static struct of_device_id rng_match[] =
+{
+ {
+ .compatible = "1682m-rng",
+ },
+ {},
+};
+
+static struct of_platform_driver rng_driver =
+{
+ .name = "pasemi-rng",
+ .match_table = rng_match,
+ .probe = rng_probe,
+ .remove = rng_remove,
+};
+
+int rng_init(void)
+{
+ return of_register_platform_driver(&rng_driver);
+}
+
+void rng_exit(void)
+{
+ of_unregister_platform_driver(&rng_driver);
+}
+
+device_initcall(rng_init);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
+MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] [2.6.22] pasemi: hardware rng driver
2007-04-25 20:45 [PATCH] [2.6.22] pasemi: hardware rng driver Olof Johansson
@ 2007-04-25 23:38 ` Arnd Bergmann
2007-04-26 0:09 ` Olof Johansson
2007-04-26 9:23 ` Michael Buesch
2007-04-26 5:37 ` [PATCH v2] " Olof Johansson
2007-04-26 9:22 ` [PATCH] " Michael Buesch
2 siblings, 2 replies; 10+ messages in thread
From: Arnd Bergmann @ 2007-04-25 23:38 UTC (permalink / raw)
To: linuxppc-dev; +Cc: Olof Johansson, egor, mb
On Wednesday 25 April 2007, Olof Johansson wrote:
> +static void __iomem *rng_regs;
> +
> +static int pasemi_rng_data_present(struct hwrng *rng)
> +{
> + return (in_le32(rng_regs + SDCRNG_CTL_REG)
> + & SDCRNG_CTL_FVLD_M) ? 1 : 0;
> +}
It would be nicer to get rid of the global rng_regs variable by sticking
it into rng->priv.
> +static int __devinit rng_probe(struct of_device *ofdev,
> + const struct of_device_id *match)
> +{
> + struct device_node *rng_np;
> + struct resource res;
> + int err = 0;
> +
> + rng_np = of_find_compatible_node(NULL, "rng", "1682m-rng");
> + if (!rng_np)
> + return -ENODEV;
I would guess that the call to of_find_compatible_node is entirely bogus
here, because the device is already passed in as ofdev in to the probe
function.
> +int rng_init(void)
> +{
> + return of_register_platform_driver(&rng_driver);
> +}
> +
> +void rng_exit(void)
> +{
> + of_unregister_platform_driver(&rng_driver);
> +}
> +
> +device_initcall(rng_init);
rng_init and rng_exit should be static
rng_init should be __init
rng_exit should be __exit
Since the driver is tristate in Kconfig, it would be more conventional to
use module_init() instead of device_initcall().
rng_exit needs to be marked as module_exit() to allow unloading the driver.
Arnd <><
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] [2.6.22] pasemi: hardware rng driver
2007-04-25 23:38 ` Arnd Bergmann
@ 2007-04-26 0:09 ` Olof Johansson
2007-04-26 9:23 ` Michael Buesch
1 sibling, 0 replies; 10+ messages in thread
From: Olof Johansson @ 2007-04-26 0:09 UTC (permalink / raw)
To: Arnd Bergmann; +Cc: linuxppc-dev, egor, mb
On Thu, Apr 26, 2007 at 01:38:31AM +0200, Arnd Bergmann wrote:
> On Wednesday 25 April 2007, Olof Johansson wrote:
>
> > +static void __iomem *rng_regs;
> > +
> > +static int pasemi_rng_data_present(struct hwrng *rng)
> > +{
> > + return (in_le32(rng_regs + SDCRNG_CTL_REG)
> > + & SDCRNG_CTL_FVLD_M) ? 1 : 0;
> > +}
>
> It would be nicer to get rid of the global rng_regs variable by sticking
> it into rng->priv.
Good point.
> > +static int __devinit rng_probe(struct of_device *ofdev,
> > + const struct of_device_id *match)
> > +{
> > + struct device_node *rng_np;
> > + struct resource res;
> > + int err = 0;
> > +
> > + rng_np = of_find_compatible_node(NULL, "rng", "1682m-rng");
> > + if (!rng_np)
> > + return -ENODEV;
>
> I would guess that the call to of_find_compatible_node is entirely bogus
> here, because the device is already passed in as ofdev in to the probe
> function.
Yup. Leftover from pre-of_platform
> > +int rng_init(void)
> > +{
> > + return of_register_platform_driver(&rng_driver);
> > +}
> > +
> > +void rng_exit(void)
> > +{
> > + of_unregister_platform_driver(&rng_driver);
> > +}
> > +
> > +device_initcall(rng_init);
>
> rng_init and rng_exit should be static
> rng_init should be __init
> rng_exit should be __exit
> Since the driver is tristate in Kconfig, it would be more conventional to
> use module_init() instead of device_initcall().
> rng_exit needs to be marked as module_exit() to allow unloading the driver.
Yup, will fix.
Thanks.
-Olof
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v2] [2.6.22] pasemi: hardware rng driver
2007-04-25 20:45 [PATCH] [2.6.22] pasemi: hardware rng driver Olof Johansson
2007-04-25 23:38 ` Arnd Bergmann
@ 2007-04-26 5:37 ` Olof Johansson
2007-04-26 8:50 ` Arnd Bergmann
2007-04-26 20:59 ` [PATCH v3] " Olof Johansson
2007-04-26 9:22 ` [PATCH] " Michael Buesch
2 siblings, 2 replies; 10+ messages in thread
From: Olof Johansson @ 2007-04-26 5:37 UTC (permalink / raw)
To: mb; +Cc: linuxppc-dev, egor, arnd
Driver for the on-chip hardware random number generator on PA Semi
PA6T-1682M.
Signed-off-by: Egor Martovetsky <egor@pasemi.com>
Signed-off-by: Olof Johansson <olof@lixom.net>
---
Changes since last version:
* Moved register pointer to hwrng->priv
* Cleanups (__init/__exit, module_init/exit)
Index: powerpc/drivers/char/hw_random/Kconfig
===================================================================
--- powerpc.orig/drivers/char/hw_random/Kconfig
+++ powerpc/drivers/char/hw_random/Kconfig
@@ -91,3 +91,17 @@ config HW_RANDOM_OMAP
module will be called omap-rng.
If unsure, say Y.
+
+config HW_RANDOM_PASEMI
+ tristate "PA Semi HW Random Number Generator support"
+ depends on HW_RANDOM && PPC_PASEMI
+ default HW_RANDOM
+ ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on PA6T-1682M processor.
+
+ To compile this driver as a module, choose M here: the
+ module will be called pasemi-rng.
+
+ If unsure, say Y.
+
Index: powerpc/drivers/char/hw_random/Makefile
===================================================================
--- powerpc.orig/drivers/char/hw_random/Makefile
+++ powerpc/drivers/char/hw_random/Makefile
@@ -10,3 +10,4 @@ obj-$(CONFIG_HW_RANDOM_GEODE) += geode-r
obj-$(CONFIG_HW_RANDOM_VIA) += via-rng.o
obj-$(CONFIG_HW_RANDOM_IXP4XX) += ixp4xx-rng.o
obj-$(CONFIG_HW_RANDOM_OMAP) += omap-rng.o
+obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o
Index: powerpc/drivers/char/hw_random/pasemi-rng.c
===================================================================
--- /dev/null
+++ powerpc/drivers/char/hw_random/pasemi-rng.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2006-2007 PA Semi, Inc
+ *
+ * Maintained by: Olof Johansson <olof@lixom.net>
+ *
+ * Driver for the PWRficient onchip rng
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <asm/of_platform.h>
+#include <asm/io.h>
+
+#define SDCRNG_CTL_REG 0x00
+#define SDCRNG_CTL_FVLD_M 0x0000f000
+#define SDCRNG_CTL_FVLD_S 12
+#define SDCRNG_CTL_KSZ 0x00000800
+#define SDCRNG_CTL_RSRC_CRG 0x00000010
+#define SDCRNG_CTL_RSRC_RRG 0x00000000
+#define SDCRNG_CTL_CE 0x00000004
+#define SDCRNG_CTL_RE 0x00000002
+#define SDCRNG_CTL_DR 0x00000001
+#define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
+#define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
+#define SDCRNG_VAL_REG 0x20
+
+#define MODULE_NAME "pasemi_rng"
+
+static int pasemi_rng_data_present(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+
+ return (in_le32(rng_regs + SDCRNG_CTL_REG)
+ & SDCRNG_CTL_FVLD_M) ? 1 : 0;
+}
+
+static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ *data = in_le32(rng_regs + SDCRNG_VAL_REG);
+ return 4;
+}
+
+static int pasemi_rng_init(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ u32 ctl;
+
+ ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
+ out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
+ out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
+
+ return 0;
+}
+
+static void pasemi_rng_cleanup(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ u32 ctl;
+
+ ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
+ out_le32(rng_regs + SDCRNG_CTL_REG,
+ in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
+}
+
+static struct hwrng pasemi_rng = {
+ .name = MODULE_NAME,
+ .init = pasemi_rng_init,
+ .cleanup = pasemi_rng_cleanup,
+ .data_present = pasemi_rng_data_present,
+ .data_read = pasemi_rng_data_read,
+};
+
+static int __devinit rng_probe(struct of_device *ofdev,
+ const struct of_device_id *match)
+{
+ struct device_node *rng_np = ofdev->node;
+ struct resource res;
+ int err = 0;
+
+ err = of_address_to_resource(rng_np, 0, &res);
+ if (err)
+ return -EINVAL;
+
+ pasemi_rng.priv = (unsigned long)ioremap(res.start, 0x100);
+
+ if (pasemi_rng.priv == 0)
+ return -EPERM;
+
+ printk(KERN_INFO "Registering PA Semi RNG\n");
+
+ return hwrng_register(&pasemi_rng);
+}
+
+static int __devexit rng_remove(struct of_device *dev)
+{
+ void __iomem *rng_regs = (void __iomem *)pasemi_rng.priv;
+
+ iounmap(rng_regs);
+ hwrng_unregister(&pasemi_rng);
+
+ return 0;
+}
+
+static struct of_device_id rng_match[] =
+{
+ {
+ .compatible = "1682m-rng",
+ },
+ {},
+};
+
+static struct of_platform_driver rng_driver =
+{
+ .name = "pasemi-rng",
+ .match_table = rng_match,
+ .probe = rng_probe,
+ .remove = rng_remove,
+};
+
+static int __init rng_init(void)
+{
+ return of_register_platform_driver(&rng_driver);
+}
+module_init(rng_init);
+
+static void __exit rng_exit(void)
+{
+ of_unregister_platform_driver(&rng_driver);
+}
+module_exit(rng_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
+MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v2] [2.6.22] pasemi: hardware rng driver
2007-04-26 5:37 ` [PATCH v2] " Olof Johansson
@ 2007-04-26 8:50 ` Arnd Bergmann
2007-04-26 20:59 ` [PATCH v3] " Olof Johansson
1 sibling, 0 replies; 10+ messages in thread
From: Arnd Bergmann @ 2007-04-26 8:50 UTC (permalink / raw)
To: Olof Johansson; +Cc: linuxppc-dev, egor, mb
On Thursday 26 April 2007, Olof Johansson wrote:
> Driver for the on-chip hardware random number generator on PA Semi
> PA6T-1682M.
>
> Signed-off-by: Egor Martovetsky <egor@pasemi.com>
> Signed-off-by: Olof Johansson <olof@lixom.net>
Acked-by: Arnd Bergmann <arnd@arndb.de>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] [2.6.22] pasemi: hardware rng driver
2007-04-25 20:45 [PATCH] [2.6.22] pasemi: hardware rng driver Olof Johansson
2007-04-25 23:38 ` Arnd Bergmann
2007-04-26 5:37 ` [PATCH v2] " Olof Johansson
@ 2007-04-26 9:22 ` Michael Buesch
2007-04-26 20:46 ` Olof Johansson
2 siblings, 1 reply; 10+ messages in thread
From: Michael Buesch @ 2007-04-26 9:22 UTC (permalink / raw)
To: Olof Johansson; +Cc: linuxppc-dev, egor
On Wednesday 25 April 2007 22:45:12 Olof Johansson wrote:
> +static int __devinit rng_probe(struct of_device *ofdev,
> + const struct of_device_id *match)
> +{
> + struct device_node *rng_np;
> + struct resource res;
> + int err = 0;
> +
> + rng_np = of_find_compatible_node(NULL, "rng", "1682m-rng");
> + if (!rng_np)
> + return -ENODEV;
> +
> + err = of_address_to_resource(rng_np, 0, &res);
> + of_node_put(rng_np);
> +
> + if (err)
> + return -EINVAL;
I think EINVAL is not the correct error code. I'd suggest ENODEV.
> + if (!rng_regs)
> + rng_regs = ioremap(res.start, 0x100);
> +
> + if (!rng_regs)
> + return -EPERM;
I think EPERM is not the correct error code. I'd suggest ENOMEM.
> + printk(KERN_INFO "Registering PA Semi RNG\n");
> +
> + return hwrng_register(&pasemi_rng);
Resource leak.
Please do something like
err = hwrng_register(&pasemi_rng);
if (err)
iounmap(rng_regs);
return err;
> +}
> +
> +static int rng_remove(struct of_device *dev)
> +{
> + iounmap(rng_regs);
> + hwrng_unregister(&pasemi_rng);
Swap these to prevent race conditions.
> +
> + return 0;
> +}
--
Greetings Michael.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] [2.6.22] pasemi: hardware rng driver
2007-04-25 23:38 ` Arnd Bergmann
2007-04-26 0:09 ` Olof Johansson
@ 2007-04-26 9:23 ` Michael Buesch
1 sibling, 0 replies; 10+ messages in thread
From: Michael Buesch @ 2007-04-26 9:23 UTC (permalink / raw)
To: Arnd Bergmann; +Cc: Olof Johansson, linuxppc-dev, egor
On Thursday 26 April 2007 01:38:31 Arnd Bergmann wrote:
> On Wednesday 25 April 2007, Olof Johansson wrote:
>
> > +static void __iomem *rng_regs;
> > +
> > +static int pasemi_rng_data_present(struct hwrng *rng)
> > +{
> > + return (in_le32(rng_regs + SDCRNG_CTL_REG)
> > + & SDCRNG_CTL_FVLD_M) ? 1 : 0;
> > +}
>
> It would be nicer to get rid of the global rng_regs variable by sticking
> it into rng->priv.
Yeah, I think that would be better. Saves one global variable.
--
Greetings Michael.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] [2.6.22] pasemi: hardware rng driver
2007-04-26 9:22 ` [PATCH] " Michael Buesch
@ 2007-04-26 20:46 ` Olof Johansson
0 siblings, 0 replies; 10+ messages in thread
From: Olof Johansson @ 2007-04-26 20:46 UTC (permalink / raw)
To: Michael Buesch; +Cc: linuxppc-dev, egor
Hi Michael,
All good points, fixed in v3 (posted shortly).
Thanks,
-Olof
On Thu, Apr 26, 2007 at 11:22:03AM +0200, Michael Buesch wrote:
> On Wednesday 25 April 2007 22:45:12 Olof Johansson wrote:
> > +static int __devinit rng_probe(struct of_device *ofdev,
> > + const struct of_device_id *match)
> > +{
> > + struct device_node *rng_np;
> > + struct resource res;
> > + int err = 0;
> > +
> > + rng_np = of_find_compatible_node(NULL, "rng", "1682m-rng");
> > + if (!rng_np)
> > + return -ENODEV;
> > +
> > + err = of_address_to_resource(rng_np, 0, &res);
> > + of_node_put(rng_np);
> > +
> > + if (err)
> > + return -EINVAL;
>
> I think EINVAL is not the correct error code. I'd suggest ENODEV.
>
> > + if (!rng_regs)
> > + rng_regs = ioremap(res.start, 0x100);
> > +
> > + if (!rng_regs)
> > + return -EPERM;
>
> I think EPERM is not the correct error code. I'd suggest ENOMEM.
>
> > + printk(KERN_INFO "Registering PA Semi RNG\n");
> > +
> > + return hwrng_register(&pasemi_rng);
>
> Resource leak.
> Please do something like
>
> err = hwrng_register(&pasemi_rng);
> if (err)
> iounmap(rng_regs);
> return err;
>
> > +}
> > +
> > +static int rng_remove(struct of_device *dev)
> > +{
> > + iounmap(rng_regs);
> > + hwrng_unregister(&pasemi_rng);
>
> Swap these to prevent race conditions.
>
> > +
> > + return 0;
> > +}
>
>
>
> --
> Greetings Michael.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v3] [2.6.22] pasemi: hardware rng driver
2007-04-26 5:37 ` [PATCH v2] " Olof Johansson
2007-04-26 8:50 ` Arnd Bergmann
@ 2007-04-26 20:59 ` Olof Johansson
1 sibling, 0 replies; 10+ messages in thread
From: Olof Johansson @ 2007-04-26 20:59 UTC (permalink / raw)
To: mb; +Cc: linuxppc-dev, egor, arnd
Driver for the on-chip hardware random number generator on PA Semi
PA6T-1682M.
Signed-off-by: Egor Martovetsky <egor@pasemi.com>
Signed-off-by: Olof Johansson <olof@lixom.net>
---
v3:
* error return codes
* iounmap on register fail
* fix iounmap race on remove
v2:
* Moved register pointer to hwrng->priv
* Cleanups (__init/__exit, module_init/exit)
Index: powerpc/drivers/char/hw_random/Kconfig
===================================================================
--- powerpc.orig/drivers/char/hw_random/Kconfig
+++ powerpc/drivers/char/hw_random/Kconfig
@@ -91,3 +91,17 @@ config HW_RANDOM_OMAP
module will be called omap-rng.
If unsure, say Y.
+
+config HW_RANDOM_PASEMI
+ tristate "PA Semi HW Random Number Generator support"
+ depends on HW_RANDOM && PPC_PASEMI
+ default HW_RANDOM
+ ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on PA6T-1682M processor.
+
+ To compile this driver as a module, choose M here: the
+ module will be called pasemi-rng.
+
+ If unsure, say Y.
+
Index: powerpc/drivers/char/hw_random/Makefile
===================================================================
--- powerpc.orig/drivers/char/hw_random/Makefile
+++ powerpc/drivers/char/hw_random/Makefile
@@ -10,3 +10,4 @@ obj-$(CONFIG_HW_RANDOM_GEODE) += geode-r
obj-$(CONFIG_HW_RANDOM_VIA) += via-rng.o
obj-$(CONFIG_HW_RANDOM_IXP4XX) += ixp4xx-rng.o
obj-$(CONFIG_HW_RANDOM_OMAP) += omap-rng.o
+obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o
Index: powerpc/drivers/char/hw_random/pasemi-rng.c
===================================================================
--- /dev/null
+++ powerpc/drivers/char/hw_random/pasemi-rng.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2006-2007 PA Semi, Inc
+ *
+ * Maintained by: Olof Johansson <olof@lixom.net>
+ *
+ * Driver for the PWRficient onchip rng
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <asm/of_platform.h>
+#include <asm/io.h>
+
+#define SDCRNG_CTL_REG 0x00
+#define SDCRNG_CTL_FVLD_M 0x0000f000
+#define SDCRNG_CTL_FVLD_S 12
+#define SDCRNG_CTL_KSZ 0x00000800
+#define SDCRNG_CTL_RSRC_CRG 0x00000010
+#define SDCRNG_CTL_RSRC_RRG 0x00000000
+#define SDCRNG_CTL_CE 0x00000004
+#define SDCRNG_CTL_RE 0x00000002
+#define SDCRNG_CTL_DR 0x00000001
+#define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
+#define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
+#define SDCRNG_VAL_REG 0x20
+
+#define MODULE_NAME "pasemi_rng"
+
+static int pasemi_rng_data_present(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+
+ return (in_le32(rng_regs + SDCRNG_CTL_REG)
+ & SDCRNG_CTL_FVLD_M) ? 1 : 0;
+}
+
+static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ *data = in_le32(rng_regs + SDCRNG_VAL_REG);
+ return 4;
+}
+
+static int pasemi_rng_init(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ u32 ctl;
+
+ ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
+ out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
+ out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
+
+ return 0;
+}
+
+static void pasemi_rng_cleanup(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ u32 ctl;
+
+ ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
+ out_le32(rng_regs + SDCRNG_CTL_REG,
+ in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
+}
+
+static struct hwrng pasemi_rng = {
+ .name = MODULE_NAME,
+ .init = pasemi_rng_init,
+ .cleanup = pasemi_rng_cleanup,
+ .data_present = pasemi_rng_data_present,
+ .data_read = pasemi_rng_data_read,
+};
+
+static int __devinit rng_probe(struct of_device *ofdev,
+ const struct of_device_id *match)
+{
+ void __iomem *rng_regs;
+ struct device_node *rng_np = ofdev->node;
+ struct resource res;
+ int err = 0;
+
+ err = of_address_to_resource(rng_np, 0, &res);
+ if (err)
+ return -ENODEV;
+
+ rng_regs = ioremap(res.start, 0x100);
+
+ if (!rng_regs)
+ return -ENOMEM;
+
+ pasemi_rng.priv = (unsigned long)rng_regs;
+
+ printk(KERN_INFO "Registering PA Semi RNG\n");
+
+ err = hwrng_register(&pasemi_rng);
+
+ if (err)
+ iounmap(rng_regs);
+
+ return err;
+}
+
+static int __devexit rng_remove(struct of_device *dev)
+{
+ void __iomem *rng_regs = (void __iomem *)pasemi_rng.priv;
+
+ hwrng_unregister(&pasemi_rng);
+ iounmap(rng_regs);
+
+ return 0;
+}
+
+static struct of_device_id rng_match[] =
+{
+ {
+ .compatible = "1682m-rng",
+ },
+ {},
+};
+
+static struct of_platform_driver rng_driver =
+{
+ .name = "pasemi-rng",
+ .match_table = rng_match,
+ .probe = rng_probe,
+ .remove = rng_remove,
+};
+
+static int __init rng_init(void)
+{
+ return of_register_platform_driver(&rng_driver);
+}
+module_init(rng_init);
+
+static void __exit rng_exit(void)
+{
+ of_unregister_platform_driver(&rng_driver);
+}
+module_exit(rng_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
+MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 2.6.22] pasemi: hardware rng driver
@ 2007-04-27 10:54 Michael Buesch
0 siblings, 0 replies; 10+ messages in thread
From: Michael Buesch @ 2007-04-27 10:54 UTC (permalink / raw)
To: Andrew Morton; +Cc: Olof Johansson, linuxppc-dev, egor, arnd
From: Olof Johansson <olof@lixom.net>
Driver for the on-chip hardware random number generator on PA Semi
PA6T-1682M.
Signed-off-by: Egor Martovetsky <egor@pasemi.com>
Signed-off-by: Olof Johansson <olof@lixom.net>
Signed-off-by: Michael Buesch <mb@bu3sch.de>
---
This is patch version 3.
v3:
* error return codes
* iounmap on register fail
* fix iounmap race on remove
v2:
* Moved register pointer to hwrng->priv
* Cleanups (__init/__exit, module_init/exit)
Index: powerpc/drivers/char/hw_random/Kconfig
===================================================================
--- powerpc.orig/drivers/char/hw_random/Kconfig
+++ powerpc/drivers/char/hw_random/Kconfig
@@ -91,3 +91,17 @@ config HW_RANDOM_OMAP
module will be called omap-rng.
If unsure, say Y.
+
+config HW_RANDOM_PASEMI
+ tristate "PA Semi HW Random Number Generator support"
+ depends on HW_RANDOM && PPC_PASEMI
+ default HW_RANDOM
+ ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on PA6T-1682M processor.
+
+ To compile this driver as a module, choose M here: the
+ module will be called pasemi-rng.
+
+ If unsure, say Y.
+
Index: powerpc/drivers/char/hw_random/Makefile
===================================================================
--- powerpc.orig/drivers/char/hw_random/Makefile
+++ powerpc/drivers/char/hw_random/Makefile
@@ -10,3 +10,4 @@ obj-$(CONFIG_HW_RANDOM_GEODE) += geode-r
obj-$(CONFIG_HW_RANDOM_VIA) += via-rng.o
obj-$(CONFIG_HW_RANDOM_IXP4XX) += ixp4xx-rng.o
obj-$(CONFIG_HW_RANDOM_OMAP) += omap-rng.o
+obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o
Index: powerpc/drivers/char/hw_random/pasemi-rng.c
===================================================================
--- /dev/null
+++ powerpc/drivers/char/hw_random/pasemi-rng.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2006-2007 PA Semi, Inc
+ *
+ * Maintained by: Olof Johansson <olof@lixom.net>
+ *
+ * Driver for the PWRficient onchip rng
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <asm/of_platform.h>
+#include <asm/io.h>
+
+#define SDCRNG_CTL_REG 0x00
+#define SDCRNG_CTL_FVLD_M 0x0000f000
+#define SDCRNG_CTL_FVLD_S 12
+#define SDCRNG_CTL_KSZ 0x00000800
+#define SDCRNG_CTL_RSRC_CRG 0x00000010
+#define SDCRNG_CTL_RSRC_RRG 0x00000000
+#define SDCRNG_CTL_CE 0x00000004
+#define SDCRNG_CTL_RE 0x00000002
+#define SDCRNG_CTL_DR 0x00000001
+#define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
+#define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
+#define SDCRNG_VAL_REG 0x20
+
+#define MODULE_NAME "pasemi_rng"
+
+static int pasemi_rng_data_present(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+
+ return (in_le32(rng_regs + SDCRNG_CTL_REG)
+ & SDCRNG_CTL_FVLD_M) ? 1 : 0;
+}
+
+static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ *data = in_le32(rng_regs + SDCRNG_VAL_REG);
+ return 4;
+}
+
+static int pasemi_rng_init(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ u32 ctl;
+
+ ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
+ out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
+ out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
+
+ return 0;
+}
+
+static void pasemi_rng_cleanup(struct hwrng *rng)
+{
+ void __iomem *rng_regs = (void __iomem *)rng->priv;
+ u32 ctl;
+
+ ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
+ out_le32(rng_regs + SDCRNG_CTL_REG,
+ in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
+}
+
+static struct hwrng pasemi_rng = {
+ .name = MODULE_NAME,
+ .init = pasemi_rng_init,
+ .cleanup = pasemi_rng_cleanup,
+ .data_present = pasemi_rng_data_present,
+ .data_read = pasemi_rng_data_read,
+};
+
+static int __devinit rng_probe(struct of_device *ofdev,
+ const struct of_device_id *match)
+{
+ void __iomem *rng_regs;
+ struct device_node *rng_np = ofdev->node;
+ struct resource res;
+ int err = 0;
+
+ err = of_address_to_resource(rng_np, 0, &res);
+ if (err)
+ return -ENODEV;
+
+ rng_regs = ioremap(res.start, 0x100);
+
+ if (!rng_regs)
+ return -ENOMEM;
+
+ pasemi_rng.priv = (unsigned long)rng_regs;
+
+ printk(KERN_INFO "Registering PA Semi RNG\n");
+
+ err = hwrng_register(&pasemi_rng);
+
+ if (err)
+ iounmap(rng_regs);
+
+ return err;
+}
+
+static int __devexit rng_remove(struct of_device *dev)
+{
+ void __iomem *rng_regs = (void __iomem *)pasemi_rng.priv;
+
+ hwrng_unregister(&pasemi_rng);
+ iounmap(rng_regs);
+
+ return 0;
+}
+
+static struct of_device_id rng_match[] =
+{
+ {
+ .compatible = "1682m-rng",
+ },
+ {},
+};
+
+static struct of_platform_driver rng_driver =
+{
+ .name = "pasemi-rng",
+ .match_table = rng_match,
+ .probe = rng_probe,
+ .remove = rng_remove,
+};
+
+static int __init rng_init(void)
+{
+ return of_register_platform_driver(&rng_driver);
+}
+module_init(rng_init);
+
+static void __exit rng_exit(void)
+{
+ of_unregister_platform_driver(&rng_driver);
+}
+module_exit(rng_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
+MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");
--
Greetings Michael.
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2007-04-27 10:55 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-25 20:45 [PATCH] [2.6.22] pasemi: hardware rng driver Olof Johansson
2007-04-25 23:38 ` Arnd Bergmann
2007-04-26 0:09 ` Olof Johansson
2007-04-26 9:23 ` Michael Buesch
2007-04-26 5:37 ` [PATCH v2] " Olof Johansson
2007-04-26 8:50 ` Arnd Bergmann
2007-04-26 20:59 ` [PATCH v3] " Olof Johansson
2007-04-26 9:22 ` [PATCH] " Michael Buesch
2007-04-26 20:46 ` Olof Johansson
-- strict thread matches above, loose matches on Subject: below --
2007-04-27 10:54 [PATCH 2.6.22] " Michael Buesch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).