blob: ed55f69de1249f45543729fd8f2f47d8b33c0061 [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
Dario Binacchib574d682020-12-30 00:16:21 +010052 if (ns || gd_size_cells_0()) {
Mario Six35616ef2018-03-12 14:53:33 +010053 /*
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{
Ovidiu Panaita633f002020-08-03 22:17:35 +0300157 fdt_addr_t addr = devfdt_get_addr_index(dev, 0);
158
159 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
Simon Glassaadfaf02017-05-17 17:18:04 -0600160}
161
Simon Glassc6548b82020-01-27 08:49:39 -0700162void *devfdt_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200163{
Cédric Le Goaterc0fb7e62018-08-14 12:48:11 +0200164 fdt_addr_t addr = devfdt_get_addr_index(dev, index);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200165
166 if (addr == FDT_ADDR_T_NONE)
167 return NULL;
168
169 return map_physmem(addr, 0, MAP_NOCACHE);
170}
171
Simon Glassc6548b82020-01-27 08:49:39 -0700172void *devfdt_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100173{
174 fdt_addr_t addr = devfdt_get_addr_name(dev, name);
175
176 if (addr == FDT_ADDR_T_NONE)
177 return NULL;
178
179 return map_physmem(addr, 0, MAP_NOCACHE);
180}
181
Simon Glassc6548b82020-01-27 08:49:39 -0700182void *devfdt_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200183{
184 return devfdt_remap_addr_index(dev, 0);
185}
186
Simon Glassc6548b82020-01-27 08:49:39 -0700187void *devfdt_map_physmem(const struct udevice *dev, unsigned long size)
Simon Glassaadfaf02017-05-17 17:18:04 -0600188{
Simon Glassba1dea42017-05-17 17:18:05 -0600189 fdt_addr_t addr = devfdt_get_addr(dev);
Simon Glassaadfaf02017-05-17 17:18:04 -0600190
191 if (addr == FDT_ADDR_T_NONE)
192 return NULL;
193
194 return map_physmem(addr, size, MAP_NOCACHE);
195}
Simon Glass23b27592019-09-15 12:08:58 -0600196
Simon Glassc6548b82020-01-27 08:49:39 -0700197fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600198{
199 ulong addr;
200
201 addr = devfdt_get_addr(dev);
202 if (CONFIG_IS_ENABLED(PCI) && IS_ENABLED(CONFIG_DM_PCI) &&
203 addr == FDT_ADDR_T_NONE) {
204 struct fdt_pci_addr pci_addr;
205 u32 bar;
206 int ret;
207
Simon Glass04ec59c2019-09-25 08:56:41 -0600208 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32,
209 "reg", &pci_addr);
Simon Glass23b27592019-09-15 12:08:58 -0600210 if (ret) {
211 /* try if there is any i/o-mapped register */
Simon Glass04ec59c2019-09-25 08:56:41 -0600212 ret = ofnode_read_pci_addr(dev_ofnode(dev),
213 FDT_PCI_SPACE_IO, "reg",
214 &pci_addr);
Simon Glass23b27592019-09-15 12:08:58 -0600215 if (ret)
216 return FDT_ADDR_T_NONE;
217 }
218 ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar);
219 if (ret)
220 return FDT_ADDR_T_NONE;
221 addr = bar;
222 }
223
224 return addr;
225}