xref: /linux/drivers/char/hw_random/stm32-rng.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1226b0b0aSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c6a97c42SDaniel Thompson /*
3c6a97c42SDaniel Thompson  * Copyright (c) 2015, Daniel Thompson
4c6a97c42SDaniel Thompson  */
5c6a97c42SDaniel Thompson 
6c6a97c42SDaniel Thompson #include <linux/clk.h>
7c6a97c42SDaniel Thompson #include <linux/delay.h>
8c6a97c42SDaniel Thompson #include <linux/hw_random.h>
9c6a97c42SDaniel Thompson #include <linux/io.h>
10279f4f8fSlionel.debieve@st.com #include <linux/iopoll.h>
11c6a97c42SDaniel Thompson #include <linux/kernel.h>
12c6a97c42SDaniel Thompson #include <linux/module.h>
130788257aSRob Herring #include <linux/of.h>
14c6a97c42SDaniel Thompson #include <linux/of_address.h>
150788257aSRob Herring #include <linux/platform_device.h>
16c6a97c42SDaniel Thompson #include <linux/pm_runtime.h>
17326ed382Slionel.debieve@st.com #include <linux/reset.h>
18c6a97c42SDaniel Thompson #include <linux/slab.h>
19c6a97c42SDaniel Thompson 
20c6a97c42SDaniel Thompson #define RNG_CR			0x00
21c6a97c42SDaniel Thompson #define RNG_CR_RNGEN		BIT(2)
22529571edSlionel.debieve@st.com #define RNG_CR_CED		BIT(5)
236b85a7e1SGatien Chevallier #define RNG_CR_CONFIG1		GENMASK(11, 8)
246b85a7e1SGatien Chevallier #define RNG_CR_NISTC		BIT(12)
256b85a7e1SGatien Chevallier #define RNG_CR_CONFIG2		GENMASK(15, 13)
2628d13f3fSGatien Chevallier #define RNG_CR_CLKDIV_SHIFT	16
2728d13f3fSGatien Chevallier #define RNG_CR_CLKDIV		GENMASK(19, 16)
286b85a7e1SGatien Chevallier #define RNG_CR_CONFIG3		GENMASK(25, 20)
296b85a7e1SGatien Chevallier #define RNG_CR_CONDRST		BIT(30)
306b85a7e1SGatien Chevallier #define RNG_CR_CONFLOCK		BIT(31)
316b85a7e1SGatien Chevallier #define RNG_CR_ENTROPY_SRC_MASK	(RNG_CR_CONFIG1 | RNG_CR_NISTC | RNG_CR_CONFIG2 | RNG_CR_CONFIG3)
3228d13f3fSGatien Chevallier #define RNG_CR_CONFIG_MASK	(RNG_CR_ENTROPY_SRC_MASK | RNG_CR_CED | RNG_CR_CLKDIV)
33c6a97c42SDaniel Thompson 
34c6a97c42SDaniel Thompson #define RNG_SR			0x04
35c6a97c42SDaniel Thompson #define RNG_SR_DRDY		BIT(0)
368f1c5227SGatien Chevallier #define RNG_SR_CECS		BIT(1)
378f1c5227SGatien Chevallier #define RNG_SR_SECS		BIT(2)
388f1c5227SGatien Chevallier #define RNG_SR_CEIS		BIT(5)
398f1c5227SGatien Chevallier #define RNG_SR_SEIS		BIT(6)
40c6a97c42SDaniel Thompson 
41c6a97c42SDaniel Thompson #define RNG_DR			0x08
42c6a97c42SDaniel Thompson 
436b85a7e1SGatien Chevallier #define RNG_NSCR		0x0C
446b85a7e1SGatien Chevallier #define RNG_NSCR_MASK		GENMASK(17, 0)
456b85a7e1SGatien Chevallier 
466b85a7e1SGatien Chevallier #define RNG_HTCR		0x10
476b85a7e1SGatien Chevallier 
48b17bc6ebSGatien Chevallier #define RNG_NB_RECOVER_TRIES	3
49b17bc6ebSGatien Chevallier 
506b85a7e1SGatien Chevallier struct stm32_rng_data {
5128d13f3fSGatien Chevallier 	uint	max_clock_rate;
526b85a7e1SGatien Chevallier 	u32	cr;
536b85a7e1SGatien Chevallier 	u32	nscr;
546b85a7e1SGatien Chevallier 	u32	htcr;
556b85a7e1SGatien Chevallier 	bool	has_cond_reset;
566b85a7e1SGatien Chevallier };
576b85a7e1SGatien Chevallier 
58ff4e4610SGatien Chevallier /**
59ff4e4610SGatien Chevallier  * struct stm32_rng_config - RNG configuration data
60ff4e4610SGatien Chevallier  *
61ff4e4610SGatien Chevallier  * @cr:			RNG configuration. 0 means default hardware RNG configuration
62ff4e4610SGatien Chevallier  * @nscr:		Noise sources control configuration.
63ff4e4610SGatien Chevallier  * @htcr:		Health tests configuration.
64ff4e4610SGatien Chevallier  */
65ff4e4610SGatien Chevallier struct stm32_rng_config {
66ff4e4610SGatien Chevallier 	u32 cr;
67ff4e4610SGatien Chevallier 	u32 nscr;
68ff4e4610SGatien Chevallier 	u32 htcr;
69ff4e4610SGatien Chevallier };
70ff4e4610SGatien Chevallier 
71c6a97c42SDaniel Thompson struct stm32_rng_private {
72c6a97c42SDaniel Thompson 	struct hwrng rng;
73771c7faaSMarek Vasut 	struct device *dev;
74c6a97c42SDaniel Thompson 	void __iomem *base;
75c6a97c42SDaniel Thompson 	struct clk *clk;
76326ed382Slionel.debieve@st.com 	struct reset_control *rst;
77ff4e4610SGatien Chevallier 	struct stm32_rng_config pm_conf;
786b85a7e1SGatien Chevallier 	const struct stm32_rng_data *data;
79529571edSlionel.debieve@st.com 	bool ced;
80a1b03e7aSGatien Chevallier 	bool lock_conf;
81c6a97c42SDaniel Thompson };
82c6a97c42SDaniel Thompson 
838f1c5227SGatien Chevallier /*
848f1c5227SGatien Chevallier  * Extracts from the STM32 RNG specification when RNG supports CONDRST.
858f1c5227SGatien Chevallier  *
868f1c5227SGatien Chevallier  * When a noise source (or seed) error occurs, the RNG stops generating
878f1c5227SGatien Chevallier  * random numbers and sets to “1” both SEIS and SECS bits to indicate
888f1c5227SGatien Chevallier  * that a seed error occurred. (...)
898f1c5227SGatien Chevallier  *
908f1c5227SGatien Chevallier  * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield
918f1c5227SGatien Chevallier  * description for details). This step is needed only if SECS is set.
928f1c5227SGatien Chevallier  * Indeed, when SEIS is set and SECS is cleared it means RNG performed
938f1c5227SGatien Chevallier  * the reset automatically (auto-reset).
948f1c5227SGatien Chevallier  * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST
958f1c5227SGatien Chevallier  * to be cleared in the RNG_CR register, then confirm that SEIS is
968f1c5227SGatien Chevallier  * cleared in the RNG_SR register. Otherwise just clear SEIS bit in
978f1c5227SGatien Chevallier  * the RNG_SR register.
988f1c5227SGatien Chevallier  * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be
998f1c5227SGatien Chevallier  * cleared by RNG. The random number generation is now back to normal.
1008f1c5227SGatien Chevallier  */
stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private * priv)1018f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv)
1028f1c5227SGatien Chevallier {
103771c7faaSMarek Vasut 	struct device *dev = priv->dev;
1048f1c5227SGatien Chevallier 	u32 sr = readl_relaxed(priv->base + RNG_SR);
1058f1c5227SGatien Chevallier 	u32 cr = readl_relaxed(priv->base + RNG_CR);
1068f1c5227SGatien Chevallier 	int err;
1078f1c5227SGatien Chevallier 
1088f1c5227SGatien Chevallier 	if (sr & RNG_SR_SECS) {
1098f1c5227SGatien Chevallier 		/* Conceal by resetting the subsystem (step 1.) */
1108f1c5227SGatien Chevallier 		writel_relaxed(cr | RNG_CR_CONDRST, priv->base + RNG_CR);
1118f1c5227SGatien Chevallier 		writel_relaxed(cr & ~RNG_CR_CONDRST, priv->base + RNG_CR);
1128f1c5227SGatien Chevallier 	} else {
1138f1c5227SGatien Chevallier 		/* RNG auto-reset (step 2.) */
1148f1c5227SGatien Chevallier 		writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
1158f1c5227SGatien Chevallier 		goto end;
1168f1c5227SGatien Chevallier 	}
1178f1c5227SGatien Chevallier 
1188f1c5227SGatien Chevallier 	err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, cr, !(cr & RNG_CR_CONDRST), 10,
1198f1c5227SGatien Chevallier 						100000);
1208f1c5227SGatien Chevallier 	if (err) {
1218f1c5227SGatien Chevallier 		dev_err(dev, "%s: timeout %x\n", __func__, sr);
1228f1c5227SGatien Chevallier 		return err;
1238f1c5227SGatien Chevallier 	}
1248f1c5227SGatien Chevallier 
1258f1c5227SGatien Chevallier 	/* Check SEIS is cleared (step 2.) */
1268f1c5227SGatien Chevallier 	if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
1278f1c5227SGatien Chevallier 		return -EINVAL;
1288f1c5227SGatien Chevallier 
1298f1c5227SGatien Chevallier 	err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 10,
1308f1c5227SGatien Chevallier 						100000);
1318f1c5227SGatien Chevallier 	if (err) {
1328f1c5227SGatien Chevallier 		dev_err(dev, "%s: timeout %x\n", __func__, sr);
1338f1c5227SGatien Chevallier 		return err;
1348f1c5227SGatien Chevallier 	}
1358f1c5227SGatien Chevallier 
1368f1c5227SGatien Chevallier end:
1378f1c5227SGatien Chevallier 	return 0;
1388f1c5227SGatien Chevallier }
1398f1c5227SGatien Chevallier 
1408f1c5227SGatien Chevallier /*
1418f1c5227SGatien Chevallier  * Extracts from the STM32 RNG specification, when CONDRST is not supported
1428f1c5227SGatien Chevallier  *
1438f1c5227SGatien Chevallier  * When a noise source (or seed) error occurs, the RNG stops generating
1448f1c5227SGatien Chevallier  * random numbers and sets to “1” both SEIS and SECS bits to indicate
1458f1c5227SGatien Chevallier  * that a seed error occurred. (...)
1468f1c5227SGatien Chevallier  *
1478f1c5227SGatien Chevallier  * The following sequence shall be used to fully recover from a seed
1488f1c5227SGatien Chevallier  * error after the RNG initialization:
1498f1c5227SGatien Chevallier  * 1. Clear the SEIS bit by writing it to “0”.
1508f1c5227SGatien Chevallier  * 2. Read out 12 words from the RNG_DR register, and discard each of
1518f1c5227SGatien Chevallier  * them in order to clean the pipeline.
1528f1c5227SGatien Chevallier  * 3. Confirm that SEIS is still cleared. Random number generation is
1538f1c5227SGatien Chevallier  * back to normal.
1548f1c5227SGatien Chevallier  */
stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_private * priv)1558f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_private *priv)
1568f1c5227SGatien Chevallier {
1578f1c5227SGatien Chevallier 	unsigned int i = 0;
1588f1c5227SGatien Chevallier 	u32 sr = readl_relaxed(priv->base + RNG_SR);
1598f1c5227SGatien Chevallier 
1608f1c5227SGatien Chevallier 	writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
1618f1c5227SGatien Chevallier 
1628f1c5227SGatien Chevallier 	for (i = 12; i != 0; i--)
1638f1c5227SGatien Chevallier 		(void)readl_relaxed(priv->base + RNG_DR);
1648f1c5227SGatien Chevallier 
1658f1c5227SGatien Chevallier 	if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
1668f1c5227SGatien Chevallier 		return -EINVAL;
1678f1c5227SGatien Chevallier 
1688f1c5227SGatien Chevallier 	return 0;
1698f1c5227SGatien Chevallier }
1708f1c5227SGatien Chevallier 
stm32_rng_conceal_seed_error(struct hwrng * rng)1718f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error(struct hwrng *rng)
1728f1c5227SGatien Chevallier {
1738f1c5227SGatien Chevallier 	struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
1748f1c5227SGatien Chevallier 
175771c7faaSMarek Vasut 	dev_dbg(priv->dev, "Concealing seed error\n");
1768f1c5227SGatien Chevallier 
1778f1c5227SGatien Chevallier 	if (priv->data->has_cond_reset)
1788f1c5227SGatien Chevallier 		return stm32_rng_conceal_seed_error_cond_reset(priv);
1798f1c5227SGatien Chevallier 	else
1808f1c5227SGatien Chevallier 		return stm32_rng_conceal_seed_error_sw_reset(priv);
1818f1c5227SGatien Chevallier };
1828f1c5227SGatien Chevallier 
1838f1c5227SGatien Chevallier 
stm32_rng_read(struct hwrng * rng,void * data,size_t max,bool wait)184c6a97c42SDaniel Thompson static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
185c6a97c42SDaniel Thompson {
186b17bc6ebSGatien Chevallier 	struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
187b17bc6ebSGatien Chevallier 	unsigned int i = 0;
188b17bc6ebSGatien Chevallier 	int retval = 0, err = 0;
189c6a97c42SDaniel Thompson 	u32 sr;
190c6a97c42SDaniel Thompson 
191771c7faaSMarek Vasut 	retval = pm_runtime_resume_and_get(priv->dev);
192f134d5dcSMarek Vasut 	if (retval)
193f134d5dcSMarek Vasut 		return retval;
194c6a97c42SDaniel Thompson 
1958f1c5227SGatien Chevallier 	if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
1968f1c5227SGatien Chevallier 		stm32_rng_conceal_seed_error(rng);
1978f1c5227SGatien Chevallier 
198e64f57e8STomas Marek 	while (max >= sizeof(u32)) {
199c6a97c42SDaniel Thompson 		sr = readl_relaxed(priv->base + RNG_SR);
200b17bc6ebSGatien Chevallier 		/*
201b17bc6ebSGatien Chevallier 		 * Manage timeout which is based on timer and take
202b17bc6ebSGatien Chevallier 		 * care of initial delay time when enabling the RNG.
203b17bc6ebSGatien Chevallier 		 */
204c6a97c42SDaniel Thompson 		if (!sr && wait) {
2057cdc5e6bSTomas Marek 			err = readl_relaxed_poll_timeout_atomic(priv->base
206279f4f8fSlionel.debieve@st.com 								   + RNG_SR,
207279f4f8fSlionel.debieve@st.com 								   sr, sr,
208279f4f8fSlionel.debieve@st.com 								   10, 50000);
209b17bc6ebSGatien Chevallier 			if (err) {
210771c7faaSMarek Vasut 				dev_err(priv->dev, "%s: timeout %x!\n", __func__, sr);
211b17bc6ebSGatien Chevallier 				break;
212c6a97c42SDaniel Thompson 			}
213b17bc6ebSGatien Chevallier 		} else if (!sr) {
214b17bc6ebSGatien Chevallier 			/* The FIFO is being filled up */
215c6a97c42SDaniel Thompson 			break;
2161ff69adfSMaxime Coquelin 		}
217c6a97c42SDaniel Thompson 
218b17bc6ebSGatien Chevallier 		if (sr != RNG_SR_DRDY) {
219b17bc6ebSGatien Chevallier 			if (sr & RNG_SR_SEIS) {
220b17bc6ebSGatien Chevallier 				err = stm32_rng_conceal_seed_error(rng);
221b17bc6ebSGatien Chevallier 				i++;
222b17bc6ebSGatien Chevallier 				if (err && i > RNG_NB_RECOVER_TRIES) {
223771c7faaSMarek Vasut 					dev_err(priv->dev, "Couldn't recover from seed error\n");
224da62ed5cSMarek Vasut 					retval = -ENOTRECOVERABLE;
225da62ed5cSMarek Vasut 					goto exit_rpm;
226b17bc6ebSGatien Chevallier 				}
227c6a97c42SDaniel Thompson 
228b17bc6ebSGatien Chevallier 				continue;
229b17bc6ebSGatien Chevallier 			}
230b17bc6ebSGatien Chevallier 
231b17bc6ebSGatien Chevallier 			if (WARN_ONCE((sr & RNG_SR_CEIS), "RNG clock too slow - %x\n", sr))
232b17bc6ebSGatien Chevallier 				writel_relaxed(0, priv->base + RNG_SR);
233b17bc6ebSGatien Chevallier 		}
234b17bc6ebSGatien Chevallier 
235b17bc6ebSGatien Chevallier 		/* Late seed error case: DR being 0 is an error status */
236b17bc6ebSGatien Chevallier 		*(u32 *)data = readl_relaxed(priv->base + RNG_DR);
237b17bc6ebSGatien Chevallier 		if (!*(u32 *)data) {
238b17bc6ebSGatien Chevallier 			err = stm32_rng_conceal_seed_error(rng);
239b17bc6ebSGatien Chevallier 			i++;
240b17bc6ebSGatien Chevallier 			if (err && i > RNG_NB_RECOVER_TRIES) {
241771c7faaSMarek Vasut 				dev_err(priv->dev, "Couldn't recover from seed error");
242da62ed5cSMarek Vasut 				retval = -ENOTRECOVERABLE;
243da62ed5cSMarek Vasut 				goto exit_rpm;
244b17bc6ebSGatien Chevallier 			}
245b17bc6ebSGatien Chevallier 
246b17bc6ebSGatien Chevallier 			continue;
247b17bc6ebSGatien Chevallier 		}
248b17bc6ebSGatien Chevallier 
249b17bc6ebSGatien Chevallier 		i = 0;
250c6a97c42SDaniel Thompson 		retval += sizeof(u32);
251c6a97c42SDaniel Thompson 		data += sizeof(u32);
252c6a97c42SDaniel Thompson 		max -= sizeof(u32);
253c6a97c42SDaniel Thompson 	}
254c6a97c42SDaniel Thompson 
255da62ed5cSMarek Vasut exit_rpm:
256771c7faaSMarek Vasut 	pm_runtime_mark_last_busy(priv->dev);
257771c7faaSMarek Vasut 	pm_runtime_put_sync_autosuspend(priv->dev);
258c6a97c42SDaniel Thompson 
259c6a97c42SDaniel Thompson 	return retval || !wait ? retval : -EIO;
260c6a97c42SDaniel Thompson }
261c6a97c42SDaniel Thompson 
stm32_rng_clock_freq_restrain(struct hwrng * rng)26228d13f3fSGatien Chevallier static uint stm32_rng_clock_freq_restrain(struct hwrng *rng)
26328d13f3fSGatien Chevallier {
26428d13f3fSGatien Chevallier 	struct stm32_rng_private *priv =
26528d13f3fSGatien Chevallier 	    container_of(rng, struct stm32_rng_private, rng);
26628d13f3fSGatien Chevallier 	unsigned long clock_rate = 0;
26728d13f3fSGatien Chevallier 	uint clock_div = 0;
26828d13f3fSGatien Chevallier 
26928d13f3fSGatien Chevallier 	clock_rate = clk_get_rate(priv->clk);
27028d13f3fSGatien Chevallier 
27128d13f3fSGatien Chevallier 	/*
27228d13f3fSGatien Chevallier 	 * Get the exponent to apply on the CLKDIV field in RNG_CR register
27328d13f3fSGatien Chevallier 	 * No need to handle the case when clock-div > 0xF as it is physically
27428d13f3fSGatien Chevallier 	 * impossible
27528d13f3fSGatien Chevallier 	 */
27628d13f3fSGatien Chevallier 	while ((clock_rate >> clock_div) > priv->data->max_clock_rate)
27728d13f3fSGatien Chevallier 		clock_div++;
27828d13f3fSGatien Chevallier 
27928d13f3fSGatien Chevallier 	pr_debug("RNG clk rate : %lu\n", clk_get_rate(priv->clk) >> clock_div);
28028d13f3fSGatien Chevallier 
28128d13f3fSGatien Chevallier 	return clock_div;
28228d13f3fSGatien Chevallier }
28328d13f3fSGatien Chevallier 
stm32_rng_init(struct hwrng * rng)284c6a97c42SDaniel Thompson static int stm32_rng_init(struct hwrng *rng)
285c6a97c42SDaniel Thompson {
286c6a97c42SDaniel Thompson 	struct stm32_rng_private *priv =
287c6a97c42SDaniel Thompson 	    container_of(rng, struct stm32_rng_private, rng);
288c6a97c42SDaniel Thompson 	int err;
2896b85a7e1SGatien Chevallier 	u32 reg;
290c6a97c42SDaniel Thompson 
291c6a97c42SDaniel Thompson 	err = clk_prepare_enable(priv->clk);
292c6a97c42SDaniel Thompson 	if (err)
293c6a97c42SDaniel Thompson 		return err;
294c6a97c42SDaniel Thompson 
295c6a97c42SDaniel Thompson 	/* clear error indicators */
296c6a97c42SDaniel Thompson 	writel_relaxed(0, priv->base + RNG_SR);
297c6a97c42SDaniel Thompson 
2986b85a7e1SGatien Chevallier 	reg = readl_relaxed(priv->base + RNG_CR);
2996b85a7e1SGatien Chevallier 
3006b85a7e1SGatien Chevallier 	/*
3016b85a7e1SGatien Chevallier 	 * Keep default RNG configuration if none was specified.
3026b85a7e1SGatien Chevallier 	 * 0 is an invalid value as it disables all entropy sources.
3036b85a7e1SGatien Chevallier 	 */
3046b85a7e1SGatien Chevallier 	if (priv->data->has_cond_reset && priv->data->cr) {
30528d13f3fSGatien Chevallier 		uint clock_div = stm32_rng_clock_freq_restrain(rng);
30628d13f3fSGatien Chevallier 
3076b85a7e1SGatien Chevallier 		reg &= ~RNG_CR_CONFIG_MASK;
30828d13f3fSGatien Chevallier 		reg |= RNG_CR_CONDRST | (priv->data->cr & RNG_CR_ENTROPY_SRC_MASK) |
30928d13f3fSGatien Chevallier 		       (clock_div << RNG_CR_CLKDIV_SHIFT);
3106b85a7e1SGatien Chevallier 		if (priv->ced)
3116b85a7e1SGatien Chevallier 			reg &= ~RNG_CR_CED;
3126b85a7e1SGatien Chevallier 		else
3136b85a7e1SGatien Chevallier 			reg |= RNG_CR_CED;
3146b85a7e1SGatien Chevallier 		writel_relaxed(reg, priv->base + RNG_CR);
3156b85a7e1SGatien Chevallier 
3166b85a7e1SGatien Chevallier 		/* Health tests and noise control registers */
3176b85a7e1SGatien Chevallier 		writel_relaxed(priv->data->htcr, priv->base + RNG_HTCR);
3186b85a7e1SGatien Chevallier 		writel_relaxed(priv->data->nscr & RNG_NSCR_MASK, priv->base + RNG_NSCR);
3196b85a7e1SGatien Chevallier 
3206b85a7e1SGatien Chevallier 		reg &= ~RNG_CR_CONDRST;
3216b85a7e1SGatien Chevallier 		reg |= RNG_CR_RNGEN;
322a1b03e7aSGatien Chevallier 		if (priv->lock_conf)
323a1b03e7aSGatien Chevallier 			reg |= RNG_CR_CONFLOCK;
324a1b03e7aSGatien Chevallier 
3256b85a7e1SGatien Chevallier 		writel_relaxed(reg, priv->base + RNG_CR);
3266b85a7e1SGatien Chevallier 
3276b85a7e1SGatien Chevallier 		err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
3286b85a7e1SGatien Chevallier 							(!(reg & RNG_CR_CONDRST)),
3296b85a7e1SGatien Chevallier 							10, 50000);
3306b85a7e1SGatien Chevallier 		if (err) {
331429fec81SYang Yingliang 			clk_disable_unprepare(priv->clk);
332771c7faaSMarek Vasut 			dev_err(priv->dev, "%s: timeout %x!\n", __func__, reg);
3336b85a7e1SGatien Chevallier 			return -EINVAL;
3346b85a7e1SGatien Chevallier 		}
3356b85a7e1SGatien Chevallier 	} else {
3366b85a7e1SGatien Chevallier 		/* Handle all RNG versions by checking if conditional reset should be set */
3376b85a7e1SGatien Chevallier 		if (priv->data->has_cond_reset)
3386b85a7e1SGatien Chevallier 			reg |= RNG_CR_CONDRST;
3396b85a7e1SGatien Chevallier 
3406b85a7e1SGatien Chevallier 		if (priv->ced)
3416b85a7e1SGatien Chevallier 			reg &= ~RNG_CR_CED;
3426b85a7e1SGatien Chevallier 		else
3436b85a7e1SGatien Chevallier 			reg |= RNG_CR_CED;
3446b85a7e1SGatien Chevallier 
3456b85a7e1SGatien Chevallier 		writel_relaxed(reg, priv->base + RNG_CR);
3466b85a7e1SGatien Chevallier 
3476b85a7e1SGatien Chevallier 		if (priv->data->has_cond_reset)
3486b85a7e1SGatien Chevallier 			reg &= ~RNG_CR_CONDRST;
3496b85a7e1SGatien Chevallier 
3506b85a7e1SGatien Chevallier 		reg |= RNG_CR_RNGEN;
3516b85a7e1SGatien Chevallier 
3526b85a7e1SGatien Chevallier 		writel_relaxed(reg, priv->base + RNG_CR);
3536b85a7e1SGatien Chevallier 	}
3546b85a7e1SGatien Chevallier 
3556b85a7e1SGatien Chevallier 	err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg,
3566b85a7e1SGatien Chevallier 						reg & RNG_SR_DRDY,
3576b85a7e1SGatien Chevallier 						10, 100000);
35831b57788SMarek Vasut 	if (err || (reg & ~RNG_SR_DRDY)) {
3596b85a7e1SGatien Chevallier 		clk_disable_unprepare(priv->clk);
360771c7faaSMarek Vasut 		dev_err(priv->dev, "%s: timeout:%x SR: %x!\n", __func__, err, reg);
3616b85a7e1SGatien Chevallier 		return -EINVAL;
3626b85a7e1SGatien Chevallier 	}
3636b85a7e1SGatien Chevallier 
364c819d7b8SMarek Vasut 	clk_disable_unprepare(priv->clk);
365c819d7b8SMarek Vasut 
366c6a97c42SDaniel Thompson 	return 0;
367c6a97c42SDaniel Thompson }
368c6a97c42SDaniel Thompson 
stm32_rng_remove(struct platform_device * ofdev)369541b0719SUwe Kleine-König static void stm32_rng_remove(struct platform_device *ofdev)
370c6a97c42SDaniel Thompson {
3716b85a7e1SGatien Chevallier 	pm_runtime_disable(&ofdev->dev);
372c6a97c42SDaniel Thompson }
373c6a97c42SDaniel Thompson 
stm32_rng_runtime_suspend(struct device * dev)374ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_runtime_suspend(struct device *dev)
3756b85a7e1SGatien Chevallier {
3766b85a7e1SGatien Chevallier 	struct stm32_rng_private *priv = dev_get_drvdata(dev);
377ff4e4610SGatien Chevallier 	u32 reg;
3786b85a7e1SGatien Chevallier 
3796b85a7e1SGatien Chevallier 	reg = readl_relaxed(priv->base + RNG_CR);
3806b85a7e1SGatien Chevallier 	reg &= ~RNG_CR_RNGEN;
3816b85a7e1SGatien Chevallier 	writel_relaxed(reg, priv->base + RNG_CR);
3826b85a7e1SGatien Chevallier 	clk_disable_unprepare(priv->clk);
3836b85a7e1SGatien Chevallier 
3846b85a7e1SGatien Chevallier 	return 0;
3856b85a7e1SGatien Chevallier }
3866b85a7e1SGatien Chevallier 
stm32_rng_suspend(struct device * dev)387ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_suspend(struct device *dev)
3886b85a7e1SGatien Chevallier {
3896b85a7e1SGatien Chevallier 	struct stm32_rng_private *priv = dev_get_drvdata(dev);
390c819d7b8SMarek Vasut 	int err;
391c819d7b8SMarek Vasut 
392c819d7b8SMarek Vasut 	err = clk_prepare_enable(priv->clk);
393c819d7b8SMarek Vasut 	if (err)
394c819d7b8SMarek Vasut 		return err;
3956b85a7e1SGatien Chevallier 
396ff4e4610SGatien Chevallier 	if (priv->data->has_cond_reset) {
397ff4e4610SGatien Chevallier 		priv->pm_conf.nscr = readl_relaxed(priv->base + RNG_NSCR);
398ff4e4610SGatien Chevallier 		priv->pm_conf.htcr = readl_relaxed(priv->base + RNG_HTCR);
399ff4e4610SGatien Chevallier 	}
400ff4e4610SGatien Chevallier 
401ff4e4610SGatien Chevallier 	/* Do not save that RNG is enabled as it will be handled at resume */
402ff4e4610SGatien Chevallier 	priv->pm_conf.cr = readl_relaxed(priv->base + RNG_CR) & ~RNG_CR_RNGEN;
403ff4e4610SGatien Chevallier 
404ff4e4610SGatien Chevallier 	writel_relaxed(priv->pm_conf.cr, priv->base + RNG_CR);
405ff4e4610SGatien Chevallier 
406ff4e4610SGatien Chevallier 	clk_disable_unprepare(priv->clk);
407ff4e4610SGatien Chevallier 
408ff4e4610SGatien Chevallier 	return 0;
409ff4e4610SGatien Chevallier }
410ff4e4610SGatien Chevallier 
stm32_rng_runtime_resume(struct device * dev)411ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_runtime_resume(struct device *dev)
412ff4e4610SGatien Chevallier {
413ff4e4610SGatien Chevallier 	struct stm32_rng_private *priv = dev_get_drvdata(dev);
414ff4e4610SGatien Chevallier 	int err;
415ff4e4610SGatien Chevallier 	u32 reg;
416ff4e4610SGatien Chevallier 
417ff4e4610SGatien Chevallier 	err = clk_prepare_enable(priv->clk);
418ff4e4610SGatien Chevallier 	if (err)
419ff4e4610SGatien Chevallier 		return err;
420ff4e4610SGatien Chevallier 
421ff4e4610SGatien Chevallier 	/* Clean error indications */
422ff4e4610SGatien Chevallier 	writel_relaxed(0, priv->base + RNG_SR);
423ff4e4610SGatien Chevallier 
4246b85a7e1SGatien Chevallier 	reg = readl_relaxed(priv->base + RNG_CR);
4256b85a7e1SGatien Chevallier 	reg |= RNG_CR_RNGEN;
4266b85a7e1SGatien Chevallier 	writel_relaxed(reg, priv->base + RNG_CR);
4276b85a7e1SGatien Chevallier 
4286b85a7e1SGatien Chevallier 	return 0;
4296b85a7e1SGatien Chevallier }
4306b85a7e1SGatien Chevallier 
stm32_rng_resume(struct device * dev)431ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_resume(struct device *dev)
432ff4e4610SGatien Chevallier {
433ff4e4610SGatien Chevallier 	struct stm32_rng_private *priv = dev_get_drvdata(dev);
434ff4e4610SGatien Chevallier 	int err;
435ff4e4610SGatien Chevallier 	u32 reg;
436ff4e4610SGatien Chevallier 
437ff4e4610SGatien Chevallier 	err = clk_prepare_enable(priv->clk);
438ff4e4610SGatien Chevallier 	if (err)
439ff4e4610SGatien Chevallier 		return err;
440ff4e4610SGatien Chevallier 
441ff4e4610SGatien Chevallier 	/* Clean error indications */
442ff4e4610SGatien Chevallier 	writel_relaxed(0, priv->base + RNG_SR);
443ff4e4610SGatien Chevallier 
444ff4e4610SGatien Chevallier 	if (priv->data->has_cond_reset) {
445ff4e4610SGatien Chevallier 		/*
446ff4e4610SGatien Chevallier 		 * Correct configuration in bits [29:4] must be set in the same
447ff4e4610SGatien Chevallier 		 * access that set RNG_CR_CONDRST bit. Else config setting is
448ff4e4610SGatien Chevallier 		 * not taken into account. CONFIGLOCK bit must also be unset but
449ff4e4610SGatien Chevallier 		 * it is not handled at the moment.
450ff4e4610SGatien Chevallier 		 */
451ff4e4610SGatien Chevallier 		writel_relaxed(priv->pm_conf.cr | RNG_CR_CONDRST, priv->base + RNG_CR);
452ff4e4610SGatien Chevallier 
453ff4e4610SGatien Chevallier 		writel_relaxed(priv->pm_conf.nscr, priv->base + RNG_NSCR);
454ff4e4610SGatien Chevallier 		writel_relaxed(priv->pm_conf.htcr, priv->base + RNG_HTCR);
455ff4e4610SGatien Chevallier 
456ff4e4610SGatien Chevallier 		reg = readl_relaxed(priv->base + RNG_CR);
457ff4e4610SGatien Chevallier 		reg |= RNG_CR_RNGEN;
458ff4e4610SGatien Chevallier 		reg &= ~RNG_CR_CONDRST;
459ff4e4610SGatien Chevallier 		writel_relaxed(reg, priv->base + RNG_CR);
460ff4e4610SGatien Chevallier 
461ff4e4610SGatien Chevallier 		err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
462ff4e4610SGatien Chevallier 							reg & ~RNG_CR_CONDRST, 10, 100000);
463ff4e4610SGatien Chevallier 
464ff4e4610SGatien Chevallier 		if (err) {
465ff4e4610SGatien Chevallier 			clk_disable_unprepare(priv->clk);
466771c7faaSMarek Vasut 			dev_err(priv->dev, "%s: timeout:%x CR: %x!\n", __func__, err, reg);
467ff4e4610SGatien Chevallier 			return -EINVAL;
468ff4e4610SGatien Chevallier 		}
469ff4e4610SGatien Chevallier 	} else {
470ff4e4610SGatien Chevallier 		reg = priv->pm_conf.cr;
471ff4e4610SGatien Chevallier 		reg |= RNG_CR_RNGEN;
472ff4e4610SGatien Chevallier 		writel_relaxed(reg, priv->base + RNG_CR);
473ff4e4610SGatien Chevallier 	}
474ff4e4610SGatien Chevallier 
475c819d7b8SMarek Vasut 	clk_disable_unprepare(priv->clk);
476c819d7b8SMarek Vasut 
477ff4e4610SGatien Chevallier 	return 0;
478ff4e4610SGatien Chevallier }
479ff4e4610SGatien Chevallier 
480ff4e4610SGatien Chevallier static const struct dev_pm_ops __maybe_unused stm32_rng_pm_ops = {
4816b85a7e1SGatien Chevallier 	SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
4826b85a7e1SGatien Chevallier 			   stm32_rng_runtime_resume, NULL)
483ff4e4610SGatien Chevallier 	SET_SYSTEM_SLEEP_PM_OPS(stm32_rng_suspend,
484ff4e4610SGatien Chevallier 				stm32_rng_resume)
4856b85a7e1SGatien Chevallier };
4866b85a7e1SGatien Chevallier 
4876b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32mp13_rng_data = {
4886b85a7e1SGatien Chevallier 	.has_cond_reset = true,
48928d13f3fSGatien Chevallier 	.max_clock_rate = 48000000,
4906b85a7e1SGatien Chevallier 	.cr = 0x00F00D00,
4916b85a7e1SGatien Chevallier 	.nscr = 0x2B5BB,
4926b85a7e1SGatien Chevallier 	.htcr = 0x969D,
4936b85a7e1SGatien Chevallier };
4946b85a7e1SGatien Chevallier 
4956b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32_rng_data = {
4966b85a7e1SGatien Chevallier 	.has_cond_reset = false,
49728d13f3fSGatien Chevallier 	.max_clock_rate = 3000000,
4986b85a7e1SGatien Chevallier };
4996b85a7e1SGatien Chevallier 
5006b85a7e1SGatien Chevallier static const struct of_device_id stm32_rng_match[] = {
5016b85a7e1SGatien Chevallier 	{
5026b85a7e1SGatien Chevallier 		.compatible = "st,stm32mp13-rng",
5036b85a7e1SGatien Chevallier 		.data = &stm32mp13_rng_data,
5046b85a7e1SGatien Chevallier 	},
5056b85a7e1SGatien Chevallier 	{
5066b85a7e1SGatien Chevallier 		.compatible = "st,stm32-rng",
5076b85a7e1SGatien Chevallier 		.data = &stm32_rng_data,
5086b85a7e1SGatien Chevallier 	},
5096b85a7e1SGatien Chevallier 	{},
5106b85a7e1SGatien Chevallier };
5116b85a7e1SGatien Chevallier MODULE_DEVICE_TABLE(of, stm32_rng_match);
5126b85a7e1SGatien Chevallier 
stm32_rng_probe(struct platform_device * ofdev)513c6a97c42SDaniel Thompson static int stm32_rng_probe(struct platform_device *ofdev)
514c6a97c42SDaniel Thompson {
515c6a97c42SDaniel Thompson 	struct device *dev = &ofdev->dev;
516c6a97c42SDaniel Thompson 	struct device_node *np = ofdev->dev.of_node;
517c6a97c42SDaniel Thompson 	struct stm32_rng_private *priv;
51818d9a826SGatien Chevallier 	struct resource *res;
519c6a97c42SDaniel Thompson 
520*4c6338f8SMarek Vasut 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
521c6a97c42SDaniel Thompson 	if (!priv)
522c6a97c42SDaniel Thompson 		return -ENOMEM;
523c6a97c42SDaniel Thompson 
52418d9a826SGatien Chevallier 	priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res);
525c6a97c42SDaniel Thompson 	if (IS_ERR(priv->base))
526c6a97c42SDaniel Thompson 		return PTR_ERR(priv->base);
527c6a97c42SDaniel Thompson 
528c6a97c42SDaniel Thompson 	priv->clk = devm_clk_get(&ofdev->dev, NULL);
529c6a97c42SDaniel Thompson 	if (IS_ERR(priv->clk))
530c6a97c42SDaniel Thompson 		return PTR_ERR(priv->clk);
531c6a97c42SDaniel Thompson 
532326ed382Slionel.debieve@st.com 	priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
533326ed382Slionel.debieve@st.com 	if (!IS_ERR(priv->rst)) {
534326ed382Slionel.debieve@st.com 		reset_control_assert(priv->rst);
535326ed382Slionel.debieve@st.com 		udelay(2);
536326ed382Slionel.debieve@st.com 		reset_control_deassert(priv->rst);
537326ed382Slionel.debieve@st.com 	}
538326ed382Slionel.debieve@st.com 
539529571edSlionel.debieve@st.com 	priv->ced = of_property_read_bool(np, "clock-error-detect");
540a1b03e7aSGatien Chevallier 	priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf");
541771c7faaSMarek Vasut 	priv->dev = dev;
542529571edSlionel.debieve@st.com 
5436b85a7e1SGatien Chevallier 	priv->data = of_device_get_match_data(dev);
5446b85a7e1SGatien Chevallier 	if (!priv->data)
5456b85a7e1SGatien Chevallier 		return -ENODEV;
5466b85a7e1SGatien Chevallier 
547c6a97c42SDaniel Thompson 	dev_set_drvdata(dev, priv);
548c6a97c42SDaniel Thompson 
549436cdcdeSJulia Lawall 	priv->rng.name = dev_driver_string(dev);
550436cdcdeSJulia Lawall 	priv->rng.init = stm32_rng_init;
551436cdcdeSJulia Lawall 	priv->rng.read = stm32_rng_read;
55238a1965fSLionel Debieve 	priv->rng.quality = 900;
553c6a97c42SDaniel Thompson 
554c6a97c42SDaniel Thompson 	pm_runtime_set_autosuspend_delay(dev, 100);
555c6a97c42SDaniel Thompson 	pm_runtime_use_autosuspend(dev);
556c6a97c42SDaniel Thompson 	pm_runtime_enable(dev);
557c6a97c42SDaniel Thompson 
558c6a97c42SDaniel Thompson 	return devm_hwrng_register(dev, &priv->rng);
559c6a97c42SDaniel Thompson }
560c6a97c42SDaniel Thompson 
561c6a97c42SDaniel Thompson static struct platform_driver stm32_rng_driver = {
562c6a97c42SDaniel Thompson 	.driver = {
563c6a97c42SDaniel Thompson 		.name = "stm32-rng",
564ff4e4610SGatien Chevallier 		.pm = pm_ptr(&stm32_rng_pm_ops),
565c6a97c42SDaniel Thompson 		.of_match_table = stm32_rng_match,
566c6a97c42SDaniel Thompson 	},
567c6a97c42SDaniel Thompson 	.probe = stm32_rng_probe,
568541b0719SUwe Kleine-König 	.remove_new = stm32_rng_remove,
569c6a97c42SDaniel Thompson };
570c6a97c42SDaniel Thompson 
571c6a97c42SDaniel Thompson module_platform_driver(stm32_rng_driver);
572c6a97c42SDaniel Thompson 
573c6a97c42SDaniel Thompson MODULE_LICENSE("GPL");
574c6a97c42SDaniel Thompson MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
575c6a97c42SDaniel Thompson MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");
576