of_addr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Taken from Linux v4.9 drivers/of/address.c
  4. *
  5. * Modified for U-Boot
  6. * Copyright (c) 2017 Google, Inc
  7. */
  8. #include <common.h>
  9. #include <log.h>
  10. #include <linux/bug.h>
  11. #include <linux/libfdt.h>
  12. #include <dm/of_access.h>
  13. #include <dm/of_addr.h>
  14. #include <linux/err.h>
  15. #include <linux/ioport.h>
  16. /* Max address size we deal with */
  17. #define OF_MAX_ADDR_CELLS 4
  18. #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
  19. #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
  20. static struct of_bus *of_match_bus(struct device_node *np);
  21. /* Debug utility */
  22. #ifdef DEBUG
  23. static void of_dump_addr(const char *s, const __be32 *addr, int na)
  24. {
  25. debug("%s", s);
  26. while (na--)
  27. pr_cont(" %08x", be32_to_cpu(*(addr++)));
  28. pr_cont("\n");
  29. }
  30. #else
  31. static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
  32. #endif
  33. /* Callbacks for bus specific translators */
  34. struct of_bus {
  35. const char *name;
  36. const char *addresses;
  37. int (*match)(struct device_node *parent);
  38. void (*count_cells)(const struct device_node *child, int *addrc,
  39. int *sizec);
  40. u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna);
  41. int (*translate)(__be32 *addr, u64 offset, int na);
  42. unsigned int (*get_flags)(const __be32 *addr);
  43. };
  44. static void of_bus_default_count_cells(const struct device_node *np,
  45. int *addrc, int *sizec)
  46. {
  47. if (addrc)
  48. *addrc = of_n_addr_cells(np);
  49. if (sizec)
  50. *sizec = of_n_size_cells(np);
  51. }
  52. static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
  53. int na, int ns, int pna)
  54. {
  55. u64 cp, s, da;
  56. cp = of_read_number(range, na);
  57. s = of_read_number(range + na + pna, ns);
  58. da = of_read_number(addr, na);
  59. debug("default map, cp=%llx, s=%llx, da=%llx\n",
  60. (unsigned long long)cp, (unsigned long long)s,
  61. (unsigned long long)da);
  62. if (da < cp || da >= (cp + s))
  63. return OF_BAD_ADDR;
  64. return da - cp;
  65. }
  66. static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
  67. {
  68. u64 a = of_read_number(addr, na);
  69. memset(addr, 0, na * 4);
  70. a += offset;
  71. if (na > 1)
  72. addr[na - 2] = cpu_to_be32(a >> 32);
  73. addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
  74. return 0;
  75. }
  76. static unsigned int of_bus_default_get_flags(const __be32 *addr)
  77. {
  78. return IORESOURCE_MEM;
  79. }
  80. /*
  81. * Array of bus-specific translators
  82. */
  83. static struct of_bus of_busses[] = {
  84. /* Default */
  85. {
  86. .name = "default",
  87. .addresses = "reg",
  88. .match = NULL,
  89. .count_cells = of_bus_default_count_cells,
  90. .map = of_bus_default_map,
  91. .translate = of_bus_default_translate,
  92. .get_flags = of_bus_default_get_flags,
  93. },
  94. };
  95. static struct of_bus *of_match_bus(struct device_node *np)
  96. {
  97. int i;
  98. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  99. if (!of_busses[i].match || of_busses[i].match(np))
  100. return &of_busses[i];
  101. BUG();
  102. return NULL;
  103. }
  104. const __be32 *of_get_address(const struct device_node *dev, int index,
  105. u64 *size, unsigned int *flags)
  106. {
  107. const __be32 *prop;
  108. int psize;
  109. struct device_node *parent;
  110. struct of_bus *bus;
  111. int onesize, i, na, ns;
  112. /* Get parent & match bus type */
  113. parent = of_get_parent(dev);
  114. if (parent == NULL)
  115. return NULL;
  116. bus = of_match_bus(parent);
  117. bus->count_cells(dev, &na, &ns);
  118. of_node_put(parent);
  119. if (!OF_CHECK_ADDR_COUNT(na))
  120. return NULL;
  121. /* Get "reg" or "assigned-addresses" property */
  122. prop = of_get_property(dev, "reg", &psize);
  123. if (prop == NULL)
  124. return NULL;
  125. psize /= 4;
  126. onesize = na + ns;
  127. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  128. if (i == index) {
  129. if (size)
  130. *size = of_read_number(prop + na, ns);
  131. if (flags)
  132. *flags = bus->get_flags(prop);
  133. return prop;
  134. }
  135. return NULL;
  136. }
  137. EXPORT_SYMBOL(of_get_address);
  138. static int of_empty_ranges_quirk(const struct device_node *np)
  139. {
  140. return false;
  141. }
  142. static int of_translate_one(const struct device_node *parent,
  143. struct of_bus *bus, struct of_bus *pbus,
  144. __be32 *addr, int na, int ns, int pna,
  145. const char *rprop)
  146. {
  147. const __be32 *ranges;
  148. int rlen;
  149. int rone;
  150. u64 offset = OF_BAD_ADDR;
  151. /*
  152. * Normally, an absence of a "ranges" property means we are
  153. * crossing a non-translatable boundary, and thus the addresses
  154. * below the current cannot be converted to CPU physical ones.
  155. * Unfortunately, while this is very clear in the spec, it's not
  156. * what Apple understood, and they do have things like /uni-n or
  157. * /ht nodes with no "ranges" property and a lot of perfectly
  158. * useable mapped devices below them. Thus we treat the absence of
  159. * "ranges" as equivalent to an empty "ranges" property which means
  160. * a 1:1 translation at that level. It's up to the caller not to try
  161. * to translate addresses that aren't supposed to be translated in
  162. * the first place. --BenH.
  163. *
  164. * As far as we know, this damage only exists on Apple machines, so
  165. * This code is only enabled on powerpc. --gcl
  166. *
  167. * This quirk also applies for 'dma-ranges' which frequently exist in
  168. * child nodes without 'dma-ranges' in the parent nodes. --RobH
  169. */
  170. ranges = of_get_property(parent, rprop, &rlen);
  171. if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
  172. strcmp(rprop, "dma-ranges")) {
  173. debug("no ranges; cannot translate\n");
  174. return 1;
  175. }
  176. if (ranges == NULL || rlen == 0) {
  177. offset = of_read_number(addr, na);
  178. memset(addr, 0, pna * 4);
  179. debug("empty ranges; 1:1 translation\n");
  180. goto finish;
  181. }
  182. debug("walking ranges...\n");
  183. /* Now walk through the ranges */
  184. rlen /= 4;
  185. rone = na + pna + ns;
  186. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  187. offset = bus->map(addr, ranges, na, ns, pna);
  188. if (offset != OF_BAD_ADDR)
  189. break;
  190. }
  191. if (offset == OF_BAD_ADDR) {
  192. debug("not found !\n");
  193. return 1;
  194. }
  195. memcpy(addr, ranges + na, 4 * pna);
  196. finish:
  197. of_dump_addr("parent translation for:", addr, pna);
  198. debug("with offset: %llx\n", (unsigned long long)offset);
  199. /* Translate it into parent bus space */
  200. return pbus->translate(addr, offset, pna);
  201. }
  202. /*
  203. * Translate an address from the device-tree into a CPU physical address,
  204. * this walks up the tree and applies the various bus mappings on the
  205. * way.
  206. *
  207. * Note: We consider that crossing any level with #size-cells == 0 to mean
  208. * that translation is impossible (that is we are not dealing with a value
  209. * that can be mapped to a cpu physical address). This is not really specified
  210. * that way, but this is traditionally the way IBM at least do things
  211. */
  212. static u64 __of_translate_address(const struct device_node *dev,
  213. const __be32 *in_addr, const char *rprop)
  214. {
  215. struct device_node *parent = NULL;
  216. struct of_bus *bus, *pbus;
  217. __be32 addr[OF_MAX_ADDR_CELLS];
  218. int na, ns, pna, pns;
  219. u64 result = OF_BAD_ADDR;
  220. debug("** translation for device %s **\n", of_node_full_name(dev));
  221. /* Increase refcount at current level */
  222. (void)of_node_get(dev);
  223. /* Get parent & match bus type */
  224. parent = of_get_parent(dev);
  225. if (parent == NULL)
  226. goto bail;
  227. bus = of_match_bus(parent);
  228. /* Count address cells & copy address locally */
  229. bus->count_cells(dev, &na, &ns);
  230. if (!OF_CHECK_COUNTS(na, ns)) {
  231. debug("Bad cell count for %s\n", of_node_full_name(dev));
  232. goto bail;
  233. }
  234. memcpy(addr, in_addr, na * 4);
  235. debug("bus is %s (na=%d, ns=%d) on %s\n", bus->name, na, ns,
  236. of_node_full_name(parent));
  237. of_dump_addr("translating address:", addr, na);
  238. /* Translate */
  239. for (;;) {
  240. /* Switch to parent bus */
  241. of_node_put(dev);
  242. dev = parent;
  243. parent = of_get_parent(dev);
  244. /* If root, we have finished */
  245. if (parent == NULL) {
  246. debug("reached root node\n");
  247. result = of_read_number(addr, na);
  248. break;
  249. }
  250. /* Get new parent bus and counts */
  251. pbus = of_match_bus(parent);
  252. pbus->count_cells(dev, &pna, &pns);
  253. if (!OF_CHECK_COUNTS(pna, pns)) {
  254. debug("Bad cell count for %s\n",
  255. of_node_full_name(dev));
  256. break;
  257. }
  258. debug("parent bus is %s (na=%d, ns=%d) on %s\n", pbus->name,
  259. pna, pns, of_node_full_name(parent));
  260. /* Apply bus translation */
  261. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  262. break;
  263. /* Complete the move up one level */
  264. na = pna;
  265. ns = pns;
  266. bus = pbus;
  267. of_dump_addr("one level translation:", addr, na);
  268. }
  269. bail:
  270. of_node_put(parent);
  271. of_node_put(dev);
  272. return result;
  273. }
  274. u64 of_translate_address(const struct device_node *dev, const __be32 *in_addr)
  275. {
  276. return __of_translate_address(dev, in_addr, "ranges");
  277. }
  278. u64 of_translate_dma_address(const struct device_node *dev, const __be32 *in_addr)
  279. {
  280. return __of_translate_address(dev, in_addr, "dma-ranges");
  281. }
  282. int of_get_dma_range(const struct device_node *dev, phys_addr_t *cpu,
  283. dma_addr_t *bus, u64 *size)
  284. {
  285. bool found_dma_ranges = false;
  286. struct device_node *parent;
  287. struct of_bus *bus_node;
  288. int na, ns, pna, pns;
  289. const __be32 *ranges;
  290. int ret = 0;
  291. int len;
  292. /* Find the closest dma-ranges property */
  293. dev = of_node_get(dev);
  294. while (dev) {
  295. ranges = of_get_property(dev, "dma-ranges", &len);
  296. /* Ignore empty ranges, they imply no translation required */
  297. if (ranges && len > 0)
  298. break;
  299. /* Once we find 'dma-ranges', then a missing one is an error */
  300. if (found_dma_ranges && !ranges) {
  301. ret = -EINVAL;
  302. goto out;
  303. }
  304. if (ranges)
  305. found_dma_ranges = true;
  306. parent = of_get_parent(dev);
  307. of_node_put(dev);
  308. dev = parent;
  309. }
  310. if (!dev || !ranges) {
  311. debug("no dma-ranges found for node %s\n",
  312. of_node_full_name(dev));
  313. ret = -ENOENT;
  314. goto out;
  315. }
  316. /* switch to that node */
  317. parent = of_get_parent(dev);
  318. if (!parent) {
  319. printf("Found dma-ranges in root node, shoudln't happen\n");
  320. ret = -EINVAL;
  321. goto out;
  322. }
  323. /* Get the address sizes both for the bus and its parent */
  324. bus_node = of_match_bus((struct device_node*)dev);
  325. bus_node->count_cells(dev, &na, &ns);
  326. if (!OF_CHECK_COUNTS(na, ns)) {
  327. printf("Bad cell count for %s\n", of_node_full_name(dev));
  328. ret = -EINVAL;
  329. goto out_parent;
  330. }
  331. bus_node = of_match_bus(parent);
  332. bus_node->count_cells(parent, &pna, &pns);
  333. if (!OF_CHECK_COUNTS(pna, pns)) {
  334. printf("Bad cell count for %s\n", of_node_full_name(parent));
  335. ret = -EINVAL;
  336. goto out_parent;
  337. }
  338. *bus = of_read_number(ranges, na);
  339. *cpu = of_translate_dma_address(dev, ranges + na);
  340. *size = of_read_number(ranges + na + pna, ns);
  341. out_parent:
  342. of_node_put(parent);
  343. out:
  344. of_node_put(dev);
  345. return ret;
  346. }
  347. static int __of_address_to_resource(const struct device_node *dev,
  348. const __be32 *addrp, u64 size, unsigned int flags,
  349. const char *name, struct resource *r)
  350. {
  351. u64 taddr;
  352. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  353. return -EINVAL;
  354. taddr = of_translate_address(dev, addrp);
  355. if (taddr == OF_BAD_ADDR)
  356. return -EINVAL;
  357. memset(r, 0, sizeof(struct resource));
  358. r->start = taddr;
  359. r->end = taddr + size - 1;
  360. r->flags = flags;
  361. r->name = name ? name : dev->full_name;
  362. return 0;
  363. }
  364. int of_address_to_resource(const struct device_node *dev, int index,
  365. struct resource *r)
  366. {
  367. const __be32 *addrp;
  368. u64 size;
  369. unsigned int flags;
  370. const char *name = NULL;
  371. addrp = of_get_address(dev, index, &size, &flags);
  372. if (addrp == NULL)
  373. return -EINVAL;
  374. /* Get optional "reg-names" property to add a name to a resource */
  375. of_property_read_string_index(dev, "reg-names", index, &name);
  376. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  377. }