fdt_address.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * FDT Address translation based on u-boot fdt_support.c which in turn was
  3. * based on the kernel unflattened DT address translation code.
  4. *
  5. * (C) Copyright 2007
  6. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  7. *
  8. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. */
  15. #define pr_fmt(fmt) "OF: fdt: " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/libfdt.h>
  18. #include <linux/of.h>
  19. #include <linux/of_fdt.h>
  20. #include <linux/sizes.h>
  21. /* Max address size we deal with */
  22. #define OF_MAX_ADDR_CELLS 4
  23. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  24. (ns) > 0)
  25. /* Debug utility */
  26. #ifdef DEBUG
  27. static void __init of_dump_addr(const char *s, const __be32 *addr, int na)
  28. {
  29. pr_debug("%s", s);
  30. while(na--)
  31. pr_cont(" %08x", *(addr++));
  32. pr_cont("\n");
  33. }
  34. #else
  35. static void __init of_dump_addr(const char *s, const __be32 *addr, int na) { }
  36. #endif
  37. /* Callbacks for bus specific translators */
  38. struct of_bus {
  39. void (*count_cells)(const void *blob, int parentoffset,
  40. int *addrc, int *sizec);
  41. u64 (*map)(__be32 *addr, const __be32 *range,
  42. int na, int ns, int pna);
  43. int (*translate)(__be32 *addr, u64 offset, int na);
  44. };
  45. /* Default translator (generic bus) */
  46. static void __init fdt_bus_default_count_cells(const void *blob, int parentoffset,
  47. int *addrc, int *sizec)
  48. {
  49. const __be32 *prop;
  50. if (addrc) {
  51. prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
  52. if (prop)
  53. *addrc = be32_to_cpup(prop);
  54. else
  55. *addrc = dt_root_addr_cells;
  56. }
  57. if (sizec) {
  58. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  59. if (prop)
  60. *sizec = be32_to_cpup(prop);
  61. else
  62. *sizec = dt_root_size_cells;
  63. }
  64. }
  65. static u64 __init fdt_bus_default_map(__be32 *addr, const __be32 *range,
  66. int na, int ns, int pna)
  67. {
  68. u64 cp, s, da;
  69. cp = of_read_number(range, na);
  70. s = of_read_number(range + na + pna, ns);
  71. da = of_read_number(addr, na);
  72. pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
  73. cp, s, da);
  74. if (da < cp || da >= (cp + s))
  75. return OF_BAD_ADDR;
  76. return da - cp;
  77. }
  78. static int __init fdt_bus_default_translate(__be32 *addr, u64 offset, int na)
  79. {
  80. u64 a = of_read_number(addr, na);
  81. memset(addr, 0, na * 4);
  82. a += offset;
  83. if (na > 1)
  84. addr[na - 2] = cpu_to_fdt32(a >> 32);
  85. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  86. return 0;
  87. }
  88. /* Array of bus specific translators */
  89. static const struct of_bus of_busses[] __initconst = {
  90. /* Default */
  91. {
  92. .count_cells = fdt_bus_default_count_cells,
  93. .map = fdt_bus_default_map,
  94. .translate = fdt_bus_default_translate,
  95. },
  96. };
  97. static int __init fdt_translate_one(const void *blob, int parent,
  98. const struct of_bus *bus,
  99. const struct of_bus *pbus, __be32 *addr,
  100. int na, int ns, int pna, const char *rprop)
  101. {
  102. const __be32 *ranges;
  103. int rlen;
  104. int rone;
  105. u64 offset = OF_BAD_ADDR;
  106. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  107. if (!ranges)
  108. return 1;
  109. if (rlen == 0) {
  110. offset = of_read_number(addr, na);
  111. memset(addr, 0, pna * 4);
  112. pr_debug("empty ranges, 1:1 translation\n");
  113. goto finish;
  114. }
  115. pr_debug("walking ranges...\n");
  116. /* Now walk through the ranges */
  117. rlen /= 4;
  118. rone = na + pna + ns;
  119. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  120. offset = bus->map(addr, ranges, na, ns, pna);
  121. if (offset != OF_BAD_ADDR)
  122. break;
  123. }
  124. if (offset == OF_BAD_ADDR) {
  125. pr_debug("not found !\n");
  126. return 1;
  127. }
  128. memcpy(addr, ranges + na, 4 * pna);
  129. finish:
  130. of_dump_addr("parent translation for:", addr, pna);
  131. pr_debug("with offset: %llx\n", offset);
  132. /* Translate it into parent bus space */
  133. return pbus->translate(addr, offset, pna);
  134. }
  135. /*
  136. * Translate an address from the device-tree into a CPU physical address,
  137. * this walks up the tree and applies the various bus mappings on the
  138. * way.
  139. *
  140. * Note: We consider that crossing any level with #size-cells == 0 to mean
  141. * that translation is impossible (that is we are not dealing with a value
  142. * that can be mapped to a cpu physical address). This is not really specified
  143. * that way, but this is traditionally the way IBM at least do things
  144. */
  145. static u64 __init fdt_translate_address(const void *blob, int node_offset)
  146. {
  147. int parent, len;
  148. const struct of_bus *bus, *pbus;
  149. const __be32 *reg;
  150. __be32 addr[OF_MAX_ADDR_CELLS];
  151. int na, ns, pna, pns;
  152. u64 result = OF_BAD_ADDR;
  153. pr_debug("** translation for device %s **\n",
  154. fdt_get_name(blob, node_offset, NULL));
  155. reg = fdt_getprop(blob, node_offset, "reg", &len);
  156. if (!reg) {
  157. pr_err("warning: device tree node '%s' has no address.\n",
  158. fdt_get_name(blob, node_offset, NULL));
  159. goto bail;
  160. }
  161. /* Get parent & match bus type */
  162. parent = fdt_parent_offset(blob, node_offset);
  163. if (parent < 0)
  164. goto bail;
  165. bus = &of_busses[0];
  166. /* Cound address cells & copy address locally */
  167. bus->count_cells(blob, parent, &na, &ns);
  168. if (!OF_CHECK_COUNTS(na, ns)) {
  169. pr_err("Bad cell count for %s\n",
  170. fdt_get_name(blob, node_offset, NULL));
  171. goto bail;
  172. }
  173. memcpy(addr, reg, na * 4);
  174. pr_debug("bus (na=%d, ns=%d) on %s\n",
  175. na, ns, fdt_get_name(blob, parent, NULL));
  176. of_dump_addr("translating address:", addr, na);
  177. /* Translate */
  178. for (;;) {
  179. /* Switch to parent bus */
  180. node_offset = parent;
  181. parent = fdt_parent_offset(blob, node_offset);
  182. /* If root, we have finished */
  183. if (parent < 0) {
  184. pr_debug("reached root node\n");
  185. result = of_read_number(addr, na);
  186. break;
  187. }
  188. /* Get new parent bus and counts */
  189. pbus = &of_busses[0];
  190. pbus->count_cells(blob, parent, &pna, &pns);
  191. if (!OF_CHECK_COUNTS(pna, pns)) {
  192. pr_err("Bad cell count for %s\n",
  193. fdt_get_name(blob, node_offset, NULL));
  194. break;
  195. }
  196. pr_debug("parent bus (na=%d, ns=%d) on %s\n",
  197. pna, pns, fdt_get_name(blob, parent, NULL));
  198. /* Apply bus translation */
  199. if (fdt_translate_one(blob, node_offset, bus, pbus,
  200. addr, na, ns, pna, "ranges"))
  201. break;
  202. /* Complete the move up one level */
  203. na = pna;
  204. ns = pns;
  205. bus = pbus;
  206. of_dump_addr("one level translation:", addr, na);
  207. }
  208. bail:
  209. return result;
  210. }
  211. /**
  212. * of_flat_dt_translate_address - translate DT addr into CPU phys addr
  213. * @node: node in the flat blob
  214. */
  215. u64 __init of_flat_dt_translate_address(unsigned long node)
  216. {
  217. return fdt_translate_address(initial_boot_params, node);
  218. }