]> Pileus Git - ~andy/linux/blob - drivers/of/irq.c
of/gpio: add default of_xlate function if device has a node pointer
[~andy/linux] / drivers / of / irq.c
1 /*
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  */
35 unsigned 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 }
45 EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
46
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  */
54 static struct device_node *of_irq_find_parent(struct device_node *child)
55 {
56         struct device_node *p;
57         const phandle *parp;
58
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
70                                 p = of_find_node_by_phandle(*parp);
71                 }
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  */
94 int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize,
95                 const u32 *addr, struct of_irq *out_irq)
96 {
97         struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
98         const __be32 *tmp, *imap, *imask;
99         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",
103             parent->full_name, intspec[0], intspec[1], ointsize);
104
105         ipar = of_node_get(parent);
106
107         /* First get the #interrupt-cells property of the current cursor
108          * that tells us how to interpret the passed-in intspec. If there
109          * is none, we are nice and just walk up the tree
110          */
111         do {
112                 tmp = of_get_property(ipar, "#interrupt-cells", NULL);
113                 if (tmp != NULL) {
114                         intsize = be32_to_cpu(*tmp);
115                         break;
116                 }
117                 tnode = ipar;
118                 ipar = of_irq_find_parent(ipar);
119                 of_node_put(tnode);
120         } while (ipar);
121         if (ipar == NULL) {
122                 pr_debug(" -> no parent found !\n");
123                 goto fail;
124         }
125
126         pr_debug("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
127
128         if (ointsize != intsize)
129                 return -EINVAL;
130
131         /* Look for this #address-cells. We have to implement the old linux
132          * trick of looking for the parent here as some device-trees rely on it
133          */
134         old = of_node_get(ipar);
135         do {
136                 tmp = of_get_property(old, "#address-cells", NULL);
137                 tnode = of_get_parent(old);
138                 of_node_put(old);
139                 old = tnode;
140         } while (old && tmp == NULL);
141         of_node_put(old);
142         old = NULL;
143         addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
144
145         pr_debug(" -> addrsize=%d\n", addrsize);
146
147         /* Now start the actual "proper" walk of the interrupt tree */
148         while (ipar != NULL) {
149                 /* Now check if cursor is an interrupt-controller and if it is
150                  * then we are done
151                  */
152                 if (of_get_property(ipar, "interrupt-controller", NULL) !=
153                                 NULL) {
154                         pr_debug(" -> got it !\n");
155                         for (i = 0; i < intsize; i++)
156                                 out_irq->specifier[i] =
157                                                 of_read_number(intspec +i, 1);
158                         out_irq->size = intsize;
159                         out_irq->controller = ipar;
160                         of_node_put(old);
161                         return 0;
162                 }
163
164                 /* Now look for an interrupt-map */
165                 imap = of_get_property(ipar, "interrupt-map", &imaplen);
166                 /* No interrupt map, check for an interrupt parent */
167                 if (imap == NULL) {
168                         pr_debug(" -> no map, getting parent\n");
169                         newpar = of_irq_find_parent(ipar);
170                         goto skiplevel;
171                 }
172                 imaplen /= sizeof(u32);
173
174                 /* Look for a mask */
175                 imask = of_get_property(ipar, "interrupt-map-mask", NULL);
176
177                 /* If we were passed no "reg" property and we attempt to parse
178                  * an interrupt-map, then #address-cells must be 0.
179                  * Fail if it's not.
180                  */
181                 if (addr == NULL && addrsize != 0) {
182                         pr_debug(" -> no reg passed in when needed !\n");
183                         goto fail;
184                 }
185
186                 /* Parse interrupt-map */
187                 match = 0;
188                 while (imaplen > (addrsize + intsize + 1) && !match) {
189                         /* Compare specifiers */
190                         match = 1;
191                         for (i = 0; i < addrsize && match; ++i) {
192                                 u32 mask = imask ? imask[i] : 0xffffffffu;
193                                 match = ((addr[i] ^ imap[i]) & mask) == 0;
194                         }
195                         for (; i < (addrsize + intsize) && match; ++i) {
196                                 u32 mask = imask ? imask[i] : 0xffffffffu;
197                                 match =
198                                    ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
199                         }
200                         imap += addrsize + intsize;
201                         imaplen -= addrsize + intsize;
202
203                         pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
204
205                         /* Get the interrupt parent */
206                         if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
207                                 newpar = of_node_get(of_irq_dflt_pic);
208                         else
209                                 newpar = of_find_node_by_phandle((phandle)*imap);
210                         imap++;
211                         --imaplen;
212
213                         /* Check if not found */
214                         if (newpar == NULL) {
215                                 pr_debug(" -> imap parent not found !\n");
216                                 goto fail;
217                         }
218
219                         /* Get #interrupt-cells and #address-cells of new
220                          * parent
221                          */
222                         tmp = of_get_property(newpar, "#interrupt-cells", NULL);
223                         if (tmp == NULL) {
224                                 pr_debug(" -> parent lacks #interrupt-cells!\n");
225                                 goto fail;
226                         }
227                         newintsize = be32_to_cpu(*tmp);
228                         tmp = of_get_property(newpar, "#address-cells", NULL);
229                         newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
230
231                         pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
232                             newintsize, newaddrsize);
233
234                         /* Check for malformed properties */
235                         if (imaplen < (newaddrsize + newintsize))
236                                 goto fail;
237
238                         imap += newaddrsize + newintsize;
239                         imaplen -= newaddrsize + newintsize;
240
241                         pr_debug(" -> imaplen=%d\n", imaplen);
242                 }
243                 if (!match)
244                         goto fail;
245
246                 of_node_put(old);
247                 old = of_node_get(newpar);
248                 addrsize = newaddrsize;
249                 intsize = newintsize;
250                 intspec = imap - intsize;
251                 addr = intspec - addrsize;
252
253         skiplevel:
254                 /* Iterate again with new parent */
255                 pr_debug(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
256                 of_node_put(ipar);
257                 ipar = newpar;
258                 newpar = NULL;
259         }
260  fail:
261         of_node_put(ipar);
262         of_node_put(old);
263         of_node_put(newpar);
264
265         return -EINVAL;
266 }
267 EXPORT_SYMBOL_GPL(of_irq_map_raw);
268
269 /**
270  * of_irq_map_one - Resolve an interrupt for a device
271  * @device: the device whose interrupt is to be resolved
272  * @index: index of the interrupt to resolve
273  * @out_irq: structure of_irq filled by this function
274  *
275  * This function resolves an interrupt, walking the tree, for a given
276  * device-tree node. It's the high level pendant to of_irq_map_raw().
277  */
278 int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
279 {
280         struct device_node *p;
281         const u32 *intspec, *tmp, *addr;
282         u32 intsize, intlen;
283         int res = -EINVAL;
284
285         pr_debug("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
286
287         /* OldWorld mac stuff is "special", handle out of line */
288         if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
289                 return of_irq_map_oldworld(device, index, out_irq);
290
291         /* Get the interrupts property */
292         intspec = of_get_property(device, "interrupts", &intlen);
293         if (intspec == NULL)
294                 return -EINVAL;
295         intlen /= sizeof(u32);
296
297         pr_debug(" intspec=%d intlen=%d\n", *intspec, intlen);
298
299         /* Get the reg property (if any) */
300         addr = of_get_property(device, "reg", NULL);
301
302         /* Look for the interrupt parent. */
303         p = of_irq_find_parent(device);
304         if (p == NULL)
305                 return -EINVAL;
306
307         /* Get size of interrupt specifier */
308         tmp = of_get_property(p, "#interrupt-cells", NULL);
309         if (tmp == NULL)
310                 goto out;
311         intsize = be32_to_cpu(*tmp);
312
313         pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
314
315         /* Check index */
316         if ((index + 1) * intsize > intlen)
317                 goto out;
318
319         /* Get new specifier and map it */
320         res = of_irq_map_raw(p, intspec + index * intsize, intsize,
321                              addr, out_irq);
322  out:
323         of_node_put(p);
324         return res;
325 }
326 EXPORT_SYMBOL_GPL(of_irq_map_one);
327
328 /**
329  * of_irq_to_resource - Decode a node's IRQ and return it as a resource
330  * @dev: pointer to device tree node
331  * @index: zero-based index of the irq
332  * @r: pointer to resource structure to return result into.
333  */
334 int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
335 {
336         int irq = irq_of_parse_and_map(dev, index);
337
338         /* Only dereference the resource if both the
339          * resource and the irq are valid. */
340         if (r && irq != NO_IRQ) {
341                 r->start = r->end = irq;
342                 r->flags = IORESOURCE_IRQ;
343                 r->name = dev->full_name;
344         }
345
346         return irq;
347 }
348 EXPORT_SYMBOL_GPL(of_irq_to_resource);