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 58*ff4e4610SGatien Chevallier /** 59*ff4e4610SGatien Chevallier * struct stm32_rng_config - RNG configuration data 60*ff4e4610SGatien Chevallier * 61*ff4e4610SGatien Chevallier * @cr: RNG configuration. 0 means default hardware RNG configuration 62*ff4e4610SGatien Chevallier * @nscr: Noise sources control configuration. 63*ff4e4610SGatien Chevallier * @htcr: Health tests configuration. 64*ff4e4610SGatien Chevallier */ 65*ff4e4610SGatien Chevallier struct stm32_rng_config { 66*ff4e4610SGatien Chevallier u32 cr; 67*ff4e4610SGatien Chevallier u32 nscr; 68*ff4e4610SGatien Chevallier u32 htcr; 69*ff4e4610SGatien Chevallier }; 70*ff4e4610SGatien Chevallier 71c6a97c42SDaniel Thompson struct stm32_rng_private { 72c6a97c42SDaniel Thompson struct hwrng rng; 73c6a97c42SDaniel Thompson void __iomem *base; 74c6a97c42SDaniel Thompson struct clk *clk; 75326ed382Slionel.debieve@st.com struct reset_control *rst; 76*ff4e4610SGatien Chevallier struct stm32_rng_config pm_conf; 776b85a7e1SGatien Chevallier const struct stm32_rng_data *data; 78529571edSlionel.debieve@st.com bool ced; 79a1b03e7aSGatien Chevallier bool lock_conf; 80c6a97c42SDaniel Thompson }; 81c6a97c42SDaniel Thompson 828f1c5227SGatien Chevallier /* 838f1c5227SGatien Chevallier * Extracts from the STM32 RNG specification when RNG supports CONDRST. 848f1c5227SGatien Chevallier * 858f1c5227SGatien Chevallier * When a noise source (or seed) error occurs, the RNG stops generating 868f1c5227SGatien Chevallier * random numbers and sets to “1” both SEIS and SECS bits to indicate 878f1c5227SGatien Chevallier * that a seed error occurred. (...) 888f1c5227SGatien Chevallier * 898f1c5227SGatien Chevallier * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield 908f1c5227SGatien Chevallier * description for details). This step is needed only if SECS is set. 918f1c5227SGatien Chevallier * Indeed, when SEIS is set and SECS is cleared it means RNG performed 928f1c5227SGatien Chevallier * the reset automatically (auto-reset). 938f1c5227SGatien Chevallier * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST 948f1c5227SGatien Chevallier * to be cleared in the RNG_CR register, then confirm that SEIS is 958f1c5227SGatien Chevallier * cleared in the RNG_SR register. Otherwise just clear SEIS bit in 968f1c5227SGatien Chevallier * the RNG_SR register. 978f1c5227SGatien Chevallier * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be 988f1c5227SGatien Chevallier * cleared by RNG. The random number generation is now back to normal. 998f1c5227SGatien Chevallier */ 1008f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv) 1018f1c5227SGatien Chevallier { 1028f1c5227SGatien Chevallier struct device *dev = (struct device *)priv->rng.priv; 1038f1c5227SGatien Chevallier u32 sr = readl_relaxed(priv->base + RNG_SR); 1048f1c5227SGatien Chevallier u32 cr = readl_relaxed(priv->base + RNG_CR); 1058f1c5227SGatien Chevallier int err; 1068f1c5227SGatien Chevallier 1078f1c5227SGatien Chevallier if (sr & RNG_SR_SECS) { 1088f1c5227SGatien Chevallier /* Conceal by resetting the subsystem (step 1.) */ 1098f1c5227SGatien Chevallier writel_relaxed(cr | RNG_CR_CONDRST, priv->base + RNG_CR); 1108f1c5227SGatien Chevallier writel_relaxed(cr & ~RNG_CR_CONDRST, priv->base + RNG_CR); 1118f1c5227SGatien Chevallier } else { 1128f1c5227SGatien Chevallier /* RNG auto-reset (step 2.) */ 1138f1c5227SGatien Chevallier writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR); 1148f1c5227SGatien Chevallier goto end; 1158f1c5227SGatien Chevallier } 1168f1c5227SGatien Chevallier 1178f1c5227SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, cr, !(cr & RNG_CR_CONDRST), 10, 1188f1c5227SGatien Chevallier 100000); 1198f1c5227SGatien Chevallier if (err) { 1208f1c5227SGatien Chevallier dev_err(dev, "%s: timeout %x\n", __func__, sr); 1218f1c5227SGatien Chevallier return err; 1228f1c5227SGatien Chevallier } 1238f1c5227SGatien Chevallier 1248f1c5227SGatien Chevallier /* Check SEIS is cleared (step 2.) */ 1258f1c5227SGatien Chevallier if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS) 1268f1c5227SGatien Chevallier return -EINVAL; 1278f1c5227SGatien Chevallier 1288f1c5227SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 10, 1298f1c5227SGatien Chevallier 100000); 1308f1c5227SGatien Chevallier if (err) { 1318f1c5227SGatien Chevallier dev_err(dev, "%s: timeout %x\n", __func__, sr); 1328f1c5227SGatien Chevallier return err; 1338f1c5227SGatien Chevallier } 1348f1c5227SGatien Chevallier 1358f1c5227SGatien Chevallier end: 1368f1c5227SGatien Chevallier return 0; 1378f1c5227SGatien Chevallier } 1388f1c5227SGatien Chevallier 1398f1c5227SGatien Chevallier /* 1408f1c5227SGatien Chevallier * Extracts from the STM32 RNG specification, when CONDRST is not supported 1418f1c5227SGatien Chevallier * 1428f1c5227SGatien Chevallier * When a noise source (or seed) error occurs, the RNG stops generating 1438f1c5227SGatien Chevallier * random numbers and sets to “1” both SEIS and SECS bits to indicate 1448f1c5227SGatien Chevallier * that a seed error occurred. (...) 1458f1c5227SGatien Chevallier * 1468f1c5227SGatien Chevallier * The following sequence shall be used to fully recover from a seed 1478f1c5227SGatien Chevallier * error after the RNG initialization: 1488f1c5227SGatien Chevallier * 1. Clear the SEIS bit by writing it to “0”. 1498f1c5227SGatien Chevallier * 2. Read out 12 words from the RNG_DR register, and discard each of 1508f1c5227SGatien Chevallier * them in order to clean the pipeline. 1518f1c5227SGatien Chevallier * 3. Confirm that SEIS is still cleared. Random number generation is 1528f1c5227SGatien Chevallier * back to normal. 1538f1c5227SGatien Chevallier */ 1548f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_private *priv) 1558f1c5227SGatien Chevallier { 1568f1c5227SGatien Chevallier unsigned int i = 0; 1578f1c5227SGatien Chevallier u32 sr = readl_relaxed(priv->base + RNG_SR); 1588f1c5227SGatien Chevallier 1598f1c5227SGatien Chevallier writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR); 1608f1c5227SGatien Chevallier 1618f1c5227SGatien Chevallier for (i = 12; i != 0; i--) 1628f1c5227SGatien Chevallier (void)readl_relaxed(priv->base + RNG_DR); 1638f1c5227SGatien Chevallier 1648f1c5227SGatien Chevallier if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS) 1658f1c5227SGatien Chevallier return -EINVAL; 1668f1c5227SGatien Chevallier 1678f1c5227SGatien Chevallier return 0; 1688f1c5227SGatien Chevallier } 1698f1c5227SGatien Chevallier 1708f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error(struct hwrng *rng) 1718f1c5227SGatien Chevallier { 1728f1c5227SGatien Chevallier struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng); 1738f1c5227SGatien Chevallier 1748f1c5227SGatien Chevallier dev_dbg((struct device *)priv->rng.priv, "Concealing seed error\n"); 1758f1c5227SGatien Chevallier 1768f1c5227SGatien Chevallier if (priv->data->has_cond_reset) 1778f1c5227SGatien Chevallier return stm32_rng_conceal_seed_error_cond_reset(priv); 1788f1c5227SGatien Chevallier else 1798f1c5227SGatien Chevallier return stm32_rng_conceal_seed_error_sw_reset(priv); 1808f1c5227SGatien Chevallier }; 1818f1c5227SGatien Chevallier 1828f1c5227SGatien Chevallier 183c6a97c42SDaniel Thompson static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 184c6a97c42SDaniel Thompson { 185b17bc6ebSGatien Chevallier struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng); 186b17bc6ebSGatien Chevallier unsigned int i = 0; 187b17bc6ebSGatien Chevallier int retval = 0, err = 0; 188c6a97c42SDaniel Thompson u32 sr; 189c6a97c42SDaniel Thompson 190c6a97c42SDaniel Thompson pm_runtime_get_sync((struct device *) priv->rng.priv); 191c6a97c42SDaniel Thompson 1928f1c5227SGatien Chevallier if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS) 1938f1c5227SGatien Chevallier stm32_rng_conceal_seed_error(rng); 1948f1c5227SGatien Chevallier 195e64f57e8STomas Marek while (max >= sizeof(u32)) { 196c6a97c42SDaniel Thompson sr = readl_relaxed(priv->base + RNG_SR); 197b17bc6ebSGatien Chevallier /* 198b17bc6ebSGatien Chevallier * Manage timeout which is based on timer and take 199b17bc6ebSGatien Chevallier * care of initial delay time when enabling the RNG. 200b17bc6ebSGatien Chevallier */ 201c6a97c42SDaniel Thompson if (!sr && wait) { 2027cdc5e6bSTomas Marek err = readl_relaxed_poll_timeout_atomic(priv->base 203279f4f8fSlionel.debieve@st.com + RNG_SR, 204279f4f8fSlionel.debieve@st.com sr, sr, 205279f4f8fSlionel.debieve@st.com 10, 50000); 206b17bc6ebSGatien Chevallier if (err) { 207279f4f8fSlionel.debieve@st.com dev_err((struct device *)priv->rng.priv, 208279f4f8fSlionel.debieve@st.com "%s: timeout %x!\n", __func__, sr); 209b17bc6ebSGatien Chevallier break; 210c6a97c42SDaniel Thompson } 211b17bc6ebSGatien Chevallier } else if (!sr) { 212b17bc6ebSGatien Chevallier /* The FIFO is being filled up */ 213c6a97c42SDaniel Thompson break; 2141ff69adfSMaxime Coquelin } 215c6a97c42SDaniel Thompson 216b17bc6ebSGatien Chevallier if (sr != RNG_SR_DRDY) { 217b17bc6ebSGatien Chevallier if (sr & RNG_SR_SEIS) { 218b17bc6ebSGatien Chevallier err = stm32_rng_conceal_seed_error(rng); 219b17bc6ebSGatien Chevallier i++; 220b17bc6ebSGatien Chevallier if (err && i > RNG_NB_RECOVER_TRIES) { 221b17bc6ebSGatien Chevallier dev_err((struct device *)priv->rng.priv, 222b17bc6ebSGatien Chevallier "Couldn't recover from seed error\n"); 223b17bc6ebSGatien Chevallier return -ENOTRECOVERABLE; 224b17bc6ebSGatien Chevallier } 225c6a97c42SDaniel Thompson 226b17bc6ebSGatien Chevallier continue; 227b17bc6ebSGatien Chevallier } 228b17bc6ebSGatien Chevallier 229b17bc6ebSGatien Chevallier if (WARN_ONCE((sr & RNG_SR_CEIS), "RNG clock too slow - %x\n", sr)) 230b17bc6ebSGatien Chevallier writel_relaxed(0, priv->base + RNG_SR); 231b17bc6ebSGatien Chevallier } 232b17bc6ebSGatien Chevallier 233b17bc6ebSGatien Chevallier /* Late seed error case: DR being 0 is an error status */ 234b17bc6ebSGatien Chevallier *(u32 *)data = readl_relaxed(priv->base + RNG_DR); 235b17bc6ebSGatien Chevallier if (!*(u32 *)data) { 236b17bc6ebSGatien Chevallier err = stm32_rng_conceal_seed_error(rng); 237b17bc6ebSGatien Chevallier i++; 238b17bc6ebSGatien Chevallier if (err && i > RNG_NB_RECOVER_TRIES) { 239b17bc6ebSGatien Chevallier dev_err((struct device *)priv->rng.priv, 240b17bc6ebSGatien Chevallier "Couldn't recover from seed error"); 241b17bc6ebSGatien Chevallier return -ENOTRECOVERABLE; 242b17bc6ebSGatien Chevallier } 243b17bc6ebSGatien Chevallier 244b17bc6ebSGatien Chevallier continue; 245b17bc6ebSGatien Chevallier } 246b17bc6ebSGatien Chevallier 247b17bc6ebSGatien Chevallier i = 0; 248c6a97c42SDaniel Thompson retval += sizeof(u32); 249c6a97c42SDaniel Thompson data += sizeof(u32); 250c6a97c42SDaniel Thompson max -= sizeof(u32); 251c6a97c42SDaniel Thompson } 252c6a97c42SDaniel Thompson 253c6a97c42SDaniel Thompson pm_runtime_mark_last_busy((struct device *) priv->rng.priv); 254c6a97c42SDaniel Thompson pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv); 255c6a97c42SDaniel Thompson 256c6a97c42SDaniel Thompson return retval || !wait ? retval : -EIO; 257c6a97c42SDaniel Thompson } 258c6a97c42SDaniel Thompson 25928d13f3fSGatien Chevallier static uint stm32_rng_clock_freq_restrain(struct hwrng *rng) 26028d13f3fSGatien Chevallier { 26128d13f3fSGatien Chevallier struct stm32_rng_private *priv = 26228d13f3fSGatien Chevallier container_of(rng, struct stm32_rng_private, rng); 26328d13f3fSGatien Chevallier unsigned long clock_rate = 0; 26428d13f3fSGatien Chevallier uint clock_div = 0; 26528d13f3fSGatien Chevallier 26628d13f3fSGatien Chevallier clock_rate = clk_get_rate(priv->clk); 26728d13f3fSGatien Chevallier 26828d13f3fSGatien Chevallier /* 26928d13f3fSGatien Chevallier * Get the exponent to apply on the CLKDIV field in RNG_CR register 27028d13f3fSGatien Chevallier * No need to handle the case when clock-div > 0xF as it is physically 27128d13f3fSGatien Chevallier * impossible 27228d13f3fSGatien Chevallier */ 27328d13f3fSGatien Chevallier while ((clock_rate >> clock_div) > priv->data->max_clock_rate) 27428d13f3fSGatien Chevallier clock_div++; 27528d13f3fSGatien Chevallier 27628d13f3fSGatien Chevallier pr_debug("RNG clk rate : %lu\n", clk_get_rate(priv->clk) >> clock_div); 27728d13f3fSGatien Chevallier 27828d13f3fSGatien Chevallier return clock_div; 27928d13f3fSGatien Chevallier } 28028d13f3fSGatien Chevallier 281c6a97c42SDaniel Thompson static int stm32_rng_init(struct hwrng *rng) 282c6a97c42SDaniel Thompson { 283c6a97c42SDaniel Thompson struct stm32_rng_private *priv = 284c6a97c42SDaniel Thompson container_of(rng, struct stm32_rng_private, rng); 285c6a97c42SDaniel Thompson int err; 2866b85a7e1SGatien Chevallier u32 reg; 287c6a97c42SDaniel Thompson 288c6a97c42SDaniel Thompson err = clk_prepare_enable(priv->clk); 289c6a97c42SDaniel Thompson if (err) 290c6a97c42SDaniel Thompson return err; 291c6a97c42SDaniel Thompson 292c6a97c42SDaniel Thompson /* clear error indicators */ 293c6a97c42SDaniel Thompson writel_relaxed(0, priv->base + RNG_SR); 294c6a97c42SDaniel Thompson 2956b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 2966b85a7e1SGatien Chevallier 2976b85a7e1SGatien Chevallier /* 2986b85a7e1SGatien Chevallier * Keep default RNG configuration if none was specified. 2996b85a7e1SGatien Chevallier * 0 is an invalid value as it disables all entropy sources. 3006b85a7e1SGatien Chevallier */ 3016b85a7e1SGatien Chevallier if (priv->data->has_cond_reset && priv->data->cr) { 30228d13f3fSGatien Chevallier uint clock_div = stm32_rng_clock_freq_restrain(rng); 30328d13f3fSGatien Chevallier 3046b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONFIG_MASK; 30528d13f3fSGatien Chevallier reg |= RNG_CR_CONDRST | (priv->data->cr & RNG_CR_ENTROPY_SRC_MASK) | 30628d13f3fSGatien Chevallier (clock_div << RNG_CR_CLKDIV_SHIFT); 3076b85a7e1SGatien Chevallier if (priv->ced) 3086b85a7e1SGatien Chevallier reg &= ~RNG_CR_CED; 3096b85a7e1SGatien Chevallier else 3106b85a7e1SGatien Chevallier reg |= RNG_CR_CED; 3116b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3126b85a7e1SGatien Chevallier 3136b85a7e1SGatien Chevallier /* Health tests and noise control registers */ 3146b85a7e1SGatien Chevallier writel_relaxed(priv->data->htcr, priv->base + RNG_HTCR); 3156b85a7e1SGatien Chevallier writel_relaxed(priv->data->nscr & RNG_NSCR_MASK, priv->base + RNG_NSCR); 3166b85a7e1SGatien Chevallier 3176b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONDRST; 3186b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 319a1b03e7aSGatien Chevallier if (priv->lock_conf) 320a1b03e7aSGatien Chevallier reg |= RNG_CR_CONFLOCK; 321a1b03e7aSGatien Chevallier 3226b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3236b85a7e1SGatien Chevallier 3246b85a7e1SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg, 3256b85a7e1SGatien Chevallier (!(reg & RNG_CR_CONDRST)), 3266b85a7e1SGatien Chevallier 10, 50000); 3276b85a7e1SGatien Chevallier if (err) { 3286b85a7e1SGatien Chevallier dev_err((struct device *)priv->rng.priv, 3296b85a7e1SGatien Chevallier "%s: timeout %x!\n", __func__, reg); 3306b85a7e1SGatien Chevallier return -EINVAL; 3316b85a7e1SGatien Chevallier } 3326b85a7e1SGatien Chevallier } else { 3336b85a7e1SGatien Chevallier /* Handle all RNG versions by checking if conditional reset should be set */ 3346b85a7e1SGatien Chevallier if (priv->data->has_cond_reset) 3356b85a7e1SGatien Chevallier reg |= RNG_CR_CONDRST; 3366b85a7e1SGatien Chevallier 3376b85a7e1SGatien Chevallier if (priv->ced) 3386b85a7e1SGatien Chevallier reg &= ~RNG_CR_CED; 3396b85a7e1SGatien Chevallier else 3406b85a7e1SGatien Chevallier reg |= RNG_CR_CED; 3416b85a7e1SGatien Chevallier 3426b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3436b85a7e1SGatien Chevallier 3446b85a7e1SGatien Chevallier if (priv->data->has_cond_reset) 3456b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONDRST; 3466b85a7e1SGatien Chevallier 3476b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 3486b85a7e1SGatien Chevallier 3496b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3506b85a7e1SGatien Chevallier } 3516b85a7e1SGatien Chevallier 3526b85a7e1SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg, 3536b85a7e1SGatien Chevallier reg & RNG_SR_DRDY, 3546b85a7e1SGatien Chevallier 10, 100000); 3556b85a7e1SGatien Chevallier if (err | (reg & ~RNG_SR_DRDY)) { 3566b85a7e1SGatien Chevallier clk_disable_unprepare(priv->clk); 3576b85a7e1SGatien Chevallier dev_err((struct device *)priv->rng.priv, 3586b85a7e1SGatien Chevallier "%s: timeout:%x SR: %x!\n", __func__, err, reg); 3596b85a7e1SGatien Chevallier return -EINVAL; 3606b85a7e1SGatien Chevallier } 3616b85a7e1SGatien Chevallier 362c6a97c42SDaniel Thompson return 0; 363c6a97c42SDaniel Thompson } 364c6a97c42SDaniel Thompson 3656b85a7e1SGatien Chevallier static int stm32_rng_remove(struct platform_device *ofdev) 366c6a97c42SDaniel Thompson { 3676b85a7e1SGatien Chevallier pm_runtime_disable(&ofdev->dev); 368c6a97c42SDaniel Thompson 3696b85a7e1SGatien Chevallier return 0; 370c6a97c42SDaniel Thompson } 371c6a97c42SDaniel Thompson 372*ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_runtime_suspend(struct device *dev) 3736b85a7e1SGatien Chevallier { 3746b85a7e1SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 375*ff4e4610SGatien Chevallier u32 reg; 3766b85a7e1SGatien Chevallier 3776b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 3786b85a7e1SGatien Chevallier reg &= ~RNG_CR_RNGEN; 3796b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3806b85a7e1SGatien Chevallier clk_disable_unprepare(priv->clk); 3816b85a7e1SGatien Chevallier 3826b85a7e1SGatien Chevallier return 0; 3836b85a7e1SGatien Chevallier } 3846b85a7e1SGatien Chevallier 385*ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_suspend(struct device *dev) 3866b85a7e1SGatien Chevallier { 3876b85a7e1SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 3886b85a7e1SGatien Chevallier 389*ff4e4610SGatien Chevallier if (priv->data->has_cond_reset) { 390*ff4e4610SGatien Chevallier priv->pm_conf.nscr = readl_relaxed(priv->base + RNG_NSCR); 391*ff4e4610SGatien Chevallier priv->pm_conf.htcr = readl_relaxed(priv->base + RNG_HTCR); 392*ff4e4610SGatien Chevallier } 393*ff4e4610SGatien Chevallier 394*ff4e4610SGatien Chevallier /* Do not save that RNG is enabled as it will be handled at resume */ 395*ff4e4610SGatien Chevallier priv->pm_conf.cr = readl_relaxed(priv->base + RNG_CR) & ~RNG_CR_RNGEN; 396*ff4e4610SGatien Chevallier 397*ff4e4610SGatien Chevallier writel_relaxed(priv->pm_conf.cr, priv->base + RNG_CR); 398*ff4e4610SGatien Chevallier 399*ff4e4610SGatien Chevallier clk_disable_unprepare(priv->clk); 400*ff4e4610SGatien Chevallier 401*ff4e4610SGatien Chevallier return 0; 402*ff4e4610SGatien Chevallier } 403*ff4e4610SGatien Chevallier 404*ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_runtime_resume(struct device *dev) 405*ff4e4610SGatien Chevallier { 406*ff4e4610SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 407*ff4e4610SGatien Chevallier int err; 408*ff4e4610SGatien Chevallier u32 reg; 409*ff4e4610SGatien Chevallier 410*ff4e4610SGatien Chevallier err = clk_prepare_enable(priv->clk); 411*ff4e4610SGatien Chevallier if (err) 412*ff4e4610SGatien Chevallier return err; 413*ff4e4610SGatien Chevallier 414*ff4e4610SGatien Chevallier /* Clean error indications */ 415*ff4e4610SGatien Chevallier writel_relaxed(0, priv->base + RNG_SR); 416*ff4e4610SGatien Chevallier 4176b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 4186b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 4196b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 4206b85a7e1SGatien Chevallier 4216b85a7e1SGatien Chevallier return 0; 4226b85a7e1SGatien Chevallier } 4236b85a7e1SGatien Chevallier 424*ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_resume(struct device *dev) 425*ff4e4610SGatien Chevallier { 426*ff4e4610SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 427*ff4e4610SGatien Chevallier int err; 428*ff4e4610SGatien Chevallier u32 reg; 429*ff4e4610SGatien Chevallier 430*ff4e4610SGatien Chevallier err = clk_prepare_enable(priv->clk); 431*ff4e4610SGatien Chevallier if (err) 432*ff4e4610SGatien Chevallier return err; 433*ff4e4610SGatien Chevallier 434*ff4e4610SGatien Chevallier /* Clean error indications */ 435*ff4e4610SGatien Chevallier writel_relaxed(0, priv->base + RNG_SR); 436*ff4e4610SGatien Chevallier 437*ff4e4610SGatien Chevallier if (priv->data->has_cond_reset) { 438*ff4e4610SGatien Chevallier /* 439*ff4e4610SGatien Chevallier * Correct configuration in bits [29:4] must be set in the same 440*ff4e4610SGatien Chevallier * access that set RNG_CR_CONDRST bit. Else config setting is 441*ff4e4610SGatien Chevallier * not taken into account. CONFIGLOCK bit must also be unset but 442*ff4e4610SGatien Chevallier * it is not handled at the moment. 443*ff4e4610SGatien Chevallier */ 444*ff4e4610SGatien Chevallier writel_relaxed(priv->pm_conf.cr | RNG_CR_CONDRST, priv->base + RNG_CR); 445*ff4e4610SGatien Chevallier 446*ff4e4610SGatien Chevallier writel_relaxed(priv->pm_conf.nscr, priv->base + RNG_NSCR); 447*ff4e4610SGatien Chevallier writel_relaxed(priv->pm_conf.htcr, priv->base + RNG_HTCR); 448*ff4e4610SGatien Chevallier 449*ff4e4610SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 450*ff4e4610SGatien Chevallier reg |= RNG_CR_RNGEN; 451*ff4e4610SGatien Chevallier reg &= ~RNG_CR_CONDRST; 452*ff4e4610SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 453*ff4e4610SGatien Chevallier 454*ff4e4610SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg, 455*ff4e4610SGatien Chevallier reg & ~RNG_CR_CONDRST, 10, 100000); 456*ff4e4610SGatien Chevallier 457*ff4e4610SGatien Chevallier if (err) { 458*ff4e4610SGatien Chevallier clk_disable_unprepare(priv->clk); 459*ff4e4610SGatien Chevallier dev_err((struct device *)priv->rng.priv, 460*ff4e4610SGatien Chevallier "%s: timeout:%x CR: %x!\n", __func__, err, reg); 461*ff4e4610SGatien Chevallier return -EINVAL; 462*ff4e4610SGatien Chevallier } 463*ff4e4610SGatien Chevallier } else { 464*ff4e4610SGatien Chevallier reg = priv->pm_conf.cr; 465*ff4e4610SGatien Chevallier reg |= RNG_CR_RNGEN; 466*ff4e4610SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 467*ff4e4610SGatien Chevallier } 468*ff4e4610SGatien Chevallier 469*ff4e4610SGatien Chevallier return 0; 470*ff4e4610SGatien Chevallier } 471*ff4e4610SGatien Chevallier 472*ff4e4610SGatien Chevallier static const struct dev_pm_ops __maybe_unused stm32_rng_pm_ops = { 4736b85a7e1SGatien Chevallier SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend, 4746b85a7e1SGatien Chevallier stm32_rng_runtime_resume, NULL) 475*ff4e4610SGatien Chevallier SET_SYSTEM_SLEEP_PM_OPS(stm32_rng_suspend, 476*ff4e4610SGatien Chevallier stm32_rng_resume) 4776b85a7e1SGatien Chevallier }; 4786b85a7e1SGatien Chevallier 4796b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32mp13_rng_data = { 4806b85a7e1SGatien Chevallier .has_cond_reset = true, 48128d13f3fSGatien Chevallier .max_clock_rate = 48000000, 4826b85a7e1SGatien Chevallier .cr = 0x00F00D00, 4836b85a7e1SGatien Chevallier .nscr = 0x2B5BB, 4846b85a7e1SGatien Chevallier .htcr = 0x969D, 4856b85a7e1SGatien Chevallier }; 4866b85a7e1SGatien Chevallier 4876b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32_rng_data = { 4886b85a7e1SGatien Chevallier .has_cond_reset = false, 48928d13f3fSGatien Chevallier .max_clock_rate = 3000000, 4906b85a7e1SGatien Chevallier }; 4916b85a7e1SGatien Chevallier 4926b85a7e1SGatien Chevallier static const struct of_device_id stm32_rng_match[] = { 4936b85a7e1SGatien Chevallier { 4946b85a7e1SGatien Chevallier .compatible = "st,stm32mp13-rng", 4956b85a7e1SGatien Chevallier .data = &stm32mp13_rng_data, 4966b85a7e1SGatien Chevallier }, 4976b85a7e1SGatien Chevallier { 4986b85a7e1SGatien Chevallier .compatible = "st,stm32-rng", 4996b85a7e1SGatien Chevallier .data = &stm32_rng_data, 5006b85a7e1SGatien Chevallier }, 5016b85a7e1SGatien Chevallier {}, 5026b85a7e1SGatien Chevallier }; 5036b85a7e1SGatien Chevallier MODULE_DEVICE_TABLE(of, stm32_rng_match); 5046b85a7e1SGatien Chevallier 505c6a97c42SDaniel Thompson static int stm32_rng_probe(struct platform_device *ofdev) 506c6a97c42SDaniel Thompson { 507c6a97c42SDaniel Thompson struct device *dev = &ofdev->dev; 508c6a97c42SDaniel Thompson struct device_node *np = ofdev->dev.of_node; 509c6a97c42SDaniel Thompson struct stm32_rng_private *priv; 51018d9a826SGatien Chevallier struct resource *res; 511c6a97c42SDaniel Thompson 512c6a97c42SDaniel Thompson priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL); 513c6a97c42SDaniel Thompson if (!priv) 514c6a97c42SDaniel Thompson return -ENOMEM; 515c6a97c42SDaniel Thompson 51618d9a826SGatien Chevallier priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res); 517c6a97c42SDaniel Thompson if (IS_ERR(priv->base)) 518c6a97c42SDaniel Thompson return PTR_ERR(priv->base); 519c6a97c42SDaniel Thompson 520c6a97c42SDaniel Thompson priv->clk = devm_clk_get(&ofdev->dev, NULL); 521c6a97c42SDaniel Thompson if (IS_ERR(priv->clk)) 522c6a97c42SDaniel Thompson return PTR_ERR(priv->clk); 523c6a97c42SDaniel Thompson 524326ed382Slionel.debieve@st.com priv->rst = devm_reset_control_get(&ofdev->dev, NULL); 525326ed382Slionel.debieve@st.com if (!IS_ERR(priv->rst)) { 526326ed382Slionel.debieve@st.com reset_control_assert(priv->rst); 527326ed382Slionel.debieve@st.com udelay(2); 528326ed382Slionel.debieve@st.com reset_control_deassert(priv->rst); 529326ed382Slionel.debieve@st.com } 530326ed382Slionel.debieve@st.com 531529571edSlionel.debieve@st.com priv->ced = of_property_read_bool(np, "clock-error-detect"); 532a1b03e7aSGatien Chevallier priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf"); 533529571edSlionel.debieve@st.com 5346b85a7e1SGatien Chevallier priv->data = of_device_get_match_data(dev); 5356b85a7e1SGatien Chevallier if (!priv->data) 5366b85a7e1SGatien Chevallier return -ENODEV; 5376b85a7e1SGatien Chevallier 538c6a97c42SDaniel Thompson dev_set_drvdata(dev, priv); 539c6a97c42SDaniel Thompson 540436cdcdeSJulia Lawall priv->rng.name = dev_driver_string(dev); 541436cdcdeSJulia Lawall priv->rng.init = stm32_rng_init; 542436cdcdeSJulia Lawall priv->rng.read = stm32_rng_read; 543c6a97c42SDaniel Thompson priv->rng.priv = (unsigned long) dev; 54438a1965fSLionel Debieve priv->rng.quality = 900; 545c6a97c42SDaniel Thompson 546c6a97c42SDaniel Thompson pm_runtime_set_autosuspend_delay(dev, 100); 547c6a97c42SDaniel Thompson pm_runtime_use_autosuspend(dev); 548c6a97c42SDaniel Thompson pm_runtime_enable(dev); 549c6a97c42SDaniel Thompson 550c6a97c42SDaniel Thompson return devm_hwrng_register(dev, &priv->rng); 551c6a97c42SDaniel Thompson } 552c6a97c42SDaniel Thompson 553c6a97c42SDaniel Thompson static struct platform_driver stm32_rng_driver = { 554c6a97c42SDaniel Thompson .driver = { 555c6a97c42SDaniel Thompson .name = "stm32-rng", 556*ff4e4610SGatien Chevallier .pm = pm_ptr(&stm32_rng_pm_ops), 557c6a97c42SDaniel Thompson .of_match_table = stm32_rng_match, 558c6a97c42SDaniel Thompson }, 559c6a97c42SDaniel Thompson .probe = stm32_rng_probe, 560af0d4442SLionel Debieve .remove = stm32_rng_remove, 561c6a97c42SDaniel Thompson }; 562c6a97c42SDaniel Thompson 563c6a97c42SDaniel Thompson module_platform_driver(stm32_rng_driver); 564c6a97c42SDaniel Thompson 565c6a97c42SDaniel Thompson MODULE_LICENSE("GPL"); 566c6a97c42SDaniel Thompson MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>"); 567c6a97c42SDaniel Thompson MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver"); 568