summaryrefslogtreecommitdiff
path: root/target/linux/atheros/files/arch/mips/atheros/ar5315/irq.c
blob: 621c7fc0b84d0caab37b50dd2a7517bb6fe3c5e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2003 Atheros Communications, Inc.,  All Rights Reserved.
 * Copyright (C) 2006 FON Technology, SL.
 * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
 */

/*
 * Platform devices for Atheros SoCs
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/reboot.h>
#include <linux/interrupt.h>
#include <asm/bootinfo.h>
#include <asm/irq_cpu.h>
#include <asm/io.h>

#include <ar531x.h>
#include <gpio.h>

static u32 gpiointmask = 0, gpiointval = 0;

static inline void ar5315_gpio_irq(void)
{
	u32 pend;
	sysRegWrite(AR5315_ISR, sysRegRead(AR5315_IMR) | ~AR5315_ISR_GPIO);

	/* only do one gpio interrupt at a time */
	pend = (sysRegRead(AR5315_GPIO_DI) ^ gpiointval) & gpiointmask;
	if (!pend)
		return;

	do_IRQ(AR531X_GPIO_IRQ_BASE + 31 - clz(pend));
}


/*
 * Called when an interrupt is received, this function
 * determines exactly which interrupt it was, and it
 * invokes the appropriate handler.
 *
 * Implicitly, we also define interrupt priority by
 * choosing which to dispatch first.
 */
asmlinkage void ar5315_irq_dispatch(void)
{
	int pending = read_c0_status() & read_c0_cause();

	if (pending & CAUSEF_IP3)
		do_IRQ(AR5315_IRQ_WLAN0_INTRS);
	else if (pending & CAUSEF_IP4)
		do_IRQ(AR5315_IRQ_ENET0_INTRS);
	else if (pending & CAUSEF_IP2) {
		unsigned int ar531x_misc_intrs = sysRegRead(AR5315_ISR) & sysRegRead(AR5315_IMR);

		if (ar531x_misc_intrs & AR5315_ISR_SPI)
			do_IRQ(AR531X_MISC_IRQ_SPI);
		else if (ar531x_misc_intrs & AR5315_ISR_TIMER)
			do_IRQ(AR531X_MISC_IRQ_TIMER);
		else if (ar531x_misc_intrs & AR5315_ISR_AHB)
			do_IRQ(AR531X_MISC_IRQ_AHB_PROC);
		else if (ar531x_misc_intrs & AR5315_ISR_GPIO)
			ar5315_gpio_irq();
		else if (ar531x_misc_intrs & AR5315_ISR_UART0)
			do_IRQ(AR531X_MISC_IRQ_UART0);
		else if (ar531x_misc_intrs & AR5315_ISR_WD)
			do_IRQ(AR531X_MISC_IRQ_WATCHDOG);
		else
			do_IRQ(AR531X_MISC_IRQ_NONE);
	} else if (pending & CAUSEF_IP7)
		do_IRQ(AR531X_IRQ_CPU_CLOCK);
}

static void ar5315_gpio_intr_enable(unsigned int irq)
{
	u32 gpio, mask;
	gpio = irq - AR531X_GPIO_IRQ_BASE;
	mask = 1 << gpio;
	gpiointmask |= mask;

	/* reconfigure GPIO line as input */
	sysRegMask(AR5315_GPIO_CR, AR5315_GPIO_CR_M(gpio), AR5315_GPIO_CR_I(gpio));
	
	/* Enable interrupt with edge detection */
	sysRegMask(AR5315_GPIO_INT, AR5315_GPIO_INT_M | AR5315_GPIO_INT_LVL_M, gpio | AR5315_GPIO_INT_LVL(3));
}

static void ar5315_gpio_intr_disable(unsigned int irq)
{
	u32 gpio, mask;
	gpio = irq - AR531X_GPIO_IRQ_BASE;
	mask = 1 << gpio;

	gpiointmask &= ~mask;

	/* Disable interrupt with edge detection */
	sysRegMask(AR5315_GPIO_INT, AR5315_GPIO_INT_M | AR5315_GPIO_INT_LVL_M, gpio | AR5315_GPIO_INT_LVL(0));
}

/* Turn on the specified AR531X_MISC_IRQ interrupt */
static unsigned int ar5315_gpio_intr_startup(unsigned int irq)
{
	ar5315_gpio_intr_enable(irq);
	return 0;
}

/* Turn off the specified AR531X_MISC_IRQ interrupt */
static void
ar5315_gpio_intr_shutdown(unsigned int irq)
{
	ar5315_gpio_intr_disable(irq);
}

static void
ar5315_gpio_intr_ack(unsigned int irq)
{
	ar5315_gpio_intr_disable(irq);
}

static void
ar5315_gpio_intr_end(unsigned int irq)
{
	if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
		ar5315_gpio_intr_enable(irq);
}

static struct irq_chip ar5315_gpio_intr_controller = {
	.typename	= "AR5315 GPIO",
	.startup	= ar5315_gpio_intr_startup,
	.shutdown	= ar5315_gpio_intr_shutdown,
	.enable		= ar5315_gpio_intr_enable,
	.disable	= ar5315_gpio_intr_disable,
	.ack		= ar5315_gpio_intr_ack,
	.end		= ar5315_gpio_intr_end,
};


/* Enable the specified AR531X_MISC_IRQ interrupt */
static void
ar5315_misc_intr_enable(unsigned int irq)
{
	unsigned int imr;

	imr = sysRegRead(AR5315_IMR);
	switch(irq)
	{
	   case AR531X_MISC_IRQ_SPI:
		 imr |= AR5315_ISR_SPI;
		 break;

	   case AR531X_MISC_IRQ_TIMER:
	     imr |= AR5315_ISR_TIMER;
	     break;

	   case AR531X_MISC_IRQ_AHB_PROC:
	     imr |= AR5315_ISR_AHB;
	     break;

	   case AR531X_MISC_IRQ_AHB_DMA:
	     imr |= 0/* ?? */;
	     break;

	   case	AR531X_MISC_IRQ_GPIO:
	     imr |= AR5315_ISR_GPIO;
	     break;

	   case AR531X_MISC_IRQ_UART0:
	     imr |= AR5315_ISR_UART0;
	     break;


	   case	AR531X_MISC_IRQ_WATCHDOG:
	     imr |= AR5315_ISR_WD;
	     break;

	   case AR531X_MISC_IRQ_LOCAL:
	     imr |= 0/* ?? */;
	     break;

	}
	sysRegWrite(AR5315_IMR, imr);
	imr=sysRegRead(AR5315_IMR); /* flush write buffer */
}

/* Disable the specified AR531X_MISC_IRQ interrupt */
static void
ar5315_misc_intr_disable(unsigned int irq)
{
	unsigned int imr;

	imr = sysRegRead(AR5315_IMR);
	switch(irq)
	{
	   case AR531X_MISC_IRQ_SPI:
		 imr &= ~AR5315_ISR_SPI;
		 break;
		 
	   case AR531X_MISC_IRQ_TIMER:
	     imr &= (~AR5315_ISR_TIMER);
	     break;

	   case AR531X_MISC_IRQ_AHB_PROC:
	     imr &= (~AR5315_ISR_AHB);
	     break;

	   case AR531X_MISC_IRQ_AHB_DMA:
	     imr &= 0/* ?? */;
	     break;

	   case	AR531X_MISC_IRQ_GPIO:
	     imr &= ~AR5315_ISR_GPIO;
	     break;

	   case AR531X_MISC_IRQ_UART0:
	     imr &= (~AR5315_ISR_UART0);
	     break;

	   case	AR531X_MISC_IRQ_WATCHDOG:
	     imr &= (~AR5315_ISR_WD);
	     break;

	   case AR531X_MISC_IRQ_LOCAL:
	     imr &= ~0/* ?? */;
	     break;

	}
	sysRegWrite(AR5315_IMR, imr);
	sysRegRead(AR5315_IMR); /* flush write buffer */
}

/* Turn on the specified AR531X_MISC_IRQ interrupt */
static unsigned int
ar5315_misc_intr_startup(unsigned int irq)
{
	ar5315_misc_intr_enable(irq);
	return 0;
}

/* Turn off the specified AR531X_MISC_IRQ interrupt */
static void
ar5315_misc_intr_shutdown(unsigned int irq)
{
	ar5315_misc_intr_disable(irq);
}

static void
ar5315_misc_intr_ack(unsigned int irq)
{
	ar5315_misc_intr_disable(irq);
}

static void
ar5315_misc_intr_end(unsigned int irq)
{
	if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
		ar5315_misc_intr_enable(irq);
}

static struct irq_chip ar5315_misc_intr_controller = {
	.typename	= "AR5315 misc",
	.startup	= ar5315_misc_intr_startup,
	.shutdown	= ar5315_misc_intr_shutdown,
	.enable		= ar5315_misc_intr_enable,
	.disable	= ar5315_misc_intr_disable,
	.ack		= ar5315_misc_intr_ack,
	.end		= ar5315_misc_intr_end,
};

static irqreturn_t ar5315_ahb_proc_handler(int cpl, void *dev_id)
{
    sysRegWrite(AR5315_AHB_ERR0,AHB_ERROR_DET);
    sysRegRead(AR5315_AHB_ERR1);

    printk("AHB fatal error\n");
    machine_restart("AHB error"); /* Catastrophic failure */

    return IRQ_HANDLED;
}

static struct irqaction ar5315_ahb_proc_interrupt  = {
	.handler	= ar5315_ahb_proc_handler,
	.flags		= IRQF_DISABLED,
	.name		= "ar5315_ahb_proc_interrupt",
};


static struct irqaction cascade  = {
	.handler	= no_action,
	.flags		= IRQF_DISABLED,
	.name		= "cascade",
};

static void ar5315_gpio_intr_init(int irq_base)
{
	int i;

	for (i = irq_base; i < irq_base + AR531X_GPIO_IRQ_COUNT; i++) {
		irq_desc[i].status = IRQ_DISABLED;
		irq_desc[i].action = NULL;
		irq_desc[i].depth = 1;
		irq_desc[i].chip = &ar5315_gpio_intr_controller;
	}
	setup_irq(AR531X_MISC_IRQ_GPIO, &cascade);
	gpiointval = sysRegRead(AR5315_GPIO_DI);
}

void ar5315_misc_intr_init(int irq_base)
{
	int i;

	for (i = irq_base; i < irq_base + AR531X_MISC_IRQ_COUNT; i++) {
		irq_desc[i].status = IRQ_DISABLED;
		irq_desc[i].action = NULL;
		irq_desc[i].depth = 1;
		irq_desc[i].chip = &ar5315_misc_intr_controller;
	}
	setup_irq(AR531X_MISC_IRQ_AHB_PROC, &ar5315_ahb_proc_interrupt);
	setup_irq(AR5315_IRQ_MISC_INTRS, &cascade);
	ar5315_gpio_intr_init(AR531X_GPIO_IRQ_BASE);
}