2009-06-02 14:54:42

by Atsushi Nemoto

[permalink] [raw]
Subject: [PATCH] hwrng: Add TX4939 RNG driver (v2)

This patch adds support for the integrated RNG of the TX4939 SoC.

Signed-off-by: Atsushi Nemoto <[email protected]>
---
Changes since v1:
* Wrap local_irq_enable/local_irq_disable
* Add some comments
* Rename tx4939_rng_init to tx4939rng_init

drivers/char/hw_random/Kconfig | 13 +++
drivers/char/hw_random/Makefile | 1 +
drivers/char/hw_random/tx4939-rng.c | 184 +++++++++++++++++++++++++++++++++++
3 files changed, 198 insertions(+), 0 deletions(-)
create mode 100644 drivers/char/hw_random/tx4939-rng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 5fab647..9d321cc 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -148,3 +148,16 @@ config HW_RANDOM_VIRTIO

To compile this driver as a module, choose M here: the
module will be called virtio-rng. If unsure, say N.
+
+config HW_RANDOM_TX4939
+ tristate "TX4939 Random Number Generator support"
+ depends on HW_RANDOM && SOC_TX4939
+ default HW_RANDOM
+ ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on TX4939 SoC.
+
+ To compile this driver as a module, choose M here: the
+ module will be called tx4939-rng.
+
+ If unsure, say Y.
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index e81d21a..936d388 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -15,3 +15,4 @@ obj-$(CONFIG_HW_RANDOM_IXP4XX) += ixp4xx-rng.o
obj-$(CONFIG_HW_RANDOM_OMAP) += omap-rng.o
obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o
obj-$(CONFIG_HW_RANDOM_VIRTIO) += virtio-rng.o
+obj-$(CONFIG_HW_RANDOM_TX4939) += tx4939-rng.o
diff --git a/drivers/char/hw_random/tx4939-rng.c b/drivers/char/hw_random/tx4939-rng.c
new file mode 100644
index 0000000..544d908
--- /dev/null
+++ b/drivers/char/hw_random/tx4939-rng.c
@@ -0,0 +1,184 @@
+/*
+ * RNG driver for TX4939 Random Number Generators (RNG)
+ *
+ * Copyright (C) 2009 Atsushi Nemoto <[email protected]>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+
+#define TX4939_RNG_RCSR 0x00000000
+#define TX4939_RNG_ROR(n) (0x00000018 + (n) * 8)
+
+#define TX4939_RNG_RCSR_INTE 0x00000008
+#define TX4939_RNG_RCSR_RST 0x00000004
+#define TX4939_RNG_RCSR_FIN 0x00000002
+#define TX4939_RNG_RCSR_ST 0x00000001
+
+struct tx4939_rng {
+ struct hwrng rng;
+ void __iomem *base;
+ u64 databuf[3];
+ unsigned int data_avail;
+};
+
+static void rng_io_start(void)
+{
+#ifndef CONFIG_64BIT
+ /*
+ * readq is reading a 64-bit register using a 64-bit load. On
+ * a 32-bit kernel however interrupts or any other processor
+ * exception would clobber the upper 32-bit of the processor
+ * register so interrupts need to be disabled.
+ */
+ local_irq_disable();
+#endif
+}
+
+static void rng_io_end(void)
+{
+#ifndef CONFIG_64BIT
+ local_irq_enable();
+#endif
+}
+
+static u64 read_rng(void __iomem *base, unsigned int offset)
+{
+ return ____raw_readq(base + offset);
+}
+
+static void write_rng(u64 val, void __iomem *base, unsigned int offset)
+{
+ return ____raw_writeq(val, base + offset);
+}
+
+static int tx4939_rng_data_present(struct hwrng *rng, int wait)
+{
+ struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng);
+ int i;
+
+ if (rngdev->data_avail)
+ return rngdev->data_avail;
+ for (i = 0; i < 20; i++) {
+ rng_io_start();
+ if (!(read_rng(rngdev->base, TX4939_RNG_RCSR)
+ & TX4939_RNG_RCSR_ST)) {
+ rngdev->databuf[0] =
+ read_rng(rngdev->base, TX4939_RNG_ROR(0));
+ rngdev->databuf[1] =
+ read_rng(rngdev->base, TX4939_RNG_ROR(1));
+ rngdev->databuf[2] =
+ read_rng(rngdev->base, TX4939_RNG_ROR(2));
+ rngdev->data_avail =
+ sizeof(rngdev->databuf) / sizeof(u32);
+ /* Start RNG */
+ write_rng(TX4939_RNG_RCSR_ST,
+ rngdev->base, TX4939_RNG_RCSR);
+ wait = 0;
+ }
+ rng_io_end();
+ if (!wait)
+ break;
+ /* 90 bus clock cycles by default for generation */
+ ndelay(90 * 5);
+ }
+ return rngdev->data_avail;
+}
+
+static int tx4939_rng_data_read(struct hwrng *rng, u32 *buffer)
+{
+ struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng);
+
+ rngdev->data_avail--;
+ *buffer = *((u32 *)&rngdev->databuf + rngdev->data_avail);
+ return sizeof(u32);
+}
+
+static int __init tx4939_rng_probe(struct platform_device *dev)
+{
+ struct tx4939_rng *rngdev;
+ struct resource *r;
+ int i;
+
+ r = platform_get_resource(dev, IORESOURCE_MEM, 0);
+ if (!r)
+ return -EBUSY;
+ rngdev = devm_kzalloc(&dev->dev, sizeof(*rngdev), GFP_KERNEL);
+ if (!rngdev)
+ return -ENOMEM;
+ if (!devm_request_mem_region(&dev->dev, r->start, resource_size(r),
+ dev_name(&dev->dev)))
+ return -EBUSY;
+ rngdev->base = devm_ioremap(&dev->dev, r->start, resource_size(r));
+ if (!rngdev->base)
+ return -EBUSY;
+
+ rngdev->rng.name = dev_name(&dev->dev);
+ rngdev->rng.data_present = tx4939_rng_data_present;
+ rngdev->rng.data_read = tx4939_rng_data_read;
+
+ rng_io_start();
+ /* Reset RNG */
+ write_rng(TX4939_RNG_RCSR_RST, rngdev->base, TX4939_RNG_RCSR);
+ write_rng(0, rngdev->base, TX4939_RNG_RCSR);
+ /* Start RNG */
+ write_rng(TX4939_RNG_RCSR_ST, rngdev->base, TX4939_RNG_RCSR);
+ rng_io_end();
+ /*
+ * Drop first two results. From the datasheet:
+ * The quality of the random numbers generated immediately
+ * after reset can be insufficient. Therefore, do not use
+ * random numbers obtained from the first and second
+ * generations; use the ones from the third or subsequent
+ * generation.
+ */
+ for (i = 0; i < 2; i++) {
+ rngdev->data_avail = 0;
+ if (!tx4939_rng_data_present(&rngdev->rng, 1))
+ return -EIO;
+ }
+
+ platform_set_drvdata(dev, rngdev);
+ return hwrng_register(&rngdev->rng);
+}
+
+static int __exit tx4939_rng_remove(struct platform_device *dev)
+{
+ struct tx4939_rng *rngdev = platform_get_drvdata(dev);
+
+ hwrng_unregister(&rngdev->rng);
+ platform_set_drvdata(dev, NULL);
+ return 0;
+}
+
+static struct platform_driver tx4939_rng_driver = {
+ .driver = {
+ .name = "tx4939-rng",
+ .owner = THIS_MODULE,
+ },
+ .remove = tx4939_rng_remove,
+};
+
+static int __init tx4939rng_init(void)
+{
+ return platform_driver_probe(&tx4939_rng_driver, tx4939_rng_probe);
+}
+
+static void __exit tx4939rng_exit(void)
+{
+ platform_driver_unregister(&tx4939_rng_driver);
+}
+
+module_init(tx4939rng_init);
+module_exit(tx4939rng_exit);
+
+MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939");
+MODULE_LICENSE("GPL");
--
1.5.6.5


2009-06-02 20:14:20

by Matt Mackall

[permalink] [raw]
Subject: Re: [PATCH] hwrng: Add TX4939 RNG driver (v2)

On Tue, 2009-06-02 at 23:54 +0900, Atsushi Nemoto wrote:
> This patch adds support for the integrated RNG of the TX4939 SoC.
>
> Signed-off-by: Atsushi Nemoto <[email protected]>

Acked-by: Matt Mackall <[email protected]>

--
http://selenic.com : development and support for Mercurial and Linux

2009-06-03 09:03:49

by Ralf Baechle

[permalink] [raw]
Subject: Re: [PATCH] hwrng: Add TX4939 RNG driver (v2)

On Tue, Jun 02, 2009 at 03:13:04PM -0500, Matt Mackall wrote:

> On Tue, 2009-06-02 at 23:54 +0900, Atsushi Nemoto wrote:
> > This patch adds support for the integrated RNG of the TX4939 SoC.
> >
> > Signed-off-by: Atsushi Nemoto <[email protected]>
>
> Acked-by: Matt Mackall <[email protected]>

Ok, I'll send this to Linus for 2.6.31 then.

Thanks,

Ralf

2009-06-03 09:26:33

by Herbert Xu

[permalink] [raw]
Subject: Re: [PATCH] hwrng: Add TX4939 RNG driver (v2)

On Wed, Jun 03, 2009 at 10:02:38AM +0100, Ralf Baechle wrote:
> On Tue, Jun 02, 2009 at 03:13:04PM -0500, Matt Mackall wrote:
>
> > On Tue, 2009-06-02 at 23:54 +0900, Atsushi Nemoto wrote:
> > > This patch adds support for the integrated RNG of the TX4939 SoC.
> > >
> > > Signed-off-by: Atsushi Nemoto <[email protected]>
> >
> > Acked-by: Matt Mackall <[email protected]>
>
> Ok, I'll send this to Linus for 2.6.31 then.

Actually I've already added it to my tree since I'm looking after
the hwrng drivers.

Thanks,
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

2009-06-03 09:29:43

by Herbert Xu

[permalink] [raw]
Subject: Re: [PATCH] hwrng: Add TX4939 RNG driver (v2)

On Wed, Jun 03, 2009 at 07:26:10PM +1000, Herbert Xu wrote:
> On Wed, Jun 03, 2009 at 10:02:38AM +0100, Ralf Baechle wrote:
>
> > Ok, I'll send this to Linus for 2.6.31 then.
>
> Actually I've already added it to my tree since I'm looking after
> the hwrng drivers.

But if this causes any conflicts for you, then please let me know
and I'll back it out.

Cheers,
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

2009-06-03 10:03:16

by Ralf Baechle

[permalink] [raw]
Subject: Re: [PATCH] hwrng: Add TX4939 RNG driver (v2)

On Wed, Jun 03, 2009 at 07:29:27PM +1000, Herbert Xu wrote:

> On Wed, Jun 03, 2009 at 07:26:10PM +1000, Herbert Xu wrote:
> > On Wed, Jun 03, 2009 at 10:02:38AM +0100, Ralf Baechle wrote:
> >
> > > Ok, I'll send this to Linus for 2.6.31 then.
> >
> > Actually I've already added it to my tree since I'm looking after
> > the hwrng drivers.
>
> But if this causes any conflicts for you, then please let me know
> and I'll back it out.

Ah, I was looking at MAINTAINERS which said Mack who had acked it was
handling it.

There is no conflict on the drivers/char/hw_random/ part, so I'll drop
that one. The platform part in arch/mips/ has to go through the MIPS
tree as it depends on another patch.

Ralf

2009-06-03 10:04:30

by Herbert Xu

[permalink] [raw]
Subject: Re: [PATCH] hwrng: Add TX4939 RNG driver (v2)

On Wed, Jun 03, 2009 at 11:02:15AM +0100, Ralf Baechle wrote:
>
> There is no conflict on the drivers/char/hw_random/ part, so I'll drop
> that one. The platform part in arch/mips/ has to go through the MIPS
> tree as it depends on another patch.

OK we can certainly do that.

Thanks,
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

2009-06-03 18:51:07

by Matt Mackall

[permalink] [raw]
Subject: Re: [PATCH] hwrng: Add TX4939 RNG driver (v2)

On Wed, 2009-06-03 at 11:02 +0100, Ralf Baechle wrote:
> On Wed, Jun 03, 2009 at 07:29:27PM +1000, Herbert Xu wrote:
>
> > On Wed, Jun 03, 2009 at 07:26:10PM +1000, Herbert Xu wrote:
> > > On Wed, Jun 03, 2009 at 10:02:38AM +0100, Ralf Baechle wrote:
> > >
> > > > Ok, I'll send this to Linus for 2.6.31 then.
> > >
> > > Actually I've already added it to my tree since I'm looking after
> > > the hwrng drivers.
> >
> > But if this causes any conflicts for you, then please let me know
> > and I'll back it out.
>
> Ah, I was looking at MAINTAINERS which said Mack who had acked it was
> handling it.

I'm the maintainer of record for random.c, which is different. Not long
ago, I sent in a patch to add myself to the HWRNG piece because people
were regularly sending me things to review and it was marked as orphan.

So, Herbert: we should probably resolve this confusion in MAINTAINERS.
We should add one or both of our names to HWRNG and, if necessary, add a
comment to RANDOM pointing to HWRNG. I have an eventual goal to make
random.c sample the hwrngs without a trip through userspace, so this may
get even more confusing.

--
http://selenic.com : development and support for Mercurial and Linux