1 // SPDX-License-Identifier: GPL-2.0+
3 * Taken from Linux v4.9 drivers/of/address.c
6 * Copyright (c) 2017 Google, Inc
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>
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)
23 static struct of_bus
*of_match_bus(struct device_node
*np
);
27 static void of_dump_addr(const char *s
, const __be32
*addr
, int na
)
31 pr_cont(" %08x", be32_to_cpu(*(addr
++)));
35 static void of_dump_addr(const char *s
, const __be32
*addr
, int na
) { }
38 /* Callbacks for bus specific translators */
41 const char *addresses
;
42 int (*match
)(struct device_node
*parent
);
43 void (*count_cells
)(const struct device_node
*child
, int *addrc
,
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
);
50 static void of_bus_default_count_cells(const struct device_node
*np
,
51 int *addrc
, int *sizec
)
54 *addrc
= of_n_addr_cells(np
);
56 *sizec
= of_n_size_cells(np
);
59 static u64
of_bus_default_map(__be32
*addr
, const __be32
*range
,
60 int na
, int ns
, int pna
)
64 cp
= of_read_number(range
, na
);
65 s
= of_read_number(range
+ na
+ pna
, ns
);
66 da
= of_read_number(addr
, na
);
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
);
72 if (da
< cp
|| da
>= (cp
+ s
))
77 static int of_bus_default_translate(__be32
*addr
, u64 offset
, int na
)
79 u64 a
= of_read_number(addr
, na
);
80 memset(addr
, 0, na
* 4);
83 addr
[na
- 2] = cpu_to_be32(a
>> 32);
84 addr
[na
- 1] = cpu_to_be32(a
& 0xffffffffu
);
89 static unsigned int of_bus_default_get_flags(const __be32
*addr
)
91 return IORESOURCE_MEM
;
95 * Array of bus-specific translators
97 static struct of_bus of_busses
[] = {
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
,
110 static struct of_bus
*of_match_bus(struct device_node
*np
)
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
];
121 static void dev_count_cells(const struct device_node
*np
, int *nap
, int *nsp
)
123 of_bus_default_count_cells(np
, nap
, nsp
);
126 const __be32
*of_get_address(const struct device_node
*dev
, int index
,
127 u64
*size
, unsigned int *flags
)
131 struct device_node
*parent
;
133 int onesize
, i
, na
, ns
;
135 /* Get parent & match bus type */
136 parent
= of_get_parent(dev
);
139 dev_count_cells(dev
, &na
, &ns
);
140 bus
= of_match_bus(parent
);
141 bus
->count_cells(dev
, &na
, &ns
);
143 if (!OF_CHECK_ADDR_COUNT(na
))
146 /* Get "reg" or "assigned-addresses" property */
147 prop
= of_get_property(dev
, "reg", &psize
);
153 for (i
= 0; psize
>= onesize
; psize
-= onesize
, prop
+= onesize
, i
++)
156 *size
= of_read_number(prop
+ na
, ns
);
158 *flags
= bus
->get_flags(prop
);
163 EXPORT_SYMBOL(of_get_address
);
165 static int of_empty_ranges_quirk(const struct device_node
*np
)
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
,
175 const __be32
*ranges
;
178 u64 offset
= OF_BAD_ADDR
;
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.
193 * As far as we know, this damage only exists on Apple machines, so
194 * This code is only enabled on powerpc. --gcl
196 * This quirk also applies for 'dma-ranges' which frequently exist in
197 * child nodes without 'dma-ranges' in the parent nodes. --RobH
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");
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");
212 debug("walking ranges...\n");
214 /* Now walk through the ranges */
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
)
222 if (offset
== OF_BAD_ADDR
) {
223 debug("not found !\n");
226 memcpy(addr
, ranges
+ na
, 4 * pna
);
229 of_dump_addr("parent translation for:", addr
, pna
);
230 debug("with offset: %llx\n", (unsigned long long)offset
);
232 /* Translate it into parent bus space */
233 return pbus
->translate(addr
, offset
, pna
);
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
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
246 static u64
__of_translate_address(const struct device_node
*dev
,
247 const __be32
*in_addr
, const char *rprop
)
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
;
255 debug("** translation for device %s **\n", of_node_full_name(dev
));
257 /* Increase refcount at current level */
258 (void)of_node_get(dev
);
260 /* Get parent & match bus type */
261 parent
= of_get_parent(dev
);
264 bus
= of_match_bus(parent
);
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
));
272 memcpy(addr
, in_addr
, na
* 4);
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
);
280 /* Switch to parent bus */
283 parent
= of_get_parent(dev
);
285 /* If root, we have finished */
286 if (parent
== NULL
) {
287 debug("reached root node\n");
288 result
= of_read_number(addr
, na
);
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
));
301 debug("parent bus is %s (na=%d, ns=%d) on %s\n", pbus
->name
,
302 pna
, pns
, of_node_full_name(parent
));
304 /* Apply bus translation */
305 if (of_translate_one(dev
, bus
, pbus
, addr
, na
, ns
, pna
, rprop
))
308 /* Complete the move up one level */
313 of_dump_addr("one level translation:", addr
, na
);
322 u64
of_translate_address(const struct device_node
*dev
, const __be32
*in_addr
)
324 return __of_translate_address(dev
, in_addr
, "ranges");
327 u64
of_translate_dma_address(const struct device_node
*dev
, const __be32
*in_addr
)
329 return __of_translate_address(dev
, in_addr
, "dma-ranges");
332 int of_get_dma_range(const struct device_node
*dev
, phys_addr_t
*cpu
,
333 dma_addr_t
*bus
, u64
*size
)
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
;
343 /* Find the closest dma-ranges property */
344 dev
= of_node_get(dev
);
346 ranges
= of_get_property(dev
, "dma-ranges", &len
);
348 /* Ignore empty ranges, they imply no translation required */
349 if (ranges
&& len
> 0)
352 /* Once we find 'dma-ranges', then a missing one is an error */
353 if (found_dma_ranges
&& !ranges
) {
359 found_dma_ranges
= true;
361 parent
= of_get_parent(dev
);
366 if (!dev
|| !ranges
) {
367 debug("no dma-ranges found for node %s\n",
368 of_node_full_name(dev
));
373 /* switch to that node */
374 parent
= of_get_parent(dev
);
376 printf("Found dma-ranges in root node, shoudln't happen\n");
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
));
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
));
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
);
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
)
416 if ((flags
& (IORESOURCE_IO
| IORESOURCE_MEM
)) == 0)
418 taddr
= of_translate_address(dev
, addrp
);
419 if (taddr
== OF_BAD_ADDR
)
421 memset(r
, 0, sizeof(struct resource
));
423 r
->end
= taddr
+ size
- 1;
425 r
->name
= name
? name
: dev
->full_name
;
430 int of_address_to_resource(const struct device_node
*dev
, int index
,
436 const char *name
= NULL
;
438 addrp
= of_get_address(dev
, index
, &size
, &flags
);
442 /* Get optional "reg-names" property to add a name to a resource */
443 of_property_read_string_index(dev
, "reg-names", index
, &name
);
445 return __of_address_to_resource(dev
, addrp
, size
, flags
, name
, r
);