Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/string.h>
0003 #include <linux/kernel.h>
0004 #include <linux/of.h>
0005 #include <linux/dma-mapping.h>
0006 #include <linux/init.h>
0007 #include <linux/export.h>
0008 #include <linux/mod_devicetable.h>
0009 #include <linux/slab.h>
0010 #include <linux/errno.h>
0011 #include <linux/irq.h>
0012 #include <linux/of_device.h>
0013 #include <linux/of_platform.h>
0014 #include <asm/spitfire.h>
0015 
0016 #include "of_device_common.h"
0017 
0018 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
0019 {
0020     unsigned long ret = res->start + offset;
0021     struct resource *r;
0022 
0023     if (res->flags & IORESOURCE_MEM)
0024         r = request_mem_region(ret, size, name);
0025     else
0026         r = request_region(ret, size, name);
0027     if (!r)
0028         ret = 0;
0029 
0030     return (void __iomem *) ret;
0031 }
0032 EXPORT_SYMBOL(of_ioremap);
0033 
0034 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
0035 {
0036     if (res->flags & IORESOURCE_MEM)
0037         release_mem_region((unsigned long) base, size);
0038     else
0039         release_region((unsigned long) base, size);
0040 }
0041 EXPORT_SYMBOL(of_iounmap);
0042 
0043 /*
0044  * PCI bus specific translator
0045  */
0046 
0047 static int of_bus_pci_match(struct device_node *np)
0048 {
0049     if (of_node_name_eq(np, "pci")) {
0050         const char *model = of_get_property(np, "model", NULL);
0051 
0052         if (model && !strcmp(model, "SUNW,simba"))
0053             return 0;
0054 
0055         /* Do not do PCI specific frobbing if the
0056          * PCI bridge lacks a ranges property.  We
0057          * want to pass it through up to the next
0058          * parent as-is, not with the PCI translate
0059          * method which chops off the top address cell.
0060          */
0061         if (!of_find_property(np, "ranges", NULL))
0062             return 0;
0063 
0064         return 1;
0065     }
0066 
0067     return 0;
0068 }
0069 
0070 static int of_bus_simba_match(struct device_node *np)
0071 {
0072     const char *model = of_get_property(np, "model", NULL);
0073 
0074     if (model && !strcmp(model, "SUNW,simba"))
0075         return 1;
0076 
0077     /* Treat PCI busses lacking ranges property just like
0078      * simba.
0079      */
0080     if (of_node_name_eq(np, "pci")) {
0081         if (!of_find_property(np, "ranges", NULL))
0082             return 1;
0083     }
0084 
0085     return 0;
0086 }
0087 
0088 static int of_bus_simba_map(u32 *addr, const u32 *range,
0089                 int na, int ns, int pna)
0090 {
0091     return 0;
0092 }
0093 
0094 static void of_bus_pci_count_cells(struct device_node *np,
0095                    int *addrc, int *sizec)
0096 {
0097     if (addrc)
0098         *addrc = 3;
0099     if (sizec)
0100         *sizec = 2;
0101 }
0102 
0103 static int of_bus_pci_map(u32 *addr, const u32 *range,
0104               int na, int ns, int pna)
0105 {
0106     u32 result[OF_MAX_ADDR_CELLS];
0107     int i;
0108 
0109     /* Check address type match */
0110     if (!((addr[0] ^ range[0]) & 0x03000000))
0111         goto type_match;
0112 
0113     /* Special exception, we can map a 64-bit address into
0114      * a 32-bit range.
0115      */
0116     if ((addr[0] & 0x03000000) == 0x03000000 &&
0117         (range[0] & 0x03000000) == 0x02000000)
0118         goto type_match;
0119 
0120     return -EINVAL;
0121 
0122 type_match:
0123     if (of_out_of_range(addr + 1, range + 1, range + na + pna,
0124                 na - 1, ns))
0125         return -EINVAL;
0126 
0127     /* Start with the parent range base.  */
0128     memcpy(result, range + na, pna * 4);
0129 
0130     /* Add in the child address offset, skipping high cell.  */
0131     for (i = 0; i < na - 1; i++)
0132         result[pna - 1 - i] +=
0133             (addr[na - 1 - i] -
0134              range[na - 1 - i]);
0135 
0136     memcpy(addr, result, pna * 4);
0137 
0138     return 0;
0139 }
0140 
0141 static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
0142 {
0143     u32 w = addr[0];
0144 
0145     /* For PCI, we override whatever child busses may have used.  */
0146     flags = 0;
0147     switch((w >> 24) & 0x03) {
0148     case 0x01:
0149         flags |= IORESOURCE_IO;
0150         break;
0151 
0152     case 0x02: /* 32 bits */
0153     case 0x03: /* 64 bits */
0154         flags |= IORESOURCE_MEM;
0155         break;
0156     }
0157     if (w & 0x40000000)
0158         flags |= IORESOURCE_PREFETCH;
0159     return flags;
0160 }
0161 
0162 /*
0163  * FHC/Central bus specific translator.
0164  *
0165  * This is just needed to hard-code the address and size cell
0166  * counts.  'fhc' and 'central' nodes lack the #address-cells and
0167  * #size-cells properties, and if you walk to the root on such
0168  * Enterprise boxes all you'll get is a #size-cells of 2 which is
0169  * not what we want to use.
0170  */
0171 static int of_bus_fhc_match(struct device_node *np)
0172 {
0173     return of_node_name_eq(np, "fhc") ||
0174         of_node_name_eq(np, "central");
0175 }
0176 
0177 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
0178 
0179 /*
0180  * Array of bus specific translators
0181  */
0182 
0183 static struct of_bus of_busses[] = {
0184     /* PCI */
0185     {
0186         .name = "pci",
0187         .addr_prop_name = "assigned-addresses",
0188         .match = of_bus_pci_match,
0189         .count_cells = of_bus_pci_count_cells,
0190         .map = of_bus_pci_map,
0191         .get_flags = of_bus_pci_get_flags,
0192     },
0193     /* SIMBA */
0194     {
0195         .name = "simba",
0196         .addr_prop_name = "assigned-addresses",
0197         .match = of_bus_simba_match,
0198         .count_cells = of_bus_pci_count_cells,
0199         .map = of_bus_simba_map,
0200         .get_flags = of_bus_pci_get_flags,
0201     },
0202     /* SBUS */
0203     {
0204         .name = "sbus",
0205         .addr_prop_name = "reg",
0206         .match = of_bus_sbus_match,
0207         .count_cells = of_bus_sbus_count_cells,
0208         .map = of_bus_default_map,
0209         .get_flags = of_bus_default_get_flags,
0210     },
0211     /* FHC */
0212     {
0213         .name = "fhc",
0214         .addr_prop_name = "reg",
0215         .match = of_bus_fhc_match,
0216         .count_cells = of_bus_fhc_count_cells,
0217         .map = of_bus_default_map,
0218         .get_flags = of_bus_default_get_flags,
0219     },
0220     /* Default */
0221     {
0222         .name = "default",
0223         .addr_prop_name = "reg",
0224         .match = NULL,
0225         .count_cells = of_bus_default_count_cells,
0226         .map = of_bus_default_map,
0227         .get_flags = of_bus_default_get_flags,
0228     },
0229 };
0230 
0231 static struct of_bus *of_match_bus(struct device_node *np)
0232 {
0233     int i;
0234 
0235     for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
0236         if (!of_busses[i].match || of_busses[i].match(np))
0237             return &of_busses[i];
0238     BUG();
0239     return NULL;
0240 }
0241 
0242 static int __init build_one_resource(struct device_node *parent,
0243                      struct of_bus *bus,
0244                      struct of_bus *pbus,
0245                      u32 *addr,
0246                      int na, int ns, int pna)
0247 {
0248     const u32 *ranges;
0249     int rone, rlen;
0250 
0251     ranges = of_get_property(parent, "ranges", &rlen);
0252     if (ranges == NULL || rlen == 0) {
0253         u32 result[OF_MAX_ADDR_CELLS];
0254         int i;
0255 
0256         memset(result, 0, pna * 4);
0257         for (i = 0; i < na; i++)
0258             result[pna - 1 - i] =
0259                 addr[na - 1 - i];
0260 
0261         memcpy(addr, result, pna * 4);
0262         return 0;
0263     }
0264 
0265     /* Now walk through the ranges */
0266     rlen /= 4;
0267     rone = na + pna + ns;
0268     for (; rlen >= rone; rlen -= rone, ranges += rone) {
0269         if (!bus->map(addr, ranges, na, ns, pna))
0270             return 0;
0271     }
0272 
0273     /* When we miss an I/O space match on PCI, just pass it up
0274      * to the next PCI bridge and/or controller.
0275      */
0276     if (!strcmp(bus->name, "pci") &&
0277         (addr[0] & 0x03000000) == 0x01000000)
0278         return 0;
0279 
0280     return 1;
0281 }
0282 
0283 static int __init use_1to1_mapping(struct device_node *pp)
0284 {
0285     /* If we have a ranges property in the parent, use it.  */
0286     if (of_find_property(pp, "ranges", NULL) != NULL)
0287         return 0;
0288 
0289     /* If the parent is the dma node of an ISA bus, pass
0290      * the translation up to the root.
0291      *
0292      * Some SBUS devices use intermediate nodes to express
0293      * hierarchy within the device itself.  These aren't
0294      * real bus nodes, and don't have a 'ranges' property.
0295      * But, we should still pass the translation work up
0296      * to the SBUS itself.
0297      */
0298     if (of_node_name_eq(pp, "dma") ||
0299         of_node_name_eq(pp, "espdma") ||
0300         of_node_name_eq(pp, "ledma") ||
0301         of_node_name_eq(pp, "lebuffer"))
0302         return 0;
0303 
0304     /* Similarly for all PCI bridges, if we get this far
0305      * it lacks a ranges property, and this will include
0306      * cases like Simba.
0307      */
0308     if (of_node_name_eq(pp, "pci"))
0309         return 0;
0310 
0311     return 1;
0312 }
0313 
0314 static int of_resource_verbose;
0315 
0316 static void __init build_device_resources(struct platform_device *op,
0317                       struct device *parent)
0318 {
0319     struct platform_device *p_op;
0320     struct of_bus *bus;
0321     int na, ns;
0322     int index, num_reg;
0323     const void *preg;
0324 
0325     if (!parent)
0326         return;
0327 
0328     p_op = to_platform_device(parent);
0329     bus = of_match_bus(p_op->dev.of_node);
0330     bus->count_cells(op->dev.of_node, &na, &ns);
0331 
0332     preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
0333     if (!preg || num_reg == 0)
0334         return;
0335 
0336     /* Convert to num-cells.  */
0337     num_reg /= 4;
0338 
0339     /* Convert to num-entries.  */
0340     num_reg /= na + ns;
0341 
0342     /* Prevent overrunning the op->resources[] array.  */
0343     if (num_reg > PROMREG_MAX) {
0344         printk(KERN_WARNING "%pOF: Too many regs (%d), "
0345                "limiting to %d.\n",
0346                op->dev.of_node, num_reg, PROMREG_MAX);
0347         num_reg = PROMREG_MAX;
0348     }
0349 
0350     op->resource = op->archdata.resource;
0351     op->num_resources = num_reg;
0352     for (index = 0; index < num_reg; index++) {
0353         struct resource *r = &op->resource[index];
0354         u32 addr[OF_MAX_ADDR_CELLS];
0355         const u32 *reg = (preg + (index * ((na + ns) * 4)));
0356         struct device_node *dp = op->dev.of_node;
0357         struct device_node *pp = p_op->dev.of_node;
0358         struct of_bus *pbus, *dbus;
0359         u64 size, result = OF_BAD_ADDR;
0360         unsigned long flags;
0361         int dna, dns;
0362         int pna, pns;
0363 
0364         size = of_read_addr(reg + na, ns);
0365         memcpy(addr, reg, na * 4);
0366 
0367         flags = bus->get_flags(addr, 0);
0368 
0369         if (use_1to1_mapping(pp)) {
0370             result = of_read_addr(addr, na);
0371             goto build_res;
0372         }
0373 
0374         dna = na;
0375         dns = ns;
0376         dbus = bus;
0377 
0378         while (1) {
0379             dp = pp;
0380             pp = dp->parent;
0381             if (!pp) {
0382                 result = of_read_addr(addr, dna);
0383                 break;
0384             }
0385 
0386             pbus = of_match_bus(pp);
0387             pbus->count_cells(dp, &pna, &pns);
0388 
0389             if (build_one_resource(dp, dbus, pbus, addr,
0390                            dna, dns, pna))
0391                 break;
0392 
0393             flags = pbus->get_flags(addr, flags);
0394 
0395             dna = pna;
0396             dns = pns;
0397             dbus = pbus;
0398         }
0399 
0400     build_res:
0401         memset(r, 0, sizeof(*r));
0402 
0403         if (of_resource_verbose)
0404             printk("%pOF reg[%d] -> %llx\n",
0405                    op->dev.of_node, index,
0406                    result);
0407 
0408         if (result != OF_BAD_ADDR) {
0409             if (tlb_type == hypervisor)
0410                 result &= 0x0fffffffffffffffUL;
0411 
0412             r->start = result;
0413             r->end = result + size - 1;
0414             r->flags = flags;
0415         }
0416         r->name = op->dev.of_node->full_name;
0417     }
0418 }
0419 
0420 static struct device_node * __init
0421 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
0422             const u32 *imap, int imlen, const u32 *imask,
0423             unsigned int *irq_p)
0424 {
0425     struct device_node *cp;
0426     unsigned int irq = *irq_p;
0427     struct of_bus *bus;
0428     phandle handle;
0429     const u32 *reg;
0430     int na, num_reg, i;
0431 
0432     bus = of_match_bus(pp);
0433     bus->count_cells(dp, &na, NULL);
0434 
0435     reg = of_get_property(dp, "reg", &num_reg);
0436     if (!reg || !num_reg)
0437         return NULL;
0438 
0439     imlen /= ((na + 3) * 4);
0440     handle = 0;
0441     for (i = 0; i < imlen; i++) {
0442         int j;
0443 
0444         for (j = 0; j < na; j++) {
0445             if ((reg[j] & imask[j]) != imap[j])
0446                 goto next;
0447         }
0448         if (imap[na] == irq) {
0449             handle = imap[na + 1];
0450             irq = imap[na + 2];
0451             break;
0452         }
0453 
0454     next:
0455         imap += (na + 3);
0456     }
0457     if (i == imlen) {
0458         /* Psycho and Sabre PCI controllers can have 'interrupt-map'
0459          * properties that do not include the on-board device
0460          * interrupts.  Instead, the device's 'interrupts' property
0461          * is already a fully specified INO value.
0462          *
0463          * Handle this by deciding that, if we didn't get a
0464          * match in the parent's 'interrupt-map', and the
0465          * parent is an IRQ translator, then use the parent as
0466          * our IRQ controller.
0467          */
0468         if (pp->irq_trans)
0469             return pp;
0470 
0471         return NULL;
0472     }
0473 
0474     *irq_p = irq;
0475     cp = of_find_node_by_phandle(handle);
0476 
0477     return cp;
0478 }
0479 
0480 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
0481                        struct device_node *pp,
0482                        unsigned int irq)
0483 {
0484     const struct linux_prom_pci_registers *regs;
0485     unsigned int bus, devfn, slot, ret;
0486 
0487     if (irq < 1 || irq > 4)
0488         return irq;
0489 
0490     regs = of_get_property(dp, "reg", NULL);
0491     if (!regs)
0492         return irq;
0493 
0494     bus = (regs->phys_hi >> 16) & 0xff;
0495     devfn = (regs->phys_hi >> 8) & 0xff;
0496     slot = (devfn >> 3) & 0x1f;
0497 
0498     if (pp->irq_trans) {
0499         /* Derived from Table 8-3, U2P User's Manual.  This branch
0500          * is handling a PCI controller that lacks a proper set of
0501          * interrupt-map and interrupt-map-mask properties.  The
0502          * Ultra-E450 is one example.
0503          *
0504          * The bit layout is BSSLL, where:
0505          * B: 0 on bus A, 1 on bus B
0506          * D: 2-bit slot number, derived from PCI device number as
0507          *    (dev - 1) for bus A, or (dev - 2) for bus B
0508          * L: 2-bit line number
0509          */
0510         if (bus & 0x80) {
0511             /* PBM-A */
0512             bus  = 0x00;
0513             slot = (slot - 1) << 2;
0514         } else {
0515             /* PBM-B */
0516             bus  = 0x10;
0517             slot = (slot - 2) << 2;
0518         }
0519         irq -= 1;
0520 
0521         ret = (bus | slot | irq);
0522     } else {
0523         /* Going through a PCI-PCI bridge that lacks a set of
0524          * interrupt-map and interrupt-map-mask properties.
0525          */
0526         ret = ((irq - 1 + (slot & 3)) & 3) + 1;
0527     }
0528 
0529     return ret;
0530 }
0531 
0532 static int of_irq_verbose;
0533 
0534 static unsigned int __init build_one_device_irq(struct platform_device *op,
0535                         struct device *parent,
0536                         unsigned int irq)
0537 {
0538     struct device_node *dp = op->dev.of_node;
0539     struct device_node *pp, *ip;
0540     unsigned int orig_irq = irq;
0541     int nid;
0542 
0543     if (irq == 0xffffffff)
0544         return irq;
0545 
0546     if (dp->irq_trans) {
0547         irq = dp->irq_trans->irq_build(dp, irq,
0548                            dp->irq_trans->data);
0549 
0550         if (of_irq_verbose)
0551             printk("%pOF: direct translate %x --> %x\n",
0552                    dp, orig_irq, irq);
0553 
0554         goto out;
0555     }
0556 
0557     /* Something more complicated.  Walk up to the root, applying
0558      * interrupt-map or bus specific translations, until we hit
0559      * an IRQ translator.
0560      *
0561      * If we hit a bus type or situation we cannot handle, we
0562      * stop and assume that the original IRQ number was in a
0563      * format which has special meaning to it's immediate parent.
0564      */
0565     pp = dp->parent;
0566     ip = NULL;
0567     while (pp) {
0568         const void *imap, *imsk;
0569         int imlen;
0570 
0571         imap = of_get_property(pp, "interrupt-map", &imlen);
0572         imsk = of_get_property(pp, "interrupt-map-mask", NULL);
0573         if (imap && imsk) {
0574             struct device_node *iret;
0575             int this_orig_irq = irq;
0576 
0577             iret = apply_interrupt_map(dp, pp,
0578                            imap, imlen, imsk,
0579                            &irq);
0580 
0581             if (of_irq_verbose)
0582                 printk("%pOF: Apply [%pOF:%x] imap --> [%pOF:%x]\n",
0583                        op->dev.of_node,
0584                        pp, this_orig_irq, iret, irq);
0585 
0586             if (!iret)
0587                 break;
0588 
0589             if (iret->irq_trans) {
0590                 ip = iret;
0591                 break;
0592             }
0593         } else {
0594             if (of_node_name_eq(pp, "pci")) {
0595                 unsigned int this_orig_irq = irq;
0596 
0597                 irq = pci_irq_swizzle(dp, pp, irq);
0598                 if (of_irq_verbose)
0599                     printk("%pOF: PCI swizzle [%pOF] "
0600                            "%x --> %x\n",
0601                            op->dev.of_node,
0602                            pp, this_orig_irq,
0603                            irq);
0604 
0605             }
0606 
0607             if (pp->irq_trans) {
0608                 ip = pp;
0609                 break;
0610             }
0611         }
0612         dp = pp;
0613         pp = pp->parent;
0614     }
0615     if (!ip)
0616         return orig_irq;
0617 
0618     irq = ip->irq_trans->irq_build(op->dev.of_node, irq,
0619                        ip->irq_trans->data);
0620     if (of_irq_verbose)
0621         printk("%pOF: Apply IRQ trans [%pOF] %x --> %x\n",
0622               op->dev.of_node, ip, orig_irq, irq);
0623 
0624 out:
0625     nid = of_node_to_nid(dp);
0626     if (nid != -1) {
0627         cpumask_t numa_mask;
0628 
0629         cpumask_copy(&numa_mask, cpumask_of_node(nid));
0630         irq_set_affinity(irq, &numa_mask);
0631     }
0632 
0633     return irq;
0634 }
0635 
0636 static struct platform_device * __init scan_one_device(struct device_node *dp,
0637                          struct device *parent)
0638 {
0639     struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
0640     const unsigned int *irq;
0641     struct dev_archdata *sd;
0642     int len, i;
0643 
0644     if (!op)
0645         return NULL;
0646 
0647     sd = &op->dev.archdata;
0648     sd->op = op;
0649 
0650     op->dev.of_node = dp;
0651 
0652     irq = of_get_property(dp, "interrupts", &len);
0653     if (irq) {
0654         op->archdata.num_irqs = len / 4;
0655 
0656         /* Prevent overrunning the op->irqs[] array.  */
0657         if (op->archdata.num_irqs > PROMINTR_MAX) {
0658             printk(KERN_WARNING "%pOF: Too many irqs (%d), "
0659                    "limiting to %d.\n",
0660                    dp, op->archdata.num_irqs, PROMINTR_MAX);
0661             op->archdata.num_irqs = PROMINTR_MAX;
0662         }
0663         memcpy(op->archdata.irqs, irq, op->archdata.num_irqs * 4);
0664     } else {
0665         op->archdata.num_irqs = 0;
0666     }
0667 
0668     build_device_resources(op, parent);
0669     for (i = 0; i < op->archdata.num_irqs; i++)
0670         op->archdata.irqs[i] = build_one_device_irq(op, parent, op->archdata.irqs[i]);
0671 
0672     op->dev.parent = parent;
0673     op->dev.bus = &platform_bus_type;
0674     if (!parent)
0675         dev_set_name(&op->dev, "root");
0676     else
0677         dev_set_name(&op->dev, "%08x", dp->phandle);
0678     op->dev.coherent_dma_mask = DMA_BIT_MASK(32);
0679     op->dev.dma_mask = &op->dev.coherent_dma_mask;
0680 
0681     if (of_device_register(op)) {
0682         printk("%pOF: Could not register of device.\n", dp);
0683         kfree(op);
0684         op = NULL;
0685     }
0686 
0687     return op;
0688 }
0689 
0690 static void __init scan_tree(struct device_node *dp, struct device *parent)
0691 {
0692     while (dp) {
0693         struct platform_device *op = scan_one_device(dp, parent);
0694 
0695         if (op)
0696             scan_tree(dp->child, &op->dev);
0697 
0698         dp = dp->sibling;
0699     }
0700 }
0701 
0702 static int __init scan_of_devices(void)
0703 {
0704     struct device_node *root = of_find_node_by_path("/");
0705     struct platform_device *parent;
0706 
0707     parent = scan_one_device(root, NULL);
0708     if (!parent)
0709         return 0;
0710 
0711     scan_tree(root->child, &parent->dev);
0712     return 0;
0713 }
0714 postcore_initcall(scan_of_devices);
0715 
0716 static int __init of_debug(char *str)
0717 {
0718     int val = 0;
0719 
0720     get_option(&str, &val);
0721     if (val & 1)
0722         of_resource_verbose = 1;
0723     if (val & 2)
0724         of_irq_verbose = 1;
0725     return 1;
0726 }
0727 
0728 __setup("of_debug=", of_debug);