irq-aspeed-vic.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (C) 2015 - Ben Herrenschmidt, IBM Corp.
  3. *
  4. * Driver for Aspeed "new" VIC as found in SoC generation 3 and later
  5. *
  6. * Based on irq-vic.c:
  7. *
  8. * Copyright (C) 1999 - 2003 ARM Limited
  9. * Copyright (C) 2000 Deep Blue Solutions Ltd
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. */
  22. #include <linux/export.h>
  23. #include <linux/init.h>
  24. #include <linux/list.h>
  25. #include <linux/io.h>
  26. #include <linux/irq.h>
  27. #include <linux/irqchip.h>
  28. #include <linux/irqchip/chained_irq.h>
  29. #include <linux/irqdomain.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/syscore_ops.h>
  34. #include <linux/device.h>
  35. #include <linux/slab.h>
  36. #include <asm/exception.h>
  37. #include <asm/irq.h>
  38. /* These definitions correspond to the "new mapping" of the
  39. * register set that interleaves "high" and "low". The offsets
  40. * below are for the "low" register, add 4 to get to the high one
  41. */
  42. #define AVIC_IRQ_STATUS 0x00
  43. #define AVIC_FIQ_STATUS 0x08
  44. #define AVIC_RAW_STATUS 0x10
  45. #define AVIC_INT_SELECT 0x18
  46. #define AVIC_INT_ENABLE 0x20
  47. #define AVIC_INT_ENABLE_CLR 0x28
  48. #define AVIC_INT_TRIGGER 0x30
  49. #define AVIC_INT_TRIGGER_CLR 0x38
  50. #define AVIC_INT_SENSE 0x40
  51. #define AVIC_INT_DUAL_EDGE 0x48
  52. #define AVIC_INT_EVENT 0x50
  53. #define AVIC_EDGE_CLR 0x58
  54. #define AVIC_EDGE_STATUS 0x60
  55. #define NUM_IRQS 64
  56. struct aspeed_vic {
  57. void __iomem *base;
  58. u32 edge_sources[2];
  59. struct irq_domain *dom;
  60. };
  61. static struct aspeed_vic *system_avic;
  62. static void vic_init_hw(struct aspeed_vic *vic)
  63. {
  64. u32 sense;
  65. /* Disable all interrupts */
  66. writel(0xffffffff, vic->base + AVIC_INT_ENABLE_CLR);
  67. writel(0xffffffff, vic->base + AVIC_INT_ENABLE_CLR + 4);
  68. /* Make sure no soft trigger is on */
  69. writel(0xffffffff, vic->base + AVIC_INT_TRIGGER_CLR);
  70. writel(0xffffffff, vic->base + AVIC_INT_TRIGGER_CLR + 4);
  71. /* Set everything to be IRQ */
  72. writel(0, vic->base + AVIC_INT_SELECT);
  73. writel(0, vic->base + AVIC_INT_SELECT + 4);
  74. /* Some interrupts have a programable high/low level trigger
  75. * (4 GPIO direct inputs), for now we assume this was configured
  76. * by firmware. We read which ones are edge now.
  77. */
  78. sense = readl(vic->base + AVIC_INT_SENSE);
  79. vic->edge_sources[0] = ~sense;
  80. sense = readl(vic->base + AVIC_INT_SENSE + 4);
  81. vic->edge_sources[1] = ~sense;
  82. /* Clear edge detection latches */
  83. writel(0xffffffff, vic->base + AVIC_EDGE_CLR);
  84. writel(0xffffffff, vic->base + AVIC_EDGE_CLR + 4);
  85. }
  86. static void __exception_irq_entry avic_handle_irq(struct pt_regs *regs)
  87. {
  88. struct aspeed_vic *vic = system_avic;
  89. u32 stat, irq;
  90. for (;;) {
  91. irq = 0;
  92. stat = readl_relaxed(vic->base + AVIC_IRQ_STATUS);
  93. if (!stat) {
  94. stat = readl_relaxed(vic->base + AVIC_IRQ_STATUS + 4);
  95. irq = 32;
  96. }
  97. if (stat == 0)
  98. break;
  99. irq += ffs(stat) - 1;
  100. handle_domain_irq(vic->dom, irq, regs);
  101. }
  102. }
  103. static void avic_ack_irq(struct irq_data *d)
  104. {
  105. struct aspeed_vic *vic = irq_data_get_irq_chip_data(d);
  106. unsigned int sidx = d->hwirq >> 5;
  107. unsigned int sbit = 1u << (d->hwirq & 0x1f);
  108. /* Clear edge latch for edge interrupts, nop for level */
  109. if (vic->edge_sources[sidx] & sbit)
  110. writel(sbit, vic->base + AVIC_EDGE_CLR + sidx * 4);
  111. }
  112. static void avic_mask_irq(struct irq_data *d)
  113. {
  114. struct aspeed_vic *vic = irq_data_get_irq_chip_data(d);
  115. unsigned int sidx = d->hwirq >> 5;
  116. unsigned int sbit = 1u << (d->hwirq & 0x1f);
  117. writel(sbit, vic->base + AVIC_INT_ENABLE_CLR + sidx * 4);
  118. }
  119. static void avic_unmask_irq(struct irq_data *d)
  120. {
  121. struct aspeed_vic *vic = irq_data_get_irq_chip_data(d);
  122. unsigned int sidx = d->hwirq >> 5;
  123. unsigned int sbit = 1u << (d->hwirq & 0x1f);
  124. writel(sbit, vic->base + AVIC_INT_ENABLE + sidx * 4);
  125. }
  126. /* For level irq, faster than going through a nop "ack" and mask */
  127. static void avic_mask_ack_irq(struct irq_data *d)
  128. {
  129. struct aspeed_vic *vic = irq_data_get_irq_chip_data(d);
  130. unsigned int sidx = d->hwirq >> 5;
  131. unsigned int sbit = 1u << (d->hwirq & 0x1f);
  132. /* First mask */
  133. writel(sbit, vic->base + AVIC_INT_ENABLE_CLR + sidx * 4);
  134. /* Then clear edge latch for edge interrupts */
  135. if (vic->edge_sources[sidx] & sbit)
  136. writel(sbit, vic->base + AVIC_EDGE_CLR + sidx * 4);
  137. }
  138. static struct irq_chip avic_chip = {
  139. .name = "AVIC",
  140. .irq_ack = avic_ack_irq,
  141. .irq_mask = avic_mask_irq,
  142. .irq_unmask = avic_unmask_irq,
  143. .irq_mask_ack = avic_mask_ack_irq,
  144. };
  145. static int avic_map(struct irq_domain *d, unsigned int irq,
  146. irq_hw_number_t hwirq)
  147. {
  148. struct aspeed_vic *vic = d->host_data;
  149. unsigned int sidx = hwirq >> 5;
  150. unsigned int sbit = 1u << (hwirq & 0x1f);
  151. /* Check if interrupt exists */
  152. if (sidx > 1)
  153. return -EPERM;
  154. if (vic->edge_sources[sidx] & sbit)
  155. irq_set_chip_and_handler(irq, &avic_chip, handle_edge_irq);
  156. else
  157. irq_set_chip_and_handler(irq, &avic_chip, handle_level_irq);
  158. irq_set_chip_data(irq, vic);
  159. irq_set_probe(irq);
  160. return 0;
  161. }
  162. static struct irq_domain_ops avic_dom_ops = {
  163. .map = avic_map,
  164. .xlate = irq_domain_xlate_onetwocell,
  165. };
  166. static int __init avic_of_init(struct device_node *node,
  167. struct device_node *parent)
  168. {
  169. void __iomem *regs;
  170. struct aspeed_vic *vic;
  171. if (WARN(parent, "non-root Aspeed VIC not supported"))
  172. return -EINVAL;
  173. if (WARN(system_avic, "duplicate Aspeed VIC not supported"))
  174. return -EINVAL;
  175. regs = of_iomap(node, 0);
  176. if (WARN_ON(!regs))
  177. return -EIO;
  178. vic = kzalloc(sizeof(struct aspeed_vic), GFP_KERNEL);
  179. if (WARN_ON(!vic)) {
  180. iounmap(regs);
  181. return -ENOMEM;
  182. }
  183. vic->base = regs;
  184. /* Initialize soures, all masked */
  185. vic_init_hw(vic);
  186. /* Ready to receive interrupts */
  187. system_avic = vic;
  188. set_handle_irq(avic_handle_irq);
  189. /* Register our domain */
  190. vic->dom = irq_domain_add_simple(node, NUM_IRQS, 0,
  191. &avic_dom_ops, vic);
  192. return 0;
  193. }
  194. IRQCHIP_DECLARE(aspeed_new_vic, "aspeed,ast2400-vic", avic_of_init);