irq-brcmstb-l2.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/io.h>
  27. #include <linux/irqdomain.h>
  28. #include <linux/irqchip.h>
  29. #include <linux/irqchip/chained_irq.h>
  30. #include <asm/mach/irq.h>
  31. #include "irqchip.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(unsigned int irq, struct irq_desc *desc)
  48. {
  49. struct brcmstb_l2_intc_data *b = irq_desc_get_handler_data(desc);
  50. struct irq_chip *chip = irq_desc_get_chip(desc);
  51. u32 status;
  52. chained_irq_enter(chip, desc);
  53. status = __raw_readl(b->base + CPU_STATUS) &
  54. ~(__raw_readl(b->base + CPU_MASK_STATUS));
  55. if (status == 0) {
  56. do_bad_IRQ(irq, desc);
  57. goto out;
  58. }
  59. do {
  60. irq = ffs(status) - 1;
  61. /* ack at our level */
  62. __raw_writel(1 << irq, b->base + CPU_CLEAR);
  63. status &= ~(1 << irq);
  64. generic_handle_irq(irq_find_mapping(b->domain, irq));
  65. } while (status);
  66. out:
  67. chained_irq_exit(chip, desc);
  68. }
  69. static void brcmstb_l2_intc_suspend(struct irq_data *d)
  70. {
  71. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  72. struct brcmstb_l2_intc_data *b = gc->private;
  73. irq_gc_lock(gc);
  74. /* Save the current mask */
  75. b->saved_mask = __raw_readl(b->base + CPU_MASK_STATUS);
  76. if (b->can_wake) {
  77. /* Program the wakeup mask */
  78. __raw_writel(~gc->wake_active, b->base + CPU_MASK_SET);
  79. __raw_writel(gc->wake_active, b->base + CPU_MASK_CLEAR);
  80. }
  81. irq_gc_unlock(gc);
  82. }
  83. static void brcmstb_l2_intc_resume(struct irq_data *d)
  84. {
  85. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  86. struct brcmstb_l2_intc_data *b = gc->private;
  87. irq_gc_lock(gc);
  88. /* Clear unmasked non-wakeup interrupts */
  89. __raw_writel(~b->saved_mask & ~gc->wake_active, b->base + CPU_CLEAR);
  90. /* Restore the saved mask */
  91. __raw_writel(b->saved_mask, b->base + CPU_MASK_SET);
  92. __raw_writel(~b->saved_mask, b->base + CPU_MASK_CLEAR);
  93. irq_gc_unlock(gc);
  94. }
  95. int __init brcmstb_l2_intc_of_init(struct device_node *np,
  96. struct device_node *parent)
  97. {
  98. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  99. struct brcmstb_l2_intc_data *data;
  100. struct irq_chip_generic *gc;
  101. struct irq_chip_type *ct;
  102. int ret;
  103. data = kzalloc(sizeof(*data), GFP_KERNEL);
  104. if (!data)
  105. return -ENOMEM;
  106. data->base = of_iomap(np, 0);
  107. if (!data->base) {
  108. pr_err("failed to remap intc L2 registers\n");
  109. ret = -ENOMEM;
  110. goto out_free;
  111. }
  112. /* Disable all interrupts by default */
  113. __raw_writel(0xffffffff, data->base + CPU_MASK_SET);
  114. __raw_writel(0xffffffff, data->base + CPU_CLEAR);
  115. data->parent_irq = irq_of_parse_and_map(np, 0);
  116. if (data->parent_irq < 0) {
  117. pr_err("failed to find parent interrupt\n");
  118. ret = data->parent_irq;
  119. goto out_unmap;
  120. }
  121. data->domain = irq_domain_add_linear(np, 32,
  122. &irq_generic_chip_ops, NULL);
  123. if (!data->domain) {
  124. ret = -ENOMEM;
  125. goto out_unmap;
  126. }
  127. /* Allocate a single Generic IRQ chip for this node */
  128. ret = irq_alloc_domain_generic_chips(data->domain, 32, 1,
  129. np->full_name, handle_edge_irq, clr, 0, 0);
  130. if (ret) {
  131. pr_err("failed to allocate generic irq chip\n");
  132. goto out_free_domain;
  133. }
  134. /* Set the IRQ chaining logic */
  135. irq_set_handler_data(data->parent_irq, data);
  136. irq_set_chained_handler(data->parent_irq, brcmstb_l2_intc_irq_handle);
  137. gc = irq_get_domain_generic_chip(data->domain, 0);
  138. gc->reg_base = data->base;
  139. gc->private = data;
  140. ct = gc->chip_types;
  141. ct->chip.irq_ack = irq_gc_ack_set_bit;
  142. ct->regs.ack = CPU_CLEAR;
  143. ct->chip.irq_mask = irq_gc_mask_disable_reg;
  144. ct->regs.disable = CPU_MASK_SET;
  145. ct->chip.irq_unmask = irq_gc_unmask_enable_reg;
  146. ct->regs.enable = CPU_MASK_CLEAR;
  147. ct->chip.irq_suspend = brcmstb_l2_intc_suspend;
  148. ct->chip.irq_resume = brcmstb_l2_intc_resume;
  149. if (of_property_read_bool(np, "brcm,irq-can-wake")) {
  150. data->can_wake = true;
  151. /* This IRQ chip can wake the system, set all child interrupts
  152. * in wake_enabled mask
  153. */
  154. gc->wake_enabled = 0xffffffff;
  155. ct->chip.irq_set_wake = irq_gc_set_wake;
  156. }
  157. pr_info("registered L2 intc (mem: 0x%p, parent irq: %d)\n",
  158. data->base, data->parent_irq);
  159. return 0;
  160. out_free_domain:
  161. irq_domain_remove(data->domain);
  162. out_unmap:
  163. iounmap(data->base);
  164. out_free:
  165. kfree(data);
  166. return ret;
  167. }
  168. IRQCHIP_DECLARE(brcmstb_l2_intc, "brcm,l2-intc", brcmstb_l2_intc_of_init);