xref: /linux/drivers/char/hw_random/stm32-rng.c (revision 326ed382256475aa4b8b7eae8a2f60689fd25e78)
1c6a97c42SDaniel Thompson /*
2c6a97c42SDaniel Thompson  * Copyright (c) 2015, Daniel Thompson
3c6a97c42SDaniel Thompson  *
4c6a97c42SDaniel Thompson  * This file is free software; you can redistribute it and/or
5c6a97c42SDaniel Thompson  * modify it under the terms of the GNU General Public License
6c6a97c42SDaniel Thompson  * as published by the Free Software Foundation; either version 2
7c6a97c42SDaniel Thompson  * of the License, or (at your option) any later version.
8c6a97c42SDaniel Thompson  *
9c6a97c42SDaniel Thompson  * This file is distributed in the hope that it will be useful,
10c6a97c42SDaniel Thompson  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11c6a97c42SDaniel Thompson  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12c6a97c42SDaniel Thompson  * GNU General Public License for more details.
13c6a97c42SDaniel Thompson  */
14c6a97c42SDaniel Thompson 
15c6a97c42SDaniel Thompson #include <linux/clk.h>
16c6a97c42SDaniel Thompson #include <linux/delay.h>
17c6a97c42SDaniel Thompson #include <linux/hw_random.h>
18c6a97c42SDaniel Thompson #include <linux/io.h>
19c6a97c42SDaniel Thompson #include <linux/kernel.h>
20c6a97c42SDaniel Thompson #include <linux/module.h>
21c6a97c42SDaniel Thompson #include <linux/of_address.h>
22c6a97c42SDaniel Thompson #include <linux/of_platform.h>
23c6a97c42SDaniel Thompson #include <linux/pm_runtime.h>
24*326ed382Slionel.debieve@st.com #include <linux/reset.h>
25c6a97c42SDaniel Thompson #include <linux/slab.h>
26c6a97c42SDaniel Thompson 
27c6a97c42SDaniel Thompson #define RNG_CR 0x00
28c6a97c42SDaniel Thompson #define RNG_CR_RNGEN BIT(2)
29c6a97c42SDaniel Thompson 
30c6a97c42SDaniel Thompson #define RNG_SR 0x04
31c6a97c42SDaniel Thompson #define RNG_SR_SEIS BIT(6)
32c6a97c42SDaniel Thompson #define RNG_SR_CEIS BIT(5)
33c6a97c42SDaniel Thompson #define RNG_SR_DRDY BIT(0)
34c6a97c42SDaniel Thompson 
35c6a97c42SDaniel Thompson #define RNG_DR 0x08
36c6a97c42SDaniel Thompson 
37c6a97c42SDaniel Thompson /*
38c6a97c42SDaniel Thompson  * It takes 40 cycles @ 48MHz to generate each random number (e.g. <1us).
39c6a97c42SDaniel Thompson  * At the time of writing STM32 parts max out at ~200MHz meaning a timeout
40c6a97c42SDaniel Thompson  * of 500 leaves us a very comfortable margin for error. The loop to which
41c6a97c42SDaniel Thompson  * the timeout applies takes at least 4 instructions per iteration so the
42c6a97c42SDaniel Thompson  * timeout is enough to take us up to multi-GHz parts!
43c6a97c42SDaniel Thompson  */
44c6a97c42SDaniel Thompson #define RNG_TIMEOUT 500
45c6a97c42SDaniel Thompson 
46c6a97c42SDaniel Thompson struct stm32_rng_private {
47c6a97c42SDaniel Thompson 	struct hwrng rng;
48c6a97c42SDaniel Thompson 	void __iomem *base;
49c6a97c42SDaniel Thompson 	struct clk *clk;
50*326ed382Slionel.debieve@st.com 	struct reset_control *rst;
51c6a97c42SDaniel Thompson };
52c6a97c42SDaniel Thompson 
53c6a97c42SDaniel Thompson static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
54c6a97c42SDaniel Thompson {
55c6a97c42SDaniel Thompson 	struct stm32_rng_private *priv =
56c6a97c42SDaniel Thompson 	    container_of(rng, struct stm32_rng_private, rng);
57c6a97c42SDaniel Thompson 	u32 sr;
58c6a97c42SDaniel Thompson 	int retval = 0;
59c6a97c42SDaniel Thompson 
60c6a97c42SDaniel Thompson 	pm_runtime_get_sync((struct device *) priv->rng.priv);
61c6a97c42SDaniel Thompson 
62c6a97c42SDaniel Thompson 	while (max > sizeof(u32)) {
63c6a97c42SDaniel Thompson 		sr = readl_relaxed(priv->base + RNG_SR);
64c6a97c42SDaniel Thompson 		if (!sr && wait) {
65c6a97c42SDaniel Thompson 			unsigned int timeout = RNG_TIMEOUT;
66c6a97c42SDaniel Thompson 
67c6a97c42SDaniel Thompson 			do {
68c6a97c42SDaniel Thompson 				cpu_relax();
69c6a97c42SDaniel Thompson 				sr = readl_relaxed(priv->base + RNG_SR);
70c6a97c42SDaniel Thompson 			} while (!sr && --timeout);
71c6a97c42SDaniel Thompson 		}
72c6a97c42SDaniel Thompson 
73c6a97c42SDaniel Thompson 		/* If error detected or data not ready... */
741ff69adfSMaxime Coquelin 		if (sr != RNG_SR_DRDY) {
751ff69adfSMaxime Coquelin 			if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
761ff69adfSMaxime Coquelin 					"bad RNG status - %x\n", sr))
771ff69adfSMaxime Coquelin 				writel_relaxed(0, priv->base + RNG_SR);
78c6a97c42SDaniel Thompson 			break;
791ff69adfSMaxime Coquelin 		}
80c6a97c42SDaniel Thompson 
81c6a97c42SDaniel Thompson 		*(u32 *)data = readl_relaxed(priv->base + RNG_DR);
82c6a97c42SDaniel Thompson 
83c6a97c42SDaniel Thompson 		retval += sizeof(u32);
84c6a97c42SDaniel Thompson 		data += sizeof(u32);
85c6a97c42SDaniel Thompson 		max -= sizeof(u32);
86c6a97c42SDaniel Thompson 	}
87c6a97c42SDaniel Thompson 
88c6a97c42SDaniel Thompson 	pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
89c6a97c42SDaniel Thompson 	pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
90c6a97c42SDaniel Thompson 
91c6a97c42SDaniel Thompson 	return retval || !wait ? retval : -EIO;
92c6a97c42SDaniel Thompson }
93c6a97c42SDaniel Thompson 
94c6a97c42SDaniel Thompson static int stm32_rng_init(struct hwrng *rng)
95c6a97c42SDaniel Thompson {
96c6a97c42SDaniel Thompson 	struct stm32_rng_private *priv =
97c6a97c42SDaniel Thompson 	    container_of(rng, struct stm32_rng_private, rng);
98c6a97c42SDaniel Thompson 	int err;
99c6a97c42SDaniel Thompson 
100c6a97c42SDaniel Thompson 	err = clk_prepare_enable(priv->clk);
101c6a97c42SDaniel Thompson 	if (err)
102c6a97c42SDaniel Thompson 		return err;
103c6a97c42SDaniel Thompson 
104c6a97c42SDaniel Thompson 	writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
105c6a97c42SDaniel Thompson 
106c6a97c42SDaniel Thompson 	/* clear error indicators */
107c6a97c42SDaniel Thompson 	writel_relaxed(0, priv->base + RNG_SR);
108c6a97c42SDaniel Thompson 
109c6a97c42SDaniel Thompson 	return 0;
110c6a97c42SDaniel Thompson }
111c6a97c42SDaniel Thompson 
112c6a97c42SDaniel Thompson static void stm32_rng_cleanup(struct hwrng *rng)
113c6a97c42SDaniel Thompson {
114c6a97c42SDaniel Thompson 	struct stm32_rng_private *priv =
115c6a97c42SDaniel Thompson 	    container_of(rng, struct stm32_rng_private, rng);
116c6a97c42SDaniel Thompson 
117c6a97c42SDaniel Thompson 	writel_relaxed(0, priv->base + RNG_CR);
118c6a97c42SDaniel Thompson 	clk_disable_unprepare(priv->clk);
119c6a97c42SDaniel Thompson }
120c6a97c42SDaniel Thompson 
121c6a97c42SDaniel Thompson static int stm32_rng_probe(struct platform_device *ofdev)
122c6a97c42SDaniel Thompson {
123c6a97c42SDaniel Thompson 	struct device *dev = &ofdev->dev;
124c6a97c42SDaniel Thompson 	struct device_node *np = ofdev->dev.of_node;
125c6a97c42SDaniel Thompson 	struct stm32_rng_private *priv;
126c6a97c42SDaniel Thompson 	struct resource res;
127c6a97c42SDaniel Thompson 	int err;
128c6a97c42SDaniel Thompson 
129c6a97c42SDaniel Thompson 	priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
130c6a97c42SDaniel Thompson 	if (!priv)
131c6a97c42SDaniel Thompson 		return -ENOMEM;
132c6a97c42SDaniel Thompson 
133c6a97c42SDaniel Thompson 	err = of_address_to_resource(np, 0, &res);
134c6a97c42SDaniel Thompson 	if (err)
135c6a97c42SDaniel Thompson 		return err;
136c6a97c42SDaniel Thompson 
137c6a97c42SDaniel Thompson 	priv->base = devm_ioremap_resource(dev, &res);
138c6a97c42SDaniel Thompson 	if (IS_ERR(priv->base))
139c6a97c42SDaniel Thompson 		return PTR_ERR(priv->base);
140c6a97c42SDaniel Thompson 
141c6a97c42SDaniel Thompson 	priv->clk = devm_clk_get(&ofdev->dev, NULL);
142c6a97c42SDaniel Thompson 	if (IS_ERR(priv->clk))
143c6a97c42SDaniel Thompson 		return PTR_ERR(priv->clk);
144c6a97c42SDaniel Thompson 
145*326ed382Slionel.debieve@st.com 	priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
146*326ed382Slionel.debieve@st.com 	if (!IS_ERR(priv->rst)) {
147*326ed382Slionel.debieve@st.com 		reset_control_assert(priv->rst);
148*326ed382Slionel.debieve@st.com 		udelay(2);
149*326ed382Slionel.debieve@st.com 		reset_control_deassert(priv->rst);
150*326ed382Slionel.debieve@st.com 	}
151*326ed382Slionel.debieve@st.com 
152c6a97c42SDaniel Thompson 	dev_set_drvdata(dev, priv);
153c6a97c42SDaniel Thompson 
154c6a97c42SDaniel Thompson 	priv->rng.name = dev_driver_string(dev),
155c6a97c42SDaniel Thompson #ifndef CONFIG_PM
156c6a97c42SDaniel Thompson 	priv->rng.init = stm32_rng_init,
157c6a97c42SDaniel Thompson 	priv->rng.cleanup = stm32_rng_cleanup,
158c6a97c42SDaniel Thompson #endif
159c6a97c42SDaniel Thompson 	priv->rng.read = stm32_rng_read,
160c6a97c42SDaniel Thompson 	priv->rng.priv = (unsigned long) dev;
161c6a97c42SDaniel Thompson 
162c6a97c42SDaniel Thompson 	pm_runtime_set_autosuspend_delay(dev, 100);
163c6a97c42SDaniel Thompson 	pm_runtime_use_autosuspend(dev);
164c6a97c42SDaniel Thompson 	pm_runtime_enable(dev);
165c6a97c42SDaniel Thompson 
166c6a97c42SDaniel Thompson 	return devm_hwrng_register(dev, &priv->rng);
167c6a97c42SDaniel Thompson }
168c6a97c42SDaniel Thompson 
169c6a97c42SDaniel Thompson #ifdef CONFIG_PM
170c6a97c42SDaniel Thompson static int stm32_rng_runtime_suspend(struct device *dev)
171c6a97c42SDaniel Thompson {
172d6ba06b8SDaniel Thompson 	struct stm32_rng_private *priv = dev_get_drvdata(dev);
173c6a97c42SDaniel Thompson 
174c6a97c42SDaniel Thompson 	stm32_rng_cleanup(&priv->rng);
175c6a97c42SDaniel Thompson 
176c6a97c42SDaniel Thompson 	return 0;
177c6a97c42SDaniel Thompson }
178c6a97c42SDaniel Thompson 
179c6a97c42SDaniel Thompson static int stm32_rng_runtime_resume(struct device *dev)
180c6a97c42SDaniel Thompson {
181d6ba06b8SDaniel Thompson 	struct stm32_rng_private *priv = dev_get_drvdata(dev);
182c6a97c42SDaniel Thompson 
183c6a97c42SDaniel Thompson 	return stm32_rng_init(&priv->rng);
184c6a97c42SDaniel Thompson }
185c6a97c42SDaniel Thompson #endif
186c6a97c42SDaniel Thompson 
187c6a97c42SDaniel Thompson static UNIVERSAL_DEV_PM_OPS(stm32_rng_pm_ops, stm32_rng_runtime_suspend,
188c6a97c42SDaniel Thompson 			    stm32_rng_runtime_resume, NULL);
189c6a97c42SDaniel Thompson 
190c6a97c42SDaniel Thompson static const struct of_device_id stm32_rng_match[] = {
191c6a97c42SDaniel Thompson 	{
192c6a97c42SDaniel Thompson 		.compatible = "st,stm32-rng",
193c6a97c42SDaniel Thompson 	},
194c6a97c42SDaniel Thompson 	{},
195c6a97c42SDaniel Thompson };
196c6a97c42SDaniel Thompson MODULE_DEVICE_TABLE(of, stm32_rng_match);
197c6a97c42SDaniel Thompson 
198c6a97c42SDaniel Thompson static struct platform_driver stm32_rng_driver = {
199c6a97c42SDaniel Thompson 	.driver = {
200c6a97c42SDaniel Thompson 		.name = "stm32-rng",
201c6a97c42SDaniel Thompson 		.pm = &stm32_rng_pm_ops,
202c6a97c42SDaniel Thompson 		.of_match_table = stm32_rng_match,
203c6a97c42SDaniel Thompson 	},
204c6a97c42SDaniel Thompson 	.probe = stm32_rng_probe,
205c6a97c42SDaniel Thompson };
206c6a97c42SDaniel Thompson 
207c6a97c42SDaniel Thompson module_platform_driver(stm32_rng_driver);
208c6a97c42SDaniel Thompson 
209c6a97c42SDaniel Thompson MODULE_LICENSE("GPL");
210c6a97c42SDaniel Thompson MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
211c6a97c42SDaniel Thompson MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");
212