prom_parse.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. #undef DEBUG
  2. #include <linux/kernel.h>
  3. #include <linux/string.h>
  4. #include <linux/pci_regs.h>
  5. #include <linux/module.h>
  6. #include <linux/ioport.h>
  7. #include <linux/etherdevice.h>
  8. #include <asm/prom.h>
  9. #include <asm/pci-bridge.h>
  10. #define PRu64 "%llx"
  11. /* Max address size we deal with */
  12. #define OF_MAX_ADDR_CELLS 4
  13. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  14. (ns) > 0)
  15. static struct of_bus *of_match_bus(struct device_node *np);
  16. static int __of_address_to_resource(struct device_node *dev,
  17. const u32 *addrp, u64 size, unsigned int flags,
  18. struct resource *r);
  19. /* Debug utility */
  20. #ifdef DEBUG
  21. static void of_dump_addr(const char *s, const u32 *addr, int na)
  22. {
  23. printk(KERN_INFO "%s", s);
  24. while (na--)
  25. printk(KERN_INFO " %08x", *(addr++));
  26. printk(KERN_INFO "\n");
  27. }
  28. #else
  29. static void of_dump_addr(const char *s, const u32 *addr, int na) { }
  30. #endif
  31. /* Callbacks for bus specific translators */
  32. struct of_bus {
  33. const char *name;
  34. const char *addresses;
  35. int (*match)(struct device_node *parent);
  36. void (*count_cells)(struct device_node *child,
  37. int *addrc, int *sizec);
  38. u64 (*map)(u32 *addr, const u32 *range,
  39. int na, int ns, int pna);
  40. int (*translate)(u32 *addr, u64 offset, int na);
  41. unsigned int (*get_flags)(const u32 *addr);
  42. };
  43. /*
  44. * Default translator (generic bus)
  45. */
  46. static void of_bus_default_count_cells(struct device_node *dev,
  47. int *addrc, int *sizec)
  48. {
  49. if (addrc)
  50. *addrc = of_n_addr_cells(dev);
  51. if (sizec)
  52. *sizec = of_n_size_cells(dev);
  53. }
  54. static u64 of_bus_default_map(u32 *addr, const u32 *range,
  55. int na, int ns, int pna)
  56. {
  57. u64 cp, s, da;
  58. cp = of_read_number(range, na);
  59. s = of_read_number(range + na + pna, ns);
  60. da = of_read_number(addr, na);
  61. pr_debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
  62. cp, s, da);
  63. if (da < cp || da >= (cp + s))
  64. return OF_BAD_ADDR;
  65. return da - cp;
  66. }
  67. static int of_bus_default_translate(u32 *addr, u64 offset, int na)
  68. {
  69. u64 a = of_read_number(addr, na);
  70. memset(addr, 0, na * 4);
  71. a += offset;
  72. if (na > 1)
  73. addr[na - 2] = a >> 32;
  74. addr[na - 1] = a & 0xffffffffu;
  75. return 0;
  76. }
  77. static unsigned int of_bus_default_get_flags(const u32 *addr)
  78. {
  79. return IORESOURCE_MEM;
  80. }
  81. #ifdef CONFIG_PCI
  82. /*
  83. * PCI bus specific translator
  84. */
  85. static int of_bus_pci_match(struct device_node *np)
  86. {
  87. /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
  88. return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
  89. }
  90. static void of_bus_pci_count_cells(struct device_node *np,
  91. int *addrc, int *sizec)
  92. {
  93. if (addrc)
  94. *addrc = 3;
  95. if (sizec)
  96. *sizec = 2;
  97. }
  98. static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  99. {
  100. u64 cp, s, da;
  101. /* Check address type match */
  102. if ((addr[0] ^ range[0]) & 0x03000000)
  103. return OF_BAD_ADDR;
  104. /* Read address values, skipping high cell */
  105. cp = of_read_number(range + 1, na - 1);
  106. s = of_read_number(range + na + pna, ns);
  107. da = of_read_number(addr + 1, na - 1);
  108. pr_debug("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  109. if (da < cp || da >= (cp + s))
  110. return OF_BAD_ADDR;
  111. return da - cp;
  112. }
  113. static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
  114. {
  115. return of_bus_default_translate(addr + 1, offset, na - 1);
  116. }
  117. static unsigned int of_bus_pci_get_flags(const u32 *addr)
  118. {
  119. unsigned int flags = 0;
  120. u32 w = addr[0];
  121. switch ((w >> 24) & 0x03) {
  122. case 0x01:
  123. flags |= IORESOURCE_IO;
  124. break;
  125. case 0x02: /* 32 bits */
  126. case 0x03: /* 64 bits */
  127. flags |= IORESOURCE_MEM;
  128. break;
  129. }
  130. if (w & 0x40000000)
  131. flags |= IORESOURCE_PREFETCH;
  132. return flags;
  133. }
  134. const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  135. unsigned int *flags)
  136. {
  137. const u32 *prop;
  138. unsigned int psize;
  139. struct device_node *parent;
  140. struct of_bus *bus;
  141. int onesize, i, na, ns;
  142. /* Get parent & match bus type */
  143. parent = of_get_parent(dev);
  144. if (parent == NULL)
  145. return NULL;
  146. bus = of_match_bus(parent);
  147. if (strcmp(bus->name, "pci")) {
  148. of_node_put(parent);
  149. return NULL;
  150. }
  151. bus->count_cells(dev, &na, &ns);
  152. of_node_put(parent);
  153. if (!OF_CHECK_COUNTS(na, ns))
  154. return NULL;
  155. /* Get "reg" or "assigned-addresses" property */
  156. prop = of_get_property(dev, bus->addresses, &psize);
  157. if (prop == NULL)
  158. return NULL;
  159. psize /= 4;
  160. onesize = na + ns;
  161. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  162. if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  163. if (size)
  164. *size = of_read_number(prop + na, ns);
  165. if (flags)
  166. *flags = bus->get_flags(prop);
  167. return prop;
  168. }
  169. return NULL;
  170. }
  171. EXPORT_SYMBOL(of_get_pci_address);
  172. int of_pci_address_to_resource(struct device_node *dev, int bar,
  173. struct resource *r)
  174. {
  175. const u32 *addrp;
  176. u64 size;
  177. unsigned int flags;
  178. addrp = of_get_pci_address(dev, bar, &size, &flags);
  179. if (addrp == NULL)
  180. return -EINVAL;
  181. return __of_address_to_resource(dev, addrp, size, flags, r);
  182. }
  183. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  184. static u8 of_irq_pci_swizzle(u8 slot, u8 pin)
  185. {
  186. return (((pin - 1) + slot) % 4) + 1;
  187. }
  188. int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
  189. {
  190. struct device_node *dn, *ppnode;
  191. struct pci_dev *ppdev;
  192. u32 lspec;
  193. u32 laddr[3];
  194. u8 pin;
  195. int rc;
  196. /* Check if we have a device node, if yes, fallback to standard OF
  197. * parsing
  198. */
  199. dn = pci_device_to_OF_node(pdev);
  200. if (dn)
  201. return of_irq_map_one(dn, 0, out_irq);
  202. /* Ok, we don't, time to have fun. Let's start by building up an
  203. * interrupt spec. we assume #interrupt-cells is 1, which is standard
  204. * for PCI. If you do different, then don't use that routine.
  205. */
  206. rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  207. if (rc != 0)
  208. return rc;
  209. /* No pin, exit */
  210. if (pin == 0)
  211. return -ENODEV;
  212. /* Now we walk up the PCI tree */
  213. lspec = pin;
  214. for (;;) {
  215. /* Get the pci_dev of our parent */
  216. ppdev = pdev->bus->self;
  217. /* Ouch, it's a host bridge... */
  218. if (ppdev == NULL) {
  219. struct pci_controller *host;
  220. host = pci_bus_to_host(pdev->bus);
  221. ppnode = host ? host->arch_data : NULL;
  222. /* No node for host bridge ? give up */
  223. if (ppnode == NULL)
  224. return -EINVAL;
  225. } else
  226. /* We found a P2P bridge, check if it has a node */
  227. ppnode = pci_device_to_OF_node(ppdev);
  228. /* Ok, we have found a parent with a device-node, hand over to
  229. * the OF parsing code.
  230. * We build a unit address from the linux device to be used for
  231. * resolution. Note that we use the linux bus number which may
  232. * not match your firmware bus numbering.
  233. * Fortunately, in most cases, interrupt-map-mask doesn't
  234. * include the bus number as part of the matching.
  235. * You should still be careful about that though if you intend
  236. * to rely on this function (you ship a firmware that doesn't
  237. * create device nodes for all PCI devices).
  238. */
  239. if (ppnode)
  240. break;
  241. /* We can only get here if we hit a P2P bridge with no node,
  242. * let's do standard swizzling and try again
  243. */
  244. lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec);
  245. pdev = ppdev;
  246. }
  247. laddr[0] = (pdev->bus->number << 16)
  248. | (pdev->devfn << 8);
  249. laddr[1] = laddr[2] = 0;
  250. return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq);
  251. }
  252. EXPORT_SYMBOL_GPL(of_irq_map_pci);
  253. #endif /* CONFIG_PCI */
  254. /*
  255. * ISA bus specific translator
  256. */
  257. static int of_bus_isa_match(struct device_node *np)
  258. {
  259. return !strcmp(np->name, "isa");
  260. }
  261. static void of_bus_isa_count_cells(struct device_node *child,
  262. int *addrc, int *sizec)
  263. {
  264. if (addrc)
  265. *addrc = 2;
  266. if (sizec)
  267. *sizec = 1;
  268. }
  269. static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  270. {
  271. u64 cp, s, da;
  272. /* Check address type match */
  273. if ((addr[0] ^ range[0]) & 0x00000001)
  274. return OF_BAD_ADDR;
  275. /* Read address values, skipping high cell */
  276. cp = of_read_number(range + 1, na - 1);
  277. s = of_read_number(range + na + pna, ns);
  278. da = of_read_number(addr + 1, na - 1);
  279. pr_debug("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  280. if (da < cp || da >= (cp + s))
  281. return OF_BAD_ADDR;
  282. return da - cp;
  283. }
  284. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  285. {
  286. return of_bus_default_translate(addr + 1, offset, na - 1);
  287. }
  288. static unsigned int of_bus_isa_get_flags(const u32 *addr)
  289. {
  290. unsigned int flags = 0;
  291. u32 w = addr[0];
  292. if (w & 1)
  293. flags |= IORESOURCE_IO;
  294. else
  295. flags |= IORESOURCE_MEM;
  296. return flags;
  297. }
  298. /*
  299. * Array of bus specific translators
  300. */
  301. static struct of_bus of_busses[] = {
  302. #ifdef CONFIG_PCI
  303. /* PCI */
  304. {
  305. .name = "pci",
  306. .addresses = "assigned-addresses",
  307. .match = of_bus_pci_match,
  308. .count_cells = of_bus_pci_count_cells,
  309. .map = of_bus_pci_map,
  310. .translate = of_bus_pci_translate,
  311. .get_flags = of_bus_pci_get_flags,
  312. },
  313. #endif /* CONFIG_PCI */
  314. /* ISA */
  315. {
  316. .name = "isa",
  317. .addresses = "reg",
  318. .match = of_bus_isa_match,
  319. .count_cells = of_bus_isa_count_cells,
  320. .map = of_bus_isa_map,
  321. .translate = of_bus_isa_translate,
  322. .get_flags = of_bus_isa_get_flags,
  323. },
  324. /* Default */
  325. {
  326. .name = "default",
  327. .addresses = "reg",
  328. .match = NULL,
  329. .count_cells = of_bus_default_count_cells,
  330. .map = of_bus_default_map,
  331. .translate = of_bus_default_translate,
  332. .get_flags = of_bus_default_get_flags,
  333. },
  334. };
  335. static struct of_bus *of_match_bus(struct device_node *np)
  336. {
  337. int i;
  338. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  339. if (!of_busses[i].match || of_busses[i].match(np))
  340. return &of_busses[i];
  341. BUG();
  342. return NULL;
  343. }
  344. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  345. struct of_bus *pbus, u32 *addr,
  346. int na, int ns, int pna)
  347. {
  348. const u32 *ranges;
  349. unsigned int rlen;
  350. int rone;
  351. u64 offset = OF_BAD_ADDR;
  352. /* Normally, an absence of a "ranges" property means we are
  353. * crossing a non-translatable boundary, and thus the addresses
  354. * below the current not cannot be converted to CPU physical ones.
  355. * Unfortunately, while this is very clear in the spec, it's not
  356. * what Apple understood, and they do have things like /uni-n or
  357. * /ht nodes with no "ranges" property and a lot of perfectly
  358. * useable mapped devices below them. Thus we treat the absence of
  359. * "ranges" as equivalent to an empty "ranges" property which means
  360. * a 1:1 translation at that level. It's up to the caller not to try
  361. * to translate addresses that aren't supposed to be translated in
  362. * the first place. --BenH.
  363. */
  364. ranges = of_get_property(parent, "ranges", (int *) &rlen);
  365. if (ranges == NULL || rlen == 0) {
  366. offset = of_read_number(addr, na);
  367. memset(addr, 0, pna * 4);
  368. pr_debug("OF: no ranges, 1:1 translation\n");
  369. goto finish;
  370. }
  371. pr_debug("OF: walking ranges...\n");
  372. /* Now walk through the ranges */
  373. rlen /= 4;
  374. rone = na + pna + ns;
  375. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  376. offset = bus->map(addr, ranges, na, ns, pna);
  377. if (offset != OF_BAD_ADDR)
  378. break;
  379. }
  380. if (offset == OF_BAD_ADDR) {
  381. pr_debug("OF: not found !\n");
  382. return 1;
  383. }
  384. memcpy(addr, ranges + na, 4 * pna);
  385. finish:
  386. of_dump_addr("OF: parent translation for:", addr, pna);
  387. pr_debug("OF: with offset: "PRu64"\n", offset);
  388. /* Translate it into parent bus space */
  389. return pbus->translate(addr, offset, pna);
  390. }
  391. /*
  392. * Translate an address from the device-tree into a CPU physical address,
  393. * this walks up the tree and applies the various bus mappings on the
  394. * way.
  395. *
  396. * Note: We consider that crossing any level with #size-cells == 0 to mean
  397. * that translation is impossible (that is we are not dealing with a value
  398. * that can be mapped to a cpu physical address). This is not really specified
  399. * that way, but this is traditionally the way IBM at least do things
  400. */
  401. u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
  402. {
  403. struct device_node *parent = NULL;
  404. struct of_bus *bus, *pbus;
  405. u32 addr[OF_MAX_ADDR_CELLS];
  406. int na, ns, pna, pns;
  407. u64 result = OF_BAD_ADDR;
  408. pr_debug("OF: ** translation for device %s **\n", dev->full_name);
  409. /* Increase refcount at current level */
  410. of_node_get(dev);
  411. /* Get parent & match bus type */
  412. parent = of_get_parent(dev);
  413. if (parent == NULL)
  414. goto bail;
  415. bus = of_match_bus(parent);
  416. /* Cound address cells & copy address locally */
  417. bus->count_cells(dev, &na, &ns);
  418. if (!OF_CHECK_COUNTS(na, ns)) {
  419. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  420. dev->full_name);
  421. goto bail;
  422. }
  423. memcpy(addr, in_addr, na * 4);
  424. pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  425. bus->name, na, ns, parent->full_name);
  426. of_dump_addr("OF: translating address:", addr, na);
  427. /* Translate */
  428. for (;;) {
  429. /* Switch to parent bus */
  430. of_node_put(dev);
  431. dev = parent;
  432. parent = of_get_parent(dev);
  433. /* If root, we have finished */
  434. if (parent == NULL) {
  435. pr_debug("OF: reached root node\n");
  436. result = of_read_number(addr, na);
  437. break;
  438. }
  439. /* Get new parent bus and counts */
  440. pbus = of_match_bus(parent);
  441. pbus->count_cells(dev, &pna, &pns);
  442. if (!OF_CHECK_COUNTS(pna, pns)) {
  443. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  444. dev->full_name);
  445. break;
  446. }
  447. pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  448. pbus->name, pna, pns, parent->full_name);
  449. /* Apply bus translation */
  450. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna))
  451. break;
  452. /* Complete the move up one level */
  453. na = pna;
  454. ns = pns;
  455. bus = pbus;
  456. of_dump_addr("OF: one level translation:", addr, na);
  457. }
  458. bail:
  459. of_node_put(parent);
  460. of_node_put(dev);
  461. return result;
  462. }
  463. EXPORT_SYMBOL(of_translate_address);
  464. const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
  465. unsigned int *flags)
  466. {
  467. const u32 *prop;
  468. unsigned int psize;
  469. struct device_node *parent;
  470. struct of_bus *bus;
  471. int onesize, i, na, ns;
  472. /* Get parent & match bus type */
  473. parent = of_get_parent(dev);
  474. if (parent == NULL)
  475. return NULL;
  476. bus = of_match_bus(parent);
  477. bus->count_cells(dev, &na, &ns);
  478. of_node_put(parent);
  479. if (!OF_CHECK_COUNTS(na, ns))
  480. return NULL;
  481. /* Get "reg" or "assigned-addresses" property */
  482. prop = of_get_property(dev, bus->addresses, (int *) &psize);
  483. if (prop == NULL)
  484. return NULL;
  485. psize /= 4;
  486. onesize = na + ns;
  487. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  488. if (i == index) {
  489. if (size)
  490. *size = of_read_number(prop + na, ns);
  491. if (flags)
  492. *flags = bus->get_flags(prop);
  493. return prop;
  494. }
  495. return NULL;
  496. }
  497. EXPORT_SYMBOL(of_get_address);
  498. static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
  499. u64 size, unsigned int flags,
  500. struct resource *r)
  501. {
  502. u64 taddr;
  503. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  504. return -EINVAL;
  505. taddr = of_translate_address(dev, addrp);
  506. if (taddr == OF_BAD_ADDR)
  507. return -EINVAL;
  508. memset(r, 0, sizeof(struct resource));
  509. if (flags & IORESOURCE_IO) {
  510. unsigned long port;
  511. port = -1; /* pci_address_to_pio(taddr); */
  512. if (port == (unsigned long)-1)
  513. return -EINVAL;
  514. r->start = port;
  515. r->end = port + size - 1;
  516. } else {
  517. r->start = taddr;
  518. r->end = taddr + size - 1;
  519. }
  520. r->flags = flags;
  521. r->name = dev->name;
  522. return 0;
  523. }
  524. int of_address_to_resource(struct device_node *dev, int index,
  525. struct resource *r)
  526. {
  527. const u32 *addrp;
  528. u64 size;
  529. unsigned int flags;
  530. addrp = of_get_address(dev, index, &size, &flags);
  531. if (addrp == NULL)
  532. return -EINVAL;
  533. return __of_address_to_resource(dev, addrp, size, flags, r);
  534. }
  535. EXPORT_SYMBOL_GPL(of_address_to_resource);
  536. void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
  537. unsigned long *busno, unsigned long *phys, unsigned long *size)
  538. {
  539. const u32 *dma_window;
  540. u32 cells;
  541. const unsigned char *prop;
  542. dma_window = dma_window_prop;
  543. /* busno is always one cell */
  544. *busno = *(dma_window++);
  545. prop = of_get_property(dn, "ibm,#dma-address-cells", NULL);
  546. if (!prop)
  547. prop = of_get_property(dn, "#address-cells", NULL);
  548. cells = prop ? *(u32 *)prop : of_n_addr_cells(dn);
  549. *phys = of_read_number(dma_window, cells);
  550. dma_window += cells;
  551. prop = of_get_property(dn, "ibm,#dma-size-cells", NULL);
  552. cells = prop ? *(u32 *)prop : of_n_size_cells(dn);
  553. *size = of_read_number(dma_window, cells);
  554. }
  555. /*
  556. * Interrupt remapper
  557. */
  558. static unsigned int of_irq_workarounds;
  559. static struct device_node *of_irq_dflt_pic;
  560. static struct device_node *of_irq_find_parent(struct device_node *child)
  561. {
  562. struct device_node *p;
  563. const phandle *parp;
  564. if (!of_node_get(child))
  565. return NULL;
  566. do {
  567. parp = of_get_property(child, "interrupt-parent", NULL);
  568. if (parp == NULL)
  569. p = of_get_parent(child);
  570. else {
  571. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  572. p = of_node_get(of_irq_dflt_pic);
  573. else
  574. p = of_find_node_by_phandle(*parp);
  575. }
  576. of_node_put(child);
  577. child = p;
  578. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  579. return p;
  580. }
  581. /* This doesn't need to be called if you don't have any special workaround
  582. * flags to pass
  583. */
  584. void of_irq_map_init(unsigned int flags)
  585. {
  586. of_irq_workarounds = flags;
  587. /* OldWorld, don't bother looking at other things */
  588. if (flags & OF_IMAP_OLDWORLD_MAC)
  589. return;
  590. /* If we don't have phandles, let's try to locate a default interrupt
  591. * controller (happens when booting with BootX). We do a first match
  592. * here, hopefully, that only ever happens on machines with one
  593. * controller.
  594. */
  595. if (flags & OF_IMAP_NO_PHANDLE) {
  596. struct device_node *np;
  597. for (np = NULL; (np = of_find_all_nodes(np)) != NULL;) {
  598. if (of_get_property(np, "interrupt-controller", NULL)
  599. == NULL)
  600. continue;
  601. /* Skip /chosen/interrupt-controller */
  602. if (strcmp(np->name, "chosen") == 0)
  603. continue;
  604. /* It seems like at least one person on this planet
  605. * wants to use BootX on a machine with an AppleKiwi
  606. * controller which happens to pretend to be an
  607. * interrupt controller too.
  608. */
  609. if (strcmp(np->name, "AppleKiwi") == 0)
  610. continue;
  611. /* I think we found one ! */
  612. of_irq_dflt_pic = np;
  613. break;
  614. }
  615. }
  616. }
  617. int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize,
  618. const u32 *addr, struct of_irq *out_irq)
  619. {
  620. struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
  621. const u32 *tmp, *imap, *imask;
  622. u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
  623. int imaplen, match, i;
  624. pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],"
  625. "ointsize=%d\n",
  626. parent->full_name, intspec[0], intspec[1], ointsize);
  627. ipar = of_node_get(parent);
  628. /* First get the #interrupt-cells property of the current cursor
  629. * that tells us how to interpret the passed-in intspec. If there
  630. * is none, we are nice and just walk up the tree
  631. */
  632. do {
  633. tmp = of_get_property(ipar, "#interrupt-cells", NULL);
  634. if (tmp != NULL) {
  635. intsize = *tmp;
  636. break;
  637. }
  638. tnode = ipar;
  639. ipar = of_irq_find_parent(ipar);
  640. of_node_put(tnode);
  641. } while (ipar);
  642. if (ipar == NULL) {
  643. pr_debug(" -> no parent found !\n");
  644. goto fail;
  645. }
  646. pr_debug("of_irq_map_raw: ipar=%s, size=%d\n",
  647. ipar->full_name, intsize);
  648. if (ointsize != intsize)
  649. return -EINVAL;
  650. /* Look for this #address-cells. We have to implement the old linux
  651. * trick of looking for the parent here as some device-trees rely on it
  652. */
  653. old = of_node_get(ipar);
  654. do {
  655. tmp = of_get_property(old, "#address-cells", NULL);
  656. tnode = of_get_parent(old);
  657. of_node_put(old);
  658. old = tnode;
  659. } while (old && tmp == NULL);
  660. of_node_put(old);
  661. old = NULL;
  662. addrsize = (tmp == NULL) ? 2 : *tmp;
  663. pr_debug(" -> addrsize=%d\n", addrsize);
  664. /* Now start the actual "proper" walk of the interrupt tree */
  665. while (ipar != NULL) {
  666. /* Now check if cursor is an interrupt-controller and if it is
  667. * then we are done
  668. */
  669. if (of_get_property(ipar, "interrupt-controller", NULL) !=
  670. NULL) {
  671. pr_debug(" -> got it !\n");
  672. memcpy(out_irq->specifier, intspec,
  673. intsize * sizeof(u32));
  674. out_irq->size = intsize;
  675. out_irq->controller = ipar;
  676. of_node_put(old);
  677. return 0;
  678. }
  679. /* Now look for an interrupt-map */
  680. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  681. /* No interrupt map, check for an interrupt parent */
  682. if (imap == NULL) {
  683. pr_debug(" -> no map, getting parent\n");
  684. newpar = of_irq_find_parent(ipar);
  685. goto skiplevel;
  686. }
  687. imaplen /= sizeof(u32);
  688. /* Look for a mask */
  689. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  690. /* If we were passed no "reg" property and we attempt to parse
  691. * an interrupt-map, then #address-cells must be 0.
  692. * Fail if it's not.
  693. */
  694. if (addr == NULL && addrsize != 0) {
  695. pr_debug(" -> no reg passed in when needed !\n");
  696. goto fail;
  697. }
  698. /* Parse interrupt-map */
  699. match = 0;
  700. while (imaplen > (addrsize + intsize + 1) && !match) {
  701. /* Compare specifiers */
  702. match = 1;
  703. for (i = 0; i < addrsize && match; ++i) {
  704. u32 mask = imask ? imask[i] : 0xffffffffu;
  705. match = ((addr[i] ^ imap[i]) & mask) == 0;
  706. }
  707. for (; i < (addrsize + intsize) && match; ++i) {
  708. u32 mask = imask ? imask[i] : 0xffffffffu;
  709. match =
  710. ((intspec[i-addrsize] ^ imap[i])
  711. & mask) == 0;
  712. }
  713. imap += addrsize + intsize;
  714. imaplen -= addrsize + intsize;
  715. pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
  716. /* Get the interrupt parent */
  717. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  718. newpar = of_node_get(of_irq_dflt_pic);
  719. else
  720. newpar =
  721. of_find_node_by_phandle((phandle)*imap);
  722. imap++;
  723. --imaplen;
  724. /* Check if not found */
  725. if (newpar == NULL) {
  726. pr_debug(" -> imap parent not found !\n");
  727. goto fail;
  728. }
  729. /* Get #interrupt-cells and #address-cells of new
  730. * parent
  731. */
  732. tmp = of_get_property(newpar, "#interrupt-cells", NULL);
  733. if (tmp == NULL) {
  734. pr_debug(" -> parent lacks "
  735. "#interrupt-cells!\n");
  736. goto fail;
  737. }
  738. newintsize = *tmp;
  739. tmp = of_get_property(newpar, "#address-cells", NULL);
  740. newaddrsize = (tmp == NULL) ? 0 : *tmp;
  741. pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
  742. newintsize, newaddrsize);
  743. /* Check for malformed properties */
  744. if (imaplen < (newaddrsize + newintsize))
  745. goto fail;
  746. imap += newaddrsize + newintsize;
  747. imaplen -= newaddrsize + newintsize;
  748. pr_debug(" -> imaplen=%d\n", imaplen);
  749. }
  750. if (!match)
  751. goto fail;
  752. of_node_put(old);
  753. old = of_node_get(newpar);
  754. addrsize = newaddrsize;
  755. intsize = newintsize;
  756. intspec = imap - intsize;
  757. addr = intspec - addrsize;
  758. skiplevel:
  759. /* Iterate again with new parent */
  760. pr_debug(" -> new parent: %s\n",
  761. newpar ? newpar->full_name : "<>");
  762. of_node_put(ipar);
  763. ipar = newpar;
  764. newpar = NULL;
  765. }
  766. fail:
  767. of_node_put(ipar);
  768. of_node_put(old);
  769. of_node_put(newpar);
  770. return -EINVAL;
  771. }
  772. EXPORT_SYMBOL_GPL(of_irq_map_raw);
  773. int of_irq_map_one(struct device_node *device,
  774. int index, struct of_irq *out_irq)
  775. {
  776. struct device_node *p;
  777. const u32 *intspec, *tmp, *addr;
  778. u32 intsize, intlen;
  779. int res;
  780. pr_debug("of_irq_map_one: dev=%s, index=%d\n",
  781. device->full_name, index);
  782. /* Get the interrupts property */
  783. intspec = of_get_property(device, "interrupts", (int *) &intlen);
  784. if (intspec == NULL)
  785. return -EINVAL;
  786. intlen /= sizeof(u32);
  787. pr_debug(" intspec=%d intlen=%d\n", *intspec, intlen);
  788. /* Get the reg property (if any) */
  789. addr = of_get_property(device, "reg", NULL);
  790. /* Look for the interrupt parent. */
  791. p = of_irq_find_parent(device);
  792. if (p == NULL)
  793. return -EINVAL;
  794. /* Get size of interrupt specifier */
  795. tmp = of_get_property(p, "#interrupt-cells", NULL);
  796. if (tmp == NULL) {
  797. of_node_put(p);
  798. return -EINVAL;
  799. }
  800. intsize = *tmp;
  801. pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
  802. /* Check index */
  803. if ((index + 1) * intsize > intlen)
  804. return -EINVAL;
  805. /* Get new specifier and map it */
  806. res = of_irq_map_raw(p, intspec + index * intsize, intsize,
  807. addr, out_irq);
  808. of_node_put(p);
  809. return res;
  810. }
  811. EXPORT_SYMBOL_GPL(of_irq_map_one);
  812. /**
  813. * Search the device tree for the best MAC address to use. 'mac-address' is
  814. * checked first, because that is supposed to contain to "most recent" MAC
  815. * address. If that isn't set, then 'local-mac-address' is checked next,
  816. * because that is the default address. If that isn't set, then the obsolete
  817. * 'address' is checked, just in case we're using an old device tree.
  818. *
  819. * Note that the 'address' property is supposed to contain a virtual address of
  820. * the register set, but some DTS files have redefined that property to be the
  821. * MAC address.
  822. *
  823. * All-zero MAC addresses are rejected, because those could be properties that
  824. * exist in the device tree, but were not set by U-Boot. For example, the
  825. * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
  826. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  827. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  828. * but is all zeros.
  829. */
  830. const void *of_get_mac_address(struct device_node *np)
  831. {
  832. struct property *pp;
  833. pp = of_find_property(np, "mac-address", NULL);
  834. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  835. return pp->value;
  836. pp = of_find_property(np, "local-mac-address", NULL);
  837. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  838. return pp->value;
  839. pp = of_find_property(np, "address", NULL);
  840. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  841. return pp->value;
  842. return NULL;
  843. }
  844. EXPORT_SYMBOL(of_get_mac_address);
  845. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  846. {
  847. struct of_irq out_irq;
  848. int irq;
  849. int res;
  850. res = of_irq_map_one(dev, index, &out_irq);
  851. /* Get irq for the device */
  852. if (res) {
  853. pr_debug("IRQ not found... code = %d", res);
  854. return NO_IRQ;
  855. }
  856. /* Assuming single interrupt controller... */
  857. irq = out_irq.specifier[0];
  858. pr_debug("IRQ found = %d", irq);
  859. /* Only dereference the resource if both the
  860. * resource and the irq are valid. */
  861. if (r && irq != NO_IRQ) {
  862. r->start = r->end = irq;
  863. r->flags = IORESOURCE_IRQ;
  864. }
  865. return irq;
  866. }
  867. EXPORT_SYMBOL_GPL(of_irq_to_resource);
  868. void __iomem *of_iomap(struct device_node *np, int index)
  869. {
  870. struct resource res;
  871. if (of_address_to_resource(np, index, &res))
  872. return NULL;
  873. return ioremap(res.start, 1 + res.end - res.start);
  874. }
  875. EXPORT_SYMBOL(of_iomap);