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