From patchwork Thu Jul 26 19:55:11 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 1244451 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 539BA3FDD9 for ; Thu, 26 Jul 2012 19:55:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752599Ab2GZTz1 (ORCPT ); Thu, 26 Jul 2012 15:55:27 -0400 Received: from moutng.kundenserver.de ([212.227.126.187]:55862 "EHLO moutng.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752543Ab2GZTzZ (ORCPT ); Thu, 26 Jul 2012 15:55:25 -0400 Received: from mailbox.adnet.avionic-design.de (mailbox.avionic-design.de [109.75.18.3]) by mrelayeu.kundenserver.de (node=mreu3) with ESMTP (Nemesis) id 0LhYH7-1TXeaQ2NYf-00mr1o; Thu, 26 Jul 2012 21:55:23 +0200 Received: from localhost (localhost [127.0.0.1]) by mailbox.adnet.avionic-design.de (Postfix) with ESMTP id F1A652A282E9; Thu, 26 Jul 2012 21:55:22 +0200 (CEST) X-Virus-Scanned: amavisd-new at avionic-design.de Received: from mailbox.adnet.avionic-design.de ([127.0.0.1]) by localhost (mailbox.avionic-design.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aIzqFzu9nOM5; Thu, 26 Jul 2012 21:55:21 +0200 (CEST) Received: from localhost (avionic-0098.adnet.avionic-design.de [172.20.31.233]) (Authenticated sender: thierry.reding) by mailbox.adnet.avionic-design.de (Postfix) with ESMTPA id 7FE8F2A28161; Thu, 26 Jul 2012 21:55:20 +0200 (CEST) From: Thierry Reding To: linux-tegra@vger.kernel.org Cc: Bjorn Helgaas , linux-pci@vger.kernel.org, Grant Likely , Rob Herring , devicetree-discuss@lists.ozlabs.org, Russell King , linux-arm-kernel@lists.infradead.org, Colin Cross , Olof Johansson , Stephen Warren , Mitch Bradley , Arnd Bergmann Subject: [PATCH v3 09/10] of: Add of_pci_parse_ranges() Date: Thu, 26 Jul 2012 21:55:11 +0200 Message-Id: <1343332512-28762-10-git-send-email-thierry.reding@avionic-design.de> X-Mailer: git-send-email 1.7.11.2 In-Reply-To: <1343332512-28762-1-git-send-email-thierry.reding@avionic-design.de> References: <1343332512-28762-1-git-send-email-thierry.reding@avionic-design.de> X-Provags-ID: V02:K0:xRQHqgHCfxWaHRAqeDSQ4DyJmPJEa3tQKtDwfTzlPmx SmKf2FNM5Mivrg9mWyqcOoLKVYXw9Q+tUPoLyzm2nBLMpNOkXL 93aFLA0j+Ul8mvjV6v1C5EScip3Ff6G8BW2mvKsCEwZkGgDjc5 ElexORyTZkOMf1n4vkvO5CFiJ444IvCnZm3pst9Qnf7V3zEImH XP9QlCe5HUL2wmysPIuNTmfdFIV5b0V+dceTis5U7xc32//f87 RljQNc/BmGjDCC6xcKr/nroSsi4MRZrzfpgvUFlBwNy4yrplkm D9BTqPHMsqbm2uPfkO5c0l5oeynMPRuXuqaGaiwxw9Oc0YB6xU vKgQtwgbdvGnFihMQ3YAx2blzBfGH3hRXV4CLn3FOyTSY1Qoub FFyVC4eVWiUcigKVKqJ+4IEKxMu88u/hnzxgYn4QqF5vSTzJVG 6yxrA Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org This new function parses the ranges property of PCI device nodes into an array of struct resource elements. It is useful in multiple-port PCI host controller drivers to collect information about the ranges that it needs to forward to the respective ports. Signed-off-by: Thierry Reding --- Changes in v3: - new patch drivers/of/of_pci.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/of_pci.h | 2 ++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c index 13e37e2..bcff301 100644 --- a/drivers/of/of_pci.c +++ b/drivers/of/of_pci.c @@ -1,7 +1,8 @@ #include #include -#include +#include #include +#include #include static inline int __of_pci_pci_compare(struct device_node *node, @@ -40,3 +41,84 @@ struct device_node *of_pci_find_child_device(struct device_node *parent, return NULL; } EXPORT_SYMBOL_GPL(of_pci_find_child_device); + +struct resource *of_pci_parse_ranges(struct device_node *node, + unsigned int *countp) +{ + unsigned int count, i = 0; + struct resource *ranges; + const __be32 *values; + int len, pna, np; + + values = of_get_property(node, "ranges", &len); + if (!values) + return NULL; + + pna = of_n_addr_cells(node); + np = pna + 5; + + count = len / (np * sizeof(*values)); + + ranges = kzalloc(sizeof(*ranges) * count, GFP_KERNEL); + if (!ranges) + return NULL; + + pr_debug("PCI ranges:\n"); + + while ((len -= np * sizeof(*values)) >= 0) { + u64 addr = of_translate_address(node, values + 3); + u64 size = of_read_number(values + 3 + pna, 2); + u32 type = be32_to_cpup(values); + const char *suffix = ""; + struct resource range; + + memset(&range, 0, sizeof(range)); + range.start = addr; + range.end = addr + size - 1; + + switch ((type >> 24) & 0x3) { + case 0: + range.flags = IORESOURCE_MEM | IORESOURCE_PCI_CS; + pr_debug(" CS %#x-%#x\n", range.start, range.end); + break; + + case 1: + range.flags = IORESOURCE_IO; + pr_debug(" IO %#x-%#x\n", range.start, range.end); + break; + + case 2: + range.flags = IORESOURCE_MEM; + + if (type & 0x40000000) { + range.flags |= IORESOURCE_PREFETCH; + suffix = "prefetch"; + } + + pr_debug(" MEM %#x-%#x %s\n", range.start, range.end, + suffix); + break; + + case 3: + range.flags = IORESOURCE_MEM | IORESOURCE_MEM_64; + + if (type & 0x40000000) { + range.flags |= IORESOURCE_PREFETCH; + suffix = "prefetch"; + } + + pr_debug(" MEM %#x-%#x 64-bit %s\n", range.start, + range.end, suffix); + break; + } + + memcpy(&ranges[i++], &range, sizeof(range)); + values += np; + } + + if (countp) + *countp = count; + + return ranges; +} +EXPORT_SYMBOL_GPL(of_pci_parse_ranges); diff --git a/include/linux/of_pci.h b/include/linux/of_pci.h index bb115de..c0db6ea 100644 --- a/include/linux/of_pci.h +++ b/include/linux/of_pci.h @@ -10,5 +10,7 @@ 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); +struct resource *of_pci_parse_ranges(struct device_node *node, + unsigned int *countp); #endif