1e3873444SGrant Likely /* 2e3873444SGrant Likely * Derived from arch/i386/kernel/irq.c 3e3873444SGrant Likely * Copyright (C) 1992 Linus Torvalds 4e3873444SGrant Likely * Adapted from arch/i386 by Gary Thomas 5e3873444SGrant Likely * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 6e3873444SGrant Likely * Updated and modified by Cort Dougan <cort@fsmlabs.com> 7e3873444SGrant Likely * Copyright (C) 1996-2001 Cort Dougan 8e3873444SGrant Likely * Adapted for Power Macintosh by Paul Mackerras 9e3873444SGrant Likely * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au) 10e3873444SGrant Likely * 11e3873444SGrant Likely * This program is free software; you can redistribute it and/or 12e3873444SGrant Likely * modify it under the terms of the GNU General Public License 13e3873444SGrant Likely * as published by the Free Software Foundation; either version 14e3873444SGrant Likely * 2 of the License, or (at your option) any later version. 15e3873444SGrant Likely * 16e3873444SGrant Likely * This file contains the code used to make IRQ descriptions in the 17e3873444SGrant Likely * device tree to actual irq numbers on an interrupt controller 18e3873444SGrant Likely * driver. 19e3873444SGrant Likely */ 20e3873444SGrant Likely 21e3873444SGrant Likely #include <linux/errno.h> 22c71a54b0SRob Herring #include <linux/list.h> 23e3873444SGrant Likely #include <linux/module.h> 24e3873444SGrant Likely #include <linux/of.h> 25e3873444SGrant Likely #include <linux/of_irq.h> 26e3873444SGrant Likely #include <linux/string.h> 27c71a54b0SRob Herring #include <linux/slab.h> 28e3873444SGrant Likely 29e3873444SGrant Likely /** 30e3873444SGrant Likely * irq_of_parse_and_map - Parse and map an interrupt into linux virq space 31d84ff46aSYijing Wang * @dev: Device node of the device whose interrupt is to be mapped 32e3873444SGrant Likely * @index: Index of the interrupt to map 33e3873444SGrant Likely * 340c02c800SGrant Likely * This function is a wrapper that chains of_irq_parse_one() and 35e3873444SGrant Likely * irq_create_of_mapping() to make things easier to callers 36e3873444SGrant Likely */ 37e3873444SGrant Likely unsigned int irq_of_parse_and_map(struct device_node *dev, int index) 38e3873444SGrant Likely { 39530210c7SGrant Likely struct of_phandle_args oirq; 40e3873444SGrant Likely 410c02c800SGrant Likely if (of_irq_parse_one(dev, index, &oirq)) 4277a7300aSAnton Vorontsov return 0; 43e3873444SGrant Likely 44e6d30ab1SGrant Likely return irq_create_of_mapping(&oirq); 45e3873444SGrant Likely } 46e3873444SGrant Likely EXPORT_SYMBOL_GPL(irq_of_parse_and_map); 477dc2e113SGrant Likely 487dc2e113SGrant Likely /** 497dc2e113SGrant Likely * of_irq_find_parent - Given a device node, find its interrupt parent node 507dc2e113SGrant Likely * @child: pointer to device node 517dc2e113SGrant Likely * 527dc2e113SGrant Likely * Returns a pointer to the interrupt parent node, or NULL if the interrupt 537dc2e113SGrant Likely * parent could not be determined. 547dc2e113SGrant Likely */ 550b2e9a8eSMichael Ellerman struct device_node *of_irq_find_parent(struct device_node *child) 567dc2e113SGrant Likely { 57b4bbb029SLinus Torvalds struct device_node *p; 589a6b2e58SGrant Likely const __be32 *parp; 597dc2e113SGrant Likely 60b4bbb029SLinus Torvalds if (!of_node_get(child)) 617dc2e113SGrant Likely return NULL; 627dc2e113SGrant Likely 637dc2e113SGrant Likely do { 64b4bbb029SLinus Torvalds parp = of_get_property(child, "interrupt-parent", NULL); 657dc2e113SGrant Likely if (parp == NULL) 66b4bbb029SLinus Torvalds p = of_get_parent(child); 677dc2e113SGrant Likely else { 687dc2e113SGrant Likely if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) 697dc2e113SGrant Likely p = of_node_get(of_irq_dflt_pic); 707dc2e113SGrant Likely else 719a6b2e58SGrant Likely p = of_find_node_by_phandle(be32_to_cpup(parp)); 727dc2e113SGrant Likely } 73b4bbb029SLinus Torvalds of_node_put(child); 74b4bbb029SLinus Torvalds child = p; 757dc2e113SGrant Likely } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL); 767dc2e113SGrant Likely 77b4bbb029SLinus Torvalds return p; 787dc2e113SGrant Likely } 797dc2e113SGrant Likely 807dc2e113SGrant Likely /** 810c02c800SGrant Likely * of_irq_parse_raw - Low level interrupt tree parsing 827dc2e113SGrant Likely * @parent: the device interrupt parent 8323616132SGrant Likely * @addr: address specifier (start of "reg" property of the device) in be32 format 8423616132SGrant Likely * @out_irq: structure of_irq updated by this function 857dc2e113SGrant Likely * 867dc2e113SGrant Likely * Returns 0 on success and a negative number on error 877dc2e113SGrant Likely * 887dc2e113SGrant Likely * This function is a low-level interrupt tree walking function. It 897dc2e113SGrant Likely * can be used to do a partial walk with synthetized reg and interrupts 907dc2e113SGrant Likely * properties, for example when resolving PCI interrupts when no device 9123616132SGrant Likely * node exist for the parent. It takes an interrupt specifier structure as 9223616132SGrant Likely * input, walks the tree looking for any interrupt-map properties, translates 9323616132SGrant Likely * the specifier for each map, and then returns the translated map. 947dc2e113SGrant Likely */ 9523616132SGrant Likely int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq) 967dc2e113SGrant Likely { 977dc2e113SGrant Likely struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL; 98355e62f5SGrant Likely __be32 initial_match_array[MAX_PHANDLE_ARGS]; 9923616132SGrant Likely const __be32 *match_array = initial_match_array; 100355e62f5SGrant Likely const __be32 *tmp, *imap, *imask, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 }; 1017dc2e113SGrant Likely u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0; 1027dc2e113SGrant Likely int imaplen, match, i; 1037dc2e113SGrant Likely 104624cfca5SGrant Likely #ifdef DEBUG 105624cfca5SGrant Likely of_print_phandle_args("of_irq_parse_raw: ", out_irq); 106624cfca5SGrant Likely #endif 1077dc2e113SGrant Likely 10823616132SGrant Likely ipar = of_node_get(out_irq->np); 1097dc2e113SGrant Likely 1107dc2e113SGrant Likely /* First get the #interrupt-cells property of the current cursor 1117dc2e113SGrant Likely * that tells us how to interpret the passed-in intspec. If there 1127dc2e113SGrant Likely * is none, we are nice and just walk up the tree 1137dc2e113SGrant Likely */ 1147dc2e113SGrant Likely do { 1157dc2e113SGrant Likely tmp = of_get_property(ipar, "#interrupt-cells", NULL); 1167dc2e113SGrant Likely if (tmp != NULL) { 117a7c194b0SRob Herring intsize = be32_to_cpu(*tmp); 1187dc2e113SGrant Likely break; 1197dc2e113SGrant Likely } 1207dc2e113SGrant Likely tnode = ipar; 1217dc2e113SGrant Likely ipar = of_irq_find_parent(ipar); 1227dc2e113SGrant Likely of_node_put(tnode); 1237dc2e113SGrant Likely } while (ipar); 1247dc2e113SGrant Likely if (ipar == NULL) { 1257dc2e113SGrant Likely pr_debug(" -> no parent found !\n"); 1267dc2e113SGrant Likely goto fail; 1277dc2e113SGrant Likely } 1287dc2e113SGrant Likely 1290c02c800SGrant Likely pr_debug("of_irq_parse_raw: ipar=%s, size=%d\n", of_node_full_name(ipar), intsize); 1307dc2e113SGrant Likely 13123616132SGrant Likely if (out_irq->args_count != intsize) 1327dc2e113SGrant Likely return -EINVAL; 1337dc2e113SGrant Likely 1347dc2e113SGrant Likely /* Look for this #address-cells. We have to implement the old linux 1357dc2e113SGrant Likely * trick of looking for the parent here as some device-trees rely on it 1367dc2e113SGrant Likely */ 1377dc2e113SGrant Likely old = of_node_get(ipar); 1387dc2e113SGrant Likely do { 1397dc2e113SGrant Likely tmp = of_get_property(old, "#address-cells", NULL); 1407dc2e113SGrant Likely tnode = of_get_parent(old); 1417dc2e113SGrant Likely of_node_put(old); 1427dc2e113SGrant Likely old = tnode; 1437dc2e113SGrant Likely } while (old && tmp == NULL); 1447dc2e113SGrant Likely of_node_put(old); 1457dc2e113SGrant Likely old = NULL; 146a7c194b0SRob Herring addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp); 1477dc2e113SGrant Likely 1487dc2e113SGrant Likely pr_debug(" -> addrsize=%d\n", addrsize); 1497dc2e113SGrant Likely 150355e62f5SGrant Likely /* Range check so that the temporary buffer doesn't overflow */ 151355e62f5SGrant Likely if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) 152355e62f5SGrant Likely goto fail; 153355e62f5SGrant Likely 15423616132SGrant Likely /* Precalculate the match array - this simplifies match loop */ 15523616132SGrant Likely for (i = 0; i < addrsize; i++) 15678119fd1SGrant Likely initial_match_array[i] = addr ? addr[i] : 0; 15723616132SGrant Likely for (i = 0; i < intsize; i++) 15823616132SGrant Likely initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]); 15923616132SGrant Likely 1607dc2e113SGrant Likely /* Now start the actual "proper" walk of the interrupt tree */ 1617dc2e113SGrant Likely while (ipar != NULL) { 1627dc2e113SGrant Likely /* Now check if cursor is an interrupt-controller and if it is 1637dc2e113SGrant Likely * then we are done 1647dc2e113SGrant Likely */ 1657dc2e113SGrant Likely if (of_get_property(ipar, "interrupt-controller", NULL) != 1667dc2e113SGrant Likely NULL) { 1677dc2e113SGrant Likely pr_debug(" -> got it !\n"); 1687dc2e113SGrant Likely of_node_put(old); 1697dc2e113SGrant Likely return 0; 1707dc2e113SGrant Likely } 1717dc2e113SGrant Likely 17278119fd1SGrant Likely /* 17378119fd1SGrant Likely * interrupt-map parsing does not work without a reg 17478119fd1SGrant Likely * property when #address-cells != 0 17578119fd1SGrant Likely */ 17678119fd1SGrant Likely if (addrsize && !addr) { 17778119fd1SGrant Likely pr_debug(" -> no reg passed in when needed !\n"); 17878119fd1SGrant Likely goto fail; 17978119fd1SGrant Likely } 18078119fd1SGrant Likely 1817dc2e113SGrant Likely /* Now look for an interrupt-map */ 1827dc2e113SGrant Likely imap = of_get_property(ipar, "interrupt-map", &imaplen); 1837dc2e113SGrant Likely /* No interrupt map, check for an interrupt parent */ 1847dc2e113SGrant Likely if (imap == NULL) { 1857dc2e113SGrant Likely pr_debug(" -> no map, getting parent\n"); 1867dc2e113SGrant Likely newpar = of_irq_find_parent(ipar); 1877dc2e113SGrant Likely goto skiplevel; 1887dc2e113SGrant Likely } 1897dc2e113SGrant Likely imaplen /= sizeof(u32); 1907dc2e113SGrant Likely 1917dc2e113SGrant Likely /* Look for a mask */ 1927dc2e113SGrant Likely imask = of_get_property(ipar, "interrupt-map-mask", NULL); 19323616132SGrant Likely if (!imask) 19423616132SGrant Likely imask = dummy_imask; 1957dc2e113SGrant Likely 1967dc2e113SGrant Likely /* Parse interrupt-map */ 1977dc2e113SGrant Likely match = 0; 1987dc2e113SGrant Likely while (imaplen > (addrsize + intsize + 1) && !match) { 1997dc2e113SGrant Likely /* Compare specifiers */ 2007dc2e113SGrant Likely match = 1; 20123616132SGrant Likely for (i = 0; i < (addrsize + intsize); i++, imaplen--) 202*74dac2edSTomasz Figa match &= !((match_array[i] ^ *imap++) & imask[i]); 2037dc2e113SGrant Likely 2047dc2e113SGrant Likely pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen); 2057dc2e113SGrant Likely 2067dc2e113SGrant Likely /* Get the interrupt parent */ 2077dc2e113SGrant Likely if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) 2087dc2e113SGrant Likely newpar = of_node_get(of_irq_dflt_pic); 2097dc2e113SGrant Likely else 2109a6b2e58SGrant Likely newpar = of_find_node_by_phandle(be32_to_cpup(imap)); 2117dc2e113SGrant Likely imap++; 2127dc2e113SGrant Likely --imaplen; 2137dc2e113SGrant Likely 2147dc2e113SGrant Likely /* Check if not found */ 2157dc2e113SGrant Likely if (newpar == NULL) { 2167dc2e113SGrant Likely pr_debug(" -> imap parent not found !\n"); 2177dc2e113SGrant Likely goto fail; 2187dc2e113SGrant Likely } 2197dc2e113SGrant Likely 2207dc2e113SGrant Likely /* Get #interrupt-cells and #address-cells of new 2217dc2e113SGrant Likely * parent 2227dc2e113SGrant Likely */ 2237dc2e113SGrant Likely tmp = of_get_property(newpar, "#interrupt-cells", NULL); 2247dc2e113SGrant Likely if (tmp == NULL) { 2257dc2e113SGrant Likely pr_debug(" -> parent lacks #interrupt-cells!\n"); 2267dc2e113SGrant Likely goto fail; 2277dc2e113SGrant Likely } 228a7c194b0SRob Herring newintsize = be32_to_cpu(*tmp); 2297dc2e113SGrant Likely tmp = of_get_property(newpar, "#address-cells", NULL); 230a7c194b0SRob Herring newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp); 2317dc2e113SGrant Likely 2327dc2e113SGrant Likely pr_debug(" -> newintsize=%d, newaddrsize=%d\n", 2337dc2e113SGrant Likely newintsize, newaddrsize); 2347dc2e113SGrant Likely 2357dc2e113SGrant Likely /* Check for malformed properties */ 236355e62f5SGrant Likely if (WARN_ON(newaddrsize + newintsize > MAX_PHANDLE_ARGS)) 237355e62f5SGrant Likely goto fail; 2387dc2e113SGrant Likely if (imaplen < (newaddrsize + newintsize)) 2397dc2e113SGrant Likely goto fail; 2407dc2e113SGrant Likely 2417dc2e113SGrant Likely imap += newaddrsize + newintsize; 2427dc2e113SGrant Likely imaplen -= newaddrsize + newintsize; 2437dc2e113SGrant Likely 2447dc2e113SGrant Likely pr_debug(" -> imaplen=%d\n", imaplen); 2457dc2e113SGrant Likely } 2467dc2e113SGrant Likely if (!match) 2477dc2e113SGrant Likely goto fail; 2487dc2e113SGrant Likely 24923616132SGrant Likely /* 25023616132SGrant Likely * Successfully parsed an interrrupt-map translation; copy new 25123616132SGrant Likely * interrupt specifier into the out_irq structure 25223616132SGrant Likely */ 25323616132SGrant Likely of_node_put(out_irq->np); 25423616132SGrant Likely out_irq->np = of_node_get(newpar); 25523616132SGrant Likely 25623616132SGrant Likely match_array = imap - newaddrsize - newintsize; 25723616132SGrant Likely for (i = 0; i < newintsize; i++) 25823616132SGrant Likely out_irq->args[i] = be32_to_cpup(imap - newintsize + i); 25923616132SGrant Likely out_irq->args_count = intsize = newintsize; 2607dc2e113SGrant Likely addrsize = newaddrsize; 2617dc2e113SGrant Likely 2627dc2e113SGrant Likely skiplevel: 2637dc2e113SGrant Likely /* Iterate again with new parent */ 26474a7f084SGrant Likely pr_debug(" -> new parent: %s\n", of_node_full_name(newpar)); 2657dc2e113SGrant Likely of_node_put(ipar); 2667dc2e113SGrant Likely ipar = newpar; 2677dc2e113SGrant Likely newpar = NULL; 2687dc2e113SGrant Likely } 2697dc2e113SGrant Likely fail: 2707dc2e113SGrant Likely of_node_put(ipar); 27123616132SGrant Likely of_node_put(out_irq->np); 2727dc2e113SGrant Likely of_node_put(newpar); 2737dc2e113SGrant Likely 2747dc2e113SGrant Likely return -EINVAL; 2757dc2e113SGrant Likely } 2760c02c800SGrant Likely EXPORT_SYMBOL_GPL(of_irq_parse_raw); 2777dc2e113SGrant Likely 2787dc2e113SGrant Likely /** 2790c02c800SGrant Likely * of_irq_parse_one - Resolve an interrupt for a device 2807dc2e113SGrant Likely * @device: the device whose interrupt is to be resolved 2817dc2e113SGrant Likely * @index: index of the interrupt to resolve 2827dc2e113SGrant Likely * @out_irq: structure of_irq filled by this function 2837dc2e113SGrant Likely * 28423616132SGrant Likely * This function resolves an interrupt for a node by walking the interrupt tree, 28523616132SGrant Likely * finding which interrupt controller node it is attached to, and returning the 28623616132SGrant Likely * interrupt specifier that can be used to retrieve a Linux IRQ number. 2877dc2e113SGrant Likely */ 288530210c7SGrant Likely int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq) 2897dc2e113SGrant Likely { 2907dc2e113SGrant Likely struct device_node *p; 291d2f71839SGrant Likely const __be32 *intspec, *tmp, *addr; 2927dc2e113SGrant Likely u32 intsize, intlen; 29323616132SGrant Likely int i, res = -EINVAL; 2947dc2e113SGrant Likely 2950c02c800SGrant Likely pr_debug("of_irq_parse_one: dev=%s, index=%d\n", of_node_full_name(device), index); 2967dc2e113SGrant Likely 2977dc2e113SGrant Likely /* OldWorld mac stuff is "special", handle out of line */ 2987dc2e113SGrant Likely if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC) 2990c02c800SGrant Likely return of_irq_parse_oldworld(device, index, out_irq); 3007dc2e113SGrant Likely 30179d97015SGrant Likely /* Get the reg property (if any) */ 30279d97015SGrant Likely addr = of_get_property(device, "reg", NULL); 30379d97015SGrant Likely 3047dc2e113SGrant Likely /* Get the interrupts property */ 3057dc2e113SGrant Likely intspec = of_get_property(device, "interrupts", &intlen); 30679d97015SGrant Likely if (intspec == NULL) { 30779d97015SGrant Likely /* Try the new-style interrupts-extended */ 30879d97015SGrant Likely res = of_parse_phandle_with_args(device, "interrupts-extended", 30979d97015SGrant Likely "#interrupt-cells", index, out_irq); 31079d97015SGrant Likely if (res) 3117dc2e113SGrant Likely return -EINVAL; 31279d97015SGrant Likely return of_irq_parse_raw(addr, out_irq); 31379d97015SGrant Likely } 314d2f71839SGrant Likely intlen /= sizeof(*intspec); 3157dc2e113SGrant Likely 316d2f71839SGrant Likely pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen); 3177dc2e113SGrant Likely 3187dc2e113SGrant Likely /* Look for the interrupt parent. */ 3197dc2e113SGrant Likely p = of_irq_find_parent(device); 3207dc2e113SGrant Likely if (p == NULL) 3217dc2e113SGrant Likely return -EINVAL; 3227dc2e113SGrant Likely 3237dc2e113SGrant Likely /* Get size of interrupt specifier */ 3247dc2e113SGrant Likely tmp = of_get_property(p, "#interrupt-cells", NULL); 3257dc2e113SGrant Likely if (tmp == NULL) 3267dc2e113SGrant Likely goto out; 327a7c194b0SRob Herring intsize = be32_to_cpu(*tmp); 3287dc2e113SGrant Likely 3297dc2e113SGrant Likely pr_debug(" intsize=%d intlen=%d\n", intsize, intlen); 3307dc2e113SGrant Likely 3317dc2e113SGrant Likely /* Check index */ 3327dc2e113SGrant Likely if ((index + 1) * intsize > intlen) 3337dc2e113SGrant Likely goto out; 3347dc2e113SGrant Likely 33523616132SGrant Likely /* Copy intspec into irq structure */ 33623616132SGrant Likely intspec += index * intsize; 33723616132SGrant Likely out_irq->np = p; 33823616132SGrant Likely out_irq->args_count = intsize; 33923616132SGrant Likely for (i = 0; i < intsize; i++) 34023616132SGrant Likely out_irq->args[i] = be32_to_cpup(intspec++); 34123616132SGrant Likely 34223616132SGrant Likely /* Check if there are any interrupt-map translations to process */ 34323616132SGrant Likely res = of_irq_parse_raw(addr, out_irq); 3447dc2e113SGrant Likely out: 3457dc2e113SGrant Likely of_node_put(p); 3467dc2e113SGrant Likely return res; 3477dc2e113SGrant Likely } 3480c02c800SGrant Likely EXPORT_SYMBOL_GPL(of_irq_parse_one); 3497dc2e113SGrant Likely 3507dc2e113SGrant Likely /** 3517dc2e113SGrant Likely * of_irq_to_resource - Decode a node's IRQ and return it as a resource 3527dc2e113SGrant Likely * @dev: pointer to device tree node 3537dc2e113SGrant Likely * @index: zero-based index of the irq 3547dc2e113SGrant Likely * @r: pointer to resource structure to return result into. 3557dc2e113SGrant Likely */ 3567dc2e113SGrant Likely int of_irq_to_resource(struct device_node *dev, int index, struct resource *r) 3577dc2e113SGrant Likely { 3587dc2e113SGrant Likely int irq = irq_of_parse_and_map(dev, index); 3597dc2e113SGrant Likely 3607dc2e113SGrant Likely /* Only dereference the resource if both the 3617dc2e113SGrant Likely * resource and the irq are valid. */ 36277a7300aSAnton Vorontsov if (r && irq) { 363661db794SBenoit Cousson const char *name = NULL; 364661db794SBenoit Cousson 365cf9e2368SSebastian Andrzej Siewior memset(r, 0, sizeof(*r)); 366661db794SBenoit Cousson /* 367661db794SBenoit Cousson * Get optional "interrupts-names" property to add a name 368661db794SBenoit Cousson * to the resource. 369661db794SBenoit Cousson */ 370661db794SBenoit Cousson of_property_read_string_index(dev, "interrupt-names", index, 371661db794SBenoit Cousson &name); 372661db794SBenoit Cousson 3737dc2e113SGrant Likely r->start = r->end = irq; 3744a43d686STomasz Figa r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq)); 3758804827bSGrant Likely r->name = name ? name : of_node_full_name(dev); 3767dc2e113SGrant Likely } 3777dc2e113SGrant Likely 3787dc2e113SGrant Likely return irq; 3797dc2e113SGrant Likely } 3807dc2e113SGrant Likely EXPORT_SYMBOL_GPL(of_irq_to_resource); 38152f6537cSAndres Salomon 38252f6537cSAndres Salomon /** 38352f6537cSAndres Salomon * of_irq_count - Count the number of IRQs a node uses 38452f6537cSAndres Salomon * @dev: pointer to device tree node 38552f6537cSAndres Salomon */ 38652f6537cSAndres Salomon int of_irq_count(struct device_node *dev) 38752f6537cSAndres Salomon { 3883da52787SThierry Reding struct of_phandle_args irq; 38952f6537cSAndres Salomon int nr = 0; 39052f6537cSAndres Salomon 3913da52787SThierry Reding while (of_irq_parse_one(dev, nr, &irq) == 0) 39252f6537cSAndres Salomon nr++; 39352f6537cSAndres Salomon 39452f6537cSAndres Salomon return nr; 39552f6537cSAndres Salomon } 39652f6537cSAndres Salomon 39752f6537cSAndres Salomon /** 39852f6537cSAndres Salomon * of_irq_to_resource_table - Fill in resource table with node's IRQ info 39952f6537cSAndres Salomon * @dev: pointer to device tree node 40052f6537cSAndres Salomon * @res: array of resources to fill in 40152f6537cSAndres Salomon * @nr_irqs: the number of IRQs (and upper bound for num of @res elements) 40252f6537cSAndres Salomon * 40352f6537cSAndres Salomon * Returns the size of the filled in table (up to @nr_irqs). 40452f6537cSAndres Salomon */ 40552f6537cSAndres Salomon int of_irq_to_resource_table(struct device_node *dev, struct resource *res, 40652f6537cSAndres Salomon int nr_irqs) 40752f6537cSAndres Salomon { 40852f6537cSAndres Salomon int i; 40952f6537cSAndres Salomon 41052f6537cSAndres Salomon for (i = 0; i < nr_irqs; i++, res++) 41177a7300aSAnton Vorontsov if (!of_irq_to_resource(dev, i, res)) 41252f6537cSAndres Salomon break; 41352f6537cSAndres Salomon 41452f6537cSAndres Salomon return i; 41552f6537cSAndres Salomon } 416a4f8bf22SJohn Crispin EXPORT_SYMBOL_GPL(of_irq_to_resource_table); 417c71a54b0SRob Herring 418c71a54b0SRob Herring struct intc_desc { 419c71a54b0SRob Herring struct list_head list; 420c71a54b0SRob Herring struct device_node *dev; 421c71a54b0SRob Herring struct device_node *interrupt_parent; 422c71a54b0SRob Herring }; 423c71a54b0SRob Herring 424c71a54b0SRob Herring /** 425c71a54b0SRob Herring * of_irq_init - Scan and init matching interrupt controllers in DT 426c71a54b0SRob Herring * @matches: 0 terminated array of nodes to match and init function to call 427c71a54b0SRob Herring * 428c71a54b0SRob Herring * This function scans the device tree for matching interrupt controller nodes, 429c71a54b0SRob Herring * and calls their initialization functions in order with parents first. 430c71a54b0SRob Herring */ 431c71a54b0SRob Herring void __init of_irq_init(const struct of_device_id *matches) 432c71a54b0SRob Herring { 433c71a54b0SRob Herring struct device_node *np, *parent = NULL; 434c71a54b0SRob Herring struct intc_desc *desc, *temp_desc; 435c71a54b0SRob Herring struct list_head intc_desc_list, intc_parent_list; 436c71a54b0SRob Herring 437c71a54b0SRob Herring INIT_LIST_HEAD(&intc_desc_list); 438c71a54b0SRob Herring INIT_LIST_HEAD(&intc_parent_list); 439c71a54b0SRob Herring 440c71a54b0SRob Herring for_each_matching_node(np, matches) { 441c71a54b0SRob Herring if (!of_find_property(np, "interrupt-controller", NULL)) 442c71a54b0SRob Herring continue; 443c71a54b0SRob Herring /* 444c71a54b0SRob Herring * Here, we allocate and populate an intc_desc with the node 445c71a54b0SRob Herring * pointer, interrupt-parent device_node etc. 446c71a54b0SRob Herring */ 447c71a54b0SRob Herring desc = kzalloc(sizeof(*desc), GFP_KERNEL); 448c71a54b0SRob Herring if (WARN_ON(!desc)) 449c71a54b0SRob Herring goto err; 450c71a54b0SRob Herring 451c71a54b0SRob Herring desc->dev = np; 452c71a54b0SRob Herring desc->interrupt_parent = of_irq_find_parent(np); 453d7fb6d0aSRob Herring if (desc->interrupt_parent == np) 454d7fb6d0aSRob Herring desc->interrupt_parent = NULL; 455c71a54b0SRob Herring list_add_tail(&desc->list, &intc_desc_list); 456c71a54b0SRob Herring } 457c71a54b0SRob Herring 458c71a54b0SRob Herring /* 459c71a54b0SRob Herring * The root irq controller is the one without an interrupt-parent. 460c71a54b0SRob Herring * That one goes first, followed by the controllers that reference it, 461c71a54b0SRob Herring * followed by the ones that reference the 2nd level controllers, etc. 462c71a54b0SRob Herring */ 463c71a54b0SRob Herring while (!list_empty(&intc_desc_list)) { 464c71a54b0SRob Herring /* 465c71a54b0SRob Herring * Process all controllers with the current 'parent'. 466c71a54b0SRob Herring * First pass will be looking for NULL as the parent. 467c71a54b0SRob Herring * The assumption is that NULL parent means a root controller. 468c71a54b0SRob Herring */ 469c71a54b0SRob Herring list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) { 470c71a54b0SRob Herring const struct of_device_id *match; 471c71a54b0SRob Herring int ret; 472c71a54b0SRob Herring of_irq_init_cb_t irq_init_cb; 473c71a54b0SRob Herring 474c71a54b0SRob Herring if (desc->interrupt_parent != parent) 475c71a54b0SRob Herring continue; 476c71a54b0SRob Herring 477c71a54b0SRob Herring list_del(&desc->list); 478c71a54b0SRob Herring match = of_match_node(matches, desc->dev); 479c71a54b0SRob Herring if (WARN(!match->data, 480c71a54b0SRob Herring "of_irq_init: no init function for %s\n", 481c71a54b0SRob Herring match->compatible)) { 482c71a54b0SRob Herring kfree(desc); 483c71a54b0SRob Herring continue; 484c71a54b0SRob Herring } 485c71a54b0SRob Herring 486c71a54b0SRob Herring pr_debug("of_irq_init: init %s @ %p, parent %p\n", 487c71a54b0SRob Herring match->compatible, 488c71a54b0SRob Herring desc->dev, desc->interrupt_parent); 489d2e41518SKim Phillips irq_init_cb = (of_irq_init_cb_t)match->data; 490c71a54b0SRob Herring ret = irq_init_cb(desc->dev, desc->interrupt_parent); 491c71a54b0SRob Herring if (ret) { 492c71a54b0SRob Herring kfree(desc); 493c71a54b0SRob Herring continue; 494c71a54b0SRob Herring } 495c71a54b0SRob Herring 496c71a54b0SRob Herring /* 497c71a54b0SRob Herring * This one is now set up; add it to the parent list so 498c71a54b0SRob Herring * its children can get processed in a subsequent pass. 499c71a54b0SRob Herring */ 500c71a54b0SRob Herring list_add_tail(&desc->list, &intc_parent_list); 501c71a54b0SRob Herring } 502c71a54b0SRob Herring 503c71a54b0SRob Herring /* Get the next pending parent that might have children */ 504c0cdfaa0SAxel Lin desc = list_first_entry_or_null(&intc_parent_list, 505c0cdfaa0SAxel Lin typeof(*desc), list); 506c0cdfaa0SAxel Lin if (!desc) { 507c71a54b0SRob Herring pr_err("of_irq_init: children remain, but no parents\n"); 508c71a54b0SRob Herring break; 509c71a54b0SRob Herring } 510c71a54b0SRob Herring list_del(&desc->list); 511c71a54b0SRob Herring parent = desc->dev; 512c71a54b0SRob Herring kfree(desc); 513c71a54b0SRob Herring } 514c71a54b0SRob Herring 515c71a54b0SRob Herring list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) { 516c71a54b0SRob Herring list_del(&desc->list); 517c71a54b0SRob Herring kfree(desc); 518c71a54b0SRob Herring } 519c71a54b0SRob Herring err: 520c71a54b0SRob Herring list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) { 521c71a54b0SRob Herring list_del(&desc->list); 522c71a54b0SRob Herring kfree(desc); 523c71a54b0SRob Herring } 524c71a54b0SRob Herring } 525