blob: 9dd2cadc31bd3cee93f69efd95ecf27f49d7c6c2 [file] [log] [blame]
Alexander Clouter9c3c1332009-02-22 12:03:56 +08001/*
2 * drivers/char/hw_random/timeriomem-rng.c
3 *
4 * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk>
5 *
6 * Derived from drivers/char/hw_random/omap-rng.c
7 * Copyright 2005 (c) MontaVista Software, Inc.
8 * Author: Deepak Saxena <dsaxena@plexity.net>
9 *
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.
13 *
14 * Overview:
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
18 * available.
19 *
20 * TODO: add support for reading sizes other than 32bits and masking
21 */
22
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/platform_device.h>
26#include <linux/hw_random.h>
27#include <linux/io.h>
Alexander Clouter1907da72013-03-31 17:34:50 +010028#include <linux/slab.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080029#include <linux/timeriomem-rng.h>
30#include <linux/jiffies.h>
31#include <linux/sched.h>
32#include <linux/timer.h>
33#include <linux/completion.h>
34
Alexander Clouter1907da72013-03-31 17:34:50 +010035struct timeriomem_rng_private_data {
36 void __iomem *io_base;
37 unsigned int expires;
38 unsigned int period;
39 unsigned int present:1;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080040
Alexander Clouter1907da72013-03-31 17:34:50 +010041 struct timer_list timer;
42 struct completion completion;
43
44 struct hwrng timeriomem_rng_ops;
45};
46
47#define to_rng_priv(rng) \
48 ((struct timeriomem_rng_private_data *)rng->priv)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080049
50/*
51 * have data return 1, however return 0 if we have nothing
52 */
53static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
54{
Alexander Clouter1907da72013-03-31 17:34:50 +010055 struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080056
Alexander Clouter1907da72013-03-31 17:34:50 +010057 if (!wait || priv->present)
58 return priv->present;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080059
Alexander Clouter1907da72013-03-31 17:34:50 +010060 wait_for_completion(&priv->completion);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080061
62 return 1;
63}
64
65static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
66{
Alexander Clouter1907da72013-03-31 17:34:50 +010067 struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080068 unsigned long cur;
69 s32 delay;
70
Alexander Clouter1907da72013-03-31 17:34:50 +010071 *data = readl(priv->io_base);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080072
Alexander Clouter1907da72013-03-31 17:34:50 +010073 cur = jiffies;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080074
Alexander Clouter1907da72013-03-31 17:34:50 +010075 delay = cur - priv->expires;
76 delay = priv->period - (delay % priv->period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080077
Alexander Clouter1907da72013-03-31 17:34:50 +010078 priv->expires = cur + delay;
79 priv->present = 0;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080080
Alexander Clouter1907da72013-03-31 17:34:50 +010081 INIT_COMPLETION(priv->completion);
82 mod_timer(&priv->timer, priv->expires);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080083
84 return 4;
85}
86
Alexander Clouter1907da72013-03-31 17:34:50 +010087static void timeriomem_rng_trigger(unsigned long data)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080088{
Alexander Clouter1907da72013-03-31 17:34:50 +010089 struct timeriomem_rng_private_data *priv
90 = (struct timeriomem_rng_private_data *)data;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080091
Alexander Clouter1907da72013-03-31 17:34:50 +010092 priv->present = 1;
93 complete(&priv->completion);
94}
Alexander Clouter9c3c1332009-02-22 12:03:56 +080095
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -080096static int timeriomem_rng_probe(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080097{
Alexander Clouter1907da72013-03-31 17:34:50 +010098 struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
99 struct timeriomem_rng_private_data *priv;
Alexander Clouter08ced852009-06-03 19:28:03 +1000100 struct resource *res;
Alexander Clouter1907da72013-03-31 17:34:50 +0100101 int err = 0;
102 int period;
103
104 if (!pdata) {
105 dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
106 return -EINVAL;
107 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800108
Alexander Clouter33413232009-03-27 12:59:54 +0800109 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexander Clouter33413232009-03-27 12:59:54 +0800110 if (!res)
Alexander Clouter1907da72013-03-31 17:34:50 +0100111 return -ENXIO;
Alexander Clouter33413232009-03-27 12:59:54 +0800112
Alexander Clouter1907da72013-03-31 17:34:50 +0100113 if (res->start % 4 != 0 || resource_size(res) != 4) {
114 dev_err(&pdev->dev,
115 "address must be four bytes wide and aligned\n");
116 return -EINVAL;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800117 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800118
Alexander Clouter1907da72013-03-31 17:34:50 +0100119 /* Allocate memory for the device structure (and zero it) */
120 priv = kzalloc(sizeof(struct timeriomem_rng_private_data), GFP_KERNEL);
121 if (!priv) {
122 dev_err(&pdev->dev, "failed to allocate device structure.\n");
123 return -ENOMEM;
124 }
125
126 platform_set_drvdata(pdev, priv);
127
128 period = pdata->period;
129
130 priv->period = usecs_to_jiffies(period);
131 if (priv->period < 1) {
132 dev_err(&pdev->dev, "period is less than one jiffy\n");
133 err = -EINVAL;
134 goto out_free;
135 }
136
137 priv->expires = jiffies;
138 priv->present = 1;
139
140 init_completion(&priv->completion);
141 complete(&priv->completion);
142
143 setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv);
144
145 priv->timeriomem_rng_ops.name = dev_name(&pdev->dev);
146 priv->timeriomem_rng_ops.data_present = timeriomem_rng_data_present;
147 priv->timeriomem_rng_ops.data_read = timeriomem_rng_data_read;
148 priv->timeriomem_rng_ops.priv = (unsigned long)priv;
149
150 if (!request_mem_region(res->start, resource_size(res),
151 dev_name(&pdev->dev))) {
152 dev_err(&pdev->dev, "request_mem_region failed\n");
153 err = -EBUSY;
154 goto out_timer;
155 }
156
157 priv->io_base = ioremap(res->start, resource_size(res));
158 if (priv->io_base == NULL) {
159 dev_err(&pdev->dev, "ioremap failed\n");
160 err = -EIO;
161 goto out_release_io;
162 }
163
164 err = hwrng_register(&priv->timeriomem_rng_ops);
165 if (err) {
166 dev_err(&pdev->dev, "problem registering\n");
167 goto out;
168 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800169
170 dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
Alexander Clouter1907da72013-03-31 17:34:50 +0100171 priv->io_base, period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800172
173 return 0;
Alexander Clouter33413232009-03-27 12:59:54 +0800174
Alexander Clouter1907da72013-03-31 17:34:50 +0100175out:
176 iounmap(priv->io_base);
177out_release_io:
178 release_mem_region(res->start, resource_size(res));
179out_timer:
180 del_timer_sync(&priv->timer);
181out_free:
182 platform_set_drvdata(pdev, NULL);
183 kfree(priv);
184 return err;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800185}
186
Bill Pemberton39af33f2012-11-19 13:26:26 -0500187static int timeriomem_rng_remove(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800188{
Alexander Clouter1907da72013-03-31 17:34:50 +0100189 struct timeriomem_rng_private_data *priv = platform_get_drvdata(pdev);
190 struct resource *res;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800191
Alexander Clouter1907da72013-03-31 17:34:50 +0100192 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193
194 hwrng_unregister(&priv->timeriomem_rng_ops);
195
196 del_timer_sync(&priv->timer);
197 iounmap(priv->io_base);
198 release_mem_region(res->start, resource_size(res));
199 platform_set_drvdata(pdev, NULL);
200 kfree(priv);
Alexander Clouter33413232009-03-27 12:59:54 +0800201
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800202 return 0;
203}
204
205static struct platform_driver timeriomem_rng_driver = {
206 .driver = {
207 .name = "timeriomem_rng",
208 .owner = THIS_MODULE,
209 },
210 .probe = timeriomem_rng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800211 .remove = timeriomem_rng_remove,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800212};
213
Axel Linb21cb322011-11-26 21:11:06 +0800214module_platform_driver(timeriomem_rng_driver);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800215
216MODULE_LICENSE("GPL");
217MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
218MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");