hlwd-pic.c 5.2 KB

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