Received: by 10.213.65.68 with SMTP id h4csp1225282imn; Wed, 14 Mar 2018 13:28:07 -0700 (PDT) X-Google-Smtp-Source: AG47ELuUjJJM2SbRj8XHNweAFGl791JPjxDoPv04pOcO3lMX+DDw+yxC0bPSLJSqNHZVvuAga/AO X-Received: by 2002:a17:902:768b:: with SMTP id m11-v6mr5362451pll.185.1521059287774; Wed, 14 Mar 2018 13:28:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521059287; cv=none; d=google.com; s=arc-20160816; b=CBP74qfohRCkgfqmQvQgpX1UKh2nq5GmiEDbCzm5UQLtXSkAtKjta6I6s+k5Rdo3Up Q49l425I4A8pM+Ex5Hk30DP71ejcxCasWAGCruWMNa/cOrIfEbrXu1r2zcRWoZoT0KzU eQ2YpyDHaa6ZDROYh01GsQ+kgZIc5xRsHRvbaGmAnUW7R/Ybp6j09CTMBXFegS+PXXas ImlZgUNI4AspGG/J3OKNB2p9tKrlnGbG7Z2TioO7CZkkcUX8Mbq+6unjhx1ZR64k4Ods 0Ebe30Ug8v64obirzwyFWTTOPZQXXOdZH7VODtlzChfSIHJ3i1I0GumVhZPVfM212+8g tecQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:cc:subject:date:to :from:arc-authentication-results; bh=fkzbXuHuWY4jf6+irseu9syAwAszD92UK7gtceocxHY=; b=ZTESQ1jR0aJGfAbriWxgdToZVIqumFeGFO8MrICwLrASeM4b/TjZ08BnKjYcuV6O6Y C7Cfp1I3NdE7IuqV0IKSGSNtIDCTN15ARXgkgjEZ0J4YWOhXzky3GZjrDpNW5P27lFFF 0BKNP8Nu6uVud/zwCL16EfjzwaZWrtvo0aqQZ09qWG4SvPlC/F/eXTGCBFjMgg8++wor RGwmGxG06z26MM8dC9VdanryIQhx5Zdwtj8AdrotEnY2gDb9a6Fn+OkaAkGrMBiYieyZ nP/wPuUPsDdo7mY4VEDj2ZvXV1XC5Gw/xVU4stVOXu9EDy5ReGYxu3JTEqOiHTGXzPaB G3/A== 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 e5si2375957pgf.646.2018.03.14.13.27.52; Wed, 14 Mar 2018 13:28:07 -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 S1752309AbeCNUZO (ORCPT + 99 others); Wed, 14 Mar 2018 16:25:14 -0400 Received: from mx2.suse.de ([195.135.220.15]:38703 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752373AbeCNUYK (ORCPT ); Wed, 14 Mar 2018 16:24:10 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 89D87B01D; Wed, 14 Mar 2018 20:24:08 +0000 (UTC) From: NeilBrown To: Greg Kroah-Hartman Date: Thu, 15 Mar 2018 07:22:36 +1100 Subject: [PATCH 10/13] staging: mt7621-eth: add mdio support for mt762X family Cc: devel@driverdev.osuosl.org, lkml , John Crispin Message-ID: <152105895630.22262.17721435026559861824.stgit@noble> In-Reply-To: <152105892255.22262.1902152685410223215.stgit@noble> References: <152105892255.22262.1902152685410223215.stgit@noble> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: John Crispin NeilBrown: this patch originally contained soc-mt7620.c but as I cannot test that, I removed it. Some functions from mdio-mt7620.c are needed for soc-mt7621.c support - fixed mt7620_has_carrier() to read correct register. Original comment: Add support for SoCs from the mt7620 family. These all have one dedicated external gbit port and a builtin 5 port 100mbit switch. Additionally one of the 5 switch ports can be changed to become an additional gbit port that we can attach a phy to. MT7620 was the first SoC released after Ralink was acquired by MTK and has seen a lot of changes to the core. With MT7620 we have seen the addition of some advanced features such as TX vlan offloading, RX scatter gather and TSO. Newer MTK SoCs are based on this design. Although the builtin MT7530 is gbit capable, the builtin PHYs are only 100mbit. There are boards in the wild that use one of the gbit MACs to attach an external MT7530. For this to work a few hacks need to be applied to reorganize the MDIO address mappings and autopolling for the SoC to correctly work with the external switch. This is however not part of the series and will be part of a later series once we evaluated if we want to use DSA or switchdev. Signed-off-by: John Crispin Signed-off-by: Felix Fietkau Signed-off-by: Michael Lee Signed-off-by: NeilBrown --- drivers/staging/mt7621-eth/TODO | 2 drivers/staging/mt7621-eth/mdio_mt7620.c | 173 ++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 drivers/staging/mt7621-eth/mdio_mt7620.c diff --git a/drivers/staging/mt7621-eth/TODO b/drivers/staging/mt7621-eth/TODO index 1ab0530131ae..50fb5a959ee8 100644 --- a/drivers/staging/mt7621-eth/TODO +++ b/drivers/staging/mt7621-eth/TODO @@ -4,5 +4,7 @@ - general code review and clean up - add support for second MAC on mt7621 - convert gsw code to use switchdev interfaces +- md7620_mmi_write etc should probably be wrapped + in a regmap abstraction. Cc: NeilBrown diff --git a/drivers/staging/mt7621-eth/mdio_mt7620.c b/drivers/staging/mt7621-eth/mdio_mt7620.c new file mode 100644 index 000000000000..ced605c2914e --- /dev/null +++ b/drivers/staging/mt7621-eth/mdio_mt7620.c @@ -0,0 +1,173 @@ +/* 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; version 2 of the License + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Copyright (C) 2009-2016 John Crispin + * Copyright (C) 2009-2016 Felix Fietkau + * Copyright (C) 2013-2016 Michael Lee + */ + +#include +#include +#include + +#include "mtk_eth_soc.h" +#include "gsw_mt7620.h" +#include "mdio.h" + +static int mt7620_mii_busy_wait(struct mt7620_gsw *gsw) +{ + unsigned long t_start = jiffies; + + while (1) { + if (!(mtk_switch_r32(gsw, + gsw->piac_offset + MT7620_GSW_REG_PIAC) & + GSW_MDIO_ACCESS)) + return 0; + if (time_after(jiffies, t_start + GSW_REG_PHY_TIMEOUT)) + break; + } + + dev_err(gsw->dev, "mdio: MDIO timeout\n"); + return -1; +} + +u32 _mt7620_mii_write(struct mt7620_gsw *gsw, u32 phy_addr, + u32 phy_register, u32 write_data) +{ + if (mt7620_mii_busy_wait(gsw)) + return -1; + + write_data &= 0xffff; + + mtk_switch_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_WRITE | + (phy_register << GSW_MDIO_REG_SHIFT) | + (phy_addr << GSW_MDIO_ADDR_SHIFT) | write_data, + MT7620_GSW_REG_PIAC); + + if (mt7620_mii_busy_wait(gsw)) + return -1; + + return 0; +} +EXPORT_SYMBOL_GPL(_mt7620_mii_write); + +u32 _mt7620_mii_read(struct mt7620_gsw *gsw, int phy_addr, int phy_reg) +{ + u32 d; + + if (mt7620_mii_busy_wait(gsw)) + return 0xffff; + + mtk_switch_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_READ | + (phy_reg << GSW_MDIO_REG_SHIFT) | + (phy_addr << GSW_MDIO_ADDR_SHIFT), + MT7620_GSW_REG_PIAC); + + if (mt7620_mii_busy_wait(gsw)) + return 0xffff; + + d = mtk_switch_r32(gsw, MT7620_GSW_REG_PIAC) & 0xffff; + + return d; +} +EXPORT_SYMBOL_GPL(_mt7620_mii_read); + +int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val) +{ + struct mtk_eth *eth = bus->priv; + struct mt7620_gsw *gsw = (struct mt7620_gsw *)eth->sw_priv; + + return _mt7620_mii_write(gsw, phy_addr, phy_reg, val); +} + +int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg) +{ + struct mtk_eth *eth = bus->priv; + struct mt7620_gsw *gsw = (struct mt7620_gsw *)eth->sw_priv; + + return _mt7620_mii_read(gsw, phy_addr, phy_reg); +} + +void mt7530_mdio_w32(struct mt7620_gsw *gsw, u32 reg, u32 val) +{ + _mt7620_mii_write(gsw, 0x1f, 0x1f, (reg >> 6) & 0x3ff); + _mt7620_mii_write(gsw, 0x1f, (reg >> 2) & 0xf, val & 0xffff); + _mt7620_mii_write(gsw, 0x1f, 0x10, val >> 16); +} +EXPORT_SYMBOL_GPL(mt7530_mdio_w32); + +u32 mt7530_mdio_r32(struct mt7620_gsw *gsw, u32 reg) +{ + u16 high, low; + + _mt7620_mii_write(gsw, 0x1f, 0x1f, (reg >> 6) & 0x3ff); + low = _mt7620_mii_read(gsw, 0x1f, (reg >> 2) & 0xf); + high = _mt7620_mii_read(gsw, 0x1f, 0x10); + + return (high << 16) | (low & 0xffff); +} +EXPORT_SYMBOL_GPL(mt7530_mdio_r32); + +void mt7530_mdio_m32(struct mt7620_gsw *gsw, u32 mask, u32 set, u32 reg) +{ + u32 val = mt7530_mdio_r32(gsw, reg); + + val &= ~mask; + val |= set; + mt7530_mdio_w32(gsw, reg, val); +} +EXPORT_SYMBOL_GPL(mt7530_mdio_m32); + +static unsigned char *mtk_speed_str(int speed) +{ + switch (speed) { + case 2: + case SPEED_1000: + return "1000"; + case 1: + case SPEED_100: + return "100"; + case 0: + case SPEED_10: + return "10"; + } + + return "? "; +} + +int mt7620_has_carrier(struct mtk_eth *eth) +{ + struct mt7620_gsw *gsw = (struct mt7620_gsw *)eth->sw_priv; + int i; + + for (i = 0; i < GSW_PORT6; i++) + if (mt7530_mdio_r32(gsw, GSW_REG_PORT_STATUS(i)) & 0x1) + return 1; + return 0; +} + +void mt7620_print_link_state(struct mtk_eth *eth, int port, int link, + int speed, int duplex) +{ + struct mt7620_gsw *gsw = eth->sw_priv; + + if (link) + dev_info(gsw->dev, "port %d link up (%sMbps/%s duplex)\n", + port, mtk_speed_str(speed), + (duplex) ? "Full" : "Half"); + else + dev_info(gsw->dev, "port %d link down\n", port); +} + +void mt7620_mdio_link_adjust(struct mtk_eth *eth, int port) +{ + mt7620_print_link_state(eth, port, eth->link[port], + eth->phy->speed[port], + (eth->phy->duplex[port] == DUPLEX_FULL)); +}