turris-omnia-mcu-trng.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CZ.NIC's Turris Omnia MCU TRNG driver
  4. *
  5. * 2024 by Marek Behún <kabel@kernel.org>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/completion.h>
  9. #include <linux/container_of.h>
  10. #include <linux/errno.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/hw_random.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/minmax.h>
  17. #include <linux/string.h>
  18. #include <linux/types.h>
  19. #include <linux/turris-omnia-mcu-interface.h>
  20. #include "turris-omnia-mcu.h"
  21. #define OMNIA_CMD_TRNG_MAX_ENTROPY_LEN 64
  22. static irqreturn_t omnia_trng_irq_handler(int irq, void *dev_id)
  23. {
  24. struct omnia_mcu *mcu = dev_id;
  25. complete(&mcu->trng_entropy_ready);
  26. return IRQ_HANDLED;
  27. }
  28. static int omnia_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  29. {
  30. struct omnia_mcu *mcu = container_of(rng, struct omnia_mcu, trng);
  31. u8 reply[1 + OMNIA_CMD_TRNG_MAX_ENTROPY_LEN];
  32. int err, bytes;
  33. if (!wait && !completion_done(&mcu->trng_entropy_ready))
  34. return 0;
  35. do {
  36. if (wait_for_completion_interruptible(&mcu->trng_entropy_ready))
  37. return -ERESTARTSYS;
  38. err = omnia_cmd_read(mcu->client,
  39. OMNIA_CMD_TRNG_COLLECT_ENTROPY,
  40. reply, sizeof(reply));
  41. if (err)
  42. return err;
  43. bytes = min3(reply[0], max, OMNIA_CMD_TRNG_MAX_ENTROPY_LEN);
  44. } while (wait && !bytes);
  45. memcpy(data, &reply[1], bytes);
  46. return bytes;
  47. }
  48. int omnia_mcu_register_trng(struct omnia_mcu *mcu)
  49. {
  50. struct device *dev = &mcu->client->dev;
  51. u8 irq_idx, dummy;
  52. int irq, err;
  53. if (!(mcu->features & OMNIA_FEAT_TRNG))
  54. return 0;
  55. irq_idx = omnia_int_to_gpio_idx[__bf_shf(OMNIA_INT_TRNG)];
  56. irq = gpiod_to_irq(gpio_device_get_desc(mcu->gc.gpiodev, irq_idx));
  57. if (irq < 0)
  58. return dev_err_probe(dev, irq, "Cannot get TRNG IRQ\n");
  59. /*
  60. * If someone else cleared the TRNG interrupt but did not read the
  61. * entropy, a new interrupt won't be generated, and entropy collection
  62. * will be stuck. Ensure an interrupt will be generated by executing
  63. * the collect entropy command (and discarding the result).
  64. */
  65. err = omnia_cmd_read(mcu->client, OMNIA_CMD_TRNG_COLLECT_ENTROPY,
  66. &dummy, 1);
  67. if (err)
  68. return err;
  69. init_completion(&mcu->trng_entropy_ready);
  70. err = devm_request_threaded_irq(dev, irq, NULL, omnia_trng_irq_handler,
  71. IRQF_ONESHOT, "turris-omnia-mcu-trng",
  72. mcu);
  73. if (err)
  74. return dev_err_probe(dev, err, "Cannot request TRNG IRQ\n");
  75. mcu->trng.name = "turris-omnia-mcu-trng";
  76. mcu->trng.read = omnia_trng_read;
  77. err = devm_hwrng_register(dev, &mcu->trng);
  78. if (err)
  79. return dev_err_probe(dev, err, "Cannot register TRNG\n");
  80. return 0;
  81. }