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> |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 20 | 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] | 21 | { |
| 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 Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 52 | 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 Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 63 | } 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 Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 78 | #if defined(CONFIG_TRANSLATION_OFFSET) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 79 | /* |
| 80 | * Some platforms need a special address translation. Those |
| 81 | * platforms (e.g. mvebu in SPL) can configure a translation |
Stefan Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 82 | * 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 Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 85 | */ |
Stefan Roese | 85bddff | 2019-04-12 16:42:28 +0200 | [diff] [blame] | 86 | addr += gd->translation_offset; |
| 87 | #endif |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 88 | |
| 89 | return addr; |
| 90 | #else |
| 91 | return FDT_ADDR_T_NONE; |
| 92 | #endif |
| 93 | } |
| 94 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 95 | fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index, |
| 96 | fdt_size_t *size) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 97 | { |
| 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 Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 111 | return devfdt_get_addr_index(dev, index); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 112 | #else |
| 113 | return FDT_ADDR_T_NONE; |
| 114 | #endif |
| 115 | } |
| 116 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 117 | 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] | 118 | { |
| 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 Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 127 | return devfdt_get_addr_index(dev, index); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 128 | #else |
| 129 | return FDT_ADDR_T_NONE; |
| 130 | #endif |
| 131 | } |
| 132 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 133 | fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev, |
| 134 | const char *name, fdt_size_t *size) |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 135 | { |
| 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 Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 150 | fdt_addr_t devfdt_get_addr(const struct udevice *dev) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 151 | { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 152 | return devfdt_get_addr_index(dev, 0); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 153 | } |
| 154 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 155 | void *devfdt_get_addr_ptr(const struct udevice *dev) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 156 | { |
Ovidiu Panait | a633f00 | 2020-08-03 22:17:35 +0300 | [diff] [blame] | 157 | fdt_addr_t addr = devfdt_get_addr_index(dev, 0); |
| 158 | |
| 159 | return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr; |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 160 | } |
| 161 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 162 | 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] | 163 | { |
Cédric Le Goater | c0fb7e6 | 2018-08-14 12:48:11 +0200 | [diff] [blame] | 164 | fdt_addr_t addr = devfdt_get_addr_index(dev, index); |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 165 | |
| 166 | if (addr == FDT_ADDR_T_NONE) |
| 167 | return NULL; |
| 168 | |
| 169 | return map_physmem(addr, 0, MAP_NOCACHE); |
| 170 | } |
| 171 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 172 | 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] | 173 | { |
| 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 Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 182 | void *devfdt_remap_addr(const struct udevice *dev) |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 183 | { |
| 184 | return devfdt_remap_addr_index(dev, 0); |
| 185 | } |
| 186 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 187 | void *devfdt_map_physmem(const struct udevice *dev, unsigned long size) |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 188 | { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 189 | fdt_addr_t addr = devfdt_get_addr(dev); |
Simon Glass | aadfaf0 | 2017-05-17 17:18:04 -0600 | [diff] [blame] | 190 | |
| 191 | if (addr == FDT_ADDR_T_NONE) |
| 192 | return NULL; |
| 193 | |
| 194 | return map_physmem(addr, size, MAP_NOCACHE); |
| 195 | } |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 196 | |
Simon Glass | c6548b8 | 2020-01-27 08:49:39 -0700 | [diff] [blame] | 197 | fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev) |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 198 | { |
| 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 Glass | 04ec59c | 2019-09-25 08:56:41 -0600 | [diff] [blame] | 208 | ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32, |
| 209 | "reg", &pci_addr); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 210 | if (ret) { |
| 211 | /* try if there is any i/o-mapped register */ |
Simon Glass | 04ec59c | 2019-09-25 08:56:41 -0600 | [diff] [blame] | 212 | ret = ofnode_read_pci_addr(dev_ofnode(dev), |
| 213 | FDT_PCI_SPACE_IO, "reg", |
| 214 | &pci_addr); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 215 | 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 | } |