pasemi-rng.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_address.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/io.h>
  29. #define SDCRNG_CTL_REG 0x00
  30. #define SDCRNG_CTL_FVLD_M 0x0000f000
  31. #define SDCRNG_CTL_FVLD_S 12
  32. #define SDCRNG_CTL_KSZ 0x00000800
  33. #define SDCRNG_CTL_RSRC_CRG 0x00000010
  34. #define SDCRNG_CTL_RSRC_RRG 0x00000000
  35. #define SDCRNG_CTL_CE 0x00000004
  36. #define SDCRNG_CTL_RE 0x00000002
  37. #define SDCRNG_CTL_DR 0x00000001
  38. #define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
  39. #define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
  40. #define SDCRNG_VAL_REG 0x20
  41. #define MODULE_NAME "pasemi_rng"
  42. static int pasemi_rng_data_present(struct hwrng *rng, int wait)
  43. {
  44. void __iomem *rng_regs = (void __iomem *)rng->priv;
  45. int data, i;
  46. for (i = 0; i < 20; i++) {
  47. data = (in_le32(rng_regs + SDCRNG_CTL_REG)
  48. & SDCRNG_CTL_FVLD_M) ? 1 : 0;
  49. if (data || !wait)
  50. break;
  51. udelay(10);
  52. }
  53. return data;
  54. }
  55. static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
  56. {
  57. void __iomem *rng_regs = (void __iomem *)rng->priv;
  58. *data = in_le32(rng_regs + SDCRNG_VAL_REG);
  59. return 4;
  60. }
  61. static int pasemi_rng_init(struct hwrng *rng)
  62. {
  63. void __iomem *rng_regs = (void __iomem *)rng->priv;
  64. u32 ctl;
  65. ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
  66. out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
  67. out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
  68. return 0;
  69. }
  70. static void pasemi_rng_cleanup(struct hwrng *rng)
  71. {
  72. void __iomem *rng_regs = (void __iomem *)rng->priv;
  73. u32 ctl;
  74. ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
  75. out_le32(rng_regs + SDCRNG_CTL_REG,
  76. in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
  77. }
  78. static struct hwrng pasemi_rng = {
  79. .name = MODULE_NAME,
  80. .init = pasemi_rng_init,
  81. .cleanup = pasemi_rng_cleanup,
  82. .data_present = pasemi_rng_data_present,
  83. .data_read = pasemi_rng_data_read,
  84. };
  85. static int rng_probe(struct platform_device *pdev)
  86. {
  87. void __iomem *rng_regs;
  88. struct resource *res;
  89. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  90. rng_regs = devm_ioremap_resource(&pdev->dev, res);
  91. if (IS_ERR(rng_regs))
  92. return PTR_ERR(rng_regs);
  93. pasemi_rng.priv = (unsigned long)rng_regs;
  94. pr_info("Registering PA Semi RNG\n");
  95. return devm_hwrng_register(&pdev->dev, &pasemi_rng);
  96. }
  97. static const struct of_device_id rng_match[] = {
  98. { .compatible = "1682m-rng", },
  99. { .compatible = "pasemi,pwrficient-rng", },
  100. { },
  101. };
  102. MODULE_DEVICE_TABLE(of, rng_match);
  103. static struct platform_driver rng_driver = {
  104. .driver = {
  105. .name = "pasemi-rng",
  106. .of_match_table = rng_match,
  107. },
  108. .probe = rng_probe,
  109. };
  110. module_platform_driver(rng_driver);
  111. MODULE_LICENSE("GPL");
  112. MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
  113. MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");