blob: bda0e5b8ce21cbacfed85716d9af55bc6cf7799e [file] [log] [blame]
Vitaly Bordugb0c110b2006-09-21 22:18:53 +04001/*
2 * Platform information definitions.
3 *
4 * Copied from arch/ppc/syslib/cpm2_pic.c with minor subsequent updates
5 * to make in work in arch/powerpc/. Original (c) belongs to Dan Malek.
6 *
7 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
8 *
9 * 1999-2001 (c) Dan Malek <dan@embeddedalley.com>
10 * 2006 (c) MontaVista Software, Inc.
11 *
12 * This file is licensed under the terms of the GNU General Public License
13 * version 2. This program is licensed "as is" without any warranty of any
14 * kind, whether express or implied.
15 */
16
17/* The CPM2 internal interrupt controller. It is usually
18 * the only interrupt controller.
19 * There are two 32-bit registers (high/low) for up to 64
20 * possible interrupts.
21 *
22 * Now, the fun starts.....Interrupt Numbers DO NOT MAP
23 * in a simple arithmetic fashion to mask or pending registers.
24 * That is, interrupt 4 does not map to bit position 4.
25 * We create two tables, indexed by vector number, to indicate
26 * which register to use and which bit in the register to use.
27 */
28
29#include <linux/stddef.h>
30#include <linux/init.h>
31#include <linux/sched.h>
32#include <linux/signal.h>
33#include <linux/irq.h>
34
35#include <asm/immap_cpm2.h>
36#include <asm/mpc8260.h>
37#include <asm/io.h>
38#include <asm/prom.h>
Vitaly Bordug73844ec2007-01-31 02:08:54 +030039#include <asm/fs_pd.h>
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040040
41#include "cpm2_pic.h"
42
Vitaly Bordug73844ec2007-01-31 02:08:54 +030043/* External IRQS */
44#define CPM2_IRQ_EXT1 19
45#define CPM2_IRQ_EXT7 25
46
47/* Port C IRQS */
48#define CPM2_IRQ_PORTC15 48
49#define CPM2_IRQ_PORTC0 63
50
Scott Wood449012d2007-09-14 15:30:44 -050051static intctl_cpm2_t __iomem *cpm2_intctl;
Vitaly Bordug73844ec2007-01-31 02:08:54 +030052
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040053static struct irq_host *cpm2_pic_host;
54#define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
55static unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
56
57static const u_char irq_to_siureg[] = {
58 1, 1, 1, 1, 1, 1, 1, 1,
59 1, 1, 1, 1, 1, 1, 1, 1,
60 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0,
62 1, 1, 1, 1, 1, 1, 1, 1,
63 1, 1, 1, 1, 1, 1, 1, 1,
64 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0
66};
67
68/* bit numbers do not match the docs, these are precomputed so the bit for
69 * a given irq is (1 << irq_to_siubit[irq]) */
70static const u_char irq_to_siubit[] = {
71 0, 15, 14, 13, 12, 11, 10, 9,
72 8, 7, 6, 5, 4, 3, 2, 1,
73 2, 1, 0, 14, 13, 12, 11, 10,
74 9, 8, 7, 6, 5, 4, 3, 0,
75 31, 30, 29, 28, 27, 26, 25, 24,
76 23, 22, 21, 20, 19, 18, 17, 16,
77 16, 17, 18, 19, 20, 21, 22, 23,
78 24, 25, 26, 27, 28, 29, 30, 31,
79};
80
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +000081static void cpm2_mask_irq(struct irq_data *d)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040082{
83 int bit, word;
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +000084 unsigned int irq_nr = virq_to_hw(d->irq);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040085
86 bit = irq_to_siubit[irq_nr];
87 word = irq_to_siureg[irq_nr];
88
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040089 ppc_cached_irq_mask[word] &= ~(1 << bit);
Vitaly Bordug73844ec2007-01-31 02:08:54 +030090 out_be32(&cpm2_intctl->ic_simrh + word, ppc_cached_irq_mask[word]);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040091}
92
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +000093static void cpm2_unmask_irq(struct irq_data *d)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040094{
95 int bit, word;
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +000096 unsigned int irq_nr = virq_to_hw(d->irq);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040097
98 bit = irq_to_siubit[irq_nr];
99 word = irq_to_siureg[irq_nr];
100
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400101 ppc_cached_irq_mask[word] |= 1 << bit;
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300102 out_be32(&cpm2_intctl->ic_simrh + word, ppc_cached_irq_mask[word]);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400103}
104
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +0000105static void cpm2_ack(struct irq_data *d)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400106{
107 int bit, word;
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +0000108 unsigned int irq_nr = virq_to_hw(d->irq);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400109
110 bit = irq_to_siubit[irq_nr];
111 word = irq_to_siureg[irq_nr];
112
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300113 out_be32(&cpm2_intctl->ic_sipnrh + word, 1 << bit);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400114}
115
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +0000116static void cpm2_end_irq(struct irq_data *d)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400117{
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000118 struct irq_desc *desc;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400119 int bit, word;
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +0000120 unsigned int irq_nr = virq_to_hw(d->irq);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400121
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000122 desc = irq_to_desc(irq_nr);
123 if (!(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS))
124 && desc->action) {
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400125
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400126 bit = irq_to_siubit[irq_nr];
127 word = irq_to_siureg[irq_nr];
128
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400129 ppc_cached_irq_mask[word] |= 1 << bit;
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300130 out_be32(&cpm2_intctl->ic_simrh + word, ppc_cached_irq_mask[word]);
131
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400132 /*
133 * Work around large numbers of spurious IRQs on PowerPC 82xx
134 * systems.
135 */
136 mb();
137 }
138}
139
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +0000140static int cpm2_set_irq_type(struct irq_data *d, unsigned int flow_type)
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300141{
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +0000142 unsigned int src = virq_to_hw(d->irq);
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300143 unsigned int vold, vnew, edibit;
144
Mark Wareb22b97c2009-12-10 22:14:34 +1100145 /* Port C interrupts are either IRQ_TYPE_EDGE_FALLING or
146 * IRQ_TYPE_EDGE_BOTH (default). All others are IRQ_TYPE_EDGE_FALLING
147 * or IRQ_TYPE_LEVEL_LOW (default)
148 */
149 if (src >= CPM2_IRQ_PORTC15 && src <= CPM2_IRQ_PORTC0) {
150 if (flow_type == IRQ_TYPE_NONE)
151 flow_type = IRQ_TYPE_EDGE_BOTH;
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300152
Mark Wareb22b97c2009-12-10 22:14:34 +1100153 if (flow_type != IRQ_TYPE_EDGE_BOTH &&
154 flow_type != IRQ_TYPE_EDGE_FALLING)
155 goto err_sense;
156 } else {
157 if (flow_type == IRQ_TYPE_NONE)
158 flow_type = IRQ_TYPE_LEVEL_LOW;
159
160 if (flow_type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH))
161 goto err_sense;
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300162 }
163
Thomas Gleixnera28ab382011-03-25 16:07:51 +0100164 irqd_set_trigger_type(d, flow_type);
165 if (flow_type & IRQ_TYPE_LEVEL_LOW)
166 __set_irq_handler_unlocked(d->irq, handle_level_irq);
167 else
168 __set_irq_handler_unlocked(d->irq, handle_edge_irq);
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300169
170 /* internal IRQ senses are LEVEL_LOW
171 * EXT IRQ and Port C IRQ senses are programmable
172 */
173 if (src >= CPM2_IRQ_EXT1 && src <= CPM2_IRQ_EXT7)
174 edibit = (14 - (src - CPM2_IRQ_EXT1));
175 else
176 if (src >= CPM2_IRQ_PORTC15 && src <= CPM2_IRQ_PORTC0)
paulfax7f3ea172009-01-27 02:44:07 -0600177 edibit = (31 - (CPM2_IRQ_PORTC0 - src));
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300178 else
Thomas Gleixnera28ab382011-03-25 16:07:51 +0100179 return (flow_type & IRQ_TYPE_LEVEL_LOW) ?
180 IRQ_SET_MASK_OK_NOCOPY : -EINVAL;
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300181
182 vold = in_be32(&cpm2_intctl->ic_siexr);
183
184 if ((flow_type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_FALLING)
185 vnew = vold | (1 << edibit);
186 else
187 vnew = vold & ~(1 << edibit);
188
189 if (vold != vnew)
190 out_be32(&cpm2_intctl->ic_siexr, vnew);
Thomas Gleixnera28ab382011-03-25 16:07:51 +0100191 return IRQ_SET_MASK_OK_NOCOPY;
Mark Wareb22b97c2009-12-10 22:14:34 +1100192
193err_sense:
194 pr_err("CPM2 PIC: sense type 0x%x not supported\n", flow_type);
195 return -EINVAL;
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300196}
197
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400198static struct irq_chip cpm2_pic = {
Anton Blanchardfc380c02010-01-31 20:33:41 +0000199 .name = "CPM2 SIU",
Lennert Buytenhekc47eefa2011-03-07 13:59:51 +0000200 .irq_mask = cpm2_mask_irq,
201 .irq_unmask = cpm2_unmask_irq,
202 .irq_ack = cpm2_ack,
203 .irq_eoi = cpm2_end_irq,
204 .irq_set_type = cpm2_set_irq_type,
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400205};
206
Olaf Hering35a84c22006-10-07 22:08:26 +1000207unsigned int cpm2_get_irq(void)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400208{
209 int irq;
210 unsigned long bits;
211
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400212 /* For CPM2, read the SIVEC register and shift the bits down
213 * to get the irq number. */
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300214 bits = in_be32(&cpm2_intctl->ic_sivec);
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400215 irq = bits >> 26;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400216
217 if (irq == 0)
218 return(-1);
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300219 return irq_linear_revmap(cpm2_pic_host, irq);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400220}
221
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400222static int cpm2_pic_host_map(struct irq_host *h, unsigned int virq,
223 irq_hw_number_t hw)
224{
225 pr_debug("cpm2_pic_host_map(%d, 0x%lx)\n", virq, hw);
226
Thomas Gleixner98488db2011-03-25 15:43:57 +0100227 irq_set_status_flags(virq, IRQ_LEVEL);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400228 set_irq_chip_and_handler(virq, &cpm2_pic, handle_level_irq);
229 return 0;
230}
231
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400232static int cpm2_pic_host_xlate(struct irq_host *h, struct device_node *ct,
Roman Fietze40d50cf2009-12-08 02:39:50 +0000233 const u32 *intspec, unsigned int intsize,
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400234 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
235{
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400236 *out_hwirq = intspec[0];
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300237 if (intsize > 1)
238 *out_flags = intspec[1];
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400239 else
240 *out_flags = IRQ_TYPE_NONE;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400241 return 0;
242}
243
244static struct irq_host_ops cpm2_pic_host_ops = {
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400245 .map = cpm2_pic_host_map,
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400246 .xlate = cpm2_pic_host_xlate,
247};
248
249void cpm2_pic_init(struct device_node *node)
250{
251 int i;
252
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300253 cpm2_intctl = cpm2_map(im_intctl);
254
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400255 /* Clear the CPM IRQ controller, in case it has any bits set
256 * from the bootloader
257 */
258
259 /* Mask out everything */
260
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300261 out_be32(&cpm2_intctl->ic_simrh, 0x00000000);
262 out_be32(&cpm2_intctl->ic_simrl, 0x00000000);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400263
264 wmb();
265
266 /* Ack everything */
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300267 out_be32(&cpm2_intctl->ic_sipnrh, 0xffffffff);
268 out_be32(&cpm2_intctl->ic_sipnrl, 0xffffffff);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400269 wmb();
270
271 /* Dummy read of the vector */
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300272 i = in_be32(&cpm2_intctl->ic_sivec);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400273 rmb();
274
275 /* Initialize the default interrupt mapping priorities,
276 * in case the boot rom changed something on us.
277 */
Vitaly Bordug73844ec2007-01-31 02:08:54 +0300278 out_be16(&cpm2_intctl->ic_sicr, 0);
279 out_be32(&cpm2_intctl->ic_scprrh, 0x05309770);
280 out_be32(&cpm2_intctl->ic_scprrl, 0x05309770);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400281
282 /* create a legacy host */
Michael Ellerman19fc65b2008-05-26 12:12:32 +1000283 cpm2_pic_host = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR,
Michael Ellerman52964f82007-08-28 18:47:54 +1000284 64, &cpm2_pic_host_ops, 64);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400285 if (cpm2_pic_host == NULL) {
286 printk(KERN_ERR "CPM2 PIC: failed to allocate irq host!\n");
287 return;
288 }
289}