hlwd-pic.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * arch/powerpc/platforms/embedded6xx/hlwd-pic.c
  3. *
  4. * Nintendo Wii "Hollywood" interrupt controller support.
  5. * Copyright (C) 2009 The GameCube Linux Team
  6. * Copyright (C) 2009 Albert Herranz
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. */
  14. #define DRV_MODULE_NAME "hlwd-pic"
  15. #define pr_fmt(fmt) DRV_MODULE_NAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/irq.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <asm/io.h>
  22. #include "hlwd-pic.h"
  23. #define HLWD_NR_IRQS 32
  24. /*
  25. * Each interrupt has a corresponding bit in both
  26. * the Interrupt Cause (ICR) and Interrupt Mask (IMR) registers.
  27. *
  28. * Enabling/disabling an interrupt line involves asserting/clearing
  29. * the corresponding bit in IMR. ACK'ing a request simply involves
  30. * asserting the corresponding bit in ICR.
  31. */
  32. #define HW_BROADWAY_ICR 0x00
  33. #define HW_BROADWAY_IMR 0x04
  34. /*
  35. * IRQ chip hooks.
  36. *
  37. */
  38. static void hlwd_pic_mask_and_ack(struct irq_data *d)
  39. {
  40. int irq = irqd_to_hwirq(d);
  41. void __iomem *io_base = irq_data_get_irq_chip_data(d);
  42. u32 mask = 1 << irq;
  43. clrbits32(io_base + HW_BROADWAY_IMR, mask);
  44. out_be32(io_base + HW_BROADWAY_ICR, mask);
  45. }
  46. static void hlwd_pic_ack(struct irq_data *d)
  47. {
  48. int irq = irqd_to_hwirq(d);
  49. void __iomem *io_base = irq_data_get_irq_chip_data(d);
  50. out_be32(io_base + HW_BROADWAY_ICR, 1 << irq);
  51. }
  52. static void hlwd_pic_mask(struct irq_data *d)
  53. {
  54. int irq = irqd_to_hwirq(d);
  55. void __iomem *io_base = irq_data_get_irq_chip_data(d);
  56. clrbits32(io_base + HW_BROADWAY_IMR, 1 << irq);
  57. }
  58. static void hlwd_pic_unmask(struct irq_data *d)
  59. {
  60. int irq = irqd_to_hwirq(d);
  61. void __iomem *io_base = irq_data_get_irq_chip_data(d);
  62. setbits32(io_base + HW_BROADWAY_IMR, 1 << irq);
  63. }
  64. static struct irq_chip hlwd_pic = {
  65. .name = "hlwd-pic",
  66. .irq_ack = hlwd_pic_ack,
  67. .irq_mask_ack = hlwd_pic_mask_and_ack,
  68. .irq_mask = hlwd_pic_mask,
  69. .irq_unmask = hlwd_pic_unmask,
  70. };
  71. /*
  72. * IRQ host hooks.
  73. *
  74. */
  75. static struct irq_domain *hlwd_irq_host;
  76. static int hlwd_pic_map(struct irq_domain *h, unsigned int virq,
  77. irq_hw_number_t hwirq)
  78. {
  79. irq_set_chip_data(virq, h->host_data);
  80. irq_set_status_flags(virq, IRQ_LEVEL);
  81. irq_set_chip_and_handler(virq, &hlwd_pic, handle_level_irq);
  82. return 0;
  83. }
  84. static const struct irq_domain_ops hlwd_irq_domain_ops = {
  85. .map = hlwd_pic_map,
  86. };
  87. static unsigned int __hlwd_pic_get_irq(struct irq_domain *h)
  88. {
  89. void __iomem *io_base = h->host_data;
  90. int irq;
  91. u32 irq_status;
  92. irq_status = in_be32(io_base + HW_BROADWAY_ICR) &
  93. in_be32(io_base + HW_BROADWAY_IMR);
  94. if (irq_status == 0)
  95. return NO_IRQ; /* no more IRQs pending */
  96. irq = __ffs(irq_status);
  97. return irq_linear_revmap(h, irq);
  98. }
  99. static void hlwd_pic_irq_cascade(struct irq_desc *desc)
  100. {
  101. struct irq_chip *chip = irq_desc_get_chip(desc);
  102. struct irq_domain *irq_domain = irq_desc_get_handler_data(desc);
  103. unsigned int virq;
  104. raw_spin_lock(&desc->lock);
  105. chip->irq_mask(&desc->irq_data); /* IRQ_LEVEL */
  106. raw_spin_unlock(&desc->lock);
  107. virq = __hlwd_pic_get_irq(irq_domain);
  108. if (virq != NO_IRQ)
  109. generic_handle_irq(virq);
  110. else
  111. pr_err("spurious interrupt!\n");
  112. raw_spin_lock(&desc->lock);
  113. chip->irq_ack(&desc->irq_data); /* IRQ_LEVEL */
  114. if (!irqd_irq_disabled(&desc->irq_data) && chip->irq_unmask)
  115. chip->irq_unmask(&desc->irq_data);
  116. raw_spin_unlock(&desc->lock);
  117. }
  118. /*
  119. * Platform hooks.
  120. *
  121. */
  122. static void __hlwd_quiesce(void __iomem *io_base)
  123. {
  124. /* mask and ack all IRQs */
  125. out_be32(io_base + HW_BROADWAY_IMR, 0);
  126. out_be32(io_base + HW_BROADWAY_ICR, 0xffffffff);
  127. }
  128. struct irq_domain *hlwd_pic_init(struct device_node *np)
  129. {
  130. struct irq_domain *irq_domain;
  131. struct resource res;
  132. void __iomem *io_base;
  133. int retval;
  134. retval = of_address_to_resource(np, 0, &res);
  135. if (retval) {
  136. pr_err("no io memory range found\n");
  137. return NULL;
  138. }
  139. io_base = ioremap(res.start, resource_size(&res));
  140. if (!io_base) {
  141. pr_err("ioremap failed\n");
  142. return NULL;
  143. }
  144. pr_info("controller at 0x%08x mapped to 0x%p\n", res.start, io_base);
  145. __hlwd_quiesce(io_base);
  146. irq_domain = irq_domain_add_linear(np, HLWD_NR_IRQS,
  147. &hlwd_irq_domain_ops, io_base);
  148. if (!irq_domain) {
  149. pr_err("failed to allocate irq_domain\n");
  150. iounmap(io_base);
  151. return NULL;
  152. }
  153. return irq_domain;
  154. }
  155. unsigned int hlwd_pic_get_irq(void)
  156. {
  157. return __hlwd_pic_get_irq(hlwd_irq_host);
  158. }
  159. /*
  160. * Probe function.
  161. *
  162. */
  163. void hlwd_pic_probe(void)
  164. {
  165. struct irq_domain *host;
  166. struct device_node *np;
  167. const u32 *interrupts;
  168. int cascade_virq;
  169. for_each_compatible_node(np, NULL, "nintendo,hollywood-pic") {
  170. interrupts = of_get_property(np, "interrupts", NULL);
  171. if (interrupts) {
  172. host = hlwd_pic_init(np);
  173. BUG_ON(!host);
  174. cascade_virq = irq_of_parse_and_map(np, 0);
  175. irq_set_handler_data(cascade_virq, host);
  176. irq_set_chained_handler(cascade_virq,
  177. hlwd_pic_irq_cascade);
  178. hlwd_irq_host = host;
  179. break;
  180. }
  181. }
  182. }
  183. /**
  184. * hlwd_quiesce() - quiesce hollywood irq controller
  185. *
  186. * Mask and ack all interrupt sources.
  187. *
  188. */
  189. void hlwd_quiesce(void)
  190. {
  191. void __iomem *io_base = hlwd_irq_host->host_data;
  192. __hlwd_quiesce(io_base);
  193. }