flipper-pic.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * arch/powerpc/platforms/embedded6xx/flipper-pic.c
  3. *
  4. * Nintendo GameCube/Wii "Flipper" interrupt controller support.
  5. * Copyright (C) 2004-2009 The GameCube Linux Team
  6. * Copyright (C) 2007,2008,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 "flipper-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 <asm/io.h>
  21. #include "flipper-pic.h"
  22. #define FLIPPER_NR_IRQS 32
  23. /*
  24. * Each interrupt has a corresponding bit in both
  25. * the Interrupt Cause (ICR) and Interrupt Mask (IMR) registers.
  26. *
  27. * Enabling/disabling an interrupt line involves setting/clearing
  28. * the corresponding bit in IMR.
  29. * Except for the RSW interrupt, all interrupts get deasserted automatically
  30. * when the source deasserts the interrupt.
  31. */
  32. #define FLIPPER_ICR 0x00
  33. #define FLIPPER_ICR_RSS (1<<16) /* reset switch state */
  34. #define FLIPPER_IMR 0x04
  35. #define FLIPPER_RESET 0x24
  36. /*
  37. * IRQ chip hooks.
  38. *
  39. */
  40. static void flipper_pic_mask_and_ack(struct irq_data *d)
  41. {
  42. int irq = irqd_to_hwirq(d);
  43. void __iomem *io_base = irq_data_get_irq_chip_data(d);
  44. u32 mask = 1 << irq;
  45. clrbits32(io_base + FLIPPER_IMR, mask);
  46. /* this is at least needed for RSW */
  47. out_be32(io_base + FLIPPER_ICR, mask);
  48. }
  49. static void flipper_pic_ack(struct irq_data *d)
  50. {
  51. int irq = irqd_to_hwirq(d);
  52. void __iomem *io_base = irq_data_get_irq_chip_data(d);
  53. /* this is at least needed for RSW */
  54. out_be32(io_base + FLIPPER_ICR, 1 << irq);
  55. }
  56. static void flipper_pic_mask(struct irq_data *d)
  57. {
  58. int irq = irqd_to_hwirq(d);
  59. void __iomem *io_base = irq_data_get_irq_chip_data(d);
  60. clrbits32(io_base + FLIPPER_IMR, 1 << irq);
  61. }
  62. static void flipper_pic_unmask(struct irq_data *d)
  63. {
  64. int irq = irqd_to_hwirq(d);
  65. void __iomem *io_base = irq_data_get_irq_chip_data(d);
  66. setbits32(io_base + FLIPPER_IMR, 1 << irq);
  67. }
  68. static struct irq_chip flipper_pic = {
  69. .name = "flipper-pic",
  70. .irq_ack = flipper_pic_ack,
  71. .irq_mask_ack = flipper_pic_mask_and_ack,
  72. .irq_mask = flipper_pic_mask,
  73. .irq_unmask = flipper_pic_unmask,
  74. };
  75. /*
  76. * IRQ host hooks.
  77. *
  78. */
  79. static struct irq_host *flipper_irq_host;
  80. static int flipper_pic_map(struct irq_host *h, unsigned int virq,
  81. irq_hw_number_t hwirq)
  82. {
  83. irq_set_chip_data(virq, h->host_data);
  84. irq_set_status_flags(virq, IRQ_LEVEL);
  85. irq_set_chip_and_handler(virq, &flipper_pic, handle_level_irq);
  86. return 0;
  87. }
  88. static int flipper_pic_match(struct irq_host *h, struct device_node *np)
  89. {
  90. return 1;
  91. }
  92. static struct irq_host_ops flipper_irq_host_ops = {
  93. .map = flipper_pic_map,
  94. .match = flipper_pic_match,
  95. };
  96. /*
  97. * Platform hooks.
  98. *
  99. */
  100. static void __flipper_quiesce(void __iomem *io_base)
  101. {
  102. /* mask and ack all IRQs */
  103. out_be32(io_base + FLIPPER_IMR, 0x00000000);
  104. out_be32(io_base + FLIPPER_ICR, 0xffffffff);
  105. }
  106. struct irq_host * __init flipper_pic_init(struct device_node *np)
  107. {
  108. struct device_node *pi;
  109. struct irq_host *irq_host = NULL;
  110. struct resource res;
  111. void __iomem *io_base;
  112. int retval;
  113. pi = of_get_parent(np);
  114. if (!pi) {
  115. pr_err("no parent found\n");
  116. goto out;
  117. }
  118. if (!of_device_is_compatible(pi, "nintendo,flipper-pi")) {
  119. pr_err("unexpected parent compatible\n");
  120. goto out;
  121. }
  122. retval = of_address_to_resource(pi, 0, &res);
  123. if (retval) {
  124. pr_err("no io memory range found\n");
  125. goto out;
  126. }
  127. io_base = ioremap(res.start, resource_size(&res));
  128. pr_info("controller at 0x%08x mapped to 0x%p\n", res.start, io_base);
  129. __flipper_quiesce(io_base);
  130. irq_host = irq_alloc_host(np, IRQ_HOST_MAP_LINEAR, FLIPPER_NR_IRQS,
  131. &flipper_irq_host_ops, -1);
  132. if (!irq_host) {
  133. pr_err("failed to allocate irq_host\n");
  134. return NULL;
  135. }
  136. irq_host->host_data = io_base;
  137. out:
  138. return irq_host;
  139. }
  140. unsigned int flipper_pic_get_irq(void)
  141. {
  142. void __iomem *io_base = flipper_irq_host->host_data;
  143. int irq;
  144. u32 irq_status;
  145. irq_status = in_be32(io_base + FLIPPER_ICR) &
  146. in_be32(io_base + FLIPPER_IMR);
  147. if (irq_status == 0)
  148. return NO_IRQ; /* no more IRQs pending */
  149. irq = __ffs(irq_status);
  150. return irq_linear_revmap(flipper_irq_host, irq);
  151. }
  152. /*
  153. * Probe function.
  154. *
  155. */
  156. void __init flipper_pic_probe(void)
  157. {
  158. struct device_node *np;
  159. np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-pic");
  160. BUG_ON(!np);
  161. flipper_irq_host = flipper_pic_init(np);
  162. BUG_ON(!flipper_irq_host);
  163. irq_set_default_host(flipper_irq_host);
  164. of_node_put(np);
  165. }
  166. /*
  167. * Misc functions related to the flipper chipset.
  168. *
  169. */
  170. /**
  171. * flipper_quiesce() - quiesce flipper irq controller
  172. *
  173. * Mask and ack all interrupt sources.
  174. *
  175. */
  176. void flipper_quiesce(void)
  177. {
  178. void __iomem *io_base = flipper_irq_host->host_data;
  179. __flipper_quiesce(io_base);
  180. }
  181. /*
  182. * Resets the platform.
  183. */
  184. void flipper_platform_reset(void)
  185. {
  186. void __iomem *io_base;
  187. if (flipper_irq_host && flipper_irq_host->host_data) {
  188. io_base = flipper_irq_host->host_data;
  189. out_8(io_base + FLIPPER_RESET, 0x00);
  190. }
  191. }
  192. /*
  193. * Returns non-zero if the reset button is pressed.
  194. */
  195. int flipper_is_reset_button_pressed(void)
  196. {
  197. void __iomem *io_base;
  198. u32 icr;
  199. if (flipper_irq_host && flipper_irq_host->host_data) {
  200. io_base = flipper_irq_host->host_data;
  201. icr = in_be32(io_base + FLIPPER_ICR);
  202. return !(icr & FLIPPER_ICR_RSS);
  203. }
  204. return 0;
  205. }