exynos-rng.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * exynos-rng.c - Random Number Generator driver for the Exynos
  3. *
  4. * Copyright (c) 2017 Krzysztof Kozlowski <krzk@kernel.org>
  5. *
  6. * Loosely based on old driver from drivers/char/hw_random/exynos-rng.c:
  7. * Copyright (C) 2012 Samsung Electronics
  8. * Jonghwa Lee <jonghwa3.lee@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation;
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/crypto.h>
  21. #include <linux/err.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <crypto/internal/rng.h>
  26. #define EXYNOS_RNG_CONTROL 0x0
  27. #define EXYNOS_RNG_STATUS 0x10
  28. #define EXYNOS_RNG_SEED_BASE 0x140
  29. #define EXYNOS_RNG_SEED(n) (EXYNOS_RNG_SEED_BASE + (n * 0x4))
  30. #define EXYNOS_RNG_OUT_BASE 0x160
  31. #define EXYNOS_RNG_OUT(n) (EXYNOS_RNG_OUT_BASE + (n * 0x4))
  32. /* EXYNOS_RNG_CONTROL bit fields */
  33. #define EXYNOS_RNG_CONTROL_START 0x18
  34. /* EXYNOS_RNG_STATUS bit fields */
  35. #define EXYNOS_RNG_STATUS_SEED_SETTING_DONE BIT(1)
  36. #define EXYNOS_RNG_STATUS_RNG_DONE BIT(5)
  37. /* Five seed and output registers, each 4 bytes */
  38. #define EXYNOS_RNG_SEED_REGS 5
  39. #define EXYNOS_RNG_SEED_SIZE (EXYNOS_RNG_SEED_REGS * 4)
  40. /*
  41. * Driver re-seeds itself with generated random numbers to increase
  42. * the randomness.
  43. *
  44. * Time for next re-seed in ms.
  45. */
  46. #define EXYNOS_RNG_RESEED_TIME 100
  47. /*
  48. * In polling mode, do not wait infinitely for the engine to finish the work.
  49. */
  50. #define EXYNOS_RNG_WAIT_RETRIES 100
  51. /* Context for crypto */
  52. struct exynos_rng_ctx {
  53. struct exynos_rng_dev *rng;
  54. };
  55. /* Device associated memory */
  56. struct exynos_rng_dev {
  57. struct device *dev;
  58. void __iomem *mem;
  59. struct clk *clk;
  60. /* Generated numbers stored for seeding during resume */
  61. u8 seed_save[EXYNOS_RNG_SEED_SIZE];
  62. unsigned int seed_save_len;
  63. /* Time of last seeding in jiffies */
  64. unsigned long last_seeding;
  65. };
  66. static struct exynos_rng_dev *exynos_rng_dev;
  67. static u32 exynos_rng_readl(struct exynos_rng_dev *rng, u32 offset)
  68. {
  69. return readl_relaxed(rng->mem + offset);
  70. }
  71. static void exynos_rng_writel(struct exynos_rng_dev *rng, u32 val, u32 offset)
  72. {
  73. writel_relaxed(val, rng->mem + offset);
  74. }
  75. static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
  76. const u8 *seed, unsigned int slen)
  77. {
  78. u32 val;
  79. int i;
  80. /* Round seed length because loop iterates over full register size */
  81. slen = ALIGN_DOWN(slen, 4);
  82. if (slen < EXYNOS_RNG_SEED_SIZE)
  83. return -EINVAL;
  84. for (i = 0; i < slen ; i += 4) {
  85. unsigned int seed_reg = (i / 4) % EXYNOS_RNG_SEED_REGS;
  86. val = seed[i] << 24;
  87. val |= seed[i + 1] << 16;
  88. val |= seed[i + 2] << 8;
  89. val |= seed[i + 3] << 0;
  90. exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(seed_reg));
  91. }
  92. val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
  93. if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
  94. dev_warn(rng->dev, "Seed setting not finished\n");
  95. return -EIO;
  96. }
  97. rng->last_seeding = jiffies;
  98. return 0;
  99. }
  100. /*
  101. * Read from output registers and put the data under 'dst' array,
  102. * up to dlen bytes.
  103. *
  104. * Returns number of bytes actually stored in 'dst' (dlen
  105. * or EXYNOS_RNG_SEED_SIZE).
  106. */
  107. static unsigned int exynos_rng_copy_random(struct exynos_rng_dev *rng,
  108. u8 *dst, unsigned int dlen)
  109. {
  110. unsigned int cnt = 0;
  111. int i, j;
  112. u32 val;
  113. for (j = 0; j < EXYNOS_RNG_SEED_REGS; j++) {
  114. val = exynos_rng_readl(rng, EXYNOS_RNG_OUT(j));
  115. for (i = 0; i < 4; i++) {
  116. dst[cnt] = val & 0xff;
  117. val >>= 8;
  118. if (++cnt >= dlen)
  119. return cnt;
  120. }
  121. }
  122. return cnt;
  123. }
  124. /*
  125. * Start the engine and poll for finish. Then read from output registers
  126. * filling the 'dst' buffer up to 'dlen' bytes or up to size of generated
  127. * random data (EXYNOS_RNG_SEED_SIZE).
  128. *
  129. * On success: return 0 and store number of read bytes under 'read' address.
  130. * On error: return -ERRNO.
  131. */
  132. static int exynos_rng_get_random(struct exynos_rng_dev *rng,
  133. u8 *dst, unsigned int dlen,
  134. unsigned int *read)
  135. {
  136. int retry = EXYNOS_RNG_WAIT_RETRIES;
  137. exynos_rng_writel(rng, EXYNOS_RNG_CONTROL_START,
  138. EXYNOS_RNG_CONTROL);
  139. while (!(exynos_rng_readl(rng,
  140. EXYNOS_RNG_STATUS) & EXYNOS_RNG_STATUS_RNG_DONE) && --retry)
  141. cpu_relax();
  142. if (!retry)
  143. return -ETIMEDOUT;
  144. /* Clear status bit */
  145. exynos_rng_writel(rng, EXYNOS_RNG_STATUS_RNG_DONE,
  146. EXYNOS_RNG_STATUS);
  147. *read = exynos_rng_copy_random(rng, dst, dlen);
  148. return 0;
  149. }
  150. /* Re-seed itself from time to time */
  151. static void exynos_rng_reseed(struct exynos_rng_dev *rng)
  152. {
  153. unsigned long next_seeding = rng->last_seeding + \
  154. msecs_to_jiffies(EXYNOS_RNG_RESEED_TIME);
  155. unsigned long now = jiffies;
  156. unsigned int read = 0;
  157. u8 seed[EXYNOS_RNG_SEED_SIZE];
  158. if (time_before(now, next_seeding))
  159. return;
  160. if (exynos_rng_get_random(rng, seed, sizeof(seed), &read))
  161. return;
  162. exynos_rng_set_seed(rng, seed, read);
  163. }
  164. static int exynos_rng_generate(struct crypto_rng *tfm,
  165. const u8 *src, unsigned int slen,
  166. u8 *dst, unsigned int dlen)
  167. {
  168. struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
  169. struct exynos_rng_dev *rng = ctx->rng;
  170. unsigned int read = 0;
  171. int ret;
  172. ret = clk_prepare_enable(rng->clk);
  173. if (ret)
  174. return ret;
  175. do {
  176. ret = exynos_rng_get_random(rng, dst, dlen, &read);
  177. if (ret)
  178. break;
  179. dlen -= read;
  180. dst += read;
  181. exynos_rng_reseed(rng);
  182. } while (dlen > 0);
  183. clk_disable_unprepare(rng->clk);
  184. return ret;
  185. }
  186. static int exynos_rng_seed(struct crypto_rng *tfm, const u8 *seed,
  187. unsigned int slen)
  188. {
  189. struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
  190. struct exynos_rng_dev *rng = ctx->rng;
  191. int ret;
  192. ret = clk_prepare_enable(rng->clk);
  193. if (ret)
  194. return ret;
  195. ret = exynos_rng_set_seed(ctx->rng, seed, slen);
  196. clk_disable_unprepare(rng->clk);
  197. return ret;
  198. }
  199. static int exynos_rng_kcapi_init(struct crypto_tfm *tfm)
  200. {
  201. struct exynos_rng_ctx *ctx = crypto_tfm_ctx(tfm);
  202. ctx->rng = exynos_rng_dev;
  203. return 0;
  204. }
  205. static struct rng_alg exynos_rng_alg = {
  206. .generate = exynos_rng_generate,
  207. .seed = exynos_rng_seed,
  208. .seedsize = EXYNOS_RNG_SEED_SIZE,
  209. .base = {
  210. .cra_name = "stdrng",
  211. .cra_driver_name = "exynos_rng",
  212. .cra_priority = 100,
  213. .cra_ctxsize = sizeof(struct exynos_rng_ctx),
  214. .cra_module = THIS_MODULE,
  215. .cra_init = exynos_rng_kcapi_init,
  216. }
  217. };
  218. static int exynos_rng_probe(struct platform_device *pdev)
  219. {
  220. struct exynos_rng_dev *rng;
  221. struct resource *res;
  222. int ret;
  223. if (exynos_rng_dev)
  224. return -EEXIST;
  225. rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
  226. if (!rng)
  227. return -ENOMEM;
  228. rng->dev = &pdev->dev;
  229. rng->clk = devm_clk_get(&pdev->dev, "secss");
  230. if (IS_ERR(rng->clk)) {
  231. dev_err(&pdev->dev, "Couldn't get clock.\n");
  232. return PTR_ERR(rng->clk);
  233. }
  234. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  235. rng->mem = devm_ioremap_resource(&pdev->dev, res);
  236. if (IS_ERR(rng->mem))
  237. return PTR_ERR(rng->mem);
  238. platform_set_drvdata(pdev, rng);
  239. exynos_rng_dev = rng;
  240. ret = crypto_register_rng(&exynos_rng_alg);
  241. if (ret) {
  242. dev_err(&pdev->dev,
  243. "Couldn't register rng crypto alg: %d\n", ret);
  244. exynos_rng_dev = NULL;
  245. }
  246. return ret;
  247. }
  248. static int exynos_rng_remove(struct platform_device *pdev)
  249. {
  250. crypto_unregister_rng(&exynos_rng_alg);
  251. exynos_rng_dev = NULL;
  252. return 0;
  253. }
  254. static int __maybe_unused exynos_rng_suspend(struct device *dev)
  255. {
  256. struct platform_device *pdev = to_platform_device(dev);
  257. struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
  258. int ret;
  259. /* If we were never seeded then after resume it will be the same */
  260. if (!rng->last_seeding)
  261. return 0;
  262. rng->seed_save_len = 0;
  263. ret = clk_prepare_enable(rng->clk);
  264. if (ret)
  265. return ret;
  266. /* Get new random numbers and store them for seeding on resume. */
  267. exynos_rng_get_random(rng, rng->seed_save, sizeof(rng->seed_save),
  268. &(rng->seed_save_len));
  269. dev_dbg(rng->dev, "Stored %u bytes for seeding on system resume\n",
  270. rng->seed_save_len);
  271. clk_disable_unprepare(rng->clk);
  272. return 0;
  273. }
  274. static int __maybe_unused exynos_rng_resume(struct device *dev)
  275. {
  276. struct platform_device *pdev = to_platform_device(dev);
  277. struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
  278. int ret;
  279. /* Never seeded so nothing to do */
  280. if (!rng->last_seeding)
  281. return 0;
  282. ret = clk_prepare_enable(rng->clk);
  283. if (ret)
  284. return ret;
  285. ret = exynos_rng_set_seed(rng, rng->seed_save, rng->seed_save_len);
  286. clk_disable_unprepare(rng->clk);
  287. return ret;
  288. }
  289. static SIMPLE_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_suspend,
  290. exynos_rng_resume);
  291. static const struct of_device_id exynos_rng_dt_match[] = {
  292. {
  293. .compatible = "samsung,exynos4-rng",
  294. },
  295. { },
  296. };
  297. MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
  298. static struct platform_driver exynos_rng_driver = {
  299. .driver = {
  300. .name = "exynos-rng",
  301. .pm = &exynos_rng_pm_ops,
  302. .of_match_table = exynos_rng_dt_match,
  303. },
  304. .probe = exynos_rng_probe,
  305. .remove = exynos_rng_remove,
  306. };
  307. module_platform_driver(exynos_rng_driver);
  308. MODULE_DESCRIPTION("Exynos H/W Random Number Generator driver");
  309. MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
  310. MODULE_LICENSE("GPL");