blob: 03ff5483d8654b069d287a12139c1bf57cb12982 [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
Rick Altherr7acd4de2017-04-05 16:20:58 -070023#include <linux/completion.h>
Rick Altherrca3bff72017-04-05 16:21:00 -070024#include <linux/delay.h>
25#include <linux/hrtimer.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080026#include <linux/hw_random.h>
27#include <linux/io.h>
Rick Altherrca3bff72017-04-05 16:21:00 -070028#include <linux/ktime.h>
Rick Altherr7acd4de2017-04-05 16:20:58 -070029#include <linux/module.h>
30#include <linux/of.h>
31#include <linux/platform_device.h>
Alexander Clouter1907da72013-03-31 17:34:50 +010032#include <linux/slab.h>
Rick Altherrca3bff72017-04-05 16:21:00 -070033#include <linux/time.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080034#include <linux/timeriomem-rng.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080035
Rick Altherr5ab693e2017-04-05 16:20:59 -070036struct timeriomem_rng_private {
Alexander Clouter1907da72013-03-31 17:34:50 +010037 void __iomem *io_base;
Rick Altherrca3bff72017-04-05 16:21:00 -070038 ktime_t period;
Alexander Clouter1907da72013-03-31 17:34:50 +010039 unsigned int present:1;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080040
Rick Altherrca3bff72017-04-05 16:21:00 -070041 struct hrtimer timer;
Alexander Clouter1907da72013-03-31 17:34:50 +010042 struct completion completion;
43
Rick Altherr5ab693e2017-04-05 16:20:59 -070044 struct hwrng rng_ops;
Alexander Clouter1907da72013-03-31 17:34:50 +010045};
46
Rick Altherr7acd4de2017-04-05 16:20:58 -070047static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
48 size_t max, bool wait)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080049{
Rick Altherr5ab693e2017-04-05 16:20:59 -070050 struct timeriomem_rng_private *priv =
51 container_of(hwrng, struct timeriomem_rng_private, rng_ops);
Rick Altherrca3bff72017-04-05 16:21:00 -070052 int retval = 0;
53 int period_us = ktime_to_us(priv->period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080054
Rick Altherrca3bff72017-04-05 16:21:00 -070055 /*
56 * The RNG provides 32-bits per read. Ensure there is enough space for
57 * at minimum one read.
58 */
Rick Altherr7acd4de2017-04-05 16:20:58 -070059 if (max < sizeof(u32))
60 return 0;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080061
Rick Altherr7acd4de2017-04-05 16:20:58 -070062 /*
63 * There may not have been enough time for new data to be generated
64 * since the last request. If the caller doesn't want to wait, let them
65 * bail out. Otherwise, wait for the completion. If the new data has
66 * already been generated, the completion should already be available.
67 */
68 if (!wait && !priv->present)
69 return 0;
70
71 wait_for_completion(&priv->completion);
72
Rick Altherrca3bff72017-04-05 16:21:00 -070073 do {
74 /*
75 * After the first read, all additional reads will need to wait
76 * for the RNG to generate new data. Since the period can have
77 * a wide range of values (1us to 1s have been observed), allow
78 * for 1% tolerance in the sleep time rather than a fixed value.
79 */
80 if (retval > 0)
81 usleep_range(period_us,
82 period_us + min(1, period_us / 100));
83
84 *(u32 *)data = readl(priv->io_base);
85 retval += sizeof(u32);
86 data += sizeof(u32);
87 max -= sizeof(u32);
88 } while (wait && max > sizeof(u32));
Rick Altherr7acd4de2017-04-05 16:20:58 -070089
90 /*
91 * Block any new callers until the RNG has had time to generate new
92 * data.
93 */
Alexander Clouter1907da72013-03-31 17:34:50 +010094 priv->present = 0;
Wolfram Sang16735d02013-11-14 14:32:02 -080095 reinit_completion(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -070096 hrtimer_forward_now(&priv->timer, priv->period);
97 hrtimer_restart(&priv->timer);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080098
Rick Altherrca3bff72017-04-05 16:21:00 -070099 return retval;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800100}
101
Rick Altherrca3bff72017-04-05 16:21:00 -0700102static enum hrtimer_restart timeriomem_rng_trigger(struct hrtimer *timer)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800103{
Rick Altherr5ab693e2017-04-05 16:20:59 -0700104 struct timeriomem_rng_private *priv
Rick Altherrca3bff72017-04-05 16:21:00 -0700105 = container_of(timer, struct timeriomem_rng_private, timer);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800106
Alexander Clouter1907da72013-03-31 17:34:50 +0100107 priv->present = 1;
108 complete(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -0700109
110 return HRTIMER_NORESTART;
Alexander Clouter1907da72013-03-31 17:34:50 +0100111}
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800112
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800113static int timeriomem_rng_probe(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800114{
Alexander Clouter1907da72013-03-31 17:34:50 +0100115 struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
Rick Altherr5ab693e2017-04-05 16:20:59 -0700116 struct timeriomem_rng_private *priv;
Alexander Clouter08ced852009-06-03 19:28:03 +1000117 struct resource *res;
Alexander Clouter1907da72013-03-31 17:34:50 +0100118 int err = 0;
119 int period;
120
Alexander Clouterb149a302013-03-31 17:34:51 +0100121 if (!pdev->dev.of_node && !pdata) {
Alexander Clouter1907da72013-03-31 17:34:50 +0100122 dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
123 return -EINVAL;
124 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800125
Alexander Clouter33413232009-03-27 12:59:54 +0800126 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexander Clouter33413232009-03-27 12:59:54 +0800127 if (!res)
Alexander Clouter1907da72013-03-31 17:34:50 +0100128 return -ENXIO;
Alexander Clouter33413232009-03-27 12:59:54 +0800129
Alexander Clouter1907da72013-03-31 17:34:50 +0100130 if (res->start % 4 != 0 || resource_size(res) != 4) {
131 dev_err(&pdev->dev,
132 "address must be four bytes wide and aligned\n");
133 return -EINVAL;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800134 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800135
Alexander Clouter1907da72013-03-31 17:34:50 +0100136 /* Allocate memory for the device structure (and zero it) */
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900137 priv = devm_kzalloc(&pdev->dev,
Rick Altherr5ab693e2017-04-05 16:20:59 -0700138 sizeof(struct timeriomem_rng_private), GFP_KERNEL);
Jingoo Han7bad94a2014-04-29 17:16:42 +0900139 if (!priv)
Alexander Clouter1907da72013-03-31 17:34:50 +0100140 return -ENOMEM;
Alexander Clouter1907da72013-03-31 17:34:50 +0100141
142 platform_set_drvdata(pdev, priv);
143
Alexander Clouterb149a302013-03-31 17:34:51 +0100144 if (pdev->dev.of_node) {
145 int i;
146
147 if (!of_property_read_u32(pdev->dev.of_node,
148 "period", &i))
149 period = i;
150 else {
151 dev_err(&pdev->dev, "missing period\n");
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900152 return -EINVAL;
Alexander Clouterb149a302013-03-31 17:34:51 +0100153 }
Rick Altherr284e7632017-05-22 14:12:24 -0700154
155 if (!of_property_read_u32(pdev->dev.of_node,
156 "quality", &i))
157 priv->rng_ops.quality = i;
158 else
159 priv->rng_ops.quality = 0;
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900160 } else {
Alexander Clouterb149a302013-03-31 17:34:51 +0100161 period = pdata->period;
Rick Altherr284e7632017-05-22 14:12:24 -0700162 priv->rng_ops.quality = pdata->quality;
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900163 }
Alexander Clouter1907da72013-03-31 17:34:50 +0100164
Rick Altherrca3bff72017-04-05 16:21:00 -0700165 priv->period = ns_to_ktime(period * NSEC_PER_USEC);
Alexander Clouter1907da72013-03-31 17:34:50 +0100166 init_completion(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -0700167 hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
168 priv->timer.function = timeriomem_rng_trigger;
Alexander Clouter1907da72013-03-31 17:34:50 +0100169
Rick Altherr5ab693e2017-04-05 16:20:59 -0700170 priv->rng_ops.name = dev_name(&pdev->dev);
171 priv->rng_ops.read = timeriomem_rng_read;
Alexander Clouter1907da72013-03-31 17:34:50 +0100172
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900173 priv->io_base = devm_ioremap_resource(&pdev->dev, res);
174 if (IS_ERR(priv->io_base)) {
Rick Altherrca3bff72017-04-05 16:21:00 -0700175 return PTR_ERR(priv->io_base);
Alexander Clouter1907da72013-03-31 17:34:50 +0100176 }
177
Rick Altherrca3bff72017-04-05 16:21:00 -0700178 /* Assume random data is already available. */
179 priv->present = 1;
180 complete(&priv->completion);
181
Rick Altherr5ab693e2017-04-05 16:20:59 -0700182 err = hwrng_register(&priv->rng_ops);
Alexander Clouter1907da72013-03-31 17:34:50 +0100183 if (err) {
184 dev_err(&pdev->dev, "problem registering\n");
Rick Altherrca3bff72017-04-05 16:21:00 -0700185 return err;
Alexander Clouter1907da72013-03-31 17:34:50 +0100186 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800187
188 dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
Alexander Clouter1907da72013-03-31 17:34:50 +0100189 priv->io_base, period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800190
191 return 0;
192}
193
Bill Pemberton39af33f2012-11-19 13:26:26 -0500194static int timeriomem_rng_remove(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800195{
Rick Altherr5ab693e2017-04-05 16:20:59 -0700196 struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
Alexander Clouter1907da72013-03-31 17:34:50 +0100197
Rick Altherr5ab693e2017-04-05 16:20:59 -0700198 hwrng_unregister(&priv->rng_ops);
Rick Altherrca3bff72017-04-05 16:21:00 -0700199 hrtimer_cancel(&priv->timer);
Alexander Clouter33413232009-03-27 12:59:54 +0800200
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800201 return 0;
202}
203
Alexander Clouterb149a302013-03-31 17:34:51 +0100204static const struct of_device_id timeriomem_rng_match[] = {
205 { .compatible = "timeriomem_rng" },
206 {},
207};
208MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
209
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800210static struct platform_driver timeriomem_rng_driver = {
211 .driver = {
212 .name = "timeriomem_rng",
Alexander Clouterb149a302013-03-31 17:34:51 +0100213 .of_match_table = timeriomem_rng_match,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800214 },
215 .probe = timeriomem_rng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800216 .remove = timeriomem_rng_remove,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800217};
218
Axel Linb21cb322011-11-26 21:11:06 +0800219module_platform_driver(timeriomem_rng_driver);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800220
221MODULE_LICENSE("GPL");
222MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
223MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");