irq-lpc32xx.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2015-2016 Vladimir Zapolskiy <vz@mleia.com>
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #define pr_fmt(fmt) "%s: " fmt, __func__
  12. #include <linux/io.h>
  13. #include <linux/irqchip.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/slab.h>
  19. #include <asm/exception.h>
  20. #define LPC32XX_INTC_MASK 0x00
  21. #define LPC32XX_INTC_RAW 0x04
  22. #define LPC32XX_INTC_STAT 0x08
  23. #define LPC32XX_INTC_POL 0x0C
  24. #define LPC32XX_INTC_TYPE 0x10
  25. #define LPC32XX_INTC_FIQ 0x14
  26. #define NR_LPC32XX_IC_IRQS 32
  27. struct lpc32xx_irq_chip {
  28. void __iomem *base;
  29. struct irq_domain *domain;
  30. struct irq_chip chip;
  31. };
  32. static struct lpc32xx_irq_chip *lpc32xx_mic_irqc;
  33. static inline u32 lpc32xx_ic_read(struct lpc32xx_irq_chip *ic, u32 reg)
  34. {
  35. return readl_relaxed(ic->base + reg);
  36. }
  37. static inline void lpc32xx_ic_write(struct lpc32xx_irq_chip *ic,
  38. u32 reg, u32 val)
  39. {
  40. writel_relaxed(val, ic->base + reg);
  41. }
  42. static void lpc32xx_irq_mask(struct irq_data *d)
  43. {
  44. struct lpc32xx_irq_chip *ic = irq_data_get_irq_chip_data(d);
  45. u32 val, mask = BIT(d->hwirq);
  46. val = lpc32xx_ic_read(ic, LPC32XX_INTC_MASK) & ~mask;
  47. lpc32xx_ic_write(ic, LPC32XX_INTC_MASK, val);
  48. }
  49. static void lpc32xx_irq_unmask(struct irq_data *d)
  50. {
  51. struct lpc32xx_irq_chip *ic = irq_data_get_irq_chip_data(d);
  52. u32 val, mask = BIT(d->hwirq);
  53. val = lpc32xx_ic_read(ic, LPC32XX_INTC_MASK) | mask;
  54. lpc32xx_ic_write(ic, LPC32XX_INTC_MASK, val);
  55. }
  56. static void lpc32xx_irq_ack(struct irq_data *d)
  57. {
  58. struct lpc32xx_irq_chip *ic = irq_data_get_irq_chip_data(d);
  59. u32 mask = BIT(d->hwirq);
  60. lpc32xx_ic_write(ic, LPC32XX_INTC_RAW, mask);
  61. }
  62. static int lpc32xx_irq_set_type(struct irq_data *d, unsigned int type)
  63. {
  64. struct lpc32xx_irq_chip *ic = irq_data_get_irq_chip_data(d);
  65. u32 val, mask = BIT(d->hwirq);
  66. bool high, edge;
  67. switch (type) {
  68. case IRQ_TYPE_EDGE_RISING:
  69. edge = true;
  70. high = true;
  71. break;
  72. case IRQ_TYPE_EDGE_FALLING:
  73. edge = true;
  74. high = false;
  75. break;
  76. case IRQ_TYPE_LEVEL_HIGH:
  77. edge = false;
  78. high = true;
  79. break;
  80. case IRQ_TYPE_LEVEL_LOW:
  81. edge = false;
  82. high = false;
  83. break;
  84. default:
  85. pr_info("unsupported irq type %d\n", type);
  86. return -EINVAL;
  87. }
  88. irqd_set_trigger_type(d, type);
  89. val = lpc32xx_ic_read(ic, LPC32XX_INTC_POL);
  90. if (high)
  91. val |= mask;
  92. else
  93. val &= ~mask;
  94. lpc32xx_ic_write(ic, LPC32XX_INTC_POL, val);
  95. val = lpc32xx_ic_read(ic, LPC32XX_INTC_TYPE);
  96. if (edge) {
  97. val |= mask;
  98. irq_set_handler_locked(d, handle_edge_irq);
  99. } else {
  100. val &= ~mask;
  101. irq_set_handler_locked(d, handle_level_irq);
  102. }
  103. lpc32xx_ic_write(ic, LPC32XX_INTC_TYPE, val);
  104. return 0;
  105. }
  106. static void __exception_irq_entry lpc32xx_handle_irq(struct pt_regs *regs)
  107. {
  108. struct lpc32xx_irq_chip *ic = lpc32xx_mic_irqc;
  109. u32 hwirq = lpc32xx_ic_read(ic, LPC32XX_INTC_STAT), irq;
  110. while (hwirq) {
  111. irq = __ffs(hwirq);
  112. hwirq &= ~BIT(irq);
  113. handle_domain_irq(lpc32xx_mic_irqc->domain, irq, regs);
  114. }
  115. }
  116. static void lpc32xx_sic_handler(struct irq_desc *desc)
  117. {
  118. struct lpc32xx_irq_chip *ic = irq_desc_get_handler_data(desc);
  119. struct irq_chip *chip = irq_desc_get_chip(desc);
  120. u32 hwirq = lpc32xx_ic_read(ic, LPC32XX_INTC_STAT), irq;
  121. chained_irq_enter(chip, desc);
  122. while (hwirq) {
  123. irq = __ffs(hwirq);
  124. hwirq &= ~BIT(irq);
  125. generic_handle_irq(irq_find_mapping(ic->domain, irq));
  126. }
  127. chained_irq_exit(chip, desc);
  128. }
  129. static int lpc32xx_irq_domain_map(struct irq_domain *id, unsigned int virq,
  130. irq_hw_number_t hw)
  131. {
  132. struct lpc32xx_irq_chip *ic = id->host_data;
  133. irq_set_chip_data(virq, ic);
  134. irq_set_chip_and_handler(virq, &ic->chip, handle_level_irq);
  135. irq_set_status_flags(virq, IRQ_LEVEL);
  136. irq_set_noprobe(virq);
  137. return 0;
  138. }
  139. static void lpc32xx_irq_domain_unmap(struct irq_domain *id, unsigned int virq)
  140. {
  141. irq_set_chip_and_handler(virq, NULL, NULL);
  142. }
  143. static const struct irq_domain_ops lpc32xx_irq_domain_ops = {
  144. .map = lpc32xx_irq_domain_map,
  145. .unmap = lpc32xx_irq_domain_unmap,
  146. .xlate = irq_domain_xlate_twocell,
  147. };
  148. static int __init lpc32xx_of_ic_init(struct device_node *node,
  149. struct device_node *parent)
  150. {
  151. struct lpc32xx_irq_chip *irqc;
  152. bool is_mic = of_device_is_compatible(node, "nxp,lpc3220-mic");
  153. const __be32 *reg = of_get_property(node, "reg", NULL);
  154. u32 parent_irq, i, addr = reg ? be32_to_cpu(*reg) : 0;
  155. irqc = kzalloc(sizeof(*irqc), GFP_KERNEL);
  156. if (!irqc)
  157. return -ENOMEM;
  158. irqc->base = of_iomap(node, 0);
  159. if (!irqc->base) {
  160. pr_err("%s: unable to map registers\n", node->full_name);
  161. kfree(irqc);
  162. return -EINVAL;
  163. }
  164. irqc->chip.irq_ack = lpc32xx_irq_ack;
  165. irqc->chip.irq_mask = lpc32xx_irq_mask;
  166. irqc->chip.irq_unmask = lpc32xx_irq_unmask;
  167. irqc->chip.irq_set_type = lpc32xx_irq_set_type;
  168. if (is_mic)
  169. irqc->chip.name = kasprintf(GFP_KERNEL, "%08x.mic", addr);
  170. else
  171. irqc->chip.name = kasprintf(GFP_KERNEL, "%08x.sic", addr);
  172. irqc->domain = irq_domain_add_linear(node, NR_LPC32XX_IC_IRQS,
  173. &lpc32xx_irq_domain_ops, irqc);
  174. if (!irqc->domain) {
  175. pr_err("unable to add irq domain\n");
  176. iounmap(irqc->base);
  177. kfree(irqc->chip.name);
  178. kfree(irqc);
  179. return -ENODEV;
  180. }
  181. if (is_mic) {
  182. lpc32xx_mic_irqc = irqc;
  183. set_handle_irq(lpc32xx_handle_irq);
  184. } else {
  185. for (i = 0; i < of_irq_count(node); i++) {
  186. parent_irq = irq_of_parse_and_map(node, i);
  187. if (parent_irq)
  188. irq_set_chained_handler_and_data(parent_irq,
  189. lpc32xx_sic_handler, irqc);
  190. }
  191. }
  192. lpc32xx_ic_write(irqc, LPC32XX_INTC_MASK, 0x00);
  193. lpc32xx_ic_write(irqc, LPC32XX_INTC_POL, 0x00);
  194. lpc32xx_ic_write(irqc, LPC32XX_INTC_TYPE, 0x00);
  195. return 0;
  196. }
  197. IRQCHIP_DECLARE(nxp_lpc32xx_mic, "nxp,lpc3220-mic", lpc32xx_of_ic_init);
  198. IRQCHIP_DECLARE(nxp_lpc32xx_sic, "nxp,lpc3220-sic", lpc32xx_of_ic_init);