From 29b635c00f3ebcdaf7a52c4948f6d948ad3757d3 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 16 May 2013 17:55:17 +0200 Subject: of/pci: Provide support for parsing PCI DT ranges property This patch factors out common implementation patterns to reduce overall kernel code and provide a means for host bridge drivers to directly obtain struct resources from the DT's ranges property without relying on architecture specific DT handling. This will make it easier to write archiecture independent host bridge drivers and mitigate against further duplication of DT parsing code. This patch can be used in the following way: struct of_pci_range_parser parser; struct of_pci_range range; if (of_pci_range_parser_init(&parser, np)) ; //no ranges property for_each_of_pci_range(&parser, &range) { /* directly access properties of the address range, e.g.: range.pci_space, range.pci_addr, range.cpu_addr, range.size, range.flags alternatively obtain a struct resource, e.g.: struct resource res; of_pci_range_to_resource(&range, np, &res); */ } Additionally the implementation takes care of adjacent ranges and merges them into a single range (as was the case with powerpc and microblaze). Signed-off-by: Andrew Murray Signed-off-by: Liviu Dudau Signed-off-by: Thomas Petazzoni Reviewed-by: Rob Herring Tested-by: Thomas Petazzoni Tested-by: Linus Walleij Tested-by: Jingoo Han Acked-by: Grant Likely Signed-off-by: Jason Cooper --- drivers/of/address.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'drivers/of') diff --git a/drivers/of/address.c b/drivers/of/address.c index 04da786c84d2..fdd0636a987d 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -227,6 +227,73 @@ int of_pci_address_to_resource(struct device_node *dev, int bar, return __of_address_to_resource(dev, addrp, size, flags, NULL, r); } EXPORT_SYMBOL_GPL(of_pci_address_to_resource); + +int of_pci_range_parser_init(struct of_pci_range_parser *parser, + struct device_node *node) +{ + const int na = 3, ns = 2; + int rlen; + + parser->node = node; + parser->pna = of_n_addr_cells(node); + parser->np = parser->pna + na + ns; + + parser->range = of_get_property(node, "ranges", &rlen); + if (parser->range == NULL) + return -ENOENT; + + parser->end = parser->range + rlen / sizeof(__be32); + + return 0; +} +EXPORT_SYMBOL_GPL(of_pci_range_parser_init); + +struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser, + struct of_pci_range *range) +{ + const int na = 3, ns = 2; + + if (!range) + return NULL; + + if (!parser->range || parser->range + parser->np > parser->end) + return NULL; + + range->pci_space = parser->range[0]; + range->flags = of_bus_pci_get_flags(parser->range); + range->pci_addr = of_read_number(parser->range + 1, ns); + range->cpu_addr = of_translate_address(parser->node, + parser->range + na); + range->size = of_read_number(parser->range + parser->pna + na, ns); + + parser->range += parser->np; + + /* Now consume following elements while they are contiguous */ + while (parser->range + parser->np <= parser->end) { + u32 flags, pci_space; + u64 pci_addr, cpu_addr, size; + + pci_space = be32_to_cpup(parser->range); + flags = of_bus_pci_get_flags(parser->range); + pci_addr = of_read_number(parser->range + 1, ns); + cpu_addr = of_translate_address(parser->node, + parser->range + na); + size = of_read_number(parser->range + parser->pna + na, ns); + + if (flags != range->flags) + break; + if (pci_addr != range->pci_addr + range->size || + cpu_addr != range->cpu_addr + range->size) + break; + + range->size += size; + parser->range += parser->np; + } + + return range; +} +EXPORT_SYMBOL_GPL(of_pci_range_parser_one); + #endif /* CONFIG_PCI */ /* -- cgit v1.2.3-59-g8ed1b From 45ab9702fb47d18dca116b3a0509efa19fbcb27a Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Thu, 16 May 2013 17:55:18 +0200 Subject: of/pci: Add of_pci_get_devfn() function This function can be used to parse the device and function number from a standard 5-cell PCI resource. PCI_SLOT() and PCI_FUNC() can be used on the returned value obtain the device and function numbers respectively. Signed-off-by: Thierry Reding Signed-off-by: Thomas Petazzoni Signed-off-by: Jason Cooper --- drivers/of/of_pci.c | 34 +++++++++++++++++++++++++++++----- include/linux/of_pci.h | 1 + 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'drivers/of') diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c index 13e37e2d8ec1..4dd7b9b80076 100644 --- a/drivers/of/of_pci.c +++ b/drivers/of/of_pci.c @@ -5,14 +5,15 @@ #include static inline int __of_pci_pci_compare(struct device_node *node, - unsigned int devfn) + unsigned int data) { - unsigned int size; - const __be32 *reg = of_get_property(node, "reg", &size); + int devfn; - if (!reg || size < 5 * sizeof(__be32)) + devfn = of_pci_get_devfn(node); + if (devfn < 0) return 0; - return ((be32_to_cpup(®[0]) >> 8) & 0xff) == devfn; + + return devfn == data; } struct device_node *of_pci_find_child_device(struct device_node *parent, @@ -40,3 +41,26 @@ struct device_node *of_pci_find_child_device(struct device_node *parent, return NULL; } EXPORT_SYMBOL_GPL(of_pci_find_child_device); + +/** + * of_pci_get_devfn() - Get device and function numbers for a device node + * @np: device node + * + * Parses a standard 5-cell PCI resource and returns an 8-bit value that can + * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device + * and function numbers respectively. On error a negative error code is + * returned. + */ +int of_pci_get_devfn(struct device_node *np) +{ + unsigned int size; + const __be32 *reg; + + reg = of_get_property(np, "reg", &size); + + if (!reg || size < 5 * sizeof(__be32)) + return -EINVAL; + + return (be32_to_cpup(reg) >> 8) & 0xff; +} +EXPORT_SYMBOL_GPL(of_pci_get_devfn); diff --git a/include/linux/of_pci.h b/include/linux/of_pci.h index bb115deb7612..91ec484e0045 100644 --- a/include/linux/of_pci.h +++ b/include/linux/of_pci.h @@ -10,5 +10,6 @@ int of_irq_map_pci(const struct pci_dev *pdev, struct of_irq *out_irq); struct device_node; struct device_node *of_pci_find_child_device(struct device_node *parent, unsigned int devfn); +int of_pci_get_devfn(struct device_node *np); #endif -- cgit v1.2.3-59-g8ed1b From 4e23d3f505e8acfeac7cc33d4113fbb5a25c3090 Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Thu, 16 May 2013 17:55:19 +0200 Subject: of/pci: Add of_pci_parse_bus_range() function This function can be used to parse a bus-range property as specified by device nodes representing PCI bridges. Signed-off-by: Thierry Reding Signed-off-by: Jason Cooper --- drivers/of/of_pci.c | 25 +++++++++++++++++++++++++ include/linux/of_pci.h | 1 + 2 files changed, 26 insertions(+) (limited to 'drivers/of') diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c index 4dd7b9b80076..42c687a820ac 100644 --- a/drivers/of/of_pci.c +++ b/drivers/of/of_pci.c @@ -64,3 +64,28 @@ int of_pci_get_devfn(struct device_node *np) return (be32_to_cpup(reg) >> 8) & 0xff; } EXPORT_SYMBOL_GPL(of_pci_get_devfn); + +/** + * of_pci_parse_bus_range() - parse the bus-range property of a PCI device + * @node: device node + * @res: address to a struct resource to return the bus-range + * + * Returns 0 on success or a negative error-code on failure. + */ +int of_pci_parse_bus_range(struct device_node *node, struct resource *res) +{ + const __be32 *values; + int len; + + values = of_get_property(node, "bus-range", &len); + if (!values || len < sizeof(*values) * 2) + return -EINVAL; + + res->name = node->name; + res->start = be32_to_cpup(values++); + res->end = be32_to_cpup(values); + res->flags = IORESOURCE_BUS; + + return 0; +} +EXPORT_SYMBOL_GPL(of_pci_parse_bus_range); diff --git a/include/linux/of_pci.h b/include/linux/of_pci.h index 91ec484e0045..7a04826018c0 100644 --- a/include/linux/of_pci.h +++ b/include/linux/of_pci.h @@ -11,5 +11,6 @@ struct device_node; struct device_node *of_pci_find_child_device(struct device_node *parent, unsigned int devfn); int of_pci_get_devfn(struct device_node *np); +int of_pci_parse_bus_range(struct device_node *node, struct resource *res); #endif -- cgit v1.2.3-59-g8ed1b