nomadik-rng.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/init.h>
  13. #include <linux/device.h>
  14. #include <linux/amba/bus.h>
  15. #include <linux/hw_random.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. static struct clk *rng_clk;
  20. static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  21. {
  22. void __iomem *base = (void __iomem *)rng->priv;
  23. /*
  24. * The register is 32 bits and gives 16 random bits (low half).
  25. * A subsequent read will delay the core for 400ns, so we just read
  26. * once and accept the very unlikely very small delay, even if wait==0.
  27. */
  28. *(u16 *)data = __raw_readl(base + 8) & 0xffff;
  29. return 2;
  30. }
  31. /* we have at most one RNG per machine, granted */
  32. static struct hwrng nmk_rng = {
  33. .name = "nomadik",
  34. .read = nmk_rng_read,
  35. };
  36. static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id)
  37. {
  38. void __iomem *base;
  39. int ret;
  40. rng_clk = clk_get(&dev->dev, NULL);
  41. if (IS_ERR(rng_clk)) {
  42. dev_err(&dev->dev, "could not get rng clock\n");
  43. ret = PTR_ERR(rng_clk);
  44. return ret;
  45. }
  46. clk_enable(rng_clk);
  47. ret = amba_request_regions(dev, dev->dev.init_name);
  48. if (ret)
  49. goto out_clk;
  50. ret = -ENOMEM;
  51. base = ioremap(dev->res.start, 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_unmap;
  58. return 0;
  59. out_unmap:
  60. iounmap(base);
  61. out_release:
  62. amba_release_regions(dev);
  63. out_clk:
  64. clk_disable(rng_clk);
  65. clk_put(rng_clk);
  66. return ret;
  67. }
  68. static int nmk_rng_remove(struct amba_device *dev)
  69. {
  70. void __iomem *base = (void __iomem *)nmk_rng.priv;
  71. hwrng_unregister(&nmk_rng);
  72. iounmap(base);
  73. amba_release_regions(dev);
  74. clk_disable(rng_clk);
  75. clk_put(rng_clk);
  76. return 0;
  77. }
  78. static struct amba_id nmk_rng_ids[] = {
  79. {
  80. .id = 0x000805e1,
  81. .mask = 0x000fffff, /* top bits are rev and cfg: accept all */
  82. },
  83. {0, 0},
  84. };
  85. MODULE_DEVICE_TABLE(amba, nmk_rng_ids);
  86. static struct amba_driver nmk_rng_driver = {
  87. .drv = {
  88. .owner = THIS_MODULE,
  89. .name = "rng",
  90. },
  91. .probe = nmk_rng_probe,
  92. .remove = nmk_rng_remove,
  93. .id_table = nmk_rng_ids,
  94. };
  95. module_amba_driver(nmk_rng_driver);
  96. MODULE_LICENSE("GPL");