blob: f615684028af710fc6a6db109c2f88282b4d6cf6 [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 /*
Rick Altherr7acd4de2017-04-05 16:20:58 -070056 * There may not have been enough time for new data to be generated
57 * since the last request. If the caller doesn't want to wait, let them
58 * bail out. Otherwise, wait for the completion. If the new data has
59 * already been generated, the completion should already be available.
60 */
61 if (!wait && !priv->present)
62 return 0;
63
64 wait_for_completion(&priv->completion);
65
Rick Altherrca3bff72017-04-05 16:21:00 -070066 do {
67 /*
68 * After the first read, all additional reads will need to wait
69 * for the RNG to generate new data. Since the period can have
70 * a wide range of values (1us to 1s have been observed), allow
71 * for 1% tolerance in the sleep time rather than a fixed value.
72 */
73 if (retval > 0)
74 usleep_range(period_us,
75 period_us + min(1, period_us / 100));
76
77 *(u32 *)data = readl(priv->io_base);
78 retval += sizeof(u32);
79 data += sizeof(u32);
80 max -= sizeof(u32);
81 } while (wait && max > sizeof(u32));
Rick Altherr7acd4de2017-04-05 16:20:58 -070082
83 /*
84 * Block any new callers until the RNG has had time to generate new
85 * data.
86 */
Alexander Clouter1907da72013-03-31 17:34:50 +010087 priv->present = 0;
Wolfram Sang16735d02013-11-14 14:32:02 -080088 reinit_completion(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -070089 hrtimer_forward_now(&priv->timer, priv->period);
90 hrtimer_restart(&priv->timer);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080091
Rick Altherrca3bff72017-04-05 16:21:00 -070092 return retval;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080093}
94
Rick Altherrca3bff72017-04-05 16:21:00 -070095static enum hrtimer_restart timeriomem_rng_trigger(struct hrtimer *timer)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080096{
Rick Altherr5ab693e2017-04-05 16:20:59 -070097 struct timeriomem_rng_private *priv
Rick Altherrca3bff72017-04-05 16:21:00 -070098 = container_of(timer, struct timeriomem_rng_private, timer);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080099
Alexander Clouter1907da72013-03-31 17:34:50 +0100100 priv->present = 1;
101 complete(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -0700102
103 return HRTIMER_NORESTART;
Alexander Clouter1907da72013-03-31 17:34:50 +0100104}
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800105
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800106static int timeriomem_rng_probe(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800107{
Alexander Clouter1907da72013-03-31 17:34:50 +0100108 struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
Rick Altherr5ab693e2017-04-05 16:20:59 -0700109 struct timeriomem_rng_private *priv;
Alexander Clouter08ced852009-06-03 19:28:03 +1000110 struct resource *res;
Alexander Clouter1907da72013-03-31 17:34:50 +0100111 int err = 0;
112 int period;
113
Alexander Clouterb149a302013-03-31 17:34:51 +0100114 if (!pdev->dev.of_node && !pdata) {
Alexander Clouter1907da72013-03-31 17:34:50 +0100115 dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
116 return -EINVAL;
117 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800118
Alexander Clouter33413232009-03-27 12:59:54 +0800119 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexander Clouter33413232009-03-27 12:59:54 +0800120 if (!res)
Alexander Clouter1907da72013-03-31 17:34:50 +0100121 return -ENXIO;
Alexander Clouter33413232009-03-27 12:59:54 +0800122
Alexander Clouter1907da72013-03-31 17:34:50 +0100123 if (res->start % 4 != 0 || resource_size(res) != 4) {
124 dev_err(&pdev->dev,
125 "address must be four bytes wide and aligned\n");
126 return -EINVAL;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800127 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800128
Alexander Clouter1907da72013-03-31 17:34:50 +0100129 /* Allocate memory for the device structure (and zero it) */
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900130 priv = devm_kzalloc(&pdev->dev,
Rick Altherr5ab693e2017-04-05 16:20:59 -0700131 sizeof(struct timeriomem_rng_private), GFP_KERNEL);
Jingoo Han7bad94a2014-04-29 17:16:42 +0900132 if (!priv)
Alexander Clouter1907da72013-03-31 17:34:50 +0100133 return -ENOMEM;
Alexander Clouter1907da72013-03-31 17:34:50 +0100134
135 platform_set_drvdata(pdev, priv);
136
Alexander Clouterb149a302013-03-31 17:34:51 +0100137 if (pdev->dev.of_node) {
138 int i;
139
140 if (!of_property_read_u32(pdev->dev.of_node,
141 "period", &i))
142 period = i;
143 else {
144 dev_err(&pdev->dev, "missing period\n");
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900145 return -EINVAL;
Alexander Clouterb149a302013-03-31 17:34:51 +0100146 }
Rick Altherr284e7632017-05-22 14:12:24 -0700147
148 if (!of_property_read_u32(pdev->dev.of_node,
149 "quality", &i))
150 priv->rng_ops.quality = i;
151 else
152 priv->rng_ops.quality = 0;
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900153 } else {
Alexander Clouterb149a302013-03-31 17:34:51 +0100154 period = pdata->period;
Rick Altherr284e7632017-05-22 14:12:24 -0700155 priv->rng_ops.quality = pdata->quality;
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900156 }
Alexander Clouter1907da72013-03-31 17:34:50 +0100157
Rick Altherrca3bff72017-04-05 16:21:00 -0700158 priv->period = ns_to_ktime(period * NSEC_PER_USEC);
Alexander Clouter1907da72013-03-31 17:34:50 +0100159 init_completion(&priv->completion);
Rick Altherrca3bff72017-04-05 16:21:00 -0700160 hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
161 priv->timer.function = timeriomem_rng_trigger;
Alexander Clouter1907da72013-03-31 17:34:50 +0100162
Rick Altherr5ab693e2017-04-05 16:20:59 -0700163 priv->rng_ops.name = dev_name(&pdev->dev);
164 priv->rng_ops.read = timeriomem_rng_read;
Alexander Clouter1907da72013-03-31 17:34:50 +0100165
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900166 priv->io_base = devm_ioremap_resource(&pdev->dev, res);
167 if (IS_ERR(priv->io_base)) {
Rick Altherrca3bff72017-04-05 16:21:00 -0700168 return PTR_ERR(priv->io_base);
Alexander Clouter1907da72013-03-31 17:34:50 +0100169 }
170
Rick Altherrca3bff72017-04-05 16:21:00 -0700171 /* Assume random data is already available. */
172 priv->present = 1;
173 complete(&priv->completion);
174
Rick Altherr5ab693e2017-04-05 16:20:59 -0700175 err = hwrng_register(&priv->rng_ops);
Alexander Clouter1907da72013-03-31 17:34:50 +0100176 if (err) {
177 dev_err(&pdev->dev, "problem registering\n");
Rick Altherrca3bff72017-04-05 16:21:00 -0700178 return err;
Alexander Clouter1907da72013-03-31 17:34:50 +0100179 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800180
181 dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
Alexander Clouter1907da72013-03-31 17:34:50 +0100182 priv->io_base, period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800183
184 return 0;
185}
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{
Rick Altherr5ab693e2017-04-05 16:20:59 -0700189 struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
Alexander Clouter1907da72013-03-31 17:34:50 +0100190
Rick Altherr5ab693e2017-04-05 16:20:59 -0700191 hwrng_unregister(&priv->rng_ops);
Rick Altherrca3bff72017-04-05 16:21:00 -0700192 hrtimer_cancel(&priv->timer);
Alexander Clouter33413232009-03-27 12:59:54 +0800193
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800194 return 0;
195}
196
Alexander Clouterb149a302013-03-31 17:34:51 +0100197static const struct of_device_id timeriomem_rng_match[] = {
198 { .compatible = "timeriomem_rng" },
199 {},
200};
201MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
202
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800203static struct platform_driver timeriomem_rng_driver = {
204 .driver = {
205 .name = "timeriomem_rng",
Alexander Clouterb149a302013-03-31 17:34:51 +0100206 .of_match_table = timeriomem_rng_match,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800207 },
208 .probe = timeriomem_rng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800209 .remove = timeriomem_rng_remove,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800210};
211
Axel Linb21cb322011-11-26 21:11:06 +0800212module_platform_driver(timeriomem_rng_driver);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800213
214MODULE_LICENSE("GPL");
215MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
216MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");