intc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2012-2013 Xilinx, Inc.
  4. * Copyright (C) 2007-2009 PetaLogix
  5. * Copyright (C) 2006 Atmark Techno, Inc.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/irqdomain.h>
  12. #include <linux/irq.h>
  13. #include <linux/of_address.h>
  14. #include <linux/io.h>
  15. #include <linux/bug.h>
  16. #include "../../drivers/irqchip/irqchip.h"
  17. static void __iomem *intc_baseaddr;
  18. /* No one else should require these constants, so define them locally here. */
  19. #define ISR 0x00 /* Interrupt Status Register */
  20. #define IPR 0x04 /* Interrupt Pending Register */
  21. #define IER 0x08 /* Interrupt Enable Register */
  22. #define IAR 0x0c /* Interrupt Acknowledge Register */
  23. #define SIE 0x10 /* Set Interrupt Enable bits */
  24. #define CIE 0x14 /* Clear Interrupt Enable bits */
  25. #define IVR 0x18 /* Interrupt Vector Register */
  26. #define MER 0x1c /* Master Enable Register */
  27. #define MER_ME (1<<0)
  28. #define MER_HIE (1<<1)
  29. static void intc_enable_or_unmask(struct irq_data *d)
  30. {
  31. unsigned long mask = 1 << d->hwirq;
  32. pr_debug("enable_or_unmask: %ld\n", d->hwirq);
  33. /* ack level irqs because they can't be acked during
  34. * ack function since the handle_level_irq function
  35. * acks the irq before calling the interrupt handler
  36. */
  37. if (irqd_is_level_type(d))
  38. out_be32(intc_baseaddr + IAR, mask);
  39. out_be32(intc_baseaddr + SIE, mask);
  40. }
  41. static void intc_disable_or_mask(struct irq_data *d)
  42. {
  43. pr_debug("disable: %ld\n", d->hwirq);
  44. out_be32(intc_baseaddr + CIE, 1 << d->hwirq);
  45. }
  46. static void intc_ack(struct irq_data *d)
  47. {
  48. pr_debug("ack: %ld\n", d->hwirq);
  49. out_be32(intc_baseaddr + IAR, 1 << d->hwirq);
  50. }
  51. static void intc_mask_ack(struct irq_data *d)
  52. {
  53. unsigned long mask = 1 << d->hwirq;
  54. pr_debug("disable_and_ack: %ld\n", d->hwirq);
  55. out_be32(intc_baseaddr + CIE, mask);
  56. out_be32(intc_baseaddr + IAR, mask);
  57. }
  58. static struct irq_chip intc_dev = {
  59. .name = "Xilinx INTC",
  60. .irq_unmask = intc_enable_or_unmask,
  61. .irq_mask = intc_disable_or_mask,
  62. .irq_ack = intc_ack,
  63. .irq_mask_ack = intc_mask_ack,
  64. };
  65. static struct irq_domain *root_domain;
  66. unsigned int get_irq(void)
  67. {
  68. unsigned int hwirq, irq = -1;
  69. hwirq = in_be32(intc_baseaddr + IVR);
  70. if (hwirq != -1U)
  71. irq = irq_find_mapping(root_domain, hwirq);
  72. pr_debug("get_irq: hwirq=%d, irq=%d\n", hwirq, irq);
  73. return irq;
  74. }
  75. static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  76. {
  77. u32 intr_mask = (u32)d->host_data;
  78. if (intr_mask & (1 << hw)) {
  79. irq_set_chip_and_handler_name(irq, &intc_dev,
  80. handle_edge_irq, "edge");
  81. irq_clear_status_flags(irq, IRQ_LEVEL);
  82. } else {
  83. irq_set_chip_and_handler_name(irq, &intc_dev,
  84. handle_level_irq, "level");
  85. irq_set_status_flags(irq, IRQ_LEVEL);
  86. }
  87. return 0;
  88. }
  89. static const struct irq_domain_ops xintc_irq_domain_ops = {
  90. .xlate = irq_domain_xlate_onetwocell,
  91. .map = xintc_map,
  92. };
  93. static int __init xilinx_intc_of_init(struct device_node *intc,
  94. struct device_node *parent)
  95. {
  96. u32 nr_irq, intr_mask;
  97. int ret;
  98. intc_baseaddr = of_iomap(intc, 0);
  99. BUG_ON(!intc_baseaddr);
  100. ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &nr_irq);
  101. if (ret < 0) {
  102. pr_err("%s: unable to read xlnx,num-intr-inputs\n", __func__);
  103. return -EINVAL;
  104. }
  105. ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &intr_mask);
  106. if (ret < 0) {
  107. pr_err("%s: unable to read xlnx,kind-of-intr\n", __func__);
  108. return -EINVAL;
  109. }
  110. if (intr_mask > (u32)((1ULL << nr_irq) - 1))
  111. pr_info(" ERROR: Mismatch in kind-of-intr param\n");
  112. pr_info("%s: num_irq=%d, edge=0x%x\n",
  113. intc->full_name, nr_irq, intr_mask);
  114. /*
  115. * Disable all external interrupts until they are
  116. * explicity requested.
  117. */
  118. out_be32(intc_baseaddr + IER, 0);
  119. /* Acknowledge any pending interrupts just in case. */
  120. out_be32(intc_baseaddr + IAR, 0xffffffff);
  121. /* Turn on the Master Enable. */
  122. out_be32(intc_baseaddr + MER, MER_HIE | MER_ME);
  123. /* Yeah, okay, casting the intr_mask to a void* is butt-ugly, but I'm
  124. * lazy and Michal can clean it up to something nicer when he tests
  125. * and commits this patch. ~~gcl */
  126. root_domain = irq_domain_add_linear(intc, nr_irq, &xintc_irq_domain_ops,
  127. (void *)intr_mask);
  128. irq_set_default_host(root_domain);
  129. return 0;
  130. }
  131. IRQCHIP_DECLARE(xilinx_intc, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init);