* [PATCH 1/2] arm nomadik: add hardware RNG driver
@ 2009-12-03 12:04 Alessandro Rubini
2009-12-03 19:50 ` Matt Mackall
0 siblings, 1 reply; 2+ messages in thread
From: Alessandro Rubini @ 2009-12-03 12:04 UTC (permalink / raw)
To: linux-arm-kernel
From: Alessandro Rubini <rubini@unipv.it>
The hardware random number generator by ST is used in both the Nomadik
8815 SoC and the U8500. It returns 16 bits every 400ns with automatic
delay if a read is issued too early. It depends on PLAT_NOMADIK.
Signed-off-by: Alessandro Rubini <rubini@unipv.it>
Acked-by: Andrea Gallo <andrea.gallo@stericsson.com>
Cc: Matt Mackall <mpm@selenic.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
---
Please note that PLAT_NOMADIK is not yet merged (current upstream has
ARCH_NOMADIK as the U8500 is not there, yet). However, it's in
Russell's queue, and I think it's better to avoid using MACH_NOMADIK
for a while and then patch this again. I've obviously tested it in my
tree which includes Srinidhi's PLAT_NOMADIK patch.
drivers/char/hw_random/Kconfig | 12 ++++
drivers/char/hw_random/Makefile | 1 +
drivers/char/hw_random/nomadik-rng.c | 103 ++++++++++++++++++++++++++++++++++
3 files changed, 116 insertions(+), 0 deletions(-)
create mode 100644 drivers/char/hw_random/nomadik-rng.c
diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 8706026..6ea1014 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -186,3 +186,15 @@ config HW_RANDOM_MXC_RNGA
module will be called mxc-rnga.
If unsure, say Y.
+
+config HW_RANDOM_NOMADIK
+ tristate "ST-Ericsson Nomadik Random Number Generator support"
+ depends on HW_RANDOM && PLAT_NOMADIK
+ ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on ST-Ericsson SoCs (8815 and 8500).
+
+ To compile this driver as a module, choose M here: the
+ module will be called nomadik-rng.
+
+ If unsure, say Y.
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 5eeb130..4273308 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_HW_RANDOM_VIRTIO) += virtio-rng.o
obj-$(CONFIG_HW_RANDOM_TX4939) += tx4939-rng.o
obj-$(CONFIG_HW_RANDOM_MXC_RNGA) += mxc-rnga.o
obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o
+obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
diff --git a/drivers/char/hw_random/nomadik-rng.c b/drivers/char/hw_random/nomadik-rng.c
new file mode 100644
index 0000000..0110543
--- /dev/null
+++ b/drivers/char/hw_random/nomadik-rng.c
@@ -0,0 +1,103 @@
+/*
+ * Nomadik RNG support
+ * Copyright 2009 Alessandro Rubini
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/device.h>
+#include <linux/amba/bus.h>
+#include <linux/hw_random.h>
+#include <linux/io.h>
+
+static int nmk_rng_read(struct hwrng *rng, u32 *buffer)
+{
+ void __iomem *base = (void __iomem *)rng->priv;
+
+ /*
+ * We have the low 16 bits as random data. It takes 0.4us to read them
+ * so return only one 32-bit sample (minimum allowed).
+ * High bits are guaranteed to be 0.
+ */
+ *buffer = __raw_readl(base + 8) << 16 | __raw_readl(base + 8);
+ return 4;
+}
+
+/* we have@most one RNG per machine, granted */
+static struct hwrng nmk_rng = {
+ .name = "nomadik",
+ .data_read = nmk_rng_read,
+};
+
+static int nmk_rng_probe(struct amba_device *dev, struct amba_id *id)
+{
+ void __iomem *base;
+ int ret;
+
+ ret = amba_request_regions(dev, dev->dev.init_name);
+ if (ret)
+ return ret;
+ ret = -ENOMEM;
+ base = ioremap(dev->res.start, resource_size(&dev->res));
+ if (!base)
+ goto out_release;
+ nmk_rng.priv = (unsigned long)base;
+ ret = hwrng_register(&nmk_rng);
+ if (ret)
+ goto out_unmap;
+ return 0;
+
+out_unmap:
+ iounmap(base);
+out_release:
+ amba_release_regions(dev);
+ return ret;
+}
+
+static int nmk_rng_remove(struct amba_device *dev)
+{
+ void __iomem *base = (void __iomem *)nmk_rng.priv;
+ hwrng_unregister(&nmk_rng);
+ iounmap(base);
+ amba_release_regions(dev);
+ return 0;
+}
+
+static struct amba_id nmk_rng_ids[] = {
+ {
+ .id = 0x000805e1,
+ .mask = 0x000fffff, /* top bits are rev and cfg: accept all */
+ },
+ {0, 0},
+};
+
+static struct amba_driver nmk_rng_driver = {
+ .drv = {
+ .owner = THIS_MODULE,
+ .name = "rng",
+ },
+ .probe = nmk_rng_probe,
+ .remove = nmk_rng_remove,
+ .id_table = nmk_rng_ids,
+};
+
+static int __init nmk_rng_init(void)
+{
+ return amba_driver_register(&nmk_rng_driver);
+}
+
+static void __devexit nmk_rng_exit(void)
+{
+ amba_driver_unregister(&nmk_rng_driver);
+}
+
+module_init(nmk_rng_init);
+module_exit(nmk_rng_exit);
+
+MODULE_LICENSE("GPL");
--
1.6.0.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 1/2] arm nomadik: add hardware RNG driver
2009-12-03 12:04 [PATCH 1/2] arm nomadik: add hardware RNG driver Alessandro Rubini
@ 2009-12-03 19:50 ` Matt Mackall
0 siblings, 0 replies; 2+ messages in thread
From: Matt Mackall @ 2009-12-03 19:50 UTC (permalink / raw)
To: linux-arm-kernel
On Thu, 2009-12-03 at 13:04 +0100, Alessandro Rubini wrote:
> From: Alessandro Rubini <rubini@unipv.it>
>
> The hardware random number generator by ST is used in both the Nomadik
> 8815 SoC and the U8500. It returns 16 bits every 400ns with automatic
> delay if a read is issued too early. It depends on PLAT_NOMADIK.
> +static int nmk_rng_read(struct hwrng *rng, u32 *buffer)
> +{
> + void __iomem *base = (void __iomem *)rng->priv;
> + /*
> + * We have the low 16 bits as random data. It takes 0.4us to read them
> + * so return only one 32-bit sample (minimum allowed).
> + * High bits are guaranteed to be 0.
> + */
> + *buffer = __raw_readl(base + 8) << 16 | __raw_readl(base + 8);
> + return 4;
Please adjust this to use the new read interface we just introduced:
http://git.kernel.org/?p=linux/kernel/git/herbert/cryptodev-2.6.git;a=blobdiff;f=include/linux/hw_random.h;h=9bede7633f745d7c5bd866d2f46df9a51021f92d;hp=7244456e7e6573466f0534857bef00b8b1dbdad7;hb=9996508b3353063f2d6c48c1a28a84543d72d70b;hpb=2f32bfd834d5d7eb230bcbf39aaacccd2a01d767
(sorry for the git URL awesomeness)
This will actually let us do 8 or 16-bit reads, so we can reduce latency
and even slightly simplify your code.
Otherwise, looks fine.
--
http://selenic.com : development and support for Mercurial and Linux
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-12-03 19:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-03 12:04 [PATCH 1/2] arm nomadik: add hardware RNG driver Alessandro Rubini
2009-12-03 19:50 ` Matt Mackall
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox