pasemi-rng.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2006-2007 PA Semi, Inc
  3. *
  4. * Maintained by: Olof Johansson <olof@lixom.net>
  5. *
  6. * Driver for the PWRficient onchip rng
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/hw_random.h>
  25. #include <linux/delay.h>
  26. #include <linux/of_platform.h>
  27. #include <asm/io.h>
  28. #define SDCRNG_CTL_REG 0x00
  29. #define SDCRNG_CTL_FVLD_M 0x0000f000
  30. #define SDCRNG_CTL_FVLD_S 12
  31. #define SDCRNG_CTL_KSZ 0x00000800
  32. #define SDCRNG_CTL_RSRC_CRG 0x00000010
  33. #define SDCRNG_CTL_RSRC_RRG 0x00000000
  34. #define SDCRNG_CTL_CE 0x00000004
  35. #define SDCRNG_CTL_RE 0x00000002
  36. #define SDCRNG_CTL_DR 0x00000001
  37. #define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
  38. #define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
  39. #define SDCRNG_VAL_REG 0x20
  40. #define MODULE_NAME "pasemi_rng"
  41. static int pasemi_rng_data_present(struct hwrng *rng, int wait)
  42. {
  43. void __iomem *rng_regs = (void __iomem *)rng->priv;
  44. int data, i;
  45. for (i = 0; i < 20; i++) {
  46. data = (in_le32(rng_regs + SDCRNG_CTL_REG)
  47. & SDCRNG_CTL_FVLD_M) ? 1 : 0;
  48. if (data || !wait)
  49. break;
  50. udelay(10);
  51. }
  52. return data;
  53. }
  54. static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
  55. {
  56. void __iomem *rng_regs = (void __iomem *)rng->priv;
  57. *data = in_le32(rng_regs + SDCRNG_VAL_REG);
  58. return 4;
  59. }
  60. static int pasemi_rng_init(struct hwrng *rng)
  61. {
  62. void __iomem *rng_regs = (void __iomem *)rng->priv;
  63. u32 ctl;
  64. ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
  65. out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
  66. out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
  67. return 0;
  68. }
  69. static void pasemi_rng_cleanup(struct hwrng *rng)
  70. {
  71. void __iomem *rng_regs = (void __iomem *)rng->priv;
  72. u32 ctl;
  73. ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
  74. out_le32(rng_regs + SDCRNG_CTL_REG,
  75. in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
  76. }
  77. static struct hwrng pasemi_rng = {
  78. .name = MODULE_NAME,
  79. .init = pasemi_rng_init,
  80. .cleanup = pasemi_rng_cleanup,
  81. .data_present = pasemi_rng_data_present,
  82. .data_read = pasemi_rng_data_read,
  83. };
  84. static int __devinit rng_probe(struct platform_device *ofdev)
  85. {
  86. void __iomem *rng_regs;
  87. struct device_node *rng_np = ofdev->dev.of_node;
  88. struct resource res;
  89. int err = 0;
  90. err = of_address_to_resource(rng_np, 0, &res);
  91. if (err)
  92. return -ENODEV;
  93. rng_regs = ioremap(res.start, 0x100);
  94. if (!rng_regs)
  95. return -ENOMEM;
  96. pasemi_rng.priv = (unsigned long)rng_regs;
  97. printk(KERN_INFO "Registering PA Semi RNG\n");
  98. err = hwrng_register(&pasemi_rng);
  99. if (err)
  100. iounmap(rng_regs);
  101. return err;
  102. }
  103. static int __devexit rng_remove(struct platform_device *dev)
  104. {
  105. void __iomem *rng_regs = (void __iomem *)pasemi_rng.priv;
  106. hwrng_unregister(&pasemi_rng);
  107. iounmap(rng_regs);
  108. return 0;
  109. }
  110. static struct of_device_id rng_match[] = {
  111. { .compatible = "1682m-rng", },
  112. { .compatible = "pasemi,pwrficient-rng", },
  113. { },
  114. };
  115. static struct platform_driver rng_driver = {
  116. .driver = {
  117. .name = "pasemi-rng",
  118. .owner = THIS_MODULE,
  119. .of_match_table = rng_match,
  120. },
  121. .probe = rng_probe,
  122. .remove = rng_remove,
  123. };
  124. static int __init rng_init(void)
  125. {
  126. return platform_driver_register(&rng_driver);
  127. }
  128. module_init(rng_init);
  129. static void __exit rng_exit(void)
  130. {
  131. platform_driver_unregister(&rng_driver);
  132. }
  133. module_exit(rng_exit);
  134. MODULE_LICENSE("GPL");
  135. MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
  136. MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");