1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Google, Inc
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
13 #include <fdt_support.h>
15 #include <dm/device-internal.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 fdt_addr_t devfdt_get_addr_index(struct udevice *dev, int index)
21 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
24 if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
29 na = fdt_address_cells(gd->fdt_blob,
30 dev_of_offset(dev->parent));
32 debug("bad #address-cells\n");
33 return FDT_ADDR_T_NONE;
36 ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
38 debug("bad #size-cells\n");
39 return FDT_ADDR_T_NONE;
42 reg = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "reg",
44 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
45 debug("Req index out of range\n");
46 return FDT_ADDR_T_NONE;
49 reg += index * (na + ns);
53 * Use the full-fledged translate function for complex
56 addr = fdt_translate_address((void *)gd->fdt_blob,
57 dev_of_offset(dev), reg);
59 /* Non translatable if #size-cells == 0 */
60 addr = fdt_read_number(reg, na);
64 * Use the "simple" translate function for less complex
67 addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
68 dev_of_offset(dev->parent), dev_of_offset(dev),
69 "reg", index, NULL, false);
70 if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
71 if (device_get_uclass_id(dev->parent) ==
73 addr = simple_bus_translate(dev->parent, addr);
77 #if defined(CONFIG_TRANSLATION_OFFSET)
79 * Some platforms need a special address translation. Those
80 * platforms (e.g. mvebu in SPL) can configure a translation
81 * offset by setting this value in the GD and enaling this
82 * feature via CONFIG_TRANSLATION_OFFSET. This value will
83 * get added to all addresses returned by devfdt_get_addr().
85 addr += gd->translation_offset;
90 return FDT_ADDR_T_NONE;
94 fdt_addr_t devfdt_get_addr_size_index(struct udevice *dev, int index,
97 #if CONFIG_IS_ENABLED(OF_CONTROL)
99 * Only get the size in this first call. We'll get the addr in the
100 * next call to the exisiting dev_get_xxx function which handles
101 * all config options.
103 fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev),
104 "reg", index, size, false);
107 * Get the base address via the existing function which handles
110 return devfdt_get_addr_index(dev, index);
112 return FDT_ADDR_T_NONE;
116 fdt_addr_t devfdt_get_addr_name(struct udevice *dev, const char *name)
118 #if CONFIG_IS_ENABLED(OF_CONTROL)
121 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
126 return devfdt_get_addr_index(dev, index);
128 return FDT_ADDR_T_NONE;
132 fdt_addr_t devfdt_get_addr_size_name(struct udevice *dev, const char *name,
135 #if CONFIG_IS_ENABLED(OF_CONTROL)
138 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
143 return devfdt_get_addr_size_index(dev, index, size);
145 return FDT_ADDR_T_NONE;
149 fdt_addr_t devfdt_get_addr(struct udevice *dev)
151 return devfdt_get_addr_index(dev, 0);
154 void *devfdt_get_addr_ptr(struct udevice *dev)
156 return (void *)(uintptr_t)devfdt_get_addr_index(dev, 0);
159 void *devfdt_remap_addr_index(struct udevice *dev, int index)
161 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
163 if (addr == FDT_ADDR_T_NONE)
166 return map_physmem(addr, 0, MAP_NOCACHE);
169 void *devfdt_remap_addr_name(struct udevice *dev, const char *name)
171 fdt_addr_t addr = devfdt_get_addr_name(dev, name);
173 if (addr == FDT_ADDR_T_NONE)
176 return map_physmem(addr, 0, MAP_NOCACHE);
179 void *devfdt_remap_addr(struct udevice *dev)
181 return devfdt_remap_addr_index(dev, 0);
184 void *devfdt_map_physmem(struct udevice *dev, unsigned long size)
186 fdt_addr_t addr = devfdt_get_addr(dev);
188 if (addr == FDT_ADDR_T_NONE)
191 return map_physmem(addr, size, MAP_NOCACHE);
194 fdt_addr_t devfdt_get_addr_pci(struct udevice *dev)
198 addr = devfdt_get_addr(dev);
199 if (CONFIG_IS_ENABLED(PCI) && IS_ENABLED(CONFIG_DM_PCI) &&
200 addr == FDT_ADDR_T_NONE) {
201 struct fdt_pci_addr pci_addr;
205 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32,
208 /* try if there is any i/o-mapped register */
209 ret = ofnode_read_pci_addr(dev_ofnode(dev),
210 FDT_PCI_SPACE_IO, "reg",
213 return FDT_ADDR_T_NONE;
215 ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar);
217 return FDT_ADDR_T_NONE;