| 1 | |
| 2 | #define pr_fmt(fmt) "OF: " fmt
|
| 3 |
|
| 4 | #include <linux/device.h>
|
| 5 | #include <linux/fwnode.h>
|
| 6 | #include <linux/io.h>
|
| 7 | #include <linux/ioport.h>
|
| 8 | #include <linux/logic_pio.h>
|
| 9 | #include <linux/module.h>
|
| 10 | #include <linux/of_address.h>
|
| 11 | #include <linux/overflow.h>
|
| 12 | #include <linux/pci.h>
|
| 13 | #include <linux/pci_regs.h>
|
| 14 | #include <linux/sizes.h>
|
| 15 | #include <linux/slab.h>
|
| 16 | #include <linux/string.h>
|
| 17 | #include <linux/dma-direct.h>
|
| 18 |
|
| 19 | #include <kunit/visibility.h>
|
| 20 |
|
| 21 |
|
| 22 | |
| 23 |
|
| 24 | #include "of_private.h"
|
| 25 |
|
| 26 |
|
| 27 | struct of_bus {
|
| 28 | const char *name;
|
| 29 | const char *addresses;
|
| 30 | int (*match)(struct device_node *parent);
|
| 31 | void (*count_cells)(struct device_node *child,
|
| 32 | int *addrc, int *sizec);
|
| 33 | u64 (*map)(__be32 *addr, const __be32 *range,
|
| 34 | int na, int ns, int pna, int fna);
|
| 35 | int (*translate)(__be32 *addr, u64 offset, int na);
|
| 36 | int flag_cells;
|
| 37 | unsigned int (*get_flags)(const __be32 *addr);
|
| 38 | };
|
| 39 |
|
| 40 | |
| 41 | * Default translator (generic bus)
|
| 42 |
|
| 43 |
|
| 44 | static void of_bus_default_count_cells(struct device_node *dev,
|
| 45 | int *addrc, int *sizec)
|
| 46 | {
|
| 47 | if (addrc)
|
| 48 | *addrc = of_n_addr_cells(dev);
|
| 49 | if (sizec)
|
| 50 | *sizec = of_n_size_cells(dev);
|
| 51 | }
|
| 52 |
|
| 53 | static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
|
| 54 | int na, int ns, int pna, int fna)
|
| 55 | {
|
| 56 | u64 cp, s, da;
|
| 57 |
|
| 58 | cp = of_read_number(range + fna, na - fna);
|
| 59 | s = of_read_number(range + na + pna, ns);
|
| 60 | da = of_read_number(addr + fna, na - fna);
|
| 61 |
|
| 62 | pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
|
| 63 |
|
| 64 | if (da < cp || da >= (cp + s))
|
| 65 | return OF_BAD_ADDR;
|
| 66 | return da - cp;
|
| 67 | }
|
| 68 |
|
| 69 | static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
|
| 70 | {
|
| 71 | u64 a = of_read_number(addr, na);
|
| 72 | memset(addr, 0, na * 4);
|
| 73 | a += offset;
|
| 74 | if (na > 1)
|
| 75 | addr[na - 2] = cpu_to_be32(a >> 32);
|
| 76 | addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
|
| 77 |
|
| 78 | return 0;
|
| 79 | }
|
| 80 |
|
| 81 | static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)
|
| 82 | {
|
| 83 | return of_read_number(addr, 1);
|
| 84 | }
|
| 85 |
|
| 86 | static unsigned int of_bus_default_get_flags(const __be32 *addr)
|
| 87 | {
|
| 88 | return IORESOURCE_MEM;
|
| 89 | }
|
| 90 |
|
| 91 | static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,
|
| 92 | int ns, int pna, int fna)
|
| 93 | {
|
| 94 |
|
| 95 | if (*addr != *range)
|
| 96 | return OF_BAD_ADDR;
|
| 97 |
|
| 98 | return of_bus_default_map(addr, range, na, ns, pna, fna);
|
| 99 | }
|
| 100 |
|
| 101 | static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)
|
| 102 | {
|
| 103 |
|
| 104 | return of_bus_default_translate(addr + 1, offset, na - 1);
|
| 105 | }
|
| 106 |
|
| 107 | #ifdef CONFIG_PCI
|
| 108 | static unsigned int of_bus_pci_get_flags(const __be32 *addr)
|
| 109 | {
|
| 110 | unsigned int flags = 0;
|
| 111 | u32 w = be32_to_cpup(addr);
|
| 112 |
|
| 113 | if (!IS_ENABLED(CONFIG_PCI))
|
| 114 | return 0;
|
| 115 |
|
| 116 | switch((w >> 24) & 0x03) {
|
| 117 | case 0x01:
|
| 118 | flags |= IORESOURCE_IO;
|
| 119 | break;
|
| 120 | case 0x02:
|
| 121 | flags |= IORESOURCE_MEM;
|
| 122 | break;
|
| 123 |
|
| 124 | case 0x03:
|
| 125 | flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
|
| 126 | break;
|
| 127 | }
|
| 128 | if (w & 0x40000000)
|
| 129 | flags |= IORESOURCE_PREFETCH;
|
| 130 | return flags;
|
| 131 | }
|
| 132 |
|
| 133 | |
| 134 | * PCI bus specific translator
|
| 135 |
|
| 136 |
|
| 137 | static bool of_node_is_pcie(const struct device_node *np)
|
| 138 | {
|
| 139 | bool is_pcie = of_node_name_eq(np, "pcie");
|
| 140 |
|
| 141 | if (is_pcie)
|
| 142 | pr_warn_once("%pOF: Missing device_type\n", np);
|
| 143 |
|
| 144 | return is_pcie;
|
| 145 | }
|
| 146 |
|
| 147 | static int of_bus_pci_match(struct device_node *np)
|
| 148 | {
|
| 149 | |
| 150 | * "pciex" is PCI Express
|
| 151 | * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
|
| 152 | * "ht" is hypertransport
|
| 153 | *
|
| 154 | * If none of the device_type match, and that the node name is
|
| 155 | * "pcie", accept the device as PCI (with a warning).
|
| 156 |
|
| 157 | return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
|
| 158 | of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
|
| 159 | of_node_is_pcie(np);
|
| 160 | }
|
| 161 |
|
| 162 | static void of_bus_pci_count_cells(struct device_node *np,
|
| 163 | int *addrc, int *sizec)
|
| 164 | {
|
| 165 | if (addrc)
|
| 166 | *addrc = 3;
|
| 167 | if (sizec)
|
| 168 | *sizec = 2;
|
| 169 | }
|
| 170 |
|
| 171 | static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
|
| 172 | int pna, int fna)
|
| 173 | {
|
| 174 | unsigned int af, rf;
|
| 175 |
|
| 176 | af = of_bus_pci_get_flags(addr);
|
| 177 | rf = of_bus_pci_get_flags(range);
|
| 178 |
|
| 179 |
|
| 180 | if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
|
| 181 | return OF_BAD_ADDR;
|
| 182 |
|
| 183 | return of_bus_default_map(addr, range, na, ns, pna, fna);
|
| 184 | }
|
| 185 |
|
| 186 | #endif
|
| 187 |
|
| 188 | VISIBLE_IF_KUNIT int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
|
| 189 | {
|
| 190 | if (overflows_type(start, r->start))
|
| 191 | return -EOVERFLOW;
|
| 192 |
|
| 193 | r->start = start;
|
| 194 |
|
| 195 | if (!size)
|
| 196 | r->end = wrapping_sub(typeof(r->end), r->start, 1);
|
| 197 | else if (size && check_add_overflow(r->start, size - 1, &r->end))
|
| 198 | return -EOVERFLOW;
|
| 199 |
|
| 200 | return 0;
|
| 201 | }
|
| 202 | EXPORT_SYMBOL_IF_KUNIT(__of_address_resource_bounds);
|
| 203 |
|
| 204 | |
| 205 | * of_pci_range_to_resource - Create a resource from an of_pci_range
|
| 206 | * @range: the PCI range that describes the resource
|
| 207 | * @np: device node where the range belongs to
|
| 208 | * @res: pointer to a valid resource that will be updated to
|
| 209 | * reflect the values contained in the range.
|
| 210 | *
|
| 211 | * Returns -EINVAL if the range cannot be converted to resource.
|
| 212 | *
|
| 213 | * Note that if the range is an IO range, the resource will be converted
|
| 214 | * using pci_address_to_pio() which can fail if it is called too early or
|
| 215 | * if the range cannot be matched to any host bridge IO space (our case here).
|
| 216 | * To guard against that we try to register the IO range first.
|
| 217 | * If that fails we know that pci_address_to_pio() will do too.
|
| 218 |
|
| 219 | int of_pci_range_to_resource(const struct of_pci_range *range,
|
| 220 | const struct device_node *np, struct resource *res)
|
| 221 | {
|
| 222 | u64 start;
|
| 223 | int err;
|
| 224 | res->flags = range->flags;
|
| 225 | res->parent = res->child = res->sibling = NULL;
|
| 226 | res->name = np->full_name;
|
| 227 |
|
| 228 | if (res->flags & IORESOURCE_IO) {
|
| 229 | unsigned long port;
|
| 230 | err = pci_register_io_range(&np->fwnode, range->cpu_addr,
|
| 231 | range->size);
|
| 232 | if (err)
|
| 233 | goto invalid_range;
|
| 234 | port = pci_address_to_pio(range->cpu_addr);
|
| 235 | if (port == (unsigned long)-1) {
|
| 236 | err = -EINVAL;
|
| 237 | goto invalid_range;
|
| 238 | }
|
| 239 | start = port;
|
| 240 | } else {
|
| 241 | start = range->cpu_addr;
|
| 242 | }
|
| 243 | return __of_address_resource_bounds(res, start, range->size);
|
| 244 |
|
| 245 | invalid_range:
|
| 246 | res->start = (resource_size_t)OF_BAD_ADDR;
|
| 247 | res->end = (resource_size_t)OF_BAD_ADDR;
|
| 248 | return err;
|
| 249 | }
|
| 250 | EXPORT_SYMBOL(of_pci_range_to_resource);
|
| 251 |
|
| 252 | |
| 253 | * of_range_to_resource - Create a resource from a ranges entry
|
| 254 | * @np: device node where the range belongs to
|
| 255 | * @index: the 'ranges' index to convert to a resource
|
| 256 | * @res: pointer to a valid resource that will be updated to
|
| 257 | * reflect the values contained in the range.
|
| 258 | *
|
| 259 | * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range
|
| 260 | * cannot be converted to resource.
|
| 261 |
|
| 262 | int of_range_to_resource(struct device_node *np, int index, struct resource *res)
|
| 263 | {
|
| 264 | int ret, i = 0;
|
| 265 | struct of_range_parser parser;
|
| 266 | struct of_range range;
|
| 267 |
|
| 268 | ret = of_range_parser_init(&parser, np);
|
| 269 | if (ret)
|
| 270 | return ret;
|
| 271 |
|
| 272 | for_each_of_range(&parser, &range)
|
| 273 | if (i++ == index)
|
| 274 | return of_pci_range_to_resource(&range, np, res);
|
| 275 |
|
| 276 | return -ENOENT;
|
| 277 | }
|
| 278 | EXPORT_SYMBOL(of_range_to_resource);
|
| 279 |
|
| 280 | |
| 281 | * ISA bus specific translator
|
| 282 |
|
| 283 |
|
| 284 | static int of_bus_isa_match(struct device_node *np)
|
| 285 | {
|
| 286 | return of_node_name_eq(np, "isa");
|
| 287 | }
|
| 288 |
|
| 289 | static void of_bus_isa_count_cells(struct device_node *child,
|
| 290 | int *addrc, int *sizec)
|
| 291 | {
|
| 292 | if (addrc)
|
| 293 | *addrc = 2;
|
| 294 | if (sizec)
|
| 295 | *sizec = 1;
|
| 296 | }
|
| 297 |
|
| 298 | static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
|
| 299 | int pna, int fna)
|
| 300 | {
|
| 301 |
|
| 302 | if ((addr[0] ^ range[0]) & cpu_to_be32(1))
|
| 303 | return OF_BAD_ADDR;
|
| 304 |
|
| 305 | return of_bus_default_map(addr, range, na, ns, pna, fna);
|
| 306 | }
|
| 307 |
|
| 308 | static unsigned int of_bus_isa_get_flags(const __be32 *addr)
|
| 309 | {
|
| 310 | unsigned int flags = 0;
|
| 311 | u32 w = be32_to_cpup(addr);
|
| 312 |
|
| 313 | if (w & 1)
|
| 314 | flags |= IORESOURCE_IO;
|
| 315 | else
|
| 316 | flags |= IORESOURCE_MEM;
|
| 317 | return flags;
|
| 318 | }
|
| 319 |
|
| 320 | static int of_bus_default_flags_match(struct device_node *np)
|
| 321 | {
|
| 322 | |
| 323 | * Check for presence first since of_bus_n_addr_cells() will warn when
|
| 324 | * walking parent nodes.
|
| 325 |
|
| 326 | return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3);
|
| 327 | }
|
| 328 |
|
| 329 | static int of_bus_default_match(struct device_node *np)
|
| 330 | {
|
| 331 | return of_property_present(np, "#address-cells");
|
| 332 | }
|
| 333 |
|
| 334 | |
| 335 | * Array of bus specific translators
|
| 336 |
|
| 337 |
|
| 338 | static const struct of_bus of_busses[] = {
|
| 339 | #ifdef CONFIG_PCI
|
| 340 |
|
| 341 | {
|
| 342 | .name = "pci",
|
| 343 | .addresses = "assigned-addresses",
|
| 344 | .match = of_bus_pci_match,
|
| 345 | .count_cells = of_bus_pci_count_cells,
|
| 346 | .map = of_bus_pci_map,
|
| 347 | .translate = of_bus_default_flags_translate,
|
| 348 | .flag_cells = 1,
|
| 349 | .get_flags = of_bus_pci_get_flags,
|
| 350 | },
|
| 351 | #endif
|
| 352 |
|
| 353 | {
|
| 354 | .name = "isa",
|
| 355 | .addresses = "reg",
|
| 356 | .match = of_bus_isa_match,
|
| 357 | .count_cells = of_bus_isa_count_cells,
|
| 358 | .map = of_bus_isa_map,
|
| 359 | .translate = of_bus_default_flags_translate,
|
| 360 | .flag_cells = 1,
|
| 361 | .get_flags = of_bus_isa_get_flags,
|
| 362 | },
|
| 363 |
|
| 364 | {
|
| 365 | .name = "default-flags",
|
| 366 | .addresses = "reg",
|
| 367 | .match = of_bus_default_flags_match,
|
| 368 | .count_cells = of_bus_default_count_cells,
|
| 369 | .map = of_bus_default_flags_map,
|
| 370 | .translate = of_bus_default_flags_translate,
|
| 371 | .flag_cells = 1,
|
| 372 | .get_flags = of_bus_default_flags_get_flags,
|
| 373 | },
|
| 374 |
|
| 375 | {
|
| 376 | .name = "default",
|
| 377 | .addresses = "reg",
|
| 378 | .match = of_bus_default_match,
|
| 379 | .count_cells = of_bus_default_count_cells,
|
| 380 | .map = of_bus_default_map,
|
| 381 | .translate = of_bus_default_translate,
|
| 382 | .get_flags = of_bus_default_get_flags,
|
| 383 | },
|
| 384 | };
|
| 385 |
|
| 386 | static const struct of_bus *of_match_bus(struct device_node *np)
|
| 387 | {
|
| 388 | int i;
|
| 389 |
|
| 390 | for (i = 0; i < ARRAY_SIZE(of_busses); i++)
|
| 391 | if (!of_busses[i].match || of_busses[i].match(np))
|
| 392 | return &of_busses[i];
|
| 393 | return NULL;
|
| 394 | }
|
| 395 |
|
| 396 | static int of_empty_ranges_quirk(const struct device_node *np)
|
| 397 | {
|
| 398 | if (IS_ENABLED(CONFIG_PPC)) {
|
| 399 |
|
| 400 | static int quirk_state = -1;
|
| 401 |
|
| 402 |
|
| 403 | if (of_device_is_compatible(np, "1682m-sdc"))
|
| 404 | return true;
|
| 405 |
|
| 406 |
|
| 407 | if (quirk_state < 0)
|
| 408 | quirk_state =
|
| 409 | of_machine_is_compatible("Power Macintosh") ||
|
| 410 | of_machine_is_compatible("MacRISC");
|
| 411 | return quirk_state;
|
| 412 | }
|
| 413 | return false;
|
| 414 | }
|
| 415 |
|
| 416 | static int of_translate_one(const struct device_node *parent, const struct of_bus *bus,
|
| 417 | const struct of_bus *pbus, __be32 *addr,
|
| 418 | int na, int ns, int pna, const char *rprop)
|
| 419 | {
|
| 420 | const __be32 *ranges;
|
| 421 | unsigned int rlen;
|
| 422 | int rone;
|
| 423 | u64 offset = OF_BAD_ADDR;
|
| 424 |
|
| 425 | |
| 426 | * Normally, an absence of a "ranges" property means we are
|
| 427 | * crossing a non-translatable boundary, and thus the addresses
|
| 428 | * below the current cannot be converted to CPU physical ones.
|
| 429 | * Unfortunately, while this is very clear in the spec, it's not
|
| 430 | * what Apple understood, and they do have things like /uni-n or
|
| 431 | * /ht nodes with no "ranges" property and a lot of perfectly
|
| 432 | * useable mapped devices below them. Thus we treat the absence of
|
| 433 | * "ranges" as equivalent to an empty "ranges" property which means
|
| 434 | * a 1:1 translation at that level. It's up to the caller not to try
|
| 435 | * to translate addresses that aren't supposed to be translated in
|
| 436 | * the first place. --BenH.
|
| 437 | *
|
| 438 | * As far as we know, this damage only exists on Apple machines, so
|
| 439 | * This code is only enabled on powerpc. --gcl
|
| 440 | *
|
| 441 | * This quirk also applies for 'dma-ranges' which frequently exist in
|
| 442 | * child nodes without 'dma-ranges' in the parent nodes. --RobH
|
| 443 |
|
| 444 | ranges = of_get_property(parent, rprop, &rlen);
|
| 445 | if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
|
| 446 | strcmp(rprop, "dma-ranges")) {
|
| 447 | pr_debug("no ranges; cannot translate\n");
|
| 448 | return 1;
|
| 449 | }
|
| 450 | if (ranges == NULL || rlen == 0) {
|
| 451 | offset = of_read_number(addr, na);
|
| 452 |
|
| 453 | memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);
|
| 454 | pr_debug("empty ranges; 1:1 translation\n");
|
| 455 | goto finish;
|
| 456 | }
|
| 457 |
|
| 458 | pr_debug("walking ranges...\n");
|
| 459 |
|
| 460 |
|
| 461 | rlen /= 4;
|
| 462 | rone = na + pna + ns;
|
| 463 | for (; rlen >= rone; rlen -= rone, ranges += rone) {
|
| 464 | offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);
|
| 465 | if (offset != OF_BAD_ADDR)
|
| 466 | break;
|
| 467 | }
|
| 468 | if (offset == OF_BAD_ADDR) {
|
| 469 | pr_debug("not found !\n");
|
| 470 | return 1;
|
| 471 | }
|
| 472 | memcpy(addr, ranges + na, 4 * pna);
|
| 473 |
|
| 474 | finish:
|
| 475 | of_dump_addr("parent translation for:", addr, pna);
|
| 476 | pr_debug("with offset: %llx\n", offset);
|
| 477 |
|
| 478 |
|
| 479 | return pbus->translate(addr, offset, pna);
|
| 480 | }
|
| 481 |
|
| 482 | |
| 483 | * Translate an address from the device-tree into a CPU physical address,
|
| 484 | * this walks up the tree and applies the various bus mappings on the
|
| 485 | * way.
|
| 486 | *
|
| 487 | * Note: We consider that crossing any level with #size-cells == 0 to mean
|
| 488 | * that translation is impossible (that is we are not dealing with a value
|
| 489 | * that can be mapped to a cpu physical address). This is not really specified
|
| 490 | * that way, but this is traditionally the way IBM at least do things
|
| 491 | *
|
| 492 | * Whenever the translation fails, the *host pointer will be set to the
|
| 493 | * device that had registered logical PIO mapping, and the return code is
|
| 494 | * relative to that node.
|
| 495 |
|
| 496 | static u64 __of_translate_address(struct device_node *node,
|
| 497 | struct device_node *(*get_parent)(const struct device_node *),
|
| 498 | const __be32 *in_addr, const char *rprop,
|
| 499 | struct device_node **host)
|
| 500 | {
|
| 501 | struct device_node *dev __free(device_node) = of_node_get(node);
|
| 502 | struct device_node *parent __free(device_node) = get_parent(dev);
|
| 503 | const struct of_bus *bus, *pbus;
|
| 504 | __be32 addr[OF_MAX_ADDR_CELLS];
|
| 505 | int na, ns, pna, pns;
|
| 506 |
|
| 507 | pr_debug("** translation for device %pOF **\n", dev);
|
| 508 |
|
| 509 | *host = NULL;
|
| 510 |
|
| 511 | if (parent == NULL)
|
| 512 | return OF_BAD_ADDR;
|
| 513 | bus = of_match_bus(parent);
|
| 514 | if (!bus)
|
| 515 | return OF_BAD_ADDR;
|
| 516 |
|
| 517 |
|
| 518 | bus->count_cells(dev, &na, &ns);
|
| 519 | if (!OF_CHECK_COUNTS(na, ns)) {
|
| 520 | pr_debug("Bad cell count for %pOF\n", dev);
|
| 521 | return OF_BAD_ADDR;
|
| 522 | }
|
| 523 | memcpy(addr, in_addr, na * 4);
|
| 524 |
|
| 525 | pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
|
| 526 | bus->name, na, ns, parent);
|
| 527 | of_dump_addr("translating address:", addr, na);
|
| 528 |
|
| 529 |
|
| 530 | for (;;) {
|
| 531 | struct logic_pio_hwaddr *iorange;
|
| 532 |
|
| 533 |
|
| 534 | of_node_put(dev);
|
| 535 | dev = parent;
|
| 536 | parent = get_parent(dev);
|
| 537 |
|
| 538 |
|
| 539 | if (parent == NULL) {
|
| 540 | pr_debug("reached root node\n");
|
| 541 | return of_read_number(addr, na);
|
| 542 | }
|
| 543 |
|
| 544 | |
| 545 | * For indirectIO device which has no ranges property, get
|
| 546 | * the address from reg directly.
|
| 547 |
|
| 548 | iorange = find_io_range_by_fwnode(&dev->fwnode);
|
| 549 | if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
|
| 550 | u64 result = of_read_number(addr + 1, na - 1);
|
| 551 | pr_debug("indirectIO matched(%pOF) 0x%llx\n",
|
| 552 | dev, result);
|
| 553 | *host = no_free_ptr(dev);
|
| 554 | return result;
|
| 555 | }
|
| 556 |
|
| 557 |
|
| 558 | pbus = of_match_bus(parent);
|
| 559 | if (!pbus)
|
| 560 | return OF_BAD_ADDR;
|
| 561 | pbus->count_cells(dev, &pna, &pns);
|
| 562 | if (!OF_CHECK_COUNTS(pna, pns)) {
|
| 563 | pr_err("Bad cell count for %pOF\n", dev);
|
| 564 | return OF_BAD_ADDR;
|
| 565 | }
|
| 566 |
|
| 567 | pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
|
| 568 | pbus->name, pna, pns, parent);
|
| 569 |
|
| 570 |
|
| 571 | if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
|
| 572 | return OF_BAD_ADDR;
|
| 573 |
|
| 574 |
|
| 575 | na = pna;
|
| 576 | ns = pns;
|
| 577 | bus = pbus;
|
| 578 |
|
| 579 | of_dump_addr("one level translation:", addr, na);
|
| 580 | }
|
| 581 |
|
| 582 | unreachable();
|
| 583 | }
|
| 584 |
|
| 585 | u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
|
| 586 | {
|
| 587 | struct device_node *host;
|
| 588 | u64 ret;
|
| 589 |
|
| 590 | ret = __of_translate_address(dev, of_get_parent,
|
| 591 | in_addr, "ranges", &host);
|
| 592 | if (host) {
|
| 593 | of_node_put(host);
|
| 594 | return OF_BAD_ADDR;
|
| 595 | }
|
| 596 |
|
| 597 | return ret;
|
| 598 | }
|
| 599 | EXPORT_SYMBOL(of_translate_address);
|
| 600 |
|
| 601 | #ifdef CONFIG_HAS_DMA
|
| 602 | struct device_node *__of_get_dma_parent(const struct device_node *np)
|
| 603 | {
|
| 604 | struct of_phandle_args args;
|
| 605 | int ret, index;
|
| 606 |
|
| 607 | index = of_property_match_string(np, "interconnect-names", "dma-mem");
|
| 608 | if (index < 0)
|
| 609 | return of_get_parent(np);
|
| 610 |
|
| 611 | ret = of_parse_phandle_with_args(np, "interconnects",
|
| 612 | "#interconnect-cells",
|
| 613 | index, &args);
|
| 614 | if (ret < 0)
|
| 615 | return of_get_parent(np);
|
| 616 |
|
| 617 | return args.np;
|
| 618 | }
|
| 619 | #endif
|
| 620 |
|
| 621 | static struct device_node *of_get_next_dma_parent(struct device_node *np)
|
| 622 | {
|
| 623 | struct device_node *parent;
|
| 624 |
|
| 625 | parent = __of_get_dma_parent(np);
|
| 626 | of_node_put(np);
|
| 627 |
|
| 628 | return parent;
|
| 629 | }
|
| 630 |
|
| 631 | u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
|
| 632 | {
|
| 633 | struct device_node *host;
|
| 634 | u64 ret;
|
| 635 |
|
| 636 | ret = __of_translate_address(dev, __of_get_dma_parent,
|
| 637 | in_addr, "dma-ranges", &host);
|
| 638 |
|
| 639 | if (host) {
|
| 640 | of_node_put(host);
|
| 641 | return OF_BAD_ADDR;
|
| 642 | }
|
| 643 |
|
| 644 | return ret;
|
| 645 | }
|
| 646 | EXPORT_SYMBOL(of_translate_dma_address);
|
| 647 |
|
| 648 | |
| 649 | * of_translate_dma_region - Translate device tree address and size tuple
|
| 650 | * @dev: device tree node for which to translate
|
| 651 | * @prop: pointer into array of cells
|
| 652 | * @start: return value for the start of the DMA range
|
| 653 | * @length: return value for the length of the DMA range
|
| 654 | *
|
| 655 | * Returns a pointer to the cell immediately following the translated DMA region.
|
| 656 |
|
| 657 | const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
|
| 658 | phys_addr_t *start, size_t *length)
|
| 659 | {
|
| 660 | struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
|
| 661 | u64 address, size;
|
| 662 | int na, ns;
|
| 663 |
|
| 664 | if (!parent)
|
| 665 | return NULL;
|
| 666 |
|
| 667 | na = of_bus_n_addr_cells(parent);
|
| 668 | ns = of_bus_n_size_cells(parent);
|
| 669 |
|
| 670 | address = of_translate_dma_address(dev, prop);
|
| 671 | if (address == OF_BAD_ADDR)
|
| 672 | return NULL;
|
| 673 |
|
| 674 | size = of_read_number(prop + na, ns);
|
| 675 |
|
| 676 | if (start)
|
| 677 | *start = address;
|
| 678 |
|
| 679 | if (length)
|
| 680 | *length = size;
|
| 681 |
|
| 682 | return prop + na + ns;
|
| 683 | }
|
| 684 | EXPORT_SYMBOL(of_translate_dma_region);
|
| 685 |
|
| 686 | const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
|
| 687 | u64 *size, unsigned int *flags)
|
| 688 | {
|
| 689 | const __be32 *prop;
|
| 690 | unsigned int psize;
|
| 691 | struct device_node *parent __free(device_node) = of_get_parent(dev);
|
| 692 | const struct of_bus *bus;
|
| 693 | int onesize, i, na, ns;
|
| 694 |
|
| 695 | if (parent == NULL)
|
| 696 | return NULL;
|
| 697 |
|
| 698 |
|
| 699 | bus = of_match_bus(parent);
|
| 700 | if (!bus || (strcmp(bus->name, "pci") && (bar_no >= 0)))
|
| 701 | return NULL;
|
| 702 |
|
| 703 |
|
| 704 | prop = of_get_property(dev, bus->addresses, &psize);
|
| 705 | if (prop == NULL)
|
| 706 | return NULL;
|
| 707 | psize /= 4;
|
| 708 |
|
| 709 | bus->count_cells(dev, &na, &ns);
|
| 710 | if (!OF_CHECK_ADDR_COUNT(na))
|
| 711 | return NULL;
|
| 712 |
|
| 713 | onesize = na + ns;
|
| 714 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
|
| 715 | u32 val = be32_to_cpu(prop[0]);
|
| 716 |
|
| 717 | if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
|
| 718 | ((index >= 0) && (i == index))) {
|
| 719 | if (size)
|
| 720 | *size = of_read_number(prop + na, ns);
|
| 721 | if (flags)
|
| 722 | *flags = bus->get_flags(prop);
|
| 723 | return prop;
|
| 724 | }
|
| 725 | }
|
| 726 | return NULL;
|
| 727 | }
|
| 728 | EXPORT_SYMBOL(__of_get_address);
|
| 729 |
|
| 730 | |
| 731 | * of_property_read_reg - Retrieve the specified "reg" entry index without translating
|
| 732 | * @np: device tree node for which to retrieve "reg" from
|
| 733 | * @idx: "reg" entry index to read
|
| 734 | * @addr: return value for the untranslated address
|
| 735 | * @size: return value for the entry size
|
| 736 | *
|
| 737 | * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
|
| 738 | * size values filled in.
|
| 739 |
|
| 740 | int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
|
| 741 | {
|
| 742 | const __be32 *prop = of_get_address(np, idx, size, NULL);
|
| 743 |
|
| 744 | if (!prop)
|
| 745 | return -EINVAL;
|
| 746 |
|
| 747 | *addr = of_read_number(prop, of_n_addr_cells(np));
|
| 748 |
|
| 749 | return 0;
|
| 750 | }
|
| 751 | EXPORT_SYMBOL(of_property_read_reg);
|
| 752 |
|
| 753 | static int parser_init(struct of_pci_range_parser *parser,
|
| 754 | struct device_node *node, const char *name)
|
| 755 | {
|
| 756 | const __be32 *range;
|
| 757 | int rlen;
|
| 758 |
|
| 759 | parser->node = node;
|
| 760 | parser->pna = of_n_addr_cells(node);
|
| 761 | parser->na = of_bus_n_addr_cells(node);
|
| 762 | parser->ns = of_bus_n_size_cells(node);
|
| 763 | parser->dma = !strcmp(name, "dma-ranges");
|
| 764 | parser->bus = of_match_bus(node);
|
| 765 | parser->range = NULL;
|
| 766 | parser->end = NULL;
|
| 767 |
|
| 768 | range = of_get_property(node, name, &rlen);
|
| 769 | if (!range)
|
| 770 | return -ENOENT;
|
| 771 |
|
| 772 | if (!parser->bus ||
|
| 773 | !OF_CHECK_COUNTS(parser->na, parser->ns) ||
|
| 774 | !OF_CHECK_ADDR_COUNT(parser->pna))
|
| 775 | return -EINVAL;
|
| 776 |
|
| 777 | parser->range = range;
|
| 778 | parser->end = range + rlen / sizeof(__be32);
|
| 779 |
|
| 780 | return 0;
|
| 781 | }
|
| 782 |
|
| 783 | int of_pci_range_parser_init(struct of_pci_range_parser *parser,
|
| 784 | struct device_node *node)
|
| 785 | {
|
| 786 | return parser_init(parser, node, "ranges");
|
| 787 | }
|
| 788 | EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
|
| 789 |
|
| 790 | int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
|
| 791 | struct device_node *node)
|
| 792 | {
|
| 793 | return parser_init(parser, node, "dma-ranges");
|
| 794 | }
|
| 795 | EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
|
| 796 | #define of_dma_range_parser_init of_pci_dma_range_parser_init
|
| 797 |
|
| 798 | struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
|
| 799 | struct of_pci_range *range)
|
| 800 | {
|
| 801 | int na = parser->na;
|
| 802 | int ns = parser->ns;
|
| 803 | int np = parser->pna + na + ns;
|
| 804 | int busflag_na;
|
| 805 |
|
| 806 | if (!range)
|
| 807 | return NULL;
|
| 808 |
|
| 809 | if (!parser->range || parser->range + np > parser->end)
|
| 810 | return NULL;
|
| 811 |
|
| 812 | busflag_na = parser->bus->flag_cells;
|
| 813 |
|
| 814 | range->flags = parser->bus->get_flags(parser->range);
|
| 815 |
|
| 816 | range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
|
| 817 |
|
| 818 | if (parser->dma)
|
| 819 | range->cpu_addr = of_translate_dma_address(parser->node,
|
| 820 | parser->range + na);
|
| 821 | else
|
| 822 | range->cpu_addr = of_translate_address(parser->node,
|
| 823 | parser->range + na);
|
| 824 |
|
| 825 | range->parent_bus_addr = of_read_number(parser->range + na, parser->pna);
|
| 826 | range->size = of_read_number(parser->range + parser->pna + na, ns);
|
| 827 |
|
| 828 | parser->range += np;
|
| 829 |
|
| 830 |
|
| 831 | while (parser->range + np <= parser->end) {
|
| 832 | u32 flags = 0;
|
| 833 | u64 bus_addr, cpu_addr, size;
|
| 834 |
|
| 835 | flags = parser->bus->get_flags(parser->range);
|
| 836 | bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
|
| 837 | if (parser->dma)
|
| 838 | cpu_addr = of_translate_dma_address(parser->node,
|
| 839 | parser->range + na);
|
| 840 | else
|
| 841 | cpu_addr = of_translate_address(parser->node,
|
| 842 | parser->range + na);
|
| 843 | size = of_read_number(parser->range + parser->pna + na, ns);
|
| 844 |
|
| 845 | if (flags != range->flags)
|
| 846 | break;
|
| 847 | if (bus_addr != range->bus_addr + range->size ||
|
| 848 | cpu_addr != range->cpu_addr + range->size)
|
| 849 | break;
|
| 850 |
|
| 851 | range->size += size;
|
| 852 | parser->range += np;
|
| 853 | }
|
| 854 |
|
| 855 | return range;
|
| 856 | }
|
| 857 | EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
|
| 858 |
|
| 859 | static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
|
| 860 | u64 size)
|
| 861 | {
|
| 862 | u64 taddr;
|
| 863 | unsigned long port;
|
| 864 | struct device_node *host;
|
| 865 |
|
| 866 | taddr = __of_translate_address(dev, of_get_parent,
|
| 867 | in_addr, "ranges", &host);
|
| 868 | if (host) {
|
| 869 |
|
| 870 | port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
|
| 871 | of_node_put(host);
|
| 872 | } else {
|
| 873 |
|
| 874 | port = pci_address_to_pio(taddr);
|
| 875 | }
|
| 876 |
|
| 877 | if (port == (unsigned long)-1)
|
| 878 | return OF_BAD_ADDR;
|
| 879 |
|
| 880 | return port;
|
| 881 | }
|
| 882 |
|
| 883 | #ifdef CONFIG_HAS_DMA
|
| 884 | |
| 885 | * of_dma_get_range - Get DMA range info and put it into a map array
|
| 886 | * @np: device node to get DMA range info
|
| 887 | * @map: dma range structure to return
|
| 888 | *
|
| 889 | * Look in bottom up direction for the first "dma-ranges" property
|
| 890 | * and parse it. Put the information into a DMA offset map array.
|
| 891 | *
|
| 892 | * dma-ranges format:
|
| 893 | * DMA addr (dma_addr) : naddr cells
|
| 894 | * CPU addr (phys_addr_t) : pna cells
|
| 895 | * size : nsize cells
|
| 896 | *
|
| 897 | * It returns -ENODEV if "dma-ranges" property was not found for this
|
| 898 | * device in the DT.
|
| 899 |
|
| 900 | int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
|
| 901 | {
|
| 902 | struct device_node *node __free(device_node) = of_node_get(np);
|
| 903 | const __be32 *ranges = NULL;
|
| 904 | bool found_dma_ranges = false;
|
| 905 | struct of_range_parser parser;
|
| 906 | struct of_range range;
|
| 907 | struct bus_dma_region *r;
|
| 908 | int len, num_ranges = 0;
|
| 909 |
|
| 910 | while (node) {
|
| 911 | ranges = of_get_property(node, "dma-ranges", &len);
|
| 912 |
|
| 913 |
|
| 914 | if (ranges && len > 0)
|
| 915 | break;
|
| 916 |
|
| 917 |
|
| 918 | if (found_dma_ranges && !ranges)
|
| 919 | return -ENODEV;
|
| 920 |
|
| 921 | found_dma_ranges = true;
|
| 922 |
|
| 923 | node = of_get_next_dma_parent(node);
|
| 924 | }
|
| 925 |
|
| 926 | if (!node || !ranges) {
|
| 927 | pr_debug("no dma-ranges found for node(%pOF)\n", np);
|
| 928 | return -ENODEV;
|
| 929 | }
|
| 930 | of_dma_range_parser_init(&parser, node);
|
| 931 | for_each_of_range(&parser, &range) {
|
| 932 | if (range.cpu_addr == OF_BAD_ADDR) {
|
| 933 | pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
|
| 934 | range.bus_addr, node);
|
| 935 | continue;
|
| 936 | }
|
| 937 | num_ranges++;
|
| 938 | }
|
| 939 |
|
| 940 | if (!num_ranges)
|
| 941 | return -EINVAL;
|
| 942 |
|
| 943 | r = kzalloc_objs(*r, num_ranges + 1);
|
| 944 | if (!r)
|
| 945 | return -ENOMEM;
|
| 946 |
|
| 947 | |
| 948 | * Record all info in the generic DMA ranges array for struct device,
|
| 949 | * returning an error if we don't find any parsable ranges.
|
| 950 |
|
| 951 | *map = r;
|
| 952 | of_dma_range_parser_init(&parser, node);
|
| 953 | for_each_of_range(&parser, &range) {
|
| 954 | pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
|
| 955 | range.bus_addr, range.cpu_addr, range.size);
|
| 956 | if (range.cpu_addr == OF_BAD_ADDR)
|
| 957 | continue;
|
| 958 | r->cpu_start = range.cpu_addr;
|
| 959 | r->dma_start = range.bus_addr;
|
| 960 | r->size = range.size;
|
| 961 | r++;
|
| 962 | }
|
| 963 | return 0;
|
| 964 | }
|
| 965 | #endif
|
| 966 |
|
| 967 | |
| 968 | * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
|
| 969 | * @np: The node to start searching from or NULL to start from the root
|
| 970 | *
|
| 971 | * Gets the highest CPU physical address that is addressable by all DMA masters
|
| 972 | * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
|
| 973 | * DMA constrained device is found, it returns PHYS_ADDR_MAX.
|
| 974 |
|
| 975 | phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
|
| 976 | {
|
| 977 | phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
|
| 978 | struct of_range_parser parser;
|
| 979 | phys_addr_t subtree_max_addr;
|
| 980 | struct device_node *child;
|
| 981 | struct of_range range;
|
| 982 | const __be32 *ranges;
|
| 983 | u64 cpu_end = 0;
|
| 984 | int len;
|
| 985 |
|
| 986 | if (!np)
|
| 987 | np = of_root;
|
| 988 |
|
| 989 | ranges = of_get_property(np, "dma-ranges", &len);
|
| 990 | if (ranges && len && !of_dma_range_parser_init(&parser, np)) {
|
| 991 | for_each_of_range(&parser, &range)
|
| 992 | if (range.cpu_addr + range.size > cpu_end)
|
| 993 | cpu_end = range.cpu_addr + range.size - 1;
|
| 994 |
|
| 995 | if (max_cpu_addr > cpu_end)
|
| 996 | max_cpu_addr = cpu_end;
|
| 997 | }
|
| 998 |
|
| 999 | for_each_available_child_of_node(np, child) {
|
| 1000 | subtree_max_addr = of_dma_get_max_cpu_address(child);
|
| 1001 | if (max_cpu_addr > subtree_max_addr)
|
| 1002 | max_cpu_addr = subtree_max_addr;
|
| 1003 | }
|
| 1004 |
|
| 1005 | return max_cpu_addr;
|
| 1006 | }
|
| 1007 |
|
| 1008 | |
| 1009 | * of_dma_is_coherent - Check if device is coherent
|
| 1010 | * @np: device node
|
| 1011 | *
|
| 1012 | * It returns true if "dma-coherent" property was found
|
| 1013 | * for this device in the DT, or if DMA is coherent by
|
| 1014 | * default for OF devices on the current platform and no
|
| 1015 | * "dma-noncoherent" property was found for this device.
|
| 1016 |
|
| 1017 | bool of_dma_is_coherent(struct device_node *np)
|
| 1018 | {
|
| 1019 | struct device_node *node __free(device_node) = of_node_get(np);
|
| 1020 |
|
| 1021 | while (node) {
|
| 1022 | if (of_property_read_bool(node, "dma-coherent"))
|
| 1023 | return true;
|
| 1024 |
|
| 1025 | if (of_property_read_bool(node, "dma-noncoherent"))
|
| 1026 | return false;
|
| 1027 |
|
| 1028 | node = of_get_next_dma_parent(node);
|
| 1029 | }
|
| 1030 | return dma_default_coherent;
|
| 1031 | }
|
| 1032 | EXPORT_SYMBOL_GPL(of_dma_is_coherent);
|
| 1033 |
|
| 1034 | |
| 1035 | * of_mmio_is_nonposted - Check if device uses non-posted MMIO
|
| 1036 | * @np: device node
|
| 1037 | *
|
| 1038 | * Returns true if the "nonposted-mmio" property was found for
|
| 1039 | * the device's bus.
|
| 1040 |
|
| 1041 | static bool of_mmio_is_nonposted(const struct device_node *np)
|
| 1042 | {
|
| 1043 | struct device_node *parent __free(device_node) = of_get_parent(np);
|
| 1044 |
|
| 1045 | if (of_property_read_bool(np, "nonposted-mmio"))
|
| 1046 | return true;
|
| 1047 |
|
| 1048 | return parent && of_property_read_bool(parent, "nonposted-mmio");
|
| 1049 | }
|
| 1050 |
|
| 1051 | static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
|
| 1052 | struct resource *r)
|
| 1053 | {
|
| 1054 | u64 taddr;
|
| 1055 | const __be32 *addrp;
|
| 1056 | u64 size;
|
| 1057 | unsigned int flags;
|
| 1058 | const char *name = NULL;
|
| 1059 |
|
| 1060 | addrp = __of_get_address(dev, index, bar_no, &size, &flags);
|
| 1061 | if (addrp == NULL)
|
| 1062 | return -EINVAL;
|
| 1063 |
|
| 1064 |
|
| 1065 | if (index >= 0)
|
| 1066 | of_property_read_string_index(dev, "reg-names", index, &name);
|
| 1067 |
|
| 1068 | if (flags & IORESOURCE_MEM)
|
| 1069 | taddr = of_translate_address(dev, addrp);
|
| 1070 | else if (flags & IORESOURCE_IO)
|
| 1071 | taddr = of_translate_ioport(dev, addrp, size);
|
| 1072 | else
|
| 1073 | return -EINVAL;
|
| 1074 |
|
| 1075 | if (taddr == OF_BAD_ADDR)
|
| 1076 | return -EINVAL;
|
| 1077 | memset(r, 0, sizeof(struct resource));
|
| 1078 |
|
| 1079 | if (of_mmio_is_nonposted(dev))
|
| 1080 | flags |= IORESOURCE_MEM_NONPOSTED;
|
| 1081 |
|
| 1082 | r->flags = flags;
|
| 1083 | r->name = name ? name : dev->full_name;
|
| 1084 |
|
| 1085 | return __of_address_resource_bounds(r, taddr, size);
|
| 1086 | }
|
| 1087 |
|
| 1088 | |
| 1089 | * of_address_to_resource - Translate device tree address and return as resource
|
| 1090 | * @dev: Caller's Device Node
|
| 1091 | * @index: Index into the array
|
| 1092 | * @r: Pointer to resource array
|
| 1093 | *
|
| 1094 | * Returns -EINVAL if the range cannot be converted to resource.
|
| 1095 | *
|
| 1096 | * Note that if your address is a PIO address, the conversion will fail if
|
| 1097 | * the physical address can't be internally converted to an IO token with
|
| 1098 | * pci_address_to_pio(), that is because it's either called too early or it
|
| 1099 | * can't be matched to any host bridge IO space
|
| 1100 |
|
| 1101 | int of_address_to_resource(struct device_node *dev, int index,
|
| 1102 | struct resource *r)
|
| 1103 | {
|
| 1104 | return __of_address_to_resource(dev, index, -1, r);
|
| 1105 | }
|
| 1106 | EXPORT_SYMBOL_GPL(of_address_to_resource);
|
| 1107 |
|
| 1108 | int of_pci_address_to_resource(struct device_node *dev, int bar,
|
| 1109 | struct resource *r)
|
| 1110 | {
|
| 1111 |
|
| 1112 | if (!IS_ENABLED(CONFIG_PCI))
|
| 1113 | return -ENOSYS;
|
| 1114 |
|
| 1115 | return __of_address_to_resource(dev, -1, bar, r);
|
| 1116 | }
|
| 1117 | EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
|
| 1118 |
|
| 1119 | |
| 1120 | * of_iomap - Maps the memory mapped IO for a given device_node
|
| 1121 | * @np: the device whose io range will be mapped
|
| 1122 | * @index: index of the io range
|
| 1123 | *
|
| 1124 | * Returns a pointer to the mapped memory
|
| 1125 |
|
| 1126 | void __iomem *of_iomap(struct device_node *np, int index)
|
| 1127 | {
|
| 1128 | struct resource res;
|
| 1129 |
|
| 1130 | if (of_address_to_resource(np, index, &res))
|
| 1131 | return NULL;
|
| 1132 |
|
| 1133 | if (res.flags & IORESOURCE_MEM_NONPOSTED)
|
| 1134 | return ioremap_np(res.start, resource_size(&res));
|
| 1135 | else
|
| 1136 | return ioremap(res.start, resource_size(&res));
|
| 1137 | }
|
| 1138 | EXPORT_SYMBOL(of_iomap);
|
| 1139 |
|
| 1140 | |
| 1141 | * of_io_request_and_map - Requests a resource and maps the memory mapped IO
|
| 1142 | * for a given device_node
|
| 1143 | * @device: the device whose io range will be mapped
|
| 1144 | * @index: index of the io range
|
| 1145 | * @name: name "override" for the memory region request or NULL
|
| 1146 | *
|
| 1147 | * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
|
| 1148 | * error code on failure. Usage example:
|
| 1149 | *
|
| 1150 | * base = of_io_request_and_map(node, 0, "foo");
|
| 1151 | * if (IS_ERR(base))
|
| 1152 | * return PTR_ERR(base);
|
| 1153 |
|
| 1154 | void __iomem *of_io_request_and_map(struct device_node *np, int index,
|
| 1155 | const char *name)
|
| 1156 | {
|
| 1157 | struct resource res;
|
| 1158 | void __iomem *mem;
|
| 1159 |
|
| 1160 | if (of_address_to_resource(np, index, &res))
|
| 1161 | return IOMEM_ERR_PTR(-EINVAL);
|
| 1162 |
|
| 1163 | if (!name)
|
| 1164 | name = res.name;
|
| 1165 | if (!request_mem_region(res.start, resource_size(&res), name))
|
| 1166 | return IOMEM_ERR_PTR(-EBUSY);
|
| 1167 |
|
| 1168 | if (res.flags & IORESOURCE_MEM_NONPOSTED)
|
| 1169 | mem = ioremap_np(res.start, resource_size(&res));
|
| 1170 | else
|
| 1171 | mem = ioremap(res.start, resource_size(&res));
|
| 1172 |
|
| 1173 | if (!mem) {
|
| 1174 | release_mem_region(res.start, resource_size(&res));
|
| 1175 | return IOMEM_ERR_PTR(-ENOMEM);
|
| 1176 | }
|
| 1177 |
|
| 1178 | return mem;
|
| 1179 | }
|
| 1180 | EXPORT_SYMBOL(of_io_request_and_map);
|