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 | |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
| 13 | #include <fdt_support.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Johan Jonker | 8664860 | 2023-04-23 11:19:41 +0200 | [diff] [blame^] | 15 | #include <mapmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <dm/device-internal.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 22 | 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] | 23 | { |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 24 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 25 | int offset = dev_of_offset(dev); |
| 26 | int parent = dev_of_offset(dev->parent); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 27 | fdt_addr_t addr; |
| 28 | |
| 29 | if (CONFIG_IS_ENABLED(OF_TRANSLATE)) { |
| 30 | const fdt32_t *reg; |
| 31 | int len = 0; |
| 32 | int na, ns; |
| 33 | |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 34 | na = fdt_address_cells(gd->fdt_blob, parent); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 35 | if (na < 1) { |
| 36 | debug("bad #address-cells\n"); |
| 37 | return FDT_ADDR_T_NONE; |
| 38 | } |
| 39 | |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 40 | ns = fdt_size_cells(gd->fdt_blob, parent); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 41 | if (ns < 0) { |
| 42 | debug("bad #size-cells\n"); |
| 43 | return FDT_ADDR_T_NONE; |
| 44 | } |
| 45 | |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 46 | reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 47 | if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) { |
| 48 | debug("Req index out of range\n"); |
| 49 | return FDT_ADDR_T_NONE; |
| 50 | } |
| 51 | |
| 52 | reg += index * (na + ns); |
| 53 | |
Dario Binacchi | f8fc703 | 2021-05-01 17:05:26 +0200 | [diff] [blame] | 54 | if (ns) { |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 55 | /* |
| 56 | * Use the full-fledged translate function for complex |
| 57 | * bus setups. |
| 58 | */ |
| 59 | addr = fdt_translate_address((void *)gd->fdt_blob, |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 60 | offset, reg); |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 61 | } else { |
| 62 | /* Non translatable if #size-cells == 0 */ |
| 63 | addr = fdt_read_number(reg, na); |
| 64 | } |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 65 | } else { |
| 66 | /* |
| 67 | * Use the "simple" translate function for less complex |
| 68 | * bus setups. |
| 69 | */ |
Simon Glass | cb12c9f | 2022-09-06 20:27:12 -0600 | [diff] [blame] | 70 | addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob, parent, |
| 71 | offset, "reg", index, |
| 72 | NULL, false); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 73 | if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) { |
| 74 | if (device_get_uclass_id(dev->parent) == |
| 75 | UCLASS_SIMPLE_BUS) |
| 76 | addr = simple_bus_translate(dev->parent, addr); |
| 77 | } |
| 78 | } |
| 79 | |
Stefan Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 80 | #if defined(CONFIG_TRANSLATION_OFFSET) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 81 | /* |
| 82 | * Some platforms need a special address translation. Those |
| 83 | * platforms (e.g. mvebu in SPL) can configure a translation |
Stefan Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 84 | * offset by setting this value in the GD and enaling this |
| 85 | * feature via CONFIG_TRANSLATION_OFFSET. This value will |
| 86 | * get added to all addresses returned by devfdt_get_addr(). |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 87 | */ |
Stefan Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 88 | addr += gd->translation_offset; |
| 89 | #endif |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 90 | |
| 91 | return addr; |
| 92 | #else |
| 93 | return FDT_ADDR_T_NONE; |
| 94 | #endif |
| 95 | } |
| 96 | |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 97 | void *devfdt_get_addr_index_ptr(const struct udevice *dev, int index) |
| 98 | { |
| 99 | fdt_addr_t addr = devfdt_get_addr_index(dev, index); |
| 100 | |
Johan Jonker | 8664860 | 2023-04-23 11:19:41 +0200 | [diff] [blame^] | 101 | if (addr == FDT_ADDR_T_NONE) |
| 102 | return NULL; |
| 103 | |
| 104 | return map_sysmem(addr, 0); |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 107 | fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index, |
| 108 | fdt_size_t *size) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 109 | { |
| 110 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 111 | /* |
| 112 | * Only get the size in this first call. We'll get the addr in the |
| 113 | * next call to the exisiting dev_get_xxx function which handles |
| 114 | * all config options. |
| 115 | */ |
| 116 | fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev), |
| 117 | "reg", index, size, false); |
| 118 | |
| 119 | /* |
| 120 | * Get the base address via the existing function which handles |
| 121 | * all Kconfig cases |
| 122 | */ |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 123 | return devfdt_get_addr_index(dev, index); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 124 | #else |
| 125 | return FDT_ADDR_T_NONE; |
| 126 | #endif |
| 127 | } |
| 128 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 129 | 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] | 130 | { |
| 131 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 132 | int index; |
| 133 | |
| 134 | index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev), |
| 135 | "reg-names", name); |
| 136 | if (index < 0) |
| 137 | return index; |
| 138 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 139 | return devfdt_get_addr_index(dev, index); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 140 | #else |
| 141 | return FDT_ADDR_T_NONE; |
| 142 | #endif |
| 143 | } |
| 144 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 145 | fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev, |
| 146 | const char *name, fdt_size_t *size) |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 147 | { |
| 148 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 149 | int index; |
| 150 | |
| 151 | index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev), |
| 152 | "reg-names", name); |
| 153 | if (index < 0) |
| 154 | return index; |
| 155 | |
| 156 | return devfdt_get_addr_size_index(dev, index, size); |
| 157 | #else |
| 158 | return FDT_ADDR_T_NONE; |
| 159 | #endif |
| 160 | } |
| 161 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 162 | fdt_addr_t devfdt_get_addr(const struct udevice *dev) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 163 | { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 164 | return devfdt_get_addr_index(dev, 0); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 165 | } |
| 166 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 167 | void *devfdt_get_addr_ptr(const struct udevice *dev) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 168 | { |
Bin Meng | a44397f | 2021-09-12 11:15:12 +0800 | [diff] [blame] | 169 | return devfdt_get_addr_index_ptr(dev, 0); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 170 | } |
| 171 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 172 | 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] | 173 | { |
Cédric Le Goater | c0fb7e6 | 2018-08-14 12:48:11 +0200 | [diff] [blame] | 174 | fdt_addr_t addr = devfdt_get_addr_index(dev, index); |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 175 | |
| 176 | if (addr == FDT_ADDR_T_NONE) |
| 177 | return NULL; |
| 178 | |
| 179 | return map_physmem(addr, 0, MAP_NOCACHE); |
| 180 | } |
| 181 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 182 | 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] | 183 | { |
| 184 | fdt_addr_t addr = devfdt_get_addr_name(dev, name); |
| 185 | |
| 186 | if (addr == FDT_ADDR_T_NONE) |
| 187 | return NULL; |
| 188 | |
| 189 | return map_physmem(addr, 0, MAP_NOCACHE); |
| 190 | } |
| 191 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 192 | void *devfdt_remap_addr(const struct udevice *dev) |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 193 | { |
| 194 | return devfdt_remap_addr_index(dev, 0); |
| 195 | } |
| 196 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 197 | void *devfdt_map_physmem(const struct udevice *dev, unsigned long size) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 198 | { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 199 | fdt_addr_t addr = devfdt_get_addr(dev); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 200 | |
| 201 | if (addr == FDT_ADDR_T_NONE) |
| 202 | return NULL; |
| 203 | |
| 204 | return map_physmem(addr, size, MAP_NOCACHE); |
| 205 | } |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 206 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 207 | fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev) |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 208 | { |
| 209 | ulong addr; |
| 210 | |
| 211 | addr = devfdt_get_addr(dev); |
Simon Glass | 82278e3 | 2021-08-01 18:54:23 -0600 | [diff] [blame] | 212 | if (CONFIG_IS_ENABLED(PCI) && addr == FDT_ADDR_T_NONE) { |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 213 | struct fdt_pci_addr pci_addr; |
| 214 | u32 bar; |
| 215 | int ret; |
| 216 | |
Simon Glass | 04ec59c | 2019-09-25 08:56:41 -0600 | [diff] [blame] | 217 | ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32, |
| 218 | "reg", &pci_addr); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 219 | if (ret) { |
| 220 | /* try if there is any i/o-mapped register */ |
Simon Glass | 04ec59c | 2019-09-25 08:56:41 -0600 | [diff] [blame] | 221 | ret = ofnode_read_pci_addr(dev_ofnode(dev), |
| 222 | FDT_PCI_SPACE_IO, "reg", |
| 223 | &pci_addr); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 224 | if (ret) |
| 225 | return FDT_ADDR_T_NONE; |
| 226 | } |
| 227 | ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar); |
| 228 | if (ret) |
| 229 | return FDT_ADDR_T_NONE; |
| 230 | addr = bar; |
| 231 | } |
| 232 | |
| 233 | return addr; |
| 234 | } |