irq-or1k-pic.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2010-2011 Jonas Bonn <[email protected]>
  4. * Copyright (C) 2014 Stefan Kristansson <[email protected]>
  5. */
  6. #include <linux/irq.h>
  7. #include <linux/irqchip.h>
  8. #include <linux/of.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/of_address.h>
  11. /* OR1K PIC implementation */
  12. struct or1k_pic_dev {
  13. struct irq_chip chip;
  14. irq_flow_handler_t handle;
  15. unsigned long flags;
  16. };
  17. /*
  18. * We're a couple of cycles faster than the generic implementations with
  19. * these 'fast' versions.
  20. */
  21. static void or1k_pic_mask(struct irq_data *data)
  22. {
  23. mtspr(SPR_PICMR, mfspr(SPR_PICMR) & ~(1UL << data->hwirq));
  24. }
  25. static void or1k_pic_unmask(struct irq_data *data)
  26. {
  27. mtspr(SPR_PICMR, mfspr(SPR_PICMR) | (1UL << data->hwirq));
  28. }
  29. static void or1k_pic_ack(struct irq_data *data)
  30. {
  31. mtspr(SPR_PICSR, (1UL << data->hwirq));
  32. }
  33. static void or1k_pic_mask_ack(struct irq_data *data)
  34. {
  35. mtspr(SPR_PICMR, mfspr(SPR_PICMR) & ~(1UL << data->hwirq));
  36. mtspr(SPR_PICSR, (1UL << data->hwirq));
  37. }
  38. /*
  39. * There are two oddities with the OR1200 PIC implementation:
  40. * i) LEVEL-triggered interrupts are latched and need to be cleared
  41. * ii) the interrupt latch is cleared by writing a 0 to the bit,
  42. * as opposed to a 1 as mandated by the spec
  43. */
  44. static void or1k_pic_or1200_ack(struct irq_data *data)
  45. {
  46. mtspr(SPR_PICSR, mfspr(SPR_PICSR) & ~(1UL << data->hwirq));
  47. }
  48. static void or1k_pic_or1200_mask_ack(struct irq_data *data)
  49. {
  50. mtspr(SPR_PICMR, mfspr(SPR_PICMR) & ~(1UL << data->hwirq));
  51. mtspr(SPR_PICSR, mfspr(SPR_PICSR) & ~(1UL << data->hwirq));
  52. }
  53. static struct or1k_pic_dev or1k_pic_level = {
  54. .chip = {
  55. .name = "or1k-PIC-level",
  56. .irq_unmask = or1k_pic_unmask,
  57. .irq_mask = or1k_pic_mask,
  58. },
  59. .handle = handle_level_irq,
  60. .flags = IRQ_LEVEL | IRQ_NOPROBE,
  61. };
  62. static struct or1k_pic_dev or1k_pic_edge = {
  63. .chip = {
  64. .name = "or1k-PIC-edge",
  65. .irq_unmask = or1k_pic_unmask,
  66. .irq_mask = or1k_pic_mask,
  67. .irq_ack = or1k_pic_ack,
  68. .irq_mask_ack = or1k_pic_mask_ack,
  69. },
  70. .handle = handle_edge_irq,
  71. .flags = IRQ_LEVEL | IRQ_NOPROBE,
  72. };
  73. static struct or1k_pic_dev or1k_pic_or1200 = {
  74. .chip = {
  75. .name = "or1200-PIC",
  76. .irq_unmask = or1k_pic_unmask,
  77. .irq_mask = or1k_pic_mask,
  78. .irq_ack = or1k_pic_or1200_ack,
  79. .irq_mask_ack = or1k_pic_or1200_mask_ack,
  80. },
  81. .handle = handle_level_irq,
  82. .flags = IRQ_LEVEL | IRQ_NOPROBE,
  83. };
  84. static struct irq_domain *root_domain;
  85. static inline int pic_get_irq(int first)
  86. {
  87. int hwirq;
  88. hwirq = ffs(mfspr(SPR_PICSR) >> first);
  89. if (!hwirq)
  90. return NO_IRQ;
  91. else
  92. hwirq = hwirq + first - 1;
  93. return hwirq;
  94. }
  95. static void or1k_pic_handle_irq(struct pt_regs *regs)
  96. {
  97. int irq = -1;
  98. while ((irq = pic_get_irq(irq + 1)) != NO_IRQ)
  99. generic_handle_domain_irq(root_domain, irq);
  100. }
  101. static int or1k_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  102. {
  103. struct or1k_pic_dev *pic = d->host_data;
  104. irq_set_chip_and_handler(irq, &pic->chip, pic->handle);
  105. irq_set_status_flags(irq, pic->flags);
  106. return 0;
  107. }
  108. static const struct irq_domain_ops or1k_irq_domain_ops = {
  109. .xlate = irq_domain_xlate_onecell,
  110. .map = or1k_map,
  111. };
  112. /*
  113. * This sets up the IRQ domain for the PIC built in to the OpenRISC
  114. * 1000 CPU. This is the "root" domain as these are the interrupts
  115. * that directly trigger an exception in the CPU.
  116. */
  117. static int __init or1k_pic_init(struct device_node *node,
  118. struct or1k_pic_dev *pic)
  119. {
  120. /* Disable all interrupts until explicitly requested */
  121. mtspr(SPR_PICMR, (0UL));
  122. root_domain = irq_domain_add_linear(node, 32, &or1k_irq_domain_ops,
  123. pic);
  124. set_handle_irq(or1k_pic_handle_irq);
  125. return 0;
  126. }
  127. static int __init or1k_pic_or1200_init(struct device_node *node,
  128. struct device_node *parent)
  129. {
  130. return or1k_pic_init(node, &or1k_pic_or1200);
  131. }
  132. IRQCHIP_DECLARE(or1k_pic_or1200, "opencores,or1200-pic", or1k_pic_or1200_init);
  133. IRQCHIP_DECLARE(or1k_pic, "opencores,or1k-pic", or1k_pic_or1200_init);
  134. static int __init or1k_pic_level_init(struct device_node *node,
  135. struct device_node *parent)
  136. {
  137. return or1k_pic_init(node, &or1k_pic_level);
  138. }
  139. IRQCHIP_DECLARE(or1k_pic_level, "opencores,or1k-pic-level",
  140. or1k_pic_level_init);
  141. static int __init or1k_pic_edge_init(struct device_node *node,
  142. struct device_node *parent)
  143. {
  144. return or1k_pic_init(node, &or1k_pic_edge);
  145. }
  146. IRQCHIP_DECLARE(or1k_pic_edge, "opencores,or1k-pic-edge", or1k_pic_edge_init);