irq-bcm7120-l2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/kernel.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/io.h>
  24. #include <linux/irqdomain.h>
  25. #include <linux/reboot.h>
  26. #include <linux/bitops.h>
  27. #include <linux/irqchip/chained_irq.h>
  28. #include "irqchip.h"
  29. /* Register offset in the L2 interrupt controller */
  30. #define IRQEN 0x00
  31. #define IRQSTAT 0x04
  32. #define MAX_WORDS 4
  33. #define MAX_MAPPINGS (MAX_WORDS * 2)
  34. #define IRQS_PER_WORD 32
  35. struct bcm7120_l2_intc_data {
  36. unsigned int n_words;
  37. void __iomem *map_base[MAX_MAPPINGS];
  38. void __iomem *pair_base[MAX_WORDS];
  39. int en_offset[MAX_WORDS];
  40. int stat_offset[MAX_WORDS];
  41. struct irq_domain *domain;
  42. bool can_wake;
  43. u32 irq_fwd_mask[MAX_WORDS];
  44. u32 irq_map_mask[MAX_WORDS];
  45. int num_parent_irqs;
  46. const __be32 *map_mask_prop;
  47. };
  48. static void bcm7120_l2_intc_irq_handle(unsigned int irq, struct irq_desc *desc)
  49. {
  50. struct bcm7120_l2_intc_data *b = irq_desc_get_handler_data(desc);
  51. struct irq_chip *chip = irq_desc_get_chip(desc);
  52. unsigned int idx;
  53. chained_irq_enter(chip, desc);
  54. for (idx = 0; idx < b->n_words; idx++) {
  55. int base = idx * IRQS_PER_WORD;
  56. struct irq_chip_generic *gc =
  57. irq_get_domain_generic_chip(b->domain, base);
  58. unsigned long pending;
  59. int hwirq;
  60. irq_gc_lock(gc);
  61. pending = irq_reg_readl(gc, b->stat_offset[idx]) &
  62. gc->mask_cache;
  63. irq_gc_unlock(gc);
  64. for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
  65. generic_handle_irq(irq_find_mapping(b->domain,
  66. base + hwirq));
  67. }
  68. }
  69. chained_irq_exit(chip, desc);
  70. }
  71. static void bcm7120_l2_intc_suspend(struct irq_data *d)
  72. {
  73. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  74. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  75. struct bcm7120_l2_intc_data *b = gc->private;
  76. irq_gc_lock(gc);
  77. if (b->can_wake)
  78. irq_reg_writel(gc, gc->mask_cache | gc->wake_active,
  79. ct->regs.mask);
  80. irq_gc_unlock(gc);
  81. }
  82. static void bcm7120_l2_intc_resume(struct irq_data *d)
  83. {
  84. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  85. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  86. /* Restore the saved mask */
  87. irq_gc_lock(gc);
  88. irq_reg_writel(gc, gc->mask_cache, ct->regs.mask);
  89. irq_gc_unlock(gc);
  90. }
  91. static int bcm7120_l2_intc_init_one(struct device_node *dn,
  92. struct bcm7120_l2_intc_data *data,
  93. int irq)
  94. {
  95. int parent_irq;
  96. unsigned int idx;
  97. parent_irq = irq_of_parse_and_map(dn, irq);
  98. if (!parent_irq) {
  99. pr_err("failed to map interrupt %d\n", irq);
  100. return -EINVAL;
  101. }
  102. /* For multiple parent IRQs with multiple words, this looks like:
  103. * <irq0_w0 irq0_w1 irq1_w0 irq1_w1 ...>
  104. */
  105. for (idx = 0; idx < data->n_words; idx++) {
  106. if (data->map_mask_prop) {
  107. data->irq_map_mask[idx] |=
  108. be32_to_cpup(data->map_mask_prop +
  109. irq * data->n_words + idx);
  110. } else {
  111. data->irq_map_mask[idx] = 0xffffffff;
  112. }
  113. }
  114. irq_set_handler_data(parent_irq, data);
  115. irq_set_chained_handler(parent_irq, bcm7120_l2_intc_irq_handle);
  116. return 0;
  117. }
  118. static int __init bcm7120_l2_intc_iomap_7120(struct device_node *dn,
  119. struct bcm7120_l2_intc_data *data)
  120. {
  121. int ret;
  122. data->map_base[0] = of_iomap(dn, 0);
  123. if (!data->map_base[0]) {
  124. pr_err("unable to map registers\n");
  125. return -ENOMEM;
  126. }
  127. data->pair_base[0] = data->map_base[0];
  128. data->en_offset[0] = IRQEN;
  129. data->stat_offset[0] = IRQSTAT;
  130. data->n_words = 1;
  131. ret = of_property_read_u32_array(dn, "brcm,int-fwd-mask",
  132. data->irq_fwd_mask, data->n_words);
  133. if (ret != 0 && ret != -EINVAL) {
  134. /* property exists but has the wrong number of words */
  135. pr_err("invalid brcm,int-fwd-mask property\n");
  136. return -EINVAL;
  137. }
  138. data->map_mask_prop = of_get_property(dn, "brcm,int-map-mask", &ret);
  139. if (!data->map_mask_prop ||
  140. (ret != (sizeof(__be32) * data->num_parent_irqs * data->n_words))) {
  141. pr_err("invalid brcm,int-map-mask property\n");
  142. return -EINVAL;
  143. }
  144. return 0;
  145. }
  146. static int __init bcm7120_l2_intc_iomap_3380(struct device_node *dn,
  147. struct bcm7120_l2_intc_data *data)
  148. {
  149. unsigned int gc_idx;
  150. for (gc_idx = 0; gc_idx < MAX_WORDS; gc_idx++) {
  151. unsigned int map_idx = gc_idx * 2;
  152. void __iomem *en = of_iomap(dn, map_idx + 0);
  153. void __iomem *stat = of_iomap(dn, map_idx + 1);
  154. void __iomem *base = min(en, stat);
  155. data->map_base[map_idx + 0] = en;
  156. data->map_base[map_idx + 1] = stat;
  157. if (!base)
  158. break;
  159. data->pair_base[gc_idx] = base;
  160. data->en_offset[gc_idx] = en - base;
  161. data->stat_offset[gc_idx] = stat - base;
  162. }
  163. if (!gc_idx) {
  164. pr_err("unable to map registers\n");
  165. return -EINVAL;
  166. }
  167. data->n_words = gc_idx;
  168. return 0;
  169. }
  170. int __init bcm7120_l2_intc_probe(struct device_node *dn,
  171. struct device_node *parent,
  172. int (*iomap_regs_fn)(struct device_node *,
  173. struct bcm7120_l2_intc_data *),
  174. const char *intc_name)
  175. {
  176. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  177. struct bcm7120_l2_intc_data *data;
  178. struct irq_chip_generic *gc;
  179. struct irq_chip_type *ct;
  180. int ret = 0;
  181. unsigned int idx, irq, flags;
  182. data = kzalloc(sizeof(*data), GFP_KERNEL);
  183. if (!data)
  184. return -ENOMEM;
  185. data->num_parent_irqs = of_irq_count(dn);
  186. if (data->num_parent_irqs <= 0) {
  187. pr_err("invalid number of parent interrupts\n");
  188. ret = -ENOMEM;
  189. goto out_unmap;
  190. }
  191. ret = iomap_regs_fn(dn, data);
  192. if (ret < 0)
  193. goto out_unmap;
  194. for (idx = 0; idx < data->n_words; idx++) {
  195. __raw_writel(data->irq_fwd_mask[idx],
  196. data->pair_base[idx] +
  197. data->en_offset[idx]);
  198. }
  199. for (irq = 0; irq < data->num_parent_irqs; irq++) {
  200. ret = bcm7120_l2_intc_init_one(dn, data, irq);
  201. if (ret)
  202. goto out_unmap;
  203. }
  204. data->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * data->n_words,
  205. &irq_generic_chip_ops, NULL);
  206. if (!data->domain) {
  207. ret = -ENOMEM;
  208. goto out_unmap;
  209. }
  210. /* MIPS chips strapped for BE will automagically configure the
  211. * peripheral registers for CPU-native byte order.
  212. */
  213. flags = IRQ_GC_INIT_MASK_CACHE;
  214. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  215. flags |= IRQ_GC_BE_IO;
  216. ret = irq_alloc_domain_generic_chips(data->domain, IRQS_PER_WORD, 1,
  217. dn->full_name, handle_level_irq, clr, 0, flags);
  218. if (ret) {
  219. pr_err("failed to allocate generic irq chip\n");
  220. goto out_free_domain;
  221. }
  222. if (of_property_read_bool(dn, "brcm,irq-can-wake"))
  223. data->can_wake = true;
  224. for (idx = 0; idx < data->n_words; idx++) {
  225. irq = idx * IRQS_PER_WORD;
  226. gc = irq_get_domain_generic_chip(data->domain, irq);
  227. gc->unused = 0xffffffff & ~data->irq_map_mask[idx];
  228. gc->private = data;
  229. ct = gc->chip_types;
  230. gc->reg_base = data->pair_base[idx];
  231. ct->regs.mask = data->en_offset[idx];
  232. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  233. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  234. ct->chip.irq_ack = irq_gc_noop;
  235. ct->chip.irq_suspend = bcm7120_l2_intc_suspend;
  236. ct->chip.irq_resume = bcm7120_l2_intc_resume;
  237. if (data->can_wake) {
  238. /* This IRQ chip can wake the system, set all
  239. * relevant child interupts in wake_enabled mask
  240. */
  241. gc->wake_enabled = 0xffffffff;
  242. gc->wake_enabled &= ~gc->unused;
  243. ct->chip.irq_set_wake = irq_gc_set_wake;
  244. }
  245. }
  246. pr_info("registered %s intc (mem: 0x%p, parent IRQ(s): %d)\n",
  247. intc_name, data->map_base[0], data->num_parent_irqs);
  248. return 0;
  249. out_free_domain:
  250. irq_domain_remove(data->domain);
  251. out_unmap:
  252. for (idx = 0; idx < MAX_MAPPINGS; idx++) {
  253. if (data->map_base[idx])
  254. iounmap(data->map_base[idx]);
  255. }
  256. kfree(data);
  257. return ret;
  258. }
  259. int __init bcm7120_l2_intc_probe_7120(struct device_node *dn,
  260. struct device_node *parent)
  261. {
  262. return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_7120,
  263. "BCM7120 L2");
  264. }
  265. int __init bcm7120_l2_intc_probe_3380(struct device_node *dn,
  266. struct device_node *parent)
  267. {
  268. return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_3380,
  269. "BCM3380 L2");
  270. }
  271. IRQCHIP_DECLARE(bcm7120_l2_intc, "brcm,bcm7120-l2-intc",
  272. bcm7120_l2_intc_probe_7120);
  273. IRQCHIP_DECLARE(bcm3380_l2_intc, "brcm,bcm3380-l2-intc",
  274. bcm7120_l2_intc_probe_3380);