mxc-rnga.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * RNG driver for Freescale RNGA
  3. *
  4. * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  5. * Author: Alan Carvalho de Assis <acassis@gmail.com>
  6. */
  7. /*
  8. * The code contained herein is licensed under the GNU General Public
  9. * License. You may obtain a copy of the GNU General Public License
  10. * Version 2 or later at the following locations:
  11. *
  12. * http://www.opensource.org/licenses/gpl-license.html
  13. * http://www.gnu.org/copyleft/gpl.html
  14. *
  15. * This driver is based on other RNG drivers.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/hw_random.h>
  20. #include <linux/io.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. /* RNGA Registers */
  25. #define RNGA_CONTROL 0x00
  26. #define RNGA_STATUS 0x04
  27. #define RNGA_ENTROPY 0x08
  28. #define RNGA_OUTPUT_FIFO 0x0c
  29. #define RNGA_MODE 0x10
  30. #define RNGA_VERIFICATION_CONTROL 0x14
  31. #define RNGA_OSC_CONTROL_COUNTER 0x18
  32. #define RNGA_OSC1_COUNTER 0x1c
  33. #define RNGA_OSC2_COUNTER 0x20
  34. #define RNGA_OSC_COUNTER_STATUS 0x24
  35. /* RNGA Registers Range */
  36. #define RNG_ADDR_RANGE 0x28
  37. /* RNGA Control Register */
  38. #define RNGA_CONTROL_SLEEP 0x00000010
  39. #define RNGA_CONTROL_CLEAR_INT 0x00000008
  40. #define RNGA_CONTROL_MASK_INTS 0x00000004
  41. #define RNGA_CONTROL_HIGH_ASSURANCE 0x00000002
  42. #define RNGA_CONTROL_GO 0x00000001
  43. #define RNGA_STATUS_LEVEL_MASK 0x0000ff00
  44. /* RNGA Status Register */
  45. #define RNGA_STATUS_OSC_DEAD 0x80000000
  46. #define RNGA_STATUS_SLEEP 0x00000010
  47. #define RNGA_STATUS_ERROR_INT 0x00000008
  48. #define RNGA_STATUS_FIFO_UNDERFLOW 0x00000004
  49. #define RNGA_STATUS_LAST_READ_STATUS 0x00000002
  50. #define RNGA_STATUS_SECURITY_VIOLATION 0x00000001
  51. struct mxc_rng {
  52. struct device *dev;
  53. struct hwrng rng;
  54. void __iomem *mem;
  55. struct clk *clk;
  56. };
  57. static int mxc_rnga_data_present(struct hwrng *rng, int wait)
  58. {
  59. int i;
  60. struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
  61. for (i = 0; i < 20; i++) {
  62. /* how many random numbers are in FIFO? [0-16] */
  63. int level = (__raw_readl(mxc_rng->mem + RNGA_STATUS) &
  64. RNGA_STATUS_LEVEL_MASK) >> 8;
  65. if (level || !wait)
  66. return !!level;
  67. udelay(10);
  68. }
  69. return 0;
  70. }
  71. static int mxc_rnga_data_read(struct hwrng *rng, u32 * data)
  72. {
  73. int err;
  74. u32 ctrl;
  75. struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
  76. /* retrieve a random number from FIFO */
  77. *data = __raw_readl(mxc_rng->mem + RNGA_OUTPUT_FIFO);
  78. /* some error while reading this random number? */
  79. err = __raw_readl(mxc_rng->mem + RNGA_STATUS) & RNGA_STATUS_ERROR_INT;
  80. /* if error: clear error interrupt, but doesn't return random number */
  81. if (err) {
  82. dev_dbg(mxc_rng->dev, "Error while reading random number!\n");
  83. ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
  84. __raw_writel(ctrl | RNGA_CONTROL_CLEAR_INT,
  85. mxc_rng->mem + RNGA_CONTROL);
  86. return 0;
  87. } else
  88. return 4;
  89. }
  90. static int mxc_rnga_init(struct hwrng *rng)
  91. {
  92. u32 ctrl, osc;
  93. struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
  94. /* wake up */
  95. ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
  96. __raw_writel(ctrl & ~RNGA_CONTROL_SLEEP, mxc_rng->mem + RNGA_CONTROL);
  97. /* verify if oscillator is working */
  98. osc = __raw_readl(mxc_rng->mem + RNGA_STATUS);
  99. if (osc & RNGA_STATUS_OSC_DEAD) {
  100. dev_err(mxc_rng->dev, "RNGA Oscillator is dead!\n");
  101. return -ENODEV;
  102. }
  103. /* go running */
  104. ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
  105. __raw_writel(ctrl | RNGA_CONTROL_GO, mxc_rng->mem + RNGA_CONTROL);
  106. return 0;
  107. }
  108. static void mxc_rnga_cleanup(struct hwrng *rng)
  109. {
  110. u32 ctrl;
  111. struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
  112. ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
  113. /* stop rnga */
  114. __raw_writel(ctrl & ~RNGA_CONTROL_GO, mxc_rng->mem + RNGA_CONTROL);
  115. }
  116. static int __init mxc_rnga_probe(struct platform_device *pdev)
  117. {
  118. int err;
  119. struct resource *res;
  120. struct mxc_rng *mxc_rng;
  121. mxc_rng = devm_kzalloc(&pdev->dev, sizeof(*mxc_rng), GFP_KERNEL);
  122. if (!mxc_rng)
  123. return -ENOMEM;
  124. mxc_rng->dev = &pdev->dev;
  125. mxc_rng->rng.name = "mxc-rnga";
  126. mxc_rng->rng.init = mxc_rnga_init;
  127. mxc_rng->rng.cleanup = mxc_rnga_cleanup,
  128. mxc_rng->rng.data_present = mxc_rnga_data_present,
  129. mxc_rng->rng.data_read = mxc_rnga_data_read,
  130. mxc_rng->clk = devm_clk_get(&pdev->dev, NULL);
  131. if (IS_ERR(mxc_rng->clk)) {
  132. dev_err(&pdev->dev, "Could not get rng_clk!\n");
  133. return PTR_ERR(mxc_rng->clk);
  134. }
  135. err = clk_prepare_enable(mxc_rng->clk);
  136. if (err)
  137. return err;
  138. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  139. mxc_rng->mem = devm_ioremap_resource(&pdev->dev, res);
  140. if (IS_ERR(mxc_rng->mem)) {
  141. err = PTR_ERR(mxc_rng->mem);
  142. goto err_ioremap;
  143. }
  144. err = hwrng_register(&mxc_rng->rng);
  145. if (err) {
  146. dev_err(&pdev->dev, "MXC RNGA registering failed (%d)\n", err);
  147. goto err_ioremap;
  148. }
  149. return 0;
  150. err_ioremap:
  151. clk_disable_unprepare(mxc_rng->clk);
  152. return err;
  153. }
  154. static int __exit mxc_rnga_remove(struct platform_device *pdev)
  155. {
  156. struct mxc_rng *mxc_rng = platform_get_drvdata(pdev);
  157. hwrng_unregister(&mxc_rng->rng);
  158. clk_disable_unprepare(mxc_rng->clk);
  159. return 0;
  160. }
  161. static const struct of_device_id mxc_rnga_of_match[] = {
  162. { .compatible = "fsl,imx21-rnga", },
  163. { .compatible = "fsl,imx31-rnga", },
  164. { /* sentinel */ },
  165. };
  166. MODULE_DEVICE_TABLE(of, mxc_rnga_of_match);
  167. static struct platform_driver mxc_rnga_driver = {
  168. .driver = {
  169. .name = "mxc_rnga",
  170. .of_match_table = mxc_rnga_of_match,
  171. },
  172. .remove = __exit_p(mxc_rnga_remove),
  173. };
  174. module_platform_driver_probe(mxc_rnga_driver, mxc_rnga_probe);
  175. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  176. MODULE_DESCRIPTION("H/W RNGA driver for i.MX");
  177. MODULE_LICENSE("GPL");