]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9c3c133b AC |
2 | /* |
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 | * | |
9c3c133b AC |
11 | * 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 | ||
7acd4de7 | 20 | #include <linux/completion.h> |
ca3bff70 RA |
21 | #include <linux/delay.h> |
22 | #include <linux/hrtimer.h> | |
9c3c133b AC |
23 | #include <linux/hw_random.h> |
24 | #include <linux/io.h> | |
ca3bff70 | 25 | #include <linux/ktime.h> |
7acd4de7 RA |
26 | #include <linux/module.h> |
27 | #include <linux/of.h> | |
28 | #include <linux/platform_device.h> | |
1907da78 | 29 | #include <linux/slab.h> |
ca3bff70 | 30 | #include <linux/time.h> |
9c3c133b | 31 | #include <linux/timeriomem-rng.h> |
9c3c133b | 32 | |
5ab693e6 | 33 | struct timeriomem_rng_private { |
1907da78 | 34 | void __iomem *io_base; |
ca3bff70 | 35 | ktime_t period; |
1907da78 | 36 | unsigned int present:1; |
9c3c133b | 37 | |
ca3bff70 | 38 | struct hrtimer timer; |
1907da78 AC |
39 | struct completion completion; |
40 | ||
5ab693e6 | 41 | struct hwrng rng_ops; |
1907da78 AC |
42 | }; |
43 | ||
7acd4de7 RA |
44 | static int timeriomem_rng_read(struct hwrng *hwrng, void *data, |
45 | size_t max, bool wait) | |
9c3c133b | 46 | { |
5ab693e6 RA |
47 | struct timeriomem_rng_private *priv = |
48 | container_of(hwrng, struct timeriomem_rng_private, rng_ops); | |
ca3bff70 RA |
49 | int retval = 0; |
50 | int period_us = ktime_to_us(priv->period); | |
9c3c133b | 51 | |
7acd4de7 RA |
52 | /* |
53 | * 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; | |
9c3c133b | 60 | |
7acd4de7 | 61 | wait_for_completion(&priv->completion); |
9c3c133b | 62 | |
ca3bff70 RA |
63 | 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)); | |
9c3c133b | 79 | |
7acd4de7 RA |
80 | /* |
81 | * Block any new callers until the RNG has had time to generate new | |
82 | * data. | |
83 | */ | |
1907da78 | 84 | priv->present = 0; |
16735d02 | 85 | reinit_completion(&priv->completion); |
ca3bff70 RA |
86 | hrtimer_forward_now(&priv->timer, priv->period); |
87 | hrtimer_restart(&priv->timer); | |
9c3c133b | 88 | |
ca3bff70 | 89 | return retval; |
9c3c133b AC |
90 | } |
91 | ||
ca3bff70 | 92 | static enum hrtimer_restart timeriomem_rng_trigger(struct hrtimer *timer) |
9c3c133b | 93 | { |
5ab693e6 | 94 | struct timeriomem_rng_private *priv |
ca3bff70 | 95 | = container_of(timer, struct timeriomem_rng_private, timer); |
9c3c133b | 96 | |
1907da78 AC |
97 | priv->present = 1; |
98 | complete(&priv->completion); | |
ca3bff70 RA |
99 | |
100 | return HRTIMER_NORESTART; | |
1907da78 | 101 | } |
9c3c133b | 102 | |
bcd2982a | 103 | static int timeriomem_rng_probe(struct platform_device *pdev) |
9c3c133b | 104 | { |
1907da78 | 105 | struct timeriomem_rng_data *pdata = pdev->dev.platform_data; |
5ab693e6 | 106 | struct timeriomem_rng_private *priv; |
08ced854 | 107 | struct resource *res; |
1907da78 AC |
108 | int err = 0; |
109 | int period; | |
9c3c133b | 110 | |
b149a30d | 111 | if (!pdev->dev.of_node && !pdata) { |
1907da78 AC |
112 | dev_err(&pdev->dev, "timeriomem_rng_data is missing\n"); |
113 | return -EINVAL; | |
114 | } | |
3341323b | 115 | |
1907da78 | 116 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
3341323b | 117 | if (!res) |
1907da78 | 118 | return -ENXIO; |
3341323b | 119 | |
2a4bfd02 | 120 | if (res->start % 4 != 0 || resource_size(res) < 4) { |
1907da78 | 121 | dev_err(&pdev->dev, |
2a4bfd02 | 122 | "address must be at least four bytes wide and 32-bit aligned\n"); |
1907da78 AC |
123 | return -EINVAL; |
124 | } | |
9c3c133b | 125 | |
1907da78 | 126 | /* Allocate memory for the device structure (and zero it) */ |
93b7f9c9 | 127 | priv = devm_kzalloc(&pdev->dev, |
5ab693e6 | 128 | sizeof(struct timeriomem_rng_private), GFP_KERNEL); |
7bad94aa | 129 | if (!priv) |
1907da78 | 130 | return -ENOMEM; |
1907da78 AC |
131 | |
132 | platform_set_drvdata(pdev, priv); | |
3341323b | 133 | |
b149a30d AC |
134 | 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"); | |
93b7f9c9 | 142 | return -EINVAL; |
b149a30d | 143 | } |
284e7638 RA |
144 | |
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; | |
93b7f9c9 | 150 | } else { |
b149a30d | 151 | period = pdata->period; |
284e7638 | 152 | priv->rng_ops.quality = pdata->quality; |
93b7f9c9 | 153 | } |
9c3c133b | 154 | |
ca3bff70 | 155 | priv->period = ns_to_ktime(period * NSEC_PER_USEC); |
1907da78 | 156 | init_completion(&priv->completion); |
ca3bff70 RA |
157 | hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
158 | priv->timer.function = timeriomem_rng_trigger; | |
1907da78 | 159 | |
5ab693e6 RA |
160 | priv->rng_ops.name = dev_name(&pdev->dev); |
161 | priv->rng_ops.read = timeriomem_rng_read; | |
1907da78 | 162 | |
93b7f9c9 JH |
163 | priv->io_base = devm_ioremap_resource(&pdev->dev, res); |
164 | if (IS_ERR(priv->io_base)) { | |
ca3bff70 | 165 | return PTR_ERR(priv->io_base); |
1907da78 AC |
166 | } |
167 | ||
ca3bff70 RA |
168 | /* Assume random data is already available. */ |
169 | priv->present = 1; | |
170 | complete(&priv->completion); | |
171 | ||
5ab693e6 | 172 | err = hwrng_register(&priv->rng_ops); |
1907da78 AC |
173 | if (err) { |
174 | dev_err(&pdev->dev, "problem registering\n"); | |
ca3bff70 | 175 | return err; |
1907da78 | 176 | } |
9c3c133b AC |
177 | |
178 | dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n", | |
1907da78 | 179 | priv->io_base, period); |
9c3c133b AC |
180 | |
181 | return 0; | |
182 | } | |
183 | ||
39af33fc | 184 | static int timeriomem_rng_remove(struct platform_device *pdev) |
9c3c133b | 185 | { |
5ab693e6 | 186 | struct timeriomem_rng_private *priv = platform_get_drvdata(pdev); |
1907da78 | 187 | |
5ab693e6 | 188 | hwrng_unregister(&priv->rng_ops); |
ca3bff70 | 189 | hrtimer_cancel(&priv->timer); |
3341323b | 190 | |
9c3c133b AC |
191 | return 0; |
192 | } | |
193 | ||
b149a30d AC |
194 | static const struct of_device_id timeriomem_rng_match[] = { |
195 | { .compatible = "timeriomem_rng" }, | |
196 | {}, | |
197 | }; | |
198 | MODULE_DEVICE_TABLE(of, timeriomem_rng_match); | |
199 | ||
9c3c133b AC |
200 | static struct platform_driver timeriomem_rng_driver = { |
201 | .driver = { | |
202 | .name = "timeriomem_rng", | |
b149a30d | 203 | .of_match_table = timeriomem_rng_match, |
9c3c133b AC |
204 | }, |
205 | .probe = timeriomem_rng_probe, | |
bcd2982a | 206 | .remove = timeriomem_rng_remove, |
9c3c133b AC |
207 | }; |
208 | ||
b21cb324 | 209 | module_platform_driver(timeriomem_rng_driver); |
9c3c133b AC |
210 | |
211 | MODULE_LICENSE("GPL"); | |
212 | MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>"); | |
213 | MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver"); |