irq.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/sh/boards/dreamcast/irq.c
  4. *
  5. * Holly IRQ support for the Sega Dreamcast.
  6. *
  7. * Copyright (c) 2001, 2002 M. R. Brown <[email protected]>
  8. *
  9. * This file is part of the LinuxDC project (www.linuxdc.org)
  10. */
  11. #include <linux/irq.h>
  12. #include <linux/io.h>
  13. #include <linux/export.h>
  14. #include <linux/err.h>
  15. #include <mach/sysasic.h>
  16. /*
  17. * Dreamcast System ASIC Hardware Events -
  18. *
  19. * The Dreamcast's System ASIC (a.k.a. Holly) is responsible for receiving
  20. * hardware events from system peripherals and triggering an SH7750 IRQ.
  21. * Hardware events can trigger IRQs 13, 11, or 9 depending on which bits are
  22. * set in the Event Mask Registers (EMRs). When a hardware event is
  23. * triggered, its corresponding bit in the Event Status Registers (ESRs)
  24. * is set, and that bit should be rewritten to the ESR to acknowledge that
  25. * event.
  26. *
  27. * There are three 32-bit ESRs located at 0xa05f6900 - 0xa05f6908. Event
  28. * types can be found in arch/sh/include/mach-dreamcast/mach/sysasic.h.
  29. * There are three groups of EMRs that parallel the ESRs. Each EMR group
  30. * corresponds to an IRQ, so 0xa05f6910 - 0xa05f6918 triggers IRQ 13,
  31. * 0xa05f6920 - 0xa05f6928 triggers IRQ 11, and 0xa05f6930 - 0xa05f6938
  32. * triggers IRQ 9.
  33. *
  34. * In the kernel, these events are mapped to virtual IRQs so that drivers can
  35. * respond to them as they would a normal interrupt. In order to keep this
  36. * mapping simple, the events are mapped as:
  37. *
  38. * 6900/6910 - Events 0-31, IRQ 13
  39. * 6904/6924 - Events 32-63, IRQ 11
  40. * 6908/6938 - Events 64-95, IRQ 9
  41. *
  42. */
  43. #define ESR_BASE 0x005f6900 /* Base event status register */
  44. #define EMR_BASE 0x005f6910 /* Base event mask register */
  45. /*
  46. * Helps us determine the EMR group that this event belongs to: 0 = 0x6910,
  47. * 1 = 0x6920, 2 = 0x6930; also determine the event offset.
  48. */
  49. #define LEVEL(event) (((event) - HW_EVENT_IRQ_BASE) / 32)
  50. /* Return the hardware event's bit position within the EMR/ESR */
  51. #define EVENT_BIT(event) (((event) - HW_EVENT_IRQ_BASE) & 31)
  52. /*
  53. * For each of these *_irq routines, the IRQ passed in is the virtual IRQ
  54. * (logically mapped to the corresponding bit for the hardware event).
  55. */
  56. /* Disable the hardware event by masking its bit in its EMR */
  57. static inline void disable_systemasic_irq(struct irq_data *data)
  58. {
  59. unsigned int irq = data->irq;
  60. __u32 emr = EMR_BASE + (LEVEL(irq) << 4) + (LEVEL(irq) << 2);
  61. __u32 mask;
  62. mask = inl(emr);
  63. mask &= ~(1 << EVENT_BIT(irq));
  64. outl(mask, emr);
  65. }
  66. /* Enable the hardware event by setting its bit in its EMR */
  67. static inline void enable_systemasic_irq(struct irq_data *data)
  68. {
  69. unsigned int irq = data->irq;
  70. __u32 emr = EMR_BASE + (LEVEL(irq) << 4) + (LEVEL(irq) << 2);
  71. __u32 mask;
  72. mask = inl(emr);
  73. mask |= (1 << EVENT_BIT(irq));
  74. outl(mask, emr);
  75. }
  76. /* Acknowledge a hardware event by writing its bit back to its ESR */
  77. static void mask_ack_systemasic_irq(struct irq_data *data)
  78. {
  79. unsigned int irq = data->irq;
  80. __u32 esr = ESR_BASE + (LEVEL(irq) << 2);
  81. disable_systemasic_irq(data);
  82. outl((1 << EVENT_BIT(irq)), esr);
  83. }
  84. struct irq_chip systemasic_int = {
  85. .name = "System ASIC",
  86. .irq_mask = disable_systemasic_irq,
  87. .irq_mask_ack = mask_ack_systemasic_irq,
  88. .irq_unmask = enable_systemasic_irq,
  89. };
  90. /*
  91. * Map the hardware event indicated by the processor IRQ to a virtual IRQ.
  92. */
  93. int systemasic_irq_demux(int irq)
  94. {
  95. __u32 emr, esr, status, level;
  96. __u32 j, bit;
  97. switch (irq) {
  98. case 13 + 16:
  99. level = 0;
  100. break;
  101. case 11 + 16:
  102. level = 1;
  103. break;
  104. case 9 + 16:
  105. level = 2;
  106. break;
  107. default:
  108. return irq;
  109. }
  110. emr = EMR_BASE + (level << 4) + (level << 2);
  111. esr = ESR_BASE + (level << 2);
  112. /* Mask the ESR to filter any spurious, unwanted interrupts */
  113. status = inl(esr);
  114. status &= inl(emr);
  115. /* Now scan and find the first set bit as the event to map */
  116. for (bit = 1, j = 0; j < 32; bit <<= 1, j++) {
  117. if (status & bit) {
  118. irq = HW_EVENT_IRQ_BASE + j + (level << 5);
  119. return irq;
  120. }
  121. }
  122. /* Not reached */
  123. return irq;
  124. }
  125. void systemasic_irq_init(void)
  126. {
  127. int irq_base, i;
  128. irq_base = irq_alloc_descs(HW_EVENT_IRQ_BASE, HW_EVENT_IRQ_BASE,
  129. HW_EVENT_IRQ_MAX - HW_EVENT_IRQ_BASE, -1);
  130. if (IS_ERR_VALUE(irq_base)) {
  131. pr_err("%s: failed hooking irqs\n", __func__);
  132. return;
  133. }
  134. for (i = HW_EVENT_IRQ_BASE; i < HW_EVENT_IRQ_MAX; i++)
  135. irq_set_chip_and_handler(i, &systemasic_int, handle_level_irq);
  136. }