atmel-rng.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2011 Peter Korsgaard <jacmet@sunsite.dk>
  3. *
  4. * This file is licensed under the terms of the GNU General Public
  5. * License version 2. This program is licensed "as is" without any
  6. * warranty of any kind, whether express or implied.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <linux/clk.h>
  13. #include <linux/io.h>
  14. #include <linux/hw_random.h>
  15. #include <linux/platform_device.h>
  16. #define TRNG_CR 0x00
  17. #define TRNG_ISR 0x1c
  18. #define TRNG_ODATA 0x50
  19. #define TRNG_KEY 0x524e4700 /* RNG */
  20. struct atmel_trng {
  21. struct clk *clk;
  22. void __iomem *base;
  23. struct hwrng rng;
  24. };
  25. static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
  26. bool wait)
  27. {
  28. struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng);
  29. u32 *data = buf;
  30. /* data ready? */
  31. if (readl(trng->base + TRNG_ISR) & 1) {
  32. *data = readl(trng->base + TRNG_ODATA);
  33. /*
  34. ensure data ready is only set again AFTER the next data
  35. word is ready in case it got set between checking ISR
  36. and reading ODATA, so we don't risk re-reading the
  37. same word
  38. */
  39. readl(trng->base + TRNG_ISR);
  40. return 4;
  41. } else
  42. return 0;
  43. }
  44. static int atmel_trng_probe(struct platform_device *pdev)
  45. {
  46. struct atmel_trng *trng;
  47. struct resource *res;
  48. int ret;
  49. trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
  50. if (!trng)
  51. return -ENOMEM;
  52. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  53. trng->base = devm_ioremap_resource(&pdev->dev, res);
  54. if (IS_ERR(trng->base))
  55. return PTR_ERR(trng->base);
  56. trng->clk = devm_clk_get(&pdev->dev, NULL);
  57. if (IS_ERR(trng->clk))
  58. return PTR_ERR(trng->clk);
  59. ret = clk_prepare_enable(trng->clk);
  60. if (ret)
  61. return ret;
  62. writel(TRNG_KEY | 1, trng->base + TRNG_CR);
  63. trng->rng.name = pdev->name;
  64. trng->rng.read = atmel_trng_read;
  65. ret = hwrng_register(&trng->rng);
  66. if (ret)
  67. goto err_register;
  68. platform_set_drvdata(pdev, trng);
  69. return 0;
  70. err_register:
  71. clk_disable(trng->clk);
  72. return ret;
  73. }
  74. static int atmel_trng_remove(struct platform_device *pdev)
  75. {
  76. struct atmel_trng *trng = platform_get_drvdata(pdev);
  77. hwrng_unregister(&trng->rng);
  78. writel(TRNG_KEY, trng->base + TRNG_CR);
  79. clk_disable_unprepare(trng->clk);
  80. return 0;
  81. }
  82. #ifdef CONFIG_PM
  83. static int atmel_trng_suspend(struct device *dev)
  84. {
  85. struct atmel_trng *trng = dev_get_drvdata(dev);
  86. clk_disable_unprepare(trng->clk);
  87. return 0;
  88. }
  89. static int atmel_trng_resume(struct device *dev)
  90. {
  91. struct atmel_trng *trng = dev_get_drvdata(dev);
  92. return clk_prepare_enable(trng->clk);
  93. }
  94. static const struct dev_pm_ops atmel_trng_pm_ops = {
  95. .suspend = atmel_trng_suspend,
  96. .resume = atmel_trng_resume,
  97. };
  98. #endif /* CONFIG_PM */
  99. static const struct of_device_id atmel_trng_dt_ids[] = {
  100. { .compatible = "atmel,at91sam9g45-trng" },
  101. { /* sentinel */ }
  102. };
  103. MODULE_DEVICE_TABLE(of, atmel_trng_dt_ids);
  104. static struct platform_driver atmel_trng_driver = {
  105. .probe = atmel_trng_probe,
  106. .remove = atmel_trng_remove,
  107. .driver = {
  108. .name = "atmel-trng",
  109. #ifdef CONFIG_PM
  110. .pm = &atmel_trng_pm_ops,
  111. #endif /* CONFIG_PM */
  112. .of_match_table = atmel_trng_dt_ids,
  113. },
  114. };
  115. module_platform_driver(atmel_trng_driver);
  116. MODULE_LICENSE("GPL");
  117. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  118. MODULE_DESCRIPTION("Atmel true random number generator driver");