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