Merge branch 'lpc32xx/dts' of git://git.antcom.de/linux-2.6 into next/dt
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / arm / mach-lpc32xx / irq.c
1 /*
2  * arch/arm/mach-lpc32xx/irq.c
3  *
4  * Author: Kevin Wells <kevin.wells@nxp.com>
5  *
6  * Copyright (C) 2010 NXP Semiconductors
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/err.h>
24 #include <linux/io.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/irqdomain.h>
29 #include <linux/module.h>
30
31 #include <mach/irqs.h>
32 #include <mach/hardware.h>
33 #include <mach/platform.h>
34 #include "common.h"
35
36 /*
37  * Default value representing the Activation polarity of all internal
38  * interrupt sources
39  */
40 #define MIC_APR_DEFAULT         0x3FF0EFE0
41 #define SIC1_APR_DEFAULT        0xFBD27186
42 #define SIC2_APR_DEFAULT        0x801810C0
43
44 /*
45  * Default value representing the Activation Type of all internal
46  * interrupt sources. All are level sensitive.
47  */
48 #define MIC_ATR_DEFAULT         0x00000000
49 #define SIC1_ATR_DEFAULT        0x00026000
50 #define SIC2_ATR_DEFAULT        0x00000000
51
52 static struct irq_domain *lpc32xx_mic_domain;
53 static struct device_node *lpc32xx_mic_np;
54
55 struct lpc32xx_event_group_regs {
56         void __iomem *enab_reg;
57         void __iomem *edge_reg;
58         void __iomem *maskstat_reg;
59         void __iomem *rawstat_reg;
60 };
61
62 static const struct lpc32xx_event_group_regs lpc32xx_event_int_regs = {
63         .enab_reg = LPC32XX_CLKPWR_INT_ER,
64         .edge_reg = LPC32XX_CLKPWR_INT_AP,
65         .maskstat_reg = LPC32XX_CLKPWR_INT_SR,
66         .rawstat_reg = LPC32XX_CLKPWR_INT_RS,
67 };
68
69 static const struct lpc32xx_event_group_regs lpc32xx_event_pin_regs = {
70         .enab_reg = LPC32XX_CLKPWR_PIN_ER,
71         .edge_reg = LPC32XX_CLKPWR_PIN_AP,
72         .maskstat_reg = LPC32XX_CLKPWR_PIN_SR,
73         .rawstat_reg = LPC32XX_CLKPWR_PIN_RS,
74 };
75
76 struct lpc32xx_event_info {
77         const struct lpc32xx_event_group_regs *event_group;
78         u32 mask;
79 };
80
81 /*
82  * Maps an IRQ number to and event mask and register
83  */
84 static const struct lpc32xx_event_info lpc32xx_events[NR_IRQS] = {
85         [IRQ_LPC32XX_GPI_08] = {
86                 .event_group = &lpc32xx_event_pin_regs,
87                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_08_BIT,
88         },
89         [IRQ_LPC32XX_GPI_09] = {
90                 .event_group = &lpc32xx_event_pin_regs,
91                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_09_BIT,
92         },
93         [IRQ_LPC32XX_GPI_19] = {
94                 .event_group = &lpc32xx_event_pin_regs,
95                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_19_BIT,
96         },
97         [IRQ_LPC32XX_GPI_07] = {
98                 .event_group = &lpc32xx_event_pin_regs,
99                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_07_BIT,
100         },
101         [IRQ_LPC32XX_GPI_00] = {
102                 .event_group = &lpc32xx_event_pin_regs,
103                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_00_BIT,
104         },
105         [IRQ_LPC32XX_GPI_01] = {
106                 .event_group = &lpc32xx_event_pin_regs,
107                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_01_BIT,
108         },
109         [IRQ_LPC32XX_GPI_02] = {
110                 .event_group = &lpc32xx_event_pin_regs,
111                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_02_BIT,
112         },
113         [IRQ_LPC32XX_GPI_03] = {
114                 .event_group = &lpc32xx_event_pin_regs,
115                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_03_BIT,
116         },
117         [IRQ_LPC32XX_GPI_04] = {
118                 .event_group = &lpc32xx_event_pin_regs,
119                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_04_BIT,
120         },
121         [IRQ_LPC32XX_GPI_05] = {
122                 .event_group = &lpc32xx_event_pin_regs,
123                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_05_BIT,
124         },
125         [IRQ_LPC32XX_GPI_06] = {
126                 .event_group = &lpc32xx_event_pin_regs,
127                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_06_BIT,
128         },
129         [IRQ_LPC32XX_GPI_28] = {
130                 .event_group = &lpc32xx_event_pin_regs,
131                 .mask = LPC32XX_CLKPWR_EXTSRC_GPI_28_BIT,
132         },
133         [IRQ_LPC32XX_GPIO_00] = {
134                 .event_group = &lpc32xx_event_int_regs,
135                 .mask = LPC32XX_CLKPWR_INTSRC_GPIO_00_BIT,
136         },
137         [IRQ_LPC32XX_GPIO_01] = {
138                 .event_group = &lpc32xx_event_int_regs,
139                 .mask = LPC32XX_CLKPWR_INTSRC_GPIO_01_BIT,
140         },
141         [IRQ_LPC32XX_GPIO_02] = {
142                 .event_group = &lpc32xx_event_int_regs,
143                 .mask = LPC32XX_CLKPWR_INTSRC_GPIO_02_BIT,
144         },
145         [IRQ_LPC32XX_GPIO_03] = {
146                 .event_group = &lpc32xx_event_int_regs,
147                 .mask = LPC32XX_CLKPWR_INTSRC_GPIO_03_BIT,
148         },
149         [IRQ_LPC32XX_GPIO_04] = {
150                 .event_group = &lpc32xx_event_int_regs,
151                 .mask = LPC32XX_CLKPWR_INTSRC_GPIO_04_BIT,
152         },
153         [IRQ_LPC32XX_GPIO_05] = {
154                 .event_group = &lpc32xx_event_int_regs,
155                 .mask = LPC32XX_CLKPWR_INTSRC_GPIO_05_BIT,
156         },
157         [IRQ_LPC32XX_KEY] = {
158                 .event_group = &lpc32xx_event_int_regs,
159                 .mask = LPC32XX_CLKPWR_INTSRC_KEY_BIT,
160         },
161         [IRQ_LPC32XX_ETHERNET] = {
162                 .event_group = &lpc32xx_event_int_regs,
163                 .mask = LPC32XX_CLKPWR_INTSRC_MAC_BIT,
164         },
165         [IRQ_LPC32XX_USB_OTG_ATX] = {
166                 .event_group = &lpc32xx_event_int_regs,
167                 .mask = LPC32XX_CLKPWR_INTSRC_USBATXINT_BIT,
168         },
169         [IRQ_LPC32XX_USB_HOST] = {
170                 .event_group = &lpc32xx_event_int_regs,
171                 .mask = LPC32XX_CLKPWR_INTSRC_USB_BIT,
172         },
173         [IRQ_LPC32XX_RTC] = {
174                 .event_group = &lpc32xx_event_int_regs,
175                 .mask = LPC32XX_CLKPWR_INTSRC_RTC_BIT,
176         },
177         [IRQ_LPC32XX_MSTIMER] = {
178                 .event_group = &lpc32xx_event_int_regs,
179                 .mask = LPC32XX_CLKPWR_INTSRC_MSTIMER_BIT,
180         },
181         [IRQ_LPC32XX_TS_AUX] = {
182                 .event_group = &lpc32xx_event_int_regs,
183                 .mask = LPC32XX_CLKPWR_INTSRC_TS_AUX_BIT,
184         },
185         [IRQ_LPC32XX_TS_P] = {
186                 .event_group = &lpc32xx_event_int_regs,
187                 .mask = LPC32XX_CLKPWR_INTSRC_TS_P_BIT,
188         },
189         [IRQ_LPC32XX_TS_IRQ] = {
190                 .event_group = &lpc32xx_event_int_regs,
191                 .mask = LPC32XX_CLKPWR_INTSRC_ADC_BIT,
192         },
193 };
194
195 static void get_controller(unsigned int irq, unsigned int *base,
196         unsigned int *irqbit)
197 {
198         if (irq < 32) {
199                 *base = LPC32XX_MIC_BASE;
200                 *irqbit = 1 << irq;
201         } else if (irq < 64) {
202                 *base = LPC32XX_SIC1_BASE;
203                 *irqbit = 1 << (irq - 32);
204         } else {
205                 *base = LPC32XX_SIC2_BASE;
206                 *irqbit = 1 << (irq - 64);
207         }
208 }
209
210 static void lpc32xx_mask_irq(struct irq_data *d)
211 {
212         unsigned int reg, ctrl, mask;
213
214         get_controller(d->hwirq, &ctrl, &mask);
215
216         reg = __raw_readl(LPC32XX_INTC_MASK(ctrl)) & ~mask;
217         __raw_writel(reg, LPC32XX_INTC_MASK(ctrl));
218 }
219
220 static void lpc32xx_unmask_irq(struct irq_data *d)
221 {
222         unsigned int reg, ctrl, mask;
223
224         get_controller(d->hwirq, &ctrl, &mask);
225
226         reg = __raw_readl(LPC32XX_INTC_MASK(ctrl)) | mask;
227         __raw_writel(reg, LPC32XX_INTC_MASK(ctrl));
228 }
229
230 static void lpc32xx_ack_irq(struct irq_data *d)
231 {
232         unsigned int ctrl, mask;
233
234         get_controller(d->hwirq, &ctrl, &mask);
235
236         __raw_writel(mask, LPC32XX_INTC_RAW_STAT(ctrl));
237
238         /* Also need to clear pending wake event */
239         if (lpc32xx_events[d->hwirq].mask != 0)
240                 __raw_writel(lpc32xx_events[d->hwirq].mask,
241                         lpc32xx_events[d->hwirq].event_group->rawstat_reg);
242 }
243
244 static void __lpc32xx_set_irq_type(unsigned int irq, int use_high_level,
245         int use_edge)
246 {
247         unsigned int reg, ctrl, mask;
248
249         get_controller(irq, &ctrl, &mask);
250
251         /* Activation level, high or low */
252         reg = __raw_readl(LPC32XX_INTC_POLAR(ctrl));
253         if (use_high_level)
254                 reg |= mask;
255         else
256                 reg &= ~mask;
257         __raw_writel(reg, LPC32XX_INTC_POLAR(ctrl));
258
259         /* Activation type, edge or level */
260         reg = __raw_readl(LPC32XX_INTC_ACT_TYPE(ctrl));
261         if (use_edge)
262                 reg |= mask;
263         else
264                 reg &= ~mask;
265         __raw_writel(reg, LPC32XX_INTC_ACT_TYPE(ctrl));
266
267         /* Use same polarity for the wake events */
268         if (lpc32xx_events[irq].mask != 0) {
269                 reg = __raw_readl(lpc32xx_events[irq].event_group->edge_reg);
270
271                 if (use_high_level)
272                         reg |= lpc32xx_events[irq].mask;
273                 else
274                         reg &= ~lpc32xx_events[irq].mask;
275
276                 __raw_writel(reg, lpc32xx_events[irq].event_group->edge_reg);
277         }
278 }
279
280 static int lpc32xx_set_irq_type(struct irq_data *d, unsigned int type)
281 {
282         switch (type) {
283         case IRQ_TYPE_EDGE_RISING:
284                 /* Rising edge sensitive */
285                 __lpc32xx_set_irq_type(d->hwirq, 1, 1);
286                 break;
287
288         case IRQ_TYPE_EDGE_FALLING:
289                 /* Falling edge sensitive */
290                 __lpc32xx_set_irq_type(d->hwirq, 0, 1);
291                 break;
292
293         case IRQ_TYPE_LEVEL_LOW:
294                 /* Low level sensitive */
295                 __lpc32xx_set_irq_type(d->hwirq, 0, 0);
296                 break;
297
298         case IRQ_TYPE_LEVEL_HIGH:
299                 /* High level sensitive */
300                 __lpc32xx_set_irq_type(d->hwirq, 1, 0);
301                 break;
302
303         /* Other modes are not supported */
304         default:
305                 return -EINVAL;
306         }
307
308         /* Ok to use the level handler for all types */
309         irq_set_handler(d->hwirq, handle_level_irq);
310
311         return 0;
312 }
313
314 static int lpc32xx_irq_wake(struct irq_data *d, unsigned int state)
315 {
316         unsigned long eventreg;
317
318         if (lpc32xx_events[d->hwirq].mask != 0) {
319                 eventreg = __raw_readl(lpc32xx_events[d->hwirq].
320                         event_group->enab_reg);
321
322                 if (state)
323                         eventreg |= lpc32xx_events[d->hwirq].mask;
324                 else {
325                         eventreg &= ~lpc32xx_events[d->hwirq].mask;
326
327                         /*
328                          * When disabling the wakeup, clear the latched
329                          * event
330                          */
331                         __raw_writel(lpc32xx_events[d->hwirq].mask,
332                                 lpc32xx_events[d->hwirq].
333                                 event_group->rawstat_reg);
334                 }
335
336                 __raw_writel(eventreg,
337                         lpc32xx_events[d->hwirq].event_group->enab_reg);
338
339                 return 0;
340         }
341
342         /* Clear event */
343         __raw_writel(lpc32xx_events[d->hwirq].mask,
344                 lpc32xx_events[d->hwirq].event_group->rawstat_reg);
345
346         return -ENODEV;
347 }
348
349 static void __init lpc32xx_set_default_mappings(unsigned int apr,
350         unsigned int atr, unsigned int offset)
351 {
352         unsigned int i;
353
354         /* Set activation levels for each interrupt */
355         i = 0;
356         while (i < 32) {
357                 __lpc32xx_set_irq_type(offset + i, ((apr >> i) & 0x1),
358                         ((atr >> i) & 0x1));
359                 i++;
360         }
361 }
362
363 static struct irq_chip lpc32xx_irq_chip = {
364         .name = "MIC",
365         .irq_ack = lpc32xx_ack_irq,
366         .irq_mask = lpc32xx_mask_irq,
367         .irq_unmask = lpc32xx_unmask_irq,
368         .irq_set_type = lpc32xx_set_irq_type,
369         .irq_set_wake = lpc32xx_irq_wake
370 };
371
372 static void lpc32xx_sic1_handler(unsigned int irq, struct irq_desc *desc)
373 {
374         unsigned long ints = __raw_readl(LPC32XX_INTC_STAT(LPC32XX_SIC1_BASE));
375
376         while (ints != 0) {
377                 int irqno = fls(ints) - 1;
378
379                 ints &= ~(1 << irqno);
380
381                 generic_handle_irq(LPC32XX_SIC1_IRQ(irqno));
382         }
383 }
384
385 static void lpc32xx_sic2_handler(unsigned int irq, struct irq_desc *desc)
386 {
387         unsigned long ints = __raw_readl(LPC32XX_INTC_STAT(LPC32XX_SIC2_BASE));
388
389         while (ints != 0) {
390                 int irqno = fls(ints) - 1;
391
392                 ints &= ~(1 << irqno);
393
394                 generic_handle_irq(LPC32XX_SIC2_IRQ(irqno));
395         }
396 }
397
398 static int __init __lpc32xx_mic_of_init(struct device_node *node,
399                                         struct device_node *parent)
400 {
401         lpc32xx_mic_np = node;
402
403         return 0;
404 }
405
406 static const struct of_device_id mic_of_match[] __initconst = {
407         { .compatible = "nxp,lpc3220-mic", .data = __lpc32xx_mic_of_init },
408         { }
409 };
410
411 void __init lpc32xx_init_irq(void)
412 {
413         unsigned int i;
414         int irq_base;
415
416         /* Setup MIC */
417         __raw_writel(0, LPC32XX_INTC_MASK(LPC32XX_MIC_BASE));
418         __raw_writel(MIC_APR_DEFAULT, LPC32XX_INTC_POLAR(LPC32XX_MIC_BASE));
419         __raw_writel(MIC_ATR_DEFAULT, LPC32XX_INTC_ACT_TYPE(LPC32XX_MIC_BASE));
420
421         /* Setup SIC1 */
422         __raw_writel(0, LPC32XX_INTC_MASK(LPC32XX_SIC1_BASE));
423         __raw_writel(SIC1_APR_DEFAULT, LPC32XX_INTC_POLAR(LPC32XX_SIC1_BASE));
424         __raw_writel(SIC1_ATR_DEFAULT,
425                                 LPC32XX_INTC_ACT_TYPE(LPC32XX_SIC1_BASE));
426
427         /* Setup SIC2 */
428         __raw_writel(0, LPC32XX_INTC_MASK(LPC32XX_SIC2_BASE));
429         __raw_writel(SIC2_APR_DEFAULT, LPC32XX_INTC_POLAR(LPC32XX_SIC2_BASE));
430         __raw_writel(SIC2_ATR_DEFAULT,
431                                 LPC32XX_INTC_ACT_TYPE(LPC32XX_SIC2_BASE));
432
433         /* Configure supported IRQ's */
434         for (i = 0; i < NR_IRQS; i++) {
435                 irq_set_chip_and_handler(i, &lpc32xx_irq_chip,
436                                          handle_level_irq);
437                 set_irq_flags(i, IRQF_VALID);
438         }
439
440         /* Set default mappings */
441         lpc32xx_set_default_mappings(MIC_APR_DEFAULT, MIC_ATR_DEFAULT, 0);
442         lpc32xx_set_default_mappings(SIC1_APR_DEFAULT, SIC1_ATR_DEFAULT, 32);
443         lpc32xx_set_default_mappings(SIC2_APR_DEFAULT, SIC2_ATR_DEFAULT, 64);
444
445         /* mask all interrupts except SUBIRQ */
446         __raw_writel(0, LPC32XX_INTC_MASK(LPC32XX_MIC_BASE));
447         __raw_writel(0, LPC32XX_INTC_MASK(LPC32XX_SIC1_BASE));
448         __raw_writel(0, LPC32XX_INTC_MASK(LPC32XX_SIC2_BASE));
449
450         /* MIC SUBIRQx interrupts will route handling to the chain handlers */
451         irq_set_chained_handler(IRQ_LPC32XX_SUB1IRQ, lpc32xx_sic1_handler);
452         irq_set_chained_handler(IRQ_LPC32XX_SUB2IRQ, lpc32xx_sic2_handler);
453
454         /* Initially disable all wake events */
455         __raw_writel(0, LPC32XX_CLKPWR_P01_ER);
456         __raw_writel(0, LPC32XX_CLKPWR_INT_ER);
457         __raw_writel(0, LPC32XX_CLKPWR_PIN_ER);
458
459         /*
460          * Default wake activation polarities, all pin sources are low edge
461          * triggered
462          */
463         __raw_writel(LPC32XX_CLKPWR_INTSRC_TS_P_BIT |
464                 LPC32XX_CLKPWR_INTSRC_MSTIMER_BIT |
465                 LPC32XX_CLKPWR_INTSRC_RTC_BIT,
466                 LPC32XX_CLKPWR_INT_AP);
467         __raw_writel(0, LPC32XX_CLKPWR_PIN_AP);
468
469         /* Clear latched wake event states */
470         __raw_writel(__raw_readl(LPC32XX_CLKPWR_PIN_RS),
471                 LPC32XX_CLKPWR_PIN_RS);
472         __raw_writel(__raw_readl(LPC32XX_CLKPWR_INT_RS),
473                 LPC32XX_CLKPWR_INT_RS);
474
475         of_irq_init(mic_of_match);
476
477         irq_base = irq_alloc_descs(-1, 0, NR_IRQS, 0);
478         if (irq_base < 0) {
479                 pr_warn("Cannot allocate irq_descs, assuming pre-allocated\n");
480                 irq_base = 0;
481         }
482
483         lpc32xx_mic_domain = irq_domain_add_legacy(lpc32xx_mic_np, NR_IRQS,
484                                                    irq_base, 0,
485                                                    &irq_domain_simple_ops,
486                                                    NULL);
487         if (!lpc32xx_mic_domain)
488                 panic("Unable to add MIC irq domain\n");
489 }