2 * drivers/char/hw_random/timeriomem-rng.c
4 * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk>
6 * Derived from drivers/char/hw_random/omap-rng.c
7 * Copyright 2005 (c) MontaVista Software, Inc.
8 * Author: Deepak Saxena <dsaxena@plexity.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 * This driver is useful for platforms that have an IO range that provides
16 * periodic random data from a single IO memory address. All the platform
17 * has to do is provide the address and 'wait time' that new data becomes
20 * TODO: add support for reading sizes other than 32bits and masking
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/platform_device.h>
26 #include <linux/hw_random.h>
28 #include <linux/timeriomem-rng.h>
29 #include <linux/jiffies.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/completion.h>
34 static struct timeriomem_rng_data *timeriomem_rng_data;
36 static void timeriomem_rng_trigger(unsigned long);
37 static DEFINE_TIMER(timeriomem_rng_timer, timeriomem_rng_trigger, 0, 0);
40 * have data return 1, however return 0 if we have nothing
42 static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
47 if (!wait || timeriomem_rng_data->present)
48 return timeriomem_rng_data->present;
50 wait_for_completion(&timeriomem_rng_data->completion);
55 static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
60 *data = readl(timeriomem_rng_data->address);
65 delay = cur - timeriomem_rng_timer.expires;
66 delay = rng->priv - (delay % rng->priv);
68 timeriomem_rng_timer.expires = cur + delay;
69 timeriomem_rng_data->present = 0;
71 init_completion(&timeriomem_rng_data->completion);
72 add_timer(&timeriomem_rng_timer);
78 static void timeriomem_rng_trigger(unsigned long dummy)
80 timeriomem_rng_data->present = 1;
81 complete(&timeriomem_rng_data->completion);
84 static struct hwrng timeriomem_rng_ops = {
86 .data_present = timeriomem_rng_data_present,
87 .data_read = timeriomem_rng_data_read,
91 static int __init timeriomem_rng_probe(struct platform_device *pdev)
93 struct resource *res, *mem;
96 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
101 mem = request_mem_region(res->start, res->end - res->start + 1,
106 dev_set_drvdata(&pdev->dev, mem);
108 timeriomem_rng_data = pdev->dev.platform_data;
110 timeriomem_rng_data->address = ioremap(res->start,
111 res->end - res->start + 1);
112 if (!timeriomem_rng_data->address) {
117 if (timeriomem_rng_data->period != 0
118 && usecs_to_jiffies(timeriomem_rng_data->period) > 0) {
119 timeriomem_rng_timer.expires = jiffies;
121 timeriomem_rng_ops.priv = usecs_to_jiffies(
122 timeriomem_rng_data->period);
124 timeriomem_rng_data->present = 1;
126 ret = hwrng_register(&timeriomem_rng_ops);
130 dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
131 timeriomem_rng_data->address,
132 timeriomem_rng_data->period);
137 dev_err(&pdev->dev, "problem registering\n");
138 iounmap(timeriomem_rng_data->address);
140 release_resource(mem);
145 static int __devexit timeriomem_rng_remove(struct platform_device *pdev)
147 struct resource *mem = dev_get_drvdata(&pdev->dev);
149 del_timer_sync(&timeriomem_rng_timer);
150 hwrng_unregister(&timeriomem_rng_ops);
152 iounmap(timeriomem_rng_data->address);
153 release_resource(mem);
158 static struct platform_driver timeriomem_rng_driver = {
160 .name = "timeriomem_rng",
161 .owner = THIS_MODULE,
163 .probe = timeriomem_rng_probe,
164 .remove = __devexit_p(timeriomem_rng_remove),
167 static int __init timeriomem_rng_init(void)
169 return platform_driver_register(&timeriomem_rng_driver);
172 static void __exit timeriomem_rng_exit(void)
174 platform_driver_unregister(&timeriomem_rng_driver);
177 module_init(timeriomem_rng_init);
178 module_exit(timeriomem_rng_exit);
180 MODULE_LICENSE("GPL");
181 MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
182 MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");