mxc-rnga.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/io.h>
  26. /* RNGA Registers */
  27. #define RNGA_CONTROL 0x00
  28. #define RNGA_STATUS 0x04
  29. #define RNGA_ENTROPY 0x08
  30. #define RNGA_OUTPUT_FIFO 0x0c
  31. #define RNGA_MODE 0x10
  32. #define RNGA_VERIFICATION_CONTROL 0x14
  33. #define RNGA_OSC_CONTROL_COUNTER 0x18
  34. #define RNGA_OSC1_COUNTER 0x1c
  35. #define RNGA_OSC2_COUNTER 0x20
  36. #define RNGA_OSC_COUNTER_STATUS 0x24
  37. /* RNGA Registers Range */
  38. #define RNG_ADDR_RANGE 0x28
  39. /* RNGA Control Register */
  40. #define RNGA_CONTROL_SLEEP 0x00000010
  41. #define RNGA_CONTROL_CLEAR_INT 0x00000008
  42. #define RNGA_CONTROL_MASK_INTS 0x00000004
  43. #define RNGA_CONTROL_HIGH_ASSURANCE 0x00000002
  44. #define RNGA_CONTROL_GO 0x00000001
  45. #define RNGA_STATUS_LEVEL_MASK 0x0000ff00
  46. /* RNGA Status Register */
  47. #define RNGA_STATUS_OSC_DEAD 0x80000000
  48. #define RNGA_STATUS_SLEEP 0x00000010
  49. #define RNGA_STATUS_ERROR_INT 0x00000008
  50. #define RNGA_STATUS_FIFO_UNDERFLOW 0x00000004
  51. #define RNGA_STATUS_LAST_READ_STATUS 0x00000002
  52. #define RNGA_STATUS_SECURITY_VIOLATION 0x00000001
  53. static struct platform_device *rng_dev;
  54. static int mxc_rnga_data_present(struct hwrng *rng)
  55. {
  56. int level;
  57. void __iomem *rng_base = (void __iomem *)rng->priv;
  58. /* how many random numbers is in FIFO? [0-16] */
  59. level = ((__raw_readl(rng_base + RNGA_STATUS) &
  60. RNGA_STATUS_LEVEL_MASK) >> 8);
  61. return level > 0 ? 1 : 0;
  62. }
  63. static int mxc_rnga_data_read(struct hwrng *rng, u32 * data)
  64. {
  65. int err;
  66. u32 ctrl;
  67. void __iomem *rng_base = (void __iomem *)rng->priv;
  68. /* retrieve a random number from FIFO */
  69. *data = __raw_readl(rng_base + RNGA_OUTPUT_FIFO);
  70. /* some error while reading this random number? */
  71. err = __raw_readl(rng_base + RNGA_STATUS) & RNGA_STATUS_ERROR_INT;
  72. /* if error: clear error interrupt, but doesn't return random number */
  73. if (err) {
  74. dev_dbg(&rng_dev->dev, "Error while reading random number!\n");
  75. ctrl = __raw_readl(rng_base + RNGA_CONTROL);
  76. __raw_writel(ctrl | RNGA_CONTROL_CLEAR_INT,
  77. rng_base + RNGA_CONTROL);
  78. return 0;
  79. } else
  80. return 4;
  81. }
  82. static int mxc_rnga_init(struct hwrng *rng)
  83. {
  84. u32 ctrl, osc;
  85. void __iomem *rng_base = (void __iomem *)rng->priv;
  86. /* wake up */
  87. ctrl = __raw_readl(rng_base + RNGA_CONTROL);
  88. __raw_writel(ctrl & ~RNGA_CONTROL_SLEEP, rng_base + RNGA_CONTROL);
  89. /* verify if oscillator is working */
  90. osc = __raw_readl(rng_base + RNGA_STATUS);
  91. if (osc & RNGA_STATUS_OSC_DEAD) {
  92. dev_err(&rng_dev->dev, "RNGA Oscillator is dead!\n");
  93. return -ENODEV;
  94. }
  95. /* go running */
  96. ctrl = __raw_readl(rng_base + RNGA_CONTROL);
  97. __raw_writel(ctrl | RNGA_CONTROL_GO, rng_base + RNGA_CONTROL);
  98. return 0;
  99. }
  100. static void mxc_rnga_cleanup(struct hwrng *rng)
  101. {
  102. u32 ctrl;
  103. void __iomem *rng_base = (void __iomem *)rng->priv;
  104. ctrl = __raw_readl(rng_base + RNGA_CONTROL);
  105. /* stop rnga */
  106. __raw_writel(ctrl & ~RNGA_CONTROL_GO, rng_base + RNGA_CONTROL);
  107. }
  108. static struct hwrng mxc_rnga = {
  109. .name = "mxc-rnga",
  110. .init = mxc_rnga_init,
  111. .cleanup = mxc_rnga_cleanup,
  112. .data_present = mxc_rnga_data_present,
  113. .data_read = mxc_rnga_data_read
  114. };
  115. static int __init mxc_rnga_probe(struct platform_device *pdev)
  116. {
  117. int err = -ENODEV;
  118. struct clk *clk;
  119. struct resource *res, *mem;
  120. void __iomem *rng_base = NULL;
  121. if (rng_dev)
  122. return -EBUSY;
  123. clk = clk_get(&pdev->dev, "rng");
  124. if (IS_ERR(clk)) {
  125. dev_err(&pdev->dev, "Could not get rng_clk!\n");
  126. err = PTR_ERR(clk);
  127. goto out;
  128. }
  129. clk_enable(clk);
  130. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. if (!res) {
  132. err = -ENOENT;
  133. goto err_region;
  134. }
  135. mem = request_mem_region(res->start, resource_size(res), pdev->name);
  136. if (mem == NULL) {
  137. err = -EBUSY;
  138. goto err_region;
  139. }
  140. rng_base = ioremap(res->start, resource_size(res));
  141. if (!rng_base) {
  142. err = -ENOMEM;
  143. goto err_ioremap;
  144. }
  145. mxc_rnga.priv = (unsigned long)rng_base;
  146. err = hwrng_register(&mxc_rnga);
  147. if (err) {
  148. dev_err(&pdev->dev, "MXC RNGA registering failed (%d)\n", err);
  149. goto err_register;
  150. }
  151. rng_dev = pdev;
  152. dev_info(&pdev->dev, "MXC RNGA Registered.\n");
  153. return 0;
  154. err_register:
  155. iounmap(rng_base);
  156. rng_base = NULL;
  157. err_ioremap:
  158. release_mem_region(res->start, resource_size(res));
  159. err_region:
  160. clk_disable(clk);
  161. clk_put(clk);
  162. out:
  163. return err;
  164. }
  165. static int __exit mxc_rnga_remove(struct platform_device *pdev)
  166. {
  167. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  168. void __iomem *rng_base = (void __iomem *)mxc_rnga.priv;
  169. struct clk *clk = clk_get(&pdev->dev, "rng");
  170. hwrng_unregister(&mxc_rnga);
  171. iounmap(rng_base);
  172. release_mem_region(res->start, resource_size(res));
  173. clk_disable(clk);
  174. clk_put(clk);
  175. return 0;
  176. }
  177. static struct platform_driver mxc_rnga_driver = {
  178. .driver = {
  179. .name = "mxc_rnga",
  180. .owner = THIS_MODULE,
  181. },
  182. .remove = __exit_p(mxc_rnga_remove),
  183. };
  184. static int __init mod_init(void)
  185. {
  186. return platform_driver_probe(&mxc_rnga_driver, mxc_rnga_probe);
  187. }
  188. static void __exit mod_exit(void)
  189. {
  190. platform_driver_unregister(&mxc_rnga_driver);
  191. }
  192. module_init(mod_init);
  193. module_exit(mod_exit);
  194. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  195. MODULE_DESCRIPTION("H/W RNGA driver for i.MX");
  196. MODULE_LICENSE("GPL");