1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for C64x+ Megamodule Interrupt Controller
5 * Copyright (C) 2010, 2011 Texas Instruments Incorporated
6 * Contributed by: Mark Salter <msalter@redhat.com>
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
12 #include <linux/of_irq.h>
13 #include <linux/of_address.h>
14 #include <linux/slab.h>
16 #include <asm/megamod-pic.h>
18 #define NR_COMBINERS 4
19 #define NR_MUX_OUTPUTS 12
21 #define IRQ_UNMAPPED 0xffff
24 * Megamodule Interrupt Controller register layout
48 struct irq_domain
*irqhost
;
49 struct megamod_regs __iomem
*regs
;
53 unsigned int output_to_irq
[NR_MUX_OUTPUTS
];
56 static struct megamod_pic
*mm_pic
;
58 struct megamod_cascade_data
{
59 struct megamod_pic
*pic
;
63 static struct megamod_cascade_data cascade_data
[NR_COMBINERS
];
65 static void mask_megamod(struct irq_data
*data
)
67 struct megamod_pic
*pic
= irq_data_get_irq_chip_data(data
);
68 irq_hw_number_t src
= irqd_to_hwirq(data
);
69 u32 __iomem
*evtmask
= &pic
->regs
->evtmask
[src
/ 32];
71 raw_spin_lock(&pic
->lock
);
72 soc_writel(soc_readl(evtmask
) | (1 << (src
& 31)), evtmask
);
73 raw_spin_unlock(&pic
->lock
);
76 static void unmask_megamod(struct irq_data
*data
)
78 struct megamod_pic
*pic
= irq_data_get_irq_chip_data(data
);
79 irq_hw_number_t src
= irqd_to_hwirq(data
);
80 u32 __iomem
*evtmask
= &pic
->regs
->evtmask
[src
/ 32];
82 raw_spin_lock(&pic
->lock
);
83 soc_writel(soc_readl(evtmask
) & ~(1 << (src
& 31)), evtmask
);
84 raw_spin_unlock(&pic
->lock
);
87 static struct irq_chip megamod_chip
= {
89 .irq_mask
= mask_megamod
,
90 .irq_unmask
= unmask_megamod
,
93 static void megamod_irq_cascade(struct irq_desc
*desc
)
95 struct megamod_cascade_data
*cascade
;
96 struct megamod_pic
*pic
;
101 cascade
= irq_desc_get_handler_data(desc
);
104 idx
= cascade
->index
;
106 while ((events
= soc_readl(&pic
->regs
->mevtflag
[idx
])) != 0) {
109 irq
= irq_linear_revmap(pic
->irqhost
, idx
* 32 + n
);
111 soc_writel(1 << n
, &pic
->regs
->evtclr
[idx
]);
113 generic_handle_irq(irq
);
117 static int megamod_map(struct irq_domain
*h
, unsigned int virq
,
120 struct megamod_pic
*pic
= h
->host_data
;
123 /* We shouldn't see a hwirq which is muxed to core controller */
124 for (i
= 0; i
< NR_MUX_OUTPUTS
; i
++)
125 if (pic
->output_to_irq
[i
] == hw
)
128 irq_set_chip_data(virq
, pic
);
129 irq_set_chip_and_handler(virq
, &megamod_chip
, handle_level_irq
);
131 /* Set default irq type */
132 irq_set_irq_type(virq
, IRQ_TYPE_NONE
);
137 static const struct irq_domain_ops megamod_domain_ops
= {
139 .xlate
= irq_domain_xlate_onecell
,
142 static void __init
set_megamod_mux(struct megamod_pic
*pic
, int src
, int output
)
147 if (src
< 0 || src
>= (NR_COMBINERS
* 32)) {
148 pic
->output_to_irq
[output
] = IRQ_UNMAPPED
;
152 /* four mappings per mux register */
154 offset
= (output
& 3) * 8;
156 val
= soc_readl(&pic
->regs
->intmux
[index
]);
157 val
&= ~(0xff << offset
);
158 val
|= src
<< offset
;
159 soc_writel(val
, &pic
->regs
->intmux
[index
]);
163 * Parse the MUX mapping, if one exists.
165 * The MUX map is an array of up to 12 cells; one for each usable core priority
166 * interrupt. The value of a given cell is the megamodule interrupt source
167 * which is to me MUXed to the output corresponding to the cell position
168 * withing the array. The first cell in the array corresponds to priority
169 * 4 and the last (12th) cell corresponds to priority 15. The allowed
170 * values are 4 - ((NR_COMBINERS * 32) - 1). Note that the combined interrupt
171 * sources (0 - 3) are not allowed to be mapped through this property. They
172 * are handled through the "interrupts" property. This allows us to use a
173 * value of zero as a "do not map" placeholder.
175 static void __init
parse_priority_map(struct megamod_pic
*pic
,
176 int *mapping
, int size
)
178 struct device_node
*np
= irq_domain_get_of_node(pic
->irqhost
);
183 map
= of_get_property(np
, "ti,c64x+megamod-pic-mux", &maplen
);
189 for (i
= 0; i
< maplen
; i
++) {
190 val
= be32_to_cpup(map
);
198 static struct megamod_pic
* __init
init_megamod_pic(struct device_node
*np
)
200 struct megamod_pic
*pic
;
202 int mapping
[NR_MUX_OUTPUTS
];
204 pr_info("Initializing C64x+ Megamodule PIC\n");
206 pic
= kzalloc(sizeof(struct megamod_pic
), GFP_KERNEL
);
208 pr_err("%pOF: Could not alloc PIC structure.\n", np
);
212 pic
->irqhost
= irq_domain_add_linear(np
, NR_COMBINERS
* 32,
213 &megamod_domain_ops
, pic
);
215 pr_err("%pOF: Could not alloc host.\n", np
);
219 pic
->irqhost
->host_data
= pic
;
221 raw_spin_lock_init(&pic
->lock
);
223 pic
->regs
= of_iomap(np
, 0);
225 pr_err("%pOF: Could not map registers.\n", np
);
229 /* Initialize MUX map */
230 for (i
= 0; i
< ARRAY_SIZE(mapping
); i
++)
231 mapping
[i
] = IRQ_UNMAPPED
;
233 parse_priority_map(pic
, mapping
, ARRAY_SIZE(mapping
));
236 * We can have up to 12 interrupts cascading to the core controller.
237 * These cascades can be from the combined interrupt sources or for
238 * individual interrupt sources. The "interrupts" property only
239 * deals with the cascaded combined interrupts. The individual
240 * interrupts muxed to the core controller use the core controller
241 * as their interrupt parent.
243 for (i
= 0; i
< NR_COMBINERS
; i
++) {
244 struct irq_data
*irq_data
;
245 irq_hw_number_t hwirq
;
247 irq
= irq_of_parse_and_map(np
, i
);
251 irq_data
= irq_get_irq_data(irq
);
253 pr_err("%pOF: combiner-%d no irq_data for virq %d!\n",
258 hwirq
= irq_data
->hwirq
;
261 * Check that device tree provided something in the range
262 * of the core priority interrupts (4 - 15).
264 if (hwirq
< 4 || hwirq
>= NR_PRIORITY_IRQS
) {
265 pr_err("%pOF: combiner-%d core irq %ld out of range!\n",
270 /* record the mapping */
271 mapping
[hwirq
- 4] = i
;
273 pr_debug("%pOF: combiner-%d cascading to hwirq %ld\n",
276 cascade_data
[i
].pic
= pic
;
277 cascade_data
[i
].index
= i
;
279 /* mask and clear all events in combiner */
280 soc_writel(~0, &pic
->regs
->evtmask
[i
]);
281 soc_writel(~0, &pic
->regs
->evtclr
[i
]);
283 irq_set_chained_handler_and_data(irq
, megamod_irq_cascade
,
287 /* Finally, set up the MUX registers */
288 for (i
= 0; i
< NR_MUX_OUTPUTS
; i
++) {
289 if (mapping
[i
] != IRQ_UNMAPPED
) {
290 pr_debug("%pOF: setting mux %d to priority %d\n",
291 np
, mapping
[i
], i
+ 4);
292 set_megamod_mux(pic
, mapping
[i
], i
);
305 * Return next active event after ACK'ing it.
306 * Return -1 if no events active.
308 static int get_exception(void)
313 for (i
= 0; i
< NR_COMBINERS
; i
++) {
314 mask
= soc_readl(&mm_pic
->regs
->mexpflag
[i
]);
317 soc_writel(1 << bit
, &mm_pic
->regs
->evtclr
[i
]);
318 return (i
* 32) + bit
;
324 static void assert_event(unsigned int val
)
326 soc_writel(val
, &mm_pic
->regs
->evtasrt
);
329 void __init
megamod_pic_init(void)
331 struct device_node
*np
;
333 np
= of_find_compatible_node(NULL
, NULL
, "ti,c64x+megamod-pic");
337 mm_pic
= init_megamod_pic(np
);
340 soc_ops
.get_exception
= get_exception
;
341 soc_ops
.assert_event
= assert_event
;