Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp959449pxb; Wed, 6 Apr 2022 05:21:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+Tp5V12ARfF81xWBXhtA5/R7TYvCpkd47HFRNxQz3X/QmVxavotX/4H3egyzAynfwaMCR X-Received: by 2002:a17:90a:5296:b0:1ca:78cc:7172 with SMTP id w22-20020a17090a529600b001ca78cc7172mr9747334pjh.99.1649247659865; Wed, 06 Apr 2022 05:20:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649247659; cv=none; d=google.com; s=arc-20160816; b=danyqXeFDDFoFwvK8f8w+Wn1QPVkLIQnDNclQnTX04nCoMeDafep4eengc18s4rWsp VH7D3NZRSEurN9XLAdIb4A7kubZgTrKipzzUnrz5fi9oIAthtrISICZVu37vV/NJ2iO3 5oPRrUEn8oDdVTvlwIirLdLf1X+VCxLgwEdR/oZjw9eUu+GmxuummN2D/bEEW7KiozxD MX7aWLU18vNIzdtKA+9VRFfctF5Df5TmM1f/0oBnbaPUNAUasPIdAnaipURpipmQSVBN C0wDG8iHWNW1SWIHGqKSxzlqrMp0ERukdSFuUzqq8kPo+T0Pku8TPiPuXU2U8m+R0E18 +OYQ== 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 :dkim-signature; bh=7WaKtmsAct4eFrA+KGGxilxHVP2Bwhzu8dzCAWhFx8o=; b=yO22CLqkyzZcxx1qvCbx4AcYixJK0UN/BRplrFvzJrIiv7BL1XXQSKZAQix+KZczjr DUvcOHZO5y4N6fCd8++Sg5O7u9eBLWKpmexnYhDWUF5N43ZENikF9Ae17rcw9G+u7bDF 8YrjAehI0CI6VHeLPVo0G9DdiS6U5xNgfyNDRTKisBwrHeoH/QsX8GM5JuqkcbRRFpeK +PA6n28rv5Dyax0XE6zdS1lINlTbZAo0zGdJ697aYl/QXaxUoY6vJK7pbJqd4crs5WVJ DVOGJEszP1P4C/yYrXkt+RHUgsbB06XYnRZKrwdgOLr99mrNf4rWb9HrVNqNtgcUj+ky sRAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@walle.cc header.s=mail2016061301 header.b=oYg7yuaM; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id p15-20020a170902e74f00b00153b2d1660csi10585012plf.532.2022.04.06.05.20.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:20:59 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@walle.cc header.s=mail2016061301 header.b=oYg7yuaM; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5BD895DE290; Wed, 6 Apr 2022 04:17:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344151AbiDEVVy (ORCPT + 99 others); Tue, 5 Apr 2022 17:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354841AbiDENH2 (ORCPT ); Tue, 5 Apr 2022 09:07:28 -0400 Received: from ssl.serverraum.org (ssl.serverraum.org [176.9.125.105]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67DA724F14; Tue, 5 Apr 2022 05:09:59 -0700 (PDT) Received: from mwalle01.kontron.local. (unknown [213.135.10.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by ssl.serverraum.org (Postfix) with ESMTPSA id ADE092224D; Tue, 5 Apr 2022 14:09:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=walle.cc; s=mail2016061301; t=1649160597; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7WaKtmsAct4eFrA+KGGxilxHVP2Bwhzu8dzCAWhFx8o=; b=oYg7yuaMfIQM3IfqfMlSOS+FFRinbYbWLjZ2z7mOaGm5ij6LMGR5XRgD/0FcrunnyZil/+ hjc/DVsyPEGkck2J2e1qhmXosWa43vLuvFJLU+R07V1WCoM0g/3pQCgK+anUImsp3yY1KM nlimXoK2aKyugaERn+/nRB9Tf9iP2Mw= From: Michael Walle To: "David S . Miller" , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Andrew Lunn , Heiner Kallweit , Russell King , Alexandre Belloni Cc: netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Michael Walle Subject: [PATCH net-next v3 3/3] net: phy: mscc-miim: add support to set MDIO bus frequency Date: Tue, 5 Apr 2022 14:09:51 +0200 Message-Id: <20220405120951.4044875-4-michael@walle.cc> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220405120951.4044875-1-michael@walle.cc> References: <20220405120951.4044875-1-michael@walle.cc> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Until now, the MDIO bus will have the hardware default bus frequency. Read the desired frequency of the bus from the device tree and configure it. Signed-off-by: Michael Walle Reviewed-by: Andrew Lunn --- drivers/net/mdio/mdio-mscc-miim.c | 58 +++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/drivers/net/mdio/mdio-mscc-miim.c b/drivers/net/mdio/mdio-mscc-miim.c index c9efcfa2a1ce..7fd979f68dc0 100644 --- a/drivers/net/mdio/mdio-mscc-miim.c +++ b/drivers/net/mdio/mdio-mscc-miim.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -30,6 +31,8 @@ #define MSCC_MIIM_CMD_VLD BIT(31) #define MSCC_MIIM_REG_DATA 0xC #define MSCC_MIIM_DATA_ERROR (BIT(16) | BIT(17)) +#define MSCC_MIIM_REG_CFG 0x10 +#define MSCC_MIIM_CFG_PRESCALE_MASK GENMASK(7, 0) #define MSCC_PHY_REG_PHY_CFG 0x0 #define PHY_CFG_PHY_ENA (BIT(0) | BIT(1) | BIT(2) | BIT(3)) @@ -50,6 +53,8 @@ struct mscc_miim_dev { int mii_status_offset; struct regmap *phy_regs; const struct mscc_miim_info *info; + struct clk *clk; + u32 bus_freq; }; /* When high resolution timers aren't built-in: we can't use usleep_range() as @@ -242,9 +247,32 @@ int mscc_miim_setup(struct device *dev, struct mii_bus **pbus, const char *name, } EXPORT_SYMBOL(mscc_miim_setup); +static int mscc_miim_clk_set(struct mii_bus *bus) +{ + struct mscc_miim_dev *miim = bus->priv; + unsigned long rate; + u32 div; + + /* Keep the current settings */ + if (!miim->bus_freq) + return 0; + + rate = clk_get_rate(miim->clk); + + div = DIV_ROUND_UP(rate, 2 * miim->bus_freq) - 1; + if (div == 0 || div & ~MSCC_MIIM_CFG_PRESCALE_MASK) { + dev_err(&bus->dev, "Incorrect MDIO clock frequency\n"); + return -EINVAL; + } + + return regmap_update_bits(miim->regs, MSCC_MIIM_REG_CFG, + MSCC_MIIM_CFG_PRESCALE_MASK, div); +} + static int mscc_miim_probe(struct platform_device *pdev) { struct regmap *mii_regmap, *phy_regmap = NULL; + struct device_node *np = pdev->dev.of_node; void __iomem *regs, *phy_regs; struct mscc_miim_dev *miim; struct resource *res; @@ -295,21 +323,47 @@ static int mscc_miim_probe(struct platform_device *pdev) if (!miim->info) return -EINVAL; - ret = of_mdiobus_register(bus, pdev->dev.of_node); + miim->clk = devm_clk_get_optional(&pdev->dev, NULL); + if (IS_ERR(miim->clk)) + return PTR_ERR(miim->clk); + + of_property_read_u32(np, "clock-frequency", &miim->bus_freq); + + if (miim->bus_freq && !miim->clk) { + dev_err(&pdev->dev, + "cannot use clock-frequency without a clock\n"); + return -EINVAL; + } + + ret = clk_prepare_enable(miim->clk); + if (ret) + return ret; + + ret = mscc_miim_clk_set(bus); + if (ret) + goto out_disable_clk; + + ret = of_mdiobus_register(bus, np); if (ret < 0) { dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret); - return ret; + goto out_disable_clk; } platform_set_drvdata(pdev, bus); return 0; + +out_disable_clk: + clk_disable_unprepare(miim->clk); + return ret; } static int mscc_miim_remove(struct platform_device *pdev) { struct mii_bus *bus = platform_get_drvdata(pdev); + struct mscc_miim_dev *miim = bus->priv; + clk_disable_unprepare(miim->clk); mdiobus_unregister(bus); return 0; -- 2.30.2