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