flipper-pic.c 5.4 KB

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