hisi-rng.c 2.9 KB

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