blob: c3a50a2b0c1a4b38a6d109e218249b1ca2494d7b [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>
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>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassc6548b82020-01-27 08:49:39 -070021fdt_addr_t devfdt_get_addr_index(const struct udevice *dev, int index)
Simon Glassaadfaf02017-05-17 17:18:04 -060022{
Simon Glass3580f6d2021-08-07 07:24:03 -060023#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassaadfaf02017-05-17 17:18:04 -060024 fdt_addr_t addr;
25
26 if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
27 const fdt32_t *reg;
28 int len = 0;
29 int na, ns;
30
31 na = fdt_address_cells(gd->fdt_blob,
32 dev_of_offset(dev->parent));
33 if (na < 1) {
34 debug("bad #address-cells\n");
35 return FDT_ADDR_T_NONE;
36 }
37
38 ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
39 if (ns < 0) {
40 debug("bad #size-cells\n");
41 return FDT_ADDR_T_NONE;
42 }
43
44 reg = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "reg",
45 &len);
46 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
47 debug("Req index out of range\n");
48 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,
59 dev_of_offset(dev), reg);
60 } 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 */
69 addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
70 dev_of_offset(dev->parent), dev_of_offset(dev),
71 "reg", index, NULL, false);
72 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;
91#else
92 return FDT_ADDR_T_NONE;
93#endif
94}
95
Bin Menga44397f2021-09-12 11:15:12 +080096void *devfdt_get_addr_index_ptr(const struct udevice *dev, int index)
97{
98 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
99
100 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
101}
102
Simon Glassc6548b82020-01-27 08:49:39 -0700103fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index,
104 fdt_size_t *size)
Simon Glassaadfaf02017-05-17 17:18:04 -0600105{
106#if CONFIG_IS_ENABLED(OF_CONTROL)
107 /*
108 * Only get the size in this first call. We'll get the addr in the
109 * next call to the exisiting dev_get_xxx function which handles
110 * all config options.
111 */
112 fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev),
113 "reg", index, size, false);
114
115 /*
116 * Get the base address via the existing function which handles
117 * all Kconfig cases
118 */
Simon Glassba1dea42017-05-17 17:18:05 -0600119 return devfdt_get_addr_index(dev, index);
Simon Glassaadfaf02017-05-17 17:18:04 -0600120#else
121 return FDT_ADDR_T_NONE;
122#endif
123}
124
Simon Glassc6548b82020-01-27 08:49:39 -0700125fdt_addr_t devfdt_get_addr_name(const struct udevice *dev, const char *name)
Simon Glassaadfaf02017-05-17 17:18:04 -0600126{
127#if CONFIG_IS_ENABLED(OF_CONTROL)
128 int index;
129
130 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
131 "reg-names", name);
132 if (index < 0)
133 return index;
134
Simon Glassba1dea42017-05-17 17:18:05 -0600135 return devfdt_get_addr_index(dev, index);
Simon Glassaadfaf02017-05-17 17:18:04 -0600136#else
137 return FDT_ADDR_T_NONE;
138#endif
139}
140
Simon Glassc6548b82020-01-27 08:49:39 -0700141fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev,
142 const char *name, fdt_size_t *size)
Sekhar Norif677c6f2019-08-01 19:12:56 +0530143{
144#if CONFIG_IS_ENABLED(OF_CONTROL)
145 int index;
146
147 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
148 "reg-names", name);
149 if (index < 0)
150 return index;
151
152 return devfdt_get_addr_size_index(dev, index, size);
153#else
154 return FDT_ADDR_T_NONE;
155#endif
156}
157
Simon Glassc6548b82020-01-27 08:49:39 -0700158fdt_addr_t devfdt_get_addr(const struct udevice *dev)
Simon Glassaadfaf02017-05-17 17:18:04 -0600159{
Simon Glassba1dea42017-05-17 17:18:05 -0600160 return devfdt_get_addr_index(dev, 0);
Simon Glassaadfaf02017-05-17 17:18:04 -0600161}
162
Simon Glassc6548b82020-01-27 08:49:39 -0700163void *devfdt_get_addr_ptr(const struct udevice *dev)
Simon Glassaadfaf02017-05-17 17:18:04 -0600164{
Bin Menga44397f2021-09-12 11:15:12 +0800165 return devfdt_get_addr_index_ptr(dev, 0);
Simon Glassaadfaf02017-05-17 17:18:04 -0600166}
167
Simon Glassc6548b82020-01-27 08:49:39 -0700168void *devfdt_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200169{
Cédric Le Goaterc0fb7e62018-08-14 12:48:11 +0200170 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200171
172 if (addr == FDT_ADDR_T_NONE)
173 return NULL;
174
175 return map_physmem(addr, 0, MAP_NOCACHE);
176}
177
Simon Glassc6548b82020-01-27 08:49:39 -0700178void *devfdt_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100179{
180 fdt_addr_t addr = devfdt_get_addr_name(dev, name);
181
182 if (addr == FDT_ADDR_T_NONE)
183 return NULL;
184
185 return map_physmem(addr, 0, MAP_NOCACHE);
186}
187
Simon Glassc6548b82020-01-27 08:49:39 -0700188void *devfdt_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200189{
190 return devfdt_remap_addr_index(dev, 0);
191}
192
Simon Glassc6548b82020-01-27 08:49:39 -0700193void *devfdt_map_physmem(const struct udevice *dev, unsigned long size)
Simon Glassaadfaf02017-05-17 17:18:04 -0600194{
Simon Glassba1dea42017-05-17 17:18:05 -0600195 fdt_addr_t addr = devfdt_get_addr(dev);
Simon Glassaadfaf02017-05-17 17:18:04 -0600196
197 if (addr == FDT_ADDR_T_NONE)
198 return NULL;
199
200 return map_physmem(addr, size, MAP_NOCACHE);
201}
Simon Glass23b27592019-09-15 12:08:58 -0600202
Simon Glassc6548b82020-01-27 08:49:39 -0700203fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600204{
205 ulong addr;
206
207 addr = devfdt_get_addr(dev);
Simon Glass82278e32021-08-01 18:54:23 -0600208 if (CONFIG_IS_ENABLED(PCI) && addr == FDT_ADDR_T_NONE) {
Simon Glass23b27592019-09-15 12:08:58 -0600209 struct fdt_pci_addr pci_addr;
210 u32 bar;
211 int ret;
212
Simon Glass04ec59c2019-09-25 08:56:41 -0600213 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32,
214 "reg", &pci_addr);
Simon Glass23b27592019-09-15 12:08:58 -0600215 if (ret) {
216 /* try if there is any i/o-mapped register */
Simon Glass04ec59c2019-09-25 08:56:41 -0600217 ret = ofnode_read_pci_addr(dev_ofnode(dev),
218 FDT_PCI_SPACE_IO, "reg",
219 &pci_addr);
Simon Glass23b27592019-09-15 12:08:58 -0600220 if (ret)
221 return FDT_ADDR_T_NONE;
222 }
223 ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar);
224 if (ret)
225 return FDT_ADDR_T_NONE;
226 addr = bar;
227 }
228
229 return addr;
230}