]>
Commit | Line | Data |
---|---|---|
9c3c133b AC |
1 | /* |
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 | ||
7acd4de7 | 23 | #include <linux/completion.h> |
9c3c133b AC |
24 | #include <linux/hw_random.h> |
25 | #include <linux/io.h> | |
7acd4de7 RA |
26 | #include <linux/jiffies.h> |
27 | #include <linux/module.h> | |
28 | #include <linux/of.h> | |
29 | #include <linux/platform_device.h> | |
1907da78 | 30 | #include <linux/slab.h> |
9c3c133b | 31 | #include <linux/timeriomem-rng.h> |
9c3c133b | 32 | #include <linux/timer.h> |
9c3c133b | 33 | |
1907da78 AC |
34 | struct timeriomem_rng_private_data { |
35 | void __iomem *io_base; | |
36 | unsigned int expires; | |
37 | unsigned int period; | |
38 | unsigned int present:1; | |
9c3c133b | 39 | |
1907da78 AC |
40 | struct timer_list timer; |
41 | struct completion completion; | |
42 | ||
43 | struct hwrng timeriomem_rng_ops; | |
44 | }; | |
45 | ||
7acd4de7 RA |
46 | static int timeriomem_rng_read(struct hwrng *hwrng, void *data, |
47 | size_t max, bool wait) | |
9c3c133b | 48 | { |
7acd4de7 RA |
49 | struct timeriomem_rng_private_data *priv = |
50 | container_of(hwrng, struct timeriomem_rng_private_data, | |
51 | timeriomem_rng_ops); | |
52 | unsigned long cur; | |
53 | s32 delay; | |
9c3c133b | 54 | |
7acd4de7 RA |
55 | /* The RNG provides 32-bit per read. Ensure there is enough space. */ |
56 | if (max < sizeof(u32)) | |
57 | return 0; | |
9c3c133b | 58 | |
7acd4de7 RA |
59 | /* |
60 | * There may not have been enough time for new data to be generated | |
61 | * since the last request. If the caller doesn't want to wait, let them | |
62 | * bail out. Otherwise, wait for the completion. If the new data has | |
63 | * already been generated, the completion should already be available. | |
64 | */ | |
65 | if (!wait && !priv->present) | |
66 | return 0; | |
9c3c133b | 67 | |
7acd4de7 | 68 | wait_for_completion(&priv->completion); |
9c3c133b | 69 | |
7acd4de7 | 70 | *(u32 *)data = readl(priv->io_base); |
9c3c133b | 71 | |
7acd4de7 RA |
72 | /* |
73 | * Block any new callers until the RNG has had time to generate new | |
74 | * data. | |
75 | */ | |
1907da78 | 76 | cur = jiffies; |
9c3c133b | 77 | |
1907da78 AC |
78 | delay = cur - priv->expires; |
79 | delay = priv->period - (delay % priv->period); | |
9c3c133b | 80 | |
1907da78 AC |
81 | priv->expires = cur + delay; |
82 | priv->present = 0; | |
9c3c133b | 83 | |
16735d02 | 84 | reinit_completion(&priv->completion); |
1907da78 | 85 | mod_timer(&priv->timer, priv->expires); |
9c3c133b AC |
86 | |
87 | return 4; | |
88 | } | |
89 | ||
1907da78 | 90 | static void timeriomem_rng_trigger(unsigned long data) |
9c3c133b | 91 | { |
1907da78 AC |
92 | struct timeriomem_rng_private_data *priv |
93 | = (struct timeriomem_rng_private_data *)data; | |
9c3c133b | 94 | |
1907da78 AC |
95 | priv->present = 1; |
96 | complete(&priv->completion); | |
97 | } | |
9c3c133b | 98 | |
bcd2982a | 99 | static int timeriomem_rng_probe(struct platform_device *pdev) |
9c3c133b | 100 | { |
1907da78 AC |
101 | struct timeriomem_rng_data *pdata = pdev->dev.platform_data; |
102 | struct timeriomem_rng_private_data *priv; | |
08ced854 | 103 | struct resource *res; |
1907da78 AC |
104 | int err = 0; |
105 | int period; | |
9c3c133b | 106 | |
b149a30d | 107 | if (!pdev->dev.of_node && !pdata) { |
1907da78 AC |
108 | dev_err(&pdev->dev, "timeriomem_rng_data is missing\n"); |
109 | return -EINVAL; | |
110 | } | |
3341323b | 111 | |
1907da78 | 112 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
3341323b | 113 | if (!res) |
1907da78 | 114 | return -ENXIO; |
3341323b | 115 | |
1907da78 AC |
116 | if (res->start % 4 != 0 || resource_size(res) != 4) { |
117 | dev_err(&pdev->dev, | |
118 | "address must be four bytes wide and aligned\n"); | |
119 | return -EINVAL; | |
120 | } | |
9c3c133b | 121 | |
1907da78 | 122 | /* Allocate memory for the device structure (and zero it) */ |
93b7f9c9 JH |
123 | priv = devm_kzalloc(&pdev->dev, |
124 | sizeof(struct timeriomem_rng_private_data), GFP_KERNEL); | |
7bad94aa | 125 | if (!priv) |
1907da78 | 126 | return -ENOMEM; |
1907da78 AC |
127 | |
128 | platform_set_drvdata(pdev, priv); | |
3341323b | 129 | |
b149a30d AC |
130 | if (pdev->dev.of_node) { |
131 | int i; | |
132 | ||
133 | if (!of_property_read_u32(pdev->dev.of_node, | |
134 | "period", &i)) | |
135 | period = i; | |
136 | else { | |
137 | dev_err(&pdev->dev, "missing period\n"); | |
93b7f9c9 | 138 | return -EINVAL; |
b149a30d | 139 | } |
93b7f9c9 | 140 | } else { |
b149a30d | 141 | period = pdata->period; |
93b7f9c9 | 142 | } |
9c3c133b | 143 | |
1907da78 AC |
144 | priv->period = usecs_to_jiffies(period); |
145 | if (priv->period < 1) { | |
146 | dev_err(&pdev->dev, "period is less than one jiffy\n"); | |
93b7f9c9 | 147 | return -EINVAL; |
9c3c133b | 148 | } |
9c3c133b | 149 | |
1907da78 AC |
150 | priv->expires = jiffies; |
151 | priv->present = 1; | |
152 | ||
153 | init_completion(&priv->completion); | |
154 | complete(&priv->completion); | |
155 | ||
156 | setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv); | |
157 | ||
158 | priv->timeriomem_rng_ops.name = dev_name(&pdev->dev); | |
7acd4de7 | 159 | priv->timeriomem_rng_ops.read = timeriomem_rng_read; |
1907da78 | 160 | |
93b7f9c9 JH |
161 | priv->io_base = devm_ioremap_resource(&pdev->dev, res); |
162 | if (IS_ERR(priv->io_base)) { | |
163 | err = PTR_ERR(priv->io_base); | |
1907da78 AC |
164 | goto out_timer; |
165 | } | |
166 | ||
1907da78 AC |
167 | err = hwrng_register(&priv->timeriomem_rng_ops); |
168 | if (err) { | |
169 | dev_err(&pdev->dev, "problem registering\n"); | |
93b7f9c9 | 170 | goto out_timer; |
1907da78 | 171 | } |
9c3c133b AC |
172 | |
173 | dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n", | |
1907da78 | 174 | priv->io_base, period); |
9c3c133b AC |
175 | |
176 | return 0; | |
3341323b | 177 | |
1907da78 AC |
178 | out_timer: |
179 | del_timer_sync(&priv->timer); | |
1907da78 | 180 | return err; |
9c3c133b AC |
181 | } |
182 | ||
39af33fc | 183 | static int timeriomem_rng_remove(struct platform_device *pdev) |
9c3c133b | 184 | { |
1907da78 | 185 | struct timeriomem_rng_private_data *priv = platform_get_drvdata(pdev); |
1907da78 AC |
186 | |
187 | hwrng_unregister(&priv->timeriomem_rng_ops); | |
9c3c133b | 188 | |
1907da78 | 189 | del_timer_sync(&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"); |