crypto4xx_trng.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/hw_random.h>
  15. #include <linux/delay.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/io.h>
  19. #include "crypto4xx_core.h"
  20. #include "crypto4xx_trng.h"
  21. #include "crypto4xx_reg_def.h"
  22. #define PPC4XX_TRNG_CTRL 0x0008
  23. #define PPC4XX_TRNG_CTRL_DALM 0x20
  24. #define PPC4XX_TRNG_STAT 0x0004
  25. #define PPC4XX_TRNG_STAT_B 0x1
  26. #define PPC4XX_TRNG_DATA 0x0000
  27. static int ppc4xx_trng_data_present(struct hwrng *rng, int wait)
  28. {
  29. struct crypto4xx_device *dev = (void *)rng->priv;
  30. int busy, i, present = 0;
  31. for (i = 0; i < 20; i++) {
  32. busy = (in_le32(dev->trng_base + PPC4XX_TRNG_STAT) &
  33. PPC4XX_TRNG_STAT_B);
  34. if (!busy || !wait) {
  35. present = 1;
  36. break;
  37. }
  38. udelay(10);
  39. }
  40. return present;
  41. }
  42. static int ppc4xx_trng_data_read(struct hwrng *rng, u32 *data)
  43. {
  44. struct crypto4xx_device *dev = (void *)rng->priv;
  45. *data = in_le32(dev->trng_base + PPC4XX_TRNG_DATA);
  46. return 4;
  47. }
  48. static void ppc4xx_trng_enable(struct crypto4xx_device *dev, bool enable)
  49. {
  50. u32 device_ctrl;
  51. device_ctrl = readl(dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
  52. if (enable)
  53. device_ctrl |= PPC4XX_TRNG_EN;
  54. else
  55. device_ctrl &= ~PPC4XX_TRNG_EN;
  56. writel(device_ctrl, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
  57. }
  58. static const struct of_device_id ppc4xx_trng_match[] = {
  59. { .compatible = "ppc4xx-rng", },
  60. { .compatible = "amcc,ppc460ex-rng", },
  61. { .compatible = "amcc,ppc440epx-rng", },
  62. {},
  63. };
  64. void ppc4xx_trng_probe(struct crypto4xx_core_device *core_dev)
  65. {
  66. struct crypto4xx_device *dev = core_dev->dev;
  67. struct device_node *trng = NULL;
  68. struct hwrng *rng = NULL;
  69. int err;
  70. /* Find the TRNG device node and map it */
  71. trng = of_find_matching_node(NULL, ppc4xx_trng_match);
  72. if (!trng || !of_device_is_available(trng)) {
  73. of_node_put(trng);
  74. return;
  75. }
  76. dev->trng_base = of_iomap(trng, 0);
  77. of_node_put(trng);
  78. if (!dev->trng_base)
  79. goto err_out;
  80. rng = kzalloc(sizeof(*rng), GFP_KERNEL);
  81. if (!rng)
  82. goto err_out;
  83. rng->name = KBUILD_MODNAME;
  84. rng->data_present = ppc4xx_trng_data_present;
  85. rng->data_read = ppc4xx_trng_data_read;
  86. rng->priv = (unsigned long) dev;
  87. core_dev->trng = rng;
  88. ppc4xx_trng_enable(dev, true);
  89. out_le32(dev->trng_base + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
  90. err = devm_hwrng_register(core_dev->device, core_dev->trng);
  91. if (err) {
  92. ppc4xx_trng_enable(dev, false);
  93. dev_err(core_dev->device, "failed to register hwrng (%d).\n",
  94. err);
  95. goto err_out;
  96. }
  97. return;
  98. err_out:
  99. iounmap(dev->trng_base);
  100. kfree(rng);
  101. dev->trng_base = NULL;
  102. core_dev->trng = NULL;
  103. }
  104. void ppc4xx_trng_remove(struct crypto4xx_core_device *core_dev)
  105. {
  106. if (core_dev && core_dev->trng) {
  107. struct crypto4xx_device *dev = core_dev->dev;
  108. devm_hwrng_unregister(core_dev->device, core_dev->trng);
  109. ppc4xx_trng_enable(dev, false);
  110. iounmap(dev->trng_base);
  111. kfree(core_dev->trng);
  112. }
  113. }
  114. MODULE_ALIAS("ppc4xx_rng");