blob: d2120ba8f3f962444dee50ba69fbd57ec16a716a [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>
Alexander Clouterb149a302013-03-31 17:34:51 +010026#include <linux/of.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080027#include <linux/hw_random.h>
28#include <linux/io.h>
Alexander Clouter1907da72013-03-31 17:34:50 +010029#include <linux/slab.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080030#include <linux/timeriomem-rng.h>
31#include <linux/jiffies.h>
32#include <linux/sched.h>
33#include <linux/timer.h>
34#include <linux/completion.h>
35
Alexander Clouter1907da72013-03-31 17:34:50 +010036struct timeriomem_rng_private_data {
37 void __iomem *io_base;
38 unsigned int expires;
39 unsigned int period;
40 unsigned int present:1;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080041
Alexander Clouter1907da72013-03-31 17:34:50 +010042 struct timer_list timer;
43 struct completion completion;
44
45 struct hwrng timeriomem_rng_ops;
46};
47
48#define to_rng_priv(rng) \
49 ((struct timeriomem_rng_private_data *)rng->priv)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080050
51/*
52 * have data return 1, however return 0 if we have nothing
53 */
54static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
55{
Alexander Clouter1907da72013-03-31 17:34:50 +010056 struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080057
Alexander Clouter1907da72013-03-31 17:34:50 +010058 if (!wait || priv->present)
59 return priv->present;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080060
Alexander Clouter1907da72013-03-31 17:34:50 +010061 wait_for_completion(&priv->completion);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080062
63 return 1;
64}
65
66static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
67{
Alexander Clouter1907da72013-03-31 17:34:50 +010068 struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080069 unsigned long cur;
70 s32 delay;
71
Alexander Clouter1907da72013-03-31 17:34:50 +010072 *data = readl(priv->io_base);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080073
Alexander Clouter1907da72013-03-31 17:34:50 +010074 cur = jiffies;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080075
Alexander Clouter1907da72013-03-31 17:34:50 +010076 delay = cur - priv->expires;
77 delay = priv->period - (delay % priv->period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080078
Alexander Clouter1907da72013-03-31 17:34:50 +010079 priv->expires = cur + delay;
80 priv->present = 0;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080081
Alexander Clouter1907da72013-03-31 17:34:50 +010082 INIT_COMPLETION(priv->completion);
83 mod_timer(&priv->timer, priv->expires);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080084
85 return 4;
86}
87
Alexander Clouter1907da72013-03-31 17:34:50 +010088static void timeriomem_rng_trigger(unsigned long data)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080089{
Alexander Clouter1907da72013-03-31 17:34:50 +010090 struct timeriomem_rng_private_data *priv
91 = (struct timeriomem_rng_private_data *)data;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080092
Alexander Clouter1907da72013-03-31 17:34:50 +010093 priv->present = 1;
94 complete(&priv->completion);
95}
Alexander Clouter9c3c1332009-02-22 12:03:56 +080096
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -080097static int timeriomem_rng_probe(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080098{
Alexander Clouter1907da72013-03-31 17:34:50 +010099 struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
100 struct timeriomem_rng_private_data *priv;
Alexander Clouter08ced852009-06-03 19:28:03 +1000101 struct resource *res;
Alexander Clouter1907da72013-03-31 17:34:50 +0100102 int err = 0;
103 int period;
104
Alexander Clouterb149a302013-03-31 17:34:51 +0100105 if (!pdev->dev.of_node && !pdata) {
Alexander Clouter1907da72013-03-31 17:34:50 +0100106 dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
107 return -EINVAL;
108 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800109
Alexander Clouter33413232009-03-27 12:59:54 +0800110 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexander Clouter33413232009-03-27 12:59:54 +0800111 if (!res)
Alexander Clouter1907da72013-03-31 17:34:50 +0100112 return -ENXIO;
Alexander Clouter33413232009-03-27 12:59:54 +0800113
Alexander Clouter1907da72013-03-31 17:34:50 +0100114 if (res->start % 4 != 0 || resource_size(res) != 4) {
115 dev_err(&pdev->dev,
116 "address must be four bytes wide and aligned\n");
117 return -EINVAL;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800118 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800119
Alexander Clouter1907da72013-03-31 17:34:50 +0100120 /* Allocate memory for the device structure (and zero it) */
121 priv = kzalloc(sizeof(struct timeriomem_rng_private_data), GFP_KERNEL);
122 if (!priv) {
123 dev_err(&pdev->dev, "failed to allocate device structure.\n");
124 return -ENOMEM;
125 }
126
127 platform_set_drvdata(pdev, priv);
128
Alexander Clouterb149a302013-03-31 17:34:51 +0100129 if (pdev->dev.of_node) {
130 int i;
131
132 if (!of_property_read_u32(pdev->dev.of_node,
133 "period", &i))
134 period = i;
135 else {
136 dev_err(&pdev->dev, "missing period\n");
137 err = -EINVAL;
138 goto out_free;
139 }
140 } else
141 period = pdata->period;
Alexander Clouter1907da72013-03-31 17:34:50 +0100142
143 priv->period = usecs_to_jiffies(period);
144 if (priv->period < 1) {
145 dev_err(&pdev->dev, "period is less than one jiffy\n");
146 err = -EINVAL;
147 goto out_free;
148 }
149
150 priv->expires = jiffies;
151 priv->present = 1;
152
153 init_completion(&priv->completion);
154 complete(&priv->completion);
155
156 setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv);
157
158 priv->timeriomem_rng_ops.name = dev_name(&pdev->dev);
159 priv->timeriomem_rng_ops.data_present = timeriomem_rng_data_present;
160 priv->timeriomem_rng_ops.data_read = timeriomem_rng_data_read;
161 priv->timeriomem_rng_ops.priv = (unsigned long)priv;
162
163 if (!request_mem_region(res->start, resource_size(res),
164 dev_name(&pdev->dev))) {
165 dev_err(&pdev->dev, "request_mem_region failed\n");
166 err = -EBUSY;
167 goto out_timer;
168 }
169
170 priv->io_base = ioremap(res->start, resource_size(res));
171 if (priv->io_base == NULL) {
172 dev_err(&pdev->dev, "ioremap failed\n");
173 err = -EIO;
174 goto out_release_io;
175 }
176
177 err = hwrng_register(&priv->timeriomem_rng_ops);
178 if (err) {
179 dev_err(&pdev->dev, "problem registering\n");
180 goto out;
181 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800182
183 dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
Alexander Clouter1907da72013-03-31 17:34:50 +0100184 priv->io_base, period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800185
186 return 0;
Alexander Clouter33413232009-03-27 12:59:54 +0800187
Alexander Clouter1907da72013-03-31 17:34:50 +0100188out:
189 iounmap(priv->io_base);
190out_release_io:
191 release_mem_region(res->start, resource_size(res));
192out_timer:
193 del_timer_sync(&priv->timer);
194out_free:
Alexander Clouter1907da72013-03-31 17:34:50 +0100195 kfree(priv);
196 return err;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800197}
198
Bill Pemberton39af33f2012-11-19 13:26:26 -0500199static int timeriomem_rng_remove(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800200{
Alexander Clouter1907da72013-03-31 17:34:50 +0100201 struct timeriomem_rng_private_data *priv = platform_get_drvdata(pdev);
202 struct resource *res;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800203
Alexander Clouter1907da72013-03-31 17:34:50 +0100204 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
205
206 hwrng_unregister(&priv->timeriomem_rng_ops);
207
208 del_timer_sync(&priv->timer);
209 iounmap(priv->io_base);
210 release_mem_region(res->start, resource_size(res));
Alexander Clouter1907da72013-03-31 17:34:50 +0100211 kfree(priv);
Alexander Clouter33413232009-03-27 12:59:54 +0800212
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800213 return 0;
214}
215
Alexander Clouterb149a302013-03-31 17:34:51 +0100216static const struct of_device_id timeriomem_rng_match[] = {
217 { .compatible = "timeriomem_rng" },
218 {},
219};
220MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
221
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800222static struct platform_driver timeriomem_rng_driver = {
223 .driver = {
224 .name = "timeriomem_rng",
225 .owner = THIS_MODULE,
Alexander Clouterb149a302013-03-31 17:34:51 +0100226 .of_match_table = timeriomem_rng_match,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800227 },
228 .probe = timeriomem_rng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800229 .remove = timeriomem_rng_remove,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800230};
231
Axel Linb21cb322011-11-26 21:11:06 +0800232module_platform_driver(timeriomem_rng_driver);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800233
234MODULE_LICENSE("GPL");
235MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
236MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");