xref: /openbmc/u-boot/drivers/core/of_addr.c (revision 679590eb)
1 /*
2  * Taken from Linux v4.9 drivers/of/address.c
3  *
4  * Modified for U-Boot
5  * Copyright (c) 2017 Google, Inc
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <libfdt.h>
12 #include <dm/of_access.h>
13 #include <dm/of_addr.h>
14 #include <linux/err.h>
15 #include <linux/ioport.h>
16 
17 /* Max address size we deal with */
18 #define OF_MAX_ADDR_CELLS	4
19 #define OF_CHECK_ADDR_COUNT(na)	((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
20 #define OF_CHECK_COUNTS(na, ns)	(OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
21 
22 static struct of_bus *of_match_bus(struct device_node *np);
23 
24 /* Debug utility */
25 #ifdef DEBUG
26 static void of_dump_addr(const char *s, const __be32 *addr, int na)
27 {
28 	debug("%s", s);
29 	while (na--)
30 		pr_cont(" %08x", be32_to_cpu(*(addr++)));
31 	pr_cont("\n");
32 }
33 #else
34 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
35 #endif
36 
37 /* Callbacks for bus specific translators */
38 struct of_bus {
39 	const char *name;
40 	const char *addresses;
41 	int (*match)(struct device_node *parent);
42 	void (*count_cells)(const struct device_node *child, int *addrc,
43 			    int *sizec);
44 	u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna);
45 	int (*translate)(__be32 *addr, u64 offset, int na);
46 	unsigned int (*get_flags)(const __be32 *addr);
47 };
48 
49 static void of_bus_default_count_cells(const struct device_node *np,
50 				       int *addrc, int *sizec)
51 {
52 	if (addrc)
53 		*addrc = of_n_addr_cells(np);
54 	if (sizec)
55 		*sizec = of_n_size_cells(np);
56 }
57 
58 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
59 		int na, int ns, int pna)
60 {
61 	u64 cp, s, da;
62 
63 	cp = of_read_number(range, na);
64 	s  = of_read_number(range + na + pna, ns);
65 	da = of_read_number(addr, na);
66 
67 	debug("default map, cp=%llx, s=%llx, da=%llx\n",
68 	      (unsigned long long)cp, (unsigned long long)s,
69 	      (unsigned long long)da);
70 
71 	if (da < cp || da >= (cp + s))
72 		return OF_BAD_ADDR;
73 	return da - cp;
74 }
75 
76 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
77 {
78 	u64 a = of_read_number(addr, na);
79 	memset(addr, 0, na * 4);
80 	a += offset;
81 	if (na > 1)
82 		addr[na - 2] = cpu_to_be32(a >> 32);
83 	addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
84 
85 	return 0;
86 }
87 
88 static unsigned int of_bus_default_get_flags(const __be32 *addr)
89 {
90 	return IORESOURCE_MEM;
91 }
92 
93 /*
94  * Array of bus-specific translators
95  */
96 static struct of_bus of_busses[] = {
97 	/* Default */
98 	{
99 		.name = "default",
100 		.addresses = "reg",
101 		.match = NULL,
102 		.count_cells = of_bus_default_count_cells,
103 		.map = of_bus_default_map,
104 		.translate = of_bus_default_translate,
105 		.get_flags = of_bus_default_get_flags,
106 	},
107 };
108 
109 static struct of_bus *of_match_bus(struct device_node *np)
110 {
111 	int i;
112 
113 	for (i = 0; i < ARRAY_SIZE(of_busses); i++)
114 		if (!of_busses[i].match || of_busses[i].match(np))
115 			return &of_busses[i];
116 	BUG();
117 	return NULL;
118 }
119 
120 static void dev_count_cells(const struct device_node *np, int *nap, int *nsp)
121 {
122 	of_bus_default_count_cells(np, nap, nsp);
123 }
124 
125 const __be32 *of_get_address(const struct device_node *dev, int index,
126 			     u64 *size, unsigned int *flags)
127 {
128 	const __be32 *prop;
129 	int psize;
130 	struct device_node *parent;
131 	struct of_bus *bus;
132 	int onesize, i, na, ns;
133 
134 	/* Get parent & match bus type */
135 	parent = of_get_parent(dev);
136 	if (parent == NULL)
137 		return NULL;
138 	dev_count_cells(dev, &na, &ns);
139 	bus = of_match_bus(parent);
140 	bus->count_cells(dev, &na, &ns);
141 	of_node_put(parent);
142 	if (!OF_CHECK_ADDR_COUNT(na))
143 		return NULL;
144 
145 	/* Get "reg" or "assigned-addresses" property */
146 	prop = of_get_property(dev, "reg", &psize);
147 	if (prop == NULL)
148 		return NULL;
149 	psize /= 4;
150 
151 	onesize = na + ns;
152 	for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
153 		if (i == index) {
154 			if (size)
155 				*size = of_read_number(prop + na, ns);
156 			if (flags)
157 				*flags = bus->get_flags(prop);
158 			return prop;
159 		}
160 	return NULL;
161 }
162 EXPORT_SYMBOL(of_get_address);
163 
164 static int of_empty_ranges_quirk(const struct device_node *np)
165 {
166 	return false;
167 }
168 
169 static int of_translate_one(const struct device_node *parent,
170 			    struct of_bus *bus, struct of_bus *pbus,
171 			    __be32 *addr, int na, int ns, int pna,
172 			    const char *rprop)
173 {
174 	const __be32 *ranges;
175 	int rlen;
176 	int rone;
177 	u64 offset = OF_BAD_ADDR;
178 
179 	/*
180 	 * Normally, an absence of a "ranges" property means we are
181 	 * crossing a non-translatable boundary, and thus the addresses
182 	 * below the current cannot be converted to CPU physical ones.
183 	 * Unfortunately, while this is very clear in the spec, it's not
184 	 * what Apple understood, and they do have things like /uni-n or
185 	 * /ht nodes with no "ranges" property and a lot of perfectly
186 	 * useable mapped devices below them. Thus we treat the absence of
187 	 * "ranges" as equivalent to an empty "ranges" property which means
188 	 * a 1:1 translation at that level. It's up to the caller not to try
189 	 * to translate addresses that aren't supposed to be translated in
190 	 * the first place. --BenH.
191 	 *
192 	 * As far as we know, this damage only exists on Apple machines, so
193 	 * This code is only enabled on powerpc. --gcl
194 	 */
195 	ranges = of_get_property(parent, rprop, &rlen);
196 	if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
197 		debug("no ranges; cannot translate\n");
198 		return 1;
199 	}
200 	if (ranges == NULL || rlen == 0) {
201 		offset = of_read_number(addr, na);
202 		memset(addr, 0, pna * 4);
203 		debug("empty ranges; 1:1 translation\n");
204 		goto finish;
205 	}
206 
207 	debug("walking ranges...\n");
208 
209 	/* Now walk through the ranges */
210 	rlen /= 4;
211 	rone = na + pna + ns;
212 	for (; rlen >= rone; rlen -= rone, ranges += rone) {
213 		offset = bus->map(addr, ranges, na, ns, pna);
214 		if (offset != OF_BAD_ADDR)
215 			break;
216 	}
217 	if (offset == OF_BAD_ADDR) {
218 		debug("not found !\n");
219 		return 1;
220 	}
221 	memcpy(addr, ranges + na, 4 * pna);
222 
223  finish:
224 	of_dump_addr("parent translation for:", addr, pna);
225 	debug("with offset: %llx\n", (unsigned long long)offset);
226 
227 	/* Translate it into parent bus space */
228 	return pbus->translate(addr, offset, pna);
229 }
230 
231 /*
232  * Translate an address from the device-tree into a CPU physical address,
233  * this walks up the tree and applies the various bus mappings on the
234  * way.
235  *
236  * Note: We consider that crossing any level with #size-cells == 0 to mean
237  * that translation is impossible (that is we are not dealing with a value
238  * that can be mapped to a cpu physical address). This is not really specified
239  * that way, but this is traditionally the way IBM at least do things
240  */
241 static u64 __of_translate_address(const struct device_node *dev,
242 				  const __be32 *in_addr, const char *rprop)
243 {
244 	struct device_node *parent = NULL;
245 	struct of_bus *bus, *pbus;
246 	__be32 addr[OF_MAX_ADDR_CELLS];
247 	int na, ns, pna, pns;
248 	u64 result = OF_BAD_ADDR;
249 
250 	debug("** translation for device %s **\n", of_node_full_name(dev));
251 
252 	/* Increase refcount at current level */
253 	(void)of_node_get(dev);
254 
255 	/* Get parent & match bus type */
256 	parent = of_get_parent(dev);
257 	if (parent == NULL)
258 		goto bail;
259 	bus = of_match_bus(parent);
260 
261 	/* Count address cells & copy address locally */
262 	bus->count_cells(dev, &na, &ns);
263 	if (!OF_CHECK_COUNTS(na, ns)) {
264 		debug("Bad cell count for %s\n", of_node_full_name(dev));
265 		goto bail;
266 	}
267 	memcpy(addr, in_addr, na * 4);
268 
269 	debug("bus is %s (na=%d, ns=%d) on %s\n", bus->name, na, ns,
270 	      of_node_full_name(parent));
271 	of_dump_addr("translating address:", addr, na);
272 
273 	/* Translate */
274 	for (;;) {
275 		/* Switch to parent bus */
276 		of_node_put(dev);
277 		dev = parent;
278 		parent = of_get_parent(dev);
279 
280 		/* If root, we have finished */
281 		if (parent == NULL) {
282 			debug("reached root node\n");
283 			result = of_read_number(addr, na);
284 			break;
285 		}
286 
287 		/* Get new parent bus and counts */
288 		pbus = of_match_bus(parent);
289 		pbus->count_cells(dev, &pna, &pns);
290 		if (!OF_CHECK_COUNTS(pna, pns)) {
291 			debug("Bad cell count for %s\n",
292 			      of_node_full_name(dev));
293 			break;
294 		}
295 
296 		debug("parent bus is %s (na=%d, ns=%d) on %s\n", pbus->name,
297 		      pna, pns, of_node_full_name(parent));
298 
299 		/* Apply bus translation */
300 		if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
301 			break;
302 
303 		/* Complete the move up one level */
304 		na = pna;
305 		ns = pns;
306 		bus = pbus;
307 
308 		of_dump_addr("one level translation:", addr, na);
309 	}
310  bail:
311 	of_node_put(parent);
312 	of_node_put(dev);
313 
314 	return result;
315 }
316 
317 u64 of_translate_address(const struct device_node *dev, const __be32 *in_addr)
318 {
319 	return __of_translate_address(dev, in_addr, "ranges");
320 }
321 
322 
323 static int __of_address_to_resource(const struct device_node *dev,
324 		const __be32 *addrp, u64 size, unsigned int flags,
325 		const char *name, struct resource *r)
326 {
327 	u64 taddr;
328 
329 	if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
330 		return -EINVAL;
331 	taddr = of_translate_address(dev, addrp);
332 	if (taddr == OF_BAD_ADDR)
333 		return -EINVAL;
334 	memset(r, 0, sizeof(struct resource));
335 	r->start = taddr;
336 	r->end = taddr + size - 1;
337 	r->flags = flags;
338 	r->name = name ? name : dev->full_name;
339 
340 	return 0;
341 }
342 
343 int of_address_to_resource(const struct device_node *dev, int index,
344 			   struct resource *r)
345 {
346 	const __be32	*addrp;
347 	u64		size;
348 	unsigned int	flags;
349 	const char	*name = NULL;
350 
351 	addrp = of_get_address(dev, index, &size, &flags);
352 	if (addrp == NULL)
353 		return -EINVAL;
354 
355 	/* Get optional "reg-names" property to add a name to a resource */
356 	of_property_read_string_index(dev, "reg-names", index, &name);
357 
358 	return __of_address_to_resource(dev, addrp, size, flags, name, r);
359 }
360