]> git.ipfire.org Git - thirdparty/u-boot.git/blob - drivers/core/of_addr.c
of: addr: Translate 'dma-ranges' for parent nodes missing 'dma-ranges'
[thirdparty/u-boot.git] / drivers / core / of_addr.c
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
9 #include <common.h>
10 #include <log.h>
11 #include <linux/bug.h>
12 #include <linux/libfdt.h>
13 #include <dm/of_access.h>
14 #include <dm/of_addr.h>
15 #include <linux/err.h>
16 #include <linux/ioport.h>
17
18 /* Max address size we deal with */
19 #define OF_MAX_ADDR_CELLS 4
20 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
21 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
22
23 static struct of_bus *of_match_bus(struct device_node *np);
24
25 /* Debug utility */
26 #ifdef DEBUG
27 static void of_dump_addr(const char *s, const __be32 *addr, int na)
28 {
29 debug("%s", s);
30 while (na--)
31 pr_cont(" %08x", be32_to_cpu(*(addr++)));
32 pr_cont("\n");
33 }
34 #else
35 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
36 #endif
37
38 /* Callbacks for bus specific translators */
39 struct of_bus {
40 const char *name;
41 const char *addresses;
42 int (*match)(struct device_node *parent);
43 void (*count_cells)(const struct device_node *child, int *addrc,
44 int *sizec);
45 u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna);
46 int (*translate)(__be32 *addr, u64 offset, int na);
47 unsigned int (*get_flags)(const __be32 *addr);
48 };
49
50 static void of_bus_default_count_cells(const struct device_node *np,
51 int *addrc, int *sizec)
52 {
53 if (addrc)
54 *addrc = of_n_addr_cells(np);
55 if (sizec)
56 *sizec = of_n_size_cells(np);
57 }
58
59 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
60 int na, int ns, int pna)
61 {
62 u64 cp, s, da;
63
64 cp = of_read_number(range, na);
65 s = of_read_number(range + na + pna, ns);
66 da = of_read_number(addr, na);
67
68 debug("default map, cp=%llx, s=%llx, da=%llx\n",
69 (unsigned long long)cp, (unsigned long long)s,
70 (unsigned long long)da);
71
72 if (da < cp || da >= (cp + s))
73 return OF_BAD_ADDR;
74 return da - cp;
75 }
76
77 static int of_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_be32(a >> 32);
84 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
85
86 return 0;
87 }
88
89 static unsigned int of_bus_default_get_flags(const __be32 *addr)
90 {
91 return IORESOURCE_MEM;
92 }
93
94 /*
95 * Array of bus-specific translators
96 */
97 static struct of_bus of_busses[] = {
98 /* Default */
99 {
100 .name = "default",
101 .addresses = "reg",
102 .match = NULL,
103 .count_cells = of_bus_default_count_cells,
104 .map = of_bus_default_map,
105 .translate = of_bus_default_translate,
106 .get_flags = of_bus_default_get_flags,
107 },
108 };
109
110 static struct of_bus *of_match_bus(struct device_node *np)
111 {
112 int i;
113
114 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
115 if (!of_busses[i].match || of_busses[i].match(np))
116 return &of_busses[i];
117 BUG();
118 return NULL;
119 }
120
121 static void dev_count_cells(const struct device_node *np, int *nap, int *nsp)
122 {
123 of_bus_default_count_cells(np, nap, nsp);
124 }
125
126 const __be32 *of_get_address(const struct device_node *dev, int index,
127 u64 *size, unsigned int *flags)
128 {
129 const __be32 *prop;
130 int psize;
131 struct device_node *parent;
132 struct of_bus *bus;
133 int onesize, i, na, ns;
134
135 /* Get parent & match bus type */
136 parent = of_get_parent(dev);
137 if (parent == NULL)
138 return NULL;
139 dev_count_cells(dev, &na, &ns);
140 bus = of_match_bus(parent);
141 bus->count_cells(dev, &na, &ns);
142 of_node_put(parent);
143 if (!OF_CHECK_ADDR_COUNT(na))
144 return NULL;
145
146 /* Get "reg" or "assigned-addresses" property */
147 prop = of_get_property(dev, "reg", &psize);
148 if (prop == NULL)
149 return NULL;
150 psize /= 4;
151
152 onesize = na + ns;
153 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
154 if (i == index) {
155 if (size)
156 *size = of_read_number(prop + na, ns);
157 if (flags)
158 *flags = bus->get_flags(prop);
159 return prop;
160 }
161 return NULL;
162 }
163 EXPORT_SYMBOL(of_get_address);
164
165 static int of_empty_ranges_quirk(const struct device_node *np)
166 {
167 return false;
168 }
169
170 static int of_translate_one(const struct device_node *parent,
171 struct of_bus *bus, struct of_bus *pbus,
172 __be32 *addr, int na, int ns, int pna,
173 const char *rprop)
174 {
175 const __be32 *ranges;
176 int rlen;
177 int rone;
178 u64 offset = OF_BAD_ADDR;
179
180 /*
181 * Normally, an absence of a "ranges" property means we are
182 * crossing a non-translatable boundary, and thus the addresses
183 * below the current cannot be converted to CPU physical ones.
184 * Unfortunately, while this is very clear in the spec, it's not
185 * what Apple understood, and they do have things like /uni-n or
186 * /ht nodes with no "ranges" property and a lot of perfectly
187 * useable mapped devices below them. Thus we treat the absence of
188 * "ranges" as equivalent to an empty "ranges" property which means
189 * a 1:1 translation at that level. It's up to the caller not to try
190 * to translate addresses that aren't supposed to be translated in
191 * the first place. --BenH.
192 *
193 * As far as we know, this damage only exists on Apple machines, so
194 * This code is only enabled on powerpc. --gcl
195 *
196 * This quirk also applies for 'dma-ranges' which frequently exist in
197 * child nodes without 'dma-ranges' in the parent nodes. --RobH
198 */
199 ranges = of_get_property(parent, rprop, &rlen);
200 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
201 strcmp(rprop, "dma-ranges")) {
202 debug("no ranges; cannot translate\n");
203 return 1;
204 }
205 if (ranges == NULL || rlen == 0) {
206 offset = of_read_number(addr, na);
207 memset(addr, 0, pna * 4);
208 debug("empty ranges; 1:1 translation\n");
209 goto finish;
210 }
211
212 debug("walking ranges...\n");
213
214 /* Now walk through the ranges */
215 rlen /= 4;
216 rone = na + pna + ns;
217 for (; rlen >= rone; rlen -= rone, ranges += rone) {
218 offset = bus->map(addr, ranges, na, ns, pna);
219 if (offset != OF_BAD_ADDR)
220 break;
221 }
222 if (offset == OF_BAD_ADDR) {
223 debug("not found !\n");
224 return 1;
225 }
226 memcpy(addr, ranges + na, 4 * pna);
227
228 finish:
229 of_dump_addr("parent translation for:", addr, pna);
230 debug("with offset: %llx\n", (unsigned long long)offset);
231
232 /* Translate it into parent bus space */
233 return pbus->translate(addr, offset, pna);
234 }
235
236 /*
237 * Translate an address from the device-tree into a CPU physical address,
238 * this walks up the tree and applies the various bus mappings on the
239 * way.
240 *
241 * Note: We consider that crossing any level with #size-cells == 0 to mean
242 * that translation is impossible (that is we are not dealing with a value
243 * that can be mapped to a cpu physical address). This is not really specified
244 * that way, but this is traditionally the way IBM at least do things
245 */
246 static u64 __of_translate_address(const struct device_node *dev,
247 const __be32 *in_addr, const char *rprop)
248 {
249 struct device_node *parent = NULL;
250 struct of_bus *bus, *pbus;
251 __be32 addr[OF_MAX_ADDR_CELLS];
252 int na, ns, pna, pns;
253 u64 result = OF_BAD_ADDR;
254
255 debug("** translation for device %s **\n", of_node_full_name(dev));
256
257 /* Increase refcount at current level */
258 (void)of_node_get(dev);
259
260 /* Get parent & match bus type */
261 parent = of_get_parent(dev);
262 if (parent == NULL)
263 goto bail;
264 bus = of_match_bus(parent);
265
266 /* Count address cells & copy address locally */
267 bus->count_cells(dev, &na, &ns);
268 if (!OF_CHECK_COUNTS(na, ns)) {
269 debug("Bad cell count for %s\n", of_node_full_name(dev));
270 goto bail;
271 }
272 memcpy(addr, in_addr, na * 4);
273
274 debug("bus is %s (na=%d, ns=%d) on %s\n", bus->name, na, ns,
275 of_node_full_name(parent));
276 of_dump_addr("translating address:", addr, na);
277
278 /* Translate */
279 for (;;) {
280 /* Switch to parent bus */
281 of_node_put(dev);
282 dev = parent;
283 parent = of_get_parent(dev);
284
285 /* If root, we have finished */
286 if (parent == NULL) {
287 debug("reached root node\n");
288 result = of_read_number(addr, na);
289 break;
290 }
291
292 /* Get new parent bus and counts */
293 pbus = of_match_bus(parent);
294 pbus->count_cells(dev, &pna, &pns);
295 if (!OF_CHECK_COUNTS(pna, pns)) {
296 debug("Bad cell count for %s\n",
297 of_node_full_name(dev));
298 break;
299 }
300
301 debug("parent bus is %s (na=%d, ns=%d) on %s\n", pbus->name,
302 pna, pns, of_node_full_name(parent));
303
304 /* Apply bus translation */
305 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
306 break;
307
308 /* Complete the move up one level */
309 na = pna;
310 ns = pns;
311 bus = pbus;
312
313 of_dump_addr("one level translation:", addr, na);
314 }
315 bail:
316 of_node_put(parent);
317 of_node_put(dev);
318
319 return result;
320 }
321
322 u64 of_translate_address(const struct device_node *dev, const __be32 *in_addr)
323 {
324 return __of_translate_address(dev, in_addr, "ranges");
325 }
326
327 u64 of_translate_dma_address(const struct device_node *dev, const __be32 *in_addr)
328 {
329 return __of_translate_address(dev, in_addr, "dma-ranges");
330 }
331
332 int of_get_dma_range(const struct device_node *dev, phys_addr_t *cpu,
333 dma_addr_t *bus, u64 *size)
334 {
335 bool found_dma_ranges = false;
336 struct device_node *parent;
337 struct of_bus *bus_node;
338 int na, ns, pna, pns;
339 const __be32 *ranges;
340 int ret = 0;
341 int len;
342
343 /* Find the closest dma-ranges property */
344 dev = of_node_get(dev);
345 while (dev) {
346 ranges = of_get_property(dev, "dma-ranges", &len);
347
348 /* Ignore empty ranges, they imply no translation required */
349 if (ranges && len > 0)
350 break;
351
352 /* Once we find 'dma-ranges', then a missing one is an error */
353 if (found_dma_ranges && !ranges) {
354 ret = -EINVAL;
355 goto out;
356 }
357
358 if (ranges)
359 found_dma_ranges = true;
360
361 parent = of_get_parent(dev);
362 of_node_put(dev);
363 dev = parent;
364 }
365
366 if (!dev || !ranges) {
367 debug("no dma-ranges found for node %s\n",
368 of_node_full_name(dev));
369 ret = -ENOENT;
370 goto out;
371 }
372
373 /* switch to that node */
374 parent = of_get_parent(dev);
375 if (!parent) {
376 printf("Found dma-ranges in root node, shoudln't happen\n");
377 ret = -EINVAL;
378 goto out;
379 }
380
381 /* Get the address sizes both for the bus and its parent */
382 bus_node = of_match_bus((struct device_node*)dev);
383 bus_node->count_cells(dev, &na, &ns);
384 if (!OF_CHECK_COUNTS(na, ns)) {
385 printf("Bad cell count for %s\n", of_node_full_name(dev));
386 ret = -EINVAL;
387 goto out_parent;
388 }
389
390 bus_node = of_match_bus(parent);
391 bus_node->count_cells(parent, &pna, &pns);
392 if (!OF_CHECK_COUNTS(pna, pns)) {
393 printf("Bad cell count for %s\n", of_node_full_name(parent));
394 ret = -EINVAL;
395 goto out_parent;
396 }
397
398 *bus = of_read_number(ranges, na);
399 *cpu = of_translate_dma_address(dev, ranges + na);
400 *size = of_read_number(ranges + na + pna, ns);
401
402 out_parent:
403 of_node_put(parent);
404 out:
405 of_node_put(dev);
406 return ret;
407 }
408
409
410 static int __of_address_to_resource(const struct device_node *dev,
411 const __be32 *addrp, u64 size, unsigned int flags,
412 const char *name, struct resource *r)
413 {
414 u64 taddr;
415
416 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
417 return -EINVAL;
418 taddr = of_translate_address(dev, addrp);
419 if (taddr == OF_BAD_ADDR)
420 return -EINVAL;
421 memset(r, 0, sizeof(struct resource));
422 r->start = taddr;
423 r->end = taddr + size - 1;
424 r->flags = flags;
425 r->name = name ? name : dev->full_name;
426
427 return 0;
428 }
429
430 int of_address_to_resource(const struct device_node *dev, int index,
431 struct resource *r)
432 {
433 const __be32 *addrp;
434 u64 size;
435 unsigned int flags;
436 const char *name = NULL;
437
438 addrp = of_get_address(dev, index, &size, &flags);
439 if (addrp == NULL)
440 return -EINVAL;
441
442 /* Get optional "reg-names" property to add a name to a resource */
443 of_property_read_string_index(dev, "reg-names", index, &name);
444
445 return __of_address_to_resource(dev, addrp, size, flags, name, r);
446 }