From patchwork Thu Nov 8 07:22:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Dongsheng" X-Patchwork-Id: 10673625 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9BA6C1057 for ; Thu, 8 Nov 2018 07:23:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8239C2D966 for ; Thu, 8 Nov 2018 07:23:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 750BA2D967; Thu, 8 Nov 2018 07:23:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4BE432D968 for ; Thu, 8 Nov 2018 07:23:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726823AbeKHQ5Q (ORCPT ); Thu, 8 Nov 2018 11:57:16 -0500 Received: from mx01.hxt-semitech.com ([223.203.96.7]:38394 "EHLO barracuda.hxt-semitech.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726143AbeKHQ5P (ORCPT ); Thu, 8 Nov 2018 11:57:15 -0500 X-ASG-Debug-ID: 1541661780-093b7e63fe004e0001-I98ny2 Received: from HXTBJIDCEMVIW02.hxtcorp.net ([10.128.0.15]) by barracuda.hxt-semitech.com with ESMTP id TmoJzrFZ8Swq4IO5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NO); Thu, 08 Nov 2018 15:23:00 +0800 (CST) X-Barracuda-Envelope-From: dongsheng.wang@hxt-semitech.com Received: from arch.hxtcorp.net (10.64.6.141) by HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Thu, 8 Nov 2018 15:22:26 +0800 From: Wang Dongsheng To: , CC: Wang Dongsheng , , , , , Subject: [RFC PATCH 1/3] acpi: Add acpi mdio support code Date: Thu, 8 Nov 2018 15:22:16 +0800 X-ASG-Orig-Subj: [RFC PATCH 1/3] acpi: Add acpi mdio support code Message-ID: <2bf82b60c52bd3fc38b733e38fd991c9d31af6b9.1541660504.git.dongsheng.wang@hxt-semitech.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: <20181029124044.GB9174@lunn.ch> MIME-Version: 1.0 X-Originating-IP: [10.64.6.141] X-ClientProxiedBy: HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) To HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) X-Barracuda-Connect: UNKNOWN[10.128.0.15] X-Barracuda-Start-Time: 1541661780 X-Barracuda-Encrypted: ECDHE-RSA-AES256-SHA384 X-Barracuda-URL: https://192.168.50.101:443/cgi-mod/mark.cgi X-Barracuda-BRTS-Status: 1 X-Virus-Scanned: by bsmtpd at hxt-semitech.com X-Barracuda-Bayes: SPAM GLOBAL 0.9999 1.0000 4.3412 X-Barracuda-Spam-Score: 4.84 X-Barracuda-Spam-Status: No, SCORE=4.84 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=9.0 tests=BSF_RULE7568M X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.61411 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE7568M Custom Rule 7568M Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add support for parsing the ACPI data node for PHY devices on an MDIO bus. The current implementation depend on mdio bus scan. With _DSD device properties we can finally do this: Device (MDIO) { Name (_DSD, Package () { ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"), Package () { Package () { "ethernet-phy@0", PHY0 }, } }) Name (PHY0, Package() { ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), Package () { Package () { "reg", 0x0 }, } }) } Device (MACO) { Name (_DSD, Package () { ToUUID ("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), Package () { Package () { "phy-handle", \_SB.MDIO, "ethernet-phy@0" }, } }) } Documentations: The DT "phy-handle" binding that we reuse for ACPI is documented in Documentation/devicetree/bindings/phy/phy-bindings.txt Documentation/acpi/dsd/data-node-references.txt Documentation/acpi/dsd/graph.txt Signed-off-by: Wang Dongsheng --- drivers/acpi/Kconfig | 6 ++ drivers/acpi/Makefile | 1 + drivers/acpi/acpi_mdio.c | 167 +++++++++++++++++++++++++++++++++++++ drivers/net/phy/mdio_bus.c | 3 + include/linux/acpi_mdio.h | 82 ++++++++++++++++++ 5 files changed, 259 insertions(+) diff --git a/drivers/acpi/Kconfig b/drivers/acpi/Kconfig index 9705fc986da9..0fefa3410ce9 100644 --- a/drivers/acpi/Kconfig +++ b/drivers/acpi/Kconfig @@ -252,6 +252,12 @@ config ACPI_PROCESSOR_IDLE config ACPI_MCFG bool +config ACPI_MDIO + def_tristate PHYLIB + depends on PHYLIB + help + ACPI MDIO bus (Ethernet PHY) accessors + config ACPI_CPPC_LIB bool depends on ACPI_PROCESSOR diff --git a/drivers/acpi/Makefile b/drivers/acpi/Makefile index 6d59aa109a91..ec7461a064fc 100644 --- a/drivers/acpi/Makefile +++ b/drivers/acpi/Makefile @@ -41,6 +41,7 @@ acpi-y += ec.o acpi-$(CONFIG_ACPI_DOCK) += dock.o acpi-y += pci_root.o pci_link.o pci_irq.o obj-$(CONFIG_ACPI_MCFG) += pci_mcfg.o +acpi-$(CONFIG_ACPI_MDIO) += acpi_mdio.o acpi-y += acpi_lpss.o acpi_apd.o acpi-y += acpi_platform.o acpi-y += acpi_pnp.o diff --git a/drivers/acpi/acpi_mdio.c b/drivers/acpi/acpi_mdio.c new file mode 100644 index 000000000000..293bf9a63197 --- /dev/null +++ b/drivers/acpi/acpi_mdio.c @@ -0,0 +1,167 @@ +// SPDX-License-Identifier: GPL-2.0+ +// Lots of code in this file is copy from drivers/of/of_mdio.c +// Copyright (c) 2018 Huaxintong Semiconductor Technology Co., Ltd. + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Helper function for acpi_phy_find_device */ +static int phy_match(struct device *dev, void *fwnode) +{ + return dev->fwnode == fwnode; +} + +/** + * acpi_phy_find_device - Give a PHY fwnode, find the phy_device + * @fwnode: Pointer to the phy's acpi data node + * + * If successful, returns a pointer to the phy_device with the embedded + * struct device refcount incremented by one, or NULL on failure. + */ +struct phy_device *acpi_phy_find_device(struct fwnode_handle *fwnode) +{ + struct device *d; + struct mdio_device *mdiodev; + + if (!fwnode) + return NULL; + + d = bus_find_device(&mdio_bus_type, NULL, fwnode, phy_match); + if (d) { + mdiodev = to_mdio_device(d); + if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) + return to_phy_device(d); + put_device(d); + } + return NULL; +} +EXPORT_SYMBOL(acpi_phy_find_device); + +static int do_acpi_mdiodev_match(struct fwnode_handle *fwnode, + struct mdio_device *mdiodev) +{ + struct device *dev = &mdiodev->dev; + struct fwnode_handle *child_node; + int addr; + int ret; + + fwnode_for_each_child_node(fwnode, child_node) { + do { + addr = acpi_mdio_parse_addr(dev, child_node); + if (addr < 0) + break; + + if (mdiodev->addr != addr) + break; + + dev->fwnode = child_node; + return 0; + } while (0); + + /* Walk hierarchical extension data nodes */ + ret = do_acpi_mdiodev_match(child_node, mdiodev); + if (!ret) + return 0; + } + + return -ENODEV; +} + +/* Walk the list of subnodes of a mdio bus and look for a node that + * matches the mdio device's address with its 'reg' property. If + * found, set the fwnode pointer for the mdio device. + */ +void acpi_mdiobus_link_mdiodev(struct mii_bus *bus, + struct mdio_device *mdiodev) +{ + struct device *dev = &mdiodev->dev; + + if (dev->fwnode || !bus->dev.fwnode) + return; + + if (!has_acpi_companion(&bus->dev)) + return; + + do_acpi_mdiodev_match(bus->dev.fwnode, mdiodev); +} + +/** + * acpi_phy_connect - Connect to the phy + * @dev: pointer to net_device claiming the phy + * @fwnode: Pointer to ACPI data node for the PHY + * @hndlr: Link state callback for the network device + * @flags: flags to pass to the PHY + * @iface: PHY data interface type + * + * If successful, returns a pointer to the phy_device with the embedded + * struct device refcount incremented by one, or NULL on failure. The + * refcount must be dropped by calling phy_disconnect() or phy_detach(). + */ +struct phy_device *acpi_phy_connect(struct net_device *dev, + struct fwnode_handle *fwnode, + void (*hndlr)(struct net_device *), + u32 flags, + phy_interface_t iface) +{ + struct phy_device *phy = acpi_phy_find_device(fwnode); + int ret; + + if (!phy) + return NULL; + + phy->dev_flags = flags; + + ret = phy_connect_direct(dev, phy, hndlr, iface); + + /* refcount is held by phy_connect_direct() on success */ + put_device(&phy->mdio.dev); + + return ret ? NULL : phy; +} +EXPORT_SYMBOL(acpi_phy_connect); + +static int acpi_mdio_node_verify(struct fwnode_handle *fwnode) +{ + return is_acpi_device_node(fwnode) ? 0 : -ENODEV; +} + +static int fwnode_mdiobus_verify_node(struct fwnode_handle *fwnode) +{ + if (!is_acpi_node(fwnode)) + return -ENODEV; + return acpi_mdio_node_verify(fwnode); +} + +/** + * acpi_mdiobus_register - Register mii_bus and create PHYs + * @mdio: pointer to mii_bus structure + * @fwnode: pointer to fw_node of MDIO bus. + * + * This function registers the mii_bus structure and scan the phy_devices + * for each child node of @fwnode. + */ +int acpi_mdiobus_register(struct mii_bus *mdio, struct fwnode_handle *fwnode) +{ + int ret; + + if (!fwnode) + return mdiobus_register(mdio); + + ret = fwnode_mdiobus_verify_node(fwnode); + if (ret) + return ret; + + /* Scan PHYs on MDIO bus */ + mdio->phy_mask = 0; + mdio->dev.fwnode = fwnode; + + /* Register the MDIO bus */ + return mdiobus_register(mdio); +} +EXPORT_SYMBOL(acpi_mdiobus_register); diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c index 2e59a8419b17..d7bca2145d0f 100644 --- a/drivers/net/phy/mdio_bus.c +++ b/drivers/net/phy/mdio_bus.c @@ -13,6 +13,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include #include #include #include @@ -516,6 +517,8 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) * in the bus node, and set the of_node pointer in this case. */ of_mdiobus_link_mdiodev(bus, &phydev->mdio); + /* Link the phy device with ACPI phy fwnode. */ + acpi_mdiobus_link_mdiodev(bus, &phydev->mdio); err = phy_device_register(phydev); if (err) { diff --git a/include/linux/acpi_mdio.h b/include/linux/acpi_mdio.h new file mode 100644 index 000000000000..1a4a30258ebc --- /dev/null +++ b/include/linux/acpi_mdio.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +// Copyright (c) 2018 Huaxintong Semiconductor Technology Co., Ltd. + +#ifndef __LINUX_ACPI_MDIO_H +#define __LINUX_ACPI_MDIO_H + +#include +#include +#include +#include + +#if IS_ENABLED(CONFIG_ACPI_MDIO) +static inline int acpi_mdio_parse_addr(struct device *dev, + const struct fwnode_handle *fwnode) +{ + u32 addr; + + if (!is_acpi_data_node(fwnode)) + return -ENODEV; + + if (!fwnode_property_present(fwnode, "reg")) + return -ENODEV; + + if (fwnode_property_read_u32(fwnode, "reg", &addr)) { + dev_err(dev, "Invalid PHY address\n"); + return -ENODEV; + } + + /* A PHY must have a reg property in the range [0-31] */ + if (addr >= PHY_MAX_ADDR) { + dev_err(dev, "PHY address %i is too large\n", addr); + return -EINVAL; + } + + return addr; +} + +struct phy_device *acpi_phy_find_device(struct fwnode_handle *fwnode); +struct phy_device *acpi_phy_connect(struct net_device *dev, + struct fwnode_handle *fwnode, + void (*hndlr)(struct net_device *), + u32 flags, phy_interface_t iface); +int acpi_mdiobus_register(struct mii_bus *mdio, struct fwnode_handle *fwnode); +void acpi_mdiobus_link_mdiodev(struct mii_bus *bus, + struct mdio_device *mdiodev); +#else +static inline int acpi_mdio_parse_addr(struct device *dev, + const struct fwnode_handle *fwnode) +{ + return -EINVAL; +} + +static inline struct phy_device * +acpi_phy_find_device(struct fwnode_handle *fwnode) +{ + return NULL; +} + +static inline struct phy_device * +acpi_phy_connect(struct net_device *dev, struct fwnode_handle *fwnode, + void (*hndlr)(struct net_device *), u32 flags, + phy_interface_t iface) +{ + return NULL; +} + +static inline int acpi_mdiobus_register(struct mii_bus *mdio, + struct fwnode_handle *fwnode) +{ + return -ENODEV; +} + +static inline void +acpi_mdiobus_link_mdiodev(struct mii_bus *bus, struct mdio_device *mdiodev) { } +#endif + +static inline struct fwnode_handle *acpi_get_phy_node(struct phy_device *phydev) +{ + return !phydev ? NULL : phydev->mdio.dev.fwnode; +} + +#endif /* __LINUX_ACPI_MDIO_H */ From patchwork Thu Nov 8 07:22:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Dongsheng" X-Patchwork-Id: 10673627 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A72D61751 for ; Thu, 8 Nov 2018 07:23:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8D6772A40E for ; Thu, 8 Nov 2018 07:23:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E2602A505; Thu, 8 Nov 2018 07:23:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F29762A40E for ; Thu, 8 Nov 2018 07:23:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726846AbeKHQ5g (ORCPT ); Thu, 8 Nov 2018 11:57:36 -0500 Received: from mx01.hxt-semitech.com ([223.203.96.7]:38421 "EHLO barracuda.hxt-semitech.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726814AbeKHQ5f (ORCPT ); Thu, 8 Nov 2018 11:57:35 -0500 X-ASG-Debug-ID: 1541661798-093b7e63fe00560001-I98ny2 Received: from HXTBJIDCEMVIW02.hxtcorp.net ([10.128.0.15]) by barracuda.hxt-semitech.com with ESMTP id R1OmgorWH8Odc76Z (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NO); Thu, 08 Nov 2018 15:23:19 +0800 (CST) X-Barracuda-Envelope-From: dongsheng.wang@hxt-semitech.com Received: from arch.hxtcorp.net (10.64.6.141) by HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Thu, 8 Nov 2018 15:22:45 +0800 From: Wang Dongsheng To: , CC: Wang Dongsheng , , , , , Subject: [RFC PATCH 2/3] net: qcom/emac: split phy_config to mdio bus create and get phy device Date: Thu, 8 Nov 2018 15:22:34 +0800 X-ASG-Orig-Subj: [RFC PATCH 2/3] net: qcom/emac: split phy_config to mdio bus create and get phy device Message-ID: <7964cfa6df086189a87613a93d6184ad596cca55.1541660504.git.dongsheng.wang@hxt-semitech.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: <20181029124044.GB9174@lunn.ch> MIME-Version: 1.0 X-Originating-IP: [10.64.6.141] X-ClientProxiedBy: HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) To HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) X-Barracuda-Connect: UNKNOWN[10.128.0.15] X-Barracuda-Start-Time: 1541661799 X-Barracuda-Encrypted: ECDHE-RSA-AES256-SHA384 X-Barracuda-URL: https://192.168.50.101:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at hxt-semitech.com X-Barracuda-BRTS-Status: 1 X-Barracuda-Bayes: SPAM GLOBAL 1.0000 1.0000 4.3426 X-Barracuda-Spam-Score: 4.34 X-Barracuda-Spam-Status: No, SCORE=4.34 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=9.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.61411 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch separate emac_mdio_bus_create and emac_get_phydev from emac_phy_config, and do some codes clean. Signed-off-by: Wang Dongsheng --- drivers/net/ethernet/qualcomm/emac/emac-phy.c | 96 +++++++++++-------- 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/drivers/net/ethernet/qualcomm/emac/emac-phy.c b/drivers/net/ethernet/qualcomm/emac/emac-phy.c index 53dbf1e163a8..8289fdda4be7 100644 --- a/drivers/net/ethernet/qualcomm/emac/emac-phy.c +++ b/drivers/net/ethernet/qualcomm/emac/emac-phy.c @@ -96,15 +96,15 @@ static int emac_mdio_write(struct mii_bus *bus, int addr, int regnum, u16 val) return 0; } -/* Configure the MDIO bus and connect the external PHY */ -int emac_phy_config(struct platform_device *pdev, struct emac_adapter *adpt) +static int emac_mdio_bus_create(struct platform_device *pdev, + struct emac_adapter *adpt) { struct device_node *np = pdev->dev.of_node; struct mii_bus *mii_bus; int ret; /* Create the mii_bus object for talking to the MDIO bus */ - adpt->mii_bus = mii_bus = devm_mdiobus_alloc(&pdev->dev); + mii_bus = devm_mdiobus_alloc(&pdev->dev); if (!mii_bus) return -ENOMEM; @@ -115,50 +115,66 @@ int emac_phy_config(struct platform_device *pdev, struct emac_adapter *adpt) mii_bus->parent = &pdev->dev; mii_bus->priv = adpt; - if (has_acpi_companion(&pdev->dev)) { - u32 phy_addr; - - ret = mdiobus_register(mii_bus); - if (ret) { - dev_err(&pdev->dev, "could not register mdio bus\n"); - return ret; - } - ret = device_property_read_u32(&pdev->dev, "phy-channel", - &phy_addr); - if (ret) - /* If we can't read a valid phy address, then assume - * that there is only one phy on this mdio bus. - */ - adpt->phydev = phy_find_first(mii_bus); - else - adpt->phydev = mdiobus_get_phy(mii_bus, phy_addr); - - /* of_phy_find_device() claims a reference to the phydev, - * so we do that here manually as well. When the driver - * later unloads, it can unilaterally drop the reference - * without worrying about ACPI vs DT. - */ - if (adpt->phydev) - get_device(&adpt->phydev->mdio.dev); - } else { - struct device_node *phy_np; - - ret = of_mdiobus_register(mii_bus, np); - if (ret) { - dev_err(&pdev->dev, "could not register mdio bus\n"); - return ret; - } + ret = of_mdiobus_register(mii_bus, has_acpi_companion(&pdev->dev) ? + NULL : np); + if (ret) + dev_err(&pdev->dev, "Could not register mdio bus\n"); + + adpt->mii_bus = ret ? NULL : mii_bus; + return ret; +} + +static int emac_get_phydev(struct platform_device *pdev, + struct emac_adapter *adpt) +{ + struct device_node *np = pdev->dev.of_node; + struct mii_bus *bus = adpt->mii_bus; + struct device_node *phy_np; + struct phy_device *phydev; + u32 phy_addr; + int ret; + + if (!has_acpi_companion(&pdev->dev)) { phy_np = of_parse_phandle(np, "phy-handle", 0); adpt->phydev = of_phy_find_device(phy_np); of_node_put(phy_np); + return adpt->phydev ? 0 : -ENODEV; } - if (!adpt->phydev) { - dev_err(&pdev->dev, "could not find external phy\n"); - mdiobus_unregister(mii_bus); + ret = device_property_read_u32(&pdev->dev, "phy-channel", + &phy_addr); + /* If we can't read a valid phy address, then assume + * that there is only one phy on this mdio bus. + */ + phydev = ret ? phy_find_first(bus) : mdiobus_get_phy(bus, phy_addr); + if (!phydev) return -ENODEV; - } + /* of_phy_find_device() claims a reference to the phydev, + * so we do that here manually as well. When the driver + * later unloads, it can unilaterally drop the reference + * without worrying about ACPI vs DT. + */ + get_device(&phydev->mdio.dev); + adpt->phydev = phydev; return 0; } + +/* Configure the MDIO bus and connect the external PHY */ +int emac_phy_config(struct platform_device *pdev, struct emac_adapter *adpt) +{ + int ret; + + ret = emac_mdio_bus_create(pdev, adpt); + if (ret) + return ret; + + ret = emac_get_phydev(pdev, adpt); + if (ret) { + dev_err(&pdev->dev, "Could not find external phy\n"); + mdiobus_unregister(adpt->mii_bus); + } + + return ret; +} From patchwork Thu Nov 8 07:22:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Dongsheng" X-Patchwork-Id: 10673633 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DEC841751 for ; Thu, 8 Nov 2018 07:23:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 890AB2A8AE for ; Thu, 8 Nov 2018 07:23:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7D1262AB47; Thu, 8 Nov 2018 07:23:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DFE5A2A8AE for ; Thu, 8 Nov 2018 07:23:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726583AbeKHQ5x (ORCPT ); Thu, 8 Nov 2018 11:57:53 -0500 Received: from mx01.hxt-semitech.com ([223.203.96.7]:38446 "EHLO barracuda.hxt-semitech.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726935AbeKHQ5x (ORCPT ); Thu, 8 Nov 2018 11:57:53 -0500 X-ASG-Debug-ID: 1541661818-093b7e63fe005a0001-I98ny2 Received: from HXTBJIDCEMVIW02.hxtcorp.net ([10.128.0.15]) by barracuda.hxt-semitech.com with ESMTP id BqPD7kH3wcWsCPD1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NO); Thu, 08 Nov 2018 15:23:39 +0800 (CST) X-Barracuda-Envelope-From: dongsheng.wang@hxt-semitech.com Received: from arch.hxtcorp.net (10.64.6.141) by HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Thu, 8 Nov 2018 15:23:05 +0800 From: Wang Dongsheng To: , CC: Wang Dongsheng , , , , , Subject: [RFC PATCH 3/3] net: qcom/emac: add phy-handle support for ACPI Date: Thu, 8 Nov 2018 15:22:56 +0800 X-ASG-Orig-Subj: [RFC PATCH 3/3] net: qcom/emac: add phy-handle support for ACPI Message-ID: <6d3ad2a70c782afa2b4b88fd225c3b0f7cdffcfa.1541660504.git.dongsheng.wang@hxt-semitech.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: <20181029124044.GB9174@lunn.ch> MIME-Version: 1.0 X-Originating-IP: [10.64.6.141] X-ClientProxiedBy: HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) To HXTBJIDCEMVIW02.hxtcorp.net (10.128.0.15) X-Barracuda-Connect: UNKNOWN[10.128.0.15] X-Barracuda-Start-Time: 1541661819 X-Barracuda-Encrypted: ECDHE-RSA-AES256-SHA384 X-Barracuda-URL: https://192.168.50.101:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at hxt-semitech.com X-Barracuda-BRTS-Status: 1 X-Barracuda-Bayes: SPAM GLOBAL 1.0000 1.0000 4.3430 X-Barracuda-Spam-Score: 4.34 X-Barracuda-Spam-Status: No, SCORE=4.34 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=9.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.61411 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Use "phy-handle" to point to an internal MDIO device port. Signed-off-by: Wang Dongsheng --- drivers/net/ethernet/qualcomm/emac/emac-mac.c | 19 ++--- drivers/net/ethernet/qualcomm/emac/emac-phy.c | 78 ++++++++++++++----- 2 files changed, 70 insertions(+), 27 deletions(-) diff --git a/drivers/net/ethernet/qualcomm/emac/emac-mac.c b/drivers/net/ethernet/qualcomm/emac/emac-mac.c index 031f6e6ee9c1..74cfe7b95bb3 100644 --- a/drivers/net/ethernet/qualcomm/emac/emac-mac.c +++ b/drivers/net/ethernet/qualcomm/emac/emac-mac.c @@ -13,6 +13,7 @@ /* Qualcomm Technologies, Inc. EMAC Ethernet Controller MAC layer support */ +#include #include #include #include @@ -939,28 +940,28 @@ static void emac_adjust_link(struct net_device *netdev) int emac_mac_up(struct emac_adapter *adpt) { struct net_device *netdev = adpt->netdev; - int ret; + struct phy_device *phydev = adpt->phydev; + struct fwnode_handle *phy_np = acpi_get_phy_node(phydev); emac_mac_rx_tx_ring_reset_all(adpt); emac_mac_config(adpt); emac_mac_rx_descs_refill(adpt, &adpt->rx_q); - adpt->phydev->irq = PHY_POLL; - ret = phy_connect_direct(netdev, adpt->phydev, emac_adjust_link, - PHY_INTERFACE_MODE_SGMII); - if (ret) { + phydev->irq = PHY_POLL; + phydev = acpi_phy_connect(netdev, phy_np, emac_adjust_link, + 0, PHY_INTERFACE_MODE_SGMII); + if (!phydev) { netdev_err(adpt->netdev, "could not connect phy\n"); - return ret; + return -ENODEV; } - phy_attached_print(adpt->phydev, NULL); + phy_attached_print(phydev, NULL); /* enable mac irq */ writel((u32)~DIS_INT, adpt->base + EMAC_INT_STATUS); writel(adpt->irq.mask, adpt->base + EMAC_INT_MASK); - phy_start(adpt->phydev); - + phy_start(phydev); napi_enable(&adpt->rx_q.napi); netif_start_queue(netdev); diff --git a/drivers/net/ethernet/qualcomm/emac/emac-phy.c b/drivers/net/ethernet/qualcomm/emac/emac-phy.c index 8289fdda4be7..6616014292b0 100644 --- a/drivers/net/ethernet/qualcomm/emac/emac-phy.c +++ b/drivers/net/ethernet/qualcomm/emac/emac-phy.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "emac.h" /* EMAC base register offsets */ @@ -96,10 +97,60 @@ static int emac_mdio_write(struct mii_bus *bus, int addr, int regnum, u16 val) return 0; } +static struct phy_device * +emac_acpi_get_phydev_from_phy_handle(struct platform_device *pdev) +{ + struct fwnode_reference_args args = {0}; + struct fwnode_handle *fw_node; + struct phy_device *phydev; + int ret; + + /* Get PHY Port reference from phy-handle */ + fw_node = acpi_fwnode_handle(ACPI_COMPANION(&pdev->dev)); + ret = acpi_node_get_property_reference(fw_node, "phy-handle", 0, + &args); + if (ret) + return ERR_PTR(-ENODEV); + + if (!is_acpi_data_node(args.fwnode)) + return ERR_PTR(-ENODEV); + + phydev = acpi_phy_find_device(args.fwnode); + return phydev ? phydev : ERR_PTR(-ENODEV); +} + +static struct phy_device * +emac_acpi_get_phydev(struct platform_device *pdev, struct emac_adapter *adpt) +{ + struct phy_device *phydev = NULL; + int phy_addr; + int ret; + + /* Compatible with "phy-channel" */ + ret = device_property_read_u32(&pdev->dev, "phy-channel", + &phy_addr); + if (!ret) + phydev = mdiobus_get_phy(adpt->mii_bus, phy_addr); + if (phydev) + return phydev; + + /* Get PHY Port reference from phy-handle */ + phydev = emac_acpi_get_phydev_from_phy_handle(pdev); + if (!IS_ERR(phydev)) + return phydev; + + /* If we can't read a valid phy address from "phy-channel"/"phy-handle", + * then assume that there is only one phy on local mdio bus. + */ + phydev = phy_find_first(adpt->mii_bus); + return phydev ? phydev : ERR_PTR(-ENODEV); +} + static int emac_mdio_bus_create(struct platform_device *pdev, struct emac_adapter *adpt) { struct device_node *np = pdev->dev.of_node; + struct fwnode_handle *fwnode = pdev->dev.fwnode; struct mii_bus *mii_bus; int ret; @@ -115,8 +166,8 @@ static int emac_mdio_bus_create(struct platform_device *pdev, mii_bus->parent = &pdev->dev; mii_bus->priv = adpt; - ret = of_mdiobus_register(mii_bus, has_acpi_companion(&pdev->dev) ? - NULL : np); + ret = is_of_node(fwnode) ? of_mdiobus_register(mii_bus, np) : + acpi_mdiobus_register(mii_bus, fwnode); if (ret) dev_err(&pdev->dev, "Could not register mdio bus\n"); @@ -128,13 +179,9 @@ static int emac_get_phydev(struct platform_device *pdev, struct emac_adapter *adpt) { struct device_node *np = pdev->dev.of_node; - struct mii_bus *bus = adpt->mii_bus; struct device_node *phy_np; struct phy_device *phydev; - u32 phy_addr; - int ret; - if (!has_acpi_companion(&pdev->dev)) { phy_np = of_parse_phandle(np, "phy-handle", 0); adpt->phydev = of_phy_find_device(phy_np); @@ -142,14 +189,9 @@ static int emac_get_phydev(struct platform_device *pdev, return adpt->phydev ? 0 : -ENODEV; } - ret = device_property_read_u32(&pdev->dev, "phy-channel", - &phy_addr); - /* If we can't read a valid phy address, then assume - * that there is only one phy on this mdio bus. - */ - phydev = ret ? phy_find_first(bus) : mdiobus_get_phy(bus, phy_addr); - if (!phydev) - return -ENODEV; + phydev = emac_acpi_get_phydev(pdev, adpt); + if (IS_ERR(phydev)) + return PTR_ERR(phydev); /* of_phy_find_device() claims a reference to the phydev, * so we do that here manually as well. When the driver @@ -171,10 +213,10 @@ int emac_phy_config(struct platform_device *pdev, struct emac_adapter *adpt) return ret; ret = emac_get_phydev(pdev, adpt); - if (ret) { - dev_err(&pdev->dev, "Could not find external phy\n"); - mdiobus_unregister(adpt->mii_bus); - } + if (!ret) + return 0; + dev_err(&pdev->dev, "Could not find external phy\n"); + mdiobus_unregister(adpt->mii_bus); return ret; }