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; 73c6a97c42SDaniel Thompson void __iomem *base; 74c6a97c42SDaniel Thompson struct clk *clk; 75326ed382Slionel.debieve@st.com struct reset_control *rst; 76ff4e4610SGatien 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"); 223*da62ed5cSMarek Vasut retval = -ENOTRECOVERABLE; 224*da62ed5cSMarek Vasut goto exit_rpm; 225b17bc6ebSGatien Chevallier } 226c6a97c42SDaniel Thompson 227b17bc6ebSGatien Chevallier continue; 228b17bc6ebSGatien Chevallier } 229b17bc6ebSGatien Chevallier 230b17bc6ebSGatien Chevallier if (WARN_ONCE((sr & RNG_SR_CEIS), "RNG clock too slow - %x\n", sr)) 231b17bc6ebSGatien Chevallier writel_relaxed(0, priv->base + RNG_SR); 232b17bc6ebSGatien Chevallier } 233b17bc6ebSGatien Chevallier 234b17bc6ebSGatien Chevallier /* Late seed error case: DR being 0 is an error status */ 235b17bc6ebSGatien Chevallier *(u32 *)data = readl_relaxed(priv->base + RNG_DR); 236b17bc6ebSGatien Chevallier if (!*(u32 *)data) { 237b17bc6ebSGatien Chevallier err = stm32_rng_conceal_seed_error(rng); 238b17bc6ebSGatien Chevallier i++; 239b17bc6ebSGatien Chevallier if (err && i > RNG_NB_RECOVER_TRIES) { 240b17bc6ebSGatien Chevallier dev_err((struct device *)priv->rng.priv, 241b17bc6ebSGatien Chevallier "Couldn't recover from seed error"); 242*da62ed5cSMarek Vasut retval = -ENOTRECOVERABLE; 243*da62ed5cSMarek 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 255*da62ed5cSMarek Vasut exit_rpm: 256c6a97c42SDaniel Thompson pm_runtime_mark_last_busy((struct device *) priv->rng.priv); 257c6a97c42SDaniel Thompson pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv); 258c6a97c42SDaniel Thompson 259c6a97c42SDaniel Thompson return retval || !wait ? retval : -EIO; 260c6a97c42SDaniel Thompson } 261c6a97c42SDaniel Thompson 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 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); 3326b85a7e1SGatien Chevallier dev_err((struct device *)priv->rng.priv, 3336b85a7e1SGatien Chevallier "%s: timeout %x!\n", __func__, reg); 3346b85a7e1SGatien Chevallier return -EINVAL; 3356b85a7e1SGatien Chevallier } 3366b85a7e1SGatien Chevallier } else { 3376b85a7e1SGatien Chevallier /* Handle all RNG versions by checking if conditional reset should be set */ 3386b85a7e1SGatien Chevallier if (priv->data->has_cond_reset) 3396b85a7e1SGatien Chevallier reg |= RNG_CR_CONDRST; 3406b85a7e1SGatien Chevallier 3416b85a7e1SGatien Chevallier if (priv->ced) 3426b85a7e1SGatien Chevallier reg &= ~RNG_CR_CED; 3436b85a7e1SGatien Chevallier else 3446b85a7e1SGatien Chevallier reg |= RNG_CR_CED; 3456b85a7e1SGatien Chevallier 3466b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3476b85a7e1SGatien Chevallier 3486b85a7e1SGatien Chevallier if (priv->data->has_cond_reset) 3496b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONDRST; 3506b85a7e1SGatien Chevallier 3516b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 3526b85a7e1SGatien Chevallier 3536b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3546b85a7e1SGatien Chevallier } 3556b85a7e1SGatien Chevallier 3566b85a7e1SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg, 3576b85a7e1SGatien Chevallier reg & RNG_SR_DRDY, 3586b85a7e1SGatien Chevallier 10, 100000); 35931b57788SMarek Vasut if (err || (reg & ~RNG_SR_DRDY)) { 3606b85a7e1SGatien Chevallier clk_disable_unprepare(priv->clk); 3616b85a7e1SGatien Chevallier dev_err((struct device *)priv->rng.priv, 3626b85a7e1SGatien Chevallier "%s: timeout:%x SR: %x!\n", __func__, err, reg); 3636b85a7e1SGatien Chevallier return -EINVAL; 3646b85a7e1SGatien Chevallier } 3656b85a7e1SGatien Chevallier 366c6a97c42SDaniel Thompson return 0; 367c6a97c42SDaniel Thompson } 368c6a97c42SDaniel Thompson 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 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 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); 3906b85a7e1SGatien Chevallier 391ff4e4610SGatien Chevallier if (priv->data->has_cond_reset) { 392ff4e4610SGatien Chevallier priv->pm_conf.nscr = readl_relaxed(priv->base + RNG_NSCR); 393ff4e4610SGatien Chevallier priv->pm_conf.htcr = readl_relaxed(priv->base + RNG_HTCR); 394ff4e4610SGatien Chevallier } 395ff4e4610SGatien Chevallier 396ff4e4610SGatien Chevallier /* Do not save that RNG is enabled as it will be handled at resume */ 397ff4e4610SGatien Chevallier priv->pm_conf.cr = readl_relaxed(priv->base + RNG_CR) & ~RNG_CR_RNGEN; 398ff4e4610SGatien Chevallier 399ff4e4610SGatien Chevallier writel_relaxed(priv->pm_conf.cr, priv->base + RNG_CR); 400ff4e4610SGatien Chevallier 401ff4e4610SGatien Chevallier clk_disable_unprepare(priv->clk); 402ff4e4610SGatien Chevallier 403ff4e4610SGatien Chevallier return 0; 404ff4e4610SGatien Chevallier } 405ff4e4610SGatien Chevallier 406ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_runtime_resume(struct device *dev) 407ff4e4610SGatien Chevallier { 408ff4e4610SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 409ff4e4610SGatien Chevallier int err; 410ff4e4610SGatien Chevallier u32 reg; 411ff4e4610SGatien Chevallier 412ff4e4610SGatien Chevallier err = clk_prepare_enable(priv->clk); 413ff4e4610SGatien Chevallier if (err) 414ff4e4610SGatien Chevallier return err; 415ff4e4610SGatien Chevallier 416ff4e4610SGatien Chevallier /* Clean error indications */ 417ff4e4610SGatien Chevallier writel_relaxed(0, priv->base + RNG_SR); 418ff4e4610SGatien Chevallier 4196b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 4206b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 4216b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 4226b85a7e1SGatien Chevallier 4236b85a7e1SGatien Chevallier return 0; 4246b85a7e1SGatien Chevallier } 4256b85a7e1SGatien Chevallier 426ff4e4610SGatien Chevallier static int __maybe_unused stm32_rng_resume(struct device *dev) 427ff4e4610SGatien Chevallier { 428ff4e4610SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 429ff4e4610SGatien Chevallier int err; 430ff4e4610SGatien Chevallier u32 reg; 431ff4e4610SGatien Chevallier 432ff4e4610SGatien Chevallier err = clk_prepare_enable(priv->clk); 433ff4e4610SGatien Chevallier if (err) 434ff4e4610SGatien Chevallier return err; 435ff4e4610SGatien Chevallier 436ff4e4610SGatien Chevallier /* Clean error indications */ 437ff4e4610SGatien Chevallier writel_relaxed(0, priv->base + RNG_SR); 438ff4e4610SGatien Chevallier 439ff4e4610SGatien Chevallier if (priv->data->has_cond_reset) { 440ff4e4610SGatien Chevallier /* 441ff4e4610SGatien Chevallier * Correct configuration in bits [29:4] must be set in the same 442ff4e4610SGatien Chevallier * access that set RNG_CR_CONDRST bit. Else config setting is 443ff4e4610SGatien Chevallier * not taken into account. CONFIGLOCK bit must also be unset but 444ff4e4610SGatien Chevallier * it is not handled at the moment. 445ff4e4610SGatien Chevallier */ 446ff4e4610SGatien Chevallier writel_relaxed(priv->pm_conf.cr | RNG_CR_CONDRST, priv->base + RNG_CR); 447ff4e4610SGatien Chevallier 448ff4e4610SGatien Chevallier writel_relaxed(priv->pm_conf.nscr, priv->base + RNG_NSCR); 449ff4e4610SGatien Chevallier writel_relaxed(priv->pm_conf.htcr, priv->base + RNG_HTCR); 450ff4e4610SGatien Chevallier 451ff4e4610SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 452ff4e4610SGatien Chevallier reg |= RNG_CR_RNGEN; 453ff4e4610SGatien Chevallier reg &= ~RNG_CR_CONDRST; 454ff4e4610SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 455ff4e4610SGatien Chevallier 456ff4e4610SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg, 457ff4e4610SGatien Chevallier reg & ~RNG_CR_CONDRST, 10, 100000); 458ff4e4610SGatien Chevallier 459ff4e4610SGatien Chevallier if (err) { 460ff4e4610SGatien Chevallier clk_disable_unprepare(priv->clk); 461ff4e4610SGatien Chevallier dev_err((struct device *)priv->rng.priv, 462ff4e4610SGatien Chevallier "%s: timeout:%x CR: %x!\n", __func__, err, reg); 463ff4e4610SGatien Chevallier return -EINVAL; 464ff4e4610SGatien Chevallier } 465ff4e4610SGatien Chevallier } else { 466ff4e4610SGatien Chevallier reg = priv->pm_conf.cr; 467ff4e4610SGatien Chevallier reg |= RNG_CR_RNGEN; 468ff4e4610SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 469ff4e4610SGatien Chevallier } 470ff4e4610SGatien Chevallier 471ff4e4610SGatien Chevallier return 0; 472ff4e4610SGatien Chevallier } 473ff4e4610SGatien Chevallier 474ff4e4610SGatien Chevallier static const struct dev_pm_ops __maybe_unused stm32_rng_pm_ops = { 4756b85a7e1SGatien Chevallier SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend, 4766b85a7e1SGatien Chevallier stm32_rng_runtime_resume, NULL) 477ff4e4610SGatien Chevallier SET_SYSTEM_SLEEP_PM_OPS(stm32_rng_suspend, 478ff4e4610SGatien Chevallier stm32_rng_resume) 4796b85a7e1SGatien Chevallier }; 4806b85a7e1SGatien Chevallier 4816b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32mp13_rng_data = { 4826b85a7e1SGatien Chevallier .has_cond_reset = true, 48328d13f3fSGatien Chevallier .max_clock_rate = 48000000, 4846b85a7e1SGatien Chevallier .cr = 0x00F00D00, 4856b85a7e1SGatien Chevallier .nscr = 0x2B5BB, 4866b85a7e1SGatien Chevallier .htcr = 0x969D, 4876b85a7e1SGatien Chevallier }; 4886b85a7e1SGatien Chevallier 4896b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32_rng_data = { 4906b85a7e1SGatien Chevallier .has_cond_reset = false, 49128d13f3fSGatien Chevallier .max_clock_rate = 3000000, 4926b85a7e1SGatien Chevallier }; 4936b85a7e1SGatien Chevallier 4946b85a7e1SGatien Chevallier static const struct of_device_id stm32_rng_match[] = { 4956b85a7e1SGatien Chevallier { 4966b85a7e1SGatien Chevallier .compatible = "st,stm32mp13-rng", 4976b85a7e1SGatien Chevallier .data = &stm32mp13_rng_data, 4986b85a7e1SGatien Chevallier }, 4996b85a7e1SGatien Chevallier { 5006b85a7e1SGatien Chevallier .compatible = "st,stm32-rng", 5016b85a7e1SGatien Chevallier .data = &stm32_rng_data, 5026b85a7e1SGatien Chevallier }, 5036b85a7e1SGatien Chevallier {}, 5046b85a7e1SGatien Chevallier }; 5056b85a7e1SGatien Chevallier MODULE_DEVICE_TABLE(of, stm32_rng_match); 5066b85a7e1SGatien Chevallier 507c6a97c42SDaniel Thompson static int stm32_rng_probe(struct platform_device *ofdev) 508c6a97c42SDaniel Thompson { 509c6a97c42SDaniel Thompson struct device *dev = &ofdev->dev; 510c6a97c42SDaniel Thompson struct device_node *np = ofdev->dev.of_node; 511c6a97c42SDaniel Thompson struct stm32_rng_private *priv; 51218d9a826SGatien Chevallier struct resource *res; 513c6a97c42SDaniel Thompson 514c6a97c42SDaniel Thompson priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL); 515c6a97c42SDaniel Thompson if (!priv) 516c6a97c42SDaniel Thompson return -ENOMEM; 517c6a97c42SDaniel Thompson 51818d9a826SGatien Chevallier priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res); 519c6a97c42SDaniel Thompson if (IS_ERR(priv->base)) 520c6a97c42SDaniel Thompson return PTR_ERR(priv->base); 521c6a97c42SDaniel Thompson 522c6a97c42SDaniel Thompson priv->clk = devm_clk_get(&ofdev->dev, NULL); 523c6a97c42SDaniel Thompson if (IS_ERR(priv->clk)) 524c6a97c42SDaniel Thompson return PTR_ERR(priv->clk); 525c6a97c42SDaniel Thompson 526326ed382Slionel.debieve@st.com priv->rst = devm_reset_control_get(&ofdev->dev, NULL); 527326ed382Slionel.debieve@st.com if (!IS_ERR(priv->rst)) { 528326ed382Slionel.debieve@st.com reset_control_assert(priv->rst); 529326ed382Slionel.debieve@st.com udelay(2); 530326ed382Slionel.debieve@st.com reset_control_deassert(priv->rst); 531326ed382Slionel.debieve@st.com } 532326ed382Slionel.debieve@st.com 533529571edSlionel.debieve@st.com priv->ced = of_property_read_bool(np, "clock-error-detect"); 534a1b03e7aSGatien Chevallier priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf"); 535529571edSlionel.debieve@st.com 5366b85a7e1SGatien Chevallier priv->data = of_device_get_match_data(dev); 5376b85a7e1SGatien Chevallier if (!priv->data) 5386b85a7e1SGatien Chevallier return -ENODEV; 5396b85a7e1SGatien Chevallier 540c6a97c42SDaniel Thompson dev_set_drvdata(dev, priv); 541c6a97c42SDaniel Thompson 542436cdcdeSJulia Lawall priv->rng.name = dev_driver_string(dev); 543436cdcdeSJulia Lawall priv->rng.init = stm32_rng_init; 544436cdcdeSJulia Lawall priv->rng.read = stm32_rng_read; 545c6a97c42SDaniel Thompson priv->rng.priv = (unsigned long) dev; 54638a1965fSLionel Debieve priv->rng.quality = 900; 547c6a97c42SDaniel Thompson 548c6a97c42SDaniel Thompson pm_runtime_set_autosuspend_delay(dev, 100); 549c6a97c42SDaniel Thompson pm_runtime_use_autosuspend(dev); 550c6a97c42SDaniel Thompson pm_runtime_enable(dev); 551c6a97c42SDaniel Thompson 552c6a97c42SDaniel Thompson return devm_hwrng_register(dev, &priv->rng); 553c6a97c42SDaniel Thompson } 554c6a97c42SDaniel Thompson 555c6a97c42SDaniel Thompson static struct platform_driver stm32_rng_driver = { 556c6a97c42SDaniel Thompson .driver = { 557c6a97c42SDaniel Thompson .name = "stm32-rng", 558ff4e4610SGatien Chevallier .pm = pm_ptr(&stm32_rng_pm_ops), 559c6a97c42SDaniel Thompson .of_match_table = stm32_rng_match, 560c6a97c42SDaniel Thompson }, 561c6a97c42SDaniel Thompson .probe = stm32_rng_probe, 562541b0719SUwe Kleine-König .remove_new = stm32_rng_remove, 563c6a97c42SDaniel Thompson }; 564c6a97c42SDaniel Thompson 565c6a97c42SDaniel Thompson module_platform_driver(stm32_rng_driver); 566c6a97c42SDaniel Thompson 567c6a97c42SDaniel Thompson MODULE_LICENSE("GPL"); 568c6a97c42SDaniel Thompson MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>"); 569c6a97c42SDaniel Thompson MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver"); 570