blob: 675335a52648a148af1448ca71a0c5d0ca509431 [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>
21#include <asm/io.h>
22
23#include "flipper-pic.h"
24
25#define FLIPPER_NR_IRQS 32
26
27/*
28 * Each interrupt has a corresponding bit in both
29 * the Interrupt Cause (ICR) and Interrupt Mask (IMR) registers.
30 *
31 * Enabling/disabling an interrupt line involves setting/clearing
32 * the corresponding bit in IMR.
33 * Except for the RSW interrupt, all interrupts get deasserted automatically
34 * when the source deasserts the interrupt.
35 */
36#define FLIPPER_ICR 0x00
37#define FLIPPER_ICR_RSS (1<<16) /* reset switch state */
38
39#define FLIPPER_IMR 0x04
40
41#define FLIPPER_RESET 0x24
42
43
44/*
45 * IRQ chip hooks.
46 *
47 */
48
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000049static void flipper_pic_mask_and_ack(struct irq_data *d)
Albert Herranz028ee972009-12-12 06:31:41 +000050{
Grant Likely476eb492011-05-04 15:02:15 +100051 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000052 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz028ee972009-12-12 06:31:41 +000053 u32 mask = 1 << irq;
54
55 clrbits32(io_base + FLIPPER_IMR, mask);
56 /* this is at least needed for RSW */
57 out_be32(io_base + FLIPPER_ICR, mask);
58}
59
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000060static void flipper_pic_ack(struct irq_data *d)
Albert Herranz028ee972009-12-12 06:31:41 +000061{
Grant Likely476eb492011-05-04 15:02:15 +100062 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000063 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz028ee972009-12-12 06:31:41 +000064
65 /* this is at least needed for RSW */
66 out_be32(io_base + FLIPPER_ICR, 1 << irq);
67}
68
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000069static void flipper_pic_mask(struct irq_data *d)
Albert Herranz028ee972009-12-12 06:31:41 +000070{
Grant Likely476eb492011-05-04 15:02:15 +100071 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000072 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz028ee972009-12-12 06:31:41 +000073
74 clrbits32(io_base + FLIPPER_IMR, 1 << irq);
75}
76
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000077static void flipper_pic_unmask(struct irq_data *d)
Albert Herranz028ee972009-12-12 06:31:41 +000078{
Grant Likely476eb492011-05-04 15:02:15 +100079 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000080 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz028ee972009-12-12 06:31:41 +000081
82 setbits32(io_base + FLIPPER_IMR, 1 << irq);
83}
84
85
86static struct irq_chip flipper_pic = {
87 .name = "flipper-pic",
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000088 .irq_ack = flipper_pic_ack,
89 .irq_mask_ack = flipper_pic_mask_and_ack,
90 .irq_mask = flipper_pic_mask,
91 .irq_unmask = flipper_pic_unmask,
Albert Herranz028ee972009-12-12 06:31:41 +000092};
93
94/*
95 * IRQ host hooks.
96 *
97 */
98
Grant Likelybae1d8f2012-02-14 14:06:50 -070099static struct irq_domain *flipper_irq_host;
Albert Herranz028ee972009-12-12 06:31:41 +0000100
Grant Likelybae1d8f2012-02-14 14:06:50 -0700101static int flipper_pic_map(struct irq_domain *h, unsigned int virq,
Albert Herranz028ee972009-12-12 06:31:41 +0000102 irq_hw_number_t hwirq)
103{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100104 irq_set_chip_data(virq, h->host_data);
Thomas Gleixner98488db2011-03-25 15:43:57 +0100105 irq_set_status_flags(virq, IRQ_LEVEL);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100106 irq_set_chip_and_handler(virq, &flipper_pic, handle_level_irq);
Albert Herranz028ee972009-12-12 06:31:41 +0000107 return 0;
108}
109
Grant Likelybae1d8f2012-02-14 14:06:50 -0700110static int flipper_pic_match(struct irq_domain *h, struct device_node *np)
Albert Herranz028ee972009-12-12 06:31:41 +0000111{
112 return 1;
113}
114
115
Grant Likely9f70b8e2012-01-26 12:24:34 -0700116static const struct irq_domain_ops flipper_irq_domain_ops = {
Albert Herranz028ee972009-12-12 06:31:41 +0000117 .map = flipper_pic_map,
Albert Herranz028ee972009-12-12 06:31:41 +0000118 .match = flipper_pic_match,
119};
120
121/*
122 * Platform hooks.
123 *
124 */
125
126static void __flipper_quiesce(void __iomem *io_base)
127{
128 /* mask and ack all IRQs */
129 out_be32(io_base + FLIPPER_IMR, 0x00000000);
130 out_be32(io_base + FLIPPER_ICR, 0xffffffff);
131}
132
Grant Likelybae1d8f2012-02-14 14:06:50 -0700133struct irq_domain * __init flipper_pic_init(struct device_node *np)
Albert Herranz028ee972009-12-12 06:31:41 +0000134{
135 struct device_node *pi;
Grant Likelybae1d8f2012-02-14 14:06:50 -0700136 struct irq_domain *irq_domain = NULL;
Albert Herranz028ee972009-12-12 06:31:41 +0000137 struct resource res;
138 void __iomem *io_base;
139 int retval;
140
141 pi = of_get_parent(np);
142 if (!pi) {
143 pr_err("no parent found\n");
144 goto out;
145 }
146 if (!of_device_is_compatible(pi, "nintendo,flipper-pi")) {
147 pr_err("unexpected parent compatible\n");
148 goto out;
149 }
150
151 retval = of_address_to_resource(pi, 0, &res);
152 if (retval) {
153 pr_err("no io memory range found\n");
154 goto out;
155 }
156 io_base = ioremap(res.start, resource_size(&res));
157
158 pr_info("controller at 0x%08x mapped to 0x%p\n", res.start, io_base);
159
160 __flipper_quiesce(io_base);
161
Grant Likelya8db8cf2012-02-14 14:06:54 -0700162 irq_domain = irq_domain_add_linear(np, FLIPPER_NR_IRQS,
163 &flipper_irq_domain_ops, io_base);
Grant Likelybae1d8f2012-02-14 14:06:50 -0700164 if (!irq_domain) {
165 pr_err("failed to allocate irq_domain\n");
Albert Herranz028ee972009-12-12 06:31:41 +0000166 return NULL;
167 }
168
Albert Herranz028ee972009-12-12 06:31:41 +0000169out:
Grant Likelybae1d8f2012-02-14 14:06:50 -0700170 return irq_domain;
Albert Herranz028ee972009-12-12 06:31:41 +0000171}
172
173unsigned int flipper_pic_get_irq(void)
174{
Grant Likelybae1d8f2012-02-14 14:06:50 -0700175 void __iomem *io_base = flipper_irq_domain->host_data;
Albert Herranz028ee972009-12-12 06:31:41 +0000176 int irq;
177 u32 irq_status;
178
179 irq_status = in_be32(io_base + FLIPPER_ICR) &
180 in_be32(io_base + FLIPPER_IMR);
181 if (irq_status == 0)
182 return NO_IRQ; /* no more IRQs pending */
183
184 irq = __ffs(irq_status);
Grant Likelybae1d8f2012-02-14 14:06:50 -0700185 return irq_linear_revmap(flipper_irq_domain, irq);
Albert Herranz028ee972009-12-12 06:31:41 +0000186}
187
188/*
189 * Probe function.
190 *
191 */
192
193void __init flipper_pic_probe(void)
194{
195 struct device_node *np;
196
197 np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-pic");
198 BUG_ON(!np);
199
Grant Likelybae1d8f2012-02-14 14:06:50 -0700200 flipper_irq_domain = flipper_pic_init(np);
Albert Herranz028ee972009-12-12 06:31:41 +0000201 BUG_ON(!flipper_irq_host);
202
203 irq_set_default_host(flipper_irq_host);
204
205 of_node_put(np);
206}
207
208/*
209 * Misc functions related to the flipper chipset.
210 *
211 */
212
213/**
214 * flipper_quiesce() - quiesce flipper irq controller
215 *
216 * Mask and ack all interrupt sources.
217 *
218 */
219void flipper_quiesce(void)
220{
221 void __iomem *io_base = flipper_irq_host->host_data;
222
223 __flipper_quiesce(io_base);
224}
225
226/*
227 * Resets the platform.
228 */
229void flipper_platform_reset(void)
230{
231 void __iomem *io_base;
232
233 if (flipper_irq_host && flipper_irq_host->host_data) {
234 io_base = flipper_irq_host->host_data;
235 out_8(io_base + FLIPPER_RESET, 0x00);
236 }
237}
238
239/*
240 * Returns non-zero if the reset button is pressed.
241 */
242int flipper_is_reset_button_pressed(void)
243{
244 void __iomem *io_base;
245 u32 icr;
246
247 if (flipper_irq_host && flipper_irq_host->host_data) {
248 io_base = flipper_irq_host->host_data;
249 icr = in_be32(io_base + FLIPPER_ICR);
250 return !(icr & FLIPPER_ICR_RSS);
251 }
252 return 0;
253}
254