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/init.h>
0006 #include <linux/mod_devicetable.h>
0007 #include <linux/slab.h>
0008 #include <linux/errno.h>
0009 #include <linux/irq.h>
0010 #include <linux/of_device.h>
0011 #include <linux/of_platform.h>
0012 #include <linux/dma-mapping.h>
0013 #include <asm/leon.h>
0014 #include <asm/leon_amba.h>
0015 
0016 #include "of_device_common.h"
0017 #include "irq.h"
0018 
0019 /*
0020  * PCI bus specific translator
0021  */
0022 
0023 static int of_bus_pci_match(struct device_node *np)
0024 {
0025     if (of_node_is_type(np, "pci") || of_node_is_type(np, "pciex")) {
0026         /* Do not do PCI specific frobbing if the
0027          * PCI bridge lacks a ranges property.  We
0028          * want to pass it through up to the next
0029          * parent as-is, not with the PCI translate
0030          * method which chops off the top address cell.
0031          */
0032         if (!of_find_property(np, "ranges", NULL))
0033             return 0;
0034 
0035         return 1;
0036     }
0037 
0038     return 0;
0039 }
0040 
0041 static void of_bus_pci_count_cells(struct device_node *np,
0042                    int *addrc, int *sizec)
0043 {
0044     if (addrc)
0045         *addrc = 3;
0046     if (sizec)
0047         *sizec = 2;
0048 }
0049 
0050 static int of_bus_pci_map(u32 *addr, const u32 *range,
0051               int na, int ns, int pna)
0052 {
0053     u32 result[OF_MAX_ADDR_CELLS];
0054     int i;
0055 
0056     /* Check address type match */
0057     if ((addr[0] ^ range[0]) & 0x03000000)
0058         return -EINVAL;
0059 
0060     if (of_out_of_range(addr + 1, range + 1, range + na + pna,
0061                 na - 1, ns))
0062         return -EINVAL;
0063 
0064     /* Start with the parent range base.  */
0065     memcpy(result, range + na, pna * 4);
0066 
0067     /* Add in the child address offset, skipping high cell.  */
0068     for (i = 0; i < na - 1; i++)
0069         result[pna - 1 - i] +=
0070             (addr[na - 1 - i] -
0071              range[na - 1 - i]);
0072 
0073     memcpy(addr, result, pna * 4);
0074 
0075     return 0;
0076 }
0077 
0078 static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
0079 {
0080     u32 w = addr[0];
0081 
0082     /* For PCI, we override whatever child busses may have used.  */
0083     flags = 0;
0084     switch((w >> 24) & 0x03) {
0085     case 0x01:
0086         flags |= IORESOURCE_IO;
0087         break;
0088 
0089     case 0x02: /* 32 bits */
0090     case 0x03: /* 64 bits */
0091         flags |= IORESOURCE_MEM;
0092         break;
0093     }
0094     if (w & 0x40000000)
0095         flags |= IORESOURCE_PREFETCH;
0096     return flags;
0097 }
0098 
0099 static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
0100 {
0101     return IORESOURCE_MEM;
0102 }
0103 
0104  /*
0105  * AMBAPP bus specific translator
0106  */
0107 
0108 static int of_bus_ambapp_match(struct device_node *np)
0109 {
0110     return of_node_is_type(np, "ambapp");
0111 }
0112 
0113 static void of_bus_ambapp_count_cells(struct device_node *child,
0114                       int *addrc, int *sizec)
0115 {
0116     if (addrc)
0117         *addrc = 1;
0118     if (sizec)
0119         *sizec = 1;
0120 }
0121 
0122 static int of_bus_ambapp_map(u32 *addr, const u32 *range,
0123                  int na, int ns, int pna)
0124 {
0125     return of_bus_default_map(addr, range, na, ns, pna);
0126 }
0127 
0128 static unsigned long of_bus_ambapp_get_flags(const u32 *addr,
0129                          unsigned long flags)
0130 {
0131     return IORESOURCE_MEM;
0132 }
0133 
0134 /*
0135  * Array of bus specific translators
0136  */
0137 
0138 static struct of_bus of_busses[] = {
0139     /* PCI */
0140     {
0141         .name = "pci",
0142         .addr_prop_name = "assigned-addresses",
0143         .match = of_bus_pci_match,
0144         .count_cells = of_bus_pci_count_cells,
0145         .map = of_bus_pci_map,
0146         .get_flags = of_bus_pci_get_flags,
0147     },
0148     /* SBUS */
0149     {
0150         .name = "sbus",
0151         .addr_prop_name = "reg",
0152         .match = of_bus_sbus_match,
0153         .count_cells = of_bus_sbus_count_cells,
0154         .map = of_bus_default_map,
0155         .get_flags = of_bus_sbus_get_flags,
0156     },
0157     /* AMBA */
0158     {
0159         .name = "ambapp",
0160         .addr_prop_name = "reg",
0161         .match = of_bus_ambapp_match,
0162         .count_cells = of_bus_ambapp_count_cells,
0163         .map = of_bus_ambapp_map,
0164         .get_flags = of_bus_ambapp_get_flags,
0165     },
0166     /* Default */
0167     {
0168         .name = "default",
0169         .addr_prop_name = "reg",
0170         .match = NULL,
0171         .count_cells = of_bus_default_count_cells,
0172         .map = of_bus_default_map,
0173         .get_flags = of_bus_default_get_flags,
0174     },
0175 };
0176 
0177 static struct of_bus *of_match_bus(struct device_node *np)
0178 {
0179     int i;
0180 
0181     for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
0182         if (!of_busses[i].match || of_busses[i].match(np))
0183             return &of_busses[i];
0184     BUG();
0185     return NULL;
0186 }
0187 
0188 static int __init build_one_resource(struct device_node *parent,
0189                      struct of_bus *bus,
0190                      struct of_bus *pbus,
0191                      u32 *addr,
0192                      int na, int ns, int pna)
0193 {
0194     const u32 *ranges;
0195     unsigned int rlen;
0196     int rone;
0197 
0198     ranges = of_get_property(parent, "ranges", &rlen);
0199     if (ranges == NULL || rlen == 0) {
0200         u32 result[OF_MAX_ADDR_CELLS];
0201         int i;
0202 
0203         memset(result, 0, pna * 4);
0204         for (i = 0; i < na; i++)
0205             result[pna - 1 - i] =
0206                 addr[na - 1 - i];
0207 
0208         memcpy(addr, result, pna * 4);
0209         return 0;
0210     }
0211 
0212     /* Now walk through the ranges */
0213     rlen /= 4;
0214     rone = na + pna + ns;
0215     for (; rlen >= rone; rlen -= rone, ranges += rone) {
0216         if (!bus->map(addr, ranges, na, ns, pna))
0217             return 0;
0218     }
0219 
0220     return 1;
0221 }
0222 
0223 static int __init use_1to1_mapping(struct device_node *pp)
0224 {
0225     /* If we have a ranges property in the parent, use it.  */
0226     if (of_find_property(pp, "ranges", NULL) != NULL)
0227         return 0;
0228 
0229     /* Some SBUS devices use intermediate nodes to express
0230      * hierarchy within the device itself.  These aren't
0231      * real bus nodes, and don't have a 'ranges' property.
0232      * But, we should still pass the translation work up
0233      * to the SBUS itself.
0234      */
0235     if (of_node_name_eq(pp, "dma") ||
0236         of_node_name_eq(pp, "espdma") ||
0237         of_node_name_eq(pp, "ledma") ||
0238         of_node_name_eq(pp, "lebuffer"))
0239         return 0;
0240 
0241     return 1;
0242 }
0243 
0244 static int of_resource_verbose;
0245 
0246 static void __init build_device_resources(struct platform_device *op,
0247                       struct device *parent)
0248 {
0249     struct platform_device *p_op;
0250     struct of_bus *bus;
0251     int na, ns;
0252     int index, num_reg;
0253     const void *preg;
0254 
0255     if (!parent)
0256         return;
0257 
0258     p_op = to_platform_device(parent);
0259     bus = of_match_bus(p_op->dev.of_node);
0260     bus->count_cells(op->dev.of_node, &na, &ns);
0261 
0262     preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
0263     if (!preg || num_reg == 0)
0264         return;
0265 
0266     /* Convert to num-cells.  */
0267     num_reg /= 4;
0268 
0269     /* Conver to num-entries.  */
0270     num_reg /= na + ns;
0271 
0272     op->resource = op->archdata.resource;
0273     op->num_resources = num_reg;
0274     for (index = 0; index < num_reg; index++) {
0275         struct resource *r = &op->resource[index];
0276         u32 addr[OF_MAX_ADDR_CELLS];
0277         const u32 *reg = (preg + (index * ((na + ns) * 4)));
0278         struct device_node *dp = op->dev.of_node;
0279         struct device_node *pp = p_op->dev.of_node;
0280         struct of_bus *pbus, *dbus;
0281         u64 size, result = OF_BAD_ADDR;
0282         unsigned long flags;
0283         int dna, dns;
0284         int pna, pns;
0285 
0286         size = of_read_addr(reg + na, ns);
0287 
0288         memcpy(addr, reg, na * 4);
0289 
0290         flags = bus->get_flags(reg, 0);
0291 
0292         if (use_1to1_mapping(pp)) {
0293             result = of_read_addr(addr, na);
0294             goto build_res;
0295         }
0296 
0297         dna = na;
0298         dns = ns;
0299         dbus = bus;
0300 
0301         while (1) {
0302             dp = pp;
0303             pp = dp->parent;
0304             if (!pp) {
0305                 result = of_read_addr(addr, dna);
0306                 break;
0307             }
0308 
0309             pbus = of_match_bus(pp);
0310             pbus->count_cells(dp, &pna, &pns);
0311 
0312             if (build_one_resource(dp, dbus, pbus, addr,
0313                            dna, dns, pna))
0314                 break;
0315 
0316             flags = pbus->get_flags(addr, flags);
0317 
0318             dna = pna;
0319             dns = pns;
0320             dbus = pbus;
0321         }
0322 
0323     build_res:
0324         memset(r, 0, sizeof(*r));
0325 
0326         if (of_resource_verbose)
0327             printk("%pOF reg[%d] -> %llx\n",
0328                    op->dev.of_node, index,
0329                    result);
0330 
0331         if (result != OF_BAD_ADDR) {
0332             r->start = result & 0xffffffff;
0333             r->end = result + size - 1;
0334             r->flags = flags | ((result >> 32ULL) & 0xffUL);
0335         }
0336         r->name = op->dev.of_node->full_name;
0337     }
0338 }
0339 
0340 static struct platform_device * __init scan_one_device(struct device_node *dp,
0341                          struct device *parent)
0342 {
0343     struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
0344     const struct linux_prom_irqs *intr;
0345     struct dev_archdata *sd;
0346     int len, i;
0347 
0348     if (!op)
0349         return NULL;
0350 
0351     sd = &op->dev.archdata;
0352     sd->op = op;
0353 
0354     op->dev.of_node = dp;
0355 
0356     intr = of_get_property(dp, "intr", &len);
0357     if (intr) {
0358         op->archdata.num_irqs = len / sizeof(struct linux_prom_irqs);
0359         for (i = 0; i < op->archdata.num_irqs; i++)
0360             op->archdata.irqs[i] =
0361                 sparc_config.build_device_irq(op, intr[i].pri);
0362     } else {
0363         const unsigned int *irq =
0364             of_get_property(dp, "interrupts", &len);
0365 
0366         if (irq) {
0367             op->archdata.num_irqs = len / sizeof(unsigned int);
0368             for (i = 0; i < op->archdata.num_irqs; i++)
0369                 op->archdata.irqs[i] =
0370                     sparc_config.build_device_irq(op, irq[i]);
0371         } else {
0372             op->archdata.num_irqs = 0;
0373         }
0374     }
0375 
0376     build_device_resources(op, parent);
0377 
0378     op->dev.parent = parent;
0379     op->dev.bus = &platform_bus_type;
0380     if (!parent)
0381         dev_set_name(&op->dev, "root");
0382     else
0383         dev_set_name(&op->dev, "%08x", dp->phandle);
0384 
0385     op->dev.coherent_dma_mask = DMA_BIT_MASK(32);
0386     op->dev.dma_mask = &op->dev.coherent_dma_mask;
0387 
0388     if (of_device_register(op)) {
0389         printk("%pOF: Could not register of device.\n", dp);
0390         kfree(op);
0391         op = NULL;
0392     }
0393 
0394     return op;
0395 }
0396 
0397 static void __init scan_tree(struct device_node *dp, struct device *parent)
0398 {
0399     while (dp) {
0400         struct platform_device *op = scan_one_device(dp, parent);
0401 
0402         if (op)
0403             scan_tree(dp->child, &op->dev);
0404 
0405         dp = dp->sibling;
0406     }
0407 }
0408 
0409 static int __init scan_of_devices(void)
0410 {
0411     struct device_node *root = of_find_node_by_path("/");
0412     struct platform_device *parent;
0413 
0414     parent = scan_one_device(root, NULL);
0415     if (!parent)
0416         return 0;
0417 
0418     scan_tree(root->child, &parent->dev);
0419     return 0;
0420 }
0421 postcore_initcall(scan_of_devices);
0422 
0423 static int __init of_debug(char *str)
0424 {
0425     int val = 0;
0426 
0427     get_option(&str, &val);
0428     if (val & 1)
0429         of_resource_verbose = 1;
0430     return 1;
0431 }
0432 
0433 __setup("of_debug=", of_debug);