ppc4xx-rng.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_address.h>
  16. #include <linux/of_platform.h>
  17. #include <asm/io.h>
  18. #define PPC4XX_TRNG_DEV_CTRL 0x60080
  19. #define PPC4XX_TRNGE 0x00020000
  20. #define PPC4XX_TRNG_CTRL 0x0008
  21. #define PPC4XX_TRNG_CTRL_DALM 0x20
  22. #define PPC4XX_TRNG_STAT 0x0004
  23. #define PPC4XX_TRNG_STAT_B 0x1
  24. #define PPC4XX_TRNG_DATA 0x0000
  25. #define MODULE_NAME "ppc4xx_rng"
  26. static int ppc4xx_rng_data_present(struct hwrng *rng, int wait)
  27. {
  28. void __iomem *rng_regs = (void __iomem *) rng->priv;
  29. int busy, i, present = 0;
  30. for (i = 0; i < 20; i++) {
  31. busy = (in_le32(rng_regs + PPC4XX_TRNG_STAT) & PPC4XX_TRNG_STAT_B);
  32. if (!busy || !wait) {
  33. present = 1;
  34. break;
  35. }
  36. udelay(10);
  37. }
  38. return present;
  39. }
  40. static int ppc4xx_rng_data_read(struct hwrng *rng, u32 *data)
  41. {
  42. void __iomem *rng_regs = (void __iomem *) rng->priv;
  43. *data = in_le32(rng_regs + PPC4XX_TRNG_DATA);
  44. return 4;
  45. }
  46. static int ppc4xx_rng_enable(int enable)
  47. {
  48. struct device_node *ctrl;
  49. void __iomem *ctrl_reg;
  50. int err = 0;
  51. u32 val;
  52. /* Find the main crypto device node and map it to turn the TRNG on */
  53. ctrl = of_find_compatible_node(NULL, NULL, "amcc,ppc4xx-crypto");
  54. if (!ctrl)
  55. return -ENODEV;
  56. ctrl_reg = of_iomap(ctrl, 0);
  57. if (!ctrl_reg) {
  58. err = -ENODEV;
  59. goto out;
  60. }
  61. val = in_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL);
  62. if (enable)
  63. val |= PPC4XX_TRNGE;
  64. else
  65. val = val & ~PPC4XX_TRNGE;
  66. out_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL, val);
  67. iounmap(ctrl_reg);
  68. out:
  69. of_node_put(ctrl);
  70. return err;
  71. }
  72. static struct hwrng ppc4xx_rng = {
  73. .name = MODULE_NAME,
  74. .data_present = ppc4xx_rng_data_present,
  75. .data_read = ppc4xx_rng_data_read,
  76. };
  77. static int ppc4xx_rng_probe(struct platform_device *dev)
  78. {
  79. void __iomem *rng_regs;
  80. int err = 0;
  81. rng_regs = of_iomap(dev->dev.of_node, 0);
  82. if (!rng_regs)
  83. return -ENODEV;
  84. err = ppc4xx_rng_enable(1);
  85. if (err)
  86. return err;
  87. out_le32(rng_regs + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
  88. ppc4xx_rng.priv = (unsigned long) rng_regs;
  89. err = hwrng_register(&ppc4xx_rng);
  90. return err;
  91. }
  92. static int ppc4xx_rng_remove(struct platform_device *dev)
  93. {
  94. void __iomem *rng_regs = (void __iomem *) ppc4xx_rng.priv;
  95. hwrng_unregister(&ppc4xx_rng);
  96. ppc4xx_rng_enable(0);
  97. iounmap(rng_regs);
  98. return 0;
  99. }
  100. static const struct of_device_id ppc4xx_rng_match[] = {
  101. { .compatible = "ppc4xx-rng", },
  102. { .compatible = "amcc,ppc460ex-rng", },
  103. { .compatible = "amcc,ppc440epx-rng", },
  104. {},
  105. };
  106. MODULE_DEVICE_TABLE(of, ppc4xx_rng_match);
  107. static struct platform_driver ppc4xx_rng_driver = {
  108. .driver = {
  109. .name = MODULE_NAME,
  110. .of_match_table = ppc4xx_rng_match,
  111. },
  112. .probe = ppc4xx_rng_probe,
  113. .remove = ppc4xx_rng_remove,
  114. };
  115. module_platform_driver(ppc4xx_rng_driver);
  116. MODULE_LICENSE("GPL");
  117. MODULE_AUTHOR("Josh Boyer <jwboyer@linux.vnet.ibm.com>");
  118. MODULE_DESCRIPTION("HW RNG driver for PPC 4xx processors");