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) 23*6b85a7e1SGatien Chevallier #define RNG_CR_CONFIG1 GENMASK(11, 8) 24*6b85a7e1SGatien Chevallier #define RNG_CR_NISTC BIT(12) 25*6b85a7e1SGatien Chevallier #define RNG_CR_CONFIG2 GENMASK(15, 13) 26*6b85a7e1SGatien Chevallier #define RNG_CR_CONFIG3 GENMASK(25, 20) 27*6b85a7e1SGatien Chevallier #define RNG_CR_CONDRST BIT(30) 28*6b85a7e1SGatien Chevallier #define RNG_CR_CONFLOCK BIT(31) 29*6b85a7e1SGatien Chevallier #define RNG_CR_ENTROPY_SRC_MASK (RNG_CR_CONFIG1 | RNG_CR_NISTC | RNG_CR_CONFIG2 | RNG_CR_CONFIG3) 30*6b85a7e1SGatien Chevallier #define RNG_CR_CONFIG_MASK (RNG_CR_ENTROPY_SRC_MASK | RNG_CR_CED) 31c6a97c42SDaniel Thompson 32c6a97c42SDaniel Thompson #define RNG_SR 0x04 33c6a97c42SDaniel Thompson #define RNG_SR_SEIS BIT(6) 34c6a97c42SDaniel Thompson #define RNG_SR_CEIS BIT(5) 35c6a97c42SDaniel Thompson #define RNG_SR_DRDY BIT(0) 36c6a97c42SDaniel Thompson 37c6a97c42SDaniel Thompson #define RNG_DR 0x08 38c6a97c42SDaniel Thompson 39*6b85a7e1SGatien Chevallier #define RNG_NSCR 0x0C 40*6b85a7e1SGatien Chevallier #define RNG_NSCR_MASK GENMASK(17, 0) 41*6b85a7e1SGatien Chevallier 42*6b85a7e1SGatien Chevallier #define RNG_HTCR 0x10 43*6b85a7e1SGatien Chevallier 44*6b85a7e1SGatien Chevallier struct stm32_rng_data { 45*6b85a7e1SGatien Chevallier u32 cr; 46*6b85a7e1SGatien Chevallier u32 nscr; 47*6b85a7e1SGatien Chevallier u32 htcr; 48*6b85a7e1SGatien Chevallier bool has_cond_reset; 49*6b85a7e1SGatien Chevallier }; 50*6b85a7e1SGatien Chevallier 51c6a97c42SDaniel Thompson struct stm32_rng_private { 52c6a97c42SDaniel Thompson struct hwrng rng; 53c6a97c42SDaniel Thompson void __iomem *base; 54c6a97c42SDaniel Thompson struct clk *clk; 55326ed382Slionel.debieve@st.com struct reset_control *rst; 56*6b85a7e1SGatien Chevallier const struct stm32_rng_data *data; 57529571edSlionel.debieve@st.com bool ced; 58c6a97c42SDaniel Thompson }; 59c6a97c42SDaniel Thompson 60c6a97c42SDaniel Thompson static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 61c6a97c42SDaniel Thompson { 62c6a97c42SDaniel Thompson struct stm32_rng_private *priv = 63c6a97c42SDaniel Thompson container_of(rng, struct stm32_rng_private, rng); 64c6a97c42SDaniel Thompson u32 sr; 65c6a97c42SDaniel Thompson int retval = 0; 66c6a97c42SDaniel Thompson 67c6a97c42SDaniel Thompson pm_runtime_get_sync((struct device *) priv->rng.priv); 68c6a97c42SDaniel Thompson 69e64f57e8STomas Marek while (max >= sizeof(u32)) { 70c6a97c42SDaniel Thompson sr = readl_relaxed(priv->base + RNG_SR); 71279f4f8fSlionel.debieve@st.com /* Manage timeout which is based on timer and take */ 72279f4f8fSlionel.debieve@st.com /* care of initial delay time when enabling rng */ 73c6a97c42SDaniel Thompson if (!sr && wait) { 747cdc5e6bSTomas Marek int err; 757e11a4fcSTomas Marek 767cdc5e6bSTomas Marek err = readl_relaxed_poll_timeout_atomic(priv->base 77279f4f8fSlionel.debieve@st.com + RNG_SR, 78279f4f8fSlionel.debieve@st.com sr, sr, 79279f4f8fSlionel.debieve@st.com 10, 50000); 807cdc5e6bSTomas Marek if (err) 81279f4f8fSlionel.debieve@st.com dev_err((struct device *)priv->rng.priv, 82279f4f8fSlionel.debieve@st.com "%s: timeout %x!\n", __func__, sr); 83c6a97c42SDaniel Thompson } 84c6a97c42SDaniel Thompson 85c6a97c42SDaniel Thompson /* If error detected or data not ready... */ 861ff69adfSMaxime Coquelin if (sr != RNG_SR_DRDY) { 871ff69adfSMaxime Coquelin if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS), 881ff69adfSMaxime Coquelin "bad RNG status - %x\n", sr)) 891ff69adfSMaxime Coquelin writel_relaxed(0, priv->base + RNG_SR); 90c6a97c42SDaniel Thompson break; 911ff69adfSMaxime Coquelin } 92c6a97c42SDaniel Thompson 93c6a97c42SDaniel Thompson *(u32 *)data = readl_relaxed(priv->base + RNG_DR); 94c6a97c42SDaniel Thompson 95c6a97c42SDaniel Thompson retval += sizeof(u32); 96c6a97c42SDaniel Thompson data += sizeof(u32); 97c6a97c42SDaniel Thompson max -= sizeof(u32); 98c6a97c42SDaniel Thompson } 99c6a97c42SDaniel Thompson 100c6a97c42SDaniel Thompson pm_runtime_mark_last_busy((struct device *) priv->rng.priv); 101c6a97c42SDaniel Thompson pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv); 102c6a97c42SDaniel Thompson 103c6a97c42SDaniel Thompson return retval || !wait ? retval : -EIO; 104c6a97c42SDaniel Thompson } 105c6a97c42SDaniel Thompson 106c6a97c42SDaniel Thompson static int stm32_rng_init(struct hwrng *rng) 107c6a97c42SDaniel Thompson { 108c6a97c42SDaniel Thompson struct stm32_rng_private *priv = 109c6a97c42SDaniel Thompson container_of(rng, struct stm32_rng_private, rng); 110c6a97c42SDaniel Thompson int err; 111*6b85a7e1SGatien Chevallier u32 reg; 112c6a97c42SDaniel Thompson 113c6a97c42SDaniel Thompson err = clk_prepare_enable(priv->clk); 114c6a97c42SDaniel Thompson if (err) 115c6a97c42SDaniel Thompson return err; 116c6a97c42SDaniel Thompson 117c6a97c42SDaniel Thompson /* clear error indicators */ 118c6a97c42SDaniel Thompson writel_relaxed(0, priv->base + RNG_SR); 119c6a97c42SDaniel Thompson 120*6b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 121*6b85a7e1SGatien Chevallier 122*6b85a7e1SGatien Chevallier /* 123*6b85a7e1SGatien Chevallier * Keep default RNG configuration if none was specified. 124*6b85a7e1SGatien Chevallier * 0 is an invalid value as it disables all entropy sources. 125*6b85a7e1SGatien Chevallier */ 126*6b85a7e1SGatien Chevallier if (priv->data->has_cond_reset && priv->data->cr) { 127*6b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONFIG_MASK; 128*6b85a7e1SGatien Chevallier reg |= RNG_CR_CONDRST | (priv->data->cr & RNG_CR_ENTROPY_SRC_MASK); 129*6b85a7e1SGatien Chevallier if (priv->ced) 130*6b85a7e1SGatien Chevallier reg &= ~RNG_CR_CED; 131*6b85a7e1SGatien Chevallier else 132*6b85a7e1SGatien Chevallier reg |= RNG_CR_CED; 133*6b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 134*6b85a7e1SGatien Chevallier 135*6b85a7e1SGatien Chevallier /* Health tests and noise control registers */ 136*6b85a7e1SGatien Chevallier writel_relaxed(priv->data->htcr, priv->base + RNG_HTCR); 137*6b85a7e1SGatien Chevallier writel_relaxed(priv->data->nscr & RNG_NSCR_MASK, priv->base + RNG_NSCR); 138*6b85a7e1SGatien Chevallier 139*6b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONDRST; 140*6b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 141*6b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 142*6b85a7e1SGatien Chevallier 143*6b85a7e1SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg, 144*6b85a7e1SGatien Chevallier (!(reg & RNG_CR_CONDRST)), 145*6b85a7e1SGatien Chevallier 10, 50000); 146*6b85a7e1SGatien Chevallier if (err) { 147*6b85a7e1SGatien Chevallier dev_err((struct device *)priv->rng.priv, 148*6b85a7e1SGatien Chevallier "%s: timeout %x!\n", __func__, reg); 149*6b85a7e1SGatien Chevallier return -EINVAL; 150*6b85a7e1SGatien Chevallier } 151*6b85a7e1SGatien Chevallier } else { 152*6b85a7e1SGatien Chevallier /* Handle all RNG versions by checking if conditional reset should be set */ 153*6b85a7e1SGatien Chevallier if (priv->data->has_cond_reset) 154*6b85a7e1SGatien Chevallier reg |= RNG_CR_CONDRST; 155*6b85a7e1SGatien Chevallier 156*6b85a7e1SGatien Chevallier if (priv->ced) 157*6b85a7e1SGatien Chevallier reg &= ~RNG_CR_CED; 158*6b85a7e1SGatien Chevallier else 159*6b85a7e1SGatien Chevallier reg |= RNG_CR_CED; 160*6b85a7e1SGatien Chevallier 161*6b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 162*6b85a7e1SGatien Chevallier 163*6b85a7e1SGatien Chevallier if (priv->data->has_cond_reset) 164*6b85a7e1SGatien Chevallier reg &= ~RNG_CR_CONDRST; 165*6b85a7e1SGatien Chevallier 166*6b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 167*6b85a7e1SGatien Chevallier 168*6b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 169*6b85a7e1SGatien Chevallier } 170*6b85a7e1SGatien Chevallier 171*6b85a7e1SGatien Chevallier err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg, 172*6b85a7e1SGatien Chevallier reg & RNG_SR_DRDY, 173*6b85a7e1SGatien Chevallier 10, 100000); 174*6b85a7e1SGatien Chevallier if (err | (reg & ~RNG_SR_DRDY)) { 175*6b85a7e1SGatien Chevallier clk_disable_unprepare(priv->clk); 176*6b85a7e1SGatien Chevallier dev_err((struct device *)priv->rng.priv, 177*6b85a7e1SGatien Chevallier "%s: timeout:%x SR: %x!\n", __func__, err, reg); 178*6b85a7e1SGatien Chevallier return -EINVAL; 179*6b85a7e1SGatien Chevallier } 180*6b85a7e1SGatien Chevallier 181c6a97c42SDaniel Thompson return 0; 182c6a97c42SDaniel Thompson } 183c6a97c42SDaniel Thompson 184*6b85a7e1SGatien Chevallier static int stm32_rng_remove(struct platform_device *ofdev) 185c6a97c42SDaniel Thompson { 186*6b85a7e1SGatien Chevallier pm_runtime_disable(&ofdev->dev); 187c6a97c42SDaniel Thompson 188*6b85a7e1SGatien Chevallier return 0; 189c6a97c42SDaniel Thompson } 190c6a97c42SDaniel Thompson 191*6b85a7e1SGatien Chevallier #ifdef CONFIG_PM 192*6b85a7e1SGatien Chevallier static int stm32_rng_runtime_suspend(struct device *dev) 193*6b85a7e1SGatien Chevallier { 194*6b85a7e1SGatien Chevallier u32 reg; 195*6b85a7e1SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 196*6b85a7e1SGatien Chevallier 197*6b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 198*6b85a7e1SGatien Chevallier reg &= ~RNG_CR_RNGEN; 199*6b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 200*6b85a7e1SGatien Chevallier clk_disable_unprepare(priv->clk); 201*6b85a7e1SGatien Chevallier 202*6b85a7e1SGatien Chevallier return 0; 203*6b85a7e1SGatien Chevallier } 204*6b85a7e1SGatien Chevallier 205*6b85a7e1SGatien Chevallier static int stm32_rng_runtime_resume(struct device *dev) 206*6b85a7e1SGatien Chevallier { 207*6b85a7e1SGatien Chevallier u32 reg; 208*6b85a7e1SGatien Chevallier struct stm32_rng_private *priv = dev_get_drvdata(dev); 209*6b85a7e1SGatien Chevallier 210*6b85a7e1SGatien Chevallier clk_prepare_enable(priv->clk); 211*6b85a7e1SGatien Chevallier reg = readl_relaxed(priv->base + RNG_CR); 212*6b85a7e1SGatien Chevallier reg |= RNG_CR_RNGEN; 213*6b85a7e1SGatien Chevallier writel_relaxed(reg, priv->base + RNG_CR); 214*6b85a7e1SGatien Chevallier 215*6b85a7e1SGatien Chevallier return 0; 216*6b85a7e1SGatien Chevallier } 217*6b85a7e1SGatien Chevallier #endif 218*6b85a7e1SGatien Chevallier 219*6b85a7e1SGatien Chevallier static const struct dev_pm_ops stm32_rng_pm_ops = { 220*6b85a7e1SGatien Chevallier SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend, 221*6b85a7e1SGatien Chevallier stm32_rng_runtime_resume, NULL) 222*6b85a7e1SGatien Chevallier SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 223*6b85a7e1SGatien Chevallier pm_runtime_force_resume) 224*6b85a7e1SGatien Chevallier }; 225*6b85a7e1SGatien Chevallier 226*6b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32mp13_rng_data = { 227*6b85a7e1SGatien Chevallier .has_cond_reset = true, 228*6b85a7e1SGatien Chevallier .cr = 0x00F00D00, 229*6b85a7e1SGatien Chevallier .nscr = 0x2B5BB, 230*6b85a7e1SGatien Chevallier .htcr = 0x969D, 231*6b85a7e1SGatien Chevallier }; 232*6b85a7e1SGatien Chevallier 233*6b85a7e1SGatien Chevallier static const struct stm32_rng_data stm32_rng_data = { 234*6b85a7e1SGatien Chevallier .has_cond_reset = false, 235*6b85a7e1SGatien Chevallier }; 236*6b85a7e1SGatien Chevallier 237*6b85a7e1SGatien Chevallier static const struct of_device_id stm32_rng_match[] = { 238*6b85a7e1SGatien Chevallier { 239*6b85a7e1SGatien Chevallier .compatible = "st,stm32mp13-rng", 240*6b85a7e1SGatien Chevallier .data = &stm32mp13_rng_data, 241*6b85a7e1SGatien Chevallier }, 242*6b85a7e1SGatien Chevallier { 243*6b85a7e1SGatien Chevallier .compatible = "st,stm32-rng", 244*6b85a7e1SGatien Chevallier .data = &stm32_rng_data, 245*6b85a7e1SGatien Chevallier }, 246*6b85a7e1SGatien Chevallier {}, 247*6b85a7e1SGatien Chevallier }; 248*6b85a7e1SGatien Chevallier MODULE_DEVICE_TABLE(of, stm32_rng_match); 249*6b85a7e1SGatien Chevallier 250c6a97c42SDaniel Thompson static int stm32_rng_probe(struct platform_device *ofdev) 251c6a97c42SDaniel Thompson { 252c6a97c42SDaniel Thompson struct device *dev = &ofdev->dev; 253c6a97c42SDaniel Thompson struct device_node *np = ofdev->dev.of_node; 254c6a97c42SDaniel Thompson struct stm32_rng_private *priv; 25518d9a826SGatien Chevallier struct resource *res; 256c6a97c42SDaniel Thompson 257c6a97c42SDaniel Thompson priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL); 258c6a97c42SDaniel Thompson if (!priv) 259c6a97c42SDaniel Thompson return -ENOMEM; 260c6a97c42SDaniel Thompson 26118d9a826SGatien Chevallier priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res); 262c6a97c42SDaniel Thompson if (IS_ERR(priv->base)) 263c6a97c42SDaniel Thompson return PTR_ERR(priv->base); 264c6a97c42SDaniel Thompson 265c6a97c42SDaniel Thompson priv->clk = devm_clk_get(&ofdev->dev, NULL); 266c6a97c42SDaniel Thompson if (IS_ERR(priv->clk)) 267c6a97c42SDaniel Thompson return PTR_ERR(priv->clk); 268c6a97c42SDaniel Thompson 269326ed382Slionel.debieve@st.com priv->rst = devm_reset_control_get(&ofdev->dev, NULL); 270326ed382Slionel.debieve@st.com if (!IS_ERR(priv->rst)) { 271326ed382Slionel.debieve@st.com reset_control_assert(priv->rst); 272326ed382Slionel.debieve@st.com udelay(2); 273326ed382Slionel.debieve@st.com reset_control_deassert(priv->rst); 274326ed382Slionel.debieve@st.com } 275326ed382Slionel.debieve@st.com 276529571edSlionel.debieve@st.com priv->ced = of_property_read_bool(np, "clock-error-detect"); 277529571edSlionel.debieve@st.com 278*6b85a7e1SGatien Chevallier priv->data = of_device_get_match_data(dev); 279*6b85a7e1SGatien Chevallier if (!priv->data) 280*6b85a7e1SGatien Chevallier return -ENODEV; 281*6b85a7e1SGatien Chevallier 282c6a97c42SDaniel Thompson dev_set_drvdata(dev, priv); 283c6a97c42SDaniel Thompson 284436cdcdeSJulia Lawall priv->rng.name = dev_driver_string(dev); 285436cdcdeSJulia Lawall priv->rng.init = stm32_rng_init; 286436cdcdeSJulia Lawall priv->rng.read = stm32_rng_read; 287c6a97c42SDaniel Thompson priv->rng.priv = (unsigned long) dev; 28838a1965fSLionel Debieve priv->rng.quality = 900; 289c6a97c42SDaniel Thompson 290c6a97c42SDaniel Thompson pm_runtime_set_autosuspend_delay(dev, 100); 291c6a97c42SDaniel Thompson pm_runtime_use_autosuspend(dev); 292c6a97c42SDaniel Thompson pm_runtime_enable(dev); 293c6a97c42SDaniel Thompson 294c6a97c42SDaniel Thompson return devm_hwrng_register(dev, &priv->rng); 295c6a97c42SDaniel Thompson } 296c6a97c42SDaniel Thompson 297c6a97c42SDaniel Thompson static struct platform_driver stm32_rng_driver = { 298c6a97c42SDaniel Thompson .driver = { 299c6a97c42SDaniel Thompson .name = "stm32-rng", 300c6a97c42SDaniel Thompson .pm = &stm32_rng_pm_ops, 301c6a97c42SDaniel Thompson .of_match_table = stm32_rng_match, 302c6a97c42SDaniel Thompson }, 303c6a97c42SDaniel Thompson .probe = stm32_rng_probe, 304af0d4442SLionel Debieve .remove = stm32_rng_remove, 305c6a97c42SDaniel Thompson }; 306c6a97c42SDaniel Thompson 307c6a97c42SDaniel Thompson module_platform_driver(stm32_rng_driver); 308c6a97c42SDaniel Thompson 309c6a97c42SDaniel Thompson MODULE_LICENSE("GPL"); 310c6a97c42SDaniel Thompson MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>"); 311c6a97c42SDaniel Thompson MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver"); 312