address.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #include <linux/device.h>
  2. #include <linux/io.h>
  3. #include <linux/ioport.h>
  4. #include <linux/module.h>
  5. #include <linux/of_address.h>
  6. #include <linux/pci_regs.h>
  7. #include <linux/string.h>
  8. /* Max address size we deal with */
  9. #define OF_MAX_ADDR_CELLS 4
  10. #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
  11. #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
  12. static struct of_bus *of_match_bus(struct device_node *np);
  13. static int __of_address_to_resource(struct device_node *dev,
  14. const __be32 *addrp, u64 size, unsigned int flags,
  15. const char *name, struct resource *r);
  16. /* Debug utility */
  17. #ifdef DEBUG
  18. static void of_dump_addr(const char *s, const __be32 *addr, int na)
  19. {
  20. printk(KERN_DEBUG "%s", s);
  21. while (na--)
  22. printk(" %08x", be32_to_cpu(*(addr++)));
  23. printk("\n");
  24. }
  25. #else
  26. static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
  27. #endif
  28. /* Callbacks for bus specific translators */
  29. struct of_bus {
  30. const char *name;
  31. const char *addresses;
  32. int (*match)(struct device_node *parent);
  33. void (*count_cells)(struct device_node *child,
  34. int *addrc, int *sizec);
  35. u64 (*map)(__be32 *addr, const __be32 *range,
  36. int na, int ns, int pna);
  37. int (*translate)(__be32 *addr, u64 offset, int na);
  38. unsigned int (*get_flags)(const __be32 *addr);
  39. };
  40. /*
  41. * Default translator (generic bus)
  42. */
  43. static void of_bus_default_count_cells(struct device_node *dev,
  44. int *addrc, int *sizec)
  45. {
  46. if (addrc)
  47. *addrc = of_n_addr_cells(dev);
  48. if (sizec)
  49. *sizec = of_n_size_cells(dev);
  50. }
  51. static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
  52. int na, int ns, int pna)
  53. {
  54. u64 cp, s, da;
  55. cp = of_read_number(range, na);
  56. s = of_read_number(range + na + pna, ns);
  57. da = of_read_number(addr, na);
  58. pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
  59. (unsigned long long)cp, (unsigned long long)s,
  60. (unsigned long long)da);
  61. /*
  62. * If the number of address cells is larger than 2 we assume the
  63. * mapping doesn't specify a physical address. Rather, the address
  64. * specifies an identifier that must match exactly.
  65. */
  66. if (na > 2 && memcmp(range, addr, na * 4) != 0)
  67. return OF_BAD_ADDR;
  68. if (da < cp || da >= (cp + s))
  69. return OF_BAD_ADDR;
  70. return da - cp;
  71. }
  72. static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
  73. {
  74. u64 a = of_read_number(addr, na);
  75. memset(addr, 0, na * 4);
  76. a += offset;
  77. if (na > 1)
  78. addr[na - 2] = cpu_to_be32(a >> 32);
  79. addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
  80. return 0;
  81. }
  82. static unsigned int of_bus_default_get_flags(const __be32 *addr)
  83. {
  84. return IORESOURCE_MEM;
  85. }
  86. #ifdef CONFIG_PCI
  87. /*
  88. * PCI bus specific translator
  89. */
  90. static int of_bus_pci_match(struct device_node *np)
  91. {
  92. /*
  93. * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
  94. * "ht" is hypertransport
  95. */
  96. return !strcmp(np->type, "pci") || !strcmp(np->type, "vci") ||
  97. !strcmp(np->type, "ht");
  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 __be32 *addr)
  108. {
  109. unsigned int flags = 0;
  110. u32 w = be32_to_cpup(addr);
  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(__be32 *addr, const __be32 *range, int na, int ns,
  125. int pna)
  126. {
  127. u64 cp, s, da;
  128. unsigned int af, rf;
  129. af = of_bus_pci_get_flags(addr);
  130. rf = of_bus_pci_get_flags(range);
  131. /* Check address type match */
  132. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  133. return OF_BAD_ADDR;
  134. /* Read address values, skipping high cell */
  135. cp = of_read_number(range + 1, na - 1);
  136. s = of_read_number(range + na + pna, ns);
  137. da = of_read_number(addr + 1, na - 1);
  138. pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
  139. (unsigned long long)cp, (unsigned long long)s,
  140. (unsigned long long)da);
  141. if (da < cp || da >= (cp + s))
  142. return OF_BAD_ADDR;
  143. return da - cp;
  144. }
  145. static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
  146. {
  147. return of_bus_default_translate(addr + 1, offset, na - 1);
  148. }
  149. const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  150. unsigned int *flags)
  151. {
  152. const __be32 *prop;
  153. unsigned int psize;
  154. struct device_node *parent;
  155. struct of_bus *bus;
  156. int onesize, i, na, ns;
  157. /* Get parent & match bus type */
  158. parent = of_get_parent(dev);
  159. if (parent == NULL)
  160. return NULL;
  161. bus = of_match_bus(parent);
  162. if (strcmp(bus->name, "pci")) {
  163. of_node_put(parent);
  164. return NULL;
  165. }
  166. bus->count_cells(dev, &na, &ns);
  167. of_node_put(parent);
  168. if (!OF_CHECK_ADDR_COUNT(na))
  169. return NULL;
  170. /* Get "reg" or "assigned-addresses" property */
  171. prop = of_get_property(dev, bus->addresses, &psize);
  172. if (prop == NULL)
  173. return NULL;
  174. psize /= 4;
  175. onesize = na + ns;
  176. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
  177. u32 val = be32_to_cpu(prop[0]);
  178. if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  179. if (size)
  180. *size = of_read_number(prop + na, ns);
  181. if (flags)
  182. *flags = bus->get_flags(prop);
  183. return prop;
  184. }
  185. }
  186. return NULL;
  187. }
  188. EXPORT_SYMBOL(of_get_pci_address);
  189. int of_pci_address_to_resource(struct device_node *dev, int bar,
  190. struct resource *r)
  191. {
  192. const __be32 *addrp;
  193. u64 size;
  194. unsigned int flags;
  195. addrp = of_get_pci_address(dev, bar, &size, &flags);
  196. if (addrp == NULL)
  197. return -EINVAL;
  198. return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
  199. }
  200. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  201. int of_pci_range_parser_init(struct of_pci_range_parser *parser,
  202. struct device_node *node)
  203. {
  204. const int na = 3, ns = 2;
  205. int rlen;
  206. parser->node = node;
  207. parser->pna = of_n_addr_cells(node);
  208. parser->np = parser->pna + na + ns;
  209. parser->range = of_get_property(node, "ranges", &rlen);
  210. if (parser->range == NULL)
  211. return -ENOENT;
  212. parser->end = parser->range + rlen / sizeof(__be32);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
  216. struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
  217. struct of_pci_range *range)
  218. {
  219. const int na = 3, ns = 2;
  220. if (!range)
  221. return NULL;
  222. if (!parser->range || parser->range + parser->np > parser->end)
  223. return NULL;
  224. range->pci_space = parser->range[0];
  225. range->flags = of_bus_pci_get_flags(parser->range);
  226. range->pci_addr = of_read_number(parser->range + 1, ns);
  227. range->cpu_addr = of_translate_address(parser->node,
  228. parser->range + na);
  229. range->size = of_read_number(parser->range + parser->pna + na, ns);
  230. parser->range += parser->np;
  231. /* Now consume following elements while they are contiguous */
  232. while (parser->range + parser->np <= parser->end) {
  233. u32 flags, pci_space;
  234. u64 pci_addr, cpu_addr, size;
  235. pci_space = be32_to_cpup(parser->range);
  236. flags = of_bus_pci_get_flags(parser->range);
  237. pci_addr = of_read_number(parser->range + 1, ns);
  238. cpu_addr = of_translate_address(parser->node,
  239. parser->range + na);
  240. size = of_read_number(parser->range + parser->pna + na, ns);
  241. if (flags != range->flags)
  242. break;
  243. if (pci_addr != range->pci_addr + range->size ||
  244. cpu_addr != range->cpu_addr + range->size)
  245. break;
  246. range->size += size;
  247. parser->range += parser->np;
  248. }
  249. return range;
  250. }
  251. EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
  252. #endif /* CONFIG_PCI */
  253. /*
  254. * ISA bus specific translator
  255. */
  256. static int of_bus_isa_match(struct device_node *np)
  257. {
  258. return !strcmp(np->name, "isa");
  259. }
  260. static void of_bus_isa_count_cells(struct device_node *child,
  261. int *addrc, int *sizec)
  262. {
  263. if (addrc)
  264. *addrc = 2;
  265. if (sizec)
  266. *sizec = 1;
  267. }
  268. static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
  269. int pna)
  270. {
  271. u64 cp, s, da;
  272. /* Check address type match */
  273. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  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=%llx, s=%llx, da=%llx\n",
  280. (unsigned long long)cp, (unsigned long long)s,
  281. (unsigned long long)da);
  282. if (da < cp || da >= (cp + s))
  283. return OF_BAD_ADDR;
  284. return da - cp;
  285. }
  286. static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
  287. {
  288. return of_bus_default_translate(addr + 1, offset, na - 1);
  289. }
  290. static unsigned int of_bus_isa_get_flags(const __be32 *addr)
  291. {
  292. unsigned int flags = 0;
  293. u32 w = be32_to_cpup(addr);
  294. if (w & 1)
  295. flags |= IORESOURCE_IO;
  296. else
  297. flags |= IORESOURCE_MEM;
  298. return flags;
  299. }
  300. /*
  301. * Array of bus specific translators
  302. */
  303. static struct of_bus of_busses[] = {
  304. #ifdef CONFIG_PCI
  305. /* PCI */
  306. {
  307. .name = "pci",
  308. .addresses = "assigned-addresses",
  309. .match = of_bus_pci_match,
  310. .count_cells = of_bus_pci_count_cells,
  311. .map = of_bus_pci_map,
  312. .translate = of_bus_pci_translate,
  313. .get_flags = of_bus_pci_get_flags,
  314. },
  315. #endif /* CONFIG_PCI */
  316. /* ISA */
  317. {
  318. .name = "isa",
  319. .addresses = "reg",
  320. .match = of_bus_isa_match,
  321. .count_cells = of_bus_isa_count_cells,
  322. .map = of_bus_isa_map,
  323. .translate = of_bus_isa_translate,
  324. .get_flags = of_bus_isa_get_flags,
  325. },
  326. /* Default */
  327. {
  328. .name = "default",
  329. .addresses = "reg",
  330. .match = NULL,
  331. .count_cells = of_bus_default_count_cells,
  332. .map = of_bus_default_map,
  333. .translate = of_bus_default_translate,
  334. .get_flags = of_bus_default_get_flags,
  335. },
  336. };
  337. static struct of_bus *of_match_bus(struct device_node *np)
  338. {
  339. int i;
  340. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  341. if (!of_busses[i].match || of_busses[i].match(np))
  342. return &of_busses[i];
  343. BUG();
  344. return NULL;
  345. }
  346. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  347. struct of_bus *pbus, __be32 *addr,
  348. int na, int ns, int pna, const char *rprop)
  349. {
  350. const __be32 *ranges;
  351. unsigned int rlen;
  352. int rone;
  353. u64 offset = OF_BAD_ADDR;
  354. /* Normally, an absence of a "ranges" property means we are
  355. * crossing a non-translatable boundary, and thus the addresses
  356. * below the current not cannot be converted to CPU physical ones.
  357. * Unfortunately, while this is very clear in the spec, it's not
  358. * what Apple understood, and they do have things like /uni-n or
  359. * /ht nodes with no "ranges" property and a lot of perfectly
  360. * useable mapped devices below them. Thus we treat the absence of
  361. * "ranges" as equivalent to an empty "ranges" property which means
  362. * a 1:1 translation at that level. It's up to the caller not to try
  363. * to translate addresses that aren't supposed to be translated in
  364. * the first place. --BenH.
  365. *
  366. * As far as we know, this damage only exists on Apple machines, so
  367. * This code is only enabled on powerpc. --gcl
  368. */
  369. ranges = of_get_property(parent, rprop, &rlen);
  370. #if !defined(CONFIG_PPC)
  371. if (ranges == NULL) {
  372. pr_err("OF: no ranges; cannot translate\n");
  373. return 1;
  374. }
  375. #endif /* !defined(CONFIG_PPC) */
  376. if (ranges == NULL || rlen == 0) {
  377. offset = of_read_number(addr, na);
  378. memset(addr, 0, pna * 4);
  379. pr_debug("OF: empty ranges; 1:1 translation\n");
  380. goto finish;
  381. }
  382. pr_debug("OF: walking ranges...\n");
  383. /* Now walk through the ranges */
  384. rlen /= 4;
  385. rone = na + pna + ns;
  386. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  387. offset = bus->map(addr, ranges, na, ns, pna);
  388. if (offset != OF_BAD_ADDR)
  389. break;
  390. }
  391. if (offset == OF_BAD_ADDR) {
  392. pr_debug("OF: not found !\n");
  393. return 1;
  394. }
  395. memcpy(addr, ranges + na, 4 * pna);
  396. finish:
  397. of_dump_addr("OF: parent translation for:", addr, pna);
  398. pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
  399. /* Translate it into parent bus space */
  400. return pbus->translate(addr, offset, pna);
  401. }
  402. /*
  403. * Translate an address from the device-tree into a CPU physical address,
  404. * this walks up the tree and applies the various bus mappings on the
  405. * way.
  406. *
  407. * Note: We consider that crossing any level with #size-cells == 0 to mean
  408. * that translation is impossible (that is we are not dealing with a value
  409. * that can be mapped to a cpu physical address). This is not really specified
  410. * that way, but this is traditionally the way IBM at least do things
  411. */
  412. static u64 __of_translate_address(struct device_node *dev,
  413. const __be32 *in_addr, const char *rprop)
  414. {
  415. struct device_node *parent = NULL;
  416. struct of_bus *bus, *pbus;
  417. __be32 addr[OF_MAX_ADDR_CELLS];
  418. int na, ns, pna, pns;
  419. u64 result = OF_BAD_ADDR;
  420. pr_debug("OF: ** translation for device %s **\n", dev->full_name);
  421. /* Increase refcount at current level */
  422. of_node_get(dev);
  423. /* Get parent & match bus type */
  424. parent = of_get_parent(dev);
  425. if (parent == NULL)
  426. goto bail;
  427. bus = of_match_bus(parent);
  428. /* Count address cells & copy address locally */
  429. bus->count_cells(dev, &na, &ns);
  430. if (!OF_CHECK_COUNTS(na, ns)) {
  431. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  432. dev->full_name);
  433. goto bail;
  434. }
  435. memcpy(addr, in_addr, na * 4);
  436. pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  437. bus->name, na, ns, parent->full_name);
  438. of_dump_addr("OF: translating address:", addr, na);
  439. /* Translate */
  440. for (;;) {
  441. /* Switch to parent bus */
  442. of_node_put(dev);
  443. dev = parent;
  444. parent = of_get_parent(dev);
  445. /* If root, we have finished */
  446. if (parent == NULL) {
  447. pr_debug("OF: reached root node\n");
  448. result = of_read_number(addr, na);
  449. break;
  450. }
  451. /* Get new parent bus and counts */
  452. pbus = of_match_bus(parent);
  453. pbus->count_cells(dev, &pna, &pns);
  454. if (!OF_CHECK_COUNTS(pna, pns)) {
  455. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  456. dev->full_name);
  457. break;
  458. }
  459. pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  460. pbus->name, pna, pns, parent->full_name);
  461. /* Apply bus translation */
  462. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  463. break;
  464. /* Complete the move up one level */
  465. na = pna;
  466. ns = pns;
  467. bus = pbus;
  468. of_dump_addr("OF: one level translation:", addr, na);
  469. }
  470. bail:
  471. of_node_put(parent);
  472. of_node_put(dev);
  473. return result;
  474. }
  475. u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
  476. {
  477. return __of_translate_address(dev, in_addr, "ranges");
  478. }
  479. EXPORT_SYMBOL(of_translate_address);
  480. u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
  481. {
  482. return __of_translate_address(dev, in_addr, "dma-ranges");
  483. }
  484. EXPORT_SYMBOL(of_translate_dma_address);
  485. bool of_can_translate_address(struct device_node *dev)
  486. {
  487. struct device_node *parent;
  488. struct of_bus *bus;
  489. int na, ns;
  490. parent = of_get_parent(dev);
  491. if (parent == NULL)
  492. return false;
  493. bus = of_match_bus(parent);
  494. bus->count_cells(dev, &na, &ns);
  495. of_node_put(parent);
  496. return OF_CHECK_COUNTS(na, ns);
  497. }
  498. EXPORT_SYMBOL(of_can_translate_address);
  499. const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
  500. unsigned int *flags)
  501. {
  502. const __be32 *prop;
  503. unsigned int psize;
  504. struct device_node *parent;
  505. struct of_bus *bus;
  506. int onesize, i, na, ns;
  507. /* Get parent & match bus type */
  508. parent = of_get_parent(dev);
  509. if (parent == NULL)
  510. return NULL;
  511. bus = of_match_bus(parent);
  512. bus->count_cells(dev, &na, &ns);
  513. of_node_put(parent);
  514. if (!OF_CHECK_ADDR_COUNT(na))
  515. return NULL;
  516. /* Get "reg" or "assigned-addresses" property */
  517. prop = of_get_property(dev, bus->addresses, &psize);
  518. if (prop == NULL)
  519. return NULL;
  520. psize /= 4;
  521. onesize = na + ns;
  522. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  523. if (i == index) {
  524. if (size)
  525. *size = of_read_number(prop + na, ns);
  526. if (flags)
  527. *flags = bus->get_flags(prop);
  528. return prop;
  529. }
  530. return NULL;
  531. }
  532. EXPORT_SYMBOL(of_get_address);
  533. static int __of_address_to_resource(struct device_node *dev,
  534. const __be32 *addrp, u64 size, unsigned int flags,
  535. const char *name, struct resource *r)
  536. {
  537. u64 taddr;
  538. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  539. return -EINVAL;
  540. taddr = of_translate_address(dev, addrp);
  541. if (taddr == OF_BAD_ADDR)
  542. return -EINVAL;
  543. memset(r, 0, sizeof(struct resource));
  544. if (flags & IORESOURCE_IO) {
  545. unsigned long port;
  546. port = pci_address_to_pio(taddr);
  547. if (port == (unsigned long)-1)
  548. return -EINVAL;
  549. r->start = port;
  550. r->end = port + size - 1;
  551. } else {
  552. r->start = taddr;
  553. r->end = taddr + size - 1;
  554. }
  555. r->flags = flags;
  556. r->name = name ? name : dev->full_name;
  557. return 0;
  558. }
  559. /**
  560. * of_address_to_resource - Translate device tree address and return as resource
  561. *
  562. * Note that if your address is a PIO address, the conversion will fail if
  563. * the physical address can't be internally converted to an IO token with
  564. * pci_address_to_pio(), that is because it's either called to early or it
  565. * can't be matched to any host bridge IO space
  566. */
  567. int of_address_to_resource(struct device_node *dev, int index,
  568. struct resource *r)
  569. {
  570. const __be32 *addrp;
  571. u64 size;
  572. unsigned int flags;
  573. const char *name = NULL;
  574. addrp = of_get_address(dev, index, &size, &flags);
  575. if (addrp == NULL)
  576. return -EINVAL;
  577. /* Get optional "reg-names" property to add a name to a resource */
  578. of_property_read_string_index(dev, "reg-names", index, &name);
  579. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  580. }
  581. EXPORT_SYMBOL_GPL(of_address_to_resource);
  582. struct device_node *of_find_matching_node_by_address(struct device_node *from,
  583. const struct of_device_id *matches,
  584. u64 base_address)
  585. {
  586. struct device_node *dn = of_find_matching_node(from, matches);
  587. struct resource res;
  588. while (dn) {
  589. if (of_address_to_resource(dn, 0, &res))
  590. continue;
  591. if (res.start == base_address)
  592. return dn;
  593. dn = of_find_matching_node(dn, matches);
  594. }
  595. return NULL;
  596. }
  597. /**
  598. * of_iomap - Maps the memory mapped IO for a given device_node
  599. * @device: the device whose io range will be mapped
  600. * @index: index of the io range
  601. *
  602. * Returns a pointer to the mapped memory
  603. */
  604. void __iomem *of_iomap(struct device_node *np, int index)
  605. {
  606. struct resource res;
  607. if (of_address_to_resource(np, index, &res))
  608. return NULL;
  609. return ioremap(res.start, resource_size(&res));
  610. }
  611. EXPORT_SYMBOL(of_iomap);