irq-tango.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2014 Mans Rullgard <mans@mansr.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/irq.h>
  11. #include <linux/irqchip.h>
  12. #include <linux/irqchip/chained_irq.h>
  13. #include <linux/ioport.h>
  14. #include <linux/io.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/slab.h>
  18. #define IRQ0_CTL_BASE 0x0000
  19. #define IRQ1_CTL_BASE 0x0100
  20. #define EDGE_CTL_BASE 0x0200
  21. #define IRQ2_CTL_BASE 0x0300
  22. #define IRQ_CTL_HI 0x18
  23. #define EDGE_CTL_HI 0x20
  24. #define IRQ_STATUS 0x00
  25. #define IRQ_RAWSTAT 0x04
  26. #define IRQ_EN_SET 0x08
  27. #define IRQ_EN_CLR 0x0c
  28. #define IRQ_SOFT_SET 0x10
  29. #define IRQ_SOFT_CLR 0x14
  30. #define EDGE_STATUS 0x00
  31. #define EDGE_RAWSTAT 0x04
  32. #define EDGE_CFG_RISE 0x08
  33. #define EDGE_CFG_FALL 0x0c
  34. #define EDGE_CFG_RISE_SET 0x10
  35. #define EDGE_CFG_RISE_CLR 0x14
  36. #define EDGE_CFG_FALL_SET 0x18
  37. #define EDGE_CFG_FALL_CLR 0x1c
  38. struct tangox_irq_chip {
  39. void __iomem *base;
  40. unsigned long ctl;
  41. };
  42. static inline u32 intc_readl(struct tangox_irq_chip *chip, int reg)
  43. {
  44. return readl_relaxed(chip->base + reg);
  45. }
  46. static inline void intc_writel(struct tangox_irq_chip *chip, int reg, u32 val)
  47. {
  48. writel_relaxed(val, chip->base + reg);
  49. }
  50. static void tangox_dispatch_irqs(struct irq_domain *dom, unsigned int status,
  51. int base)
  52. {
  53. unsigned int hwirq;
  54. unsigned int virq;
  55. while (status) {
  56. hwirq = __ffs(status);
  57. virq = irq_find_mapping(dom, base + hwirq);
  58. if (virq)
  59. generic_handle_irq(virq);
  60. status &= ~BIT(hwirq);
  61. }
  62. }
  63. static void tangox_irq_handler(struct irq_desc *desc)
  64. {
  65. struct irq_domain *dom = irq_desc_get_handler_data(desc);
  66. struct irq_chip *host_chip = irq_desc_get_chip(desc);
  67. struct tangox_irq_chip *chip = dom->host_data;
  68. unsigned int status_lo, status_hi;
  69. chained_irq_enter(host_chip, desc);
  70. status_lo = intc_readl(chip, chip->ctl + IRQ_STATUS);
  71. status_hi = intc_readl(chip, chip->ctl + IRQ_CTL_HI + IRQ_STATUS);
  72. tangox_dispatch_irqs(dom, status_lo, 0);
  73. tangox_dispatch_irqs(dom, status_hi, 32);
  74. chained_irq_exit(host_chip, desc);
  75. }
  76. static int tangox_irq_set_type(struct irq_data *d, unsigned int flow_type)
  77. {
  78. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  79. struct tangox_irq_chip *chip = gc->domain->host_data;
  80. struct irq_chip_regs *regs = &gc->chip_types[0].regs;
  81. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  82. case IRQ_TYPE_EDGE_RISING:
  83. intc_writel(chip, regs->type + EDGE_CFG_RISE_SET, d->mask);
  84. intc_writel(chip, regs->type + EDGE_CFG_FALL_CLR, d->mask);
  85. break;
  86. case IRQ_TYPE_EDGE_FALLING:
  87. intc_writel(chip, regs->type + EDGE_CFG_RISE_CLR, d->mask);
  88. intc_writel(chip, regs->type + EDGE_CFG_FALL_SET, d->mask);
  89. break;
  90. case IRQ_TYPE_LEVEL_HIGH:
  91. intc_writel(chip, regs->type + EDGE_CFG_RISE_CLR, d->mask);
  92. intc_writel(chip, regs->type + EDGE_CFG_FALL_CLR, d->mask);
  93. break;
  94. case IRQ_TYPE_LEVEL_LOW:
  95. intc_writel(chip, regs->type + EDGE_CFG_RISE_SET, d->mask);
  96. intc_writel(chip, regs->type + EDGE_CFG_FALL_SET, d->mask);
  97. break;
  98. default:
  99. pr_err("Invalid trigger mode %x for IRQ %d\n",
  100. flow_type, d->irq);
  101. return -EINVAL;
  102. }
  103. return irq_setup_alt_chip(d, flow_type);
  104. }
  105. static void __init tangox_irq_init_chip(struct irq_chip_generic *gc,
  106. unsigned long ctl_offs,
  107. unsigned long edge_offs)
  108. {
  109. struct tangox_irq_chip *chip = gc->domain->host_data;
  110. struct irq_chip_type *ct = gc->chip_types;
  111. unsigned long ctl_base = chip->ctl + ctl_offs;
  112. unsigned long edge_base = EDGE_CTL_BASE + edge_offs;
  113. int i;
  114. gc->reg_base = chip->base;
  115. gc->unused = 0;
  116. for (i = 0; i < 2; i++) {
  117. ct[i].chip.irq_ack = irq_gc_ack_set_bit;
  118. ct[i].chip.irq_mask = irq_gc_mask_disable_reg;
  119. ct[i].chip.irq_mask_ack = irq_gc_mask_disable_reg_and_ack;
  120. ct[i].chip.irq_unmask = irq_gc_unmask_enable_reg;
  121. ct[i].chip.irq_set_type = tangox_irq_set_type;
  122. ct[i].chip.name = gc->domain->name;
  123. ct[i].regs.enable = ctl_base + IRQ_EN_SET;
  124. ct[i].regs.disable = ctl_base + IRQ_EN_CLR;
  125. ct[i].regs.ack = edge_base + EDGE_RAWSTAT;
  126. ct[i].regs.type = edge_base;
  127. }
  128. ct[0].type = IRQ_TYPE_LEVEL_MASK;
  129. ct[0].handler = handle_level_irq;
  130. ct[1].type = IRQ_TYPE_EDGE_BOTH;
  131. ct[1].handler = handle_edge_irq;
  132. intc_writel(chip, ct->regs.disable, 0xffffffff);
  133. intc_writel(chip, ct->regs.ack, 0xffffffff);
  134. }
  135. static void __init tangox_irq_domain_init(struct irq_domain *dom)
  136. {
  137. struct irq_chip_generic *gc;
  138. int i;
  139. for (i = 0; i < 2; i++) {
  140. gc = irq_get_domain_generic_chip(dom, i * 32);
  141. tangox_irq_init_chip(gc, i * IRQ_CTL_HI, i * EDGE_CTL_HI);
  142. }
  143. }
  144. static int __init tangox_irq_init(void __iomem *base, struct resource *baseres,
  145. struct device_node *node)
  146. {
  147. struct tangox_irq_chip *chip;
  148. struct irq_domain *dom;
  149. struct resource res;
  150. int irq;
  151. int err;
  152. irq = irq_of_parse_and_map(node, 0);
  153. if (!irq)
  154. panic("%s: failed to get IRQ", node->name);
  155. err = of_address_to_resource(node, 0, &res);
  156. if (err)
  157. panic("%s: failed to get address", node->name);
  158. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  159. chip->ctl = res.start - baseres->start;
  160. chip->base = base;
  161. dom = irq_domain_add_linear(node, 64, &irq_generic_chip_ops, chip);
  162. if (!dom)
  163. panic("%s: failed to create irqdomain", node->name);
  164. err = irq_alloc_domain_generic_chips(dom, 32, 2, node->name,
  165. handle_level_irq, 0, 0, 0);
  166. if (err)
  167. panic("%s: failed to allocate irqchip", node->name);
  168. tangox_irq_domain_init(dom);
  169. irq_set_chained_handler(irq, tangox_irq_handler);
  170. irq_set_handler_data(irq, dom);
  171. return 0;
  172. }
  173. static int __init tangox_of_irq_init(struct device_node *node,
  174. struct device_node *parent)
  175. {
  176. struct device_node *c;
  177. struct resource res;
  178. void __iomem *base;
  179. base = of_iomap(node, 0);
  180. if (!base)
  181. panic("%s: of_iomap failed", node->name);
  182. of_address_to_resource(node, 0, &res);
  183. for_each_child_of_node(node, c)
  184. tangox_irq_init(base, &res, c);
  185. return 0;
  186. }
  187. IRQCHIP_DECLARE(tangox_intc, "sigma,smp8642-intc", tangox_of_irq_init);