blob: 3b59b70c24ae409f6aeba1d4b48f9065e985ccd6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassaadfaf02017-05-17 17:18:04 -06002/*
3 * Device addresses
4 *
5 * Copyright (c) 2017 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassaadfaf02017-05-17 17:18:04 -06009 */
10
11#include <common.h>
12#include <dm.h>
13#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Johan Jonker86648602023-04-23 11:19:41 +020015#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glassaadfaf02017-05-17 17:18:04 -060017#include <asm/io.h>
18#include <dm/device-internal.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glassc6548b82020-01-27 08:49:39 -070022fdt_addr_t devfdt_get_addr_index(const struct udevice *dev, int index)
Simon Glassaadfaf02017-05-17 17:18:04 -060023{
Simon Glass3580f6d2021-08-07 07:24:03 -060024#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glasscb12c9f2022-09-06 20:27:12 -060025 int offset = dev_of_offset(dev);
26 int parent = dev_of_offset(dev->parent);
Simon Glassaadfaf02017-05-17 17:18:04 -060027 fdt_addr_t addr;
28
29 if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
30 const fdt32_t *reg;
31 int len = 0;
32 int na, ns;
33
Simon Glasscb12c9f2022-09-06 20:27:12 -060034 na = fdt_address_cells(gd->fdt_blob, parent);
Simon Glassaadfaf02017-05-17 17:18:04 -060035 if (na < 1) {
36 debug("bad #address-cells\n");
37 return FDT_ADDR_T_NONE;
38 }
39
Simon Glasscb12c9f2022-09-06 20:27:12 -060040 ns = fdt_size_cells(gd->fdt_blob, parent);
Simon Glassaadfaf02017-05-17 17:18:04 -060041 if (ns < 0) {
42 debug("bad #size-cells\n");
43 return FDT_ADDR_T_NONE;
44 }
45
Simon Glasscb12c9f2022-09-06 20:27:12 -060046 reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
Simon Glassaadfaf02017-05-17 17:18:04 -060047 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
48 debug("Req index out of range\n");
49 return FDT_ADDR_T_NONE;
50 }
51
52 reg += index * (na + ns);
53
Dario Binacchif8fc7032021-05-01 17:05:26 +020054 if (ns) {
Mario Six35616ef2018-03-12 14:53:33 +010055 /*
56 * Use the full-fledged translate function for complex
57 * bus setups.
58 */
59 addr = fdt_translate_address((void *)gd->fdt_blob,
Simon Glasscb12c9f2022-09-06 20:27:12 -060060 offset, reg);
Mario Six35616ef2018-03-12 14:53:33 +010061 } else {
62 /* Non translatable if #size-cells == 0 */
63 addr = fdt_read_number(reg, na);
64 }
Simon Glassaadfaf02017-05-17 17:18:04 -060065 } else {
66 /*
67 * Use the "simple" translate function for less complex
68 * bus setups.
69 */
Simon Glasscb12c9f2022-09-06 20:27:12 -060070 addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob, parent,
71 offset, "reg", index,
72 NULL, false);
Simon Glassaadfaf02017-05-17 17:18:04 -060073 if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
74 if (device_get_uclass_id(dev->parent) ==
75 UCLASS_SIMPLE_BUS)
76 addr = simple_bus_translate(dev->parent, addr);
77 }
78 }
79
Stefan Roese85bddff2019-04-12 16:42:28 +020080#if defined(CONFIG_TRANSLATION_OFFSET)
Simon Glassaadfaf02017-05-17 17:18:04 -060081 /*
82 * Some platforms need a special address translation. Those
83 * platforms (e.g. mvebu in SPL) can configure a translation
Stefan Roese85bddff2019-04-12 16:42:28 +020084 * offset by setting this value in the GD and enaling this
85 * feature via CONFIG_TRANSLATION_OFFSET. This value will
86 * get added to all addresses returned by devfdt_get_addr().
Simon Glassaadfaf02017-05-17 17:18:04 -060087 */
Stefan Roese85bddff2019-04-12 16:42:28 +020088 addr += gd->translation_offset;
89#endif
Simon Glassaadfaf02017-05-17 17:18:04 -060090
91 return addr;
92#else
93 return FDT_ADDR_T_NONE;
94#endif
95}
96
Bin Menga44397f2021-09-12 11:15:12 +080097void *devfdt_get_addr_index_ptr(const struct udevice *dev, int index)
98{
99 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
100
Johan Jonker86648602023-04-23 11:19:41 +0200101 if (addr == FDT_ADDR_T_NONE)
102 return NULL;
103
104 return map_sysmem(addr, 0);
Bin Menga44397f2021-09-12 11:15:12 +0800105}
106
Simon Glassc6548b82020-01-27 08:49:39 -0700107fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index,
108 fdt_size_t *size)
Simon Glassaadfaf02017-05-17 17:18:04 -0600109{
110#if CONFIG_IS_ENABLED(OF_CONTROL)
111 /*
112 * Only get the size in this first call. We'll get the addr in the
113 * next call to the exisiting dev_get_xxx function which handles
114 * all config options.
115 */
116 fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev),
117 "reg", index, size, false);
118
119 /*
120 * Get the base address via the existing function which handles
121 * all Kconfig cases
122 */
Simon Glassba1dea42017-05-17 17:18:05 -0600123 return devfdt_get_addr_index(dev, index);
Simon Glassaadfaf02017-05-17 17:18:04 -0600124#else
125 return FDT_ADDR_T_NONE;
126#endif
127}
128
Johan Jonkeref6769f2023-04-21 17:33:58 +0200129void *devfdt_get_addr_size_index_ptr(const struct udevice *dev, int index,
130 fdt_size_t *size)
131{
132 fdt_addr_t addr = devfdt_get_addr_size_index(dev, index, size);
133
134 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
135}
136
Simon Glassc6548b82020-01-27 08:49:39 -0700137fdt_addr_t devfdt_get_addr_name(const struct udevice *dev, const char *name)
Simon Glassaadfaf02017-05-17 17:18:04 -0600138{
139#if CONFIG_IS_ENABLED(OF_CONTROL)
140 int index;
141
142 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
143 "reg-names", name);
144 if (index < 0)
145 return index;
146
Simon Glassba1dea42017-05-17 17:18:05 -0600147 return devfdt_get_addr_index(dev, index);
Simon Glassaadfaf02017-05-17 17:18:04 -0600148#else
149 return FDT_ADDR_T_NONE;
150#endif
151}
152
Simon Glassc6548b82020-01-27 08:49:39 -0700153fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev,
154 const char *name, fdt_size_t *size)
Sekhar Norif677c6f2019-08-01 19:12:56 +0530155{
156#if CONFIG_IS_ENABLED(OF_CONTROL)
157 int index;
158
159 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
160 "reg-names", name);
161 if (index < 0)
162 return index;
163
164 return devfdt_get_addr_size_index(dev, index, size);
165#else
166 return FDT_ADDR_T_NONE;
167#endif
168}
169
Simon Glassc6548b82020-01-27 08:49:39 -0700170fdt_addr_t devfdt_get_addr(const struct udevice *dev)
Simon Glassaadfaf02017-05-17 17:18:04 -0600171{
Simon Glassba1dea42017-05-17 17:18:05 -0600172 return devfdt_get_addr_index(dev, 0);
Simon Glassaadfaf02017-05-17 17:18:04 -0600173}
174
Simon Glassc6548b82020-01-27 08:49:39 -0700175void *devfdt_get_addr_ptr(const struct udevice *dev)
Simon Glassaadfaf02017-05-17 17:18:04 -0600176{
Bin Menga44397f2021-09-12 11:15:12 +0800177 return devfdt_get_addr_index_ptr(dev, 0);
Simon Glassaadfaf02017-05-17 17:18:04 -0600178}
179
Simon Glassc6548b82020-01-27 08:49:39 -0700180void *devfdt_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200181{
Cédric Le Goaterc0fb7e62018-08-14 12:48:11 +0200182 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200183
184 if (addr == FDT_ADDR_T_NONE)
185 return NULL;
186
187 return map_physmem(addr, 0, MAP_NOCACHE);
188}
189
Simon Glassc6548b82020-01-27 08:49:39 -0700190void *devfdt_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100191{
192 fdt_addr_t addr = devfdt_get_addr_name(dev, name);
193
194 if (addr == FDT_ADDR_T_NONE)
195 return NULL;
196
197 return map_physmem(addr, 0, MAP_NOCACHE);
198}
199
Simon Glassc6548b82020-01-27 08:49:39 -0700200void *devfdt_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200201{
202 return devfdt_remap_addr_index(dev, 0);
203}
204
Simon Glassc6548b82020-01-27 08:49:39 -0700205void *devfdt_map_physmem(const struct udevice *dev, unsigned long size)
Simon Glassaadfaf02017-05-17 17:18:04 -0600206{
Simon Glassba1dea42017-05-17 17:18:05 -0600207 fdt_addr_t addr = devfdt_get_addr(dev);
Simon Glassaadfaf02017-05-17 17:18:04 -0600208
209 if (addr == FDT_ADDR_T_NONE)
210 return NULL;
211
212 return map_physmem(addr, size, MAP_NOCACHE);
213}
Simon Glass23b27592019-09-15 12:08:58 -0600214
Simon Glassc6548b82020-01-27 08:49:39 -0700215fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600216{
217 ulong addr;
218
219 addr = devfdt_get_addr(dev);
Simon Glass82278e32021-08-01 18:54:23 -0600220 if (CONFIG_IS_ENABLED(PCI) && addr == FDT_ADDR_T_NONE) {
Simon Glass23b27592019-09-15 12:08:58 -0600221 struct fdt_pci_addr pci_addr;
222 u32 bar;
223 int ret;
224
Simon Glass04ec59c2019-09-25 08:56:41 -0600225 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32,
226 "reg", &pci_addr);
Simon Glass23b27592019-09-15 12:08:58 -0600227 if (ret) {
228 /* try if there is any i/o-mapped register */
Simon Glass04ec59c2019-09-25 08:56:41 -0600229 ret = ofnode_read_pci_addr(dev_ofnode(dev),
230 FDT_PCI_SPACE_IO, "reg",
231 &pci_addr);
Simon Glass23b27592019-09-15 12:08:58 -0600232 if (ret)
233 return FDT_ADDR_T_NONE;
234 }
235 ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar);
236 if (ret)
237 return FDT_ADDR_T_NONE;
238 addr = bar;
239 }
240
241 return addr;
242}