nomadik-rng.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Nomadik RNG support
  3. * Copyright 2009 Alessandro Rubini
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/amba/bus.h>
  14. #include <linux/hw_random.h>
  15. #include <linux/io.h>
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. static struct clk *rng_clk;
  19. static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  20. {
  21. void __iomem *base = (void __iomem *)rng->priv;
  22. /*
  23. * The register is 32 bits and gives 16 random bits (low half).
  24. * A subsequent read will delay the core for 400ns, so we just read
  25. * once and accept the very unlikely very small delay, even if wait==0.
  26. */
  27. *(u16 *)data = __raw_readl(base + 8) & 0xffff;
  28. return 2;
  29. }
  30. /* we have at most one RNG per machine, granted */
  31. static struct hwrng nmk_rng = {
  32. .name = "nomadik",
  33. .read = nmk_rng_read,
  34. };
  35. static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id)
  36. {
  37. void __iomem *base;
  38. int ret;
  39. rng_clk = devm_clk_get(&dev->dev, NULL);
  40. if (IS_ERR(rng_clk)) {
  41. dev_err(&dev->dev, "could not get rng clock\n");
  42. ret = PTR_ERR(rng_clk);
  43. return ret;
  44. }
  45. clk_prepare_enable(rng_clk);
  46. ret = amba_request_regions(dev, dev->dev.init_name);
  47. if (ret)
  48. goto out_clk;
  49. ret = -ENOMEM;
  50. base = devm_ioremap(&dev->dev, dev->res.start,
  51. resource_size(&dev->res));
  52. if (!base)
  53. goto out_release;
  54. nmk_rng.priv = (unsigned long)base;
  55. ret = hwrng_register(&nmk_rng);
  56. if (ret)
  57. goto out_release;
  58. return 0;
  59. out_release:
  60. amba_release_regions(dev);
  61. out_clk:
  62. clk_disable(rng_clk);
  63. return ret;
  64. }
  65. static int nmk_rng_remove(struct amba_device *dev)
  66. {
  67. hwrng_unregister(&nmk_rng);
  68. amba_release_regions(dev);
  69. clk_disable(rng_clk);
  70. return 0;
  71. }
  72. static struct amba_id nmk_rng_ids[] = {
  73. {
  74. .id = 0x000805e1,
  75. .mask = 0x000fffff, /* top bits are rev and cfg: accept all */
  76. },
  77. {0, 0},
  78. };
  79. MODULE_DEVICE_TABLE(amba, nmk_rng_ids);
  80. static struct amba_driver nmk_rng_driver = {
  81. .drv = {
  82. .owner = THIS_MODULE,
  83. .name = "rng",
  84. },
  85. .probe = nmk_rng_probe,
  86. .remove = nmk_rng_remove,
  87. .id_table = nmk_rng_ids,
  88. };
  89. module_amba_driver(nmk_rng_driver);
  90. MODULE_LICENSE("GPL");