irq_sandbox.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sandbox driver for interrupts
  4. *
  5. * Copyright 2019 Google LLC
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <irq.h>
  10. #include <asm/test.h>
  11. /**
  12. * struct sandbox_irq_priv - private data for this driver
  13. *
  14. * @count: Counts the number calls to the read_and_clear() method
  15. * @pending: true if an interrupt is pending, else false
  16. */
  17. struct sandbox_irq_priv {
  18. int count;
  19. bool pending;
  20. };
  21. static int sandbox_set_polarity(struct udevice *dev, uint irq, bool active_low)
  22. {
  23. if (irq > 10)
  24. return -EINVAL;
  25. return 0;
  26. }
  27. static int sandbox_route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
  28. {
  29. if (pmc_gpe_num > 10)
  30. return -ENOENT;
  31. return pmc_gpe_num + 1;
  32. }
  33. static int sandbox_snapshot_polarities(struct udevice *dev)
  34. {
  35. return 0;
  36. }
  37. static int sandbox_restore_polarities(struct udevice *dev)
  38. {
  39. return 0;
  40. }
  41. static int sandbox_irq_read_and_clear(struct irq *irq)
  42. {
  43. struct sandbox_irq_priv *priv = dev_get_priv(irq->dev);
  44. if (irq->id != SANDBOX_IRQN_PEND)
  45. return -EINVAL;
  46. priv->count++;
  47. if (priv->pending) {
  48. priv->pending = false;
  49. return 1;
  50. }
  51. if (!(priv->count % 3))
  52. priv->pending = true;
  53. return 0;
  54. }
  55. static int sandbox_irq_of_xlate(struct irq *irq,
  56. struct ofnode_phandle_args *args)
  57. {
  58. irq->id = args->args[0];
  59. return 0;
  60. }
  61. static const struct irq_ops sandbox_irq_ops = {
  62. .route_pmc_gpio_gpe = sandbox_route_pmc_gpio_gpe,
  63. .set_polarity = sandbox_set_polarity,
  64. .snapshot_polarities = sandbox_snapshot_polarities,
  65. .restore_polarities = sandbox_restore_polarities,
  66. .read_and_clear = sandbox_irq_read_and_clear,
  67. .of_xlate = sandbox_irq_of_xlate,
  68. };
  69. static const struct udevice_id sandbox_irq_ids[] = {
  70. { .compatible = "sandbox,irq", SANDBOX_IRQT_BASE },
  71. { }
  72. };
  73. U_BOOT_DRIVER(sandbox_irq_drv) = {
  74. .name = "sandbox_irq",
  75. .id = UCLASS_IRQ,
  76. .of_match = sandbox_irq_ids,
  77. .ops = &sandbox_irq_ops,
  78. .priv_auto_alloc_size = sizeof(struct sandbox_irq_priv),
  79. };