stm32-rng.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2015, Daniel Thompson
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/hw_random.h>
  8. #include <linux/io.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/reset.h>
  17. #include <linux/slab.h>
  18. #define RNG_CR 0x00
  19. #define RNG_CR_RNGEN BIT(2)
  20. #define RNG_CR_CED BIT(5)
  21. #define RNG_CR_CONFIG1 GENMASK(11, 8)
  22. #define RNG_CR_NISTC BIT(12)
  23. #define RNG_CR_CONFIG2 GENMASK(15, 13)
  24. #define RNG_CR_CLKDIV_SHIFT 16
  25. #define RNG_CR_CLKDIV GENMASK(19, 16)
  26. #define RNG_CR_CONFIG3 GENMASK(25, 20)
  27. #define RNG_CR_CONDRST BIT(30)
  28. #define RNG_CR_CONFLOCK BIT(31)
  29. #define RNG_CR_ENTROPY_SRC_MASK (RNG_CR_CONFIG1 | RNG_CR_NISTC | RNG_CR_CONFIG2 | RNG_CR_CONFIG3)
  30. #define RNG_CR_CONFIG_MASK (RNG_CR_ENTROPY_SRC_MASK | RNG_CR_CED | RNG_CR_CLKDIV)
  31. #define RNG_SR 0x04
  32. #define RNG_SR_DRDY BIT(0)
  33. #define RNG_SR_CECS BIT(1)
  34. #define RNG_SR_SECS BIT(2)
  35. #define RNG_SR_CEIS BIT(5)
  36. #define RNG_SR_SEIS BIT(6)
  37. #define RNG_DR 0x08
  38. #define RNG_NSCR 0x0C
  39. #define RNG_NSCR_MASK GENMASK(17, 0)
  40. #define RNG_HTCR 0x10
  41. #define RNG_NB_RECOVER_TRIES 3
  42. struct stm32_rng_data {
  43. uint max_clock_rate;
  44. u32 cr;
  45. u32 nscr;
  46. u32 htcr;
  47. bool has_cond_reset;
  48. };
  49. /**
  50. * struct stm32_rng_config - RNG configuration data
  51. *
  52. * @cr: RNG configuration. 0 means default hardware RNG configuration
  53. * @nscr: Noise sources control configuration.
  54. * @htcr: Health tests configuration.
  55. */
  56. struct stm32_rng_config {
  57. u32 cr;
  58. u32 nscr;
  59. u32 htcr;
  60. };
  61. struct stm32_rng_private {
  62. struct hwrng rng;
  63. struct device *dev;
  64. void __iomem *base;
  65. struct clk *clk;
  66. struct reset_control *rst;
  67. struct stm32_rng_config pm_conf;
  68. const struct stm32_rng_data *data;
  69. bool ced;
  70. bool lock_conf;
  71. };
  72. /*
  73. * Extracts from the STM32 RNG specification when RNG supports CONDRST.
  74. *
  75. * When a noise source (or seed) error occurs, the RNG stops generating
  76. * random numbers and sets to “1” both SEIS and SECS bits to indicate
  77. * that a seed error occurred. (...)
  78. *
  79. * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield
  80. * description for details). This step is needed only if SECS is set.
  81. * Indeed, when SEIS is set and SECS is cleared it means RNG performed
  82. * the reset automatically (auto-reset).
  83. * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST
  84. * to be cleared in the RNG_CR register, then confirm that SEIS is
  85. * cleared in the RNG_SR register. Otherwise just clear SEIS bit in
  86. * the RNG_SR register.
  87. * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be
  88. * cleared by RNG. The random number generation is now back to normal.
  89. */
  90. static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv)
  91. {
  92. struct device *dev = priv->dev;
  93. u32 sr = readl_relaxed(priv->base + RNG_SR);
  94. u32 cr = readl_relaxed(priv->base + RNG_CR);
  95. int err;
  96. if (sr & RNG_SR_SECS) {
  97. /* Conceal by resetting the subsystem (step 1.) */
  98. writel_relaxed(cr | RNG_CR_CONDRST, priv->base + RNG_CR);
  99. writel_relaxed(cr & ~RNG_CR_CONDRST, priv->base + RNG_CR);
  100. } else {
  101. /* RNG auto-reset (step 2.) */
  102. writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
  103. goto end;
  104. }
  105. err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, cr, !(cr & RNG_CR_CONDRST), 10,
  106. 100000);
  107. if (err) {
  108. dev_err(dev, "%s: timeout %x\n", __func__, sr);
  109. return err;
  110. }
  111. /* Check SEIS is cleared (step 2.) */
  112. if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
  113. return -EINVAL;
  114. err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 10,
  115. 100000);
  116. if (err) {
  117. dev_err(dev, "%s: timeout %x\n", __func__, sr);
  118. return err;
  119. }
  120. end:
  121. return 0;
  122. }
  123. /*
  124. * Extracts from the STM32 RNG specification, when CONDRST is not supported
  125. *
  126. * When a noise source (or seed) error occurs, the RNG stops generating
  127. * random numbers and sets to “1” both SEIS and SECS bits to indicate
  128. * that a seed error occurred. (...)
  129. *
  130. * The following sequence shall be used to fully recover from a seed
  131. * error after the RNG initialization:
  132. * 1. Clear the SEIS bit by writing it to “0”.
  133. * 2. Read out 12 words from the RNG_DR register, and discard each of
  134. * them in order to clean the pipeline.
  135. * 3. Confirm that SEIS is still cleared. Random number generation is
  136. * back to normal.
  137. */
  138. static int stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_private *priv)
  139. {
  140. unsigned int i = 0;
  141. u32 sr = readl_relaxed(priv->base + RNG_SR);
  142. writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
  143. for (i = 12; i != 0; i--)
  144. (void)readl_relaxed(priv->base + RNG_DR);
  145. if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
  146. return -EINVAL;
  147. return 0;
  148. }
  149. static int stm32_rng_conceal_seed_error(struct hwrng *rng)
  150. {
  151. struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
  152. dev_dbg(priv->dev, "Concealing seed error\n");
  153. if (priv->data->has_cond_reset)
  154. return stm32_rng_conceal_seed_error_cond_reset(priv);
  155. else
  156. return stm32_rng_conceal_seed_error_sw_reset(priv);
  157. };
  158. static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  159. {
  160. struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
  161. unsigned int i = 0;
  162. int retval = 0, err = 0;
  163. u32 sr;
  164. retval = pm_runtime_resume_and_get(priv->dev);
  165. if (retval)
  166. return retval;
  167. if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
  168. stm32_rng_conceal_seed_error(rng);
  169. while (max >= sizeof(u32)) {
  170. sr = readl_relaxed(priv->base + RNG_SR);
  171. /*
  172. * Manage timeout which is based on timer and take
  173. * care of initial delay time when enabling the RNG.
  174. */
  175. if (!sr && wait) {
  176. err = readl_relaxed_poll_timeout_atomic(priv->base
  177. + RNG_SR,
  178. sr, sr,
  179. 10, 50000);
  180. if (err) {
  181. dev_err(priv->dev, "%s: timeout %x!\n", __func__, sr);
  182. break;
  183. }
  184. } else if (!sr) {
  185. /* The FIFO is being filled up */
  186. break;
  187. }
  188. if (sr != RNG_SR_DRDY) {
  189. if (sr & RNG_SR_SEIS) {
  190. err = stm32_rng_conceal_seed_error(rng);
  191. i++;
  192. if (err && i > RNG_NB_RECOVER_TRIES) {
  193. dev_err(priv->dev, "Couldn't recover from seed error\n");
  194. retval = -ENOTRECOVERABLE;
  195. goto exit_rpm;
  196. }
  197. continue;
  198. }
  199. if (WARN_ONCE((sr & RNG_SR_CEIS), "RNG clock too slow - %x\n", sr))
  200. writel_relaxed(0, priv->base + RNG_SR);
  201. }
  202. /* Late seed error case: DR being 0 is an error status */
  203. *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
  204. if (!*(u32 *)data) {
  205. err = stm32_rng_conceal_seed_error(rng);
  206. i++;
  207. if (err && i > RNG_NB_RECOVER_TRIES) {
  208. dev_err(priv->dev, "Couldn't recover from seed error");
  209. retval = -ENOTRECOVERABLE;
  210. goto exit_rpm;
  211. }
  212. continue;
  213. }
  214. i = 0;
  215. retval += sizeof(u32);
  216. data += sizeof(u32);
  217. max -= sizeof(u32);
  218. }
  219. exit_rpm:
  220. pm_runtime_mark_last_busy(priv->dev);
  221. pm_runtime_put_sync_autosuspend(priv->dev);
  222. return retval || !wait ? retval : -EIO;
  223. }
  224. static uint stm32_rng_clock_freq_restrain(struct hwrng *rng)
  225. {
  226. struct stm32_rng_private *priv =
  227. container_of(rng, struct stm32_rng_private, rng);
  228. unsigned long clock_rate = 0;
  229. uint clock_div = 0;
  230. clock_rate = clk_get_rate(priv->clk);
  231. /*
  232. * Get the exponent to apply on the CLKDIV field in RNG_CR register
  233. * No need to handle the case when clock-div > 0xF as it is physically
  234. * impossible
  235. */
  236. while ((clock_rate >> clock_div) > priv->data->max_clock_rate)
  237. clock_div++;
  238. pr_debug("RNG clk rate : %lu\n", clk_get_rate(priv->clk) >> clock_div);
  239. return clock_div;
  240. }
  241. static int stm32_rng_init(struct hwrng *rng)
  242. {
  243. struct stm32_rng_private *priv =
  244. container_of(rng, struct stm32_rng_private, rng);
  245. int err;
  246. u32 reg;
  247. err = clk_prepare_enable(priv->clk);
  248. if (err)
  249. return err;
  250. /* clear error indicators */
  251. writel_relaxed(0, priv->base + RNG_SR);
  252. reg = readl_relaxed(priv->base + RNG_CR);
  253. /*
  254. * Keep default RNG configuration if none was specified.
  255. * 0 is an invalid value as it disables all entropy sources.
  256. */
  257. if (priv->data->has_cond_reset && priv->data->cr) {
  258. uint clock_div = stm32_rng_clock_freq_restrain(rng);
  259. reg &= ~RNG_CR_CONFIG_MASK;
  260. reg |= RNG_CR_CONDRST | (priv->data->cr & RNG_CR_ENTROPY_SRC_MASK) |
  261. (clock_div << RNG_CR_CLKDIV_SHIFT);
  262. if (priv->ced)
  263. reg &= ~RNG_CR_CED;
  264. else
  265. reg |= RNG_CR_CED;
  266. writel_relaxed(reg, priv->base + RNG_CR);
  267. /* Health tests and noise control registers */
  268. writel_relaxed(priv->data->htcr, priv->base + RNG_HTCR);
  269. writel_relaxed(priv->data->nscr & RNG_NSCR_MASK, priv->base + RNG_NSCR);
  270. reg &= ~RNG_CR_CONDRST;
  271. reg |= RNG_CR_RNGEN;
  272. if (priv->lock_conf)
  273. reg |= RNG_CR_CONFLOCK;
  274. writel_relaxed(reg, priv->base + RNG_CR);
  275. err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
  276. (!(reg & RNG_CR_CONDRST)),
  277. 10, 50000);
  278. if (err) {
  279. clk_disable_unprepare(priv->clk);
  280. dev_err(priv->dev, "%s: timeout %x!\n", __func__, reg);
  281. return -EINVAL;
  282. }
  283. } else {
  284. /* Handle all RNG versions by checking if conditional reset should be set */
  285. if (priv->data->has_cond_reset)
  286. reg |= RNG_CR_CONDRST;
  287. if (priv->ced)
  288. reg &= ~RNG_CR_CED;
  289. else
  290. reg |= RNG_CR_CED;
  291. writel_relaxed(reg, priv->base + RNG_CR);
  292. if (priv->data->has_cond_reset)
  293. reg &= ~RNG_CR_CONDRST;
  294. reg |= RNG_CR_RNGEN;
  295. writel_relaxed(reg, priv->base + RNG_CR);
  296. }
  297. err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg,
  298. reg & RNG_SR_DRDY,
  299. 10, 100000);
  300. if (err || (reg & ~RNG_SR_DRDY)) {
  301. clk_disable_unprepare(priv->clk);
  302. dev_err(priv->dev, "%s: timeout:%x SR: %x!\n", __func__, err, reg);
  303. return -EINVAL;
  304. }
  305. clk_disable_unprepare(priv->clk);
  306. return 0;
  307. }
  308. static void stm32_rng_remove(struct platform_device *ofdev)
  309. {
  310. pm_runtime_disable(&ofdev->dev);
  311. }
  312. static int __maybe_unused stm32_rng_runtime_suspend(struct device *dev)
  313. {
  314. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  315. u32 reg;
  316. reg = readl_relaxed(priv->base + RNG_CR);
  317. reg &= ~RNG_CR_RNGEN;
  318. writel_relaxed(reg, priv->base + RNG_CR);
  319. clk_disable_unprepare(priv->clk);
  320. return 0;
  321. }
  322. static int __maybe_unused stm32_rng_suspend(struct device *dev)
  323. {
  324. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  325. int err;
  326. err = clk_prepare_enable(priv->clk);
  327. if (err)
  328. return err;
  329. if (priv->data->has_cond_reset) {
  330. priv->pm_conf.nscr = readl_relaxed(priv->base + RNG_NSCR);
  331. priv->pm_conf.htcr = readl_relaxed(priv->base + RNG_HTCR);
  332. }
  333. /* Do not save that RNG is enabled as it will be handled at resume */
  334. priv->pm_conf.cr = readl_relaxed(priv->base + RNG_CR) & ~RNG_CR_RNGEN;
  335. writel_relaxed(priv->pm_conf.cr, priv->base + RNG_CR);
  336. clk_disable_unprepare(priv->clk);
  337. return 0;
  338. }
  339. static int __maybe_unused stm32_rng_runtime_resume(struct device *dev)
  340. {
  341. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  342. int err;
  343. u32 reg;
  344. err = clk_prepare_enable(priv->clk);
  345. if (err)
  346. return err;
  347. /* Clean error indications */
  348. writel_relaxed(0, priv->base + RNG_SR);
  349. reg = readl_relaxed(priv->base + RNG_CR);
  350. reg |= RNG_CR_RNGEN;
  351. writel_relaxed(reg, priv->base + RNG_CR);
  352. return 0;
  353. }
  354. static int __maybe_unused stm32_rng_resume(struct device *dev)
  355. {
  356. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  357. int err;
  358. u32 reg;
  359. err = clk_prepare_enable(priv->clk);
  360. if (err)
  361. return err;
  362. /* Clean error indications */
  363. writel_relaxed(0, priv->base + RNG_SR);
  364. if (priv->data->has_cond_reset) {
  365. /*
  366. * Correct configuration in bits [29:4] must be set in the same
  367. * access that set RNG_CR_CONDRST bit. Else config setting is
  368. * not taken into account. CONFIGLOCK bit must also be unset but
  369. * it is not handled at the moment.
  370. */
  371. writel_relaxed(priv->pm_conf.cr | RNG_CR_CONDRST, priv->base + RNG_CR);
  372. writel_relaxed(priv->pm_conf.nscr, priv->base + RNG_NSCR);
  373. writel_relaxed(priv->pm_conf.htcr, priv->base + RNG_HTCR);
  374. reg = readl_relaxed(priv->base + RNG_CR);
  375. reg |= RNG_CR_RNGEN;
  376. reg &= ~RNG_CR_CONDRST;
  377. writel_relaxed(reg, priv->base + RNG_CR);
  378. err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
  379. reg & ~RNG_CR_CONDRST, 10, 100000);
  380. if (err) {
  381. clk_disable_unprepare(priv->clk);
  382. dev_err(priv->dev, "%s: timeout:%x CR: %x!\n", __func__, err, reg);
  383. return -EINVAL;
  384. }
  385. } else {
  386. reg = priv->pm_conf.cr;
  387. reg |= RNG_CR_RNGEN;
  388. writel_relaxed(reg, priv->base + RNG_CR);
  389. }
  390. clk_disable_unprepare(priv->clk);
  391. return 0;
  392. }
  393. static const struct dev_pm_ops __maybe_unused stm32_rng_pm_ops = {
  394. SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
  395. stm32_rng_runtime_resume, NULL)
  396. SET_SYSTEM_SLEEP_PM_OPS(stm32_rng_suspend,
  397. stm32_rng_resume)
  398. };
  399. static const struct stm32_rng_data stm32mp13_rng_data = {
  400. .has_cond_reset = true,
  401. .max_clock_rate = 48000000,
  402. .cr = 0x00F00D00,
  403. .nscr = 0x2B5BB,
  404. .htcr = 0x969D,
  405. };
  406. static const struct stm32_rng_data stm32_rng_data = {
  407. .has_cond_reset = false,
  408. .max_clock_rate = 3000000,
  409. };
  410. static const struct of_device_id stm32_rng_match[] = {
  411. {
  412. .compatible = "st,stm32mp13-rng",
  413. .data = &stm32mp13_rng_data,
  414. },
  415. {
  416. .compatible = "st,stm32-rng",
  417. .data = &stm32_rng_data,
  418. },
  419. {},
  420. };
  421. MODULE_DEVICE_TABLE(of, stm32_rng_match);
  422. static int stm32_rng_probe(struct platform_device *ofdev)
  423. {
  424. struct device *dev = &ofdev->dev;
  425. struct device_node *np = ofdev->dev.of_node;
  426. struct stm32_rng_private *priv;
  427. struct resource *res;
  428. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  429. if (!priv)
  430. return -ENOMEM;
  431. priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res);
  432. if (IS_ERR(priv->base))
  433. return PTR_ERR(priv->base);
  434. priv->clk = devm_clk_get(&ofdev->dev, NULL);
  435. if (IS_ERR(priv->clk))
  436. return PTR_ERR(priv->clk);
  437. priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
  438. if (!IS_ERR(priv->rst)) {
  439. reset_control_assert(priv->rst);
  440. udelay(2);
  441. reset_control_deassert(priv->rst);
  442. }
  443. priv->ced = of_property_read_bool(np, "clock-error-detect");
  444. priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf");
  445. priv->dev = dev;
  446. priv->data = of_device_get_match_data(dev);
  447. if (!priv->data)
  448. return -ENODEV;
  449. dev_set_drvdata(dev, priv);
  450. priv->rng.name = dev_driver_string(dev);
  451. priv->rng.init = stm32_rng_init;
  452. priv->rng.read = stm32_rng_read;
  453. priv->rng.quality = 900;
  454. pm_runtime_set_autosuspend_delay(dev, 100);
  455. pm_runtime_use_autosuspend(dev);
  456. pm_runtime_enable(dev);
  457. return devm_hwrng_register(dev, &priv->rng);
  458. }
  459. static struct platform_driver stm32_rng_driver = {
  460. .driver = {
  461. .name = "stm32-rng",
  462. .pm = pm_ptr(&stm32_rng_pm_ops),
  463. .of_match_table = stm32_rng_match,
  464. },
  465. .probe = stm32_rng_probe,
  466. .remove_new = stm32_rng_remove,
  467. };
  468. module_platform_driver(stm32_rng_driver);
  469. MODULE_LICENSE("GPL");
  470. MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
  471. MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");