1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2015, Daniel Thompson 4 */ 5 6 #include <linux/clk.h> 7 #include <linux/delay.h> 8 #include <linux/hw_random.h> 9 #include <linux/io.h> 10 #include <linux/iopoll.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/of.h> 14 #include <linux/of_address.h> 15 #include <linux/platform_device.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/reset.h> 18 #include <linux/slab.h> 19 20 #define RNG_CR 0x00 21 #define RNG_CR_RNGEN BIT(2) 22 #define RNG_CR_CED BIT(5) 23 24 #define RNG_SR 0x04 25 #define RNG_SR_SEIS BIT(6) 26 #define RNG_SR_CEIS BIT(5) 27 #define RNG_SR_DRDY BIT(0) 28 29 #define RNG_DR 0x08 30 31 struct stm32_rng_private { 32 struct hwrng rng; 33 void __iomem *base; 34 struct clk *clk; 35 struct reset_control *rst; 36 bool ced; 37 }; 38 39 static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 40 { 41 struct stm32_rng_private *priv = 42 container_of(rng, struct stm32_rng_private, rng); 43 u32 sr; 44 int retval = 0; 45 46 pm_runtime_get_sync((struct device *) priv->rng.priv); 47 48 while (max >= sizeof(u32)) { 49 sr = readl_relaxed(priv->base + RNG_SR); 50 /* Manage timeout which is based on timer and take */ 51 /* care of initial delay time when enabling rng */ 52 if (!sr && wait) { 53 int err; 54 55 err = readl_relaxed_poll_timeout_atomic(priv->base 56 + RNG_SR, 57 sr, sr, 58 10, 50000); 59 if (err) 60 dev_err((struct device *)priv->rng.priv, 61 "%s: timeout %x!\n", __func__, sr); 62 } 63 64 /* If error detected or data not ready... */ 65 if (sr != RNG_SR_DRDY) { 66 if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS), 67 "bad RNG status - %x\n", sr)) 68 writel_relaxed(0, priv->base + RNG_SR); 69 break; 70 } 71 72 *(u32 *)data = readl_relaxed(priv->base + RNG_DR); 73 74 retval += sizeof(u32); 75 data += sizeof(u32); 76 max -= sizeof(u32); 77 } 78 79 pm_runtime_mark_last_busy((struct device *) priv->rng.priv); 80 pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv); 81 82 return retval || !wait ? retval : -EIO; 83 } 84 85 static int stm32_rng_init(struct hwrng *rng) 86 { 87 struct stm32_rng_private *priv = 88 container_of(rng, struct stm32_rng_private, rng); 89 int err; 90 91 err = clk_prepare_enable(priv->clk); 92 if (err) 93 return err; 94 95 if (priv->ced) 96 writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR); 97 else 98 writel_relaxed(RNG_CR_RNGEN | RNG_CR_CED, 99 priv->base + RNG_CR); 100 101 /* clear error indicators */ 102 writel_relaxed(0, priv->base + RNG_SR); 103 104 return 0; 105 } 106 107 static void stm32_rng_cleanup(struct hwrng *rng) 108 { 109 struct stm32_rng_private *priv = 110 container_of(rng, struct stm32_rng_private, rng); 111 112 writel_relaxed(0, priv->base + RNG_CR); 113 clk_disable_unprepare(priv->clk); 114 } 115 116 static int stm32_rng_probe(struct platform_device *ofdev) 117 { 118 struct device *dev = &ofdev->dev; 119 struct device_node *np = ofdev->dev.of_node; 120 struct stm32_rng_private *priv; 121 struct resource res; 122 int err; 123 124 priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL); 125 if (!priv) 126 return -ENOMEM; 127 128 err = of_address_to_resource(np, 0, &res); 129 if (err) 130 return err; 131 132 priv->base = devm_ioremap_resource(dev, &res); 133 if (IS_ERR(priv->base)) 134 return PTR_ERR(priv->base); 135 136 priv->clk = devm_clk_get(&ofdev->dev, NULL); 137 if (IS_ERR(priv->clk)) 138 return PTR_ERR(priv->clk); 139 140 priv->rst = devm_reset_control_get(&ofdev->dev, NULL); 141 if (!IS_ERR(priv->rst)) { 142 reset_control_assert(priv->rst); 143 udelay(2); 144 reset_control_deassert(priv->rst); 145 } 146 147 priv->ced = of_property_read_bool(np, "clock-error-detect"); 148 149 dev_set_drvdata(dev, priv); 150 151 priv->rng.name = dev_driver_string(dev); 152 #ifndef CONFIG_PM 153 priv->rng.init = stm32_rng_init; 154 priv->rng.cleanup = stm32_rng_cleanup; 155 #endif 156 priv->rng.read = stm32_rng_read; 157 priv->rng.priv = (unsigned long) dev; 158 priv->rng.quality = 900; 159 160 pm_runtime_set_autosuspend_delay(dev, 100); 161 pm_runtime_use_autosuspend(dev); 162 pm_runtime_enable(dev); 163 164 return devm_hwrng_register(dev, &priv->rng); 165 } 166 167 static int stm32_rng_remove(struct platform_device *ofdev) 168 { 169 pm_runtime_disable(&ofdev->dev); 170 171 return 0; 172 } 173 174 #ifdef CONFIG_PM 175 static int stm32_rng_runtime_suspend(struct device *dev) 176 { 177 struct stm32_rng_private *priv = dev_get_drvdata(dev); 178 179 stm32_rng_cleanup(&priv->rng); 180 181 return 0; 182 } 183 184 static int stm32_rng_runtime_resume(struct device *dev) 185 { 186 struct stm32_rng_private *priv = dev_get_drvdata(dev); 187 188 return stm32_rng_init(&priv->rng); 189 } 190 #endif 191 192 static const struct dev_pm_ops stm32_rng_pm_ops = { 193 SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend, 194 stm32_rng_runtime_resume, NULL) 195 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 196 pm_runtime_force_resume) 197 }; 198 199 200 static const struct of_device_id stm32_rng_match[] = { 201 { 202 .compatible = "st,stm32-rng", 203 }, 204 {}, 205 }; 206 MODULE_DEVICE_TABLE(of, stm32_rng_match); 207 208 static struct platform_driver stm32_rng_driver = { 209 .driver = { 210 .name = "stm32-rng", 211 .pm = &stm32_rng_pm_ops, 212 .of_match_table = stm32_rng_match, 213 }, 214 .probe = stm32_rng_probe, 215 .remove = stm32_rng_remove, 216 }; 217 218 module_platform_driver(stm32_rng_driver); 219 220 MODULE_LICENSE("GPL"); 221 MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>"); 222 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver"); 223