blob: 15396333a90bd50e4ab8304a6d9645cd6ede6d72 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Albert Herranz9c210252009-12-12 06:31:47 +00002/*
3 * arch/powerpc/platforms/embedded6xx/hlwd-pic.c
4 *
5 * Nintendo Wii "Hollywood" interrupt controller support.
6 * Copyright (C) 2009 The GameCube Linux Team
7 * Copyright (C) 2009 Albert Herranz
Albert Herranz9c210252009-12-12 06:31:47 +00008 */
9#define DRV_MODULE_NAME "hlwd-pic"
10#define pr_fmt(fmt) DRV_MODULE_NAME ": " fmt
11
12#include <linux/kernel.h>
Albert Herranz9c210252009-12-12 06:31:47 +000013#include <linux/irq.h>
14#include <linux/of.h>
Rob Herring26a20562013-09-26 07:40:04 -050015#include <linux/of_address.h>
16#include <linux/of_irq.h>
Albert Herranz9c210252009-12-12 06:31:47 +000017#include <asm/io.h>
18
19#include "hlwd-pic.h"
20
21#define HLWD_NR_IRQS 32
22
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 asserting/clearing
28 * the corresponding bit in IMR. ACK'ing a request simply involves
29 * asserting the corresponding bit in ICR.
30 */
31#define HW_BROADWAY_ICR 0x00
32#define HW_BROADWAY_IMR 0x04
Jonathan Neuschäfer9dcb3df2018-05-10 23:59:19 +020033#define HW_STARLET_ICR 0x08
34#define HW_STARLET_IMR 0x0c
Albert Herranz9c210252009-12-12 06:31:47 +000035
36
37/*
38 * IRQ chip hooks.
39 *
40 */
41
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000042static void hlwd_pic_mask_and_ack(struct irq_data *d)
Albert Herranz9c210252009-12-12 06:31:47 +000043{
Grant Likely476eb492011-05-04 15:02:15 +100044 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000045 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz9c210252009-12-12 06:31:47 +000046 u32 mask = 1 << irq;
47
48 clrbits32(io_base + HW_BROADWAY_IMR, mask);
49 out_be32(io_base + HW_BROADWAY_ICR, mask);
50}
51
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000052static void hlwd_pic_ack(struct irq_data *d)
Albert Herranz9c210252009-12-12 06:31:47 +000053{
Grant Likely476eb492011-05-04 15:02:15 +100054 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000055 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz9c210252009-12-12 06:31:47 +000056
57 out_be32(io_base + HW_BROADWAY_ICR, 1 << irq);
58}
59
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000060static void hlwd_pic_mask(struct irq_data *d)
Albert Herranz9c210252009-12-12 06:31:47 +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 Herranz9c210252009-12-12 06:31:47 +000064
65 clrbits32(io_base + HW_BROADWAY_IMR, 1 << irq);
66}
67
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000068static void hlwd_pic_unmask(struct irq_data *d)
Albert Herranz9c210252009-12-12 06:31:47 +000069{
Grant Likely476eb492011-05-04 15:02:15 +100070 int irq = irqd_to_hwirq(d);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000071 void __iomem *io_base = irq_data_get_irq_chip_data(d);
Albert Herranz9c210252009-12-12 06:31:47 +000072
73 setbits32(io_base + HW_BROADWAY_IMR, 1 << irq);
Jonathan Neuschäfer9dcb3df2018-05-10 23:59:19 +020074
75 /* Make sure the ARM (aka. Starlet) doesn't handle this interrupt. */
76 clrbits32(io_base + HW_STARLET_IMR, 1 << irq);
Albert Herranz9c210252009-12-12 06:31:47 +000077}
78
79
80static struct irq_chip hlwd_pic = {
81 .name = "hlwd-pic",
Lennert Buytenhek0bf88782011-03-08 22:26:53 +000082 .irq_ack = hlwd_pic_ack,
83 .irq_mask_ack = hlwd_pic_mask_and_ack,
84 .irq_mask = hlwd_pic_mask,
85 .irq_unmask = hlwd_pic_unmask,
Albert Herranz9c210252009-12-12 06:31:47 +000086};
87
88/*
89 * IRQ host hooks.
90 *
91 */
92
Grant Likelybae1d8f2012-02-14 14:06:50 -070093static struct irq_domain *hlwd_irq_host;
Albert Herranz9c210252009-12-12 06:31:47 +000094
Grant Likelybae1d8f2012-02-14 14:06:50 -070095static int hlwd_pic_map(struct irq_domain *h, unsigned int virq,
Albert Herranz9c210252009-12-12 06:31:47 +000096 irq_hw_number_t hwirq)
97{
Thomas Gleixnerec775d02011-03-25 16:45:20 +010098 irq_set_chip_data(virq, h->host_data);
Thomas Gleixner98488db2011-03-25 15:43:57 +010099 irq_set_status_flags(virq, IRQ_LEVEL);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100100 irq_set_chip_and_handler(virq, &hlwd_pic, handle_level_irq);
Albert Herranz9c210252009-12-12 06:31:47 +0000101 return 0;
102}
103
Grant Likely9f70b8e2012-01-26 12:24:34 -0700104static const struct irq_domain_ops hlwd_irq_domain_ops = {
Albert Herranz9c210252009-12-12 06:31:47 +0000105 .map = hlwd_pic_map,
Albert Herranz9c210252009-12-12 06:31:47 +0000106};
107
Grant Likelybae1d8f2012-02-14 14:06:50 -0700108static unsigned int __hlwd_pic_get_irq(struct irq_domain *h)
Albert Herranz9c210252009-12-12 06:31:47 +0000109{
110 void __iomem *io_base = h->host_data;
Albert Herranz9c210252009-12-12 06:31:47 +0000111 u32 irq_status;
112
113 irq_status = in_be32(io_base + HW_BROADWAY_ICR) &
114 in_be32(io_base + HW_BROADWAY_IMR);
115 if (irq_status == 0)
Michael Ellermanef24ba72016-09-06 21:53:24 +1000116 return 0; /* no more IRQs pending */
Albert Herranz9c210252009-12-12 06:31:47 +0000117
Marc Zyngier2c899652021-05-04 17:42:18 +0100118 return __ffs(irq_status);
Albert Herranz9c210252009-12-12 06:31:47 +0000119}
120
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200121static void hlwd_pic_irq_cascade(struct irq_desc *desc)
Albert Herranz9c210252009-12-12 06:31:47 +0000122{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100123 struct irq_chip *chip = irq_desc_get_chip(desc);
Jiang Liuc1231a72015-05-20 17:59:52 +0800124 struct irq_domain *irq_domain = irq_desc_get_handler_data(desc);
Marc Zyngier2c899652021-05-04 17:42:18 +0100125 unsigned int hwirq;
Albert Herranz9c210252009-12-12 06:31:47 +0000126
Albert Herranz7ccec3e2009-12-18 10:04:42 +0000127 raw_spin_lock(&desc->lock);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +0000128 chip->irq_mask(&desc->irq_data); /* IRQ_LEVEL */
Albert Herranz7ccec3e2009-12-18 10:04:42 +0000129 raw_spin_unlock(&desc->lock);
Albert Herranz9c210252009-12-12 06:31:47 +0000130
Marc Zyngier2c899652021-05-04 17:42:18 +0100131 hwirq = __hlwd_pic_get_irq(irq_domain);
132 if (hwirq)
133 generic_handle_domain_irq(irq_domain, hwirq);
Albert Herranz9c210252009-12-12 06:31:47 +0000134 else
135 pr_err("spurious interrupt!\n");
136
Albert Herranz7ccec3e2009-12-18 10:04:42 +0000137 raw_spin_lock(&desc->lock);
Lennert Buytenhek0bf88782011-03-08 22:26:53 +0000138 chip->irq_ack(&desc->irq_data); /* IRQ_LEVEL */
Thomas Gleixner98488db2011-03-25 15:43:57 +0100139 if (!irqd_irq_disabled(&desc->irq_data) && chip->irq_unmask)
Lennert Buytenhek0bf88782011-03-08 22:26:53 +0000140 chip->irq_unmask(&desc->irq_data);
Albert Herranz7ccec3e2009-12-18 10:04:42 +0000141 raw_spin_unlock(&desc->lock);
Albert Herranz9c210252009-12-12 06:31:47 +0000142}
143
144/*
145 * Platform hooks.
146 *
147 */
148
149static void __hlwd_quiesce(void __iomem *io_base)
150{
151 /* mask and ack all IRQs */
152 out_be32(io_base + HW_BROADWAY_IMR, 0);
153 out_be32(io_base + HW_BROADWAY_ICR, 0xffffffff);
154}
155
Mathieu Malaterreeed69642018-04-23 21:45:32 +0200156static struct irq_domain *hlwd_pic_init(struct device_node *np)
Albert Herranz9c210252009-12-12 06:31:47 +0000157{
Grant Likelybae1d8f2012-02-14 14:06:50 -0700158 struct irq_domain *irq_domain;
Albert Herranz9c210252009-12-12 06:31:47 +0000159 struct resource res;
160 void __iomem *io_base;
161 int retval;
162
163 retval = of_address_to_resource(np, 0, &res);
164 if (retval) {
165 pr_err("no io memory range found\n");
166 return NULL;
167 }
168 io_base = ioremap(res.start, resource_size(&res));
169 if (!io_base) {
170 pr_err("ioremap failed\n");
171 return NULL;
172 }
173
174 pr_info("controller at 0x%08x mapped to 0x%p\n", res.start, io_base);
175
176 __hlwd_quiesce(io_base);
177
Grant Likelya8db8cf2012-02-14 14:06:54 -0700178 irq_domain = irq_domain_add_linear(np, HLWD_NR_IRQS,
179 &hlwd_irq_domain_ops, io_base);
Grant Likelybae1d8f2012-02-14 14:06:50 -0700180 if (!irq_domain) {
181 pr_err("failed to allocate irq_domain\n");
Wei Yongjun8d7c0b52013-10-12 15:13:19 +0800182 iounmap(io_base);
Albert Herranz9c210252009-12-12 06:31:47 +0000183 return NULL;
184 }
Albert Herranz9c210252009-12-12 06:31:47 +0000185
Grant Likelybae1d8f2012-02-14 14:06:50 -0700186 return irq_domain;
Albert Herranz9c210252009-12-12 06:31:47 +0000187}
188
189unsigned int hlwd_pic_get_irq(void)
190{
Marc Zyngier2c899652021-05-04 17:42:18 +0100191 unsigned int hwirq = __hlwd_pic_get_irq(hlwd_irq_host);
192 return hwirq ? irq_linear_revmap(hlwd_irq_host, hwirq) : 0;
Albert Herranz9c210252009-12-12 06:31:47 +0000193}
194
195/*
196 * Probe function.
197 *
198 */
199
200void hlwd_pic_probe(void)
201{
Grant Likelybae1d8f2012-02-14 14:06:50 -0700202 struct irq_domain *host;
Albert Herranz9c210252009-12-12 06:31:47 +0000203 struct device_node *np;
204 const u32 *interrupts;
205 int cascade_virq;
206
207 for_each_compatible_node(np, NULL, "nintendo,hollywood-pic") {
208 interrupts = of_get_property(np, "interrupts", NULL);
209 if (interrupts) {
210 host = hlwd_pic_init(np);
211 BUG_ON(!host);
212 cascade_virq = irq_of_parse_and_map(np, 0);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100213 irq_set_handler_data(cascade_virq, host);
214 irq_set_chained_handler(cascade_virq,
Albert Herranz9c210252009-12-12 06:31:47 +0000215 hlwd_pic_irq_cascade);
Paul Gortmaker6d166fe2012-02-22 18:35:03 -0500216 hlwd_irq_host = host;
Albert Herranz9c210252009-12-12 06:31:47 +0000217 break;
218 }
219 }
220}
221
222/**
223 * hlwd_quiesce() - quiesce hollywood irq controller
224 *
225 * Mask and ack all interrupt sources.
226 *
227 */
228void hlwd_quiesce(void)
229{
Paul Gortmaker6d166fe2012-02-22 18:35:03 -0500230 void __iomem *io_base = hlwd_irq_host->host_data;
Albert Herranz9c210252009-12-12 06:31:47 +0000231
232 __hlwd_quiesce(io_base);
233}
234