of_addr.c 9.2 KB

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