hisi-rng.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 HiSilicon Co., Ltd.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/kernel.h>
  7. #include <linux/hw_random.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/random.h>
  13. #define RNG_SEED 0x0
  14. #define RNG_CTRL 0x4
  15. #define RNG_SEED_SEL BIT(2)
  16. #define RNG_RING_EN BIT(1)
  17. #define RNG_EN BIT(0)
  18. #define RNG_RAN_NUM 0x10
  19. #define RNG_PHY_SEED 0x14
  20. #define to_hisi_rng(p) container_of(p, struct hisi_rng, rng)
  21. static int seed_sel;
  22. module_param(seed_sel, int, S_IRUGO);
  23. MODULE_PARM_DESC(seed_sel, "Auto reload seed. 0, use LFSR(default); 1, use ring oscillator.");
  24. struct hisi_rng {
  25. void __iomem *base;
  26. struct hwrng rng;
  27. };
  28. static int hisi_rng_init(struct hwrng *rng)
  29. {
  30. struct hisi_rng *hrng = to_hisi_rng(rng);
  31. int val = RNG_EN;
  32. u32 seed;
  33. /* get a random number as initial seed */
  34. get_random_bytes(&seed, sizeof(seed));
  35. writel_relaxed(seed, hrng->base + RNG_SEED);
  36. /**
  37. * The seed is reload periodically, there are two choice
  38. * of seeds, default seed using the value from LFSR, or
  39. * will use seed generated by ring oscillator.
  40. */
  41. if (seed_sel == 1)
  42. val |= RNG_RING_EN | RNG_SEED_SEL;
  43. writel_relaxed(val, hrng->base + RNG_CTRL);
  44. return 0;
  45. }
  46. static void hisi_rng_cleanup(struct hwrng *rng)
  47. {
  48. struct hisi_rng *hrng = to_hisi_rng(rng);
  49. writel_relaxed(0, hrng->base + RNG_CTRL);
  50. }
  51. static int hisi_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  52. {
  53. struct hisi_rng *hrng = to_hisi_rng(rng);
  54. u32 *data = buf;
  55. *data = readl_relaxed(hrng->base + RNG_RAN_NUM);
  56. return 4;
  57. }
  58. static int hisi_rng_probe(struct platform_device *pdev)
  59. {
  60. struct hisi_rng *rng;
  61. int ret;
  62. rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
  63. if (!rng)
  64. return -ENOMEM;
  65. rng->base = devm_platform_ioremap_resource(pdev, 0);
  66. if (IS_ERR(rng->base))
  67. return PTR_ERR(rng->base);
  68. rng->rng.name = pdev->name;
  69. rng->rng.init = hisi_rng_init;
  70. rng->rng.cleanup = hisi_rng_cleanup;
  71. rng->rng.read = hisi_rng_read;
  72. ret = devm_hwrng_register(&pdev->dev, &rng->rng);
  73. if (ret)
  74. return dev_err_probe(&pdev->dev, ret, "failed to register hwrng\n");
  75. return 0;
  76. }
  77. static const struct of_device_id hisi_rng_dt_ids[] __maybe_unused = {
  78. { .compatible = "hisilicon,hip04-rng" },
  79. { .compatible = "hisilicon,hip05-rng" },
  80. { }
  81. };
  82. MODULE_DEVICE_TABLE(of, hisi_rng_dt_ids);
  83. static struct platform_driver hisi_rng_driver = {
  84. .probe = hisi_rng_probe,
  85. .driver = {
  86. .name = "hisi-rng",
  87. .of_match_table = of_match_ptr(hisi_rng_dt_ids),
  88. },
  89. };
  90. module_platform_driver(hisi_rng_driver);
  91. MODULE_LICENSE("GPL");
  92. MODULE_AUTHOR("Kefeng Wang <wangkefeng.wang@huawei>");
  93. MODULE_DESCRIPTION("Hisilicon random number generator driver");