mxc-rnga.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/module.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/clk.h>
  21. #include <linux/err.h>
  22. #include <linux/ioport.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/hw_random.h>
  25. #include <linux/delay.h>
  26. #include <linux/io.h>
  27. /* RNGA Registers */
  28. #define RNGA_CONTROL 0x00
  29. #define RNGA_STATUS 0x04
  30. #define RNGA_ENTROPY 0x08
  31. #define RNGA_OUTPUT_FIFO 0x0c
  32. #define RNGA_MODE 0x10
  33. #define RNGA_VERIFICATION_CONTROL 0x14
  34. #define RNGA_OSC_CONTROL_COUNTER 0x18
  35. #define RNGA_OSC1_COUNTER 0x1c
  36. #define RNGA_OSC2_COUNTER 0x20
  37. #define RNGA_OSC_COUNTER_STATUS 0x24
  38. /* RNGA Registers Range */
  39. #define RNG_ADDR_RANGE 0x28
  40. /* RNGA Control Register */
  41. #define RNGA_CONTROL_SLEEP 0x00000010
  42. #define RNGA_CONTROL_CLEAR_INT 0x00000008
  43. #define RNGA_CONTROL_MASK_INTS 0x00000004
  44. #define RNGA_CONTROL_HIGH_ASSURANCE 0x00000002
  45. #define RNGA_CONTROL_GO 0x00000001
  46. #define RNGA_STATUS_LEVEL_MASK 0x0000ff00
  47. /* RNGA Status Register */
  48. #define RNGA_STATUS_OSC_DEAD 0x80000000
  49. #define RNGA_STATUS_SLEEP 0x00000010
  50. #define RNGA_STATUS_ERROR_INT 0x00000008
  51. #define RNGA_STATUS_FIFO_UNDERFLOW 0x00000004
  52. #define RNGA_STATUS_LAST_READ_STATUS 0x00000002
  53. #define RNGA_STATUS_SECURITY_VIOLATION 0x00000001
  54. struct mxc_rng {
  55. struct device *dev;
  56. struct hwrng rng;
  57. void __iomem *mem;
  58. struct clk *clk;
  59. };
  60. static int mxc_rnga_data_present(struct hwrng *rng, int wait)
  61. {
  62. int i;
  63. struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
  64. for (i = 0; i < 20; i++) {
  65. /* how many random numbers are in FIFO? [0-16] */
  66. int level = (__raw_readl(mxc_rng->mem + RNGA_STATUS) &
  67. RNGA_STATUS_LEVEL_MASK) >> 8;
  68. if (level || !wait)
  69. return !!level;
  70. udelay(10);
  71. }
  72. return 0;
  73. }
  74. static int mxc_rnga_data_read(struct hwrng *rng, u32 * data)
  75. {
  76. int err;
  77. u32 ctrl;
  78. struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
  79. /* retrieve a random number from FIFO */
  80. *data = __raw_readl(mxc_rng->mem + RNGA_OUTPUT_FIFO);
  81. /* some error while reading this random number? */
  82. err = __raw_readl(mxc_rng->mem + RNGA_STATUS) & RNGA_STATUS_ERROR_INT;
  83. /* if error: clear error interrupt, but doesn't return random number */
  84. if (err) {
  85. dev_dbg(mxc_rng->dev, "Error while reading random number!\n");
  86. ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
  87. __raw_writel(ctrl | RNGA_CONTROL_CLEAR_INT,
  88. mxc_rng->mem + RNGA_CONTROL);
  89. return 0;
  90. } else
  91. return 4;
  92. }
  93. static int mxc_rnga_init(struct hwrng *rng)
  94. {
  95. u32 ctrl, osc;
  96. struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
  97. /* wake up */
  98. ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
  99. __raw_writel(ctrl & ~RNGA_CONTROL_SLEEP, mxc_rng->mem + RNGA_CONTROL);
  100. /* verify if oscillator is working */
  101. osc = __raw_readl(mxc_rng->mem + RNGA_STATUS);
  102. if (osc & RNGA_STATUS_OSC_DEAD) {
  103. dev_err(mxc_rng->dev, "RNGA Oscillator is dead!\n");
  104. return -ENODEV;
  105. }
  106. /* go running */
  107. ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
  108. __raw_writel(ctrl | RNGA_CONTROL_GO, mxc_rng->mem + RNGA_CONTROL);
  109. return 0;
  110. }
  111. static void mxc_rnga_cleanup(struct hwrng *rng)
  112. {
  113. u32 ctrl;
  114. struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
  115. ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
  116. /* stop rnga */
  117. __raw_writel(ctrl & ~RNGA_CONTROL_GO, mxc_rng->mem + RNGA_CONTROL);
  118. }
  119. static int __init mxc_rnga_probe(struct platform_device *pdev)
  120. {
  121. int err = -ENODEV;
  122. struct resource *res;
  123. struct mxc_rng *mxc_rng;
  124. mxc_rng = devm_kzalloc(&pdev->dev, sizeof(struct mxc_rng),
  125. GFP_KERNEL);
  126. if (!mxc_rng)
  127. return -ENOMEM;
  128. mxc_rng->dev = &pdev->dev;
  129. mxc_rng->rng.name = "mxc-rnga";
  130. mxc_rng->rng.init = mxc_rnga_init;
  131. mxc_rng->rng.cleanup = mxc_rnga_cleanup,
  132. mxc_rng->rng.data_present = mxc_rnga_data_present,
  133. mxc_rng->rng.data_read = mxc_rnga_data_read,
  134. mxc_rng->clk = devm_clk_get(&pdev->dev, NULL);
  135. if (IS_ERR(mxc_rng->clk)) {
  136. dev_err(&pdev->dev, "Could not get rng_clk!\n");
  137. err = PTR_ERR(mxc_rng->clk);
  138. goto out;
  139. }
  140. err = clk_prepare_enable(mxc_rng->clk);
  141. if (err)
  142. goto out;
  143. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  144. mxc_rng->mem = devm_ioremap_resource(&pdev->dev, res);
  145. if (IS_ERR(mxc_rng->mem)) {
  146. err = PTR_ERR(mxc_rng->mem);
  147. goto err_ioremap;
  148. }
  149. err = hwrng_register(&mxc_rng->rng);
  150. if (err) {
  151. dev_err(&pdev->dev, "MXC RNGA registering failed (%d)\n", err);
  152. goto err_ioremap;
  153. }
  154. dev_info(&pdev->dev, "MXC RNGA Registered.\n");
  155. return 0;
  156. err_ioremap:
  157. clk_disable_unprepare(mxc_rng->clk);
  158. out:
  159. return err;
  160. }
  161. static int __exit mxc_rnga_remove(struct platform_device *pdev)
  162. {
  163. struct mxc_rng *mxc_rng = platform_get_drvdata(pdev);
  164. hwrng_unregister(&mxc_rng->rng);
  165. clk_disable_unprepare(mxc_rng->clk);
  166. return 0;
  167. }
  168. static struct platform_driver mxc_rnga_driver = {
  169. .driver = {
  170. .name = "mxc_rnga",
  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");