1/*
2 * Interrupt management for most GSC and related devices.
3 *
4 * (c) Copyright 1999 Alex deVries for The Puffin Group
5 * (c) Copyright 1999 Grant Grundler for Hewlett-Packard
6 * (c) Copyright 1999 Matthew Wilcox
7 * (c) Copyright 2000 Helge Deller
8 * (c) Copyright 2001 Matthew Wilcox for Hewlett-Packard
9 *
10 *	This program is free software; you can redistribute it and/or modify
11 *	it under the terms of the GNU General Public License as published by
12 *      the Free Software Foundation; either version 2 of the License, or
13 *      (at your option) any later version.
14 */
15
16#include <linux/bitops.h>
17#include <linux/errno.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/ioport.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/types.h>
24
25#include <asm/hardware.h>
26#include <asm/io.h>
27
28#include "gsc.h"
29
30#undef DEBUG
31
32#ifdef DEBUG
33#define DEBPRINTK printk
34#else
35#define DEBPRINTK(x,...)
36#endif
37
38int gsc_alloc_irq(struct gsc_irq *i)
39{
40	int irq = txn_alloc_irq(GSC_EIM_WIDTH);
41	if (irq < 0) {
42		printk("cannot get irq\n");
43		return irq;
44	}
45
46	i->txn_addr = txn_alloc_addr(irq);
47	i->txn_data = txn_alloc_data(irq);
48	i->irq = irq;
49
50	return irq;
51}
52
53int gsc_claim_irq(struct gsc_irq *i, int irq)
54{
55	int c = irq;
56
57	irq += CPU_IRQ_BASE; /* virtualize the IRQ first */
58
59	irq = txn_claim_irq(irq);
60	if (irq < 0) {
61		printk("cannot claim irq %d\n", c);
62		return irq;
63	}
64
65	i->txn_addr = txn_alloc_addr(irq);
66	i->txn_data = txn_alloc_data(irq);
67	i->irq = irq;
68
69	return irq;
70}
71
72EXPORT_SYMBOL(gsc_alloc_irq);
73EXPORT_SYMBOL(gsc_claim_irq);
74
75/* Common interrupt demultiplexer used by Asp, Lasi & Wax.  */
76irqreturn_t gsc_asic_intr(int gsc_asic_irq, void *dev)
77{
78	unsigned long irr;
79	struct gsc_asic *gsc_asic = dev;
80
81	irr = gsc_readl(gsc_asic->hpa + OFFSET_IRR);
82	if (irr == 0)
83		return IRQ_NONE;
84
85	DEBPRINTK("%s intr, mask=0x%x\n", gsc_asic->name, irr);
86
87	do {
88		int local_irq = __ffs(irr);
89		unsigned int irq = gsc_asic->global_irq[local_irq];
90		__do_IRQ(irq);
91		irr &= ~(1 << local_irq);
92	} while (irr);
93
94	return IRQ_HANDLED;
95}
96
97int gsc_find_local_irq(unsigned int irq, int *global_irqs, int limit)
98{
99	int local_irq;
100
101	for (local_irq = 0; local_irq < limit; local_irq++) {
102		if (global_irqs[local_irq] == irq)
103			return local_irq;
104	}
105
106	return NO_IRQ;
107}
108
109static void gsc_asic_disable_irq(unsigned int irq)
110{
111	struct gsc_asic *irq_dev = irq_desc[irq].chip_data;
112	int local_irq = gsc_find_local_irq(irq, irq_dev->global_irq, 32);
113	u32 imr;
114
115	DEBPRINTK(KERN_DEBUG "%s(%d) %s: IMR 0x%x\n", __FUNCTION__, irq,
116			irq_dev->name, imr);
117
118	/* Disable the IRQ line by clearing the bit in the IMR */
119	imr = gsc_readl(irq_dev->hpa + OFFSET_IMR);
120	imr &= ~(1 << local_irq);
121	gsc_writel(imr, irq_dev->hpa + OFFSET_IMR);
122}
123
124static void gsc_asic_enable_irq(unsigned int irq)
125{
126	struct gsc_asic *irq_dev = irq_desc[irq].chip_data;
127	int local_irq = gsc_find_local_irq(irq, irq_dev->global_irq, 32);
128	u32 imr;
129
130	DEBPRINTK(KERN_DEBUG "%s(%d) %s: IMR 0x%x\n", __FUNCTION__, irq,
131			irq_dev->name, imr);
132
133	/* Enable the IRQ line by setting the bit in the IMR */
134	imr = gsc_readl(irq_dev->hpa + OFFSET_IMR);
135	imr |= 1 << local_irq;
136	gsc_writel(imr, irq_dev->hpa + OFFSET_IMR);
137}
138
139static unsigned int gsc_asic_startup_irq(unsigned int irq)
140{
141	gsc_asic_enable_irq(irq);
142	return 0;
143}
144
145static struct hw_interrupt_type gsc_asic_interrupt_type = {
146	.typename =	"GSC-ASIC",
147	.startup =	gsc_asic_startup_irq,
148	.shutdown =	gsc_asic_disable_irq,
149	.enable =	gsc_asic_enable_irq,
150	.disable =	gsc_asic_disable_irq,
151	.ack =		no_ack_irq,
152	.end =		no_end_irq,
153};
154
155int gsc_assign_irq(struct hw_interrupt_type *type, void *data)
156{
157	static int irq = GSC_IRQ_BASE;
158
159	if (irq > GSC_IRQ_MAX)
160		return NO_IRQ;
161
162	irq_desc[irq].chip = type;
163	irq_desc[irq].chip_data = data;
164	return irq++;
165}
166
167void gsc_asic_assign_irq(struct gsc_asic *asic, int local_irq, int *irqp)
168{
169	int irq = asic->global_irq[local_irq];
170
171	if (irq <= 0) {
172		irq = gsc_assign_irq(&gsc_asic_interrupt_type, asic);
173		if (irq == NO_IRQ)
174			return;
175
176		asic->global_irq[local_irq] = irq;
177	}
178	*irqp = irq;
179}
180
181static struct device *next_device(struct klist_iter *i)
182{
183	struct klist_node * n = klist_next(i);
184	return n ? container_of(n, struct device, knode_parent) : NULL;
185}
186
187void gsc_fixup_irqs(struct parisc_device *parent, void *ctrl,
188			void (*choose_irq)(struct parisc_device *, void *))
189{
190	struct device *dev;
191	struct klist_iter i;
192
193	klist_iter_init(&parent->dev.klist_children, &i);
194	while ((dev = next_device(&i))) {
195		struct parisc_device *padev = to_parisc_device(dev);
196
197		if (padev->id.hw_type == HPHW_FAULTY)
198			return gsc_fixup_irqs(padev, ctrl, choose_irq);
199		choose_irq(padev, ctrl);
200	}
201	klist_iter_exit(&i);
202}
203
204int gsc_common_setup(struct parisc_device *parent, struct gsc_asic *gsc_asic)
205{
206	struct resource *res;
207	int i;
208
209	gsc_asic->gsc = parent;
210
211	/* Initialise local irq -> global irq mapping */
212	for (i = 0; i < 32; i++) {
213		gsc_asic->global_irq[i] = NO_IRQ;
214	}
215
216	/* allocate resource region */
217	res = request_mem_region(gsc_asic->hpa, 0x100000, gsc_asic->name);
218	if (res) {
219		res->flags = IORESOURCE_MEM; 	/* do not mark it busy ! */
220	}
221
222
223	return 0;
224}
225
226extern struct parisc_driver lasi_driver;
227extern struct parisc_driver asp_driver;
228extern struct parisc_driver wax_driver;
229
230void __init gsc_init(void)
231{
232#ifdef CONFIG_GSC_LASI
233	register_parisc_driver(&lasi_driver);
234	register_parisc_driver(&asp_driver);
235#endif
236#ifdef CONFIG_GSC_WAX
237	register_parisc_driver(&wax_driver);
238#endif
239}
240