| Grant Likely | e387344 | 2010-06-18 11:09:59 -0600 | [diff] [blame] | 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> | 
| Rob Herring | c71a54b | 2011-09-20 15:13:50 -0500 | [diff] [blame] | 22 | #include <linux/list.h> | 
| Grant Likely | e387344 | 2010-06-18 11:09:59 -0600 | [diff] [blame] | 23 | #include <linux/module.h> | 
|  | 24 | #include <linux/of.h> | 
|  | 25 | #include <linux/of_irq.h> | 
|  | 26 | #include <linux/string.h> | 
| Rob Herring | c71a54b | 2011-09-20 15:13:50 -0500 | [diff] [blame] | 27 | #include <linux/slab.h> | 
| Grant Likely | e387344 | 2010-06-18 11:09:59 -0600 | [diff] [blame] | 28 |  | 
| Andres Salomon | 52f6537 | 2010-10-10 21:35:05 -0600 | [diff] [blame] | 29 | /* For archs that don't support NO_IRQ (such as x86), provide a dummy value */ | 
|  | 30 | #ifndef NO_IRQ | 
|  | 31 | #define NO_IRQ 0 | 
|  | 32 | #endif | 
|  | 33 |  | 
| Grant Likely | e387344 | 2010-06-18 11:09:59 -0600 | [diff] [blame] | 34 | /** | 
|  | 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 | 
|  | 38 | * | 
|  | 39 | * This function is a wrapper that chains of_irq_map_one() and | 
|  | 40 | * irq_create_of_mapping() to make things easier to callers | 
|  | 41 | */ | 
|  | 42 | unsigned int irq_of_parse_and_map(struct device_node *dev, int index) | 
|  | 43 | { | 
|  | 44 | struct of_irq oirq; | 
|  | 45 |  | 
|  | 46 | if (of_irq_map_one(dev, index, &oirq)) | 
|  | 47 | return NO_IRQ; | 
|  | 48 |  | 
|  | 49 | return irq_create_of_mapping(oirq.controller, oirq.specifier, | 
|  | 50 | oirq.size); | 
|  | 51 | } | 
|  | 52 | EXPORT_SYMBOL_GPL(irq_of_parse_and_map); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 53 |  | 
|  | 54 | /** | 
|  | 55 | * of_irq_find_parent - Given a device node, find its interrupt parent node | 
|  | 56 | * @child: pointer to device node | 
|  | 57 | * | 
|  | 58 | * Returns a pointer to the interrupt parent node, or NULL if the interrupt | 
|  | 59 | * parent could not be determined. | 
|  | 60 | */ | 
| Michael Ellerman | 0b2e9a8 | 2011-04-14 22:31:57 +0000 | [diff] [blame] | 61 | struct device_node *of_irq_find_parent(struct device_node *child) | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 62 | { | 
| Linus Torvalds | b4bbb02 | 2011-11-22 15:09:20 -0800 | [diff] [blame] | 63 | struct device_node *p; | 
| Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 64 | const __be32 *parp; | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 65 |  | 
| Linus Torvalds | b4bbb02 | 2011-11-22 15:09:20 -0800 | [diff] [blame] | 66 | if (!of_node_get(child)) | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 67 | return NULL; | 
|  | 68 |  | 
|  | 69 | do { | 
| Linus Torvalds | b4bbb02 | 2011-11-22 15:09:20 -0800 | [diff] [blame] | 70 | parp = of_get_property(child, "interrupt-parent", NULL); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 71 | if (parp == NULL) | 
| Linus Torvalds | b4bbb02 | 2011-11-22 15:09:20 -0800 | [diff] [blame] | 72 | p = of_get_parent(child); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 73 | else { | 
|  | 74 | if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) | 
|  | 75 | p = of_node_get(of_irq_dflt_pic); | 
|  | 76 | else | 
| Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 77 | p = of_find_node_by_phandle(be32_to_cpup(parp)); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 78 | } | 
| Linus Torvalds | b4bbb02 | 2011-11-22 15:09:20 -0800 | [diff] [blame] | 79 | of_node_put(child); | 
|  | 80 | child = p; | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 81 | } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL); | 
|  | 82 |  | 
| Linus Torvalds | b4bbb02 | 2011-11-22 15:09:20 -0800 | [diff] [blame] | 83 | return p; | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | /** | 
|  | 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 | 
|  | 93 | * | 
|  | 94 | * Returns 0 on success and a negative number on error | 
|  | 95 | * | 
|  | 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. | 
|  | 100 | */ | 
| Grant Likely | d2f7183 | 2010-07-23 16:56:19 -0600 | [diff] [blame] | 101 | int of_irq_map_raw(struct device_node *parent, const __be32 *intspec, | 
|  | 102 | u32 ointsize, const __be32 *addr, struct of_irq *out_irq) | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 103 | { | 
|  | 104 | struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL; | 
| Rob Herring | a7c194b | 2010-06-08 07:48:08 -0600 | [diff] [blame] | 105 | const __be32 *tmp, *imap, *imask; | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 106 | u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0; | 
|  | 107 | int imaplen, match, i; | 
|  | 108 |  | 
|  | 109 | pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n", | 
| Grant Likely | d2f7183 | 2010-07-23 16:56:19 -0600 | [diff] [blame] | 110 | parent->full_name, be32_to_cpup(intspec), | 
|  | 111 | be32_to_cpup(intspec + 1), ointsize); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 112 |  | 
|  | 113 | ipar = of_node_get(parent); | 
|  | 114 |  | 
|  | 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 | 
|  | 118 | */ | 
|  | 119 | do { | 
|  | 120 | tmp = of_get_property(ipar, "#interrupt-cells", NULL); | 
|  | 121 | if (tmp != NULL) { | 
| Rob Herring | a7c194b | 2010-06-08 07:48:08 -0600 | [diff] [blame] | 122 | intsize = be32_to_cpu(*tmp); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 123 | break; | 
|  | 124 | } | 
|  | 125 | tnode = ipar; | 
|  | 126 | ipar = of_irq_find_parent(ipar); | 
|  | 127 | of_node_put(tnode); | 
|  | 128 | } while (ipar); | 
|  | 129 | if (ipar == NULL) { | 
|  | 130 | pr_debug(" -> no parent found !\n"); | 
|  | 131 | goto fail; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | pr_debug("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize); | 
|  | 135 |  | 
|  | 136 | if (ointsize != intsize) | 
|  | 137 | return -EINVAL; | 
|  | 138 |  | 
|  | 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 | 
|  | 141 | */ | 
|  | 142 | old = of_node_get(ipar); | 
|  | 143 | do { | 
|  | 144 | tmp = of_get_property(old, "#address-cells", NULL); | 
|  | 145 | tnode = of_get_parent(old); | 
|  | 146 | of_node_put(old); | 
|  | 147 | old = tnode; | 
|  | 148 | } while (old && tmp == NULL); | 
|  | 149 | of_node_put(old); | 
|  | 150 | old = NULL; | 
| Rob Herring | a7c194b | 2010-06-08 07:48:08 -0600 | [diff] [blame] | 151 | addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 152 |  | 
|  | 153 | pr_debug(" -> addrsize=%d\n", addrsize); | 
|  | 154 |  | 
|  | 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 | 
|  | 158 | * then we are done | 
|  | 159 | */ | 
|  | 160 | if (of_get_property(ipar, "interrupt-controller", NULL) != | 
|  | 161 | NULL) { | 
|  | 162 | pr_debug(" -> got it !\n"); | 
| Rob Herring | a7c194b | 2010-06-08 07:48:08 -0600 | [diff] [blame] | 163 | for (i = 0; i < intsize; i++) | 
|  | 164 | out_irq->specifier[i] = | 
|  | 165 | of_read_number(intspec +i, 1); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 166 | out_irq->size = intsize; | 
|  | 167 | out_irq->controller = ipar; | 
|  | 168 | of_node_put(old); | 
|  | 169 | return 0; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 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 */ | 
|  | 175 | if (imap == NULL) { | 
|  | 176 | pr_debug(" -> no map, getting parent\n"); | 
|  | 177 | newpar = of_irq_find_parent(ipar); | 
|  | 178 | goto skiplevel; | 
|  | 179 | } | 
|  | 180 | imaplen /= sizeof(u32); | 
|  | 181 |  | 
|  | 182 | /* Look for a mask */ | 
|  | 183 | imask = of_get_property(ipar, "interrupt-map-mask", NULL); | 
|  | 184 |  | 
|  | 185 | /* If we were passed no "reg" property and we attempt to parse | 
|  | 186 | * an interrupt-map, then #address-cells must be 0. | 
|  | 187 | * Fail if it's not. | 
|  | 188 | */ | 
|  | 189 | if (addr == NULL && addrsize != 0) { | 
|  | 190 | pr_debug(" -> no reg passed in when needed !\n"); | 
|  | 191 | goto fail; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | /* Parse interrupt-map */ | 
|  | 195 | match = 0; | 
|  | 196 | while (imaplen > (addrsize + intsize + 1) && !match) { | 
|  | 197 | /* Compare specifiers */ | 
|  | 198 | match = 1; | 
|  | 199 | for (i = 0; i < addrsize && match; ++i) { | 
|  | 200 | u32 mask = imask ? imask[i] : 0xffffffffu; | 
|  | 201 | match = ((addr[i] ^ imap[i]) & mask) == 0; | 
|  | 202 | } | 
|  | 203 | for (; i < (addrsize + intsize) && match; ++i) { | 
|  | 204 | u32 mask = imask ? imask[i] : 0xffffffffu; | 
|  | 205 | match = | 
|  | 206 | ((intspec[i-addrsize] ^ imap[i]) & mask) == 0; | 
|  | 207 | } | 
|  | 208 | imap += addrsize + intsize; | 
|  | 209 | imaplen -= addrsize + intsize; | 
|  | 210 |  | 
|  | 211 | pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen); | 
|  | 212 |  | 
|  | 213 | /* Get the interrupt parent */ | 
|  | 214 | if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) | 
|  | 215 | newpar = of_node_get(of_irq_dflt_pic); | 
|  | 216 | else | 
| Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 217 | newpar = of_find_node_by_phandle(be32_to_cpup(imap)); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 218 | imap++; | 
|  | 219 | --imaplen; | 
|  | 220 |  | 
|  | 221 | /* Check if not found */ | 
|  | 222 | if (newpar == NULL) { | 
|  | 223 | pr_debug(" -> imap parent not found !\n"); | 
|  | 224 | goto fail; | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | /* Get #interrupt-cells and #address-cells of new | 
|  | 228 | * parent | 
|  | 229 | */ | 
|  | 230 | tmp = of_get_property(newpar, "#interrupt-cells", NULL); | 
|  | 231 | if (tmp == NULL) { | 
|  | 232 | pr_debug(" -> parent lacks #interrupt-cells!\n"); | 
|  | 233 | goto fail; | 
|  | 234 | } | 
| Rob Herring | a7c194b | 2010-06-08 07:48:08 -0600 | [diff] [blame] | 235 | newintsize = be32_to_cpu(*tmp); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 236 | tmp = of_get_property(newpar, "#address-cells", NULL); | 
| Rob Herring | a7c194b | 2010-06-08 07:48:08 -0600 | [diff] [blame] | 237 | newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 238 |  | 
|  | 239 | pr_debug(" -> newintsize=%d, newaddrsize=%d\n", | 
|  | 240 | newintsize, newaddrsize); | 
|  | 241 |  | 
|  | 242 | /* Check for malformed properties */ | 
|  | 243 | if (imaplen < (newaddrsize + newintsize)) | 
|  | 244 | goto fail; | 
|  | 245 |  | 
|  | 246 | imap += newaddrsize + newintsize; | 
|  | 247 | imaplen -= newaddrsize + newintsize; | 
|  | 248 |  | 
|  | 249 | pr_debug(" -> imaplen=%d\n", imaplen); | 
|  | 250 | } | 
|  | 251 | if (!match) | 
|  | 252 | goto fail; | 
|  | 253 |  | 
|  | 254 | of_node_put(old); | 
|  | 255 | old = of_node_get(newpar); | 
|  | 256 | addrsize = newaddrsize; | 
|  | 257 | intsize = newintsize; | 
|  | 258 | intspec = imap - intsize; | 
|  | 259 | addr = intspec - addrsize; | 
|  | 260 |  | 
|  | 261 | skiplevel: | 
|  | 262 | /* Iterate again with new parent */ | 
|  | 263 | pr_debug(" -> new parent: %s\n", newpar ? newpar->full_name : "<>"); | 
|  | 264 | of_node_put(ipar); | 
|  | 265 | ipar = newpar; | 
|  | 266 | newpar = NULL; | 
|  | 267 | } | 
|  | 268 | fail: | 
|  | 269 | of_node_put(ipar); | 
|  | 270 | of_node_put(old); | 
|  | 271 | of_node_put(newpar); | 
|  | 272 |  | 
|  | 273 | return -EINVAL; | 
|  | 274 | } | 
|  | 275 | EXPORT_SYMBOL_GPL(of_irq_map_raw); | 
|  | 276 |  | 
|  | 277 | /** | 
|  | 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 | 
|  | 282 | * | 
|  | 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(). | 
|  | 285 | */ | 
|  | 286 | int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq) | 
|  | 287 | { | 
|  | 288 | struct device_node *p; | 
| Grant Likely | d2f7183 | 2010-07-23 16:56:19 -0600 | [diff] [blame] | 289 | const __be32 *intspec, *tmp, *addr; | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 290 | u32 intsize, intlen; | 
|  | 291 | int res = -EINVAL; | 
|  | 292 |  | 
|  | 293 | pr_debug("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index); | 
|  | 294 |  | 
|  | 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); | 
|  | 298 |  | 
|  | 299 | /* Get the interrupts property */ | 
|  | 300 | intspec = of_get_property(device, "interrupts", &intlen); | 
|  | 301 | if (intspec == NULL) | 
|  | 302 | return -EINVAL; | 
| Grant Likely | d2f7183 | 2010-07-23 16:56:19 -0600 | [diff] [blame] | 303 | intlen /= sizeof(*intspec); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 304 |  | 
| Grant Likely | d2f7183 | 2010-07-23 16:56:19 -0600 | [diff] [blame] | 305 | pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 306 |  | 
|  | 307 | /* Get the reg property (if any) */ | 
|  | 308 | addr = of_get_property(device, "reg", NULL); | 
|  | 309 |  | 
|  | 310 | /* Look for the interrupt parent. */ | 
|  | 311 | p = of_irq_find_parent(device); | 
|  | 312 | if (p == NULL) | 
|  | 313 | return -EINVAL; | 
|  | 314 |  | 
|  | 315 | /* Get size of interrupt specifier */ | 
|  | 316 | tmp = of_get_property(p, "#interrupt-cells", NULL); | 
|  | 317 | if (tmp == NULL) | 
|  | 318 | goto out; | 
| Rob Herring | a7c194b | 2010-06-08 07:48:08 -0600 | [diff] [blame] | 319 | intsize = be32_to_cpu(*tmp); | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 320 |  | 
|  | 321 | pr_debug(" intsize=%d intlen=%d\n", intsize, intlen); | 
|  | 322 |  | 
|  | 323 | /* Check index */ | 
|  | 324 | if ((index + 1) * intsize > intlen) | 
|  | 325 | goto out; | 
|  | 326 |  | 
|  | 327 | /* Get new specifier and map it */ | 
|  | 328 | res = of_irq_map_raw(p, intspec + index * intsize, intsize, | 
|  | 329 | addr, out_irq); | 
|  | 330 | out: | 
|  | 331 | of_node_put(p); | 
|  | 332 | return res; | 
|  | 333 | } | 
|  | 334 | EXPORT_SYMBOL_GPL(of_irq_map_one); | 
|  | 335 |  | 
|  | 336 | /** | 
|  | 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. | 
|  | 341 | */ | 
|  | 342 | int of_irq_to_resource(struct device_node *dev, int index, struct resource *r) | 
|  | 343 | { | 
|  | 344 | int irq = irq_of_parse_and_map(dev, index); | 
|  | 345 |  | 
|  | 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; | 
| Grant Likely | d3571c3 | 2010-06-08 07:48:12 -0600 | [diff] [blame] | 351 | r->name = dev->full_name; | 
| Grant Likely | 7dc2e11 | 2010-06-08 07:48:06 -0600 | [diff] [blame] | 352 | } | 
|  | 353 |  | 
|  | 354 | return irq; | 
|  | 355 | } | 
|  | 356 | EXPORT_SYMBOL_GPL(of_irq_to_resource); | 
| Andres Salomon | 52f6537 | 2010-10-10 21:35:05 -0600 | [diff] [blame] | 357 |  | 
|  | 358 | /** | 
|  | 359 | * of_irq_count - Count the number of IRQs a node uses | 
|  | 360 | * @dev: pointer to device tree node | 
|  | 361 | */ | 
|  | 362 | int of_irq_count(struct device_node *dev) | 
|  | 363 | { | 
|  | 364 | int nr = 0; | 
|  | 365 |  | 
|  | 366 | while (of_irq_to_resource(dev, nr, NULL) != NO_IRQ) | 
|  | 367 | nr++; | 
|  | 368 |  | 
|  | 369 | return nr; | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | /** | 
|  | 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) | 
|  | 377 | * | 
|  | 378 | * Returns the size of the filled in table (up to @nr_irqs). | 
|  | 379 | */ | 
|  | 380 | int of_irq_to_resource_table(struct device_node *dev, struct resource *res, | 
|  | 381 | int nr_irqs) | 
|  | 382 | { | 
|  | 383 | int i; | 
|  | 384 |  | 
|  | 385 | for (i = 0; i < nr_irqs; i++, res++) | 
|  | 386 | if (of_irq_to_resource(dev, i, res) == NO_IRQ) | 
|  | 387 | break; | 
|  | 388 |  | 
|  | 389 | return i; | 
|  | 390 | } | 
| Rob Herring | c71a54b | 2011-09-20 15:13:50 -0500 | [diff] [blame] | 391 |  | 
|  | 392 | struct intc_desc { | 
|  | 393 | struct list_head	list; | 
|  | 394 | struct device_node	*dev; | 
|  | 395 | struct device_node	*interrupt_parent; | 
|  | 396 | }; | 
|  | 397 |  | 
|  | 398 | /** | 
|  | 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 | 
|  | 401 | * | 
|  | 402 | * This function scans the device tree for matching interrupt controller nodes, | 
|  | 403 | * and calls their initialization functions in order with parents first. | 
|  | 404 | */ | 
|  | 405 | void __init of_irq_init(const struct of_device_id *matches) | 
|  | 406 | { | 
|  | 407 | struct device_node *np, *parent = NULL; | 
|  | 408 | struct intc_desc *desc, *temp_desc; | 
|  | 409 | struct list_head intc_desc_list, intc_parent_list; | 
|  | 410 |  | 
|  | 411 | INIT_LIST_HEAD(&intc_desc_list); | 
|  | 412 | INIT_LIST_HEAD(&intc_parent_list); | 
|  | 413 |  | 
|  | 414 | for_each_matching_node(np, matches) { | 
|  | 415 | if (!of_find_property(np, "interrupt-controller", NULL)) | 
|  | 416 | continue; | 
|  | 417 | /* | 
|  | 418 | * Here, we allocate and populate an intc_desc with the node | 
|  | 419 | * pointer, interrupt-parent device_node etc. | 
|  | 420 | */ | 
|  | 421 | desc = kzalloc(sizeof(*desc), GFP_KERNEL); | 
|  | 422 | if (WARN_ON(!desc)) | 
|  | 423 | goto err; | 
|  | 424 |  | 
|  | 425 | desc->dev = np; | 
|  | 426 | desc->interrupt_parent = of_irq_find_parent(np); | 
| Rob Herring | d7fb6d0 | 2011-11-27 20:16:33 -0600 | [diff] [blame] | 427 | if (desc->interrupt_parent == np) | 
|  | 428 | desc->interrupt_parent = NULL; | 
| Rob Herring | c71a54b | 2011-09-20 15:13:50 -0500 | [diff] [blame] | 429 | list_add_tail(&desc->list, &intc_desc_list); | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | /* | 
|  | 433 | * The root irq controller is the one without an interrupt-parent. | 
|  | 434 | * That one goes first, followed by the controllers that reference it, | 
|  | 435 | * followed by the ones that reference the 2nd level controllers, etc. | 
|  | 436 | */ | 
|  | 437 | while (!list_empty(&intc_desc_list)) { | 
|  | 438 | /* | 
|  | 439 | * Process all controllers with the current 'parent'. | 
|  | 440 | * First pass will be looking for NULL as the parent. | 
|  | 441 | * The assumption is that NULL parent means a root controller. | 
|  | 442 | */ | 
|  | 443 | list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) { | 
|  | 444 | const struct of_device_id *match; | 
|  | 445 | int ret; | 
|  | 446 | of_irq_init_cb_t irq_init_cb; | 
|  | 447 |  | 
|  | 448 | if (desc->interrupt_parent != parent) | 
|  | 449 | continue; | 
|  | 450 |  | 
|  | 451 | list_del(&desc->list); | 
|  | 452 | match = of_match_node(matches, desc->dev); | 
|  | 453 | if (WARN(!match->data, | 
|  | 454 | "of_irq_init: no init function for %s\n", | 
|  | 455 | match->compatible)) { | 
|  | 456 | kfree(desc); | 
|  | 457 | continue; | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | pr_debug("of_irq_init: init %s @ %p, parent %p\n", | 
|  | 461 | match->compatible, | 
|  | 462 | desc->dev, desc->interrupt_parent); | 
|  | 463 | irq_init_cb = match->data; | 
|  | 464 | ret = irq_init_cb(desc->dev, desc->interrupt_parent); | 
|  | 465 | if (ret) { | 
|  | 466 | kfree(desc); | 
|  | 467 | continue; | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | /* | 
|  | 471 | * This one is now set up; add it to the parent list so | 
|  | 472 | * its children can get processed in a subsequent pass. | 
|  | 473 | */ | 
|  | 474 | list_add_tail(&desc->list, &intc_parent_list); | 
|  | 475 | } | 
|  | 476 |  | 
|  | 477 | /* Get the next pending parent that might have children */ | 
|  | 478 | desc = list_first_entry(&intc_parent_list, typeof(*desc), list); | 
|  | 479 | if (list_empty(&intc_parent_list) || !desc) { | 
|  | 480 | pr_err("of_irq_init: children remain, but no parents\n"); | 
|  | 481 | break; | 
|  | 482 | } | 
|  | 483 | list_del(&desc->list); | 
|  | 484 | parent = desc->dev; | 
|  | 485 | kfree(desc); | 
|  | 486 | } | 
|  | 487 |  | 
|  | 488 | list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) { | 
|  | 489 | list_del(&desc->list); | 
|  | 490 | kfree(desc); | 
|  | 491 | } | 
|  | 492 | err: | 
|  | 493 | list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) { | 
|  | 494 | list_del(&desc->list); | 
|  | 495 | kfree(desc); | 
|  | 496 | } | 
|  | 497 | } |