ppc4xx-rng.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Generic PowerPC 44x RNG driver
  3. *
  4. * Copyright 2011 IBM Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; version 2 of the License.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/hw_random.h>
  14. #include <linux/delay.h>
  15. #include <linux/of_platform.h>
  16. #include <asm/io.h>
  17. #define PPC4XX_TRNG_DEV_CTRL 0x60080
  18. #define PPC4XX_TRNGE 0x00020000
  19. #define PPC4XX_TRNG_CTRL 0x0008
  20. #define PPC4XX_TRNG_CTRL_DALM 0x20
  21. #define PPC4XX_TRNG_STAT 0x0004
  22. #define PPC4XX_TRNG_STAT_B 0x1
  23. #define PPC4XX_TRNG_DATA 0x0000
  24. #define MODULE_NAME "ppc4xx_rng"
  25. static int ppc4xx_rng_data_present(struct hwrng *rng, int wait)
  26. {
  27. void __iomem *rng_regs = (void __iomem *) rng->priv;
  28. int busy, i, present = 0;
  29. for (i = 0; i < 20; i++) {
  30. busy = (in_le32(rng_regs + PPC4XX_TRNG_STAT) & PPC4XX_TRNG_STAT_B);
  31. if (!busy || !wait) {
  32. present = 1;
  33. break;
  34. }
  35. udelay(10);
  36. }
  37. return present;
  38. }
  39. static int ppc4xx_rng_data_read(struct hwrng *rng, u32 *data)
  40. {
  41. void __iomem *rng_regs = (void __iomem *) rng->priv;
  42. *data = in_le32(rng_regs + PPC4XX_TRNG_DATA);
  43. return 4;
  44. }
  45. static int ppc4xx_rng_enable(int enable)
  46. {
  47. struct device_node *ctrl;
  48. void __iomem *ctrl_reg;
  49. int err = 0;
  50. u32 val;
  51. /* Find the main crypto device node and map it to turn the TRNG on */
  52. ctrl = of_find_compatible_node(NULL, NULL, "amcc,ppc4xx-crypto");
  53. if (!ctrl)
  54. return -ENODEV;
  55. ctrl_reg = of_iomap(ctrl, 0);
  56. if (!ctrl_reg) {
  57. err = -ENODEV;
  58. goto out;
  59. }
  60. val = in_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL);
  61. if (enable)
  62. val |= PPC4XX_TRNGE;
  63. else
  64. val = val & ~PPC4XX_TRNGE;
  65. out_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL, val);
  66. iounmap(ctrl_reg);
  67. out:
  68. of_node_put(ctrl);
  69. return err;
  70. }
  71. static struct hwrng ppc4xx_rng = {
  72. .name = MODULE_NAME,
  73. .data_present = ppc4xx_rng_data_present,
  74. .data_read = ppc4xx_rng_data_read,
  75. };
  76. static int __devinit ppc4xx_rng_probe(struct platform_device *dev)
  77. {
  78. void __iomem *rng_regs;
  79. int err = 0;
  80. rng_regs = of_iomap(dev->dev.of_node, 0);
  81. if (!rng_regs)
  82. return -ENODEV;
  83. err = ppc4xx_rng_enable(1);
  84. if (err)
  85. return err;
  86. out_le32(rng_regs + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
  87. ppc4xx_rng.priv = (unsigned long) rng_regs;
  88. err = hwrng_register(&ppc4xx_rng);
  89. return err;
  90. }
  91. static int __devexit ppc4xx_rng_remove(struct platform_device *dev)
  92. {
  93. void __iomem *rng_regs = (void __iomem *) ppc4xx_rng.priv;
  94. hwrng_unregister(&ppc4xx_rng);
  95. ppc4xx_rng_enable(0);
  96. iounmap(rng_regs);
  97. return 0;
  98. }
  99. static struct of_device_id ppc4xx_rng_match[] = {
  100. { .compatible = "ppc4xx-rng", },
  101. { .compatible = "amcc,ppc460ex-rng", },
  102. { .compatible = "amcc,ppc440epx-rng", },
  103. {},
  104. };
  105. static struct platform_driver ppc4xx_rng_driver = {
  106. .driver = {
  107. .name = MODULE_NAME,
  108. .owner = THIS_MODULE,
  109. .of_match_table = ppc4xx_rng_match,
  110. },
  111. .probe = ppc4xx_rng_probe,
  112. .remove = ppc4xx_rng_remove,
  113. };
  114. static int __init ppc4xx_rng_init(void)
  115. {
  116. return platform_driver_register(&ppc4xx_rng_driver);
  117. }
  118. module_init(ppc4xx_rng_init);
  119. static void __exit ppc4xx_rng_exit(void)
  120. {
  121. platform_driver_unregister(&ppc4xx_rng_driver);
  122. }
  123. module_exit(ppc4xx_rng_exit);
  124. MODULE_LICENSE("GPL");
  125. MODULE_AUTHOR("Josh Boyer <jwboyer@linux.vnet.ibm.com>");
  126. MODULE_DESCRIPTION("HW RNG driver for PPC 4xx processors");