blob: 2aa58b006f1a93615c3fd56aaadb3274ddd0dd65 [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
Simon Glassaadfaf02017-05-17 17:18:04 -060011#include <dm.h>
12#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Johan Jonker86648602023-04-23 11:19:41 +020014#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glassaadfaf02017-05-17 17:18:04 -060016#include <asm/io.h>
17#include <dm/device-internal.h>
Quentin Schulz95a5a2b2024-06-11 15:04:26 +020018#include <dm/util.h>
Simon Glassaadfaf02017-05-17 17:18:04 -060019
20DECLARE_GLOBAL_DATA_PTR;
21
Jonas Karlman42a89ec2024-08-04 15:05:49 +000022#if CONFIG_IS_ENABLED(OF_REAL) || CONFIG_IS_ENABLED(OF_CONTROL)
23fdt_addr_t devfdt_get_addr_index_parent(const struct udevice *dev, int index,
24 int offset, int parent)
Simon Glassaadfaf02017-05-17 17:18:04 -060025{
Simon Glassaadfaf02017-05-17 17:18:04 -060026 fdt_addr_t addr;
27
28 if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
29 const fdt32_t *reg;
30 int len = 0;
31 int na, ns;
32
Simon Glasscb12c9f2022-09-06 20:27:12 -060033 na = fdt_address_cells(gd->fdt_blob, parent);
Simon Glassaadfaf02017-05-17 17:18:04 -060034 if (na < 1) {
Quentin Schulz95a5a2b2024-06-11 15:04:26 +020035 dm_warn("bad #address-cells\n");
Simon Glassaadfaf02017-05-17 17:18:04 -060036 return FDT_ADDR_T_NONE;
37 }
38
Simon Glasscb12c9f2022-09-06 20:27:12 -060039 ns = fdt_size_cells(gd->fdt_blob, parent);
Simon Glassaadfaf02017-05-17 17:18:04 -060040 if (ns < 0) {
Quentin Schulz95a5a2b2024-06-11 15:04:26 +020041 dm_warn("bad #size-cells\n");
Simon Glassaadfaf02017-05-17 17:18:04 -060042 return FDT_ADDR_T_NONE;
43 }
44
Simon Glasscb12c9f2022-09-06 20:27:12 -060045 reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
Simon Glassaadfaf02017-05-17 17:18:04 -060046 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
Quentin Schulz95a5a2b2024-06-11 15:04:26 +020047 dm_warn("Req index out of range\n");
Simon Glassaadfaf02017-05-17 17:18:04 -060048 return FDT_ADDR_T_NONE;
49 }
50
51 reg += index * (na + ns);
52
Dario Binacchif8fc7032021-05-01 17:05:26 +020053 if (ns) {
Mario Six35616ef2018-03-12 14:53:33 +010054 /*
55 * Use the full-fledged translate function for complex
56 * bus setups.
57 */
58 addr = fdt_translate_address((void *)gd->fdt_blob,
Simon Glasscb12c9f2022-09-06 20:27:12 -060059 offset, reg);
Mario Six35616ef2018-03-12 14:53:33 +010060 } else {
61 /* Non translatable if #size-cells == 0 */
62 addr = fdt_read_number(reg, na);
63 }
Simon Glassaadfaf02017-05-17 17:18:04 -060064 } else {
65 /*
66 * Use the "simple" translate function for less complex
67 * bus setups.
68 */
Simon Glasscb12c9f2022-09-06 20:27:12 -060069 addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob, parent,
70 offset, "reg", index,
71 NULL, false);
Simon Glassaadfaf02017-05-17 17:18:04 -060072 if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
73 if (device_get_uclass_id(dev->parent) ==
74 UCLASS_SIMPLE_BUS)
75 addr = simple_bus_translate(dev->parent, addr);
76 }
77 }
78
Stefan Roese85bddff2019-04-12 16:42:28 +020079#if defined(CONFIG_TRANSLATION_OFFSET)
Simon Glassaadfaf02017-05-17 17:18:04 -060080 /*
81 * Some platforms need a special address translation. Those
82 * platforms (e.g. mvebu in SPL) can configure a translation
Stefan Roese85bddff2019-04-12 16:42:28 +020083 * offset by setting this value in the GD and enaling this
84 * feature via CONFIG_TRANSLATION_OFFSET. This value will
85 * get added to all addresses returned by devfdt_get_addr().
Simon Glassaadfaf02017-05-17 17:18:04 -060086 */
Stefan Roese85bddff2019-04-12 16:42:28 +020087 addr += gd->translation_offset;
88#endif
Simon Glassaadfaf02017-05-17 17:18:04 -060089
90 return addr;
Jonas Karlman42a89ec2024-08-04 15:05:49 +000091}
92#endif
93
94fdt_addr_t devfdt_get_addr_index(const struct udevice *dev, int index)
95{
96#if CONFIG_IS_ENABLED(OF_REAL)
97 int offset = dev_of_offset(dev);
98 int parent = fdt_parent_offset(gd->fdt_blob, offset);
99 return devfdt_get_addr_index_parent(dev, index, offset, parent);
Simon Glassaadfaf02017-05-17 17:18:04 -0600100#else
101 return FDT_ADDR_T_NONE;
102#endif
103}
104
Bin Menga44397f2021-09-12 11:15:12 +0800105void *devfdt_get_addr_index_ptr(const struct udevice *dev, int index)
106{
107 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
108
Johan Jonker86648602023-04-23 11:19:41 +0200109 if (addr == FDT_ADDR_T_NONE)
110 return NULL;
111
112 return map_sysmem(addr, 0);
Bin Menga44397f2021-09-12 11:15:12 +0800113}
114
Simon Glassc6548b82020-01-27 08:49:39 -0700115fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index,
116 fdt_size_t *size)
Simon Glassaadfaf02017-05-17 17:18:04 -0600117{
118#if CONFIG_IS_ENABLED(OF_CONTROL)
119 /*
120 * Only get the size in this first call. We'll get the addr in the
121 * next call to the exisiting dev_get_xxx function which handles
122 * all config options.
123 */
Jonas Karlman42a89ec2024-08-04 15:05:49 +0000124 int offset = dev_of_offset(dev);
125 int parent = fdt_parent_offset(gd->fdt_blob, offset);
126 fdtdec_get_addr_size_auto_parent(gd->fdt_blob, parent, offset,
127 "reg", index, size, false);
Simon Glassaadfaf02017-05-17 17:18:04 -0600128
129 /*
130 * Get the base address via the existing function which handles
131 * all Kconfig cases
132 */
Jonas Karlman42a89ec2024-08-04 15:05:49 +0000133 return devfdt_get_addr_index_parent(dev, index, offset, parent);
Simon Glassaadfaf02017-05-17 17:18:04 -0600134#else
135 return FDT_ADDR_T_NONE;
136#endif
137}
138
Johan Jonkeref6769f2023-04-21 17:33:58 +0200139void *devfdt_get_addr_size_index_ptr(const struct udevice *dev, int index,
140 fdt_size_t *size)
141{
142 fdt_addr_t addr = devfdt_get_addr_size_index(dev, index, size);
143
Johan Jonker4d3bce42023-05-10 23:48:44 +0200144 if (addr == FDT_ADDR_T_NONE)
145 return NULL;
146
147 return map_sysmem(addr, 0);
Johan Jonkeref6769f2023-04-21 17:33:58 +0200148}
149
Simon Glassc6548b82020-01-27 08:49:39 -0700150fdt_addr_t devfdt_get_addr_name(const struct udevice *dev, const char *name)
Simon Glassaadfaf02017-05-17 17:18:04 -0600151{
152#if CONFIG_IS_ENABLED(OF_CONTROL)
153 int index;
154
155 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
156 "reg-names", name);
157 if (index < 0)
Matthias Schifferbc2b85d2023-09-27 15:33:31 +0200158 return FDT_ADDR_T_NONE;
Simon Glassaadfaf02017-05-17 17:18:04 -0600159
Simon Glassba1dea42017-05-17 17:18:05 -0600160 return devfdt_get_addr_index(dev, index);
Simon Glassaadfaf02017-05-17 17:18:04 -0600161#else
162 return FDT_ADDR_T_NONE;
163#endif
164}
165
Matthias Schiffer5e90d4e2023-09-27 15:33:32 +0200166void *devfdt_get_addr_name_ptr(const struct udevice *dev, const char *name)
167{
168 fdt_addr_t addr = devfdt_get_addr_name(dev, name);
169
170 if (addr == FDT_ADDR_T_NONE)
171 return NULL;
172
173 return map_sysmem(addr, 0);
174}
175
Simon Glassc6548b82020-01-27 08:49:39 -0700176fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev,
177 const char *name, fdt_size_t *size)
Sekhar Norif677c6f2019-08-01 19:12:56 +0530178{
179#if CONFIG_IS_ENABLED(OF_CONTROL)
180 int index;
181
182 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
183 "reg-names", name);
184 if (index < 0)
Matthias Schifferbc2b85d2023-09-27 15:33:31 +0200185 return FDT_ADDR_T_NONE;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530186
187 return devfdt_get_addr_size_index(dev, index, size);
188#else
189 return FDT_ADDR_T_NONE;
190#endif
191}
192
Matthias Schiffer5e90d4e2023-09-27 15:33:32 +0200193void *devfdt_get_addr_size_name_ptr(const struct udevice *dev,
194 const char *name, fdt_size_t *size)
195{
196 fdt_addr_t addr = devfdt_get_addr_size_name(dev, name, size);
197
198 if (addr == FDT_ADDR_T_NONE)
199 return NULL;
200
201 return map_sysmem(addr, 0);
202}
203
Simon Glassc6548b82020-01-27 08:49:39 -0700204fdt_addr_t devfdt_get_addr(const struct udevice *dev)
Simon Glassaadfaf02017-05-17 17:18:04 -0600205{
Simon Glassba1dea42017-05-17 17:18:05 -0600206 return devfdt_get_addr_index(dev, 0);
Simon Glassaadfaf02017-05-17 17:18:04 -0600207}
208
Simon Glassc6548b82020-01-27 08:49:39 -0700209void *devfdt_get_addr_ptr(const struct udevice *dev)
Simon Glassaadfaf02017-05-17 17:18:04 -0600210{
Bin Menga44397f2021-09-12 11:15:12 +0800211 return devfdt_get_addr_index_ptr(dev, 0);
Simon Glassaadfaf02017-05-17 17:18:04 -0600212}
213
Simon Glassc6548b82020-01-27 08:49:39 -0700214void *devfdt_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200215{
Cédric Le Goaterc0fb7e62018-08-14 12:48:11 +0200216 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200217
218 if (addr == FDT_ADDR_T_NONE)
219 return NULL;
220
221 return map_physmem(addr, 0, MAP_NOCACHE);
222}
223
Simon Glassc6548b82020-01-27 08:49:39 -0700224void *devfdt_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100225{
226 fdt_addr_t addr = devfdt_get_addr_name(dev, name);
227
228 if (addr == FDT_ADDR_T_NONE)
229 return NULL;
230
231 return map_physmem(addr, 0, MAP_NOCACHE);
232}
233
Simon Glassc6548b82020-01-27 08:49:39 -0700234void *devfdt_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200235{
236 return devfdt_remap_addr_index(dev, 0);
237}
238
Simon Glassc6548b82020-01-27 08:49:39 -0700239void *devfdt_map_physmem(const struct udevice *dev, unsigned long size)
Simon Glassaadfaf02017-05-17 17:18:04 -0600240{
Simon Glassba1dea42017-05-17 17:18:05 -0600241 fdt_addr_t addr = devfdt_get_addr(dev);
Simon Glassaadfaf02017-05-17 17:18:04 -0600242
243 if (addr == FDT_ADDR_T_NONE)
244 return NULL;
245
246 return map_physmem(addr, size, MAP_NOCACHE);
247}
Simon Glass23b27592019-09-15 12:08:58 -0600248
Simon Glass4289c262023-09-26 08:14:58 -0600249fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev, fdt_size_t *sizep)
Simon Glass23b27592019-09-15 12:08:58 -0600250{
251 ulong addr;
252
253 addr = devfdt_get_addr(dev);
Simon Glass82278e32021-08-01 18:54:23 -0600254 if (CONFIG_IS_ENABLED(PCI) && addr == FDT_ADDR_T_NONE) {
Simon Glass23b27592019-09-15 12:08:58 -0600255 struct fdt_pci_addr pci_addr;
256 u32 bar;
257 int ret;
258
Simon Glass04ec59c2019-09-25 08:56:41 -0600259 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32,
Simon Glass4289c262023-09-26 08:14:58 -0600260 "reg", &pci_addr, sizep);
Simon Glass23b27592019-09-15 12:08:58 -0600261 if (ret) {
262 /* try if there is any i/o-mapped register */
Simon Glass04ec59c2019-09-25 08:56:41 -0600263 ret = ofnode_read_pci_addr(dev_ofnode(dev),
264 FDT_PCI_SPACE_IO, "reg",
Simon Glass4289c262023-09-26 08:14:58 -0600265 &pci_addr, sizep);
Simon Glass23b27592019-09-15 12:08:58 -0600266 if (ret)
267 return FDT_ADDR_T_NONE;
268 }
269 ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar);
270 if (ret)
271 return FDT_ADDR_T_NONE;
272 addr = bar;
273 }
274
275 return addr;
276}