prom_parse.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 unsigned int of_bus_pci_get_flags(const u32 *addr)
  108. {
  109. unsigned int flags = 0;
  110. u32 w = addr[0];
  111. switch((w >> 24) & 0x03) {
  112. case 0x01:
  113. flags |= IORESOURCE_IO;
  114. break;
  115. case 0x02: /* 32 bits */
  116. case 0x03: /* 64 bits */
  117. flags |= IORESOURCE_MEM;
  118. break;
  119. }
  120. if (w & 0x40000000)
  121. flags |= IORESOURCE_PREFETCH;
  122. return flags;
  123. }
  124. static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  125. {
  126. u64 cp, s, da;
  127. unsigned int af, rf;
  128. af = of_bus_pci_get_flags(addr);
  129. rf = of_bus_pci_get_flags(range);
  130. /* Check address type match */
  131. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  132. return OF_BAD_ADDR;
  133. /* Read address values, skipping high cell */
  134. cp = of_read_number(range + 1, na - 1);
  135. s = of_read_number(range + na + pna, ns);
  136. da = of_read_number(addr + 1, na - 1);
  137. DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  138. if (da < cp || da >= (cp + s))
  139. return OF_BAD_ADDR;
  140. return da - cp;
  141. }
  142. static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
  143. {
  144. return of_bus_default_translate(addr + 1, offset, na - 1);
  145. }
  146. const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  147. unsigned int *flags)
  148. {
  149. const u32 *prop;
  150. unsigned int psize;
  151. struct device_node *parent;
  152. struct of_bus *bus;
  153. int onesize, i, na, ns;
  154. /* Get parent & match bus type */
  155. parent = of_get_parent(dev);
  156. if (parent == NULL)
  157. return NULL;
  158. bus = of_match_bus(parent);
  159. if (strcmp(bus->name, "pci")) {
  160. of_node_put(parent);
  161. return NULL;
  162. }
  163. bus->count_cells(dev, &na, &ns);
  164. of_node_put(parent);
  165. if (!OF_CHECK_COUNTS(na, ns))
  166. return NULL;
  167. /* Get "reg" or "assigned-addresses" property */
  168. prop = of_get_property(dev, bus->addresses, &psize);
  169. if (prop == NULL)
  170. return NULL;
  171. psize /= 4;
  172. onesize = na + ns;
  173. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  174. if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  175. if (size)
  176. *size = of_read_number(prop + na, ns);
  177. if (flags)
  178. *flags = bus->get_flags(prop);
  179. return prop;
  180. }
  181. return NULL;
  182. }
  183. EXPORT_SYMBOL(of_get_pci_address);
  184. int of_pci_address_to_resource(struct device_node *dev, int bar,
  185. struct resource *r)
  186. {
  187. const u32 *addrp;
  188. u64 size;
  189. unsigned int flags;
  190. addrp = of_get_pci_address(dev, bar, &size, &flags);
  191. if (addrp == NULL)
  192. return -EINVAL;
  193. return __of_address_to_resource(dev, addrp, size, flags, r);
  194. }
  195. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  196. int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
  197. {
  198. struct device_node *dn, *ppnode;
  199. struct pci_dev *ppdev;
  200. u32 lspec;
  201. u32 laddr[3];
  202. u8 pin;
  203. int rc;
  204. /* Check if we have a device node, if yes, fallback to standard OF
  205. * parsing
  206. */
  207. dn = pci_device_to_OF_node(pdev);
  208. if (dn) {
  209. rc = of_irq_map_one(dn, 0, out_irq);
  210. if (!rc)
  211. return rc;
  212. }
  213. /* Ok, we don't, time to have fun. Let's start by building up an
  214. * interrupt spec. we assume #interrupt-cells is 1, which is standard
  215. * for PCI. If you do different, then don't use that routine.
  216. */
  217. rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  218. if (rc != 0)
  219. return rc;
  220. /* No pin, exit */
  221. if (pin == 0)
  222. return -ENODEV;
  223. /* Now we walk up the PCI tree */
  224. lspec = pin;
  225. for (;;) {
  226. /* Get the pci_dev of our parent */
  227. ppdev = pdev->bus->self;
  228. /* Ouch, it's a host bridge... */
  229. if (ppdev == NULL) {
  230. #ifdef CONFIG_PPC64
  231. ppnode = pci_bus_to_OF_node(pdev->bus);
  232. #else
  233. struct pci_controller *host;
  234. host = pci_bus_to_host(pdev->bus);
  235. ppnode = host ? host->dn : NULL;
  236. #endif
  237. /* No node for host bridge ? give up */
  238. if (ppnode == NULL)
  239. return -EINVAL;
  240. } else
  241. /* We found a P2P bridge, check if it has a node */
  242. ppnode = pci_device_to_OF_node(ppdev);
  243. /* Ok, we have found a parent with a device-node, hand over to
  244. * the OF parsing code.
  245. * We build a unit address from the linux device to be used for
  246. * resolution. Note that we use the linux bus number which may
  247. * not match your firmware bus numbering.
  248. * Fortunately, in most cases, interrupt-map-mask doesn't include
  249. * the bus number as part of the matching.
  250. * You should still be careful about that though if you intend
  251. * to rely on this function (you ship a firmware that doesn't
  252. * create device nodes for all PCI devices).
  253. */
  254. if (ppnode)
  255. break;
  256. /* We can only get here if we hit a P2P bridge with no node,
  257. * let's do standard swizzling and try again
  258. */
  259. lspec = pci_swizzle_interrupt_pin(pdev, lspec);
  260. pdev = ppdev;
  261. }
  262. laddr[0] = (pdev->bus->number << 16)
  263. | (pdev->devfn << 8);
  264. laddr[1] = laddr[2] = 0;
  265. return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq);
  266. }
  267. EXPORT_SYMBOL_GPL(of_irq_map_pci);
  268. #endif /* CONFIG_PCI */
  269. /*
  270. * ISA bus specific translator
  271. */
  272. static int of_bus_isa_match(struct device_node *np)
  273. {
  274. return !strcmp(np->name, "isa");
  275. }
  276. static void of_bus_isa_count_cells(struct device_node *child,
  277. int *addrc, int *sizec)
  278. {
  279. if (addrc)
  280. *addrc = 2;
  281. if (sizec)
  282. *sizec = 1;
  283. }
  284. static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  285. {
  286. u64 cp, s, da;
  287. /* Check address type match */
  288. if ((addr[0] ^ range[0]) & 0x00000001)
  289. return OF_BAD_ADDR;
  290. /* Read address values, skipping high cell */
  291. cp = of_read_number(range + 1, na - 1);
  292. s = of_read_number(range + na + pna, ns);
  293. da = of_read_number(addr + 1, na - 1);
  294. DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  295. if (da < cp || da >= (cp + s))
  296. return OF_BAD_ADDR;
  297. return da - cp;
  298. }
  299. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  300. {
  301. return of_bus_default_translate(addr + 1, offset, na - 1);
  302. }
  303. static unsigned int of_bus_isa_get_flags(const u32 *addr)
  304. {
  305. unsigned int flags = 0;
  306. u32 w = addr[0];
  307. if (w & 1)
  308. flags |= IORESOURCE_IO;
  309. else
  310. flags |= IORESOURCE_MEM;
  311. return flags;
  312. }
  313. /*
  314. * Array of bus specific translators
  315. */
  316. static struct of_bus of_busses[] = {
  317. #ifdef CONFIG_PCI
  318. /* PCI */
  319. {
  320. .name = "pci",
  321. .addresses = "assigned-addresses",
  322. .match = of_bus_pci_match,
  323. .count_cells = of_bus_pci_count_cells,
  324. .map = of_bus_pci_map,
  325. .translate = of_bus_pci_translate,
  326. .get_flags = of_bus_pci_get_flags,
  327. },
  328. #endif /* CONFIG_PCI */
  329. /* ISA */
  330. {
  331. .name = "isa",
  332. .addresses = "reg",
  333. .match = of_bus_isa_match,
  334. .count_cells = of_bus_isa_count_cells,
  335. .map = of_bus_isa_map,
  336. .translate = of_bus_isa_translate,
  337. .get_flags = of_bus_isa_get_flags,
  338. },
  339. /* Default */
  340. {
  341. .name = "default",
  342. .addresses = "reg",
  343. .match = NULL,
  344. .count_cells = of_bus_default_count_cells,
  345. .map = of_bus_default_map,
  346. .translate = of_bus_default_translate,
  347. .get_flags = of_bus_default_get_flags,
  348. },
  349. };
  350. static struct of_bus *of_match_bus(struct device_node *np)
  351. {
  352. int i;
  353. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  354. if (!of_busses[i].match || of_busses[i].match(np))
  355. return &of_busses[i];
  356. BUG();
  357. return NULL;
  358. }
  359. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  360. struct of_bus *pbus, u32 *addr,
  361. int na, int ns, int pna, const char *rprop)
  362. {
  363. const u32 *ranges;
  364. unsigned int rlen;
  365. int rone;
  366. u64 offset = OF_BAD_ADDR;
  367. /* Normally, an absence of a "ranges" property means we are
  368. * crossing a non-translatable boundary, and thus the addresses
  369. * below the current not cannot be converted to CPU physical ones.
  370. * Unfortunately, while this is very clear in the spec, it's not
  371. * what Apple understood, and they do have things like /uni-n or
  372. * /ht nodes with no "ranges" property and a lot of perfectly
  373. * useable mapped devices below them. Thus we treat the absence of
  374. * "ranges" as equivalent to an empty "ranges" property which means
  375. * a 1:1 translation at that level. It's up to the caller not to try
  376. * to translate addresses that aren't supposed to be translated in
  377. * the first place. --BenH.
  378. */
  379. ranges = of_get_property(parent, rprop, &rlen);
  380. if (ranges == NULL || rlen == 0) {
  381. offset = of_read_number(addr, na);
  382. memset(addr, 0, pna * 4);
  383. DBG("OF: no ranges, 1:1 translation\n");
  384. goto finish;
  385. }
  386. DBG("OF: walking ranges...\n");
  387. /* Now walk through the ranges */
  388. rlen /= 4;
  389. rone = na + pna + ns;
  390. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  391. offset = bus->map(addr, ranges, na, ns, pna);
  392. if (offset != OF_BAD_ADDR)
  393. break;
  394. }
  395. if (offset == OF_BAD_ADDR) {
  396. DBG("OF: not found !\n");
  397. return 1;
  398. }
  399. memcpy(addr, ranges + na, 4 * pna);
  400. finish:
  401. of_dump_addr("OF: parent translation for:", addr, pna);
  402. DBG("OF: with offset: "PRu64"\n", offset);
  403. /* Translate it into parent bus space */
  404. return pbus->translate(addr, offset, pna);
  405. }
  406. /*
  407. * Translate an address from the device-tree into a CPU physical address,
  408. * this walks up the tree and applies the various bus mappings on the
  409. * way.
  410. *
  411. * Note: We consider that crossing any level with #size-cells == 0 to mean
  412. * that translation is impossible (that is we are not dealing with a value
  413. * that can be mapped to a cpu physical address). This is not really specified
  414. * that way, but this is traditionally the way IBM at least do things
  415. */
  416. u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
  417. const char *rprop)
  418. {
  419. struct device_node *parent = NULL;
  420. struct of_bus *bus, *pbus;
  421. u32 addr[OF_MAX_ADDR_CELLS];
  422. int na, ns, pna, pns;
  423. u64 result = OF_BAD_ADDR;
  424. DBG("OF: ** translation for device %s **\n", dev->full_name);
  425. /* Increase refcount at current level */
  426. of_node_get(dev);
  427. /* Get parent & match bus type */
  428. parent = of_get_parent(dev);
  429. if (parent == NULL)
  430. goto bail;
  431. bus = of_match_bus(parent);
  432. /* Cound address cells & copy address locally */
  433. bus->count_cells(dev, &na, &ns);
  434. if (!OF_CHECK_COUNTS(na, ns)) {
  435. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  436. dev->full_name);
  437. goto bail;
  438. }
  439. memcpy(addr, in_addr, na * 4);
  440. DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
  441. bus->name, na, ns, parent->full_name);
  442. of_dump_addr("OF: translating address:", addr, na);
  443. /* Translate */
  444. for (;;) {
  445. /* Switch to parent bus */
  446. of_node_put(dev);
  447. dev = parent;
  448. parent = of_get_parent(dev);
  449. /* If root, we have finished */
  450. if (parent == NULL) {
  451. DBG("OF: reached root node\n");
  452. result = of_read_number(addr, na);
  453. break;
  454. }
  455. /* Get new parent bus and counts */
  456. pbus = of_match_bus(parent);
  457. pbus->count_cells(dev, &pna, &pns);
  458. if (!OF_CHECK_COUNTS(pna, pns)) {
  459. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  460. dev->full_name);
  461. break;
  462. }
  463. DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  464. pbus->name, pna, pns, parent->full_name);
  465. /* Apply bus translation */
  466. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  467. break;
  468. /* Complete the move up one level */
  469. na = pna;
  470. ns = pns;
  471. bus = pbus;
  472. of_dump_addr("OF: one level translation:", addr, na);
  473. }
  474. bail:
  475. of_node_put(parent);
  476. of_node_put(dev);
  477. return result;
  478. }
  479. u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
  480. {
  481. return __of_translate_address(dev, in_addr, "ranges");
  482. }
  483. EXPORT_SYMBOL(of_translate_address);
  484. u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
  485. {
  486. return __of_translate_address(dev, in_addr, "dma-ranges");
  487. }
  488. EXPORT_SYMBOL(of_translate_dma_address);
  489. const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
  490. unsigned int *flags)
  491. {
  492. const u32 *prop;
  493. unsigned int psize;
  494. struct device_node *parent;
  495. struct of_bus *bus;
  496. int onesize, i, na, ns;
  497. /* Get parent & match bus type */
  498. parent = of_get_parent(dev);
  499. if (parent == NULL)
  500. return NULL;
  501. bus = of_match_bus(parent);
  502. bus->count_cells(dev, &na, &ns);
  503. of_node_put(parent);
  504. if (!OF_CHECK_COUNTS(na, ns))
  505. return NULL;
  506. /* Get "reg" or "assigned-addresses" property */
  507. prop = of_get_property(dev, bus->addresses, &psize);
  508. if (prop == NULL)
  509. return NULL;
  510. psize /= 4;
  511. onesize = na + ns;
  512. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  513. if (i == index) {
  514. if (size)
  515. *size = of_read_number(prop + na, ns);
  516. if (flags)
  517. *flags = bus->get_flags(prop);
  518. return prop;
  519. }
  520. return NULL;
  521. }
  522. EXPORT_SYMBOL(of_get_address);
  523. static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
  524. u64 size, unsigned int flags,
  525. struct resource *r)
  526. {
  527. u64 taddr;
  528. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  529. return -EINVAL;
  530. taddr = of_translate_address(dev, addrp);
  531. if (taddr == OF_BAD_ADDR)
  532. return -EINVAL;
  533. memset(r, 0, sizeof(struct resource));
  534. if (flags & IORESOURCE_IO) {
  535. unsigned long port;
  536. port = pci_address_to_pio(taddr);
  537. if (port == (unsigned long)-1)
  538. return -EINVAL;
  539. r->start = port;
  540. r->end = port + size - 1;
  541. } else {
  542. r->start = taddr;
  543. r->end = taddr + size - 1;
  544. }
  545. r->flags = flags;
  546. r->name = dev->name;
  547. return 0;
  548. }
  549. int of_address_to_resource(struct device_node *dev, int index,
  550. struct resource *r)
  551. {
  552. const u32 *addrp;
  553. u64 size;
  554. unsigned int flags;
  555. addrp = of_get_address(dev, index, &size, &flags);
  556. if (addrp == NULL)
  557. return -EINVAL;
  558. return __of_address_to_resource(dev, addrp, size, flags, r);
  559. }
  560. EXPORT_SYMBOL_GPL(of_address_to_resource);
  561. void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
  562. unsigned long *busno, unsigned long *phys, unsigned long *size)
  563. {
  564. const u32 *dma_window;
  565. u32 cells;
  566. const unsigned char *prop;
  567. dma_window = dma_window_prop;
  568. /* busno is always one cell */
  569. *busno = *(dma_window++);
  570. prop = of_get_property(dn, "ibm,#dma-address-cells", NULL);
  571. if (!prop)
  572. prop = of_get_property(dn, "#address-cells", NULL);
  573. cells = prop ? *(u32 *)prop : of_n_addr_cells(dn);
  574. *phys = of_read_number(dma_window, cells);
  575. dma_window += cells;
  576. prop = of_get_property(dn, "ibm,#dma-size-cells", NULL);
  577. cells = prop ? *(u32 *)prop : of_n_size_cells(dn);
  578. *size = of_read_number(dma_window, cells);
  579. }
  580. /**
  581. * Search the device tree for the best MAC address to use. 'mac-address' is
  582. * checked first, because that is supposed to contain to "most recent" MAC
  583. * address. If that isn't set, then 'local-mac-address' is checked next,
  584. * because that is the default address. If that isn't set, then the obsolete
  585. * 'address' is checked, just in case we're using an old device tree.
  586. *
  587. * Note that the 'address' property is supposed to contain a virtual address of
  588. * the register set, but some DTS files have redefined that property to be the
  589. * MAC address.
  590. *
  591. * All-zero MAC addresses are rejected, because those could be properties that
  592. * exist in the device tree, but were not set by U-Boot. For example, the
  593. * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
  594. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  595. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  596. * but is all zeros.
  597. */
  598. const void *of_get_mac_address(struct device_node *np)
  599. {
  600. struct property *pp;
  601. pp = of_find_property(np, "mac-address", NULL);
  602. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  603. return pp->value;
  604. pp = of_find_property(np, "local-mac-address", NULL);
  605. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  606. return pp->value;
  607. pp = of_find_property(np, "address", NULL);
  608. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  609. return pp->value;
  610. return NULL;
  611. }
  612. EXPORT_SYMBOL(of_get_mac_address);
  613. void __iomem *of_iomap(struct device_node *np, int index)
  614. {
  615. struct resource res;
  616. if (of_address_to_resource(np, index, &res))
  617. return NULL;
  618. return ioremap(res.start, 1 + res.end - res.start);
  619. }
  620. EXPORT_SYMBOL(of_iomap);