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