of_addr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Taken from Linux v4.9 drivers/of/address.c
  3. *
  4. * Modified for U-Boot
  5. * Copyright (c) 2017 Google, Inc
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <libfdt.h>
  11. #include <dm/of_access.h>
  12. #include <dm/of_addr.h>
  13. #include <linux/err.h>
  14. #include <linux/ioport.h>
  15. /* Max address size we deal with */
  16. #define OF_MAX_ADDR_CELLS 4
  17. #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
  18. #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
  19. static struct of_bus *of_match_bus(struct device_node *np);
  20. /* Debug utility */
  21. #ifdef DEBUG
  22. static void of_dump_addr(const char *s, const __be32 *addr, int na)
  23. {
  24. debug("%s", s);
  25. while (na--)
  26. pr_cont(" %08x", be32_to_cpu(*(addr++)));
  27. pr_cont("\n");
  28. }
  29. #else
  30. static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
  31. #endif
  32. /* Callbacks for bus specific translators */
  33. struct of_bus {
  34. const char *name;
  35. const char *addresses;
  36. int (*match)(struct device_node *parent);
  37. void (*count_cells)(const struct device_node *child, int *addrc,
  38. int *sizec);
  39. u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna);
  40. int (*translate)(__be32 *addr, u64 offset, int na);
  41. unsigned int (*get_flags)(const __be32 *addr);
  42. };
  43. static void of_bus_default_count_cells(const struct device_node *np,
  44. int *addrc, int *sizec)
  45. {
  46. if (addrc)
  47. *addrc = of_n_addr_cells(np);
  48. if (sizec)
  49. *sizec = of_n_size_cells(np);
  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. debug("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. /*
  80. * Array of bus-specific translators
  81. */
  82. static struct of_bus of_busses[] = {
  83. /* Default */
  84. {
  85. .name = "default",
  86. .addresses = "reg",
  87. .match = NULL,
  88. .count_cells = of_bus_default_count_cells,
  89. .map = of_bus_default_map,
  90. .translate = of_bus_default_translate,
  91. .get_flags = of_bus_default_get_flags,
  92. },
  93. };
  94. static struct of_bus *of_match_bus(struct device_node *np)
  95. {
  96. int i;
  97. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  98. if (!of_busses[i].match || of_busses[i].match(np))
  99. return &of_busses[i];
  100. BUG();
  101. return NULL;
  102. }
  103. static void dev_count_cells(const struct device_node *np, int *nap, int *nsp)
  104. {
  105. of_bus_default_count_cells(np, nap, nsp);
  106. }
  107. const __be32 *of_get_address(const struct device_node *dev, int index,
  108. u64 *size, unsigned int *flags)
  109. {
  110. const __be32 *prop;
  111. int psize;
  112. struct device_node *parent;
  113. struct of_bus *bus;
  114. int onesize, i, na, ns;
  115. /* Get parent & match bus type */
  116. parent = of_get_parent(dev);
  117. if (parent == NULL)
  118. return NULL;
  119. dev_count_cells(dev, &na, &ns);
  120. bus = of_match_bus(parent);
  121. bus->count_cells(dev, &na, &ns);
  122. of_node_put(parent);
  123. if (!OF_CHECK_ADDR_COUNT(na))
  124. return NULL;
  125. /* Get "reg" or "assigned-addresses" property */
  126. prop = of_get_property(dev, "reg", &psize);
  127. if (prop == NULL)
  128. return NULL;
  129. psize /= 4;
  130. onesize = na + ns;
  131. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  132. if (i == index) {
  133. if (size)
  134. *size = of_read_number(prop + na, ns);
  135. if (flags)
  136. *flags = bus->get_flags(prop);
  137. return prop;
  138. }
  139. return NULL;
  140. }
  141. EXPORT_SYMBOL(of_get_address);
  142. static int of_empty_ranges_quirk(const struct device_node *np)
  143. {
  144. return false;
  145. }
  146. static int of_translate_one(const struct device_node *parent,
  147. struct of_bus *bus, struct of_bus *pbus,
  148. __be32 *addr, int na, int ns, int pna,
  149. const char *rprop)
  150. {
  151. const __be32 *ranges;
  152. int rlen;
  153. int rone;
  154. u64 offset = OF_BAD_ADDR;
  155. /*
  156. * Normally, an absence of a "ranges" property means we are
  157. * crossing a non-translatable boundary, and thus the addresses
  158. * below the current cannot be converted to CPU physical ones.
  159. * Unfortunately, while this is very clear in the spec, it's not
  160. * what Apple understood, and they do have things like /uni-n or
  161. * /ht nodes with no "ranges" property and a lot of perfectly
  162. * useable mapped devices below them. Thus we treat the absence of
  163. * "ranges" as equivalent to an empty "ranges" property which means
  164. * a 1:1 translation at that level. It's up to the caller not to try
  165. * to translate addresses that aren't supposed to be translated in
  166. * the first place. --BenH.
  167. *
  168. * As far as we know, this damage only exists on Apple machines, so
  169. * This code is only enabled on powerpc. --gcl
  170. */
  171. ranges = of_get_property(parent, rprop, &rlen);
  172. if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
  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. static int __of_address_to_resource(const struct device_node *dev,
  279. const __be32 *addrp, u64 size, unsigned int flags,
  280. const char *name, struct resource *r)
  281. {
  282. u64 taddr;
  283. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  284. return -EINVAL;
  285. taddr = of_translate_address(dev, addrp);
  286. if (taddr == OF_BAD_ADDR)
  287. return -EINVAL;
  288. memset(r, 0, sizeof(struct resource));
  289. r->start = taddr;
  290. r->end = taddr + size - 1;
  291. r->flags = flags;
  292. r->name = name ? name : dev->full_name;
  293. return 0;
  294. }
  295. int of_address_to_resource(const struct device_node *dev, int index,
  296. struct resource *r)
  297. {
  298. const __be32 *addrp;
  299. u64 size;
  300. unsigned int flags;
  301. const char *name = NULL;
  302. addrp = of_get_address(dev, index, &size, &flags);
  303. if (addrp == NULL)
  304. return -EINVAL;
  305. /* Get optional "reg-names" property to add a name to a resource */
  306. of_property_read_string_index(dev, "reg-names", index, &name);
  307. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  308. }