address.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. if (da < cp || da >= (cp + s))
  62. return OF_BAD_ADDR;
  63. return da - cp;
  64. }
  65. static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
  66. {
  67. u64 a = of_read_number(addr, na);
  68. memset(addr, 0, na * 4);
  69. a += offset;
  70. if (na > 1)
  71. addr[na - 2] = cpu_to_be32(a >> 32);
  72. addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
  73. return 0;
  74. }
  75. static unsigned int of_bus_default_get_flags(const __be32 *addr)
  76. {
  77. return IORESOURCE_MEM;
  78. }
  79. #ifdef CONFIG_OF_ADDRESS_PCI
  80. /*
  81. * PCI bus specific translator
  82. */
  83. static int of_bus_pci_match(struct device_node *np)
  84. {
  85. /*
  86. * "pciex" is PCI Express
  87. * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
  88. * "ht" is hypertransport
  89. */
  90. return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") ||
  91. !strcmp(np->type, "vci") || !strcmp(np->type, "ht");
  92. }
  93. static void of_bus_pci_count_cells(struct device_node *np,
  94. int *addrc, int *sizec)
  95. {
  96. if (addrc)
  97. *addrc = 3;
  98. if (sizec)
  99. *sizec = 2;
  100. }
  101. static unsigned int of_bus_pci_get_flags(const __be32 *addr)
  102. {
  103. unsigned int flags = 0;
  104. u32 w = be32_to_cpup(addr);
  105. switch((w >> 24) & 0x03) {
  106. case 0x01:
  107. flags |= IORESOURCE_IO;
  108. break;
  109. case 0x02: /* 32 bits */
  110. case 0x03: /* 64 bits */
  111. flags |= IORESOURCE_MEM;
  112. break;
  113. }
  114. if (w & 0x40000000)
  115. flags |= IORESOURCE_PREFETCH;
  116. return flags;
  117. }
  118. static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
  119. int pna)
  120. {
  121. u64 cp, s, da;
  122. unsigned int af, rf;
  123. af = of_bus_pci_get_flags(addr);
  124. rf = of_bus_pci_get_flags(range);
  125. /* Check address type match */
  126. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  127. return OF_BAD_ADDR;
  128. /* Read address values, skipping high cell */
  129. cp = of_read_number(range + 1, na - 1);
  130. s = of_read_number(range + na + pna, ns);
  131. da = of_read_number(addr + 1, na - 1);
  132. pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
  133. (unsigned long long)cp, (unsigned long long)s,
  134. (unsigned long long)da);
  135. if (da < cp || da >= (cp + s))
  136. return OF_BAD_ADDR;
  137. return da - cp;
  138. }
  139. static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
  140. {
  141. return of_bus_default_translate(addr + 1, offset, na - 1);
  142. }
  143. #endif /* CONFIG_OF_ADDRESS_PCI */
  144. #ifdef CONFIG_PCI
  145. const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  146. unsigned int *flags)
  147. {
  148. const __be32 *prop;
  149. unsigned int psize;
  150. struct device_node *parent;
  151. struct of_bus *bus;
  152. int onesize, i, na, ns;
  153. /* Get parent & match bus type */
  154. parent = of_get_parent(dev);
  155. if (parent == NULL)
  156. return NULL;
  157. bus = of_match_bus(parent);
  158. if (strcmp(bus->name, "pci")) {
  159. of_node_put(parent);
  160. return NULL;
  161. }
  162. bus->count_cells(dev, &na, &ns);
  163. of_node_put(parent);
  164. if (!OF_CHECK_ADDR_COUNT(na))
  165. return NULL;
  166. /* Get "reg" or "assigned-addresses" property */
  167. prop = of_get_property(dev, bus->addresses, &psize);
  168. if (prop == NULL)
  169. return NULL;
  170. psize /= 4;
  171. onesize = na + ns;
  172. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
  173. u32 val = be32_to_cpu(prop[0]);
  174. if ((val & 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. }
  182. return NULL;
  183. }
  184. EXPORT_SYMBOL(of_get_pci_address);
  185. int of_pci_address_to_resource(struct device_node *dev, int bar,
  186. struct resource *r)
  187. {
  188. const __be32 *addrp;
  189. u64 size;
  190. unsigned int flags;
  191. addrp = of_get_pci_address(dev, bar, &size, &flags);
  192. if (addrp == NULL)
  193. return -EINVAL;
  194. return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
  195. }
  196. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  197. int of_pci_range_parser_init(struct of_pci_range_parser *parser,
  198. struct device_node *node)
  199. {
  200. const int na = 3, ns = 2;
  201. int rlen;
  202. parser->node = node;
  203. parser->pna = of_n_addr_cells(node);
  204. parser->np = parser->pna + na + ns;
  205. parser->range = of_get_property(node, "ranges", &rlen);
  206. if (parser->range == NULL)
  207. return -ENOENT;
  208. parser->end = parser->range + rlen / sizeof(__be32);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
  212. struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
  213. struct of_pci_range *range)
  214. {
  215. const int na = 3, ns = 2;
  216. if (!range)
  217. return NULL;
  218. if (!parser->range || parser->range + parser->np > parser->end)
  219. return NULL;
  220. range->pci_space = parser->range[0];
  221. range->flags = of_bus_pci_get_flags(parser->range);
  222. range->pci_addr = of_read_number(parser->range + 1, ns);
  223. range->cpu_addr = of_translate_address(parser->node,
  224. parser->range + na);
  225. range->size = of_read_number(parser->range + parser->pna + na, ns);
  226. parser->range += parser->np;
  227. /* Now consume following elements while they are contiguous */
  228. while (parser->range + parser->np <= parser->end) {
  229. u32 flags, pci_space;
  230. u64 pci_addr, cpu_addr, size;
  231. pci_space = be32_to_cpup(parser->range);
  232. flags = of_bus_pci_get_flags(parser->range);
  233. pci_addr = of_read_number(parser->range + 1, ns);
  234. cpu_addr = of_translate_address(parser->node,
  235. parser->range + na);
  236. size = of_read_number(parser->range + parser->pna + na, ns);
  237. if (flags != range->flags)
  238. break;
  239. if (pci_addr != range->pci_addr + range->size ||
  240. cpu_addr != range->cpu_addr + range->size)
  241. break;
  242. range->size += size;
  243. parser->range += parser->np;
  244. }
  245. return range;
  246. }
  247. EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
  248. #endif /* CONFIG_PCI */
  249. /*
  250. * ISA bus specific translator
  251. */
  252. static int of_bus_isa_match(struct device_node *np)
  253. {
  254. return !strcmp(np->name, "isa");
  255. }
  256. static void of_bus_isa_count_cells(struct device_node *child,
  257. int *addrc, int *sizec)
  258. {
  259. if (addrc)
  260. *addrc = 2;
  261. if (sizec)
  262. *sizec = 1;
  263. }
  264. static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
  265. int pna)
  266. {
  267. u64 cp, s, da;
  268. /* Check address type match */
  269. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  270. return OF_BAD_ADDR;
  271. /* Read address values, skipping high cell */
  272. cp = of_read_number(range + 1, na - 1);
  273. s = of_read_number(range + na + pna, ns);
  274. da = of_read_number(addr + 1, na - 1);
  275. pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
  276. (unsigned long long)cp, (unsigned long long)s,
  277. (unsigned long long)da);
  278. if (da < cp || da >= (cp + s))
  279. return OF_BAD_ADDR;
  280. return da - cp;
  281. }
  282. static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
  283. {
  284. return of_bus_default_translate(addr + 1, offset, na - 1);
  285. }
  286. static unsigned int of_bus_isa_get_flags(const __be32 *addr)
  287. {
  288. unsigned int flags = 0;
  289. u32 w = be32_to_cpup(addr);
  290. if (w & 1)
  291. flags |= IORESOURCE_IO;
  292. else
  293. flags |= IORESOURCE_MEM;
  294. return flags;
  295. }
  296. /*
  297. * Array of bus specific translators
  298. */
  299. static struct of_bus of_busses[] = {
  300. #ifdef CONFIG_OF_ADDRESS_PCI
  301. /* PCI */
  302. {
  303. .name = "pci",
  304. .addresses = "assigned-addresses",
  305. .match = of_bus_pci_match,
  306. .count_cells = of_bus_pci_count_cells,
  307. .map = of_bus_pci_map,
  308. .translate = of_bus_pci_translate,
  309. .get_flags = of_bus_pci_get_flags,
  310. },
  311. #endif /* CONFIG_OF_ADDRESS_PCI */
  312. /* ISA */
  313. {
  314. .name = "isa",
  315. .addresses = "reg",
  316. .match = of_bus_isa_match,
  317. .count_cells = of_bus_isa_count_cells,
  318. .map = of_bus_isa_map,
  319. .translate = of_bus_isa_translate,
  320. .get_flags = of_bus_isa_get_flags,
  321. },
  322. /* Default */
  323. {
  324. .name = "default",
  325. .addresses = "reg",
  326. .match = NULL,
  327. .count_cells = of_bus_default_count_cells,
  328. .map = of_bus_default_map,
  329. .translate = of_bus_default_translate,
  330. .get_flags = of_bus_default_get_flags,
  331. },
  332. };
  333. static struct of_bus *of_match_bus(struct device_node *np)
  334. {
  335. int i;
  336. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  337. if (!of_busses[i].match || of_busses[i].match(np))
  338. return &of_busses[i];
  339. BUG();
  340. return NULL;
  341. }
  342. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  343. struct of_bus *pbus, __be32 *addr,
  344. int na, int ns, int pna, const char *rprop)
  345. {
  346. const __be32 *ranges;
  347. unsigned int rlen;
  348. int rone;
  349. u64 offset = OF_BAD_ADDR;
  350. /* Normally, an absence of a "ranges" property means we are
  351. * crossing a non-translatable boundary, and thus the addresses
  352. * below the current not cannot be converted to CPU physical ones.
  353. * Unfortunately, while this is very clear in the spec, it's not
  354. * what Apple understood, and they do have things like /uni-n or
  355. * /ht nodes with no "ranges" property and a lot of perfectly
  356. * useable mapped devices below them. Thus we treat the absence of
  357. * "ranges" as equivalent to an empty "ranges" property which means
  358. * a 1:1 translation at that level. It's up to the caller not to try
  359. * to translate addresses that aren't supposed to be translated in
  360. * the first place. --BenH.
  361. *
  362. * As far as we know, this damage only exists on Apple machines, so
  363. * This code is only enabled on powerpc. --gcl
  364. */
  365. ranges = of_get_property(parent, rprop, &rlen);
  366. #if !defined(CONFIG_PPC)
  367. if (ranges == NULL) {
  368. pr_err("OF: no ranges; cannot translate\n");
  369. return 1;
  370. }
  371. #endif /* !defined(CONFIG_PPC) */
  372. if (ranges == NULL || rlen == 0) {
  373. offset = of_read_number(addr, na);
  374. memset(addr, 0, pna * 4);
  375. pr_debug("OF: empty ranges; 1:1 translation\n");
  376. goto finish;
  377. }
  378. pr_debug("OF: walking ranges...\n");
  379. /* Now walk through the ranges */
  380. rlen /= 4;
  381. rone = na + pna + ns;
  382. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  383. offset = bus->map(addr, ranges, na, ns, pna);
  384. if (offset != OF_BAD_ADDR)
  385. break;
  386. }
  387. if (offset == OF_BAD_ADDR) {
  388. pr_debug("OF: not found !\n");
  389. return 1;
  390. }
  391. memcpy(addr, ranges + na, 4 * pna);
  392. finish:
  393. of_dump_addr("OF: parent translation for:", addr, pna);
  394. pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
  395. /* Translate it into parent bus space */
  396. return pbus->translate(addr, offset, pna);
  397. }
  398. /*
  399. * Translate an address from the device-tree into a CPU physical address,
  400. * this walks up the tree and applies the various bus mappings on the
  401. * way.
  402. *
  403. * Note: We consider that crossing any level with #size-cells == 0 to mean
  404. * that translation is impossible (that is we are not dealing with a value
  405. * that can be mapped to a cpu physical address). This is not really specified
  406. * that way, but this is traditionally the way IBM at least do things
  407. */
  408. static u64 __of_translate_address(struct device_node *dev,
  409. const __be32 *in_addr, const char *rprop)
  410. {
  411. struct device_node *parent = NULL;
  412. struct of_bus *bus, *pbus;
  413. __be32 addr[OF_MAX_ADDR_CELLS];
  414. int na, ns, pna, pns;
  415. u64 result = OF_BAD_ADDR;
  416. pr_debug("OF: ** translation for device %s **\n", of_node_full_name(dev));
  417. /* Increase refcount at current level */
  418. of_node_get(dev);
  419. /* Get parent & match bus type */
  420. parent = of_get_parent(dev);
  421. if (parent == NULL)
  422. goto bail;
  423. bus = of_match_bus(parent);
  424. /* Count address cells & copy address locally */
  425. bus->count_cells(dev, &na, &ns);
  426. if (!OF_CHECK_COUNTS(na, ns)) {
  427. pr_debug("OF: Bad cell count for %s\n", of_node_full_name(dev));
  428. goto bail;
  429. }
  430. memcpy(addr, in_addr, na * 4);
  431. pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  432. bus->name, na, ns, of_node_full_name(parent));
  433. of_dump_addr("OF: translating address:", addr, na);
  434. /* Translate */
  435. for (;;) {
  436. /* Switch to parent bus */
  437. of_node_put(dev);
  438. dev = parent;
  439. parent = of_get_parent(dev);
  440. /* If root, we have finished */
  441. if (parent == NULL) {
  442. pr_debug("OF: reached root node\n");
  443. result = of_read_number(addr, na);
  444. break;
  445. }
  446. /* Get new parent bus and counts */
  447. pbus = of_match_bus(parent);
  448. pbus->count_cells(dev, &pna, &pns);
  449. if (!OF_CHECK_COUNTS(pna, pns)) {
  450. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  451. of_node_full_name(dev));
  452. break;
  453. }
  454. pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  455. pbus->name, pna, pns, of_node_full_name(parent));
  456. /* Apply bus translation */
  457. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  458. break;
  459. /* Complete the move up one level */
  460. na = pna;
  461. ns = pns;
  462. bus = pbus;
  463. of_dump_addr("OF: one level translation:", addr, na);
  464. }
  465. bail:
  466. of_node_put(parent);
  467. of_node_put(dev);
  468. return result;
  469. }
  470. u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
  471. {
  472. return __of_translate_address(dev, in_addr, "ranges");
  473. }
  474. EXPORT_SYMBOL(of_translate_address);
  475. u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
  476. {
  477. return __of_translate_address(dev, in_addr, "dma-ranges");
  478. }
  479. EXPORT_SYMBOL(of_translate_dma_address);
  480. const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
  481. unsigned int *flags)
  482. {
  483. const __be32 *prop;
  484. unsigned int psize;
  485. struct device_node *parent;
  486. struct of_bus *bus;
  487. int onesize, i, na, ns;
  488. /* Get parent & match bus type */
  489. parent = of_get_parent(dev);
  490. if (parent == NULL)
  491. return NULL;
  492. bus = of_match_bus(parent);
  493. bus->count_cells(dev, &na, &ns);
  494. of_node_put(parent);
  495. if (!OF_CHECK_ADDR_COUNT(na))
  496. return NULL;
  497. /* Get "reg" or "assigned-addresses" property */
  498. prop = of_get_property(dev, bus->addresses, &psize);
  499. if (prop == NULL)
  500. return NULL;
  501. psize /= 4;
  502. onesize = na + ns;
  503. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  504. if (i == index) {
  505. if (size)
  506. *size = of_read_number(prop + na, ns);
  507. if (flags)
  508. *flags = bus->get_flags(prop);
  509. return prop;
  510. }
  511. return NULL;
  512. }
  513. EXPORT_SYMBOL(of_get_address);
  514. unsigned long __weak pci_address_to_pio(phys_addr_t address)
  515. {
  516. if (address > IO_SPACE_LIMIT)
  517. return (unsigned long)-1;
  518. return (unsigned long) address;
  519. }
  520. static int __of_address_to_resource(struct device_node *dev,
  521. const __be32 *addrp, u64 size, unsigned int flags,
  522. const char *name, struct resource *r)
  523. {
  524. u64 taddr;
  525. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  526. return -EINVAL;
  527. taddr = of_translate_address(dev, addrp);
  528. if (taddr == OF_BAD_ADDR)
  529. return -EINVAL;
  530. memset(r, 0, sizeof(struct resource));
  531. if (flags & IORESOURCE_IO) {
  532. unsigned long port;
  533. port = pci_address_to_pio(taddr);
  534. if (port == (unsigned long)-1)
  535. return -EINVAL;
  536. r->start = port;
  537. r->end = port + size - 1;
  538. } else {
  539. r->start = taddr;
  540. r->end = taddr + size - 1;
  541. }
  542. r->flags = flags;
  543. r->name = name ? name : dev->full_name;
  544. return 0;
  545. }
  546. /**
  547. * of_address_to_resource - Translate device tree address and return as resource
  548. *
  549. * Note that if your address is a PIO address, the conversion will fail if
  550. * the physical address can't be internally converted to an IO token with
  551. * pci_address_to_pio(), that is because it's either called to early or it
  552. * can't be matched to any host bridge IO space
  553. */
  554. int of_address_to_resource(struct device_node *dev, int index,
  555. struct resource *r)
  556. {
  557. const __be32 *addrp;
  558. u64 size;
  559. unsigned int flags;
  560. const char *name = NULL;
  561. addrp = of_get_address(dev, index, &size, &flags);
  562. if (addrp == NULL)
  563. return -EINVAL;
  564. /* Get optional "reg-names" property to add a name to a resource */
  565. of_property_read_string_index(dev, "reg-names", index, &name);
  566. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  567. }
  568. EXPORT_SYMBOL_GPL(of_address_to_resource);
  569. struct device_node *of_find_matching_node_by_address(struct device_node *from,
  570. const struct of_device_id *matches,
  571. u64 base_address)
  572. {
  573. struct device_node *dn = of_find_matching_node(from, matches);
  574. struct resource res;
  575. while (dn) {
  576. if (of_address_to_resource(dn, 0, &res))
  577. continue;
  578. if (res.start == base_address)
  579. return dn;
  580. dn = of_find_matching_node(dn, matches);
  581. }
  582. return NULL;
  583. }
  584. /**
  585. * of_iomap - Maps the memory mapped IO for a given device_node
  586. * @device: the device whose io range will be mapped
  587. * @index: index of the io range
  588. *
  589. * Returns a pointer to the mapped memory
  590. */
  591. void __iomem *of_iomap(struct device_node *np, int index)
  592. {
  593. struct resource res;
  594. if (of_address_to_resource(np, index, &res))
  595. return NULL;
  596. return ioremap(res.start, resource_size(&res));
  597. }
  598. EXPORT_SYMBOL(of_iomap);
  599. /**
  600. * of_dma_get_range - Get DMA range info
  601. * @np: device node to get DMA range info
  602. * @dma_addr: pointer to store initial DMA address of DMA range
  603. * @paddr: pointer to store initial CPU address of DMA range
  604. * @size: pointer to store size of DMA range
  605. *
  606. * Look in bottom up direction for the first "dma-ranges" property
  607. * and parse it.
  608. * dma-ranges format:
  609. * DMA addr (dma_addr) : naddr cells
  610. * CPU addr (phys_addr_t) : pna cells
  611. * size : nsize cells
  612. *
  613. * It returns -ENODEV if "dma-ranges" property was not found
  614. * for this device in DT.
  615. */
  616. int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
  617. {
  618. struct device_node *node = of_node_get(np);
  619. const __be32 *ranges = NULL;
  620. int len, naddr, nsize, pna;
  621. int ret = 0;
  622. u64 dmaaddr;
  623. if (!node)
  624. return -EINVAL;
  625. while (1) {
  626. naddr = of_n_addr_cells(node);
  627. nsize = of_n_size_cells(node);
  628. node = of_get_next_parent(node);
  629. if (!node)
  630. break;
  631. ranges = of_get_property(node, "dma-ranges", &len);
  632. /* Ignore empty ranges, they imply no translation required */
  633. if (ranges && len > 0)
  634. break;
  635. /*
  636. * At least empty ranges has to be defined for parent node if
  637. * DMA is supported
  638. */
  639. if (!ranges)
  640. break;
  641. }
  642. if (!ranges) {
  643. pr_debug("%s: no dma-ranges found for node(%s)\n",
  644. __func__, np->full_name);
  645. ret = -ENODEV;
  646. goto out;
  647. }
  648. len /= sizeof(u32);
  649. pna = of_n_addr_cells(node);
  650. /* dma-ranges format:
  651. * DMA addr : naddr cells
  652. * CPU addr : pna cells
  653. * size : nsize cells
  654. */
  655. dmaaddr = of_read_number(ranges, naddr);
  656. *paddr = of_translate_dma_address(np, ranges);
  657. if (*paddr == OF_BAD_ADDR) {
  658. pr_err("%s: translation of DMA address(%pad) to CPU address failed node(%s)\n",
  659. __func__, dma_addr, np->full_name);
  660. ret = -EINVAL;
  661. goto out;
  662. }
  663. *dma_addr = dmaaddr;
  664. *size = of_read_number(ranges + naddr + pna, nsize);
  665. pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
  666. *dma_addr, *paddr, *size);
  667. out:
  668. of_node_put(node);
  669. return ret;
  670. }
  671. EXPORT_SYMBOL_GPL(of_dma_get_range);
  672. /**
  673. * of_dma_is_coherent - Check if device is coherent
  674. * @np: device node
  675. *
  676. * It returns true if "dma-coherent" property was found
  677. * for this device in DT.
  678. */
  679. bool of_dma_is_coherent(struct device_node *np)
  680. {
  681. struct device_node *node = of_node_get(np);
  682. while (node) {
  683. if (of_property_read_bool(node, "dma-coherent")) {
  684. of_node_put(node);
  685. return true;
  686. }
  687. node = of_get_next_parent(node);
  688. }
  689. of_node_put(node);
  690. return false;
  691. }
  692. EXPORT_SYMBOL_GPL(of_dma_is_coherent);