2 * Derived from arch/i386/kernel/irq.c
3 * Copyright (C) 1992 Linus Torvalds
4 * Adapted from arch/i386 by Gary Thomas
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 * Updated and modified by Cort Dougan <cort@fsmlabs.com>
7 * Copyright (C) 1996-2001 Cort Dougan
8 * Adapted for Power Macintosh by Paul Mackerras
9 * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
16 * This file contains the code used to make IRQ descriptions in the
17 * device tree to actual irq numbers on an interrupt controller
21 #include <linux/errno.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
25 #include <linux/of_irq.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
29 /* For archs that don't support NO_IRQ (such as x86), provide a dummy value */
35 * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
36 * @device: Device node of the device whose interrupt is to be mapped
37 * @index: Index of the interrupt to map
39 * This function is a wrapper that chains of_irq_map_one() and
40 * irq_create_of_mapping() to make things easier to callers
42 unsigned int irq_of_parse_and_map(struct device_node
*dev
, int index
)
46 if (of_irq_map_one(dev
, index
, &oirq
))
49 return irq_create_of_mapping(oirq
.controller
, oirq
.specifier
,
52 EXPORT_SYMBOL_GPL(irq_of_parse_and_map
);
55 * of_irq_find_parent - Given a device node, find its interrupt parent node
56 * @child: pointer to device node
58 * Returns a pointer to the interrupt parent node, or NULL if the interrupt
59 * parent could not be determined.
61 struct device_node
*of_irq_find_parent(struct device_node
*child
)
63 struct device_node
*p
, *c
= child
;
70 parp
= of_get_property(c
, "interrupt-parent", NULL
);
74 if (of_irq_workarounds
& OF_IMAP_NO_PHANDLE
)
75 p
= of_node_get(of_irq_dflt_pic
);
77 p
= of_find_node_by_phandle(be32_to_cpup(parp
));
81 } while (p
&& of_get_property(p
, "#interrupt-cells", NULL
) == NULL
);
83 return (p
== child
) ? NULL
: p
;
87 * of_irq_map_raw - Low level interrupt tree parsing
88 * @parent: the device interrupt parent
89 * @intspec: interrupt specifier ("interrupts" property of the device)
90 * @ointsize: size of the passed in interrupt specifier
91 * @addr: address specifier (start of "reg" property of the device)
92 * @out_irq: structure of_irq filled by this function
94 * Returns 0 on success and a negative number on error
96 * This function is a low-level interrupt tree walking function. It
97 * can be used to do a partial walk with synthetized reg and interrupts
98 * properties, for example when resolving PCI interrupts when no device
99 * node exist for the parent.
101 int of_irq_map_raw(struct device_node
*parent
, const __be32
*intspec
,
102 u32 ointsize
, const __be32
*addr
, struct of_irq
*out_irq
)
104 struct device_node
*ipar
, *tnode
, *old
= NULL
, *newpar
= NULL
;
105 const __be32
*tmp
, *imap
, *imask
;
106 u32 intsize
= 1, addrsize
, newintsize
= 0, newaddrsize
= 0;
107 int imaplen
, match
, i
;
109 pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
110 parent
->full_name
, be32_to_cpup(intspec
),
111 be32_to_cpup(intspec
+ 1), ointsize
);
113 ipar
= of_node_get(parent
);
115 /* First get the #interrupt-cells property of the current cursor
116 * that tells us how to interpret the passed-in intspec. If there
117 * is none, we are nice and just walk up the tree
120 tmp
= of_get_property(ipar
, "#interrupt-cells", NULL
);
122 intsize
= be32_to_cpu(*tmp
);
126 ipar
= of_irq_find_parent(ipar
);
130 pr_debug(" -> no parent found !\n");
134 pr_debug("of_irq_map_raw: ipar=%s, size=%d\n", ipar
->full_name
, intsize
);
136 if (ointsize
!= intsize
)
139 /* Look for this #address-cells. We have to implement the old linux
140 * trick of looking for the parent here as some device-trees rely on it
142 old
= of_node_get(ipar
);
144 tmp
= of_get_property(old
, "#address-cells", NULL
);
145 tnode
= of_get_parent(old
);
148 } while (old
&& tmp
== NULL
);
151 addrsize
= (tmp
== NULL
) ? 2 : be32_to_cpu(*tmp
);
153 pr_debug(" -> addrsize=%d\n", addrsize
);
155 /* Now start the actual "proper" walk of the interrupt tree */
156 while (ipar
!= NULL
) {
157 /* Now check if cursor is an interrupt-controller and if it is
160 if (of_get_property(ipar
, "interrupt-controller", NULL
) !=
162 pr_debug(" -> got it !\n");
163 for (i
= 0; i
< intsize
; i
++)
164 out_irq
->specifier
[i
] =
165 of_read_number(intspec
+i
, 1);
166 out_irq
->size
= intsize
;
167 out_irq
->controller
= ipar
;
172 /* Now look for an interrupt-map */
173 imap
= of_get_property(ipar
, "interrupt-map", &imaplen
);
174 /* No interrupt map, check for an interrupt parent */
176 pr_debug(" -> no map, getting parent\n");
177 newpar
= of_irq_find_parent(ipar
);
180 imaplen
/= sizeof(u32
);
182 /* Look for a mask */
183 imask
= of_get_property(ipar
, "interrupt-map-mask", NULL
);
185 /* If we were passed no "reg" property and we attempt to parse
186 * an interrupt-map, then #address-cells must be 0.
189 if (addr
== NULL
&& addrsize
!= 0) {
190 pr_debug(" -> no reg passed in when needed !\n");
194 /* Parse interrupt-map */
196 while (imaplen
> (addrsize
+ intsize
+ 1) && !match
) {
197 /* Compare specifiers */
199 for (i
= 0; i
< addrsize
&& match
; ++i
) {
200 u32 mask
= imask
? imask
[i
] : 0xffffffffu
;
201 match
= ((addr
[i
] ^ imap
[i
]) & mask
) == 0;
203 for (; i
< (addrsize
+ intsize
) && match
; ++i
) {
204 u32 mask
= imask
? imask
[i
] : 0xffffffffu
;
206 ((intspec
[i
-addrsize
] ^ imap
[i
]) & mask
) == 0;
208 imap
+= addrsize
+ intsize
;
209 imaplen
-= addrsize
+ intsize
;
211 pr_debug(" -> match=%d (imaplen=%d)\n", match
, imaplen
);
213 /* Get the interrupt parent */
214 if (of_irq_workarounds
& OF_IMAP_NO_PHANDLE
)
215 newpar
= of_node_get(of_irq_dflt_pic
);
217 newpar
= of_find_node_by_phandle(be32_to_cpup(imap
));
221 /* Check if not found */
222 if (newpar
== NULL
) {
223 pr_debug(" -> imap parent not found !\n");
227 /* Get #interrupt-cells and #address-cells of new
230 tmp
= of_get_property(newpar
, "#interrupt-cells", NULL
);
232 pr_debug(" -> parent lacks #interrupt-cells!\n");
235 newintsize
= be32_to_cpu(*tmp
);
236 tmp
= of_get_property(newpar
, "#address-cells", NULL
);
237 newaddrsize
= (tmp
== NULL
) ? 0 : be32_to_cpu(*tmp
);
239 pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
240 newintsize
, newaddrsize
);
242 /* Check for malformed properties */
243 if (imaplen
< (newaddrsize
+ newintsize
))
246 imap
+= newaddrsize
+ newintsize
;
247 imaplen
-= newaddrsize
+ newintsize
;
249 pr_debug(" -> imaplen=%d\n", imaplen
);
255 old
= of_node_get(newpar
);
256 addrsize
= newaddrsize
;
257 intsize
= newintsize
;
258 intspec
= imap
- intsize
;
259 addr
= intspec
- addrsize
;
262 /* Iterate again with new parent */
263 pr_debug(" -> new parent: %s\n", newpar
? newpar
->full_name
: "<>");
275 EXPORT_SYMBOL_GPL(of_irq_map_raw
);
278 * of_irq_map_one - Resolve an interrupt for a device
279 * @device: the device whose interrupt is to be resolved
280 * @index: index of the interrupt to resolve
281 * @out_irq: structure of_irq filled by this function
283 * This function resolves an interrupt, walking the tree, for a given
284 * device-tree node. It's the high level pendant to of_irq_map_raw().
286 int of_irq_map_one(struct device_node
*device
, int index
, struct of_irq
*out_irq
)
288 struct device_node
*p
;
289 const __be32
*intspec
, *tmp
, *addr
;
293 pr_debug("of_irq_map_one: dev=%s, index=%d\n", device
->full_name
, index
);
295 /* OldWorld mac stuff is "special", handle out of line */
296 if (of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)
297 return of_irq_map_oldworld(device
, index
, out_irq
);
299 /* Get the interrupts property */
300 intspec
= of_get_property(device
, "interrupts", &intlen
);
303 intlen
/= sizeof(*intspec
);
305 pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec
), intlen
);
307 /* Get the reg property (if any) */
308 addr
= of_get_property(device
, "reg", NULL
);
310 /* Look for the interrupt parent. */
311 p
= of_irq_find_parent(device
);
315 /* Get size of interrupt specifier */
316 tmp
= of_get_property(p
, "#interrupt-cells", NULL
);
319 intsize
= be32_to_cpu(*tmp
);
321 pr_debug(" intsize=%d intlen=%d\n", intsize
, intlen
);
324 if ((index
+ 1) * intsize
> intlen
)
327 /* Get new specifier and map it */
328 res
= of_irq_map_raw(p
, intspec
+ index
* intsize
, intsize
,
334 EXPORT_SYMBOL_GPL(of_irq_map_one
);
337 * of_irq_to_resource - Decode a node's IRQ and return it as a resource
338 * @dev: pointer to device tree node
339 * @index: zero-based index of the irq
340 * @r: pointer to resource structure to return result into.
342 int of_irq_to_resource(struct device_node
*dev
, int index
, struct resource
*r
)
344 int irq
= irq_of_parse_and_map(dev
, index
);
346 /* Only dereference the resource if both the
347 * resource and the irq are valid. */
348 if (r
&& irq
!= NO_IRQ
) {
349 r
->start
= r
->end
= irq
;
350 r
->flags
= IORESOURCE_IRQ
;
351 r
->name
= dev
->full_name
;
356 EXPORT_SYMBOL_GPL(of_irq_to_resource
);
359 * of_irq_count - Count the number of IRQs a node uses
360 * @dev: pointer to device tree node
362 int of_irq_count(struct device_node
*dev
)
366 while (of_irq_to_resource(dev
, nr
, NULL
) != NO_IRQ
)
373 * of_irq_to_resource_table - Fill in resource table with node's IRQ info
374 * @dev: pointer to device tree node
375 * @res: array of resources to fill in
376 * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
378 * Returns the size of the filled in table (up to @nr_irqs).
380 int of_irq_to_resource_table(struct device_node
*dev
, struct resource
*res
,
385 for (i
= 0; i
< nr_irqs
; i
++, res
++)
386 if (of_irq_to_resource(dev
, i
, res
) == NO_IRQ
)
393 struct list_head list
;
394 struct device_node
*dev
;
395 struct device_node
*interrupt_parent
;
399 * of_irq_init - Scan and init matching interrupt controllers in DT
400 * @matches: 0 terminated array of nodes to match and init function to call
402 * This function scans the device tree for matching interrupt controller nodes,
403 * and calls their initialization functions in order with parents first.
405 void __init
of_irq_init(const struct of_device_id
*matches
)
407 struct device_node
*np
, *parent
= NULL
;
408 struct intc_desc
*desc
, *temp_desc
;
409 struct list_head intc_desc_list
, intc_parent_list
;
411 INIT_LIST_HEAD(&intc_desc_list
);
412 INIT_LIST_HEAD(&intc_parent_list
);
414 for_each_matching_node(np
, matches
) {
415 if (!of_find_property(np
, "interrupt-controller", NULL
))
418 * Here, we allocate and populate an intc_desc with the node
419 * pointer, interrupt-parent device_node etc.
421 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
426 desc
->interrupt_parent
= of_irq_find_parent(np
);
427 list_add_tail(&desc
->list
, &intc_desc_list
);
431 * The root irq controller is the one without an interrupt-parent.
432 * That one goes first, followed by the controllers that reference it,
433 * followed by the ones that reference the 2nd level controllers, etc.
435 while (!list_empty(&intc_desc_list
)) {
437 * Process all controllers with the current 'parent'.
438 * First pass will be looking for NULL as the parent.
439 * The assumption is that NULL parent means a root controller.
441 list_for_each_entry_safe(desc
, temp_desc
, &intc_desc_list
, list
) {
442 const struct of_device_id
*match
;
444 of_irq_init_cb_t irq_init_cb
;
446 if (desc
->interrupt_parent
!= parent
)
449 list_del(&desc
->list
);
450 match
= of_match_node(matches
, desc
->dev
);
451 if (WARN(!match
->data
,
452 "of_irq_init: no init function for %s\n",
453 match
->compatible
)) {
458 pr_debug("of_irq_init: init %s @ %p, parent %p\n",
460 desc
->dev
, desc
->interrupt_parent
);
461 irq_init_cb
= match
->data
;
462 ret
= irq_init_cb(desc
->dev
, desc
->interrupt_parent
);
469 * This one is now set up; add it to the parent list so
470 * its children can get processed in a subsequent pass.
472 list_add_tail(&desc
->list
, &intc_parent_list
);
475 /* Get the next pending parent that might have children */
476 desc
= list_first_entry(&intc_parent_list
, typeof(*desc
), list
);
477 if (list_empty(&intc_parent_list
) || !desc
) {
478 pr_err("of_irq_init: children remain, but no parents\n");
481 list_del(&desc
->list
);
486 list_for_each_entry_safe(desc
, temp_desc
, &intc_parent_list
, list
) {
487 list_del(&desc
->list
);
491 list_for_each_entry_safe(desc
, temp_desc
, &intc_desc_list
, list
) {
492 list_del(&desc
->list
);