irq.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. #include <linux/errno.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/string.h>
  25. /**
  26. * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
  27. * @device: Device node of the device whose interrupt is to be mapped
  28. * @index: Index of the interrupt to map
  29. *
  30. * This function is a wrapper that chains of_irq_map_one() and
  31. * irq_create_of_mapping() to make things easier to callers
  32. */
  33. unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  34. {
  35. struct of_irq oirq;
  36. if (of_irq_map_one(dev, index, &oirq))
  37. return NO_IRQ;
  38. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  39. oirq.size);
  40. }
  41. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  42. /**
  43. * of_irq_find_parent - Given a device node, find its interrupt parent node
  44. * @child: pointer to device node
  45. *
  46. * Returns a pointer to the interrupt parent node, or NULL if the interrupt
  47. * parent could not be determined.
  48. */
  49. static struct device_node *of_irq_find_parent(struct device_node *child)
  50. {
  51. struct device_node *p;
  52. const phandle *parp;
  53. if (!of_node_get(child))
  54. return NULL;
  55. do {
  56. parp = of_get_property(child, "interrupt-parent", NULL);
  57. if (parp == NULL)
  58. p = of_get_parent(child);
  59. else {
  60. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  61. p = of_node_get(of_irq_dflt_pic);
  62. else
  63. p = of_find_node_by_phandle(*parp);
  64. }
  65. of_node_put(child);
  66. child = p;
  67. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  68. return p;
  69. }
  70. /**
  71. * of_irq_map_raw - Low level interrupt tree parsing
  72. * @parent: the device interrupt parent
  73. * @intspec: interrupt specifier ("interrupts" property of the device)
  74. * @ointsize: size of the passed in interrupt specifier
  75. * @addr: address specifier (start of "reg" property of the device)
  76. * @out_irq: structure of_irq filled by this function
  77. *
  78. * Returns 0 on success and a negative number on error
  79. *
  80. * This function is a low-level interrupt tree walking function. It
  81. * can be used to do a partial walk with synthetized reg and interrupts
  82. * properties, for example when resolving PCI interrupts when no device
  83. * node exist for the parent.
  84. */
  85. int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize,
  86. const u32 *addr, struct of_irq *out_irq)
  87. {
  88. struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
  89. const u32 *tmp, *imap, *imask;
  90. u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
  91. int imaplen, match, i;
  92. pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
  93. parent->full_name, intspec[0], intspec[1], ointsize);
  94. ipar = of_node_get(parent);
  95. /* First get the #interrupt-cells property of the current cursor
  96. * that tells us how to interpret the passed-in intspec. If there
  97. * is none, we are nice and just walk up the tree
  98. */
  99. do {
  100. tmp = of_get_property(ipar, "#interrupt-cells", NULL);
  101. if (tmp != NULL) {
  102. intsize = *tmp;
  103. break;
  104. }
  105. tnode = ipar;
  106. ipar = of_irq_find_parent(ipar);
  107. of_node_put(tnode);
  108. } while (ipar);
  109. if (ipar == NULL) {
  110. pr_debug(" -> no parent found !\n");
  111. goto fail;
  112. }
  113. pr_debug("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
  114. if (ointsize != intsize)
  115. return -EINVAL;
  116. /* Look for this #address-cells. We have to implement the old linux
  117. * trick of looking for the parent here as some device-trees rely on it
  118. */
  119. old = of_node_get(ipar);
  120. do {
  121. tmp = of_get_property(old, "#address-cells", NULL);
  122. tnode = of_get_parent(old);
  123. of_node_put(old);
  124. old = tnode;
  125. } while (old && tmp == NULL);
  126. of_node_put(old);
  127. old = NULL;
  128. addrsize = (tmp == NULL) ? 2 : *tmp;
  129. pr_debug(" -> addrsize=%d\n", addrsize);
  130. /* Now start the actual "proper" walk of the interrupt tree */
  131. while (ipar != NULL) {
  132. /* Now check if cursor is an interrupt-controller and if it is
  133. * then we are done
  134. */
  135. if (of_get_property(ipar, "interrupt-controller", NULL) !=
  136. NULL) {
  137. pr_debug(" -> got it !\n");
  138. memcpy(out_irq->specifier, intspec,
  139. intsize * sizeof(u32));
  140. out_irq->size = intsize;
  141. out_irq->controller = ipar;
  142. of_node_put(old);
  143. return 0;
  144. }
  145. /* Now look for an interrupt-map */
  146. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  147. /* No interrupt map, check for an interrupt parent */
  148. if (imap == NULL) {
  149. pr_debug(" -> no map, getting parent\n");
  150. newpar = of_irq_find_parent(ipar);
  151. goto skiplevel;
  152. }
  153. imaplen /= sizeof(u32);
  154. /* Look for a mask */
  155. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  156. /* If we were passed no "reg" property and we attempt to parse
  157. * an interrupt-map, then #address-cells must be 0.
  158. * Fail if it's not.
  159. */
  160. if (addr == NULL && addrsize != 0) {
  161. pr_debug(" -> no reg passed in when needed !\n");
  162. goto fail;
  163. }
  164. /* Parse interrupt-map */
  165. match = 0;
  166. while (imaplen > (addrsize + intsize + 1) && !match) {
  167. /* Compare specifiers */
  168. match = 1;
  169. for (i = 0; i < addrsize && match; ++i) {
  170. u32 mask = imask ? imask[i] : 0xffffffffu;
  171. match = ((addr[i] ^ imap[i]) & mask) == 0;
  172. }
  173. for (; i < (addrsize + intsize) && match; ++i) {
  174. u32 mask = imask ? imask[i] : 0xffffffffu;
  175. match =
  176. ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
  177. }
  178. imap += addrsize + intsize;
  179. imaplen -= addrsize + intsize;
  180. pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
  181. /* Get the interrupt parent */
  182. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  183. newpar = of_node_get(of_irq_dflt_pic);
  184. else
  185. newpar = of_find_node_by_phandle((phandle)*imap);
  186. imap++;
  187. --imaplen;
  188. /* Check if not found */
  189. if (newpar == NULL) {
  190. pr_debug(" -> imap parent not found !\n");
  191. goto fail;
  192. }
  193. /* Get #interrupt-cells and #address-cells of new
  194. * parent
  195. */
  196. tmp = of_get_property(newpar, "#interrupt-cells", NULL);
  197. if (tmp == NULL) {
  198. pr_debug(" -> parent lacks #interrupt-cells!\n");
  199. goto fail;
  200. }
  201. newintsize = *tmp;
  202. tmp = of_get_property(newpar, "#address-cells", NULL);
  203. newaddrsize = (tmp == NULL) ? 0 : *tmp;
  204. pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
  205. newintsize, newaddrsize);
  206. /* Check for malformed properties */
  207. if (imaplen < (newaddrsize + newintsize))
  208. goto fail;
  209. imap += newaddrsize + newintsize;
  210. imaplen -= newaddrsize + newintsize;
  211. pr_debug(" -> imaplen=%d\n", imaplen);
  212. }
  213. if (!match)
  214. goto fail;
  215. of_node_put(old);
  216. old = of_node_get(newpar);
  217. addrsize = newaddrsize;
  218. intsize = newintsize;
  219. intspec = imap - intsize;
  220. addr = intspec - addrsize;
  221. skiplevel:
  222. /* Iterate again with new parent */
  223. pr_debug(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
  224. of_node_put(ipar);
  225. ipar = newpar;
  226. newpar = NULL;
  227. }
  228. fail:
  229. of_node_put(ipar);
  230. of_node_put(old);
  231. of_node_put(newpar);
  232. return -EINVAL;
  233. }
  234. EXPORT_SYMBOL_GPL(of_irq_map_raw);
  235. /**
  236. * of_irq_map_one - Resolve an interrupt for a device
  237. * @device: the device whose interrupt is to be resolved
  238. * @index: index of the interrupt to resolve
  239. * @out_irq: structure of_irq filled by this function
  240. *
  241. * This function resolves an interrupt, walking the tree, for a given
  242. * device-tree node. It's the high level pendant to of_irq_map_raw().
  243. */
  244. int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
  245. {
  246. struct device_node *p;
  247. const u32 *intspec, *tmp, *addr;
  248. u32 intsize, intlen;
  249. int res = -EINVAL;
  250. pr_debug("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
  251. /* OldWorld mac stuff is "special", handle out of line */
  252. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  253. return of_irq_map_oldworld(device, index, out_irq);
  254. /* Get the interrupts property */
  255. intspec = of_get_property(device, "interrupts", &intlen);
  256. if (intspec == NULL)
  257. return -EINVAL;
  258. intlen /= sizeof(u32);
  259. pr_debug(" intspec=%d intlen=%d\n", *intspec, intlen);
  260. /* Get the reg property (if any) */
  261. addr = of_get_property(device, "reg", NULL);
  262. /* Look for the interrupt parent. */
  263. p = of_irq_find_parent(device);
  264. if (p == NULL)
  265. return -EINVAL;
  266. /* Get size of interrupt specifier */
  267. tmp = of_get_property(p, "#interrupt-cells", NULL);
  268. if (tmp == NULL)
  269. goto out;
  270. intsize = *tmp;
  271. pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
  272. /* Check index */
  273. if ((index + 1) * intsize > intlen)
  274. goto out;
  275. /* Get new specifier and map it */
  276. res = of_irq_map_raw(p, intspec + index * intsize, intsize,
  277. addr, out_irq);
  278. out:
  279. of_node_put(p);
  280. return res;
  281. }
  282. EXPORT_SYMBOL_GPL(of_irq_map_one);
  283. /**
  284. * of_irq_to_resource - Decode a node's IRQ and return it as a resource
  285. * @dev: pointer to device tree node
  286. * @index: zero-based index of the irq
  287. * @r: pointer to resource structure to return result into.
  288. */
  289. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  290. {
  291. int irq = irq_of_parse_and_map(dev, index);
  292. /* Only dereference the resource if both the
  293. * resource and the irq are valid. */
  294. if (r && irq != NO_IRQ) {
  295. r->start = r->end = irq;
  296. r->flags = IORESOURCE_IRQ;
  297. }
  298. return irq;
  299. }
  300. EXPORT_SYMBOL_GPL(of_irq_to_resource);