pic-gic-common.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-01-30 GuEe-GUI first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #define DBG_TAG "pic.gic*"
  13. #define DBG_LVL DBG_LOG
  14. #include <rtdbg.h>
  15. #include <drivers/pic.h>
  16. #include "pic-gicv2.h"
  17. #include "pic-gic-common.h"
  18. void gic_common_init_quirk_ofw(const struct rt_ofw_node *ic_np, const struct gic_quirk *quirks, void *data)
  19. {
  20. for (; quirks->desc; ++quirks)
  21. {
  22. if (!quirks->compatible || !rt_ofw_node_is_compatible(ic_np, quirks->compatible))
  23. {
  24. continue;
  25. }
  26. RT_ASSERT(quirks->init != RT_NULL);
  27. if (!quirks->init(data))
  28. {
  29. LOG_I("Enable workaround for %s", quirks->desc);
  30. }
  31. }
  32. }
  33. void gic_common_init_quirk_hw(rt_uint32_t iidr, const struct gic_quirk *quirks, void *data)
  34. {
  35. for (; quirks->desc; ++quirks)
  36. {
  37. if (quirks->compatible)
  38. {
  39. continue;
  40. }
  41. if (quirks->iidr == (iidr & quirks->iidr_mask))
  42. {
  43. RT_ASSERT(quirks->init != RT_NULL);
  44. if (!quirks->init(data))
  45. {
  46. LOG_I("Enable workaround for %s", quirks->desc);
  47. }
  48. }
  49. }
  50. }
  51. void gic_common_sgi_config(void *base, void *data, int irq_base)
  52. {
  53. #ifdef RT_USING_SMP
  54. if (irq_base < 2)
  55. {
  56. struct rt_pic_irq *pirq;
  57. #define DECLARE_GIC_IPI(ipi, hwirq) \
  58. rt_pic_config_ipi(data, ipi, hwirq); \
  59. pirq = rt_pic_find_ipi(data, ipi); \
  60. pirq->mode = RT_IRQ_MODE_EDGE_RISING; \
  61. DECLARE_GIC_IPI(RT_SCHEDULE_IPI, 0);
  62. DECLARE_GIC_IPI(RT_STOP_IPI, 1);
  63. #undef DECLARE_GIC_IPI
  64. }
  65. #endif /* RT_USING_SMP */
  66. }
  67. rt_err_t gic_common_configure_irq(void *base, int irq, rt_uint32_t mode, void (*sync_access)(void *), void *data)
  68. {
  69. rt_err_t err = RT_EOK;
  70. rt_ubase_t level;
  71. rt_uint32_t val, oldval;
  72. rt_uint32_t confoff = (irq / 16) * 4;
  73. rt_uint32_t confmask = 0x2 << ((irq % 16) * 2);
  74. static struct rt_spinlock ic_lock = { 0 };
  75. level = rt_spin_lock_irqsave(&ic_lock);
  76. val = oldval = HWREG32(base + confoff);
  77. if (mode & RT_IRQ_MODE_LEVEL_MASK)
  78. {
  79. /* Level-sensitive */
  80. val &= ~confmask;
  81. }
  82. else if (mode & RT_IRQ_MODE_EDGE_BOTH)
  83. {
  84. /* Edge-triggered */
  85. val |= confmask;
  86. }
  87. if (val != oldval)
  88. {
  89. HWREG32(base + confoff) = val;
  90. if (HWREG32(base + confoff) != val)
  91. {
  92. err = -RT_EINVAL;
  93. }
  94. if (sync_access)
  95. {
  96. sync_access(data);
  97. }
  98. }
  99. rt_spin_unlock_irqrestore(&ic_lock, level);
  100. return err;
  101. }
  102. void gic_common_dist_config(void *base, int max_irqs, void (*sync_access)(void *), void *data)
  103. {
  104. rt_uint32_t i;
  105. /* Set all global interrupts to be level triggered, active low. */
  106. for (i = 32; i < max_irqs; i += 16)
  107. {
  108. HWREG32(base + GIC_DIST_CONFIG + i / 4) = GICD_INT_ACTLOW_LVLTRIG;
  109. }
  110. /* Set priority on all global interrupts. */
  111. for (i = 32; i < max_irqs; i += 4)
  112. {
  113. HWREG32(base + GIC_DIST_PRI + i * 4 / 4) = GICD_INT_DEF_PRI_X4;
  114. }
  115. /* Disable all SPIs. */
  116. for (i = 32; i < max_irqs; i += 32)
  117. {
  118. HWREG32(base + GIC_DIST_ACTIVE_CLEAR + i / 8) = GICD_INT_EN_CLR_X32;
  119. HWREG32(base + GIC_DIST_ENABLE_CLEAR + i / 8) = GICD_INT_EN_CLR_X32;
  120. }
  121. if (sync_access)
  122. {
  123. sync_access(data);
  124. }
  125. }
  126. void gic_common_cpu_config(void *base, int nr, void (*sync_access)(void *), void *data)
  127. {
  128. rt_uint32_t i;
  129. /* Disable all SGIs, PPIs. */
  130. for (i = 0; i < nr; i += 32)
  131. {
  132. HWREG32(base + GIC_DIST_ACTIVE_CLEAR + i / 8) = GICD_INT_EN_CLR_X32;
  133. HWREG32(base + GIC_DIST_ENABLE_CLEAR + i / 8) = GICD_INT_EN_CLR_X32;
  134. }
  135. /* Set priority on all PPI and SGI. */
  136. for (i = 0; i < nr; i += 4)
  137. {
  138. HWREG32(base + GIC_DIST_PRI + i * 4 / 4) = GICD_INT_DEF_PRI_X4;
  139. }
  140. if (sync_access)
  141. {
  142. sync_access(data);
  143. }
  144. }