2009-06-02 21:54:21 +07:00
|
|
|
/*
|
|
|
|
* RNG driver for TX4939 Random Number Generators (RNG)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-01-21 17:08:59 +07:00
|
|
|
#include <linux/err.h>
|
2009-06-02 21:54:21 +07:00
|
|
|
#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>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 15:04:11 +07:00
|
|
|
#include <linux/gfp.h>
|
2009-06-02 21:54:21 +07:00
|
|
|
|
|
|
|
#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;
|
2013-01-21 17:08:59 +07:00
|
|
|
rngdev->base = devm_ioremap_resource(&dev->dev, r);
|
|
|
|
if (IS_ERR(rngdev->base))
|
|
|
|
return PTR_ERR(rngdev->base);
|
2009-06-02 21:54:21 +07:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2013-03-15 00:09:33 +07:00
|
|
|
module_platform_driver_probe(tx4939_rng_driver, tx4939_rng_probe);
|
2009-06-02 21:54:21 +07:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939");
|
|
|
|
MODULE_LICENSE("GPL");
|