blob: dfcb868f6504709c9b2eb21c54fde59b44797c84 [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 Glassaadfaf02017-05-17 17:18:04 -060015#include <asm/io.h>
16#include <dm/device-internal.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Simon Glassc6548b82020-01-27 08:49:39 -070020fdt_addr_t devfdt_get_addr_index(const struct udevice *dev, int index)
Simon Glassaadfaf02017-05-17 17:18:04 -060021{
22#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
23 fdt_addr_t addr;
24
25 if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
26 const fdt32_t *reg;
27 int len = 0;
28 int na, ns;
29
30 na = fdt_address_cells(gd->fdt_blob,
31 dev_of_offset(dev->parent));
32 if (na < 1) {
33 debug("bad #address-cells\n");
34 return FDT_ADDR_T_NONE;
35 }
36
37 ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
38 if (ns < 0) {
39 debug("bad #size-cells\n");
40 return FDT_ADDR_T_NONE;
41 }
42
43 reg = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "reg",
44 &len);
45 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
46 debug("Req index out of range\n");
47 return FDT_ADDR_T_NONE;
48 }
49
50 reg += index * (na + ns);
51
Mario Six35616ef2018-03-12 14:53:33 +010052 if (ns) {
53 /*
54 * Use the full-fledged translate function for complex
55 * bus setups.
56 */
57 addr = fdt_translate_address((void *)gd->fdt_blob,
58 dev_of_offset(dev), reg);
59 } else {
60 /* Non translatable if #size-cells == 0 */
61 addr = fdt_read_number(reg, na);
62 }
Simon Glassaadfaf02017-05-17 17:18:04 -060063 } else {
64 /*
65 * Use the "simple" translate function for less complex
66 * bus setups.
67 */
68 addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
69 dev_of_offset(dev->parent), dev_of_offset(dev),
70 "reg", index, NULL, false);
71 if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
72 if (device_get_uclass_id(dev->parent) ==
73 UCLASS_SIMPLE_BUS)
74 addr = simple_bus_translate(dev->parent, addr);
75 }
76 }
77
Stefan Roese85bddff2019-04-12 16:42:28 +020078#if defined(CONFIG_TRANSLATION_OFFSET)
Simon Glassaadfaf02017-05-17 17:18:04 -060079 /*
80 * Some platforms need a special address translation. Those
81 * platforms (e.g. mvebu in SPL) can configure a translation
Stefan Roese85bddff2019-04-12 16:42:28 +020082 * offset by setting this value in the GD and enaling this
83 * feature via CONFIG_TRANSLATION_OFFSET. This value will
84 * get added to all addresses returned by devfdt_get_addr().
Simon Glassaadfaf02017-05-17 17:18:04 -060085 */
Stefan Roese85bddff2019-04-12 16:42:28 +020086 addr += gd->translation_offset;
87#endif
Simon Glassaadfaf02017-05-17 17:18:04 -060088
89 return addr;
90#else
91 return FDT_ADDR_T_NONE;
92#endif
93}
94
Simon Glassc6548b82020-01-27 08:49:39 -070095fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index,
96 fdt_size_t *size)
Simon Glassaadfaf02017-05-17 17:18:04 -060097{
98#if CONFIG_IS_ENABLED(OF_CONTROL)
99 /*
100 * Only get the size in this first call. We'll get the addr in the
101 * next call to the exisiting dev_get_xxx function which handles
102 * all config options.
103 */
104 fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev),
105 "reg", index, size, false);
106
107 /*
108 * Get the base address via the existing function which handles
109 * all Kconfig cases
110 */
Simon Glassba1dea42017-05-17 17:18:05 -0600111 return devfdt_get_addr_index(dev, index);
Simon Glassaadfaf02017-05-17 17:18:04 -0600112#else
113 return FDT_ADDR_T_NONE;
114#endif
115}
116
Simon Glassc6548b82020-01-27 08:49:39 -0700117fdt_addr_t devfdt_get_addr_name(const struct udevice *dev, const char *name)
Simon Glassaadfaf02017-05-17 17:18:04 -0600118{
119#if CONFIG_IS_ENABLED(OF_CONTROL)
120 int index;
121
122 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
123 "reg-names", name);
124 if (index < 0)
125 return index;
126
Simon Glassba1dea42017-05-17 17:18:05 -0600127 return devfdt_get_addr_index(dev, index);
Simon Glassaadfaf02017-05-17 17:18:04 -0600128#else
129 return FDT_ADDR_T_NONE;
130#endif
131}
132
Simon Glassc6548b82020-01-27 08:49:39 -0700133fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev,
134 const char *name, fdt_size_t *size)
Sekhar Norif677c6f2019-08-01 19:12:56 +0530135{
136#if CONFIG_IS_ENABLED(OF_CONTROL)
137 int index;
138
139 index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
140 "reg-names", name);
141 if (index < 0)
142 return index;
143
144 return devfdt_get_addr_size_index(dev, index, size);
145#else
146 return FDT_ADDR_T_NONE;
147#endif
148}
149
Simon Glassc6548b82020-01-27 08:49:39 -0700150fdt_addr_t devfdt_get_addr(const struct udevice *dev)
Simon Glassaadfaf02017-05-17 17:18:04 -0600151{
Simon Glassba1dea42017-05-17 17:18:05 -0600152 return devfdt_get_addr_index(dev, 0);
Simon Glassaadfaf02017-05-17 17:18:04 -0600153}
154
Simon Glassc6548b82020-01-27 08:49:39 -0700155void *devfdt_get_addr_ptr(const struct udevice *dev)
Simon Glassaadfaf02017-05-17 17:18:04 -0600156{
Simon Glassba1dea42017-05-17 17:18:05 -0600157 return (void *)(uintptr_t)devfdt_get_addr_index(dev, 0);
Simon Glassaadfaf02017-05-17 17:18:04 -0600158}
159
Simon Glassc6548b82020-01-27 08:49:39 -0700160void *devfdt_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200161{
Cédric Le Goaterc0fb7e62018-08-14 12:48:11 +0200162 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200163
164 if (addr == FDT_ADDR_T_NONE)
165 return NULL;
166
167 return map_physmem(addr, 0, MAP_NOCACHE);
168}
169
Simon Glassc6548b82020-01-27 08:49:39 -0700170void *devfdt_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100171{
172 fdt_addr_t addr = devfdt_get_addr_name(dev, name);
173
174 if (addr == FDT_ADDR_T_NONE)
175 return NULL;
176
177 return map_physmem(addr, 0, MAP_NOCACHE);
178}
179
Simon Glassc6548b82020-01-27 08:49:39 -0700180void *devfdt_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200181{
182 return devfdt_remap_addr_index(dev, 0);
183}
184
Simon Glassc6548b82020-01-27 08:49:39 -0700185void *devfdt_map_physmem(const struct udevice *dev, unsigned long size)
Simon Glassaadfaf02017-05-17 17:18:04 -0600186{
Simon Glassba1dea42017-05-17 17:18:05 -0600187 fdt_addr_t addr = devfdt_get_addr(dev);
Simon Glassaadfaf02017-05-17 17:18:04 -0600188
189 if (addr == FDT_ADDR_T_NONE)
190 return NULL;
191
192 return map_physmem(addr, size, MAP_NOCACHE);
193}
Simon Glass23b27592019-09-15 12:08:58 -0600194
Simon Glassc6548b82020-01-27 08:49:39 -0700195fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600196{
197 ulong addr;
198
199 addr = devfdt_get_addr(dev);
200 if (CONFIG_IS_ENABLED(PCI) && IS_ENABLED(CONFIG_DM_PCI) &&
201 addr == FDT_ADDR_T_NONE) {
202 struct fdt_pci_addr pci_addr;
203 u32 bar;
204 int ret;
205
Simon Glass04ec59c2019-09-25 08:56:41 -0600206 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32,
207 "reg", &pci_addr);
Simon Glass23b27592019-09-15 12:08:58 -0600208 if (ret) {
209 /* try if there is any i/o-mapped register */
Simon Glass04ec59c2019-09-25 08:56:41 -0600210 ret = ofnode_read_pci_addr(dev_ofnode(dev),
211 FDT_PCI_SPACE_IO, "reg",
212 &pci_addr);
Simon Glass23b27592019-09-15 12:08:58 -0600213 if (ret)
214 return FDT_ADDR_T_NONE;
215 }
216 ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar);
217 if (ret)
218 return FDT_ADDR_T_NONE;
219 addr = bar;
220 }
221
222 return addr;
223}