Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Device addresses |
| 4 | * |
| 5 | * Copyright (c) 2017 Google, Inc |
| 6 | * |
| 7 | * (C) Copyright 2012 |
| 8 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 9 | */ |
| 10 | |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <fdt_support.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Johan Jonker | 8664860 | 2023-04-23 11:19:41 +0200 | [diff] [blame] | 14 | #include <mapmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | #include <dm/device-internal.h> |
Quentin Schulz | 95a5a2b | 2024-06-11 15:04:26 +0200 | [diff] [blame] | 18 | #include <dm/util.h> |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Jonas Karlman | 42a89ec | 2024-08-04 15:05:49 +0000 | [diff] [blame] | 22 | #if CONFIG_IS_ENABLED(OF_REAL) || CONFIG_IS_ENABLED(OF_CONTROL) |
| 23 | fdt_addr_t devfdt_get_addr_index_parent(const struct udevice *dev, int index, |
| 24 | int offset, int parent) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 25 | { |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 26 | 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 Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 33 | na = fdt_address_cells(gd->fdt_blob, parent); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 34 | if (na < 1) { |
Quentin Schulz | 95a5a2b | 2024-06-11 15:04:26 +0200 | [diff] [blame] | 35 | dm_warn("bad #address-cells\n"); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 36 | return FDT_ADDR_T_NONE; |
| 37 | } |
| 38 | |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 39 | ns = fdt_size_cells(gd->fdt_blob, parent); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 40 | if (ns < 0) { |
Quentin Schulz | 95a5a2b | 2024-06-11 15:04:26 +0200 | [diff] [blame] | 41 | dm_warn("bad #size-cells\n"); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 42 | return FDT_ADDR_T_NONE; |
| 43 | } |
| 44 | |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 45 | reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 46 | if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) { |
Quentin Schulz | 95a5a2b | 2024-06-11 15:04:26 +0200 | [diff] [blame] | 47 | dm_warn("Req index out of range\n"); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 48 | return FDT_ADDR_T_NONE; |
| 49 | } |
| 50 | |
| 51 | reg += index * (na + ns); |
| 52 | |
Dario Binacchi | f8fc703 | 2021-05-01 17:05:26 +0200 | [diff] [blame] | 53 | if (ns) { |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 54 | /* |
| 55 | * Use the full-fledged translate function for complex |
| 56 | * bus setups. |
| 57 | */ |
| 58 | addr = fdt_translate_address((void *)gd->fdt_blob, |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 59 | offset, reg); |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 60 | } else { |
| 61 | /* Non translatable if #size-cells == 0 */ |
| 62 | addr = fdt_read_number(reg, na); |
| 63 | } |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 64 | } else { |
| 65 | /* |
| 66 | * Use the "simple" translate function for less complex |
| 67 | * bus setups. |
| 68 | */ |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 69 | addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob, parent, |
| 70 | offset, "reg", index, |
| 71 | NULL, false); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 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 Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 79 | #if defined(CONFIG_TRANSLATION_OFFSET) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 80 | /* |
| 81 | * Some platforms need a special address translation. Those |
| 82 | * platforms (e.g. mvebu in SPL) can configure a translation |
Stefan Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 83 | * 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 Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 86 | */ |
Stefan Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 87 | addr += gd->translation_offset; |
| 88 | #endif |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 89 | |
| 90 | return addr; |
Jonas Karlman | 42a89ec | 2024-08-04 15:05:49 +0000 | [diff] [blame] | 91 | } |
| 92 | #endif |
| 93 | |
| 94 | fdt_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 Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 100 | #else |
| 101 | return FDT_ADDR_T_NONE; |
| 102 | #endif |
| 103 | } |
| 104 | |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 105 | void *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 Jonker | 8664860 | 2023-04-23 11:19:41 +0200 | [diff] [blame] | 109 | if (addr == FDT_ADDR_T_NONE) |
| 110 | return NULL; |
| 111 | |
| 112 | return map_sysmem(addr, 0); |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 115 | fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index, |
| 116 | fdt_size_t *size) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 117 | { |
| 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 Karlman | 42a89ec | 2024-08-04 15:05:49 +0000 | [diff] [blame] | 124 | 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 Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * Get the base address via the existing function which handles |
| 131 | * all Kconfig cases |
| 132 | */ |
Jonas Karlman | 42a89ec | 2024-08-04 15:05:49 +0000 | [diff] [blame] | 133 | return devfdt_get_addr_index_parent(dev, index, offset, parent); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 134 | #else |
| 135 | return FDT_ADDR_T_NONE; |
| 136 | #endif |
| 137 | } |
| 138 | |
Johan Jonker | ef6769f | 2023-04-21 17:33:58 +0200 | [diff] [blame] | 139 | void *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 Jonker | 4d3bce4 | 2023-05-10 23:48:44 +0200 | [diff] [blame] | 144 | if (addr == FDT_ADDR_T_NONE) |
| 145 | return NULL; |
| 146 | |
| 147 | return map_sysmem(addr, 0); |
Johan Jonker | ef6769f | 2023-04-21 17:33:58 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 150 | fdt_addr_t devfdt_get_addr_name(const struct udevice *dev, const char *name) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 151 | { |
| 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 Schiffer | bc2b85d | 2023-09-27 15:33:31 +0200 | [diff] [blame] | 158 | return FDT_ADDR_T_NONE; |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 159 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 160 | return devfdt_get_addr_index(dev, index); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 161 | #else |
| 162 | return FDT_ADDR_T_NONE; |
| 163 | #endif |
| 164 | } |
| 165 | |
Matthias Schiffer | 5e90d4e | 2023-09-27 15:33:32 +0200 | [diff] [blame] | 166 | void *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 Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 176 | fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev, |
| 177 | const char *name, fdt_size_t *size) |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 178 | { |
| 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 Schiffer | bc2b85d | 2023-09-27 15:33:31 +0200 | [diff] [blame] | 185 | return FDT_ADDR_T_NONE; |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 186 | |
| 187 | return devfdt_get_addr_size_index(dev, index, size); |
| 188 | #else |
| 189 | return FDT_ADDR_T_NONE; |
| 190 | #endif |
| 191 | } |
| 192 | |
Matthias Schiffer | 5e90d4e | 2023-09-27 15:33:32 +0200 | [diff] [blame] | 193 | void *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 Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 204 | fdt_addr_t devfdt_get_addr(const struct udevice *dev) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 205 | { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 206 | return devfdt_get_addr_index(dev, 0); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 207 | } |
| 208 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 209 | void *devfdt_get_addr_ptr(const struct udevice *dev) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 210 | { |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 211 | return devfdt_get_addr_index_ptr(dev, 0); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 212 | } |
| 213 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 214 | void *devfdt_remap_addr_index(const struct udevice *dev, int index) |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 215 | { |
Cédric Le Goater | c0fb7e6 | 2018-08-14 12:48:11 +0200 | [diff] [blame] | 216 | fdt_addr_t addr = devfdt_get_addr_index(dev, index); |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 217 | |
| 218 | if (addr == FDT_ADDR_T_NONE) |
| 219 | return NULL; |
| 220 | |
| 221 | return map_physmem(addr, 0, MAP_NOCACHE); |
| 222 | } |
| 223 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 224 | void *devfdt_remap_addr_name(const struct udevice *dev, const char *name) |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 225 | { |
| 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 Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 234 | void *devfdt_remap_addr(const struct udevice *dev) |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 235 | { |
| 236 | return devfdt_remap_addr_index(dev, 0); |
| 237 | } |
| 238 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 239 | void *devfdt_map_physmem(const struct udevice *dev, unsigned long size) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 240 | { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 241 | fdt_addr_t addr = devfdt_get_addr(dev); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 242 | |
| 243 | if (addr == FDT_ADDR_T_NONE) |
| 244 | return NULL; |
| 245 | |
| 246 | return map_physmem(addr, size, MAP_NOCACHE); |
| 247 | } |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 248 | |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 249 | fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev, fdt_size_t *sizep) |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 250 | { |
| 251 | ulong addr; |
| 252 | |
| 253 | addr = devfdt_get_addr(dev); |
Simon Glass | 82278e3 | 2021-08-01 18:54:23 -0600 | [diff] [blame] | 254 | if (CONFIG_IS_ENABLED(PCI) && addr == FDT_ADDR_T_NONE) { |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 255 | struct fdt_pci_addr pci_addr; |
| 256 | u32 bar; |
| 257 | int ret; |
| 258 | |
Simon Glass | 04ec59c | 2019-09-25 08:56:41 -0600 | [diff] [blame] | 259 | ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32, |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 260 | "reg", &pci_addr, sizep); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 261 | if (ret) { |
| 262 | /* try if there is any i/o-mapped register */ |
Simon Glass | 04ec59c | 2019-09-25 08:56:41 -0600 | [diff] [blame] | 263 | ret = ofnode_read_pci_addr(dev_ofnode(dev), |
| 264 | FDT_PCI_SPACE_IO, "reg", |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 265 | &pci_addr, sizep); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 266 | 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 | } |