blob: 024bdff7999f361852c971e61c3c213e9c55ff43 [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>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080024#include <linux/hw_random.h>
25#include <linux/io.h>
Rick Altherr7acd4de2017-04-05 16:20:58 -070026#include <linux/jiffies.h>
27#include <linux/module.h>
28#include <linux/of.h>
29#include <linux/platform_device.h>
Alexander Clouter1907da72013-03-31 17:34:50 +010030#include <linux/slab.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080031#include <linux/timeriomem-rng.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080032#include <linux/timer.h>
Alexander Clouter9c3c1332009-02-22 12:03:56 +080033
Rick Altherr5ab693e2017-04-05 16:20:59 -070034struct timeriomem_rng_private {
Alexander Clouter1907da72013-03-31 17:34:50 +010035 void __iomem *io_base;
36 unsigned int expires;
37 unsigned int period;
38 unsigned int present:1;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080039
Alexander Clouter1907da72013-03-31 17:34:50 +010040 struct timer_list timer;
41 struct completion completion;
42
Rick Altherr5ab693e2017-04-05 16:20:59 -070043 struct hwrng rng_ops;
Alexander Clouter1907da72013-03-31 17:34:50 +010044};
45
Rick Altherr7acd4de2017-04-05 16:20:58 -070046static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
47 size_t max, bool wait)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080048{
Rick Altherr5ab693e2017-04-05 16:20:59 -070049 struct timeriomem_rng_private *priv =
50 container_of(hwrng, struct timeriomem_rng_private, rng_ops);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080051 unsigned long cur;
52 s32 delay;
53
Rick Altherr7acd4de2017-04-05 16:20:58 -070054 /* The RNG provides 32-bit per read. Ensure there is enough space. */
55 if (max < sizeof(u32))
56 return 0;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080057
Rick Altherr7acd4de2017-04-05 16:20:58 -070058 /*
59 * There may not have been enough time for new data to be generated
60 * since the last request. If the caller doesn't want to wait, let them
61 * bail out. Otherwise, wait for the completion. If the new data has
62 * already been generated, the completion should already be available.
63 */
64 if (!wait && !priv->present)
65 return 0;
66
67 wait_for_completion(&priv->completion);
68
69 *(u32 *)data = readl(priv->io_base);
70
71 /*
72 * Block any new callers until the RNG has had time to generate new
73 * data.
74 */
Alexander Clouter1907da72013-03-31 17:34:50 +010075 cur = jiffies;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080076
Alexander Clouter1907da72013-03-31 17:34:50 +010077 delay = cur - priv->expires;
78 delay = priv->period - (delay % priv->period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080079
Alexander Clouter1907da72013-03-31 17:34:50 +010080 priv->expires = cur + delay;
81 priv->present = 0;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080082
Wolfram Sang16735d02013-11-14 14:32:02 -080083 reinit_completion(&priv->completion);
Alexander Clouter1907da72013-03-31 17:34:50 +010084 mod_timer(&priv->timer, priv->expires);
Alexander Clouter9c3c1332009-02-22 12:03:56 +080085
86 return 4;
87}
88
Alexander Clouter1907da72013-03-31 17:34:50 +010089static void timeriomem_rng_trigger(unsigned long data)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080090{
Rick Altherr5ab693e2017-04-05 16:20:59 -070091 struct timeriomem_rng_private *priv
92 = (struct timeriomem_rng_private *)data;
Alexander Clouter9c3c1332009-02-22 12:03:56 +080093
Alexander Clouter1907da72013-03-31 17:34:50 +010094 priv->present = 1;
95 complete(&priv->completion);
96}
Alexander Clouter9c3c1332009-02-22 12:03:56 +080097
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -080098static int timeriomem_rng_probe(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +080099{
Alexander Clouter1907da72013-03-31 17:34:50 +0100100 struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
Rick Altherr5ab693e2017-04-05 16:20:59 -0700101 struct timeriomem_rng_private *priv;
Alexander Clouter08ced852009-06-03 19:28:03 +1000102 struct resource *res;
Alexander Clouter1907da72013-03-31 17:34:50 +0100103 int err = 0;
104 int period;
105
Alexander Clouterb149a302013-03-31 17:34:51 +0100106 if (!pdev->dev.of_node && !pdata) {
Alexander Clouter1907da72013-03-31 17:34:50 +0100107 dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
108 return -EINVAL;
109 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800110
Alexander Clouter33413232009-03-27 12:59:54 +0800111 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexander Clouter33413232009-03-27 12:59:54 +0800112 if (!res)
Alexander Clouter1907da72013-03-31 17:34:50 +0100113 return -ENXIO;
Alexander Clouter33413232009-03-27 12:59:54 +0800114
Alexander Clouter1907da72013-03-31 17:34:50 +0100115 if (res->start % 4 != 0 || resource_size(res) != 4) {
116 dev_err(&pdev->dev,
117 "address must be four bytes wide and aligned\n");
118 return -EINVAL;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800119 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800120
Alexander Clouter1907da72013-03-31 17:34:50 +0100121 /* Allocate memory for the device structure (and zero it) */
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900122 priv = devm_kzalloc(&pdev->dev,
Rick Altherr5ab693e2017-04-05 16:20:59 -0700123 sizeof(struct timeriomem_rng_private), GFP_KERNEL);
Jingoo Han7bad94a2014-04-29 17:16:42 +0900124 if (!priv)
Alexander Clouter1907da72013-03-31 17:34:50 +0100125 return -ENOMEM;
Alexander Clouter1907da72013-03-31 17:34:50 +0100126
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");
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900137 return -EINVAL;
Alexander Clouterb149a302013-03-31 17:34:51 +0100138 }
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900139 } else {
Alexander Clouterb149a302013-03-31 17:34:51 +0100140 period = pdata->period;
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900141 }
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");
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900146 return -EINVAL;
Alexander Clouter1907da72013-03-31 17:34:50 +0100147 }
148
149 priv->expires = jiffies;
150 priv->present = 1;
151
152 init_completion(&priv->completion);
153 complete(&priv->completion);
154
155 setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv);
156
Rick Altherr5ab693e2017-04-05 16:20:59 -0700157 priv->rng_ops.name = dev_name(&pdev->dev);
158 priv->rng_ops.read = timeriomem_rng_read;
Alexander Clouter1907da72013-03-31 17:34:50 +0100159
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900160 priv->io_base = devm_ioremap_resource(&pdev->dev, res);
161 if (IS_ERR(priv->io_base)) {
162 err = PTR_ERR(priv->io_base);
Alexander Clouter1907da72013-03-31 17:34:50 +0100163 goto out_timer;
164 }
165
Rick Altherr5ab693e2017-04-05 16:20:59 -0700166 err = hwrng_register(&priv->rng_ops);
Alexander Clouter1907da72013-03-31 17:34:50 +0100167 if (err) {
168 dev_err(&pdev->dev, "problem registering\n");
Jingoo Han93b7f9c2014-02-27 14:07:52 +0900169 goto out_timer;
Alexander Clouter1907da72013-03-31 17:34:50 +0100170 }
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800171
172 dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
Alexander Clouter1907da72013-03-31 17:34:50 +0100173 priv->io_base, period);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800174
175 return 0;
Alexander Clouter33413232009-03-27 12:59:54 +0800176
Alexander Clouter1907da72013-03-31 17:34:50 +0100177out_timer:
178 del_timer_sync(&priv->timer);
Alexander Clouter1907da72013-03-31 17:34:50 +0100179 return err;
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800180}
181
Bill Pemberton39af33f2012-11-19 13:26:26 -0500182static int timeriomem_rng_remove(struct platform_device *pdev)
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800183{
Rick Altherr5ab693e2017-04-05 16:20:59 -0700184 struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
Alexander Clouter1907da72013-03-31 17:34:50 +0100185
Rick Altherr5ab693e2017-04-05 16:20:59 -0700186 hwrng_unregister(&priv->rng_ops);
Alexander Clouter1907da72013-03-31 17:34:50 +0100187
188 del_timer_sync(&priv->timer);
Alexander Clouter33413232009-03-27 12:59:54 +0800189
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800190 return 0;
191}
192
Alexander Clouterb149a302013-03-31 17:34:51 +0100193static const struct of_device_id timeriomem_rng_match[] = {
194 { .compatible = "timeriomem_rng" },
195 {},
196};
197MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
198
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800199static struct platform_driver timeriomem_rng_driver = {
200 .driver = {
201 .name = "timeriomem_rng",
Alexander Clouterb149a302013-03-31 17:34:51 +0100202 .of_match_table = timeriomem_rng_match,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800203 },
204 .probe = timeriomem_rng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800205 .remove = timeriomem_rng_remove,
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800206};
207
Axel Linb21cb322011-11-26 21:11:06 +0800208module_platform_driver(timeriomem_rng_driver);
Alexander Clouter9c3c1332009-02-22 12:03:56 +0800209
210MODULE_LICENSE("GPL");
211MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
212MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");