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> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 21 | fdt_addr_t devfdt_get_addr_index(const struct udevice *dev, int index) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 22 | { |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 24 | int offset = dev_of_offset(dev); |
Jonas Karlman | 4dd834a | 2024-02-17 13:19:41 +0000 | [diff] [blame] | 25 | int parent = fdt_parent_offset(gd->fdt_blob, offset); |
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) { |
| 35 | debug("bad #address-cells\n"); |
| 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) { |
| 41 | debug("bad #size-cells\n"); |
| 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)))) { |
| 47 | debug("Req index out of range\n"); |
| 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; |
| 91 | #else |
| 92 | return FDT_ADDR_T_NONE; |
| 93 | #endif |
| 94 | } |
| 95 | |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 96 | void *devfdt_get_addr_index_ptr(const struct udevice *dev, int index) |
| 97 | { |
| 98 | fdt_addr_t addr = devfdt_get_addr_index(dev, index); |
| 99 | |
Johan Jonker | 8664860 | 2023-04-23 11:19:41 +0200 | [diff] [blame] | 100 | if (addr == FDT_ADDR_T_NONE) |
| 101 | return NULL; |
| 102 | |
| 103 | return map_sysmem(addr, 0); |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 104 | } |
| 105 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 106 | fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index, |
| 107 | fdt_size_t *size) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 108 | { |
| 109 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 110 | /* |
| 111 | * Only get the size in this first call. We'll get the addr in the |
| 112 | * next call to the exisiting dev_get_xxx function which handles |
| 113 | * all config options. |
| 114 | */ |
| 115 | fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev), |
| 116 | "reg", index, size, false); |
| 117 | |
| 118 | /* |
| 119 | * Get the base address via the existing function which handles |
| 120 | * all Kconfig cases |
| 121 | */ |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 122 | return devfdt_get_addr_index(dev, index); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 123 | #else |
| 124 | return FDT_ADDR_T_NONE; |
| 125 | #endif |
| 126 | } |
| 127 | |
Johan Jonker | ef6769f | 2023-04-21 17:33:58 +0200 | [diff] [blame] | 128 | void *devfdt_get_addr_size_index_ptr(const struct udevice *dev, int index, |
| 129 | fdt_size_t *size) |
| 130 | { |
| 131 | fdt_addr_t addr = devfdt_get_addr_size_index(dev, index, size); |
| 132 | |
Johan Jonker | 4d3bce4 | 2023-05-10 23:48:44 +0200 | [diff] [blame] | 133 | if (addr == FDT_ADDR_T_NONE) |
| 134 | return NULL; |
| 135 | |
| 136 | return map_sysmem(addr, 0); |
Johan Jonker | ef6769f | 2023-04-21 17:33:58 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 139 | 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] | 140 | { |
| 141 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 142 | int index; |
| 143 | |
| 144 | index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev), |
| 145 | "reg-names", name); |
| 146 | if (index < 0) |
Matthias Schiffer | bc2b85d | 2023-09-27 15:33:31 +0200 | [diff] [blame] | 147 | return FDT_ADDR_T_NONE; |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 148 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 149 | return devfdt_get_addr_index(dev, index); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 150 | #else |
| 151 | return FDT_ADDR_T_NONE; |
| 152 | #endif |
| 153 | } |
| 154 | |
Matthias Schiffer | 5e90d4e | 2023-09-27 15:33:32 +0200 | [diff] [blame] | 155 | void *devfdt_get_addr_name_ptr(const struct udevice *dev, const char *name) |
| 156 | { |
| 157 | fdt_addr_t addr = devfdt_get_addr_name(dev, name); |
| 158 | |
| 159 | if (addr == FDT_ADDR_T_NONE) |
| 160 | return NULL; |
| 161 | |
| 162 | return map_sysmem(addr, 0); |
| 163 | } |
| 164 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 165 | fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev, |
| 166 | const char *name, fdt_size_t *size) |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 167 | { |
| 168 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 169 | int index; |
| 170 | |
| 171 | index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev), |
| 172 | "reg-names", name); |
| 173 | if (index < 0) |
Matthias Schiffer | bc2b85d | 2023-09-27 15:33:31 +0200 | [diff] [blame] | 174 | return FDT_ADDR_T_NONE; |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 175 | |
| 176 | return devfdt_get_addr_size_index(dev, index, size); |
| 177 | #else |
| 178 | return FDT_ADDR_T_NONE; |
| 179 | #endif |
| 180 | } |
| 181 | |
Matthias Schiffer | 5e90d4e | 2023-09-27 15:33:32 +0200 | [diff] [blame] | 182 | void *devfdt_get_addr_size_name_ptr(const struct udevice *dev, |
| 183 | const char *name, fdt_size_t *size) |
| 184 | { |
| 185 | fdt_addr_t addr = devfdt_get_addr_size_name(dev, name, size); |
| 186 | |
| 187 | if (addr == FDT_ADDR_T_NONE) |
| 188 | return NULL; |
| 189 | |
| 190 | return map_sysmem(addr, 0); |
| 191 | } |
| 192 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 193 | fdt_addr_t devfdt_get_addr(const struct udevice *dev) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 194 | { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 195 | return devfdt_get_addr_index(dev, 0); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 196 | } |
| 197 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 198 | void *devfdt_get_addr_ptr(const struct udevice *dev) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 199 | { |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 200 | return devfdt_get_addr_index_ptr(dev, 0); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 203 | 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] | 204 | { |
Cédric Le Goater | c0fb7e6 | 2018-08-14 12:48:11 +0200 | [diff] [blame] | 205 | fdt_addr_t addr = devfdt_get_addr_index(dev, index); |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 206 | |
| 207 | if (addr == FDT_ADDR_T_NONE) |
| 208 | return NULL; |
| 209 | |
| 210 | return map_physmem(addr, 0, MAP_NOCACHE); |
| 211 | } |
| 212 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 213 | 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] | 214 | { |
| 215 | fdt_addr_t addr = devfdt_get_addr_name(dev, name); |
| 216 | |
| 217 | if (addr == FDT_ADDR_T_NONE) |
| 218 | return NULL; |
| 219 | |
| 220 | return map_physmem(addr, 0, MAP_NOCACHE); |
| 221 | } |
| 222 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 223 | void *devfdt_remap_addr(const struct udevice *dev) |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 224 | { |
| 225 | return devfdt_remap_addr_index(dev, 0); |
| 226 | } |
| 227 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 228 | void *devfdt_map_physmem(const struct udevice *dev, unsigned long size) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 229 | { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 230 | fdt_addr_t addr = devfdt_get_addr(dev); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 231 | |
| 232 | if (addr == FDT_ADDR_T_NONE) |
| 233 | return NULL; |
| 234 | |
| 235 | return map_physmem(addr, size, MAP_NOCACHE); |
| 236 | } |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 237 | |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 238 | 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] | 239 | { |
| 240 | ulong addr; |
| 241 | |
| 242 | addr = devfdt_get_addr(dev); |
Simon Glass | 82278e3 | 2021-08-01 18:54:23 -0600 | [diff] [blame] | 243 | if (CONFIG_IS_ENABLED(PCI) && addr == FDT_ADDR_T_NONE) { |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 244 | struct fdt_pci_addr pci_addr; |
| 245 | u32 bar; |
| 246 | int ret; |
| 247 | |
Simon Glass | 04ec59c | 2019-09-25 08:56:41 -0600 | [diff] [blame] | 248 | ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32, |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 249 | "reg", &pci_addr, sizep); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 250 | if (ret) { |
| 251 | /* try if there is any i/o-mapped register */ |
Simon Glass | 04ec59c | 2019-09-25 08:56:41 -0600 | [diff] [blame] | 252 | ret = ofnode_read_pci_addr(dev_ofnode(dev), |
| 253 | FDT_PCI_SPACE_IO, "reg", |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 254 | &pci_addr, sizep); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 255 | if (ret) |
| 256 | return FDT_ADDR_T_NONE; |
| 257 | } |
| 258 | ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar); |
| 259 | if (ret) |
| 260 | return FDT_ADDR_T_NONE; |
| 261 | addr = bar; |
| 262 | } |
| 263 | |
| 264 | return addr; |
| 265 | } |