irq-bcm7120-l2.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Broadcom BCM7120 style Level 2 interrupt controller driver
  3. *
  4. * Copyright (C) 2014 Broadcom Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/kconfig.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/io.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/reboot.h>
  25. #include <linux/bitops.h>
  26. #include <linux/irqchip/chained_irq.h>
  27. #include "irqchip.h"
  28. /* Register offset in the L2 interrupt controller */
  29. #define IRQEN 0x00
  30. #define IRQSTAT 0x04
  31. #define MAX_WORDS 4
  32. #define IRQS_PER_WORD 32
  33. struct bcm7120_l2_intc_data {
  34. unsigned int n_words;
  35. void __iomem *base[MAX_WORDS];
  36. struct irq_domain *domain;
  37. bool can_wake;
  38. u32 irq_fwd_mask[MAX_WORDS];
  39. u32 irq_map_mask[MAX_WORDS];
  40. };
  41. static void bcm7120_l2_intc_irq_handle(unsigned int irq, struct irq_desc *desc)
  42. {
  43. struct bcm7120_l2_intc_data *b = irq_desc_get_handler_data(desc);
  44. struct irq_chip *chip = irq_desc_get_chip(desc);
  45. unsigned int idx;
  46. chained_irq_enter(chip, desc);
  47. for (idx = 0; idx < b->n_words; idx++) {
  48. int base = idx * IRQS_PER_WORD;
  49. struct irq_chip_generic *gc =
  50. irq_get_domain_generic_chip(b->domain, base);
  51. unsigned long pending;
  52. int hwirq;
  53. irq_gc_lock(gc);
  54. pending = irq_reg_readl(gc, IRQSTAT) & gc->mask_cache;
  55. irq_gc_unlock(gc);
  56. for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
  57. generic_handle_irq(irq_find_mapping(b->domain,
  58. base + hwirq));
  59. }
  60. }
  61. chained_irq_exit(chip, desc);
  62. }
  63. static void bcm7120_l2_intc_suspend(struct irq_data *d)
  64. {
  65. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  66. struct bcm7120_l2_intc_data *b = gc->private;
  67. irq_gc_lock(gc);
  68. if (b->can_wake)
  69. irq_reg_writel(gc, gc->mask_cache | gc->wake_active, IRQEN);
  70. irq_gc_unlock(gc);
  71. }
  72. static void bcm7120_l2_intc_resume(struct irq_data *d)
  73. {
  74. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  75. /* Restore the saved mask */
  76. irq_gc_lock(gc);
  77. irq_reg_writel(gc, gc->mask_cache, IRQEN);
  78. irq_gc_unlock(gc);
  79. }
  80. static int bcm7120_l2_intc_init_one(struct device_node *dn,
  81. struct bcm7120_l2_intc_data *data,
  82. int irq, const __be32 *map_mask)
  83. {
  84. int parent_irq;
  85. unsigned int idx;
  86. parent_irq = irq_of_parse_and_map(dn, irq);
  87. if (!parent_irq) {
  88. pr_err("failed to map interrupt %d\n", irq);
  89. return -EINVAL;
  90. }
  91. /* For multiple parent IRQs with multiple words, this looks like:
  92. * <irq0_w0 irq0_w1 irq1_w0 irq1_w1 ...>
  93. */
  94. for (idx = 0; idx < data->n_words; idx++)
  95. data->irq_map_mask[idx] |=
  96. be32_to_cpup(map_mask + irq * data->n_words + idx);
  97. irq_set_handler_data(parent_irq, data);
  98. irq_set_chained_handler(parent_irq, bcm7120_l2_intc_irq_handle);
  99. return 0;
  100. }
  101. int __init bcm7120_l2_intc_of_init(struct device_node *dn,
  102. struct device_node *parent)
  103. {
  104. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  105. struct bcm7120_l2_intc_data *data;
  106. struct irq_chip_generic *gc;
  107. struct irq_chip_type *ct;
  108. const __be32 *map_mask;
  109. int num_parent_irqs;
  110. int ret = 0, len;
  111. unsigned int idx, irq, flags;
  112. data = kzalloc(sizeof(*data), GFP_KERNEL);
  113. if (!data)
  114. return -ENOMEM;
  115. for (idx = 0; idx < MAX_WORDS; idx++) {
  116. data->base[idx] = of_iomap(dn, idx);
  117. if (!data->base[idx])
  118. break;
  119. data->n_words = idx + 1;
  120. }
  121. if (!data->n_words) {
  122. pr_err("failed to remap intc L2 registers\n");
  123. ret = -ENOMEM;
  124. goto out_unmap;
  125. }
  126. /* Enable all interrupts specified in the interrupt forward mask;
  127. * disable all others. If the property doesn't exist (-EINVAL),
  128. * assume all zeroes.
  129. */
  130. ret = of_property_read_u32_array(dn, "brcm,int-fwd-mask",
  131. data->irq_fwd_mask, data->n_words);
  132. if (ret == 0 || ret == -EINVAL) {
  133. for (idx = 0; idx < data->n_words; idx++)
  134. __raw_writel(data->irq_fwd_mask[idx],
  135. data->base[idx] + IRQEN);
  136. } else {
  137. /* property exists but has the wrong number of words */
  138. pr_err("invalid int-fwd-mask property\n");
  139. ret = -EINVAL;
  140. goto out_unmap;
  141. }
  142. num_parent_irqs = of_irq_count(dn);
  143. if (num_parent_irqs <= 0) {
  144. pr_err("invalid number of parent interrupts\n");
  145. ret = -ENOMEM;
  146. goto out_unmap;
  147. }
  148. map_mask = of_get_property(dn, "brcm,int-map-mask", &len);
  149. if (!map_mask ||
  150. (len != (sizeof(*map_mask) * num_parent_irqs * data->n_words))) {
  151. pr_err("invalid brcm,int-map-mask property\n");
  152. ret = -EINVAL;
  153. goto out_unmap;
  154. }
  155. for (irq = 0; irq < num_parent_irqs; irq++) {
  156. ret = bcm7120_l2_intc_init_one(dn, data, irq, map_mask);
  157. if (ret)
  158. goto out_unmap;
  159. }
  160. data->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * data->n_words,
  161. &irq_generic_chip_ops, NULL);
  162. if (!data->domain) {
  163. ret = -ENOMEM;
  164. goto out_unmap;
  165. }
  166. /* MIPS chips strapped for BE will automagically configure the
  167. * peripheral registers for CPU-native byte order.
  168. */
  169. flags = IRQ_GC_INIT_MASK_CACHE;
  170. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  171. flags |= IRQ_GC_BE_IO;
  172. ret = irq_alloc_domain_generic_chips(data->domain, IRQS_PER_WORD, 1,
  173. dn->full_name, handle_level_irq, clr, 0, flags);
  174. if (ret) {
  175. pr_err("failed to allocate generic irq chip\n");
  176. goto out_free_domain;
  177. }
  178. if (of_property_read_bool(dn, "brcm,irq-can-wake"))
  179. data->can_wake = true;
  180. for (idx = 0; idx < data->n_words; idx++) {
  181. irq = idx * IRQS_PER_WORD;
  182. gc = irq_get_domain_generic_chip(data->domain, irq);
  183. gc->unused = 0xffffffff & ~data->irq_map_mask[idx];
  184. gc->reg_base = data->base[idx];
  185. gc->private = data;
  186. ct = gc->chip_types;
  187. ct->regs.mask = IRQEN;
  188. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  189. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  190. ct->chip.irq_ack = irq_gc_noop;
  191. ct->chip.irq_suspend = bcm7120_l2_intc_suspend;
  192. ct->chip.irq_resume = bcm7120_l2_intc_resume;
  193. if (data->can_wake) {
  194. /* This IRQ chip can wake the system, set all
  195. * relevant child interupts in wake_enabled mask
  196. */
  197. gc->wake_enabled = 0xffffffff;
  198. gc->wake_enabled &= ~gc->unused;
  199. ct->chip.irq_set_wake = irq_gc_set_wake;
  200. }
  201. }
  202. pr_info("registered BCM7120 L2 intc (mem: 0x%p, parent IRQ(s): %d)\n",
  203. data->base[0], num_parent_irqs);
  204. return 0;
  205. out_free_domain:
  206. irq_domain_remove(data->domain);
  207. out_unmap:
  208. for (idx = 0; idx < MAX_WORDS; idx++) {
  209. if (data->base[idx])
  210. iounmap(data->base[idx]);
  211. }
  212. kfree(data);
  213. return ret;
  214. }
  215. IRQCHIP_DECLARE(bcm7120_l2_intc, "brcm,bcm7120-l2-intc",
  216. bcm7120_l2_intc_of_init);