1
2
3
4
5
6
7
8
9
10
11
12
13
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
21
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
26
27#ifdef DEBUG
28static void __init of_dump_addr(const char *s, const __be32 *addr, int na)
29{
30 pr_debug("%s", s);
31 while(na--)
32 pr_cont(" %08x", *(addr++));
33 pr_debug("\n");
34}
35#else
36static void __init of_dump_addr(const char *s, const __be32 *addr, int na) { }
37#endif
38
39
40struct of_bus {
41 void (*count_cells)(const void *blob, int parentoffset,
42 int *addrc, int *sizec);
43 u64 (*map)(__be32 *addr, const __be32 *range,
44 int na, int ns, int pna);
45 int (*translate)(__be32 *addr, u64 offset, int na);
46};
47
48
49static void __init fdt_bus_default_count_cells(const void *blob, int parentoffset,
50 int *addrc, int *sizec)
51{
52 const __be32 *prop;
53
54 if (addrc) {
55 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
56 if (prop)
57 *addrc = be32_to_cpup(prop);
58 else
59 *addrc = dt_root_addr_cells;
60 }
61
62 if (sizec) {
63 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
64 if (prop)
65 *sizec = be32_to_cpup(prop);
66 else
67 *sizec = dt_root_size_cells;
68 }
69}
70
71static u64 __init fdt_bus_default_map(__be32 *addr, const __be32 *range,
72 int na, int ns, int pna)
73{
74 u64 cp, s, da;
75
76 cp = of_read_number(range, na);
77 s = of_read_number(range + na + pna, ns);
78 da = of_read_number(addr, na);
79
80 pr_debug("FDT: default map, cp=%llx, s=%llx, da=%llx\n",
81 cp, s, da);
82
83 if (da < cp || da >= (cp + s))
84 return OF_BAD_ADDR;
85 return da - cp;
86}
87
88static int __init fdt_bus_default_translate(__be32 *addr, u64 offset, int na)
89{
90 u64 a = of_read_number(addr, na);
91 memset(addr, 0, na * 4);
92 a += offset;
93 if (na > 1)
94 addr[na - 2] = cpu_to_fdt32(a >> 32);
95 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
96
97 return 0;
98}
99
100
101static const struct of_bus of_busses[] __initconst = {
102
103 {
104 .count_cells = fdt_bus_default_count_cells,
105 .map = fdt_bus_default_map,
106 .translate = fdt_bus_default_translate,
107 },
108};
109
110static int __init fdt_translate_one(const void *blob, int parent,
111 const struct of_bus *bus,
112 const struct of_bus *pbus, __be32 *addr,
113 int na, int ns, int pna, const char *rprop)
114{
115 const __be32 *ranges;
116 int rlen;
117 int rone;
118 u64 offset = OF_BAD_ADDR;
119
120 ranges = fdt_getprop(blob, parent, rprop, &rlen);
121 if (!ranges)
122 return 1;
123 if (rlen == 0) {
124 offset = of_read_number(addr, na);
125 memset(addr, 0, pna * 4);
126 pr_debug("FDT: empty ranges, 1:1 translation\n");
127 goto finish;
128 }
129
130 pr_debug("FDT: walking ranges...\n");
131
132
133 rlen /= 4;
134 rone = na + pna + ns;
135 for (; rlen >= rone; rlen -= rone, ranges += rone) {
136 offset = bus->map(addr, ranges, na, ns, pna);
137 if (offset != OF_BAD_ADDR)
138 break;
139 }
140 if (offset == OF_BAD_ADDR) {
141 pr_debug("FDT: not found !\n");
142 return 1;
143 }
144 memcpy(addr, ranges + na, 4 * pna);
145
146 finish:
147 of_dump_addr("FDT: parent translation for:", addr, pna);
148 pr_debug("FDT: with offset: %llx\n", offset);
149
150
151 return pbus->translate(addr, offset, pna);
152}
153
154
155
156
157
158
159
160
161
162
163
164u64 __init fdt_translate_address(const void *blob, int node_offset)
165{
166 int parent, len;
167 const struct of_bus *bus, *pbus;
168 const __be32 *reg;
169 __be32 addr[OF_MAX_ADDR_CELLS];
170 int na, ns, pna, pns;
171 u64 result = OF_BAD_ADDR;
172
173 pr_debug("FDT: ** translation for device %s **\n",
174 fdt_get_name(blob, node_offset, NULL));
175
176 reg = fdt_getprop(blob, node_offset, "reg", &len);
177 if (!reg) {
178 pr_err("FDT: warning: device tree node '%s' has no address.\n",
179 fdt_get_name(blob, node_offset, NULL));
180 goto bail;
181 }
182
183
184 parent = fdt_parent_offset(blob, node_offset);
185 if (parent < 0)
186 goto bail;
187 bus = &of_busses[0];
188
189
190 bus->count_cells(blob, parent, &na, &ns);
191 if (!OF_CHECK_COUNTS(na, ns)) {
192 pr_err("FDT: Bad cell count for %s\n",
193 fdt_get_name(blob, node_offset, NULL));
194 goto bail;
195 }
196 memcpy(addr, reg, na * 4);
197
198 pr_debug("FDT: bus (na=%d, ns=%d) on %s\n",
199 na, ns, fdt_get_name(blob, parent, NULL));
200 of_dump_addr("OF: translating address:", addr, na);
201
202
203 for (;;) {
204
205 node_offset = parent;
206 parent = fdt_parent_offset(blob, node_offset);
207
208
209 if (parent < 0) {
210 pr_debug("FDT: reached root node\n");
211 result = of_read_number(addr, na);
212 break;
213 }
214
215
216 pbus = &of_busses[0];
217 pbus->count_cells(blob, parent, &pna, &pns);
218 if (!OF_CHECK_COUNTS(pna, pns)) {
219 pr_err("FDT: Bad cell count for %s\n",
220 fdt_get_name(blob, node_offset, NULL));
221 break;
222 }
223
224 pr_debug("FDT: parent bus (na=%d, ns=%d) on %s\n",
225 pna, pns, fdt_get_name(blob, parent, NULL));
226
227
228 if (fdt_translate_one(blob, node_offset, bus, pbus,
229 addr, na, ns, pna, "ranges"))
230 break;
231
232
233 na = pna;
234 ns = pns;
235 bus = pbus;
236
237 of_dump_addr("FDT: one level translation:", addr, na);
238 }
239 bail:
240 return result;
241}
242