blob: ade83829d5e8bb60d60616a1bc8de5336dedf7c8 [file] [log] [blame]
Albert Herranz028ee972009-12-12 06:31:41 +00001/*
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
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/irq.h>
20#include <linux/of.h>
Rob Herring26a20562013-09-26 07:40:04 -050021#include <linux/of_address.h>
Albert Herranz028ee972009-12-12 06:31:41 +000022#include <asm/io.h>
23
24#include "flipper-pic.h"
25
26#define FLIPPER_NR_IRQS 32
27
28/*
29 * Each interrupt has a corresponding bit in both
30 * the Interrupt Cause (ICR) and Interrupt Mask (IMR) registers.
31 *
32 * Enabling/disabling an interrupt line involves setting/clearing
33 * the corresponding bit in IMR.
34 * Except for the RSW interrupt, all interrupts get deasserted automatically
35 * when the source deasserts the interrupt.
36 */
37#define FLIPPER_ICR 0x00
38#define FLIPPER_ICR_RSS (1<<16) /* reset switch state */
39
40#define FLIPPER_IMR 0x04
41
42#define FLIPPER_RESET 0x24
43
44
45/*
46 * IRQ chip hooks.
47 *
48 */
49
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000050static void flipper_pic_mask_and_ack(struct irq_data *d)
Albert Herranz028ee972009-12-12 06:31:41 +000051{
Grant Likely476eb492011-05-04 15:02:15 +100052 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000053 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz028ee972009-12-12 06:31:41 +000054 u32 mask = 1 << irq;
55
56 clrbits32(io_base + FLIPPER_IMR, mask);
57 /* this is at least needed for RSW */
58 out_be32(io_base + FLIPPER_ICR, mask);
59}
60
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000061static void flipper_pic_ack(struct irq_data *d)
Albert Herranz028ee972009-12-12 06:31:41 +000062{
Grant Likely476eb492011-05-04 15:02:15 +100063 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000064 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz028ee972009-12-12 06:31:41 +000065
66 /* this is at least needed for RSW */
67 out_be32(io_base + FLIPPER_ICR, 1 << irq);
68}
69
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000070static void flipper_pic_mask(struct irq_data *d)
Albert Herranz028ee972009-12-12 06:31:41 +000071{
Grant Likely476eb492011-05-04 15:02:15 +100072 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000073 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz028ee972009-12-12 06:31:41 +000074
75 clrbits32(io_base + FLIPPER_IMR, 1 << irq);
76}
77
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000078static void flipper_pic_unmask(struct irq_data *d)
Albert Herranz028ee972009-12-12 06:31:41 +000079{
Grant Likely476eb492011-05-04 15:02:15 +100080 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000081 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz028ee972009-12-12 06:31:41 +000082
83 setbits32(io_base + FLIPPER_IMR, 1 << irq);
84}
85
86
87static struct irq_chip flipper_pic = {
88 .name = "flipper-pic",
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000089 .irq_ack = flipper_pic_ack,
90 .irq_mask_ack = flipper_pic_mask_and_ack,
91 .irq_mask = flipper_pic_mask,
92 .irq_unmask = flipper_pic_unmask,
Albert Herranz028ee972009-12-12 06:31:41 +000093};
94
95/*
96 * IRQ host hooks.
97 *
98 */
99
Grant Likelybae1d8f2012-02-14 14:06:50 -0700100static struct irq_domain *flipper_irq_host;
Albert Herranz028ee972009-12-12 06:31:41 +0000101
Grant Likelybae1d8f2012-02-14 14:06:50 -0700102static int flipper_pic_map(struct irq_domain *h, unsigned int virq,
Albert Herranz028ee972009-12-12 06:31:41 +0000103 irq_hw_number_t hwirq)
104{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100105 irq_set_chip_data(virq, h->host_data);
Thomas Gleixner98488db2011-03-25 15:43:57 +0100106 irq_set_status_flags(virq, IRQ_LEVEL);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100107 irq_set_chip_and_handler(virq, &flipper_pic, handle_level_irq);
Albert Herranz028ee972009-12-12 06:31:41 +0000108 return 0;
109}
110
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100111static int flipper_pic_match(struct irq_domain *h, struct device_node *np,
112 enum irq_domain_bus_token bus_token)
Albert Herranz028ee972009-12-12 06:31:41 +0000113{
114 return 1;
115}
116
117
Grant Likely9f70b8e2012-01-26 12:24:34 -0700118static const struct irq_domain_ops flipper_irq_domain_ops = {
Albert Herranz028ee972009-12-12 06:31:41 +0000119 .map = flipper_pic_map,
Albert Herranz028ee972009-12-12 06:31:41 +0000120 .match = flipper_pic_match,
121};
122
123/*
124 * Platform hooks.
125 *
126 */
127
128static void __flipper_quiesce(void __iomem *io_base)
129{
130 /* mask and ack all IRQs */
131 out_be32(io_base + FLIPPER_IMR, 0x00000000);
132 out_be32(io_base + FLIPPER_ICR, 0xffffffff);
133}
134
Grant Likelybae1d8f2012-02-14 14:06:50 -0700135struct irq_domain * __init flipper_pic_init(struct device_node *np)
Albert Herranz028ee972009-12-12 06:31:41 +0000136{
137 struct device_node *pi;
Grant Likelybae1d8f2012-02-14 14:06:50 -0700138 struct irq_domain *irq_domain = NULL;
Albert Herranz028ee972009-12-12 06:31:41 +0000139 struct resource res;
140 void __iomem *io_base;
141 int retval;
142
143 pi = of_get_parent(np);
144 if (!pi) {
145 pr_err("no parent found\n");
146 goto out;
147 }
148 if (!of_device_is_compatible(pi, "nintendo,flipper-pi")) {
149 pr_err("unexpected parent compatible\n");
150 goto out;
151 }
152
153 retval = of_address_to_resource(pi, 0, &res);
154 if (retval) {
155 pr_err("no io memory range found\n");
156 goto out;
157 }
158 io_base = ioremap(res.start, resource_size(&res));
159
160 pr_info("controller at 0x%08x mapped to 0x%p\n", res.start, io_base);
161
162 __flipper_quiesce(io_base);
163
Grant Likelya8db8cf2012-02-14 14:06:54 -0700164 irq_domain = irq_domain_add_linear(np, FLIPPER_NR_IRQS,
165 &flipper_irq_domain_ops, io_base);
Grant Likelybae1d8f2012-02-14 14:06:50 -0700166 if (!irq_domain) {
167 pr_err("failed to allocate irq_domain\n");
Albert Herranz028ee972009-12-12 06:31:41 +0000168 return NULL;
169 }
170
Albert Herranz028ee972009-12-12 06:31:41 +0000171out:
Grant Likelybae1d8f2012-02-14 14:06:50 -0700172 return irq_domain;
Albert Herranz028ee972009-12-12 06:31:41 +0000173}
174
175unsigned int flipper_pic_get_irq(void)
176{
Paul Gortmaker6d166fe2012-02-22 18:35:03 -0500177 void __iomem *io_base = flipper_irq_host->host_data;
Albert Herranz028ee972009-12-12 06:31:41 +0000178 int irq;
179 u32 irq_status;
180
181 irq_status = in_be32(io_base + FLIPPER_ICR) &
182 in_be32(io_base + FLIPPER_IMR);
183 if (irq_status == 0)
Michael Ellermanef24ba72016-09-06 21:53:24 +1000184 return 0; /* no more IRQs pending */
Albert Herranz028ee972009-12-12 06:31:41 +0000185
186 irq = __ffs(irq_status);
Paul Gortmaker6d166fe2012-02-22 18:35:03 -0500187 return irq_linear_revmap(flipper_irq_host, irq);
Albert Herranz028ee972009-12-12 06:31:41 +0000188}
189
190/*
191 * Probe function.
192 *
193 */
194
195void __init flipper_pic_probe(void)
196{
197 struct device_node *np;
198
199 np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-pic");
200 BUG_ON(!np);
201
Paul Gortmaker6d166fe2012-02-22 18:35:03 -0500202 flipper_irq_host = flipper_pic_init(np);
Albert Herranz028ee972009-12-12 06:31:41 +0000203 BUG_ON(!flipper_irq_host);
204
205 irq_set_default_host(flipper_irq_host);
206
207 of_node_put(np);
208}
209
210/*
211 * Misc functions related to the flipper chipset.
212 *
213 */
214
215/**
216 * flipper_quiesce() - quiesce flipper irq controller
217 *
218 * Mask and ack all interrupt sources.
219 *
220 */
221void flipper_quiesce(void)
222{
223 void __iomem *io_base = flipper_irq_host->host_data;
224
225 __flipper_quiesce(io_base);
226}
227
228/*
229 * Resets the platform.
230 */
231void flipper_platform_reset(void)
232{
233 void __iomem *io_base;
234
235 if (flipper_irq_host && flipper_irq_host->host_data) {
236 io_base = flipper_irq_host->host_data;
237 out_8(io_base + FLIPPER_RESET, 0x00);
238 }
239}
240
241/*
242 * Returns non-zero if the reset button is pressed.
243 */
244int flipper_is_reset_button_pressed(void)
245{
246 void __iomem *io_base;
247 u32 icr;
248
249 if (flipper_irq_host && flipper_irq_host->host_data) {
250 io_base = flipper_irq_host->host_data;
251 icr = in_be32(io_base + FLIPPER_ICR);
252 return !(icr & FLIPPER_ICR_RSS);
253 }
254 return 0;
255}
256