Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6fe43a3 | 2017-05-18 20:08:59 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Taken from Linux v4.9 drivers/of/address.c |
| 4 | * |
| 5 | * Modified for U-Boot |
| 6 | * Copyright (c) 2017 Google, Inc |
Simon Glass | 6fe43a3 | 2017-05-18 20:08:59 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 11 | #include <linux/bug.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 12 | #include <linux/libfdt.h> |
Simon Glass | 6fe43a3 | 2017-05-18 20:08:59 -0600 | [diff] [blame] | 13 | #include <dm/of_access.h> |
| 14 | #include <dm/of_addr.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/ioport.h> |
| 17 | |
| 18 | /* Max address size we deal with */ |
| 19 | #define OF_MAX_ADDR_CELLS 4 |
| 20 | #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS) |
| 21 | #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0) |
| 22 | |
| 23 | static struct of_bus *of_match_bus(struct device_node *np); |
| 24 | |
| 25 | /* Debug utility */ |
| 26 | #ifdef DEBUG |
| 27 | static void of_dump_addr(const char *s, const __be32 *addr, int na) |
| 28 | { |
| 29 | debug("%s", s); |
| 30 | while (na--) |
| 31 | pr_cont(" %08x", be32_to_cpu(*(addr++))); |
| 32 | pr_cont("\n"); |
| 33 | } |
| 34 | #else |
| 35 | static void of_dump_addr(const char *s, const __be32 *addr, int na) { } |
| 36 | #endif |
| 37 | |
| 38 | /* Callbacks for bus specific translators */ |
| 39 | struct of_bus { |
| 40 | const char *name; |
| 41 | const char *addresses; |
| 42 | int (*match)(struct device_node *parent); |
| 43 | void (*count_cells)(const struct device_node *child, int *addrc, |
| 44 | int *sizec); |
| 45 | u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna); |
| 46 | int (*translate)(__be32 *addr, u64 offset, int na); |
| 47 | unsigned int (*get_flags)(const __be32 *addr); |
| 48 | }; |
| 49 | |
| 50 | static void of_bus_default_count_cells(const struct device_node *np, |
| 51 | int *addrc, int *sizec) |
| 52 | { |
| 53 | if (addrc) |
| 54 | *addrc = of_n_addr_cells(np); |
| 55 | if (sizec) |
| 56 | *sizec = of_n_size_cells(np); |
| 57 | } |
| 58 | |
| 59 | static u64 of_bus_default_map(__be32 *addr, const __be32 *range, |
| 60 | int na, int ns, int pna) |
| 61 | { |
| 62 | u64 cp, s, da; |
| 63 | |
| 64 | cp = of_read_number(range, na); |
| 65 | s = of_read_number(range + na + pna, ns); |
| 66 | da = of_read_number(addr, na); |
| 67 | |
| 68 | debug("default map, cp=%llx, s=%llx, da=%llx\n", |
| 69 | (unsigned long long)cp, (unsigned long long)s, |
| 70 | (unsigned long long)da); |
| 71 | |
| 72 | if (da < cp || da >= (cp + s)) |
| 73 | return OF_BAD_ADDR; |
| 74 | return da - cp; |
| 75 | } |
| 76 | |
| 77 | static int of_bus_default_translate(__be32 *addr, u64 offset, int na) |
| 78 | { |
| 79 | u64 a = of_read_number(addr, na); |
| 80 | memset(addr, 0, na * 4); |
| 81 | a += offset; |
| 82 | if (na > 1) |
| 83 | addr[na - 2] = cpu_to_be32(a >> 32); |
| 84 | addr[na - 1] = cpu_to_be32(a & 0xffffffffu); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static unsigned int of_bus_default_get_flags(const __be32 *addr) |
| 90 | { |
| 91 | return IORESOURCE_MEM; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Array of bus-specific translators |
| 96 | */ |
| 97 | static struct of_bus of_busses[] = { |
| 98 | /* Default */ |
| 99 | { |
| 100 | .name = "default", |
| 101 | .addresses = "reg", |
| 102 | .match = NULL, |
| 103 | .count_cells = of_bus_default_count_cells, |
| 104 | .map = of_bus_default_map, |
| 105 | .translate = of_bus_default_translate, |
| 106 | .get_flags = of_bus_default_get_flags, |
| 107 | }, |
| 108 | }; |
| 109 | |
| 110 | static struct of_bus *of_match_bus(struct device_node *np) |
| 111 | { |
| 112 | int i; |
| 113 | |
| 114 | for (i = 0; i < ARRAY_SIZE(of_busses); i++) |
| 115 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 116 | return &of_busses[i]; |
| 117 | BUG(); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | static void dev_count_cells(const struct device_node *np, int *nap, int *nsp) |
| 122 | { |
| 123 | of_bus_default_count_cells(np, nap, nsp); |
| 124 | } |
| 125 | |
| 126 | const __be32 *of_get_address(const struct device_node *dev, int index, |
| 127 | u64 *size, unsigned int *flags) |
| 128 | { |
| 129 | const __be32 *prop; |
| 130 | int psize; |
| 131 | struct device_node *parent; |
| 132 | struct of_bus *bus; |
| 133 | int onesize, i, na, ns; |
| 134 | |
| 135 | /* Get parent & match bus type */ |
| 136 | parent = of_get_parent(dev); |
| 137 | if (parent == NULL) |
| 138 | return NULL; |
| 139 | dev_count_cells(dev, &na, &ns); |
| 140 | bus = of_match_bus(parent); |
| 141 | bus->count_cells(dev, &na, &ns); |
| 142 | of_node_put(parent); |
| 143 | if (!OF_CHECK_ADDR_COUNT(na)) |
| 144 | return NULL; |
| 145 | |
| 146 | /* Get "reg" or "assigned-addresses" property */ |
| 147 | prop = of_get_property(dev, "reg", &psize); |
| 148 | if (prop == NULL) |
| 149 | return NULL; |
| 150 | psize /= 4; |
| 151 | |
| 152 | onesize = na + ns; |
| 153 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) |
| 154 | if (i == index) { |
| 155 | if (size) |
| 156 | *size = of_read_number(prop + na, ns); |
| 157 | if (flags) |
| 158 | *flags = bus->get_flags(prop); |
| 159 | return prop; |
| 160 | } |
| 161 | return NULL; |
| 162 | } |
| 163 | EXPORT_SYMBOL(of_get_address); |
| 164 | |
| 165 | static int of_empty_ranges_quirk(const struct device_node *np) |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | static int of_translate_one(const struct device_node *parent, |
| 171 | struct of_bus *bus, struct of_bus *pbus, |
| 172 | __be32 *addr, int na, int ns, int pna, |
| 173 | const char *rprop) |
| 174 | { |
| 175 | const __be32 *ranges; |
| 176 | int rlen; |
| 177 | int rone; |
| 178 | u64 offset = OF_BAD_ADDR; |
| 179 | |
| 180 | /* |
| 181 | * Normally, an absence of a "ranges" property means we are |
| 182 | * crossing a non-translatable boundary, and thus the addresses |
| 183 | * below the current cannot be converted to CPU physical ones. |
| 184 | * Unfortunately, while this is very clear in the spec, it's not |
| 185 | * what Apple understood, and they do have things like /uni-n or |
| 186 | * /ht nodes with no "ranges" property and a lot of perfectly |
| 187 | * useable mapped devices below them. Thus we treat the absence of |
| 188 | * "ranges" as equivalent to an empty "ranges" property which means |
| 189 | * a 1:1 translation at that level. It's up to the caller not to try |
| 190 | * to translate addresses that aren't supposed to be translated in |
| 191 | * the first place. --BenH. |
| 192 | * |
| 193 | * As far as we know, this damage only exists on Apple machines, so |
| 194 | * This code is only enabled on powerpc. --gcl |
| 195 | */ |
| 196 | ranges = of_get_property(parent, rprop, &rlen); |
| 197 | if (ranges == NULL && !of_empty_ranges_quirk(parent)) { |
| 198 | debug("no ranges; cannot translate\n"); |
| 199 | return 1; |
| 200 | } |
| 201 | if (ranges == NULL || rlen == 0) { |
| 202 | offset = of_read_number(addr, na); |
| 203 | memset(addr, 0, pna * 4); |
| 204 | debug("empty ranges; 1:1 translation\n"); |
| 205 | goto finish; |
| 206 | } |
| 207 | |
| 208 | debug("walking ranges...\n"); |
| 209 | |
| 210 | /* Now walk through the ranges */ |
| 211 | rlen /= 4; |
| 212 | rone = na + pna + ns; |
| 213 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
| 214 | offset = bus->map(addr, ranges, na, ns, pna); |
| 215 | if (offset != OF_BAD_ADDR) |
| 216 | break; |
| 217 | } |
| 218 | if (offset == OF_BAD_ADDR) { |
| 219 | debug("not found !\n"); |
| 220 | return 1; |
| 221 | } |
| 222 | memcpy(addr, ranges + na, 4 * pna); |
| 223 | |
| 224 | finish: |
| 225 | of_dump_addr("parent translation for:", addr, pna); |
| 226 | debug("with offset: %llx\n", (unsigned long long)offset); |
| 227 | |
| 228 | /* Translate it into parent bus space */ |
| 229 | return pbus->translate(addr, offset, pna); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Translate an address from the device-tree into a CPU physical address, |
| 234 | * this walks up the tree and applies the various bus mappings on the |
| 235 | * way. |
| 236 | * |
| 237 | * Note: We consider that crossing any level with #size-cells == 0 to mean |
| 238 | * that translation is impossible (that is we are not dealing with a value |
| 239 | * that can be mapped to a cpu physical address). This is not really specified |
| 240 | * that way, but this is traditionally the way IBM at least do things |
| 241 | */ |
| 242 | static u64 __of_translate_address(const struct device_node *dev, |
| 243 | const __be32 *in_addr, const char *rprop) |
| 244 | { |
| 245 | struct device_node *parent = NULL; |
| 246 | struct of_bus *bus, *pbus; |
| 247 | __be32 addr[OF_MAX_ADDR_CELLS]; |
| 248 | int na, ns, pna, pns; |
| 249 | u64 result = OF_BAD_ADDR; |
| 250 | |
| 251 | debug("** translation for device %s **\n", of_node_full_name(dev)); |
| 252 | |
| 253 | /* Increase refcount at current level */ |
| 254 | (void)of_node_get(dev); |
| 255 | |
| 256 | /* Get parent & match bus type */ |
| 257 | parent = of_get_parent(dev); |
| 258 | if (parent == NULL) |
| 259 | goto bail; |
| 260 | bus = of_match_bus(parent); |
| 261 | |
| 262 | /* Count address cells & copy address locally */ |
| 263 | bus->count_cells(dev, &na, &ns); |
| 264 | if (!OF_CHECK_COUNTS(na, ns)) { |
| 265 | debug("Bad cell count for %s\n", of_node_full_name(dev)); |
| 266 | goto bail; |
| 267 | } |
| 268 | memcpy(addr, in_addr, na * 4); |
| 269 | |
| 270 | debug("bus is %s (na=%d, ns=%d) on %s\n", bus->name, na, ns, |
| 271 | of_node_full_name(parent)); |
| 272 | of_dump_addr("translating address:", addr, na); |
| 273 | |
| 274 | /* Translate */ |
| 275 | for (;;) { |
| 276 | /* Switch to parent bus */ |
| 277 | of_node_put(dev); |
| 278 | dev = parent; |
| 279 | parent = of_get_parent(dev); |
| 280 | |
| 281 | /* If root, we have finished */ |
| 282 | if (parent == NULL) { |
| 283 | debug("reached root node\n"); |
| 284 | result = of_read_number(addr, na); |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | /* Get new parent bus and counts */ |
| 289 | pbus = of_match_bus(parent); |
| 290 | pbus->count_cells(dev, &pna, &pns); |
| 291 | if (!OF_CHECK_COUNTS(pna, pns)) { |
| 292 | debug("Bad cell count for %s\n", |
| 293 | of_node_full_name(dev)); |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | debug("parent bus is %s (na=%d, ns=%d) on %s\n", pbus->name, |
| 298 | pna, pns, of_node_full_name(parent)); |
| 299 | |
| 300 | /* Apply bus translation */ |
| 301 | if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) |
| 302 | break; |
| 303 | |
| 304 | /* Complete the move up one level */ |
| 305 | na = pna; |
| 306 | ns = pns; |
| 307 | bus = pbus; |
| 308 | |
| 309 | of_dump_addr("one level translation:", addr, na); |
| 310 | } |
| 311 | bail: |
| 312 | of_node_put(parent); |
| 313 | of_node_put(dev); |
| 314 | |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | u64 of_translate_address(const struct device_node *dev, const __be32 *in_addr) |
| 319 | { |
| 320 | return __of_translate_address(dev, in_addr, "ranges"); |
| 321 | } |
| 322 | |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 323 | u64 of_translate_dma_address(const struct device_node *dev, const __be32 *in_addr) |
| 324 | { |
| 325 | return __of_translate_address(dev, in_addr, "dma-ranges"); |
| 326 | } |
Simon Glass | 6fe43a3 | 2017-05-18 20:08:59 -0600 | [diff] [blame] | 327 | |
| 328 | static int __of_address_to_resource(const struct device_node *dev, |
| 329 | const __be32 *addrp, u64 size, unsigned int flags, |
| 330 | const char *name, struct resource *r) |
| 331 | { |
| 332 | u64 taddr; |
| 333 | |
| 334 | if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0) |
| 335 | return -EINVAL; |
| 336 | taddr = of_translate_address(dev, addrp); |
| 337 | if (taddr == OF_BAD_ADDR) |
| 338 | return -EINVAL; |
| 339 | memset(r, 0, sizeof(struct resource)); |
| 340 | r->start = taddr; |
| 341 | r->end = taddr + size - 1; |
| 342 | r->flags = flags; |
| 343 | r->name = name ? name : dev->full_name; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int of_address_to_resource(const struct device_node *dev, int index, |
| 349 | struct resource *r) |
| 350 | { |
| 351 | const __be32 *addrp; |
| 352 | u64 size; |
| 353 | unsigned int flags; |
| 354 | const char *name = NULL; |
| 355 | |
| 356 | addrp = of_get_address(dev, index, &size, &flags); |
| 357 | if (addrp == NULL) |
| 358 | return -EINVAL; |
| 359 | |
| 360 | /* Get optional "reg-names" property to add a name to a resource */ |
| 361 | of_property_read_string_index(dev, "reg-names", index, &name); |
| 362 | |
| 363 | return __of_address_to_resource(dev, addrp, size, flags, name, r); |
| 364 | } |