This driver adds support for the True Random Number Generator in
the Picochip PC3X3 and later devices.
Cc: Matt Mackall <redacted>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Jamie Iles <redacted>
---
ARCH_PICOXCELL machine support patches have been posted the ARM mailing
lists for review (with another revision to follow soon).
drivers/char/hw_random/Kconfig | 12 ++
drivers/char/hw_random/Makefile | 1 +
drivers/char/hw_random/picoxcell-rng.c | 175 ++++++++++++++++++++++++++++++++
3 files changed, 188 insertions(+), 0 deletions(-)
create mode 100644 drivers/char/hw_random/picoxcell-rng.c
From: Matt Mackall <hidden> Date: 2011-01-14 17:49:21
On Fri, 2011-01-14 at 16:10 +0000, Jamie Iles wrote:
quoted hunk
This driver adds support for the True Random Number Generator in
the Picochip PC3X3 and later devices.
Cc: Matt Mackall <redacted>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Jamie Iles <redacted>
---
ARCH_PICOXCELL machine support patches have been posted the ARM mailing
lists for review (with another revision to follow soon).
drivers/char/hw_random/Kconfig | 12 ++
drivers/char/hw_random/Makefile | 1 +
drivers/char/hw_random/picoxcell-rng.c | 175 ++++++++++++++++++++++++++++++++
3 files changed, 188 insertions(+), 0 deletions(-)
create mode 100644 drivers/char/hw_random/picoxcell-rng.c
Some whitespace weirdness here. Recommend never using tabs except at the
beginning of a line.
+static void __iomem *rng_base;
+static struct clk *rng_clk;
+
+/*
+ * Get some random data from the random number generator. The hw_random core
+ * layer provides us with locking. We can't rely on data being word aligned
+ * though so we'll need to do a memcpy.
+ */
+static int picoxcell_trng_read(struct hwrng *rng, void *buf, size_t max,
+ bool wait)
+{
+ u32 __iomem *csr = rng_base + CSR_REG_OFFSET;
+ int data_avail = !(__raw_readl(csr) & CSR_OUT_EMPTY_MASK);
+ u32 data;
+
+ if (!data_avail && !wait)
+ return 0;
+
+ /* Wait for some data to become available. */
+ while (!data_avail) {
+ data_avail = !(__raw_readl(csr) & CSR_OUT_EMPTY_MASK);
+ cpu_relax();
+ }
This could be simplified a bit:
- deduplicate avail check
- only relax when data's not available
- drop some one use vars
while (!__raw_read(rng_base + ...) {
if (!wait)
return;
cpu_relax();
}
Some whitespace weirdness here. Recommend never using tabs except at the
beginning of a line.
I'll fix that up, not sure what happened there.
quoted
+static void __iomem *rng_base;
+static struct clk *rng_clk;
+
+/*
+ * Get some random data from the random number generator. The hw_random core
+ * layer provides us with locking. We can't rely on data being word aligned
+ * though so we'll need to do a memcpy.
+ */
+static int picoxcell_trng_read(struct hwrng *rng, void *buf, size_t max,
+ bool wait)
+{
+ u32 __iomem *csr = rng_base + CSR_REG_OFFSET;
+ int data_avail = !(__raw_readl(csr) & CSR_OUT_EMPTY_MASK);
+ u32 data;
+
+ if (!data_avail && !wait)
+ return 0;
+
+ /* Wait for some data to become available. */
+ while (!data_avail) {
+ data_avail = !(__raw_readl(csr) & CSR_OUT_EMPTY_MASK);
+ cpu_relax();
+ }
This could be simplified a bit:
- deduplicate avail check
- only relax when data's not available
- drop some one use vars
while (!__raw_read(rng_base + ...) {
if (!wait)
return;
cpu_relax();
}
The buffer passed in is guaranteed aligned:
static u8 rng_buffer[SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES]
__cacheline_aligned;
...
return rng->read(rng, (void *)buffer, size, wait);
Ok, that makes sense. I must have confused myself with the __user
buffer which doesn't have any alignment guarantees.
Thanks again for the review, I'll get a respin out early next week.
Jamie
This driver adds support for the True Random Number Generator in
the Picochip PC3X3 and later devices.
v2: fix indentation and cleanup read function to remove single use
variables and to take advantage of the rng buffer being aligned.
Cc: Matt Mackall <redacted>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Jamie Iles <redacted>
---
drivers/char/hw_random/Kconfig | 12 +++
drivers/char/hw_random/Makefile | 1 +
drivers/char/hw_random/picoxcell-rng.c | 169 ++++++++++++++++++++++++++++++++
3 files changed, 182 insertions(+), 0 deletions(-)
create mode 100644 drivers/char/hw_random/picoxcell-rng.c
From: Matt Mackall <hidden> Date: 2011-01-14 18:48:20
On Fri, 2011-01-14 at 18:44 +0000, Jamie Iles wrote:
This driver adds support for the True Random Number Generator in
the Picochip PC3X3 and later devices.
v2: fix indentation and cleanup read function to remove single use
variables and to take advantage of the rng buffer being aligned.
Cc: Matt Mackall <redacted>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Acked-by: Matt Mackall <redacted>
--
Mathematics is the supreme nostalgia of our time.
From: Alexander Clouter <alex@digriz.org.uk> Date: 2011-01-14 21:10:09
In gmane.linux.ports.arm.kernel Jamie Iles [off-list ref] wrote:
This driver adds support for the True Random Number Generator in
the Picochip PC3X3 and later devices.
[snipped]
+/*
+ * Take the random number generator out of reset and make sure the interrupts
+ * are masked. We shouldn't need to get large amounts of random bytes so just
+ * poll the status register. The hardware generates 32 bits every 320ns so we
+ * shouldn't have to wait long enough to warrant waiting for an IRQ.
+ */
timeriomem-rng? Example of usage in
arch/arm/mach-orion5x/ts78xx-setup.c.
Cheers
--
Alexander Clouter
.sigmonster says: The more I know men the more I like my horse.
On Fri, Jan 14, 2011 at 08:45:19PM +0000, Alexander Clouter wrote:
In gmane.linux.ports.arm.kernel Jamie Iles [off-list ref] wrote:
quoted
This driver adds support for the True Random Number Generator in
the Picochip PC3X3 and later devices.
[snipped]
+/*
+ * Take the random number generator out of reset and make sure the interrupts
+ * are masked. We shouldn't need to get large amounts of random bytes so just
+ * poll the status register. The hardware generates 32 bits every 320ns so we
+ * shouldn't have to wait long enough to warrant waiting for an IRQ.
+ */
timeriomem-rng? Example of usage in
arch/arm/mach-orion5x/ts78xx-setup.c.
No, that's not quite suitable for our hardware. Although the hardware
generates the data every 320ns, it also performs self test of the data
in hardware to make sure it satisfies certain random qualities. If the
data doesn't pass the tests then it's rejected and not placed into the
FIFO so we'd have the possibility of an underflow with the
timeriomem-rng driver.
Jamie
On Sat, Jan 15, 2011 at 11:44:16AM +1100, Herbert Xu wrote:
On Fri, Jan 14, 2011 at 06:44:21PM +0000, Jamie Iles wrote:
quoted
+ /* Wait for some data to become available. */
+ while (__raw_readl(csr) & CSR_OUT_EMPTY_MASK) {
+ if (!wait)
+ return 0;
+ cpu_relax();
+ }
This has the potential to loop indefinitely. Please add a time-out
to prevent that (see existing RNG drivers for example).
Ok, that's a fair point! Here's an updated version with a timeout.
I've also added in support for the fault detection which I previously
overlooked.
Jamie
8<--------