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) 23c6a97c42SDaniel Thompson 24c6a97c42SDaniel Thompson #define RNG_SR 0x04 25c6a97c42SDaniel Thompson #define RNG_SR_SEIS BIT(6) 26c6a97c42SDaniel Thompson #define RNG_SR_CEIS BIT(5) 27c6a97c42SDaniel Thompson #define RNG_SR_DRDY BIT(0) 28c6a97c42SDaniel Thompson 29c6a97c42SDaniel Thompson #define RNG_DR 0x08 30c6a97c42SDaniel Thompson 31c6a97c42SDaniel Thompson struct stm32_rng_private { 32c6a97c42SDaniel Thompson struct hwrng rng; 33c6a97c42SDaniel Thompson void __iomem *base; 34c6a97c42SDaniel Thompson struct clk *clk; 35326ed382Slionel.debieve@st.com struct reset_control *rst; 36529571edSlionel.debieve@st.com bool ced; 37c6a97c42SDaniel Thompson }; 38c6a97c42SDaniel Thompson 39c6a97c42SDaniel Thompson static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 40c6a97c42SDaniel Thompson { 41c6a97c42SDaniel Thompson struct stm32_rng_private *priv = 42c6a97c42SDaniel Thompson container_of(rng, struct stm32_rng_private, rng); 43c6a97c42SDaniel Thompson u32 sr; 44c6a97c42SDaniel Thompson int retval = 0; 45c6a97c42SDaniel Thompson 46c6a97c42SDaniel Thompson pm_runtime_get_sync((struct device *) priv->rng.priv); 47c6a97c42SDaniel Thompson 48e64f57e8STomas Marek while (max >= sizeof(u32)) { 49c6a97c42SDaniel Thompson sr = readl_relaxed(priv->base + RNG_SR); 50279f4f8fSlionel.debieve@st.com /* Manage timeout which is based on timer and take */ 51279f4f8fSlionel.debieve@st.com /* care of initial delay time when enabling rng */ 52c6a97c42SDaniel Thompson if (!sr && wait) { 537cdc5e6bSTomas Marek int err; 547e11a4fcSTomas Marek 557cdc5e6bSTomas Marek err = readl_relaxed_poll_timeout_atomic(priv->base 56279f4f8fSlionel.debieve@st.com + RNG_SR, 57279f4f8fSlionel.debieve@st.com sr, sr, 58279f4f8fSlionel.debieve@st.com 10, 50000); 597cdc5e6bSTomas Marek if (err) 60279f4f8fSlionel.debieve@st.com dev_err((struct device *)priv->rng.priv, 61279f4f8fSlionel.debieve@st.com "%s: timeout %x!\n", __func__, sr); 62c6a97c42SDaniel Thompson } 63c6a97c42SDaniel Thompson 64c6a97c42SDaniel Thompson /* If error detected or data not ready... */ 651ff69adfSMaxime Coquelin if (sr != RNG_SR_DRDY) { 661ff69adfSMaxime Coquelin if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS), 671ff69adfSMaxime Coquelin "bad RNG status - %x\n", sr)) 681ff69adfSMaxime Coquelin writel_relaxed(0, priv->base + RNG_SR); 69c6a97c42SDaniel Thompson break; 701ff69adfSMaxime Coquelin } 71c6a97c42SDaniel Thompson 72c6a97c42SDaniel Thompson *(u32 *)data = readl_relaxed(priv->base + RNG_DR); 73c6a97c42SDaniel Thompson 74c6a97c42SDaniel Thompson retval += sizeof(u32); 75c6a97c42SDaniel Thompson data += sizeof(u32); 76c6a97c42SDaniel Thompson max -= sizeof(u32); 77c6a97c42SDaniel Thompson } 78c6a97c42SDaniel Thompson 79c6a97c42SDaniel Thompson pm_runtime_mark_last_busy((struct device *) priv->rng.priv); 80c6a97c42SDaniel Thompson pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv); 81c6a97c42SDaniel Thompson 82c6a97c42SDaniel Thompson return retval || !wait ? retval : -EIO; 83c6a97c42SDaniel Thompson } 84c6a97c42SDaniel Thompson 85c6a97c42SDaniel Thompson static int stm32_rng_init(struct hwrng *rng) 86c6a97c42SDaniel Thompson { 87c6a97c42SDaniel Thompson struct stm32_rng_private *priv = 88c6a97c42SDaniel Thompson container_of(rng, struct stm32_rng_private, rng); 89c6a97c42SDaniel Thompson int err; 90c6a97c42SDaniel Thompson 91c6a97c42SDaniel Thompson err = clk_prepare_enable(priv->clk); 92c6a97c42SDaniel Thompson if (err) 93c6a97c42SDaniel Thompson return err; 94c6a97c42SDaniel Thompson 95529571edSlionel.debieve@st.com if (priv->ced) 96c6a97c42SDaniel Thompson writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR); 97529571edSlionel.debieve@st.com else 98529571edSlionel.debieve@st.com writel_relaxed(RNG_CR_RNGEN | RNG_CR_CED, 99529571edSlionel.debieve@st.com priv->base + RNG_CR); 100c6a97c42SDaniel Thompson 101c6a97c42SDaniel Thompson /* clear error indicators */ 102c6a97c42SDaniel Thompson writel_relaxed(0, priv->base + RNG_SR); 103c6a97c42SDaniel Thompson 104c6a97c42SDaniel Thompson return 0; 105c6a97c42SDaniel Thompson } 106c6a97c42SDaniel Thompson 107c6a97c42SDaniel Thompson static void stm32_rng_cleanup(struct hwrng *rng) 108c6a97c42SDaniel Thompson { 109c6a97c42SDaniel Thompson struct stm32_rng_private *priv = 110c6a97c42SDaniel Thompson container_of(rng, struct stm32_rng_private, rng); 111c6a97c42SDaniel Thompson 112c6a97c42SDaniel Thompson writel_relaxed(0, priv->base + RNG_CR); 113c6a97c42SDaniel Thompson clk_disable_unprepare(priv->clk); 114c6a97c42SDaniel Thompson } 115c6a97c42SDaniel Thompson 116c6a97c42SDaniel Thompson static int stm32_rng_probe(struct platform_device *ofdev) 117c6a97c42SDaniel Thompson { 118c6a97c42SDaniel Thompson struct device *dev = &ofdev->dev; 119c6a97c42SDaniel Thompson struct device_node *np = ofdev->dev.of_node; 120c6a97c42SDaniel Thompson struct stm32_rng_private *priv; 121*18d9a826SGatien Chevallier struct resource *res; 122c6a97c42SDaniel Thompson 123c6a97c42SDaniel Thompson priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL); 124c6a97c42SDaniel Thompson if (!priv) 125c6a97c42SDaniel Thompson return -ENOMEM; 126c6a97c42SDaniel Thompson 127*18d9a826SGatien Chevallier priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res); 128c6a97c42SDaniel Thompson if (IS_ERR(priv->base)) 129c6a97c42SDaniel Thompson return PTR_ERR(priv->base); 130c6a97c42SDaniel Thompson 131c6a97c42SDaniel Thompson priv->clk = devm_clk_get(&ofdev->dev, NULL); 132c6a97c42SDaniel Thompson if (IS_ERR(priv->clk)) 133c6a97c42SDaniel Thompson return PTR_ERR(priv->clk); 134c6a97c42SDaniel Thompson 135326ed382Slionel.debieve@st.com priv->rst = devm_reset_control_get(&ofdev->dev, NULL); 136326ed382Slionel.debieve@st.com if (!IS_ERR(priv->rst)) { 137326ed382Slionel.debieve@st.com reset_control_assert(priv->rst); 138326ed382Slionel.debieve@st.com udelay(2); 139326ed382Slionel.debieve@st.com reset_control_deassert(priv->rst); 140326ed382Slionel.debieve@st.com } 141326ed382Slionel.debieve@st.com 142529571edSlionel.debieve@st.com priv->ced = of_property_read_bool(np, "clock-error-detect"); 143529571edSlionel.debieve@st.com 144c6a97c42SDaniel Thompson dev_set_drvdata(dev, priv); 145c6a97c42SDaniel Thompson 146436cdcdeSJulia Lawall priv->rng.name = dev_driver_string(dev); 147c6a97c42SDaniel Thompson #ifndef CONFIG_PM 148436cdcdeSJulia Lawall priv->rng.init = stm32_rng_init; 149436cdcdeSJulia Lawall priv->rng.cleanup = stm32_rng_cleanup; 150c6a97c42SDaniel Thompson #endif 151436cdcdeSJulia Lawall priv->rng.read = stm32_rng_read; 152c6a97c42SDaniel Thompson priv->rng.priv = (unsigned long) dev; 15338a1965fSLionel Debieve priv->rng.quality = 900; 154c6a97c42SDaniel Thompson 155c6a97c42SDaniel Thompson pm_runtime_set_autosuspend_delay(dev, 100); 156c6a97c42SDaniel Thompson pm_runtime_use_autosuspend(dev); 157c6a97c42SDaniel Thompson pm_runtime_enable(dev); 158c6a97c42SDaniel Thompson 159c6a97c42SDaniel Thompson return devm_hwrng_register(dev, &priv->rng); 160c6a97c42SDaniel Thompson } 161c6a97c42SDaniel Thompson 162af0d4442SLionel Debieve static int stm32_rng_remove(struct platform_device *ofdev) 163af0d4442SLionel Debieve { 164af0d4442SLionel Debieve pm_runtime_disable(&ofdev->dev); 165af0d4442SLionel Debieve 166af0d4442SLionel Debieve return 0; 167af0d4442SLionel Debieve } 168af0d4442SLionel Debieve 169c6a97c42SDaniel Thompson #ifdef CONFIG_PM 170c6a97c42SDaniel Thompson static int stm32_rng_runtime_suspend(struct device *dev) 171c6a97c42SDaniel Thompson { 172d6ba06b8SDaniel Thompson struct stm32_rng_private *priv = dev_get_drvdata(dev); 173c6a97c42SDaniel Thompson 174c6a97c42SDaniel Thompson stm32_rng_cleanup(&priv->rng); 175c6a97c42SDaniel Thompson 176c6a97c42SDaniel Thompson return 0; 177c6a97c42SDaniel Thompson } 178c6a97c42SDaniel Thompson 179c6a97c42SDaniel Thompson static int stm32_rng_runtime_resume(struct device *dev) 180c6a97c42SDaniel Thompson { 181d6ba06b8SDaniel Thompson struct stm32_rng_private *priv = dev_get_drvdata(dev); 182c6a97c42SDaniel Thompson 183c6a97c42SDaniel Thompson return stm32_rng_init(&priv->rng); 184c6a97c42SDaniel Thompson } 185c6a97c42SDaniel Thompson #endif 186c6a97c42SDaniel Thompson 1879bae5494Slionel.debieve@st.com static const struct dev_pm_ops stm32_rng_pm_ops = { 1889bae5494Slionel.debieve@st.com SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend, 1899bae5494Slionel.debieve@st.com stm32_rng_runtime_resume, NULL) 1909bae5494Slionel.debieve@st.com SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1919bae5494Slionel.debieve@st.com pm_runtime_force_resume) 1929bae5494Slionel.debieve@st.com }; 1939bae5494Slionel.debieve@st.com 194c6a97c42SDaniel Thompson 195c6a97c42SDaniel Thompson static const struct of_device_id stm32_rng_match[] = { 196c6a97c42SDaniel Thompson { 197c6a97c42SDaniel Thompson .compatible = "st,stm32-rng", 198c6a97c42SDaniel Thompson }, 199c6a97c42SDaniel Thompson {}, 200c6a97c42SDaniel Thompson }; 201c6a97c42SDaniel Thompson MODULE_DEVICE_TABLE(of, stm32_rng_match); 202c6a97c42SDaniel Thompson 203c6a97c42SDaniel Thompson static struct platform_driver stm32_rng_driver = { 204c6a97c42SDaniel Thompson .driver = { 205c6a97c42SDaniel Thompson .name = "stm32-rng", 206c6a97c42SDaniel Thompson .pm = &stm32_rng_pm_ops, 207c6a97c42SDaniel Thompson .of_match_table = stm32_rng_match, 208c6a97c42SDaniel Thompson }, 209c6a97c42SDaniel Thompson .probe = stm32_rng_probe, 210af0d4442SLionel Debieve .remove = stm32_rng_remove, 211c6a97c42SDaniel Thompson }; 212c6a97c42SDaniel Thompson 213c6a97c42SDaniel Thompson module_platform_driver(stm32_rng_driver); 214c6a97c42SDaniel Thompson 215c6a97c42SDaniel Thompson MODULE_LICENSE("GPL"); 216c6a97c42SDaniel Thompson MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>"); 217c6a97c42SDaniel Thompson MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver"); 218