prom_parse.c 26 KB

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