of_addr.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. static void dev_count_cells(const struct device_node *np, int *nap, int *nsp)
  105. {
  106. of_bus_default_count_cells(np, nap, nsp);
  107. }
  108. const __be32 *of_get_address(const struct device_node *dev, int index,
  109. u64 *size, unsigned int *flags)
  110. {
  111. const __be32 *prop;
  112. int psize;
  113. struct device_node *parent;
  114. struct of_bus *bus;
  115. int onesize, i, na, ns;
  116. /* Get parent & match bus type */
  117. parent = of_get_parent(dev);
  118. if (parent == NULL)
  119. return NULL;
  120. dev_count_cells(dev, &na, &ns);
  121. bus = of_match_bus(parent);
  122. bus->count_cells(dev, &na, &ns);
  123. of_node_put(parent);
  124. if (!OF_CHECK_ADDR_COUNT(na))
  125. return NULL;
  126. /* Get "reg" or "assigned-addresses" property */
  127. prop = of_get_property(dev, "reg", &psize);
  128. if (prop == NULL)
  129. return NULL;
  130. psize /= 4;
  131. onesize = na + ns;
  132. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  133. if (i == index) {
  134. if (size)
  135. *size = of_read_number(prop + na, ns);
  136. if (flags)
  137. *flags = bus->get_flags(prop);
  138. return prop;
  139. }
  140. return NULL;
  141. }
  142. EXPORT_SYMBOL(of_get_address);
  143. static int of_empty_ranges_quirk(const struct device_node *np)
  144. {
  145. return false;
  146. }
  147. static int of_translate_one(const struct device_node *parent,
  148. struct of_bus *bus, struct of_bus *pbus,
  149. __be32 *addr, int na, int ns, int pna,
  150. const char *rprop)
  151. {
  152. const __be32 *ranges;
  153. int rlen;
  154. int rone;
  155. u64 offset = OF_BAD_ADDR;
  156. /*
  157. * Normally, an absence of a "ranges" property means we are
  158. * crossing a non-translatable boundary, and thus the addresses
  159. * below the current cannot be converted to CPU physical ones.
  160. * Unfortunately, while this is very clear in the spec, it's not
  161. * what Apple understood, and they do have things like /uni-n or
  162. * /ht nodes with no "ranges" property and a lot of perfectly
  163. * useable mapped devices below them. Thus we treat the absence of
  164. * "ranges" as equivalent to an empty "ranges" property which means
  165. * a 1:1 translation at that level. It's up to the caller not to try
  166. * to translate addresses that aren't supposed to be translated in
  167. * the first place. --BenH.
  168. *
  169. * As far as we know, this damage only exists on Apple machines, so
  170. * This code is only enabled on powerpc. --gcl
  171. */
  172. ranges = of_get_property(parent, rprop, &rlen);
  173. if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
  174. debug("no ranges; cannot translate\n");
  175. return 1;
  176. }
  177. if (ranges == NULL || rlen == 0) {
  178. offset = of_read_number(addr, na);
  179. memset(addr, 0, pna * 4);
  180. debug("empty ranges; 1:1 translation\n");
  181. goto finish;
  182. }
  183. debug("walking ranges...\n");
  184. /* Now walk through the ranges */
  185. rlen /= 4;
  186. rone = na + pna + ns;
  187. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  188. offset = bus->map(addr, ranges, na, ns, pna);
  189. if (offset != OF_BAD_ADDR)
  190. break;
  191. }
  192. if (offset == OF_BAD_ADDR) {
  193. debug("not found !\n");
  194. return 1;
  195. }
  196. memcpy(addr, ranges + na, 4 * pna);
  197. finish:
  198. of_dump_addr("parent translation for:", addr, pna);
  199. debug("with offset: %llx\n", (unsigned long long)offset);
  200. /* Translate it into parent bus space */
  201. return pbus->translate(addr, offset, pna);
  202. }
  203. /*
  204. * Translate an address from the device-tree into a CPU physical address,
  205. * this walks up the tree and applies the various bus mappings on the
  206. * way.
  207. *
  208. * Note: We consider that crossing any level with #size-cells == 0 to mean
  209. * that translation is impossible (that is we are not dealing with a value
  210. * that can be mapped to a cpu physical address). This is not really specified
  211. * that way, but this is traditionally the way IBM at least do things
  212. */
  213. static u64 __of_translate_address(const struct device_node *dev,
  214. const __be32 *in_addr, const char *rprop)
  215. {
  216. struct device_node *parent = NULL;
  217. struct of_bus *bus, *pbus;
  218. __be32 addr[OF_MAX_ADDR_CELLS];
  219. int na, ns, pna, pns;
  220. u64 result = OF_BAD_ADDR;
  221. debug("** translation for device %s **\n", of_node_full_name(dev));
  222. /* Increase refcount at current level */
  223. (void)of_node_get(dev);
  224. /* Get parent & match bus type */
  225. parent = of_get_parent(dev);
  226. if (parent == NULL)
  227. goto bail;
  228. bus = of_match_bus(parent);
  229. /* Count address cells & copy address locally */
  230. bus->count_cells(dev, &na, &ns);
  231. if (!OF_CHECK_COUNTS(na, ns)) {
  232. debug("Bad cell count for %s\n", of_node_full_name(dev));
  233. goto bail;
  234. }
  235. memcpy(addr, in_addr, na * 4);
  236. debug("bus is %s (na=%d, ns=%d) on %s\n", bus->name, na, ns,
  237. of_node_full_name(parent));
  238. of_dump_addr("translating address:", addr, na);
  239. /* Translate */
  240. for (;;) {
  241. /* Switch to parent bus */
  242. of_node_put(dev);
  243. dev = parent;
  244. parent = of_get_parent(dev);
  245. /* If root, we have finished */
  246. if (parent == NULL) {
  247. debug("reached root node\n");
  248. result = of_read_number(addr, na);
  249. break;
  250. }
  251. /* Get new parent bus and counts */
  252. pbus = of_match_bus(parent);
  253. pbus->count_cells(dev, &pna, &pns);
  254. if (!OF_CHECK_COUNTS(pna, pns)) {
  255. debug("Bad cell count for %s\n",
  256. of_node_full_name(dev));
  257. break;
  258. }
  259. debug("parent bus is %s (na=%d, ns=%d) on %s\n", pbus->name,
  260. pna, pns, of_node_full_name(parent));
  261. /* Apply bus translation */
  262. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  263. break;
  264. /* Complete the move up one level */
  265. na = pna;
  266. ns = pns;
  267. bus = pbus;
  268. of_dump_addr("one level translation:", addr, na);
  269. }
  270. bail:
  271. of_node_put(parent);
  272. of_node_put(dev);
  273. return result;
  274. }
  275. u64 of_translate_address(const struct device_node *dev, const __be32 *in_addr)
  276. {
  277. return __of_translate_address(dev, in_addr, "ranges");
  278. }
  279. u64 of_translate_dma_address(const struct device_node *dev, const __be32 *in_addr)
  280. {
  281. return __of_translate_address(dev, in_addr, "dma-ranges");
  282. }
  283. static int __of_address_to_resource(const struct device_node *dev,
  284. const __be32 *addrp, u64 size, unsigned int flags,
  285. const char *name, struct resource *r)
  286. {
  287. u64 taddr;
  288. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  289. return -EINVAL;
  290. taddr = of_translate_address(dev, addrp);
  291. if (taddr == OF_BAD_ADDR)
  292. return -EINVAL;
  293. memset(r, 0, sizeof(struct resource));
  294. r->start = taddr;
  295. r->end = taddr + size - 1;
  296. r->flags = flags;
  297. r->name = name ? name : dev->full_name;
  298. return 0;
  299. }
  300. int of_address_to_resource(const struct device_node *dev, int index,
  301. struct resource *r)
  302. {
  303. const __be32 *addrp;
  304. u64 size;
  305. unsigned int flags;
  306. const char *name = NULL;
  307. addrp = of_get_address(dev, index, &size, &flags);
  308. if (addrp == NULL)
  309. return -EINVAL;
  310. /* Get optional "reg-names" property to add a name to a resource */
  311. of_property_read_string_index(dev, "reg-names", index, &name);
  312. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  313. }