blob: ccd1f6e0696bfd777aea5ae87b26e7ddd4a941c5 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Alexander Clouter9c3c1332009-02-22 12:03:56 +08002/*
3 * drivers/char/hw_random/timeriomem-rng.c
4 *
5 * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk>
6 *
7 * Derived from drivers/char/hw_random/omap-rng.c
8 * Copyright 2005 (c) MontaVista Software, Inc.
9 * Author: Deepak Saxena <dsaxena@plexity.net>
10 *
Alexander Clouter9c3c1332009-02-22 12:03:56 +080011 * Overview:
12 * This driver is useful for platforms that have an IO range that provides
13 * periodic random data from a single IO memory address. All the platform
14 * has to do is provide the address and 'wait time' that new data becomes
15 * available.
16 *
17 * TODO: add support for reading sizes other than 32bits and masking
18 */
19
Rick Altherr7acd4de2017-04-05 16:20:58 -070020#include <linux/completion.h>
Rick Altherrca3bff72017-04-05 16:21:00 -070021#include <linux/delay.h>
22#include <linux/hrtimer.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080023#include <linux/hw_random.h>
24#include <linux/io.h>
Rick Altherrca3bff72017-04-05 16:21:00 -070025#include <linux/ktime.h>
Rick Altherr7acd4de2017-04-05 16:20:58 -070026#include <linux/module.h>
27#include <linux/of.h>
28#include <linux/platform_device.h>
Alexander Clouter1907da72013-03-31 17:34:50 +010029#include <linux/slab.h>
Rick Altherrca3bff72017-04-05 16:21:00 -070030#include <linux/time.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080031#include <linux/timeriomem-rng.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080032
Rick Altherr5ab693e2017-04-05 16:20:59 -070033struct timeriomem_rng_private {
Alexander Clouter1907da72013-03-31 17:34:50 +010034 void __iomem *io_base;
Rick Altherrca3bff72017-04-05 16:21:00 -070035 ktime_t period;
Alexander Clouter1907da72013-03-31 17:34:50 +010036 unsigned int present:1;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080037
Rick Altherrca3bff72017-04-05 16:21:00 -070038 struct hrtimer timer;
Alexander Clouter1907da72013-03-31 17:34:50 +010039 struct completion completion;
40
Rick Altherr5ab693e2017-04-05 16:20:59 -070041 struct hwrng rng_ops;
Alexander Clouter1907da72013-03-31 17:34:50 +010042};
43
Rick Altherr7acd4de2017-04-05 16:20:58 -070044static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
45 size_t max, bool wait)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080046{
Rick Altherr5ab693e2017-04-05 16:20:59 -070047 struct timeriomem_rng_private *priv =
48 container_of(hwrng, struct timeriomem_rng_private, rng_ops);
Rick Altherrca3bff72017-04-05 16:21:00 -070049 int retval = 0;
50 int period_us = ktime_to_us(priv->period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080051
Rick Altherrca3bff72017-04-05 16:21:00 -070052 /*
Rick Altherr7acd4de2017-04-05 16:20:58 -070053 * There may not have been enough time for new data to be generated
54 * since the last request. If the caller doesn't want to wait, let them
55 * bail out. Otherwise, wait for the completion. If the new data has
56 * already been generated, the completion should already be available.
57 */
58 if (!wait && !priv->present)
59 return 0;
60
61 wait_for_completion(&priv->completion);
62
Rick Altherrca3bff72017-04-05 16:21:00 -070063 do {
64 /*
65 * After the first read, all additional reads will need to wait
66 * for the RNG to generate new data. Since the period can have
67 * a wide range of values (1us to 1s have been observed), allow
68 * for 1% tolerance in the sleep time rather than a fixed value.
69 */
70 if (retval > 0)
71 usleep_range(period_us,
72 period_us + min(1, period_us / 100));
73
74 *(u32 *)data = readl(priv->io_base);
75 retval += sizeof(u32);
76 data += sizeof(u32);
77 max -= sizeof(u32);
78 } while (wait && max > sizeof(u32));
Rick Altherr7acd4de2017-04-05 16:20:58 -070079
80 /*
81 * Block any new callers until the RNG has had time to generate new
82 * data.
83 */
Alexander Clouter1907da72013-03-31 17:34:50 +010084 priv->present = 0;
Wolfram Sang16735d02013-11-14 14:32:02 -080085 reinit_completion(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -070086 hrtimer_forward_now(&priv->timer, priv->period);
87 hrtimer_restart(&priv->timer);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080088
Rick Altherrca3bff72017-04-05 16:21:00 -070089 return retval;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080090}
91
Rick Altherrca3bff72017-04-05 16:21:00 -070092static enum hrtimer_restart timeriomem_rng_trigger(struct hrtimer *timer)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080093{
Rick Altherr5ab693e2017-04-05 16:20:59 -070094 struct timeriomem_rng_private *priv
Rick Altherrca3bff72017-04-05 16:21:00 -070095 = container_of(timer, struct timeriomem_rng_private, timer);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080096
Alexander Clouter1907da72013-03-31 17:34:50 +010097 priv->present = 1;
98 complete(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -070099
100 return HRTIMER_NORESTART;
Alexander Clouter1907da72013-03-31 17:34:50 +0100101}
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800102
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800103static int timeriomem_rng_probe(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800104{
Alexander Clouter1907da72013-03-31 17:34:50 +0100105 struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
Rick Altherr5ab693e2017-04-05 16:20:59 -0700106 struct timeriomem_rng_private *priv;
Alexander Clouter08ced852009-06-03 19:28:03 +1000107 struct resource *res;
Alexander Clouter1907da72013-03-31 17:34:50 +0100108 int err = 0;
109 int period;
110
Alexander Clouterb149a302013-03-31 17:34:51 +0100111 if (!pdev->dev.of_node && !pdata) {
Alexander Clouter1907da72013-03-31 17:34:50 +0100112 dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
113 return -EINVAL;
114 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800115
Alexander Clouter33413232009-03-27 12:59:54 +0800116 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexander Clouter33413232009-03-27 12:59:54 +0800117 if (!res)
Alexander Clouter1907da72013-03-31 17:34:50 +0100118 return -ENXIO;
Alexander Clouter33413232009-03-27 12:59:54 +0800119
Alexander Clouter1907da72013-03-31 17:34:50 +0100120 if (res->start % 4 != 0 || resource_size(res) != 4) {
121 dev_err(&pdev->dev,
122 "address must be four bytes wide and aligned\n");
123 return -EINVAL;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800124 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800125
Alexander Clouter1907da72013-03-31 17:34:50 +0100126 /* Allocate memory for the device structure (and zero it) */
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900127 priv = devm_kzalloc(&pdev->dev,
Rick Altherr5ab693e2017-04-05 16:20:59 -0700128 sizeof(struct timeriomem_rng_private), GFP_KERNEL);
Jingoo Han7bad94a2014-04-29 17:16:42 +0900129 if (!priv)
Alexander Clouter1907da72013-03-31 17:34:50 +0100130 return -ENOMEM;
Alexander Clouter1907da72013-03-31 17:34:50 +0100131
132 platform_set_drvdata(pdev, priv);
133
Alexander Clouterb149a302013-03-31 17:34:51 +0100134 if (pdev->dev.of_node) {
135 int i;
136
137 if (!of_property_read_u32(pdev->dev.of_node,
138 "period", &i))
139 period = i;
140 else {
141 dev_err(&pdev->dev, "missing period\n");
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900142 return -EINVAL;
Alexander Clouterb149a302013-03-31 17:34:51 +0100143 }
Rick Altherr284e7632017-05-22 14:12:24 -0700144
145 if (!of_property_read_u32(pdev->dev.of_node,
146 "quality", &i))
147 priv->rng_ops.quality = i;
148 else
149 priv->rng_ops.quality = 0;
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900150 } else {
Alexander Clouterb149a302013-03-31 17:34:51 +0100151 period = pdata->period;
Rick Altherr284e7632017-05-22 14:12:24 -0700152 priv->rng_ops.quality = pdata->quality;
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900153 }
Alexander Clouter1907da72013-03-31 17:34:50 +0100154
Rick Altherrca3bff72017-04-05 16:21:00 -0700155 priv->period = ns_to_ktime(period * NSEC_PER_USEC);
Alexander Clouter1907da72013-03-31 17:34:50 +0100156 init_completion(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -0700157 hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
158 priv->timer.function = timeriomem_rng_trigger;
Alexander Clouter1907da72013-03-31 17:34:50 +0100159
Rick Altherr5ab693e2017-04-05 16:20:59 -0700160 priv->rng_ops.name = dev_name(&pdev->dev);
161 priv->rng_ops.read = timeriomem_rng_read;
Alexander Clouter1907da72013-03-31 17:34:50 +0100162
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900163 priv->io_base = devm_ioremap_resource(&pdev->dev, res);
164 if (IS_ERR(priv->io_base)) {
Rick Altherrca3bff72017-04-05 16:21:00 -0700165 return PTR_ERR(priv->io_base);
Alexander Clouter1907da72013-03-31 17:34:50 +0100166 }
167
Rick Altherrca3bff72017-04-05 16:21:00 -0700168 /* Assume random data is already available. */
169 priv->present = 1;
170 complete(&priv->completion);
171
Rick Altherr5ab693e2017-04-05 16:20:59 -0700172 err = hwrng_register(&priv->rng_ops);
Alexander Clouter1907da72013-03-31 17:34:50 +0100173 if (err) {
174 dev_err(&pdev->dev, "problem registering\n");
Rick Altherrca3bff72017-04-05 16:21:00 -0700175 return err;
Alexander Clouter1907da72013-03-31 17:34:50 +0100176 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800177
178 dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
Alexander Clouter1907da72013-03-31 17:34:50 +0100179 priv->io_base, period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800180
181 return 0;
182}
183
Bill Pemberton39af33f2012-11-19 13:26:26 -0500184static int timeriomem_rng_remove(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800185{
Rick Altherr5ab693e2017-04-05 16:20:59 -0700186 struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
Alexander Clouter1907da72013-03-31 17:34:50 +0100187
Rick Altherr5ab693e2017-04-05 16:20:59 -0700188 hwrng_unregister(&priv->rng_ops);
Rick Altherrca3bff72017-04-05 16:21:00 -0700189 hrtimer_cancel(&priv->timer);
Alexander Clouter33413232009-03-27 12:59:54 +0800190
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800191 return 0;
192}
193
Alexander Clouterb149a302013-03-31 17:34:51 +0100194static const struct of_device_id timeriomem_rng_match[] = {
195 { .compatible = "timeriomem_rng" },
196 {},
197};
198MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
199
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800200static struct platform_driver timeriomem_rng_driver = {
201 .driver = {
202 .name = "timeriomem_rng",
Alexander Clouterb149a302013-03-31 17:34:51 +0100203 .of_match_table = timeriomem_rng_match,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800204 },
205 .probe = timeriomem_rng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800206 .remove = timeriomem_rng_remove,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800207};
208
Axel Linb21cb322011-11-26 21:11:06 +0800209module_platform_driver(timeriomem_rng_driver);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800210
211MODULE_LICENSE("GPL");
212MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
213MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");