irq-brcmstb-l2.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Generic Broadcom Set Top Box 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. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/kconfig.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/io.h>
  29. #include <linux/irqdomain.h>
  30. #include <linux/irqchip.h>
  31. #include <linux/irqchip/chained_irq.h>
  32. /* Register offsets in the L2 interrupt controller */
  33. #define CPU_STATUS 0x00
  34. #define CPU_SET 0x04
  35. #define CPU_CLEAR 0x08
  36. #define CPU_MASK_STATUS 0x0c
  37. #define CPU_MASK_SET 0x10
  38. #define CPU_MASK_CLEAR 0x14
  39. /* L2 intc private data structure */
  40. struct brcmstb_l2_intc_data {
  41. int parent_irq;
  42. void __iomem *base;
  43. struct irq_domain *domain;
  44. bool can_wake;
  45. u32 saved_mask; /* for suspend/resume */
  46. };
  47. static void brcmstb_l2_intc_irq_handle(struct irq_desc *desc)
  48. {
  49. struct brcmstb_l2_intc_data *b = irq_desc_get_handler_data(desc);
  50. struct irq_chip_generic *gc = irq_get_domain_generic_chip(b->domain, 0);
  51. struct irq_chip *chip = irq_desc_get_chip(desc);
  52. unsigned int irq;
  53. u32 status;
  54. chained_irq_enter(chip, desc);
  55. status = irq_reg_readl(gc, CPU_STATUS) &
  56. ~(irq_reg_readl(gc, CPU_MASK_STATUS));
  57. if (status == 0) {
  58. raw_spin_lock(&desc->lock);
  59. handle_bad_irq(desc);
  60. raw_spin_unlock(&desc->lock);
  61. goto out;
  62. }
  63. do {
  64. irq = ffs(status) - 1;
  65. /* ack at our level */
  66. irq_reg_writel(gc, 1 << irq, CPU_CLEAR);
  67. status &= ~(1 << irq);
  68. generic_handle_irq(irq_find_mapping(b->domain, irq));
  69. } while (status);
  70. out:
  71. chained_irq_exit(chip, desc);
  72. }
  73. static void brcmstb_l2_intc_suspend(struct irq_data *d)
  74. {
  75. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  76. struct brcmstb_l2_intc_data *b = gc->private;
  77. irq_gc_lock(gc);
  78. /* Save the current mask */
  79. b->saved_mask = irq_reg_readl(gc, CPU_MASK_STATUS);
  80. if (b->can_wake) {
  81. /* Program the wakeup mask */
  82. irq_reg_writel(gc, ~gc->wake_active, CPU_MASK_SET);
  83. irq_reg_writel(gc, gc->wake_active, CPU_MASK_CLEAR);
  84. }
  85. irq_gc_unlock(gc);
  86. }
  87. static void brcmstb_l2_intc_resume(struct irq_data *d)
  88. {
  89. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  90. struct brcmstb_l2_intc_data *b = gc->private;
  91. irq_gc_lock(gc);
  92. /* Clear unmasked non-wakeup interrupts */
  93. irq_reg_writel(gc, ~b->saved_mask & ~gc->wake_active, CPU_CLEAR);
  94. /* Restore the saved mask */
  95. irq_reg_writel(gc, b->saved_mask, CPU_MASK_SET);
  96. irq_reg_writel(gc, ~b->saved_mask, CPU_MASK_CLEAR);
  97. irq_gc_unlock(gc);
  98. }
  99. int __init brcmstb_l2_intc_of_init(struct device_node *np,
  100. struct device_node *parent)
  101. {
  102. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  103. struct brcmstb_l2_intc_data *data;
  104. struct irq_chip_generic *gc;
  105. struct irq_chip_type *ct;
  106. int ret;
  107. unsigned int flags;
  108. data = kzalloc(sizeof(*data), GFP_KERNEL);
  109. if (!data)
  110. return -ENOMEM;
  111. data->base = of_iomap(np, 0);
  112. if (!data->base) {
  113. pr_err("failed to remap intc L2 registers\n");
  114. ret = -ENOMEM;
  115. goto out_free;
  116. }
  117. /* Disable all interrupts by default */
  118. writel(0xffffffff, data->base + CPU_MASK_SET);
  119. /* Wakeup interrupts may be retained from S5 (cold boot) */
  120. data->can_wake = of_property_read_bool(np, "brcm,irq-can-wake");
  121. if (!data->can_wake)
  122. writel(0xffffffff, data->base + CPU_CLEAR);
  123. data->parent_irq = irq_of_parse_and_map(np, 0);
  124. if (!data->parent_irq) {
  125. pr_err("failed to find parent interrupt\n");
  126. ret = -EINVAL;
  127. goto out_unmap;
  128. }
  129. data->domain = irq_domain_add_linear(np, 32,
  130. &irq_generic_chip_ops, NULL);
  131. if (!data->domain) {
  132. ret = -ENOMEM;
  133. goto out_unmap;
  134. }
  135. /* MIPS chips strapped for BE will automagically configure the
  136. * peripheral registers for CPU-native byte order.
  137. */
  138. flags = 0;
  139. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  140. flags |= IRQ_GC_BE_IO;
  141. /* Allocate a single Generic IRQ chip for this node */
  142. ret = irq_alloc_domain_generic_chips(data->domain, 32, 1,
  143. np->full_name, handle_edge_irq, clr, 0, flags);
  144. if (ret) {
  145. pr_err("failed to allocate generic irq chip\n");
  146. goto out_free_domain;
  147. }
  148. /* Set the IRQ chaining logic */
  149. irq_set_chained_handler_and_data(data->parent_irq,
  150. brcmstb_l2_intc_irq_handle, data);
  151. gc = irq_get_domain_generic_chip(data->domain, 0);
  152. gc->reg_base = data->base;
  153. gc->private = data;
  154. ct = gc->chip_types;
  155. ct->chip.irq_ack = irq_gc_ack_set_bit;
  156. ct->regs.ack = CPU_CLEAR;
  157. ct->chip.irq_mask = irq_gc_mask_disable_reg;
  158. ct->regs.disable = CPU_MASK_SET;
  159. ct->chip.irq_unmask = irq_gc_unmask_enable_reg;
  160. ct->regs.enable = CPU_MASK_CLEAR;
  161. ct->chip.irq_suspend = brcmstb_l2_intc_suspend;
  162. ct->chip.irq_resume = brcmstb_l2_intc_resume;
  163. if (data->can_wake) {
  164. /* This IRQ chip can wake the system, set all child interrupts
  165. * in wake_enabled mask
  166. */
  167. gc->wake_enabled = 0xffffffff;
  168. ct->chip.irq_set_wake = irq_gc_set_wake;
  169. }
  170. pr_info("registered L2 intc (mem: 0x%p, parent irq: %d)\n",
  171. data->base, data->parent_irq);
  172. return 0;
  173. out_free_domain:
  174. irq_domain_remove(data->domain);
  175. out_unmap:
  176. iounmap(data->base);
  177. out_free:
  178. kfree(data);
  179. return ret;
  180. }
  181. IRQCHIP_DECLARE(brcmstb_l2_intc, "brcm,l2-intc", brcmstb_l2_intc_of_init);