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
of_dump_addr(const char * s,const __be32 * addr,int na)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
of_dump_addr(const char * s,const __be32 * addr,int na)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 
of_bus_default_count_cells(const struct device_node * np,int * addrc,int * sizec)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 
of_bus_default_map(__be32 * addr,const __be32 * range,int na,int ns,int pna)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 
of_bus_default_translate(__be32 * addr,u64 offset,int na)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 
of_bus_default_get_flags(const __be32 * addr)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 
of_match_bus(struct device_node * np)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 
of_get_address(const struct device_node * dev,int index,u64 * size,unsigned int * flags)121 const __be32 *of_get_address(const struct device_node *dev, int index,
122 			     u64 *size, unsigned int *flags)
123 {
124 	const __be32 *prop;
125 	int psize;
126 	struct device_node *parent;
127 	struct of_bus *bus;
128 	int onesize, i, na, ns;
129 
130 	/* Get parent & match bus type */
131 	parent = of_get_parent(dev);
132 	if (parent == NULL)
133 		return NULL;
134 	bus = of_match_bus(parent);
135 	bus->count_cells(dev, &na, &ns);
136 	of_node_put(parent);
137 	if (!OF_CHECK_ADDR_COUNT(na))
138 		return NULL;
139 
140 	/* Get "reg" or "assigned-addresses" property */
141 	prop = of_get_property(dev, "reg", &psize);
142 	if (prop == NULL)
143 		return NULL;
144 	psize /= 4;
145 
146 	onesize = na + ns;
147 	for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
148 		if (i == index) {
149 			if (size)
150 				*size = of_read_number(prop + na, ns);
151 			if (flags)
152 				*flags = bus->get_flags(prop);
153 			return prop;
154 		}
155 	return NULL;
156 }
157 EXPORT_SYMBOL(of_get_address);
158 
of_empty_ranges_quirk(const struct device_node * np)159 static int of_empty_ranges_quirk(const struct device_node *np)
160 {
161 	return false;
162 }
163 
of_translate_one(const struct device_node * parent,struct of_bus * bus,struct of_bus * pbus,__be32 * addr,int na,int ns,int pna,const char * rprop)164 static int of_translate_one(const struct device_node *parent,
165 			    struct of_bus *bus, struct of_bus *pbus,
166 			    __be32 *addr, int na, int ns, int pna,
167 			    const char *rprop)
168 {
169 	const __be32 *ranges;
170 	int rlen;
171 	int rone;
172 	u64 offset = OF_BAD_ADDR;
173 
174 	/*
175 	 * Normally, an absence of a "ranges" property means we are
176 	 * crossing a non-translatable boundary, and thus the addresses
177 	 * below the current cannot be converted to CPU physical ones.
178 	 * Unfortunately, while this is very clear in the spec, it's not
179 	 * what Apple understood, and they do have things like /uni-n or
180 	 * /ht nodes with no "ranges" property and a lot of perfectly
181 	 * useable mapped devices below them. Thus we treat the absence of
182 	 * "ranges" as equivalent to an empty "ranges" property which means
183 	 * a 1:1 translation at that level. It's up to the caller not to try
184 	 * to translate addresses that aren't supposed to be translated in
185 	 * the first place. --BenH.
186 	 *
187 	 * As far as we know, this damage only exists on Apple machines, so
188 	 * This code is only enabled on powerpc. --gcl
189 	 *
190 	 * This quirk also applies for 'dma-ranges' which frequently exist in
191 	 * child nodes without 'dma-ranges' in the parent nodes. --RobH
192 	 */
193 	ranges = of_get_property(parent, rprop, &rlen);
194 	if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
195 	    strcmp(rprop, "dma-ranges")) {
196 		debug("no ranges; cannot translate\n");
197 		return 1;
198 	}
199 	if (ranges == NULL || rlen == 0) {
200 		offset = of_read_number(addr, na);
201 		memset(addr, 0, pna * 4);
202 		debug("empty ranges; 1:1 translation\n");
203 		goto finish;
204 	}
205 
206 	debug("walking ranges...\n");
207 
208 	/* Now walk through the ranges */
209 	rlen /= 4;
210 	rone = na + pna + ns;
211 	for (; rlen >= rone; rlen -= rone, ranges += rone) {
212 		offset = bus->map(addr, ranges, na, ns, pna);
213 		if (offset != OF_BAD_ADDR)
214 			break;
215 	}
216 	if (offset == OF_BAD_ADDR) {
217 		debug("not found !\n");
218 		return 1;
219 	}
220 	memcpy(addr, ranges + na, 4 * pna);
221 
222  finish:
223 	of_dump_addr("parent translation for:", addr, pna);
224 	debug("with offset: %llx\n", (unsigned long long)offset);
225 
226 	/* Translate it into parent bus space */
227 	return pbus->translate(addr, offset, pna);
228 }
229 
230 /*
231  * Translate an address from the device-tree into a CPU physical address,
232  * this walks up the tree and applies the various bus mappings on the
233  * way.
234  *
235  * Note: We consider that crossing any level with #size-cells == 0 to mean
236  * that translation is impossible (that is we are not dealing with a value
237  * that can be mapped to a cpu physical address). This is not really specified
238  * that way, but this is traditionally the way IBM at least do things
239  */
__of_translate_address(const struct device_node * dev,const __be32 * in_addr,const char * rprop)240 static u64 __of_translate_address(const struct device_node *dev,
241 				  const __be32 *in_addr, const char *rprop)
242 {
243 	struct device_node *parent = NULL;
244 	struct of_bus *bus, *pbus;
245 	__be32 addr[OF_MAX_ADDR_CELLS];
246 	int na, ns, pna, pns;
247 	u64 result = OF_BAD_ADDR;
248 
249 	debug("** translation for device %s **\n", of_node_full_name(dev));
250 
251 	/* Increase refcount at current level */
252 	(void)of_node_get(dev);
253 
254 	/* Get parent & match bus type */
255 	parent = of_get_parent(dev);
256 	if (parent == NULL)
257 		goto bail;
258 	bus = of_match_bus(parent);
259 
260 	/* Count address cells & copy address locally */
261 	bus->count_cells(dev, &na, &ns);
262 	if (!OF_CHECK_COUNTS(na, ns)) {
263 		debug("Bad cell count for %s\n", of_node_full_name(dev));
264 		goto bail;
265 	}
266 	memcpy(addr, in_addr, na * 4);
267 
268 	debug("bus is %s (na=%d, ns=%d) on %s\n", bus->name, na, ns,
269 	      of_node_full_name(parent));
270 	of_dump_addr("translating address:", addr, na);
271 
272 	/* Translate */
273 	for (;;) {
274 		/* Switch to parent bus */
275 		of_node_put(dev);
276 		dev = parent;
277 		parent = of_get_parent(dev);
278 
279 		/* If root, we have finished */
280 		if (parent == NULL) {
281 			debug("reached root node\n");
282 			result = of_read_number(addr, na);
283 			break;
284 		}
285 
286 		/* Get new parent bus and counts */
287 		pbus = of_match_bus(parent);
288 		pbus->count_cells(dev, &pna, &pns);
289 		if (!OF_CHECK_COUNTS(pna, pns)) {
290 			debug("Bad cell count for %s\n",
291 			      of_node_full_name(dev));
292 			break;
293 		}
294 
295 		debug("parent bus is %s (na=%d, ns=%d) on %s\n", pbus->name,
296 		      pna, pns, of_node_full_name(parent));
297 
298 		/* Apply bus translation */
299 		if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
300 			break;
301 
302 		/* Complete the move up one level */
303 		na = pna;
304 		ns = pns;
305 		bus = pbus;
306 
307 		of_dump_addr("one level translation:", addr, na);
308 	}
309  bail:
310 	of_node_put(parent);
311 	of_node_put(dev);
312 
313 	return result;
314 }
315 
of_translate_address(const struct device_node * dev,const __be32 * in_addr)316 u64 of_translate_address(const struct device_node *dev, const __be32 *in_addr)
317 {
318 	return __of_translate_address(dev, in_addr, "ranges");
319 }
320 
of_translate_dma_address(const struct device_node * dev,const __be32 * in_addr)321 u64 of_translate_dma_address(const struct device_node *dev, const __be32 *in_addr)
322 {
323 	return __of_translate_address(dev, in_addr, "dma-ranges");
324 }
325 
of_get_dma_range(const struct device_node * dev,phys_addr_t * cpu,dma_addr_t * bus,u64 * size)326 int of_get_dma_range(const struct device_node *dev, phys_addr_t *cpu,
327 		     dma_addr_t *bus, u64 *size)
328 {
329 	bool found_dma_ranges = false;
330 	struct device_node *parent;
331 	struct of_bus *bus_node;
332 	int na, ns, pna, pns;
333 	const __be32 *ranges;
334 	int ret = 0;
335 	int len;
336 
337 	/* Find the closest dma-ranges property */
338 	dev = of_node_get(dev);
339 	while (dev) {
340 		ranges = of_get_property(dev, "dma-ranges", &len);
341 
342 		/* Ignore empty ranges, they imply no translation required */
343 		if (ranges && len > 0)
344 			break;
345 
346 		/* Once we find 'dma-ranges', then a missing one is an error */
347 		if (found_dma_ranges && !ranges) {
348 			ret = -EINVAL;
349 			goto out;
350 		}
351 
352 		if (ranges)
353 			found_dma_ranges = true;
354 
355 		parent = of_get_parent(dev);
356 		of_node_put(dev);
357 		dev = parent;
358 	}
359 
360 	if (!dev || !ranges) {
361 		debug("no dma-ranges found for node %s\n",
362 		      of_node_full_name(dev));
363 		ret = -ENOENT;
364 		goto out;
365 	}
366 
367 	/* switch to that node */
368 	parent = of_get_parent(dev);
369 	if (!parent) {
370 		printf("Found dma-ranges in root node, shoudln't happen\n");
371 		ret = -EINVAL;
372 		goto out;
373 	}
374 
375 	/* Get the address sizes both for the bus and its parent */
376 	bus_node = of_match_bus((struct device_node*)dev);
377 	bus_node->count_cells(dev, &na, &ns);
378 	if (!OF_CHECK_COUNTS(na, ns)) {
379 		printf("Bad cell count for %s\n", of_node_full_name(dev));
380 		ret = -EINVAL;
381 		goto out_parent;
382 	}
383 
384 	bus_node = of_match_bus(parent);
385 	bus_node->count_cells(parent, &pna, &pns);
386 	if (!OF_CHECK_COUNTS(pna, pns)) {
387 		printf("Bad cell count for %s\n", of_node_full_name(parent));
388 		ret = -EINVAL;
389 		goto out_parent;
390 	}
391 
392 	*bus = of_read_number(ranges, na);
393 	*cpu = of_translate_dma_address(dev, ranges + na);
394 	*size = of_read_number(ranges + na + pna, ns);
395 
396 out_parent:
397 	of_node_put(parent);
398 out:
399 	of_node_put(dev);
400 	return ret;
401 }
402 
403 
__of_address_to_resource(const struct device_node * dev,const __be32 * addrp,u64 size,unsigned int flags,const char * name,struct resource * r)404 static int __of_address_to_resource(const struct device_node *dev,
405 		const __be32 *addrp, u64 size, unsigned int flags,
406 		const char *name, struct resource *r)
407 {
408 	u64 taddr;
409 
410 	if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
411 		return -EINVAL;
412 	taddr = of_translate_address(dev, addrp);
413 	if (taddr == OF_BAD_ADDR)
414 		return -EINVAL;
415 	memset(r, 0, sizeof(struct resource));
416 	r->start = taddr;
417 	r->end = taddr + size - 1;
418 	r->flags = flags;
419 	r->name = name ? name : dev->full_name;
420 
421 	return 0;
422 }
423 
of_address_to_resource(const struct device_node * dev,int index,struct resource * r)424 int of_address_to_resource(const struct device_node *dev, int index,
425 			   struct resource *r)
426 {
427 	const __be32	*addrp;
428 	u64		size;
429 	unsigned int	flags;
430 	const char	*name = NULL;
431 
432 	addrp = of_get_address(dev, index, &size, &flags);
433 	if (addrp == NULL)
434 		return -EINVAL;
435 
436 	/* Get optional "reg-names" property to add a name to a resource */
437 	of_property_read_string_index(dev, "reg-names", index, &name);
438 
439 	return __of_address_to_resource(dev, addrp, size, flags, name, r);
440 }
441