Received: by 2002:ac8:156:0:b0:3e0:cd10:60c8 with SMTP id f22csp273238qtg; Fri, 31 Mar 2023 01:07:44 -0700 (PDT) X-Google-Smtp-Source: AKy350Z1Raj9yYz0i0OJPTTjk/ktivGfuFHOkB57rfhvsjQHC6xRafVrp/5cXbSTOXHMU5y/ko8a X-Received: by 2002:a17:902:da88:b0:19d:2a3:f019 with SMTP id j8-20020a170902da8800b0019d02a3f019mr5432689plx.1.1680250064118; Fri, 31 Mar 2023 01:07:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680250064; cv=none; d=google.com; s=arc-20160816; b=Cy7X4nY1PW316XsxkBP39T92q4QV2k4cFt+BVTx7a9KYXOEOgHzAoVJ+9efpsrB4Ob B2m9L3LxizHNZo9bHRfGkwaK2Z3TctMHVvQ4g5uu8FhQFtx7iN0OuVvltJCONSWvqXco QR2hZKN/nVlGPzDGhuGBYGd+ZsCQ/23h+UTLz1td9/ypvHWoG5W/CM3J4xlD/HvHkj/Q FDjU2KnOYB0fiIWHa6GdxiKYeNSVmRTA10Iq7b/fpak3RM5zVFJjVBPpnkEkzmM2ZHc6 0PmD3ckrQwpq25ec6Z+/5C1R9030VXpbUli4EoArDvZ+vMV9E/Vc0mhBBeg4/qmGaK3h idjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=LuI3F16paXXw8E9wTjLyHAutoC8GagQYwnyWrGBkmVo=; b=Mgl+xr8byckx2ZIld8IGh9Mn/W2NA3486ZOvKlTaaDMnx7IxpgK4heYoDzpHvUo3hY yjVjfGPmwQtJH8uNq9ZH+FL7jzkU+j94oQiIhhCIZGzCR7Iz6VRtoSq1jPVd/4LZsyvL R/eEuzYHpOwiG3R0OlJx77d+OPMvy02pVbCiNyUsPgp8CU9KOQ5GQy/SZqNIGHuWPi35 yK488US1tF23lUhyE9e93G0jzYpycUtPBqXcBBZw5TGT+Ku4zNmIjKYs9+rXwQJg1UuZ csDkOwTDnDKJjSzQxWfkjhSDgF3MZBX+zLKhqKH47I3qZpoOV3tIDIPcnD+N3pHvNJZU tkkA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w9-20020a170902a70900b00194bdcf1aafsi1493951plq.541.2023.03.31.01.07.31; Fri, 31 Mar 2023 01:07:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231325AbjCaHxq (ORCPT + 99 others); Fri, 31 Mar 2023 03:53:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231165AbjCaHxN (ORCPT ); Fri, 31 Mar 2023 03:53:13 -0400 Received: from laurent.telenet-ops.be (laurent.telenet-ops.be [IPv6:2a02:1800:110:4::f00:19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42915191C1 for ; Fri, 31 Mar 2023 00:52:38 -0700 (PDT) Received: from ramsan.of.borg ([84.195.187.55]) by laurent.telenet-ops.be with bizsmtp id evsc290061C8whw01vsci5; Fri, 31 Mar 2023 09:52:36 +0200 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtp (Exim 4.95) (envelope-from ) id 1pi9Y6-00FTC2-IG; Fri, 31 Mar 2023 09:52:36 +0200 Received: from geert by rox.of.borg with local (Exim 4.95) (envelope-from ) id 1pi9Yp-008Ra7-U0; Fri, 31 Mar 2023 09:52:35 +0200 From: Geert Uytterhoeven To: Rob Herring , Frank Rowand Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH 3/3] of: address: Reshuffle to remove forward declarations Date: Fri, 31 Mar 2023 09:52:30 +0200 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.4 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,T_FILL_THIS_FORM_SHORT autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reshuffle the code to get rid of the forward declarations, which improves readability. Signed-off-by: Geert Uytterhoeven --- drivers/of/address.c | 271 +++++++++++++++++++++---------------------- 1 file changed, 133 insertions(+), 138 deletions(-) diff --git a/drivers/of/address.c b/drivers/of/address.c index bd965d104c83a1ac..80b36dfdb69c24c8 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -24,11 +24,6 @@ #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS) #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0) -static struct of_bus *of_match_bus(struct device_node *np); -static int __of_address_to_resource(struct device_node *dev, int index, - int bar_no, struct resource *r); -static bool of_mmio_is_nonposted(struct device_node *np); - /* Debug utility */ #ifdef DEBUG static void of_dump_addr(const char *s, const __be32 *addr, int na) @@ -198,17 +193,6 @@ static int of_bus_pci_translate(__be32 *addr, u64 offset, int na) } #endif /* CONFIG_PCI */ -int of_pci_address_to_resource(struct device_node *dev, int bar, - struct resource *r) -{ - - if (!IS_ENABLED(CONFIG_PCI)) - return -ENOSYS; - - return __of_address_to_resource(dev, -1, bar, r); -} -EXPORT_SYMBOL_GPL(of_pci_address_to_resource); - /* * of_pci_range_to_resource - Create a resource from an of_pci_range * @range: the PCI range that describes the resource @@ -837,128 +821,6 @@ static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr, return port; } -static int __of_address_to_resource(struct device_node *dev, int index, int bar_no, - struct resource *r) -{ - u64 taddr; - const __be32 *addrp; - u64 size; - unsigned int flags; - const char *name = NULL; - - addrp = __of_get_address(dev, index, bar_no, &size, &flags); - if (addrp == NULL) - return -EINVAL; - - /* Get optional "reg-names" property to add a name to a resource */ - if (index >= 0) - of_property_read_string_index(dev, "reg-names", index, &name); - - if (flags & IORESOURCE_MEM) - taddr = of_translate_address(dev, addrp); - else if (flags & IORESOURCE_IO) - taddr = of_translate_ioport(dev, addrp, size); - else - return -EINVAL; - - if (taddr == OF_BAD_ADDR) - return -EINVAL; - memset(r, 0, sizeof(struct resource)); - - if (of_mmio_is_nonposted(dev)) - flags |= IORESOURCE_MEM_NONPOSTED; - - r->start = taddr; - r->end = taddr + size - 1; - r->flags = flags; - r->name = name ? name : dev->full_name; - - return 0; -} - -/** - * of_address_to_resource - Translate device tree address and return as resource - * @dev: Caller's Device Node - * @index: Index into the array - * @r: Pointer to resource array - * - * Returns -EINVAL if the range cannot be converted to resource. - * - * Note that if your address is a PIO address, the conversion will fail if - * the physical address can't be internally converted to an IO token with - * pci_address_to_pio(), that is because it's either called too early or it - * can't be matched to any host bridge IO space - */ -int of_address_to_resource(struct device_node *dev, int index, - struct resource *r) -{ - return __of_address_to_resource(dev, index, -1, r); -} -EXPORT_SYMBOL_GPL(of_address_to_resource); - -/** - * of_iomap - Maps the memory mapped IO for a given device_node - * @np: the device whose io range will be mapped - * @index: index of the io range - * - * Returns a pointer to the mapped memory - */ -void __iomem *of_iomap(struct device_node *np, int index) -{ - struct resource res; - - if (of_address_to_resource(np, index, &res)) - return NULL; - - if (res.flags & IORESOURCE_MEM_NONPOSTED) - return ioremap_np(res.start, resource_size(&res)); - else - return ioremap(res.start, resource_size(&res)); -} -EXPORT_SYMBOL(of_iomap); - -/* - * of_io_request_and_map - Requests a resource and maps the memory mapped IO - * for a given device_node - * @device: the device whose io range will be mapped - * @index: index of the io range - * @name: name "override" for the memory region request or NULL - * - * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded - * error code on failure. Usage example: - * - * base = of_io_request_and_map(node, 0, "foo"); - * if (IS_ERR(base)) - * return PTR_ERR(base); - */ -void __iomem *of_io_request_and_map(struct device_node *np, int index, - const char *name) -{ - struct resource res; - void __iomem *mem; - - if (of_address_to_resource(np, index, &res)) - return IOMEM_ERR_PTR(-EINVAL); - - if (!name) - name = res.name; - if (!request_mem_region(res.start, resource_size(&res), name)) - return IOMEM_ERR_PTR(-EBUSY); - - if (res.flags & IORESOURCE_MEM_NONPOSTED) - mem = ioremap_np(res.start, resource_size(&res)); - else - mem = ioremap(res.start, resource_size(&res)); - - if (!mem) { - release_mem_region(res.start, resource_size(&res)); - return IOMEM_ERR_PTR(-ENOMEM); - } - - return mem; -} -EXPORT_SYMBOL(of_io_request_and_map); - #ifdef CONFIG_HAS_DMA /** * of_dma_get_range - Get DMA range info and put it into a map array @@ -1155,3 +1017,136 @@ static bool of_mmio_is_nonposted(struct device_node *np) of_node_put(parent); return nonposted; } + +static int __of_address_to_resource(struct device_node *dev, int index, int bar_no, + struct resource *r) +{ + u64 taddr; + const __be32 *addrp; + u64 size; + unsigned int flags; + const char *name = NULL; + + addrp = __of_get_address(dev, index, bar_no, &size, &flags); + if (addrp == NULL) + return -EINVAL; + + /* Get optional "reg-names" property to add a name to a resource */ + if (index >= 0) + of_property_read_string_index(dev, "reg-names", index, &name); + + if (flags & IORESOURCE_MEM) + taddr = of_translate_address(dev, addrp); + else if (flags & IORESOURCE_IO) + taddr = of_translate_ioport(dev, addrp, size); + else + return -EINVAL; + + if (taddr == OF_BAD_ADDR) + return -EINVAL; + memset(r, 0, sizeof(struct resource)); + + if (of_mmio_is_nonposted(dev)) + flags |= IORESOURCE_MEM_NONPOSTED; + + r->start = taddr; + r->end = taddr + size - 1; + r->flags = flags; + r->name = name ? name : dev->full_name; + + return 0; +} + +/** + * of_address_to_resource - Translate device tree address and return as resource + * @dev: Caller's Device Node + * @index: Index into the array + * @r: Pointer to resource array + * + * Returns -EINVAL if the range cannot be converted to resource. + * + * Note that if your address is a PIO address, the conversion will fail if + * the physical address can't be internally converted to an IO token with + * pci_address_to_pio(), that is because it's either called too early or it + * can't be matched to any host bridge IO space + */ +int of_address_to_resource(struct device_node *dev, int index, + struct resource *r) +{ + return __of_address_to_resource(dev, index, -1, r); +} +EXPORT_SYMBOL_GPL(of_address_to_resource); + +int of_pci_address_to_resource(struct device_node *dev, int bar, + struct resource *r) +{ + + if (!IS_ENABLED(CONFIG_PCI)) + return -ENOSYS; + + return __of_address_to_resource(dev, -1, bar, r); +} +EXPORT_SYMBOL_GPL(of_pci_address_to_resource); + +/** + * of_iomap - Maps the memory mapped IO for a given device_node + * @np: the device whose io range will be mapped + * @index: index of the io range + * + * Returns a pointer to the mapped memory + */ +void __iomem *of_iomap(struct device_node *np, int index) +{ + struct resource res; + + if (of_address_to_resource(np, index, &res)) + return NULL; + + if (res.flags & IORESOURCE_MEM_NONPOSTED) + return ioremap_np(res.start, resource_size(&res)); + else + return ioremap(res.start, resource_size(&res)); +} +EXPORT_SYMBOL(of_iomap); + +/* + * of_io_request_and_map - Requests a resource and maps the memory mapped IO + * for a given device_node + * @device: the device whose io range will be mapped + * @index: index of the io range + * @name: name "override" for the memory region request or NULL + * + * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded + * error code on failure. Usage example: + * + * base = of_io_request_and_map(node, 0, "foo"); + * if (IS_ERR(base)) + * return PTR_ERR(base); + */ +void __iomem *of_io_request_and_map(struct device_node *np, int index, + const char *name) +{ + struct resource res; + void __iomem *mem; + + if (of_address_to_resource(np, index, &res)) + return IOMEM_ERR_PTR(-EINVAL); + + if (!name) + name = res.name; + if (!request_mem_region(res.start, resource_size(&res), name)) + return IOMEM_ERR_PTR(-EBUSY); + + if (res.flags & IORESOURCE_MEM_NONPOSTED) + mem = ioremap_np(res.start, resource_size(&res)); + else + mem = ioremap(res.start, resource_size(&res)); + + if (!mem) { + release_mem_region(res.start, resource_size(&res)); + return IOMEM_ERR_PTR(-ENOMEM); + } + + return mem; +} +EXPORT_SYMBOL(of_io_request_and_map); -- 2.34.1