1 // SPDX-License-Identifier: GPL-2.0+
3 * Taken from Linux v4.9 drivers/of/address.c
6 * Copyright (c) 2017 Google, Inc
10 #include <linux/libfdt.h>
11 #include <dm/of_access.h>
12 #include <dm/of_addr.h>
13 #include <linux/err.h>
14 #include <linux/ioport.h>
16 /* Max address size we deal with */
17 #define OF_MAX_ADDR_CELLS 4
18 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
19 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
21 static struct of_bus *of_match_bus(struct device_node *np);
25 static void of_dump_addr(const char *s, const __be32 *addr, int na)
29 pr_cont(" %08x", be32_to_cpu(*(addr++)));
33 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
36 /* Callbacks for bus specific translators */
39 const char *addresses;
40 int (*match)(struct device_node *parent);
41 void (*count_cells)(const struct device_node *child, int *addrc,
43 u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna);
44 int (*translate)(__be32 *addr, u64 offset, int na);
45 unsigned int (*get_flags)(const __be32 *addr);
48 static void of_bus_default_count_cells(const struct device_node *np,
49 int *addrc, int *sizec)
52 *addrc = of_n_addr_cells(np);
54 *sizec = of_n_size_cells(np);
57 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
58 int na, int ns, int pna)
62 cp = of_read_number(range, na);
63 s = of_read_number(range + na + pna, ns);
64 da = of_read_number(addr, na);
66 debug("default map, cp=%llx, s=%llx, da=%llx\n",
67 (unsigned long long)cp, (unsigned long long)s,
68 (unsigned long long)da);
70 if (da < cp || da >= (cp + s))
75 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
77 u64 a = of_read_number(addr, na);
78 memset(addr, 0, na * 4);
81 addr[na - 2] = cpu_to_be32(a >> 32);
82 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
87 static unsigned int of_bus_default_get_flags(const __be32 *addr)
89 return IORESOURCE_MEM;
93 * Array of bus-specific translators
95 static struct of_bus of_busses[] = {
101 .count_cells = of_bus_default_count_cells,
102 .map = of_bus_default_map,
103 .translate = of_bus_default_translate,
104 .get_flags = of_bus_default_get_flags,
108 static struct of_bus *of_match_bus(struct device_node *np)
112 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
113 if (!of_busses[i].match || of_busses[i].match(np))
114 return &of_busses[i];
119 static void dev_count_cells(const struct device_node *np, int *nap, int *nsp)
121 of_bus_default_count_cells(np, nap, nsp);
124 const __be32 *of_get_address(const struct device_node *dev, int index,
125 u64 *size, unsigned int *flags)
129 struct device_node *parent;
131 int onesize, i, na, ns;
133 /* Get parent & match bus type */
134 parent = of_get_parent(dev);
137 dev_count_cells(dev, &na, &ns);
138 bus = of_match_bus(parent);
139 bus->count_cells(dev, &na, &ns);
141 if (!OF_CHECK_ADDR_COUNT(na))
144 /* Get "reg" or "assigned-addresses" property */
145 prop = of_get_property(dev, "reg", &psize);
151 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
154 *size = of_read_number(prop + na, ns);
156 *flags = bus->get_flags(prop);
161 EXPORT_SYMBOL(of_get_address);
163 static int of_empty_ranges_quirk(const struct device_node *np)
168 static int of_translate_one(const struct device_node *parent,
169 struct of_bus *bus, struct of_bus *pbus,
170 __be32 *addr, int na, int ns, int pna,
173 const __be32 *ranges;
176 u64 offset = OF_BAD_ADDR;
179 * Normally, an absence of a "ranges" property means we are
180 * crossing a non-translatable boundary, and thus the addresses
181 * below the current cannot be converted to CPU physical ones.
182 * Unfortunately, while this is very clear in the spec, it's not
183 * what Apple understood, and they do have things like /uni-n or
184 * /ht nodes with no "ranges" property and a lot of perfectly
185 * useable mapped devices below them. Thus we treat the absence of
186 * "ranges" as equivalent to an empty "ranges" property which means
187 * a 1:1 translation at that level. It's up to the caller not to try
188 * to translate addresses that aren't supposed to be translated in
189 * the first place. --BenH.
191 * As far as we know, this damage only exists on Apple machines, so
192 * This code is only enabled on powerpc. --gcl
194 ranges = of_get_property(parent, rprop, &rlen);
195 if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
196 debug("no ranges; cannot translate\n");
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");
206 debug("walking ranges...\n");
208 /* Now walk through the ranges */
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)
216 if (offset == OF_BAD_ADDR) {
217 debug("not found !\n");
220 memcpy(addr, ranges + na, 4 * pna);
223 of_dump_addr("parent translation for:", addr, pna);
224 debug("with offset: %llx\n", (unsigned long long)offset);
226 /* Translate it into parent bus space */
227 return pbus->translate(addr, offset, pna);
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
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
240 static u64 __of_translate_address(const struct device_node *dev,
241 const __be32 *in_addr, const char *rprop)
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;
249 debug("** translation for device %s **\n", of_node_full_name(dev));
251 /* Increase refcount at current level */
252 (void)of_node_get(dev);
254 /* Get parent & match bus type */
255 parent = of_get_parent(dev);
258 bus = of_match_bus(parent);
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));
266 memcpy(addr, in_addr, na * 4);
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);
274 /* Switch to parent bus */
277 parent = of_get_parent(dev);
279 /* If root, we have finished */
280 if (parent == NULL) {
281 debug("reached root node\n");
282 result = of_read_number(addr, na);
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));
295 debug("parent bus is %s (na=%d, ns=%d) on %s\n", pbus->name,
296 pna, pns, of_node_full_name(parent));
298 /* Apply bus translation */
299 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
302 /* Complete the move up one level */
307 of_dump_addr("one level translation:", addr, na);
316 u64 of_translate_address(const struct device_node *dev, const __be32 *in_addr)
318 return __of_translate_address(dev, in_addr, "ranges");
322 static int __of_address_to_resource(const struct device_node *dev,
323 const __be32 *addrp, u64 size, unsigned int flags,
324 const char *name, struct resource *r)
328 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
330 taddr = of_translate_address(dev, addrp);
331 if (taddr == OF_BAD_ADDR)
333 memset(r, 0, sizeof(struct resource));
335 r->end = taddr + size - 1;
337 r->name = name ? name : dev->full_name;
342 int of_address_to_resource(const struct device_node *dev, int index,
348 const char *name = NULL;
350 addrp = of_get_address(dev, index, &size, &flags);
354 /* Get optional "reg-names" property to add a name to a resource */
355 of_property_read_string_index(dev, "reg-names", index, &name);
357 return __of_address_to_resource(dev, addrp, size, flags, name, r);