irq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  5. * Copyright (C) 2013 John Crispin <john@phrozen.org>
  6. */
  7. #include <linux/io.h>
  8. #include <linux/bitops.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/interrupt.h>
  14. #include <asm/irq_cpu.h>
  15. #include <asm/mipsregs.h>
  16. #include <asm/time.h>
  17. #include "common.h"
  18. #define INTC_INT_GLOBAL BIT(31)
  19. #define RALINK_CPU_IRQ_INTC (MIPS_CPU_IRQ_BASE + 2)
  20. #define RALINK_CPU_IRQ_PCI (MIPS_CPU_IRQ_BASE + 4)
  21. #define RALINK_CPU_IRQ_FE (MIPS_CPU_IRQ_BASE + 5)
  22. #define RALINK_CPU_IRQ_WIFI (MIPS_CPU_IRQ_BASE + 6)
  23. #define RALINK_CPU_IRQ_COUNTER (MIPS_CPU_IRQ_BASE + 7)
  24. /* we have a cascade of 8 irqs */
  25. #define RALINK_INTC_IRQ_BASE 8
  26. /* we have 32 SoC irqs */
  27. #define RALINK_INTC_IRQ_COUNT 32
  28. #define RALINK_INTC_IRQ_PERFC (RALINK_INTC_IRQ_BASE + 9)
  29. enum rt_intc_regs_enum {
  30. INTC_REG_STATUS0 = 0,
  31. INTC_REG_STATUS1,
  32. INTC_REG_TYPE,
  33. INTC_REG_RAW_STATUS,
  34. INTC_REG_ENABLE,
  35. INTC_REG_DISABLE,
  36. };
  37. static u32 rt_intc_regs[] = {
  38. [INTC_REG_STATUS0] = 0x00,
  39. [INTC_REG_STATUS1] = 0x04,
  40. [INTC_REG_TYPE] = 0x20,
  41. [INTC_REG_RAW_STATUS] = 0x30,
  42. [INTC_REG_ENABLE] = 0x34,
  43. [INTC_REG_DISABLE] = 0x38,
  44. };
  45. static void __iomem *rt_intc_membase;
  46. static int rt_perfcount_irq;
  47. static inline void rt_intc_w32(u32 val, unsigned reg)
  48. {
  49. __raw_writel(val, rt_intc_membase + rt_intc_regs[reg]);
  50. }
  51. static inline u32 rt_intc_r32(unsigned reg)
  52. {
  53. return __raw_readl(rt_intc_membase + rt_intc_regs[reg]);
  54. }
  55. static void ralink_intc_irq_unmask(struct irq_data *d)
  56. {
  57. rt_intc_w32(BIT(d->hwirq), INTC_REG_ENABLE);
  58. }
  59. static void ralink_intc_irq_mask(struct irq_data *d)
  60. {
  61. rt_intc_w32(BIT(d->hwirq), INTC_REG_DISABLE);
  62. }
  63. static struct irq_chip ralink_intc_irq_chip = {
  64. .name = "INTC",
  65. .irq_unmask = ralink_intc_irq_unmask,
  66. .irq_mask = ralink_intc_irq_mask,
  67. .irq_mask_ack = ralink_intc_irq_mask,
  68. };
  69. int get_c0_perfcount_int(void)
  70. {
  71. return rt_perfcount_irq;
  72. }
  73. EXPORT_SYMBOL_GPL(get_c0_perfcount_int);
  74. unsigned int get_c0_compare_int(void)
  75. {
  76. return CP0_LEGACY_COMPARE_IRQ;
  77. }
  78. static void ralink_intc_irq_handler(struct irq_desc *desc)
  79. {
  80. u32 pending = rt_intc_r32(INTC_REG_STATUS0);
  81. if (pending) {
  82. struct irq_domain *domain = irq_desc_get_handler_data(desc);
  83. generic_handle_domain_irq(domain, __ffs(pending));
  84. } else {
  85. spurious_interrupt();
  86. }
  87. }
  88. asmlinkage void plat_irq_dispatch(void)
  89. {
  90. unsigned long pending;
  91. pending = read_c0_status() & read_c0_cause() & ST0_IM;
  92. if (pending & STATUSF_IP7)
  93. do_IRQ(RALINK_CPU_IRQ_COUNTER);
  94. else if (pending & STATUSF_IP5)
  95. do_IRQ(RALINK_CPU_IRQ_FE);
  96. else if (pending & STATUSF_IP6)
  97. do_IRQ(RALINK_CPU_IRQ_WIFI);
  98. else if (pending & STATUSF_IP4)
  99. do_IRQ(RALINK_CPU_IRQ_PCI);
  100. else if (pending & STATUSF_IP2)
  101. do_IRQ(RALINK_CPU_IRQ_INTC);
  102. else
  103. spurious_interrupt();
  104. }
  105. static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  106. {
  107. irq_set_chip_and_handler(irq, &ralink_intc_irq_chip, handle_level_irq);
  108. return 0;
  109. }
  110. static const struct irq_domain_ops irq_domain_ops = {
  111. .xlate = irq_domain_xlate_onecell,
  112. .map = intc_map,
  113. };
  114. static int __init intc_of_init(struct device_node *node,
  115. struct device_node *parent)
  116. {
  117. struct resource res;
  118. struct irq_domain *domain;
  119. int irq;
  120. if (!of_property_read_u32_array(node, "ralink,intc-registers",
  121. rt_intc_regs, 6))
  122. pr_info("intc: using register map from devicetree\n");
  123. irq = irq_of_parse_and_map(node, 0);
  124. if (!irq)
  125. panic("Failed to get INTC IRQ");
  126. if (of_address_to_resource(node, 0, &res))
  127. panic("Failed to get intc memory range");
  128. if (!request_mem_region(res.start, resource_size(&res),
  129. res.name))
  130. pr_err("Failed to request intc memory");
  131. rt_intc_membase = ioremap(res.start,
  132. resource_size(&res));
  133. if (!rt_intc_membase)
  134. panic("Failed to remap intc memory");
  135. /* disable all interrupts */
  136. rt_intc_w32(~0, INTC_REG_DISABLE);
  137. /* route all INTC interrupts to MIPS HW0 interrupt */
  138. rt_intc_w32(0, INTC_REG_TYPE);
  139. domain = irq_domain_create_legacy(of_fwnode_handle(node), RALINK_INTC_IRQ_COUNT,
  140. RALINK_INTC_IRQ_BASE, 0, &irq_domain_ops, NULL);
  141. if (!domain)
  142. panic("Failed to add irqdomain");
  143. rt_intc_w32(INTC_INT_GLOBAL, INTC_REG_ENABLE);
  144. irq_set_chained_handler_and_data(irq, ralink_intc_irq_handler, domain);
  145. /* tell the kernel which irq is used for performance monitoring */
  146. rt_perfcount_irq = irq_create_mapping(domain, 9);
  147. return 0;
  148. }
  149. static struct of_device_id __initdata of_irq_ids[] = {
  150. { .compatible = "mti,cpu-interrupt-controller", .data = mips_cpu_irq_of_init },
  151. { .compatible = "ralink,rt2880-intc", .data = intc_of_init },
  152. {},
  153. };
  154. void __init arch_init_irq(void)
  155. {
  156. of_irq_init(of_irq_ids);
  157. }