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) 26*28d13f3fSGatien Chevallier #define RNG_CR_CLKDIV_SHIFT 16 27*28d13f3fSGatien 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) 32*28d13f3fSGatien 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 { 51*28d13f3fSGatien 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 58c6a97c42SDaniel Thompson struct stm32_rng_private { 59c6a97c42SDaniel Thompson struct hwrng rng; 60c6a97c42SDaniel Thompson void __iomem *base; 61c6a97c42SDaniel Thompson struct clk *clk; 62326ed382Slionel.debieve@st.com struct reset_control *rst; 636b85a7e1SGatien Chevallier const struct stm32_rng_data *data; 64529571edSlionel.debieve@st.com bool ced; 65c6a97c42SDaniel Thompson }; 66c6a97c42SDaniel Thompson 678f1c5227SGatien Chevallier /* 688f1c5227SGatien Chevallier * Extracts from the STM32 RNG specification when RNG supports CONDRST. 698f1c5227SGatien Chevallier * 708f1c5227SGatien Chevallier * When a noise source (or seed) error occurs, the RNG stops generating 718f1c5227SGatien Chevallier * random numbers and sets to “1” both SEIS and SECS bits to indicate 728f1c5227SGatien Chevallier * that a seed error occurred. (...) 738f1c5227SGatien Chevallier * 748f1c5227SGatien Chevallier * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield 758f1c5227SGatien Chevallier * description for details). This step is needed only if SECS is set. 768f1c5227SGatien Chevallier * Indeed, when SEIS is set and SECS is cleared it means RNG performed 778f1c5227SGatien Chevallier * the reset automatically (auto-reset). 788f1c5227SGatien Chevallier * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST 798f1c5227SGatien Chevallier * to be cleared in the RNG_CR register, then confirm that SEIS is 808f1c5227SGatien Chevallier * cleared in the RNG_SR register. Otherwise just clear SEIS bit in 818f1c5227SGatien Chevallier * the RNG_SR register. 828f1c5227SGatien Chevallier * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be 838f1c5227SGatien Chevallier * cleared by RNG. The random number generation is now back to normal. 848f1c5227SGatien Chevallier */ 858f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv) 868f1c5227SGatien Chevallier { 878f1c5227SGatien Chevallier struct device *dev = (struct device *)priv->rng.priv; 888f1c5227SGatien Chevallier u32 sr = readl_relaxed(priv->base + RNG_SR); 898f1c5227SGatien Chevallier u32 cr = readl_relaxed(priv->base + RNG_CR); 908f1c5227SGatien Chevallier int err; 918f1c5227SGatien Chevallier 928f1c5227SGatien Chevallier if (sr & RNG_SR_SECS) { 938f1c5227SGatien Chevallier /* Conceal by resetting the subsystem (step 1.) */ 948f1c5227SGatien Chevallier writel_relaxed(cr | RNG_CR_CONDRST, priv->base + RNG_CR); 958f1c5227SGatien Chevallier writel_relaxed(cr & ~RNG_CR_CONDRST, priv->base + RNG_CR); 968f1c5227SGatien Chevallier } else { 978f1c5227SGatien Chevallier /* RNG auto-reset (step 2.) */ 988f1c5227SGatien Chevallier writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR); 998f1c5227SGatien Chevallier goto end; 1008f1c5227SGatien Chevallier } 1018f1c5227SGatien Chevallier 1028f1c5227SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, cr, !(cr & RNG_CR_CONDRST), 10, 1038f1c5227SGatien Chevallier 100000); 1048f1c5227SGatien Chevallier if (err) { 1058f1c5227SGatien Chevallier dev_err(dev, "%s: timeout %x\n", __func__, sr); 1068f1c5227SGatien Chevallier return err; 1078f1c5227SGatien Chevallier } 1088f1c5227SGatien Chevallier 1098f1c5227SGatien Chevallier /* Check SEIS is cleared (step 2.) */ 1108f1c5227SGatien Chevallier if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS) 1118f1c5227SGatien Chevallier return -EINVAL; 1128f1c5227SGatien Chevallier 1138f1c5227SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 10, 1148f1c5227SGatien Chevallier 100000); 1158f1c5227SGatien Chevallier if (err) { 1168f1c5227SGatien Chevallier dev_err(dev, "%s: timeout %x\n", __func__, sr); 1178f1c5227SGatien Chevallier return err; 1188f1c5227SGatien Chevallier } 1198f1c5227SGatien Chevallier 1208f1c5227SGatien Chevallier end: 1218f1c5227SGatien Chevallier return 0; 1228f1c5227SGatien Chevallier } 1238f1c5227SGatien Chevallier 1248f1c5227SGatien Chevallier /* 1258f1c5227SGatien Chevallier * Extracts from the STM32 RNG specification, when CONDRST is not supported 1268f1c5227SGatien Chevallier * 1278f1c5227SGatien Chevallier * When a noise source (or seed) error occurs, the RNG stops generating 1288f1c5227SGatien Chevallier * random numbers and sets to “1” both SEIS and SECS bits to indicate 1298f1c5227SGatien Chevallier * that a seed error occurred. (...) 1308f1c5227SGatien Chevallier * 1318f1c5227SGatien Chevallier * The following sequence shall be used to fully recover from a seed 1328f1c5227SGatien Chevallier * error after the RNG initialization: 1338f1c5227SGatien Chevallier * 1. Clear the SEIS bit by writing it to “0”. 1348f1c5227SGatien Chevallier * 2. Read out 12 words from the RNG_DR register, and discard each of 1358f1c5227SGatien Chevallier * them in order to clean the pipeline. 1368f1c5227SGatien Chevallier * 3. Confirm that SEIS is still cleared. Random number generation is 1378f1c5227SGatien Chevallier * back to normal. 1388f1c5227SGatien Chevallier */ 1398f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_private *priv) 1408f1c5227SGatien Chevallier { 1418f1c5227SGatien Chevallier unsigned int i = 0; 1428f1c5227SGatien Chevallier u32 sr = readl_relaxed(priv->base + RNG_SR); 1438f1c5227SGatien Chevallier 1448f1c5227SGatien Chevallier writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR); 1458f1c5227SGatien Chevallier 1468f1c5227SGatien Chevallier for (i = 12; i != 0; i--) 1478f1c5227SGatien Chevallier (void)readl_relaxed(priv->base + RNG_DR); 1488f1c5227SGatien Chevallier 1498f1c5227SGatien Chevallier if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS) 1508f1c5227SGatien Chevallier return -EINVAL; 1518f1c5227SGatien Chevallier 1528f1c5227SGatien Chevallier return 0; 1538f1c5227SGatien Chevallier } 1548f1c5227SGatien Chevallier 1558f1c5227SGatien Chevallier static int stm32_rng_conceal_seed_error(struct hwrng *rng) 1568f1c5227SGatien Chevallier { 1578f1c5227SGatien Chevallier struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng); 1588f1c5227SGatien Chevallier 1598f1c5227SGatien Chevallier dev_dbg((struct device *)priv->rng.priv, "Concealing seed error\n"); 1608f1c5227SGatien Chevallier 1618f1c5227SGatien Chevallier if (priv->data->has_cond_reset) 1628f1c5227SGatien Chevallier return stm32_rng_conceal_seed_error_cond_reset(priv); 1638f1c5227SGatien Chevallier else 1648f1c5227SGatien Chevallier return stm32_rng_conceal_seed_error_sw_reset(priv); 1658f1c5227SGatien Chevallier }; 1668f1c5227SGatien Chevallier 1678f1c5227SGatien Chevallier 168c6a97c42SDaniel Thompson static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 169c6a97c42SDaniel Thompson { 170b17bc6ebSGatien Chevallier struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng); 171b17bc6ebSGatien Chevallier unsigned int i = 0; 172b17bc6ebSGatien Chevallier int retval = 0, err = 0; 173c6a97c42SDaniel Thompson u32 sr; 174c6a97c42SDaniel Thompson 175c6a97c42SDaniel Thompson pm_runtime_get_sync((struct device *) priv->rng.priv); 176c6a97c42SDaniel Thompson 1778f1c5227SGatien Chevallier if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS) 1788f1c5227SGatien Chevallier stm32_rng_conceal_seed_error(rng); 1798f1c5227SGatien Chevallier 180e64f57e8STomas Marek while (max >= sizeof(u32)) { 181c6a97c42SDaniel Thompson sr = readl_relaxed(priv->base + RNG_SR); 182b17bc6ebSGatien Chevallier /* 183b17bc6ebSGatien Chevallier * Manage timeout which is based on timer and take 184b17bc6ebSGatien Chevallier * care of initial delay time when enabling the RNG. 185b17bc6ebSGatien Chevallier */ 186c6a97c42SDaniel Thompson if (!sr && wait) { 1877cdc5e6bSTomas Marek err = readl_relaxed_poll_timeout_atomic(priv->base 188279f4f8fSlionel.debieve@st.com + RNG_SR, 189279f4f8fSlionel.debieve@st.com sr, sr, 190279f4f8fSlionel.debieve@st.com 10, 50000); 191b17bc6ebSGatien Chevallier if (err) { 192279f4f8fSlionel.debieve@st.com dev_err((struct device *)priv->rng.priv, 193279f4f8fSlionel.debieve@st.com "%s: timeout %x!\n", __func__, sr); 194b17bc6ebSGatien Chevallier break; 195c6a97c42SDaniel Thompson } 196b17bc6ebSGatien Chevallier } else if (!sr) { 197b17bc6ebSGatien Chevallier /* The FIFO is being filled up */ 198c6a97c42SDaniel Thompson break; 1991ff69adfSMaxime Coquelin } 200c6a97c42SDaniel Thompson 201b17bc6ebSGatien Chevallier if (sr != RNG_SR_DRDY) { 202b17bc6ebSGatien Chevallier if (sr & RNG_SR_SEIS) { 203b17bc6ebSGatien Chevallier err = stm32_rng_conceal_seed_error(rng); 204b17bc6ebSGatien Chevallier i++; 205b17bc6ebSGatien Chevallier if (err && i > RNG_NB_RECOVER_TRIES) { 206b17bc6ebSGatien Chevallier dev_err((struct device *)priv->rng.priv, 207b17bc6ebSGatien Chevallier "Couldn't recover from seed error\n"); 208b17bc6ebSGatien Chevallier return -ENOTRECOVERABLE; 209b17bc6ebSGatien Chevallier } 210c6a97c42SDaniel Thompson 211b17bc6ebSGatien Chevallier continue; 212b17bc6ebSGatien Chevallier } 213b17bc6ebSGatien Chevallier 214b17bc6ebSGatien Chevallier if (WARN_ONCE((sr & RNG_SR_CEIS), "RNG clock too slow - %x\n", sr)) 215b17bc6ebSGatien Chevallier writel_relaxed(0, priv->base + RNG_SR); 216b17bc6ebSGatien Chevallier } 217b17bc6ebSGatien Chevallier 218b17bc6ebSGatien Chevallier /* Late seed error case: DR being 0 is an error status */ 219b17bc6ebSGatien Chevallier *(u32 *)data = readl_relaxed(priv->base + RNG_DR); 220b17bc6ebSGatien Chevallier if (!*(u32 *)data) { 221b17bc6ebSGatien Chevallier err = stm32_rng_conceal_seed_error(rng); 222b17bc6ebSGatien Chevallier i++; 223b17bc6ebSGatien Chevallier if (err && i > RNG_NB_RECOVER_TRIES) { 224b17bc6ebSGatien Chevallier dev_err((struct device *)priv->rng.priv, 225b17bc6ebSGatien Chevallier "Couldn't recover from seed error"); 226b17bc6ebSGatien Chevallier return -ENOTRECOVERABLE; 227b17bc6ebSGatien Chevallier } 228b17bc6ebSGatien Chevallier 229b17bc6ebSGatien Chevallier continue; 230b17bc6ebSGatien Chevallier } 231b17bc6ebSGatien Chevallier 232b17bc6ebSGatien Chevallier i = 0; 233c6a97c42SDaniel Thompson retval += sizeof(u32); 234c6a97c42SDaniel Thompson data += sizeof(u32); 235c6a97c42SDaniel Thompson max -= sizeof(u32); 236c6a97c42SDaniel Thompson } 237c6a97c42SDaniel Thompson 238c6a97c42SDaniel Thompson pm_runtime_mark_last_busy((struct device *) priv->rng.priv); 239c6a97c42SDaniel Thompson pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv); 240c6a97c42SDaniel Thompson 241c6a97c42SDaniel Thompson return retval || !wait ? retval : -EIO; 242c6a97c42SDaniel Thompson } 243c6a97c42SDaniel Thompson 244*28d13f3fSGatien Chevallier static uint stm32_rng_clock_freq_restrain(struct hwrng *rng) 245*28d13f3fSGatien Chevallier { 246*28d13f3fSGatien Chevallier struct stm32_rng_private *priv = 247*28d13f3fSGatien Chevallier container_of(rng, struct stm32_rng_private, rng); 248*28d13f3fSGatien Chevallier unsigned long clock_rate = 0; 249*28d13f3fSGatien Chevallier uint clock_div = 0; 250*28d13f3fSGatien Chevallier 251*28d13f3fSGatien Chevallier clock_rate = clk_get_rate(priv->clk); 252*28d13f3fSGatien Chevallier 253*28d13f3fSGatien Chevallier /* 254*28d13f3fSGatien Chevallier * Get the exponent to apply on the CLKDIV field in RNG_CR register 255*28d13f3fSGatien Chevallier * No need to handle the case when clock-div > 0xF as it is physically 256*28d13f3fSGatien Chevallier * impossible 257*28d13f3fSGatien Chevallier */ 258*28d13f3fSGatien Chevallier while ((clock_rate >> clock_div) > priv->data->max_clock_rate) 259*28d13f3fSGatien Chevallier clock_div++; 260*28d13f3fSGatien Chevallier 261*28d13f3fSGatien Chevallier pr_debug("RNG clk rate : %lu\n", clk_get_rate(priv->clk) >> clock_div); 262*28d13f3fSGatien Chevallier 263*28d13f3fSGatien Chevallier return clock_div; 264*28d13f3fSGatien Chevallier } 265*28d13f3fSGatien Chevallier 266c6a97c42SDaniel Thompson static int stm32_rng_init(struct hwrng *rng) 267c6a97c42SDaniel Thompson { 268c6a97c42SDaniel Thompson struct stm32_rng_private *priv = 269c6a97c42SDaniel Thompson container_of(rng, struct stm32_rng_private, rng); 270c6a97c42SDaniel Thompson int err; 2716b85a7e1SGatien Chevallier u32 reg; 272c6a97c42SDaniel Thompson 273c6a97c42SDaniel Thompson err = clk_prepare_enable(priv->clk); 274c6a97c42SDaniel Thompson if (err) 275c6a97c42SDaniel Thompson return err; 276c6a97c42SDaniel Thompson 277c6a97c42SDaniel Thompson /* clear error indicators */ 278c6a97c42SDaniel Thompson writel_relaxed(0, priv->base + RNG_SR); 279c6a97c42SDaniel Thompson 2806b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 2816b85a7e1SGatien Chevallier 2826b85a7e1SGatien Chevallier /* 2836b85a7e1SGatien Chevallier * Keep default RNG configuration if none was specified. 2846b85a7e1SGatien Chevallier * 0 is an invalid value as it disables all entropy sources. 2856b85a7e1SGatien Chevallier */ 2866b85a7e1SGatien Chevallier if (priv->data->has_cond_reset && priv->data->cr) { 287*28d13f3fSGatien Chevallier uint clock_div = stm32_rng_clock_freq_restrain(rng); 288*28d13f3fSGatien Chevallier 2896b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONFIG_MASK; 290*28d13f3fSGatien Chevallier reg |= RNG_CR_CONDRST | (priv->data->cr & RNG_CR_ENTROPY_SRC_MASK) | 291*28d13f3fSGatien Chevallier (clock_div << RNG_CR_CLKDIV_SHIFT); 2926b85a7e1SGatien Chevallier if (priv->ced) 2936b85a7e1SGatien Chevallier reg &= ~RNG_CR_CED; 2946b85a7e1SGatien Chevallier else 2956b85a7e1SGatien Chevallier reg |= RNG_CR_CED; 2966b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 2976b85a7e1SGatien Chevallier 2986b85a7e1SGatien Chevallier /* Health tests and noise control registers */ 2996b85a7e1SGatien Chevallier writel_relaxed(priv->data->htcr, priv->base + RNG_HTCR); 3006b85a7e1SGatien Chevallier writel_relaxed(priv->data->nscr & RNG_NSCR_MASK, priv->base + RNG_NSCR); 3016b85a7e1SGatien Chevallier 3026b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONDRST; 3036b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 3046b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3056b85a7e1SGatien Chevallier 3066b85a7e1SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg, 3076b85a7e1SGatien Chevallier (!(reg & RNG_CR_CONDRST)), 3086b85a7e1SGatien Chevallier 10, 50000); 3096b85a7e1SGatien Chevallier if (err) { 3106b85a7e1SGatien Chevallier dev_err((struct device *)priv->rng.priv, 3116b85a7e1SGatien Chevallier "%s: timeout %x!\n", __func__, reg); 3126b85a7e1SGatien Chevallier return -EINVAL; 3136b85a7e1SGatien Chevallier } 3146b85a7e1SGatien Chevallier } else { 3156b85a7e1SGatien Chevallier /* Handle all RNG versions by checking if conditional reset should be set */ 3166b85a7e1SGatien Chevallier if (priv->data->has_cond_reset) 3176b85a7e1SGatien Chevallier reg |= RNG_CR_CONDRST; 3186b85a7e1SGatien Chevallier 3196b85a7e1SGatien Chevallier if (priv->ced) 3206b85a7e1SGatien Chevallier reg &= ~RNG_CR_CED; 3216b85a7e1SGatien Chevallier else 3226b85a7e1SGatien Chevallier reg |= RNG_CR_CED; 3236b85a7e1SGatien Chevallier 3246b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3256b85a7e1SGatien Chevallier 3266b85a7e1SGatien Chevallier if (priv->data->has_cond_reset) 3276b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONDRST; 3286b85a7e1SGatien Chevallier 3296b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 3306b85a7e1SGatien Chevallier 3316b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3326b85a7e1SGatien Chevallier } 3336b85a7e1SGatien Chevallier 3346b85a7e1SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg, 3356b85a7e1SGatien Chevallier reg & RNG_SR_DRDY, 3366b85a7e1SGatien Chevallier 10, 100000); 3376b85a7e1SGatien Chevallier if (err | (reg & ~RNG_SR_DRDY)) { 3386b85a7e1SGatien Chevallier clk_disable_unprepare(priv->clk); 3396b85a7e1SGatien Chevallier dev_err((struct device *)priv->rng.priv, 3406b85a7e1SGatien Chevallier "%s: timeout:%x SR: %x!\n", __func__, err, reg); 3416b85a7e1SGatien Chevallier return -EINVAL; 3426b85a7e1SGatien Chevallier } 3436b85a7e1SGatien Chevallier 344c6a97c42SDaniel Thompson return 0; 345c6a97c42SDaniel Thompson } 346c6a97c42SDaniel Thompson 3476b85a7e1SGatien Chevallier static int stm32_rng_remove(struct platform_device *ofdev) 348c6a97c42SDaniel Thompson { 3496b85a7e1SGatien Chevallier pm_runtime_disable(&ofdev->dev); 350c6a97c42SDaniel Thompson 3516b85a7e1SGatien Chevallier return 0; 352c6a97c42SDaniel Thompson } 353c6a97c42SDaniel Thompson 3546b85a7e1SGatien Chevallier #ifdef CONFIG_PM 3556b85a7e1SGatien Chevallier static int stm32_rng_runtime_suspend(struct device *dev) 3566b85a7e1SGatien Chevallier { 3576b85a7e1SGatien Chevallier u32 reg; 3586b85a7e1SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 3596b85a7e1SGatien Chevallier 3606b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 3616b85a7e1SGatien Chevallier reg &= ~RNG_CR_RNGEN; 3626b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3636b85a7e1SGatien Chevallier clk_disable_unprepare(priv->clk); 3646b85a7e1SGatien Chevallier 3656b85a7e1SGatien Chevallier return 0; 3666b85a7e1SGatien Chevallier } 3676b85a7e1SGatien Chevallier 3686b85a7e1SGatien Chevallier static int stm32_rng_runtime_resume(struct device *dev) 3696b85a7e1SGatien Chevallier { 3706b85a7e1SGatien Chevallier u32 reg; 3716b85a7e1SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 3726b85a7e1SGatien Chevallier 3736b85a7e1SGatien Chevallier clk_prepare_enable(priv->clk); 3746b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 3756b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 3766b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 3776b85a7e1SGatien Chevallier 3786b85a7e1SGatien Chevallier return 0; 3796b85a7e1SGatien Chevallier } 3806b85a7e1SGatien Chevallier #endif 3816b85a7e1SGatien Chevallier 3826b85a7e1SGatien Chevallier static const struct dev_pm_ops stm32_rng_pm_ops = { 3836b85a7e1SGatien Chevallier SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend, 3846b85a7e1SGatien Chevallier stm32_rng_runtime_resume, NULL) 3856b85a7e1SGatien Chevallier SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 3866b85a7e1SGatien Chevallier pm_runtime_force_resume) 3876b85a7e1SGatien Chevallier }; 3886b85a7e1SGatien Chevallier 3896b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32mp13_rng_data = { 3906b85a7e1SGatien Chevallier .has_cond_reset = true, 391*28d13f3fSGatien Chevallier .max_clock_rate = 48000000, 3926b85a7e1SGatien Chevallier .cr = 0x00F00D00, 3936b85a7e1SGatien Chevallier .nscr = 0x2B5BB, 3946b85a7e1SGatien Chevallier .htcr = 0x969D, 3956b85a7e1SGatien Chevallier }; 3966b85a7e1SGatien Chevallier 3976b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32_rng_data = { 3986b85a7e1SGatien Chevallier .has_cond_reset = false, 399*28d13f3fSGatien Chevallier .max_clock_rate = 3000000, 4006b85a7e1SGatien Chevallier }; 4016b85a7e1SGatien Chevallier 4026b85a7e1SGatien Chevallier static const struct of_device_id stm32_rng_match[] = { 4036b85a7e1SGatien Chevallier { 4046b85a7e1SGatien Chevallier .compatible = "st,stm32mp13-rng", 4056b85a7e1SGatien Chevallier .data = &stm32mp13_rng_data, 4066b85a7e1SGatien Chevallier }, 4076b85a7e1SGatien Chevallier { 4086b85a7e1SGatien Chevallier .compatible = "st,stm32-rng", 4096b85a7e1SGatien Chevallier .data = &stm32_rng_data, 4106b85a7e1SGatien Chevallier }, 4116b85a7e1SGatien Chevallier {}, 4126b85a7e1SGatien Chevallier }; 4136b85a7e1SGatien Chevallier MODULE_DEVICE_TABLE(of, stm32_rng_match); 4146b85a7e1SGatien Chevallier 415c6a97c42SDaniel Thompson static int stm32_rng_probe(struct platform_device *ofdev) 416c6a97c42SDaniel Thompson { 417c6a97c42SDaniel Thompson struct device *dev = &ofdev->dev; 418c6a97c42SDaniel Thompson struct device_node *np = ofdev->dev.of_node; 419c6a97c42SDaniel Thompson struct stm32_rng_private *priv; 42018d9a826SGatien Chevallier struct resource *res; 421c6a97c42SDaniel Thompson 422c6a97c42SDaniel Thompson priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL); 423c6a97c42SDaniel Thompson if (!priv) 424c6a97c42SDaniel Thompson return -ENOMEM; 425c6a97c42SDaniel Thompson 42618d9a826SGatien Chevallier priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res); 427c6a97c42SDaniel Thompson if (IS_ERR(priv->base)) 428c6a97c42SDaniel Thompson return PTR_ERR(priv->base); 429c6a97c42SDaniel Thompson 430c6a97c42SDaniel Thompson priv->clk = devm_clk_get(&ofdev->dev, NULL); 431c6a97c42SDaniel Thompson if (IS_ERR(priv->clk)) 432c6a97c42SDaniel Thompson return PTR_ERR(priv->clk); 433c6a97c42SDaniel Thompson 434326ed382Slionel.debieve@st.com priv->rst = devm_reset_control_get(&ofdev->dev, NULL); 435326ed382Slionel.debieve@st.com if (!IS_ERR(priv->rst)) { 436326ed382Slionel.debieve@st.com reset_control_assert(priv->rst); 437326ed382Slionel.debieve@st.com udelay(2); 438326ed382Slionel.debieve@st.com reset_control_deassert(priv->rst); 439326ed382Slionel.debieve@st.com } 440326ed382Slionel.debieve@st.com 441529571edSlionel.debieve@st.com priv->ced = of_property_read_bool(np, "clock-error-detect"); 442529571edSlionel.debieve@st.com 4436b85a7e1SGatien Chevallier priv->data = of_device_get_match_data(dev); 4446b85a7e1SGatien Chevallier if (!priv->data) 4456b85a7e1SGatien Chevallier return -ENODEV; 4466b85a7e1SGatien Chevallier 447c6a97c42SDaniel Thompson dev_set_drvdata(dev, priv); 448c6a97c42SDaniel Thompson 449436cdcdeSJulia Lawall priv->rng.name = dev_driver_string(dev); 450436cdcdeSJulia Lawall priv->rng.init = stm32_rng_init; 451436cdcdeSJulia Lawall priv->rng.read = stm32_rng_read; 452c6a97c42SDaniel Thompson priv->rng.priv = (unsigned long) dev; 45338a1965fSLionel Debieve priv->rng.quality = 900; 454c6a97c42SDaniel Thompson 455c6a97c42SDaniel Thompson pm_runtime_set_autosuspend_delay(dev, 100); 456c6a97c42SDaniel Thompson pm_runtime_use_autosuspend(dev); 457c6a97c42SDaniel Thompson pm_runtime_enable(dev); 458c6a97c42SDaniel Thompson 459c6a97c42SDaniel Thompson return devm_hwrng_register(dev, &priv->rng); 460c6a97c42SDaniel Thompson } 461c6a97c42SDaniel Thompson 462c6a97c42SDaniel Thompson static struct platform_driver stm32_rng_driver = { 463c6a97c42SDaniel Thompson .driver = { 464c6a97c42SDaniel Thompson .name = "stm32-rng", 465c6a97c42SDaniel Thompson .pm = &stm32_rng_pm_ops, 466c6a97c42SDaniel Thompson .of_match_table = stm32_rng_match, 467c6a97c42SDaniel Thompson }, 468c6a97c42SDaniel Thompson .probe = stm32_rng_probe, 469af0d4442SLionel Debieve .remove = stm32_rng_remove, 470c6a97c42SDaniel Thompson }; 471c6a97c42SDaniel Thompson 472c6a97c42SDaniel Thompson module_platform_driver(stm32_rng_driver); 473c6a97c42SDaniel Thompson 474c6a97c42SDaniel Thompson MODULE_LICENSE("GPL"); 475c6a97c42SDaniel Thompson MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>"); 476c6a97c42SDaniel Thompson MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver"); 477