linux/arch/sparc/kernel/of_device.c
<<
>>
Prefs
   1#include <linux/string.h>
   2#include <linux/kernel.h>
   3#include <linux/of.h>
   4#include <linux/init.h>
   5#include <linux/module.h>
   6#include <linux/mod_devicetable.h>
   7#include <linux/slab.h>
   8#include <linux/errno.h>
   9#include <linux/of_device.h>
  10#include <linux/of_platform.h>
  11
  12static int node_match(struct device *dev, void *data)
  13{
  14        struct of_device *op = to_of_device(dev);
  15        struct device_node *dp = data;
  16
  17        return (op->node == dp);
  18}
  19
  20struct of_device *of_find_device_by_node(struct device_node *dp)
  21{
  22        struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
  23                                             dp, node_match);
  24
  25        if (dev)
  26                return to_of_device(dev);
  27
  28        return NULL;
  29}
  30EXPORT_SYMBOL(of_find_device_by_node);
  31
  32#ifdef CONFIG_PCI
  33struct bus_type ebus_bus_type;
  34EXPORT_SYMBOL(ebus_bus_type);
  35#endif
  36
  37#ifdef CONFIG_SBUS
  38struct bus_type sbus_bus_type;
  39EXPORT_SYMBOL(sbus_bus_type);
  40#endif
  41
  42struct bus_type of_platform_bus_type;
  43EXPORT_SYMBOL(of_platform_bus_type);
  44
  45static inline u64 of_read_addr(const u32 *cell, int size)
  46{
  47        u64 r = 0;
  48        while (size--)
  49                r = (r << 32) | *(cell++);
  50        return r;
  51}
  52
  53static void __init get_cells(struct device_node *dp,
  54                             int *addrc, int *sizec)
  55{
  56        if (addrc)
  57                *addrc = of_n_addr_cells(dp);
  58        if (sizec)
  59                *sizec = of_n_size_cells(dp);
  60}
  61
  62/* Max address size we deal with */
  63#define OF_MAX_ADDR_CELLS       4
  64
  65struct of_bus {
  66        const char      *name;
  67        const char      *addr_prop_name;
  68        int             (*match)(struct device_node *parent);
  69        void            (*count_cells)(struct device_node *child,
  70                                       int *addrc, int *sizec);
  71        int             (*map)(u32 *addr, const u32 *range,
  72                               int na, int ns, int pna);
  73        unsigned long   (*get_flags)(const u32 *addr, unsigned long);
  74};
  75
  76/*
  77 * Default translator (generic bus)
  78 */
  79
  80static void of_bus_default_count_cells(struct device_node *dev,
  81                                       int *addrc, int *sizec)
  82{
  83        get_cells(dev, addrc, sizec);
  84}
  85
  86/* Make sure the least significant 64-bits are in-range.  Even
  87 * for 3 or 4 cell values it is a good enough approximation.
  88 */
  89static int of_out_of_range(const u32 *addr, const u32 *base,
  90                           const u32 *size, int na, int ns)
  91{
  92        u64 a = of_read_addr(addr, na);
  93        u64 b = of_read_addr(base, na);
  94
  95        if (a < b)
  96                return 1;
  97
  98        b += of_read_addr(size, ns);
  99        if (a >= b)
 100                return 1;
 101
 102        return 0;
 103}
 104
 105static int of_bus_default_map(u32 *addr, const u32 *range,
 106                              int na, int ns, int pna)
 107{
 108        u32 result[OF_MAX_ADDR_CELLS];
 109        int i;
 110
 111        if (ns > 2) {
 112                printk("of_device: Cannot handle size cells (%d) > 2.", ns);
 113                return -EINVAL;
 114        }
 115
 116        if (of_out_of_range(addr, range, range + na + pna, na, ns))
 117                return -EINVAL;
 118
 119        /* Start with the parent range base.  */
 120        memcpy(result, range + na, pna * 4);
 121
 122        /* Add in the child address offset.  */
 123        for (i = 0; i < na; i++)
 124                result[pna - 1 - i] +=
 125                        (addr[na - 1 - i] -
 126                         range[na - 1 - i]);
 127
 128        memcpy(addr, result, pna * 4);
 129
 130        return 0;
 131}
 132
 133static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
 134{
 135        if (flags)
 136                return flags;
 137        return IORESOURCE_MEM;
 138}
 139
 140/*
 141 * PCI bus specific translator
 142 */
 143
 144static int of_bus_pci_match(struct device_node *np)
 145{
 146        if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
 147                /* Do not do PCI specific frobbing if the
 148                 * PCI bridge lacks a ranges property.  We
 149                 * want to pass it through up to the next
 150                 * parent as-is, not with the PCI translate
 151                 * method which chops off the top address cell.
 152                 */
 153                if (!of_find_property(np, "ranges", NULL))
 154                        return 0;
 155
 156                return 1;
 157        }
 158
 159        return 0;
 160}
 161
 162static 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
 171static int of_bus_pci_map(u32 *addr, const u32 *range,
 172                          int na, int ns, int pna)
 173{
 174        u32 result[OF_MAX_ADDR_CELLS];
 175        int i;
 176
 177        /* Check address type match */
 178        if ((addr[0] ^ range[0]) & 0x03000000)
 179                return -EINVAL;
 180
 181        if (of_out_of_range(addr + 1, range + 1, range + na + pna,
 182                            na - 1, ns))
 183                return -EINVAL;
 184
 185        /* Start with the parent range base.  */
 186        memcpy(result, range + na, pna * 4);
 187
 188        /* Add in the child address offset, skipping high cell.  */
 189        for (i = 0; i < na - 1; i++)
 190                result[pna - 1 - i] +=
 191                        (addr[na - 1 - i] -
 192                         range[na - 1 - i]);
 193
 194        memcpy(addr, result, pna * 4);
 195
 196        return 0;
 197}
 198
 199static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
 200{
 201        u32 w = addr[0];
 202
 203        /* For PCI, we override whatever child busses may have used.  */
 204        flags = 0;
 205        switch((w >> 24) & 0x03) {
 206        case 0x01:
 207                flags |= IORESOURCE_IO;
 208                break;
 209
 210        case 0x02: /* 32 bits */
 211        case 0x03: /* 64 bits */
 212                flags |= IORESOURCE_MEM;
 213                break;
 214        }
 215        if (w & 0x40000000)
 216                flags |= IORESOURCE_PREFETCH;
 217        return flags;
 218}
 219
 220/*
 221 * SBUS bus specific translator
 222 */
 223
 224static int of_bus_sbus_match(struct device_node *np)
 225{
 226        struct device_node *dp = np;
 227
 228        while (dp) {
 229                if (!strcmp(dp->name, "sbus") ||
 230                    !strcmp(dp->name, "sbi"))
 231                        return 1;
 232
 233                /* Have a look at use_1to1_mapping().  We're trying
 234                 * to match SBUS if that's the top-level bus and we
 235                 * don't have some intervening real bus that provides
 236                 * ranges based translations.
 237                 */
 238                if (of_find_property(dp, "ranges", NULL) != NULL)
 239                        break;
 240
 241                dp = dp->parent;
 242        }
 243
 244        return 0;
 245}
 246
 247static void of_bus_sbus_count_cells(struct device_node *child,
 248                                   int *addrc, int *sizec)
 249{
 250        if (addrc)
 251                *addrc = 2;
 252        if (sizec)
 253                *sizec = 1;
 254}
 255
 256static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
 257{
 258        return of_bus_default_map(addr, range, na, ns, pna);
 259}
 260
 261static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
 262{
 263        return IORESOURCE_MEM;
 264}
 265
 266
 267/*
 268 * Array of bus specific translators
 269 */
 270
 271static struct of_bus of_busses[] = {
 272        /* PCI */
 273        {
 274                .name = "pci",
 275                .addr_prop_name = "assigned-addresses",
 276                .match = of_bus_pci_match,
 277                .count_cells = of_bus_pci_count_cells,
 278                .map = of_bus_pci_map,
 279                .get_flags = of_bus_pci_get_flags,
 280        },
 281        /* SBUS */
 282        {
 283                .name = "sbus",
 284                .addr_prop_name = "reg",
 285                .match = of_bus_sbus_match,
 286                .count_cells = of_bus_sbus_count_cells,
 287                .map = of_bus_sbus_map,
 288                .get_flags = of_bus_sbus_get_flags,
 289        },
 290        /* Default */
 291        {
 292                .name = "default",
 293                .addr_prop_name = "reg",
 294                .match = NULL,
 295                .count_cells = of_bus_default_count_cells,
 296                .map = of_bus_default_map,
 297                .get_flags = of_bus_default_get_flags,
 298        },
 299};
 300
 301static struct of_bus *of_match_bus(struct device_node *np)
 302{
 303        int i;
 304
 305        for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
 306                if (!of_busses[i].match || of_busses[i].match(np))
 307                        return &of_busses[i];
 308        BUG();
 309        return NULL;
 310}
 311
 312static int __init build_one_resource(struct device_node *parent,
 313                                     struct of_bus *bus,
 314                                     struct of_bus *pbus,
 315                                     u32 *addr,
 316                                     int na, int ns, int pna)
 317{
 318        const u32 *ranges;
 319        unsigned int rlen;
 320        int rone;
 321
 322        ranges = of_get_property(parent, "ranges", &rlen);
 323        if (ranges == NULL || rlen == 0) {
 324                u32 result[OF_MAX_ADDR_CELLS];
 325                int i;
 326
 327                memset(result, 0, pna * 4);
 328                for (i = 0; i < na; i++)
 329                        result[pna - 1 - i] =
 330                                addr[na - 1 - i];
 331
 332                memcpy(addr, result, pna * 4);
 333                return 0;
 334        }
 335
 336        /* Now walk through the ranges */
 337        rlen /= 4;
 338        rone = na + pna + ns;
 339        for (; rlen >= rone; rlen -= rone, ranges += rone) {
 340                if (!bus->map(addr, ranges, na, ns, pna))
 341                        return 0;
 342        }
 343
 344        return 1;
 345}
 346
 347static int of_resource_verbose;
 348
 349static void __init build_device_resources(struct of_device *op,
 350                                          struct device *parent)
 351{
 352        struct of_device *p_op;
 353        struct of_bus *bus;
 354        int na, ns;
 355        int index, num_reg;
 356        const void *preg;
 357
 358        if (!parent)
 359                return;
 360
 361        p_op = to_of_device(parent);
 362        bus = of_match_bus(p_op->node);
 363        bus->count_cells(op->node, &na, &ns);
 364
 365        preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
 366        if (!preg || num_reg == 0)
 367                return;
 368
 369        /* Convert to num-cells.  */
 370        num_reg /= 4;
 371
 372        /* Conver to num-entries.  */
 373        num_reg /= na + ns;
 374
 375        for (index = 0; index < num_reg; index++) {
 376                struct resource *r = &op->resource[index];
 377                u32 addr[OF_MAX_ADDR_CELLS];
 378                const u32 *reg = (preg + (index * ((na + ns) * 4)));
 379                struct device_node *dp = op->node;
 380                struct device_node *pp = p_op->node;
 381                struct of_bus *pbus, *dbus;
 382                u64 size, result = OF_BAD_ADDR;
 383                unsigned long flags;
 384                int dna, dns;
 385                int pna, pns;
 386
 387                size = of_read_addr(reg + na, ns);
 388
 389                memcpy(addr, reg, na * 4);
 390
 391                flags = bus->get_flags(reg, 0);
 392
 393                /* If the immediate parent has no ranges property to apply,
 394                 * just use a 1<->1 mapping.
 395                 */
 396                if (of_find_property(pp, "ranges", NULL) == NULL) {
 397                        result = of_read_addr(addr, na);
 398                        goto build_res;
 399                }
 400
 401                dna = na;
 402                dns = ns;
 403                dbus = bus;
 404
 405                while (1) {
 406                        dp = pp;
 407                        pp = dp->parent;
 408                        if (!pp) {
 409                                result = of_read_addr(addr, dna);
 410                                break;
 411                        }
 412
 413                        pbus = of_match_bus(pp);
 414                        pbus->count_cells(dp, &pna, &pns);
 415
 416                        if (build_one_resource(dp, dbus, pbus, addr,
 417                                               dna, dns, pna))
 418                                break;
 419
 420                        flags = pbus->get_flags(addr, flags);
 421
 422                        dna = pna;
 423                        dns = pns;
 424                        dbus = pbus;
 425                }
 426
 427        build_res:
 428                memset(r, 0, sizeof(*r));
 429
 430                if (of_resource_verbose)
 431                        printk("%s reg[%d] -> %llx\n",
 432                               op->node->full_name, index,
 433                               result);
 434
 435                if (result != OF_BAD_ADDR) {
 436                        r->start = result & 0xffffffff;
 437                        r->end = result + size - 1;
 438                        r->flags = flags | ((result >> 32ULL) & 0xffUL);
 439                }
 440                r->name = op->node->name;
 441        }
 442}
 443
 444static struct of_device * __init scan_one_device(struct device_node *dp,
 445                                                 struct device *parent)
 446{
 447        struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
 448        const struct linux_prom_irqs *intr;
 449        struct dev_archdata *sd;
 450        int len, i;
 451
 452        if (!op)
 453                return NULL;
 454
 455        sd = &op->dev.archdata;
 456        sd->prom_node = dp;
 457        sd->op = op;
 458
 459        op->node = dp;
 460
 461        op->clock_freq = of_getintprop_default(dp, "clock-frequency",
 462                                               (25*1000*1000));
 463        op->portid = of_getintprop_default(dp, "upa-portid", -1);
 464        if (op->portid == -1)
 465                op->portid = of_getintprop_default(dp, "portid", -1);
 466
 467        intr = of_get_property(dp, "intr", &len);
 468        if (intr) {
 469                op->num_irqs = len / sizeof(struct linux_prom_irqs);
 470                for (i = 0; i < op->num_irqs; i++)
 471                        op->irqs[i] = intr[i].pri;
 472        } else {
 473                const unsigned int *irq =
 474                        of_get_property(dp, "interrupts", &len);
 475
 476                if (irq) {
 477                        op->num_irqs = len / sizeof(unsigned int);
 478                        for (i = 0; i < op->num_irqs; i++)
 479                                op->irqs[i] = irq[i];
 480                } else {
 481                        op->num_irqs = 0;
 482                }
 483        }
 484        if (sparc_cpu_model == sun4d) {
 485                static int pil_to_sbus[] = {
 486                        0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
 487                };
 488                struct device_node *io_unit, *sbi = dp->parent;
 489                const struct linux_prom_registers *regs;
 490                int board, slot;
 491
 492                while (sbi) {
 493                        if (!strcmp(sbi->name, "sbi"))
 494                                break;
 495
 496                        sbi = sbi->parent;
 497                }
 498                if (!sbi)
 499                        goto build_resources;
 500
 501                regs = of_get_property(dp, "reg", NULL);
 502                if (!regs)
 503                        goto build_resources;
 504
 505                slot = regs->which_io;
 506
 507                /* If SBI's parent is not io-unit or the io-unit lacks
 508                 * a "board#" property, something is very wrong.
 509                 */
 510                if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
 511                        printk("%s: Error, parent is not io-unit.\n",
 512                               sbi->full_name);
 513                        goto build_resources;
 514                }
 515                io_unit = sbi->parent;
 516                board = of_getintprop_default(io_unit, "board#", -1);
 517                if (board == -1) {
 518                        printk("%s: Error, lacks board# property.\n",
 519                               io_unit->full_name);
 520                        goto build_resources;
 521                }
 522
 523                for (i = 0; i < op->num_irqs; i++) {
 524                        int this_irq = op->irqs[i];
 525                        int sbusl = pil_to_sbus[this_irq];
 526
 527                        if (sbusl)
 528                                this_irq = (((board + 1) << 5) +
 529                                            (sbusl << 2) +
 530                                            slot);
 531
 532                        op->irqs[i] = this_irq;
 533                }
 534        }
 535
 536build_resources:
 537        build_device_resources(op, parent);
 538
 539        op->dev.parent = parent;
 540        op->dev.bus = &of_platform_bus_type;
 541        if (!parent)
 542                strcpy(op->dev.bus_id, "root");
 543        else
 544                sprintf(op->dev.bus_id, "%08x", dp->node);
 545
 546        if (of_device_register(op)) {
 547                printk("%s: Could not register of device.\n",
 548                       dp->full_name);
 549                kfree(op);
 550                op = NULL;
 551        }
 552
 553        return op;
 554}
 555
 556static void __init scan_tree(struct device_node *dp, struct device *parent)
 557{
 558        while (dp) {
 559                struct of_device *op = scan_one_device(dp, parent);
 560
 561                if (op)
 562                        scan_tree(dp->child, &op->dev);
 563
 564                dp = dp->sibling;
 565        }
 566}
 567
 568static void __init scan_of_devices(void)
 569{
 570        struct device_node *root = of_find_node_by_path("/");
 571        struct of_device *parent;
 572
 573        parent = scan_one_device(root, NULL);
 574        if (!parent)
 575                return;
 576
 577        scan_tree(root->child, &parent->dev);
 578}
 579
 580static int __init of_bus_driver_init(void)
 581{
 582        int err;
 583
 584        err = of_bus_type_init(&of_platform_bus_type, "of");
 585#ifdef CONFIG_PCI
 586        if (!err)
 587                err = of_bus_type_init(&ebus_bus_type, "ebus");
 588#endif
 589#ifdef CONFIG_SBUS
 590        if (!err)
 591                err = of_bus_type_init(&sbus_bus_type, "sbus");
 592#endif
 593
 594        if (!err)
 595                scan_of_devices();
 596
 597        return err;
 598}
 599
 600postcore_initcall(of_bus_driver_init);
 601
 602static int __init of_debug(char *str)
 603{
 604        int val = 0;
 605
 606        get_option(&str, &val);
 607        if (val & 1)
 608                of_resource_verbose = 1;
 609        return 1;
 610}
 611
 612__setup("of_debug=", of_debug);
 613