fdt_address.c 6.2 KB

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