irq-or1k-pic.c 4.5 KB

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