blob: 6e595e5a39770b912e05feccb4c4f9a85c0742a4 [file] [log] [blame]
Grant Likelye3873442010-06-18 11:09:59 -06001/*
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)
10 *
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.
15 *
16 * This file contains the code used to make IRQ descriptions in the
17 * device tree to actual irq numbers on an interrupt controller
18 * driver.
19 */
20
21#include <linux/errno.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/of_irq.h>
25#include <linux/string.h>
26
27/**
28 * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
29 * @device: Device node of the device whose interrupt is to be mapped
30 * @index: Index of the interrupt to map
31 *
32 * This function is a wrapper that chains of_irq_map_one() and
33 * irq_create_of_mapping() to make things easier to callers
34 */
35unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
36{
37 struct of_irq oirq;
38
39 if (of_irq_map_one(dev, index, &oirq))
40 return NO_IRQ;
41
42 return irq_create_of_mapping(oirq.controller, oirq.specifier,
43 oirq.size);
44}
45EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
Grant Likely7dc2e112010-06-08 07:48:06 -060046
47/**
48 * of_irq_find_parent - Given a device node, find its interrupt parent node
49 * @child: pointer to device node
50 *
51 * Returns a pointer to the interrupt parent node, or NULL if the interrupt
52 * parent could not be determined.
53 */
54static struct device_node *of_irq_find_parent(struct device_node *child)
55{
56 struct device_node *p;
Grant Likely9a6b2e52010-07-23 01:48:25 -060057 const __be32 *parp;
Grant Likely7dc2e112010-06-08 07:48:06 -060058
59 if (!of_node_get(child))
60 return NULL;
61
62 do {
63 parp = of_get_property(child, "interrupt-parent", NULL);
64 if (parp == NULL)
65 p = of_get_parent(child);
66 else {
67 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
68 p = of_node_get(of_irq_dflt_pic);
69 else
Grant Likely9a6b2e52010-07-23 01:48:25 -060070 p = of_find_node_by_phandle(be32_to_cpup(parp));
Grant Likely7dc2e112010-06-08 07:48:06 -060071 }
72 of_node_put(child);
73 child = p;
74 } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
75
76 return p;
77}
78
79/**
80 * of_irq_map_raw - Low level interrupt tree parsing
81 * @parent: the device interrupt parent
82 * @intspec: interrupt specifier ("interrupts" property of the device)
83 * @ointsize: size of the passed in interrupt specifier
84 * @addr: address specifier (start of "reg" property of the device)
85 * @out_irq: structure of_irq filled by this function
86 *
87 * Returns 0 on success and a negative number on error
88 *
89 * This function is a low-level interrupt tree walking function. It
90 * can be used to do a partial walk with synthetized reg and interrupts
91 * properties, for example when resolving PCI interrupts when no device
92 * node exist for the parent.
93 */
Grant Likelyd2f71832010-07-23 16:56:19 -060094int of_irq_map_raw(struct device_node *parent, const __be32 *intspec,
95 u32 ointsize, const __be32 *addr, struct of_irq *out_irq)
Grant Likely7dc2e112010-06-08 07:48:06 -060096{
97 struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
Rob Herringa7c194b2010-06-08 07:48:08 -060098 const __be32 *tmp, *imap, *imask;
Grant Likely7dc2e112010-06-08 07:48:06 -060099 u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
100 int imaplen, match, i;
101
102 pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
Grant Likelyd2f71832010-07-23 16:56:19 -0600103 parent->full_name, be32_to_cpup(intspec),
104 be32_to_cpup(intspec + 1), ointsize);
Grant Likely7dc2e112010-06-08 07:48:06 -0600105
106 ipar = of_node_get(parent);
107
108 /* First get the #interrupt-cells property of the current cursor
109 * that tells us how to interpret the passed-in intspec. If there
110 * is none, we are nice and just walk up the tree
111 */
112 do {
113 tmp = of_get_property(ipar, "#interrupt-cells", NULL);
114 if (tmp != NULL) {
Rob Herringa7c194b2010-06-08 07:48:08 -0600115 intsize = be32_to_cpu(*tmp);
Grant Likely7dc2e112010-06-08 07:48:06 -0600116 break;
117 }
118 tnode = ipar;
119 ipar = of_irq_find_parent(ipar);
120 of_node_put(tnode);
121 } while (ipar);
122 if (ipar == NULL) {
123 pr_debug(" -> no parent found !\n");
124 goto fail;
125 }
126
127 pr_debug("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
128
129 if (ointsize != intsize)
130 return -EINVAL;
131
132 /* Look for this #address-cells. We have to implement the old linux
133 * trick of looking for the parent here as some device-trees rely on it
134 */
135 old = of_node_get(ipar);
136 do {
137 tmp = of_get_property(old, "#address-cells", NULL);
138 tnode = of_get_parent(old);
139 of_node_put(old);
140 old = tnode;
141 } while (old && tmp == NULL);
142 of_node_put(old);
143 old = NULL;
Rob Herringa7c194b2010-06-08 07:48:08 -0600144 addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
Grant Likely7dc2e112010-06-08 07:48:06 -0600145
146 pr_debug(" -> addrsize=%d\n", addrsize);
147
148 /* Now start the actual "proper" walk of the interrupt tree */
149 while (ipar != NULL) {
150 /* Now check if cursor is an interrupt-controller and if it is
151 * then we are done
152 */
153 if (of_get_property(ipar, "interrupt-controller", NULL) !=
154 NULL) {
155 pr_debug(" -> got it !\n");
Rob Herringa7c194b2010-06-08 07:48:08 -0600156 for (i = 0; i < intsize; i++)
157 out_irq->specifier[i] =
158 of_read_number(intspec +i, 1);
Grant Likely7dc2e112010-06-08 07:48:06 -0600159 out_irq->size = intsize;
160 out_irq->controller = ipar;
161 of_node_put(old);
162 return 0;
163 }
164
165 /* Now look for an interrupt-map */
166 imap = of_get_property(ipar, "interrupt-map", &imaplen);
167 /* No interrupt map, check for an interrupt parent */
168 if (imap == NULL) {
169 pr_debug(" -> no map, getting parent\n");
170 newpar = of_irq_find_parent(ipar);
171 goto skiplevel;
172 }
173 imaplen /= sizeof(u32);
174
175 /* Look for a mask */
176 imask = of_get_property(ipar, "interrupt-map-mask", NULL);
177
178 /* If we were passed no "reg" property and we attempt to parse
179 * an interrupt-map, then #address-cells must be 0.
180 * Fail if it's not.
181 */
182 if (addr == NULL && addrsize != 0) {
183 pr_debug(" -> no reg passed in when needed !\n");
184 goto fail;
185 }
186
187 /* Parse interrupt-map */
188 match = 0;
189 while (imaplen > (addrsize + intsize + 1) && !match) {
190 /* Compare specifiers */
191 match = 1;
192 for (i = 0; i < addrsize && match; ++i) {
193 u32 mask = imask ? imask[i] : 0xffffffffu;
194 match = ((addr[i] ^ imap[i]) & mask) == 0;
195 }
196 for (; i < (addrsize + intsize) && match; ++i) {
197 u32 mask = imask ? imask[i] : 0xffffffffu;
198 match =
199 ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
200 }
201 imap += addrsize + intsize;
202 imaplen -= addrsize + intsize;
203
204 pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
205
206 /* Get the interrupt parent */
207 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
208 newpar = of_node_get(of_irq_dflt_pic);
209 else
Grant Likely9a6b2e52010-07-23 01:48:25 -0600210 newpar = of_find_node_by_phandle(be32_to_cpup(imap));
Grant Likely7dc2e112010-06-08 07:48:06 -0600211 imap++;
212 --imaplen;
213
214 /* Check if not found */
215 if (newpar == NULL) {
216 pr_debug(" -> imap parent not found !\n");
217 goto fail;
218 }
219
220 /* Get #interrupt-cells and #address-cells of new
221 * parent
222 */
223 tmp = of_get_property(newpar, "#interrupt-cells", NULL);
224 if (tmp == NULL) {
225 pr_debug(" -> parent lacks #interrupt-cells!\n");
226 goto fail;
227 }
Rob Herringa7c194b2010-06-08 07:48:08 -0600228 newintsize = be32_to_cpu(*tmp);
Grant Likely7dc2e112010-06-08 07:48:06 -0600229 tmp = of_get_property(newpar, "#address-cells", NULL);
Rob Herringa7c194b2010-06-08 07:48:08 -0600230 newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
Grant Likely7dc2e112010-06-08 07:48:06 -0600231
232 pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
233 newintsize, newaddrsize);
234
235 /* Check for malformed properties */
236 if (imaplen < (newaddrsize + newintsize))
237 goto fail;
238
239 imap += newaddrsize + newintsize;
240 imaplen -= newaddrsize + newintsize;
241
242 pr_debug(" -> imaplen=%d\n", imaplen);
243 }
244 if (!match)
245 goto fail;
246
247 of_node_put(old);
248 old = of_node_get(newpar);
249 addrsize = newaddrsize;
250 intsize = newintsize;
251 intspec = imap - intsize;
252 addr = intspec - addrsize;
253
254 skiplevel:
255 /* Iterate again with new parent */
256 pr_debug(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
257 of_node_put(ipar);
258 ipar = newpar;
259 newpar = NULL;
260 }
261 fail:
262 of_node_put(ipar);
263 of_node_put(old);
264 of_node_put(newpar);
265
266 return -EINVAL;
267}
268EXPORT_SYMBOL_GPL(of_irq_map_raw);
269
270/**
271 * of_irq_map_one - Resolve an interrupt for a device
272 * @device: the device whose interrupt is to be resolved
273 * @index: index of the interrupt to resolve
274 * @out_irq: structure of_irq filled by this function
275 *
276 * This function resolves an interrupt, walking the tree, for a given
277 * device-tree node. It's the high level pendant to of_irq_map_raw().
278 */
279int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
280{
281 struct device_node *p;
Grant Likelyd2f71832010-07-23 16:56:19 -0600282 const __be32 *intspec, *tmp, *addr;
Grant Likely7dc2e112010-06-08 07:48:06 -0600283 u32 intsize, intlen;
284 int res = -EINVAL;
285
286 pr_debug("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
287
288 /* OldWorld mac stuff is "special", handle out of line */
289 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
290 return of_irq_map_oldworld(device, index, out_irq);
291
292 /* Get the interrupts property */
293 intspec = of_get_property(device, "interrupts", &intlen);
294 if (intspec == NULL)
295 return -EINVAL;
Grant Likelyd2f71832010-07-23 16:56:19 -0600296 intlen /= sizeof(*intspec);
Grant Likely7dc2e112010-06-08 07:48:06 -0600297
Grant Likelyd2f71832010-07-23 16:56:19 -0600298 pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
Grant Likely7dc2e112010-06-08 07:48:06 -0600299
300 /* Get the reg property (if any) */
301 addr = of_get_property(device, "reg", NULL);
302
303 /* Look for the interrupt parent. */
304 p = of_irq_find_parent(device);
305 if (p == NULL)
306 return -EINVAL;
307
308 /* Get size of interrupt specifier */
309 tmp = of_get_property(p, "#interrupt-cells", NULL);
310 if (tmp == NULL)
311 goto out;
Rob Herringa7c194b2010-06-08 07:48:08 -0600312 intsize = be32_to_cpu(*tmp);
Grant Likely7dc2e112010-06-08 07:48:06 -0600313
314 pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
315
316 /* Check index */
317 if ((index + 1) * intsize > intlen)
318 goto out;
319
320 /* Get new specifier and map it */
321 res = of_irq_map_raw(p, intspec + index * intsize, intsize,
322 addr, out_irq);
323 out:
324 of_node_put(p);
325 return res;
326}
327EXPORT_SYMBOL_GPL(of_irq_map_one);
328
329/**
330 * of_irq_to_resource - Decode a node's IRQ and return it as a resource
331 * @dev: pointer to device tree node
332 * @index: zero-based index of the irq
333 * @r: pointer to resource structure to return result into.
334 */
335int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
336{
337 int irq = irq_of_parse_and_map(dev, index);
338
339 /* Only dereference the resource if both the
340 * resource and the irq are valid. */
341 if (r && irq != NO_IRQ) {
342 r->start = r->end = irq;
343 r->flags = IORESOURCE_IRQ;
Grant Likelyd3571c32010-06-08 07:48:12 -0600344 r->name = dev->full_name;
Grant Likely7dc2e112010-06-08 07:48:06 -0600345 }
346
347 return irq;
348}
349EXPORT_SYMBOL_GPL(of_irq_to_resource);