irq-bcm7120-l2.c 9.2 KB

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