From patchwork Sat Jun 17 17:24:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salil Mehta X-Patchwork-Id: 105779 Delivered-To: patch@linaro.org Received: by 10.140.91.2 with SMTP id y2csp173078qgd; Sat, 17 Jun 2017 10:26:15 -0700 (PDT) X-Received: by 10.99.108.193 with SMTP id h184mr17827506pgc.166.1497720375781; Sat, 17 Jun 2017 10:26:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1497720375; cv=none; d=google.com; s=arc-20160816; b=YBA/npKgUGJmrpoBrsn7KWu1hvZXC5wf9TLuQnKtsDhMQPKQpAIduiT/Aq7spN/hIV cXA1Fs//i0mixnslLS6zfSFZVK0uelHtGBn+0bkZ73V8UpRTkVmQXn0IyGSCfKjMeipt dICb6DITI5nJPfMOxbmQECMB0Sy1zZbl+Pat6ibQV6LFN2e8b11hah0Dx2xOkykmFUpV 4XqJaXmoiiVnh4r7IAirR3yOn5FLzGj+cXvxeDSG92zC1kyLbWzgJHXyaz+na2jcNpWn C94HLKRbnEBbLTeBJhmUpp7ETbvKczekgMI8v2grtBdMU0T/O76drotNW4TunZ22OkTE FSWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=h/CXmiaRaWhk/A6VaCofQ6ikQ00cEsl8eI0T9f06xeQ=; b=zLQ32yLUgaRTCGT83X43R1HlOUQTVuMZUy+uYwRdzrkYzjwyeBYc1nwBpH8SSa5mSb iLVpgrszhyhUazp6/nUUB9VVGz0A7k8jw30+4ov2X8DxMrjzC1zZNnsjOrZX2cr04oN1 EfR4dQrHl+YzwCtRq/71NZnQDc5ExFgbnRHMpC0GPPUmFvt6pQIUSj1etagc026H6yt0 oAjMdABoygWkC/v648h/UkOLMAcNy4xDMJYDGUvGfhjpaqmDYEHBvjUCdhLHlb2K8gNj 0lVIafdJlSXzaB4zjumm+ZGgEZ2gOvUzwOD62VW9A0r/GDNiKyW0xDbflyx778avYNRG YL6A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n127si4572389pga.135.2017.06.17.10.26.15; Sat, 17 Jun 2017 10:26:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752899AbdFQR0B (ORCPT + 25 others); Sat, 17 Jun 2017 13:26:01 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:8325 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752744AbdFQRZt (ORCPT ); Sat, 17 Jun 2017 13:25:49 -0400 Received: from 172.30.72.56 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.56]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id APN01389; Sun, 18 Jun 2017 01:25:37 +0800 (CST) Received: from S00293818-DELL1.china.huawei.com (10.203.181.160) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Sun, 18 Jun 2017 01:25:30 +0800 From: Salil Mehta To: CC: , , , , , , , Subject: [PATCH V3 net-next 6/8] net: hns3: Add MDIO support to HNS3 Ethernet driver for hip08 SoC Date: Sat, 17 Jun 2017 18:24:29 +0100 Message-ID: <20170617172431.177044-7-salil.mehta@huawei.com> X-Mailer: git-send-email 2.8.3 In-Reply-To: <20170617172431.177044-1-salil.mehta@huawei.com> References: <20170617172431.177044-1-salil.mehta@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.203.181.160] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A0B0208.59456611.00BB, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 7d9facaeeb01b568fd8beb346b740690 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds the support of MDIO bus interface for HNS3 driver. Code provides various interfaces to start and stop the PHY layer and to read and write the MDIO bus or PHY. Signed-off-by: Daode Huang Signed-off-by: lipeng Signed-off-by: Salil Mehta Signed-off-by: Yisen Zhuang --- Patch V3: Addressed Below comments: 1. Florian Fainelli: https://lkml.org/lkml/2017/6/13/963 2. Andrew Lunn: https://lkml.org/lkml/2017/6/13/1039 Patch V2: Addressed below comments: 1. Florian Fainelli: https://lkml.org/lkml/2017/6/10/130 2. Andrew Lunn: https://lkml.org/lkml/2017/6/10/168 Patch V1: Initial Submit --- .../ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c | 249 +++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c -- 2.7.4 diff --git a/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c b/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c new file mode 100644 index 0000000..5b21c50 --- /dev/null +++ b/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c @@ -0,0 +1,249 @@ +/* + * Copyright (c) 2016~2017 Hisilicon Limited. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include + +#include "hclge_cmd.h" +#include "hclge_main.h" + +enum hclge_mdio_c22_op_seq { + HCLGE_MDIO_C22_WRITE = 1, + HCLGE_MDIO_C22_READ = 2 +}; + +#define HCLGE_MDIO_CTRL_START_BIT BIT(0) +#define HCLGE_MDIO_CTRL_ST_MSK GENMASK(2, 1) +#define HCLGE_MDIO_CTRL_ST_LSH 1 +#define HCLGE_MDIO_IS_C22(c22) (((c22) << HCLGE_MDIO_CTRL_ST_LSH) & \ + HCLGE_MDIO_CTRL_ST_MSK) + +#define HCLGE_MDIO_CTRL_OP_MSK GENMASK(4, 3) +#define HCLGE_MDIO_CTRL_OP_LSH 3 +#define HCLGE_MDIO_CTRL_OP(access) \ + (((access) << HCLGE_MDIO_CTRL_OP_LSH) & HCLGE_MDIO_CTRL_OP_MSK) +#define HCLGE_MDIO_CTRL_PRTAD_MSK GENMASK(4, 0) +#define HCLGE_MDIO_CTRL_DEVAD_MSK GENMASK(4, 0) + +#define HCLGE_MDIO_STA_VAL(val) ((val) & BIT(0)) + +struct hclge_mdio_cfg_cmd { + u8 ctrl_bit; + u8 prtad; /* The external port address */ + u8 devad; /* The external device address */ + u8 rsvd; + __le16 reserve; + __le16 data_wr; + __le16 data_rd; + __le16 sta; +}; + +static int hclge_mdio_write(struct mii_bus *bus, int phy_id, int regnum, + u16 data) +{ + struct hclge_dev *hdev = (struct hclge_dev *)bus->priv; + struct hclge_mdio_cfg_cmd *mdio_cmd; + enum hclge_cmd_status status; + struct hclge_desc desc; + u8 devad; + + if (!bus) + return -EINVAL; + + devad = ((regnum >> 16) & 0x1f); + + dev_dbg(&bus->dev, "phy id=%d, devad=%d\n", phy_id, devad); + + hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MDIO_CONFIG, false); + + mdio_cmd = (struct hclge_mdio_cfg_cmd *)desc.data; + + mdio_cmd->prtad = phy_id & HCLGE_MDIO_CTRL_PRTAD_MSK; + mdio_cmd->data_wr = cpu_to_le16(data); + mdio_cmd->devad = devad & HCLGE_MDIO_CTRL_DEVAD_MSK; + + /* Write reg and data */ + mdio_cmd->ctrl_bit = HCLGE_MDIO_IS_C22(1); + mdio_cmd->ctrl_bit |= HCLGE_MDIO_CTRL_OP(HCLGE_MDIO_C22_WRITE); + mdio_cmd->ctrl_bit |= HCLGE_MDIO_CTRL_START_BIT; + + status = hclge_cmd_send(&hdev->hw, &desc, 1); + if (status) { + dev_err(&hdev->pdev->dev, + "mdio write fail when sending cmd, status is %d.\n", + status); + return -EIO; + } + + return 0; +} + +static int hclge_mdio_read(struct mii_bus *bus, int phy_id, int regnum) +{ + struct hclge_dev *hdev = (struct hclge_dev *)bus->priv; + struct hclge_mdio_cfg_cmd *mdio_cmd; + enum hclge_cmd_status status; + struct hclge_desc desc; + u8 devad; + + if (!bus) + return -EINVAL; + + devad = ((regnum >> 16) & GENMASK(4, 0)); + + hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MDIO_CONFIG, true); + + mdio_cmd = (struct hclge_mdio_cfg_cmd *)desc.data; + + dev_dbg(&bus->dev, "phy id=%d, devad=%d\n", phy_id, devad); + + mdio_cmd->prtad = phy_id & HCLGE_MDIO_CTRL_PRTAD_MSK; + mdio_cmd->devad = devad & HCLGE_MDIO_CTRL_DEVAD_MSK; + + /* Write reg and data */ + mdio_cmd->ctrl_bit = HCLGE_MDIO_IS_C22(1); + mdio_cmd->ctrl_bit |= HCLGE_MDIO_CTRL_OP(HCLGE_MDIO_C22_WRITE); + mdio_cmd->ctrl_bit |= HCLGE_MDIO_CTRL_START_BIT; + + /* Read out phy data */ + status = hclge_cmd_send(&hdev->hw, &desc, 1); + if (status) { + dev_err(&hdev->pdev->dev, + "mdio read fail when get data, status is %d.\n", + status); + return status; + } + + if (HCLGE_MDIO_STA_VAL(mdio_cmd->sta)) { + dev_err(&hdev->pdev->dev, "mdio read data error\n"); + return -EIO; + } + + return le16_to_cpu(mdio_cmd->data_rd); +} + +int hclge_mac_mdio_config(struct hclge_dev *hdev) +{ + struct hclge_mac *mac = &hdev->hw.mac; + struct net_device *ndev = &mac->ndev; + struct phy_device *phy_dev; + struct mii_bus *mdio_bus; + int ret; + + if (hdev->hw.mac.phy_addr >= PHY_MAX_ADDR) + return 0; + + SET_NETDEV_DEV(ndev, &hdev->pdev->dev); + + mdio_bus = devm_mdiobus_alloc(&hdev->pdev->dev); + if (!mdio_bus) { + ret = -ENOMEM; + goto err_miibus_alloc; + } + + mdio_bus->name = "hisilicon MII bus"; + mdio_bus->read = hclge_mdio_read; + mdio_bus->write = hclge_mdio_write; + snprintf(mdio_bus->id, MII_BUS_ID_SIZE, "%s-%s", "mii", + dev_name(&hdev->pdev->dev)); + + mdio_bus->parent = &hdev->pdev->dev; + mdio_bus->priv = hdev; + mdio_bus->phy_mask = ~(1 << mac->phy_addr); + ret = mdiobus_register(mdio_bus); + if (ret) { + dev_err(mdio_bus->parent, + "Failed to register MDIO bus ret = %#x\n", ret); + goto err_mdio_register; + } + + phy_dev = mdiobus_get_phy(mdio_bus, mac->phy_addr); + if (!phy_dev || IS_ERR(phy_dev)) { + dev_err(mdio_bus->parent, "Failed to get phy device\n"); + ret = -EIO; + goto err_mdio_register; + } + + phy_dev->irq = mdio_bus->irq[mac->phy_addr]; + mac->phy_dev = phy_dev; + + return 0; + +err_mdio_register: + mdiobus_unregister(mdio_bus); + mdiobus_free(mdio_bus); +err_miibus_alloc: + return ret; +} + +static void hclge_mac_adjust_link(struct net_device *net_dev) +{ + struct hclge_mac *hw_mac; + struct hclge_dev *hdev; + struct hclge_hw *hw; + int duplex; + int speed; + + if (!net_dev) + return; + + hw_mac = container_of(net_dev, struct hclge_mac, ndev); + hw = container_of(hw_mac, struct hclge_hw, mac); + hdev = hw->back; + + speed = hw_mac->phy_dev->speed; + duplex = hw_mac->phy_dev->duplex; + + /* update antoneg. */ + hw_mac->autoneg = hw_mac->phy_dev->autoneg; + + if ((hw_mac->speed != speed) || (hw_mac->duplex != duplex)) + (void)hclge_cfg_mac_speed_dup(hdev, speed, !!duplex); +} + +int hclge_mac_start_phy(struct hclge_dev *hdev) +{ + struct hclge_mac *mac = &hdev->hw.mac; + struct phy_device *phy_dev = mac->phy_dev; + struct net_device *ndev = &mac->ndev; + int ret; + + if (!phy_dev) + return 0; + + phy_dev->dev_flags = 0; + + ret = phy_connect_direct(ndev, phy_dev, + hclge_mac_adjust_link, + PHY_INTERFACE_MODE_SGMII); + if (unlikely(ret)) { + pr_info("phy_connect_direct err"); + return -ENODEV; + } + + phy_dev->supported = SUPPORTED_10baseT_Half | + SUPPORTED_10baseT_Full | + SUPPORTED_100baseT_Half | + SUPPORTED_100baseT_Full | + SUPPORTED_Autoneg | + SUPPORTED_1000baseT_Full; + + phy_start(mac->phy_dev); + + return 0; +} + +void hclge_mac_stop_phy(struct hclge_dev *hdev) +{ + if (!hdev->hw.mac.phy_dev) + return; + + phy_disconnect(hdev->hw.mac.phy_dev); + phy_stop(hdev->hw.mac.phy_dev); +}