Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2019, Linaro Limited |
| 4 | */ |
| 5 | |
Heinrich Schuchardt | d909bab | 2020-09-17 16:49:02 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_RNG |
| 7 | |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <clk.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 12 | #include <reset.h> |
| 13 | #include <rng.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 16 | |
| 17 | #include <asm/io.h> |
| 18 | #include <linux/iopoll.h> |
| 19 | #include <linux/kernel.h> |
| 20 | |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 21 | #define RNG_CR 0x00 |
| 22 | #define RNG_CR_RNGEN BIT(2) |
| 23 | #define RNG_CR_CED BIT(5) |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 24 | #define RNG_CR_CONFIG1 GENMASK(11, 8) |
| 25 | #define RNG_CR_NISTC BIT(12) |
| 26 | #define RNG_CR_CONFIG2 GENMASK(15, 13) |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 27 | #define RNG_CR_CLKDIV_SHIFT 16 |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 28 | #define RNG_CR_CLKDIV GENMASK(19, 16) |
| 29 | #define RNG_CR_CONFIG3 GENMASK(25, 20) |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 30 | #define RNG_CR_CONDRST BIT(30) |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 31 | #define RNG_CR_ENTROPY_SRC_MASK (RNG_CR_CONFIG1 | RNG_CR_NISTC | RNG_CR_CONFIG2 | RNG_CR_CONFIG3) |
| 32 | #define RNG_CR_CONFIG_MASK (RNG_CR_ENTROPY_SRC_MASK | RNG_CR_CED | RNG_CR_CLKDIV) |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 33 | |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 34 | #define RNG_SR 0x04 |
| 35 | #define RNG_SR_SEIS BIT(6) |
| 36 | #define RNG_SR_CEIS BIT(5) |
| 37 | #define RNG_SR_SECS BIT(2) |
| 38 | #define RNG_SR_DRDY BIT(0) |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 39 | |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 40 | #define RNG_DR 0x08 |
| 41 | |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 42 | #define RNG_NSCR 0x0C |
| 43 | #define RNG_NSCR_MASK GENMASK(17, 0) |
| 44 | |
| 45 | #define RNG_HTCR 0x10 |
| 46 | |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 47 | #define RNG_NB_RECOVER_TRIES 3 |
| 48 | |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 49 | /* |
| 50 | * struct stm32_rng_data - RNG compat data |
| 51 | * |
| 52 | * @max_clock_rate: Max RNG clock frequency, in Hertz |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 53 | * @cr: Entropy source configuration |
| 54 | * @nscr: Noice sources control configuration |
| 55 | * @htcr: Health tests configuration |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 56 | * @has_cond_reset: True if conditionnal reset is supported |
| 57 | * |
| 58 | */ |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 59 | struct stm32_rng_data { |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 60 | uint max_clock_rate; |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 61 | u32 cr; |
| 62 | u32 nscr; |
| 63 | u32 htcr; |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 64 | bool has_cond_reset; |
| 65 | }; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 66 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 67 | struct stm32_rng_plat { |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 68 | fdt_addr_t base; |
| 69 | struct clk clk; |
| 70 | struct reset_ctl rst; |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 71 | const struct stm32_rng_data *data; |
Gatien Chevallier | e2acc95 | 2023-09-19 17:27:55 +0200 | [diff] [blame] | 72 | bool ced; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 73 | }; |
| 74 | |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Extracts from the STM32 RNG specification when RNG supports CONDRST. |
| 77 | * |
| 78 | * When a noise source (or seed) error occurs, the RNG stops generating |
| 79 | * random numbers and sets to “1” both SEIS and SECS bits to indicate |
| 80 | * that a seed error occurred. (...) |
| 81 | * |
| 82 | * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield |
| 83 | * description for details). This step is needed only if SECS is set. |
| 84 | * Indeed, when SEIS is set and SECS is cleared it means RNG performed |
| 85 | * the reset automatically (auto-reset). |
| 86 | * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST |
| 87 | * to be cleared in the RNG_CR register, then confirm that SEIS is |
| 88 | * cleared in the RNG_SR register. Otherwise just clear SEIS bit in |
| 89 | * the RNG_SR register. |
| 90 | * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be |
| 91 | * cleared by RNG. The random number generation is now back to normal. |
| 92 | */ |
| 93 | static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_plat *pdata) |
| 94 | { |
| 95 | u32 sr = readl_relaxed(pdata->base + RNG_SR); |
| 96 | u32 cr = readl_relaxed(pdata->base + RNG_CR); |
| 97 | int err; |
| 98 | |
| 99 | if (sr & RNG_SR_SECS) { |
| 100 | /* Conceal by resetting the subsystem (step 1.) */ |
| 101 | writel_relaxed(cr | RNG_CR_CONDRST, pdata->base + RNG_CR); |
| 102 | writel_relaxed(cr & ~RNG_CR_CONDRST, pdata->base + RNG_CR); |
| 103 | } else { |
| 104 | /* RNG auto-reset (step 2.) */ |
| 105 | writel_relaxed(sr & ~RNG_SR_SEIS, pdata->base + RNG_SR); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | err = readl_relaxed_poll_timeout(pdata->base + RNG_SR, sr, !(sr & RNG_CR_CONDRST), 100000); |
| 110 | if (err) { |
| 111 | log_err("%s: timeout %x\n", __func__, sr); |
| 112 | return err; |
| 113 | } |
| 114 | |
| 115 | /* Check SEIS is cleared (step 2.) */ |
| 116 | if (readl_relaxed(pdata->base + RNG_SR) & RNG_SR_SEIS) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | err = readl_relaxed_poll_timeout(pdata->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 100000); |
| 120 | if (err) { |
| 121 | log_err("%s: timeout %x\n", __func__, sr); |
| 122 | return err; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Extracts from the STM32 RNG specification, when CONDRST is not supported |
| 130 | * |
| 131 | * When a noise source (or seed) error occurs, the RNG stops generating |
| 132 | * random numbers and sets to “1” both SEIS and SECS bits to indicate |
| 133 | * that a seed error occurred. (...) |
| 134 | * |
| 135 | * The following sequence shall be used to fully recover from a seed |
| 136 | * error after the RNG initialization: |
| 137 | * 1. Clear the SEIS bit by writing it to “0”. |
| 138 | * 2. Read out 12 words from the RNG_DR register, and discard each of |
| 139 | * them in order to clean the pipeline. |
| 140 | * 3. Confirm that SEIS is still cleared. Random number generation is |
| 141 | * back to normal. |
| 142 | */ |
| 143 | static int stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_plat *pdata) |
| 144 | { |
| 145 | uint i = 0; |
| 146 | u32 sr = readl_relaxed(pdata->base + RNG_SR); |
| 147 | |
| 148 | writel_relaxed(sr & ~RNG_SR_SEIS, pdata->base + RNG_SR); |
| 149 | |
| 150 | for (i = 12; i != 0; i--) |
| 151 | (void)readl_relaxed(pdata->base + RNG_DR); |
| 152 | |
| 153 | if (readl_relaxed(pdata->base + RNG_SR) & RNG_SR_SEIS) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int stm32_rng_conceal_seed_error(struct stm32_rng_plat *pdata) |
| 160 | { |
| 161 | log_debug("Concealing RNG seed error\n"); |
| 162 | |
| 163 | if (pdata->data->has_cond_reset) |
| 164 | return stm32_rng_conceal_seed_error_cond_reset(pdata); |
| 165 | else |
| 166 | return stm32_rng_conceal_seed_error_sw_reset(pdata); |
| 167 | }; |
| 168 | |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 169 | static int stm32_rng_read(struct udevice *dev, void *data, size_t len) |
| 170 | { |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 171 | int retval; |
| 172 | u32 sr, reg; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 173 | size_t increment; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 174 | struct stm32_rng_plat *pdata = dev_get_plat(dev); |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 175 | uint tries = 0; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 176 | |
| 177 | while (len > 0) { |
| 178 | retval = readl_poll_timeout(pdata->base + RNG_SR, sr, |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 179 | sr, 10000); |
| 180 | if (retval) { |
| 181 | log_err("%s: Timeout RNG no data", __func__); |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 182 | return retval; |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (sr != RNG_SR_DRDY) { |
| 186 | if (sr & RNG_SR_SEIS) { |
| 187 | retval = stm32_rng_conceal_seed_error(pdata); |
| 188 | tries++; |
| 189 | if (retval || tries > RNG_NB_RECOVER_TRIES) { |
| 190 | log_err("%s: Couldn't recover from seed error", __func__); |
| 191 | return -ENOTRECOVERABLE; |
| 192 | } |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 193 | |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 194 | /* Start again */ |
| 195 | continue; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 196 | } |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 197 | |
| 198 | if (sr & RNG_SR_CEIS) { |
| 199 | log_info("RNG clock too slow"); |
| 200 | writel_relaxed(0, pdata->base + RNG_SR); |
| 201 | } |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Once the DRDY bit is set, the RNG_DR register can |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 206 | * be read up to four consecutive times. |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 207 | */ |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 208 | reg = readl(pdata->base + RNG_DR); |
| 209 | /* Late seed error case: DR being 0 is an error status */ |
| 210 | if (!reg) { |
| 211 | retval = stm32_rng_conceal_seed_error(pdata); |
| 212 | tries++; |
| 213 | |
| 214 | if (retval || tries > RNG_NB_RECOVER_TRIES) { |
| 215 | log_err("%s: Couldn't recover from seed error", __func__); |
| 216 | return -ENOTRECOVERABLE; |
| 217 | } |
| 218 | |
| 219 | /* Start again */ |
| 220 | continue; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 221 | } |
Gatien Chevallier | 278ed19 | 2023-09-19 17:27:57 +0200 | [diff] [blame] | 222 | |
| 223 | increment = min(len, sizeof(u32)); |
| 224 | memcpy(data, ®, increment); |
| 225 | data += increment; |
| 226 | len -= increment; |
| 227 | |
| 228 | tries = 0; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 234 | static uint stm32_rng_clock_freq_restrain(struct stm32_rng_plat *pdata) |
| 235 | { |
| 236 | ulong clock_rate = 0; |
| 237 | uint clock_div = 0; |
| 238 | |
| 239 | clock_rate = clk_get_rate(&pdata->clk); |
| 240 | |
| 241 | /* |
| 242 | * Get the exponent to apply on the CLKDIV field in RNG_CR register. |
| 243 | * No need to handle the case when clock-div > 0xF as it is physically |
| 244 | * impossible. |
| 245 | */ |
| 246 | while ((clock_rate >> clock_div) > pdata->data->max_clock_rate) |
| 247 | clock_div++; |
| 248 | |
| 249 | log_debug("RNG clk rate : %lu\n", clk_get_rate(&pdata->clk) >> clock_div); |
| 250 | |
| 251 | return clock_div; |
| 252 | } |
| 253 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 254 | static int stm32_rng_init(struct stm32_rng_plat *pdata) |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 255 | { |
| 256 | int err; |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 257 | u32 cr, sr; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 258 | |
| 259 | err = clk_enable(&pdata->clk); |
| 260 | if (err) |
| 261 | return err; |
| 262 | |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 263 | cr = readl(pdata->base + RNG_CR); |
| 264 | |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 265 | /* |
| 266 | * Keep default RNG configuration if none was specified, that is when conf.cr is set to 0. |
| 267 | */ |
| 268 | if (pdata->data->has_cond_reset && pdata->data->cr) { |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 269 | uint clock_div = stm32_rng_clock_freq_restrain(pdata); |
| 270 | |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 271 | cr &= ~RNG_CR_CONFIG_MASK; |
| 272 | cr |= RNG_CR_CONDRST | (pdata->data->cr & RNG_CR_ENTROPY_SRC_MASK) | |
| 273 | (clock_div << RNG_CR_CLKDIV_SHIFT); |
Gatien Chevallier | e2acc95 | 2023-09-19 17:27:55 +0200 | [diff] [blame] | 274 | if (pdata->ced) |
| 275 | cr &= ~RNG_CR_CED; |
| 276 | else |
| 277 | cr |= RNG_CR_CED; |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 278 | writel(cr, pdata->base + RNG_CR); |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 279 | |
| 280 | /* Health tests and noise control registers */ |
| 281 | writel_relaxed(pdata->data->htcr, pdata->base + RNG_HTCR); |
| 282 | writel_relaxed(pdata->data->nscr & RNG_NSCR_MASK, pdata->base + RNG_NSCR); |
| 283 | |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 284 | cr &= ~RNG_CR_CONDRST; |
Gatien Chevallier | e2acc95 | 2023-09-19 17:27:55 +0200 | [diff] [blame] | 285 | cr |= RNG_CR_RNGEN; |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 286 | writel(cr, pdata->base + RNG_CR); |
| 287 | err = readl_poll_timeout(pdata->base + RNG_CR, cr, |
| 288 | (!(cr & RNG_CR_CONDRST)), 10000); |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 289 | if (err) { |
| 290 | log_err("%s: Timeout!", __func__); |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 291 | return err; |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 292 | } |
Gatien Chevallier | e2acc95 | 2023-09-19 17:27:55 +0200 | [diff] [blame] | 293 | } else { |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 294 | if (pdata->data->has_cond_reset) |
| 295 | cr |= RNG_CR_CONDRST; |
| 296 | |
Gatien Chevallier | e2acc95 | 2023-09-19 17:27:55 +0200 | [diff] [blame] | 297 | if (pdata->ced) |
| 298 | cr &= ~RNG_CR_CED; |
| 299 | else |
| 300 | cr |= RNG_CR_CED; |
| 301 | |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 302 | writel(cr, pdata->base + RNG_CR); |
| 303 | |
| 304 | if (pdata->data->has_cond_reset) |
| 305 | cr &= ~RNG_CR_CONDRST; |
| 306 | |
Gatien Chevallier | e2acc95 | 2023-09-19 17:27:55 +0200 | [diff] [blame] | 307 | cr |= RNG_CR_RNGEN; |
| 308 | |
| 309 | writel(cr, pdata->base + RNG_CR); |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 310 | } |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 311 | |
| 312 | /* clear error indicators */ |
| 313 | writel(0, pdata->base + RNG_SR); |
| 314 | |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 315 | err = readl_poll_timeout(pdata->base + RNG_SR, sr, |
| 316 | sr & RNG_SR_DRDY, 10000); |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 317 | if (err) |
| 318 | log_err("%s: Timeout!", __func__); |
| 319 | |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 320 | return err; |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 321 | } |
| 322 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 323 | static int stm32_rng_cleanup(struct stm32_rng_plat *pdata) |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 324 | { |
| 325 | writel(0, pdata->base + RNG_CR); |
| 326 | |
| 327 | return clk_disable(&pdata->clk); |
| 328 | } |
| 329 | |
| 330 | static int stm32_rng_probe(struct udevice *dev) |
| 331 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 332 | struct stm32_rng_plat *pdata = dev_get_plat(dev); |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 333 | |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 334 | pdata->data = (struct stm32_rng_data *)dev_get_driver_data(dev); |
| 335 | |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 336 | reset_assert(&pdata->rst); |
| 337 | udelay(20); |
| 338 | reset_deassert(&pdata->rst); |
| 339 | |
| 340 | return stm32_rng_init(pdata); |
| 341 | } |
| 342 | |
| 343 | static int stm32_rng_remove(struct udevice *dev) |
| 344 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 345 | struct stm32_rng_plat *pdata = dev_get_plat(dev); |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 346 | |
| 347 | return stm32_rng_cleanup(pdata); |
| 348 | } |
| 349 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 350 | static int stm32_rng_of_to_plat(struct udevice *dev) |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 351 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 352 | struct stm32_rng_plat *pdata = dev_get_plat(dev); |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 353 | int err; |
| 354 | |
| 355 | pdata->base = dev_read_addr(dev); |
| 356 | if (!pdata->base) |
| 357 | return -ENOMEM; |
| 358 | |
| 359 | err = clk_get_by_index(dev, 0, &pdata->clk); |
| 360 | if (err) |
| 361 | return err; |
| 362 | |
| 363 | err = reset_get_by_index(dev, 0, &pdata->rst); |
| 364 | if (err) |
| 365 | return err; |
| 366 | |
Gatien Chevallier | e2acc95 | 2023-09-19 17:27:55 +0200 | [diff] [blame] | 367 | pdata->ced = dev_read_bool(dev, "clock-error-detect"); |
| 368 | |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static const struct dm_rng_ops stm32_rng_ops = { |
| 373 | .read = stm32_rng_read, |
| 374 | }; |
| 375 | |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 376 | static const struct stm32_rng_data stm32mp13_rng_data = { |
| 377 | .has_cond_reset = true, |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 378 | .max_clock_rate = 48000000, |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 379 | .htcr = 0x969D, |
| 380 | .nscr = 0x2B5BB, |
| 381 | .cr = 0xF00D00, |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | static const struct stm32_rng_data stm32_rng_data = { |
| 385 | .has_cond_reset = false, |
Gatien Chevallier | 4f1f9e7 | 2023-09-19 17:27:56 +0200 | [diff] [blame] | 386 | .max_clock_rate = 3000000, |
Gatien Chevallier | 5e65d46 | 2023-09-19 17:27:58 +0200 | [diff] [blame] | 387 | /* Not supported */ |
| 388 | .htcr = 0, |
| 389 | .nscr = 0, |
| 390 | .cr = 0, |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 391 | }; |
| 392 | |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 393 | static const struct udevice_id stm32_rng_match[] = { |
Lionel Debieve | 71f4421 | 2022-06-30 10:20:15 +0200 | [diff] [blame] | 394 | {.compatible = "st,stm32mp13-rng", .data = (ulong)&stm32mp13_rng_data}, |
| 395 | {.compatible = "st,stm32-rng", .data = (ulong)&stm32_rng_data}, |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 396 | {}, |
| 397 | }; |
| 398 | |
| 399 | U_BOOT_DRIVER(stm32_rng) = { |
| 400 | .name = "stm32-rng", |
| 401 | .id = UCLASS_RNG, |
| 402 | .of_match = stm32_rng_match, |
| 403 | .ops = &stm32_rng_ops, |
| 404 | .probe = stm32_rng_probe, |
| 405 | .remove = stm32_rng_remove, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 406 | .plat_auto = sizeof(struct stm32_rng_plat), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 407 | .of_to_plat = stm32_rng_of_to_plat, |
Sughosh Ganu | cd9a2f9 | 2019-12-28 23:58:29 +0530 | [diff] [blame] | 408 | }; |