Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2592754pxb; Fri, 17 Sep 2021 13:32:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxvDsa45U+3MgfbZEYvTZUT/D5KEzSLcetKK/TSz74J85ovWyuprpfppP/hheuIz0HFsxWI X-Received: by 2002:a5d:8f91:: with SMTP id l17mr9974018iol.121.1631910731294; Fri, 17 Sep 2021 13:32:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631910731; cv=none; d=google.com; s=arc-20160816; b=nu8kPmXOZaTNRc+N1/raOoHqfvF0Mv5qwTlBUlD/Zk7CC7tuwz60L0jI1kqcwwdrvG kYNhOVHznAMYZ96L29ohlqm5SQM6aAVOQkkQQ049kwbUxVIrl8Mcnrqz8MvmoAroTl5G 4U8bq3ICBUAfvFM3+6VAEp0+J77mtqG6wDoKUC2INMM9dole2i4oi2mPUSUjTe2dpSR0 kXGNtowEYxYsdi8Ie8+8wDPY/gOlAHaKRvFhAtvvBKLhW+W3zdkrwJv62S/1VUptezLY PzZ36W6e3xv1cI2PfW24OAGpXeZ8jq0qptTBPgyKYdzyWEKz47CKRdP+UNL1DQ/iTs+x vNcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:ironport-sdr:dkim-signature; bh=C+Ipsr9MWCBIReIiIdm/JXfyrMPITaUdzhjzUOXFSVU=; b=DQs0bJwvi0QI64+tQxPLxto479+x4uC29iQ4+5MQWiFex3d8Gp98ySdYHUQQ/XvfWx VeqHfTTX3o7AzPsAm7Rt6Be3kYJGxAjH/xRBDsH5+Tnx7WY9HydXHu/fhB+5tU3EkQ9c 5dasTUHJS2bGUbMJzEl/Cfv56WQSj1SG9J6Pp8AXSk93lUpS/igBryDMFOdtNmI3v7Ep 0dssnmVG8TGAsiETajnMmVW8E36QjuRkROkni5WCTLdwuHJ3xV1pzrUvM02g+YVU8PzS SCsXWRpQI8LrrJOP87qHuXNmqxK2FrjPmPmDCPp/++cOqukfxWvcvpWk7hvFBFwAIR8e 4Zqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@microchip.com header.s=mchp header.b=aRO+pDNL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o1si7224661ilk.81.2021.09.17.13.31.23; Fri, 17 Sep 2021 13:32:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@microchip.com header.s=mchp header.b=aRO+pDNL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244084AbhIQMYB (ORCPT + 99 others); Fri, 17 Sep 2021 08:24:01 -0400 Received: from esa.microchip.iphmx.com ([68.232.154.123]:26772 "EHLO esa.microchip.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243593AbhIQMXy (ORCPT ); Fri, 17 Sep 2021 08:23:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1631881352; x=1663417352; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=EAL4TcgvQ5ldll0LpCCOFs+B+WDb8XB728u5ME2731A=; b=aRO+pDNL7YzdfrHlgZn44JJgrA1uVeDxnGm3wemFJoe0VUUuzTgQv+Eh nF3z91Hh6wm2rG6WZ+gtZtGkfUHSxxYn1YQYQwqituAtbtywsWgNju+B2 WY7iVfYhmTsk7CWGwgiL9/1UirJL38t0Fv5Ytbw7Yo/iYwBlEzrG7R73D PknfOJyFcNuO4ajLNzTYXb0YcdVrIqPYczGr00n8AImN9UkPcvCbdZgkJ dwBc5B1lZd725+vwuyZFCBTSv4Lc3wtfeDGZmr3BklAMfU+jgrfHZmx6g 8L6Hr9uq3TjHiLyxJ60v7aTK28z35wYABj03UgmNLfhXDfB+53FnKApIE g==; IronPort-SDR: vwuH/1bLfGbQ+tyakf0yIgMlO0jA9uEZq64gySzHdOXkTMc4NkkS7j4JkaR+N2iliBVM642drg FpsQdXoIMZPalq6JdBFfiLifwk6NQa8b67S44D14/lGGdzNYtsMllvkDRSMXx01/SoeU4WnQop e2Whd8vSOOkMIVFoIWfojGHLOyYwmimxLrUTatHw1WwfsFxhaxxxzaUWUF4pNpw06LcAZwByoZ HIJWMd/69MdDT8PqP3fAjZqc8PgNe37z17ZwtLnZjRC5654vItzJ1da3xdvvpGF0ZX7h0TAGdc 3KeAI+JKtnubCMaSMP+uCWfd X-IronPort-AV: E=Sophos;i="5.85,301,1624345200"; d="scan'208";a="69666041" Received: from smtpout.microchip.com (HELO email.microchip.com) ([198.175.253.82]) by esa6.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 17 Sep 2021 05:22:31 -0700 Received: from chn-vm-ex03.mchp-main.com (10.10.85.151) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.14; Fri, 17 Sep 2021 05:22:31 -0700 Received: from kavya-HP-Compaq-6000-Pro-SFF-PC.microchip.com (10.10.115.15) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server id 15.1.2176.14 via Frontend Transport; Fri, 17 Sep 2021 05:22:28 -0700 From: Kavyasree Kotagiri To: , , CC: , , , , , , Subject: [PATCH v4 3/3] clk: lan966x: Add lan966x SoC clock driver Date: Fri, 17 Sep 2021 17:51:57 +0530 Message-ID: <20210917122157.557-4-kavyasree.kotagiri@microchip.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210917122157.557-1-kavyasree.kotagiri@microchip.com> References: <20210917122157.557-1-kavyasree.kotagiri@microchip.com> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds Generic Clock Controller driver for lan966x SoC. Lan966x clock controller contains 3 PLLs - cpu_clk, ddr_clk and sys_clk. It generates and supplies clock to various peripherals within SoC. Register settings required to provide GCK clocking to a peripheral is as below: GCK_SRC_SEL = Select clock source. GCK_PRESCALER = Set divider value. GCK_ENA = 1 - Enable GCK clock. Signed-off-by: Kavyasree Kotagiri Signed-off-by: Horatiu Vultur Co-developed-by: Horatiu Vultur --- v3 -> v4: - Used clk_parent_data instead of of_clk_get_parent_name(). v2 -> v3: - Fixed dt_binding_check errors. v1 -> v2: - Updated example provided for clk controller DT node. drivers/clk/clk-lan966x.c | 236 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 drivers/clk/clk-lan966x.c diff --git a/drivers/clk/clk-lan966x.c b/drivers/clk/clk-lan966x.c new file mode 100644 index 000000000000..08720bc152f5 --- /dev/null +++ b/drivers/clk/clk-lan966x.c @@ -0,0 +1,236 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Microchip LAN966x SoC Clock driver. + * + * Copyright (C) 2021 Microchip Technology, Inc. and its subsidiaries + * + * Author: Kavyasree Kotagiri + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define GCK_ENA BIT(0) +#define GCK_SRC_SEL GENMASK(9, 8) +#define GCK_PRESCALER GENMASK(23, 16) + +static const char *clk_names[N_CLOCKS] = { + "qspi0", "qspi1", "qspi2", "sdmmc0", + "pi", "mcan0", "mcan1", "flexcom0", + "flexcom1", "flexcom2", "flexcom3", + "flexcom4", "timer", "usb_refclk", +}; + +struct lan966x_gck { + struct clk_hw hw; + void __iomem *reg; +}; +#define to_lan966x_gck(hw) container_of(hw, struct lan966x_gck, hw) + +static const struct clk_parent_data lan966x_gck_pdata[] = { + { .fw_name = "cpu_clk", .name = "cpu_clk" }, + { .fw_name = "ddr_clk", .name = "ddr_clk" }, + { .fw_name = "sys_clk", .name = "sys_clk" }, +}; + +static struct clk_init_data init = { + .parent_data = lan966x_gck_pdata, + .num_parents = ARRAY_SIZE(lan966x_gck_pdata), +}; + +static void __iomem *base; + +static int lan966x_gck_enable(struct clk_hw *hw) +{ + struct lan966x_gck *gck = to_lan966x_gck(hw); + u32 val = readl(gck->reg); + + val |= GCK_ENA; + writel(val, gck->reg); + + return 0; +} + +static void lan966x_gck_disable(struct clk_hw *hw) +{ + struct lan966x_gck *gck = to_lan966x_gck(hw); + u32 val = readl(gck->reg); + + val &= ~GCK_ENA; + writel(val, gck->reg); +} + +static int lan966x_gck_set_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate) +{ + struct lan966x_gck *gck = to_lan966x_gck(hw); + u32 div, val = readl(gck->reg); + + if (rate == 0 || parent_rate == 0) + return -EINVAL; + + /* Set Prescalar */ + div = parent_rate / rate; + val &= ~GCK_PRESCALER; + val |= FIELD_PREP(GCK_PRESCALER, (div - 1)); + writel(val, gck->reg); + + return 0; +} + +static long lan966x_gck_round_rate(struct clk_hw *hw, unsigned long rate, + unsigned long *parent_rate) +{ + unsigned int div; + + if (rate == 0 || *parent_rate == 0) + return -EINVAL; + + if (rate >= *parent_rate) + return *parent_rate; + + div = DIV_ROUND_CLOSEST(*parent_rate, rate); + + return *parent_rate / div; +} + +static unsigned long lan966x_gck_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct lan966x_gck *gck = to_lan966x_gck(hw); + u32 div, val = readl(gck->reg); + + div = FIELD_GET(GCK_PRESCALER, val); + + return parent_rate / (div + 1); +} + +static int lan966x_gck_determine_rate(struct clk_hw *hw, + struct clk_rate_request *req) +{ + struct clk_hw *parent; + int i; + + for (i = 0; i < clk_hw_get_num_parents(hw); ++i) { + parent = clk_hw_get_parent_by_index(hw, i); + if (!parent) + continue; + + if (clk_hw_get_rate(parent) / req->rate < 254) { + req->best_parent_hw = parent; + req->best_parent_rate = clk_hw_get_rate(parent); + + return 0; + } + } + + return -EINVAL; +} + +static u8 lan966x_gck_get_parent(struct clk_hw *hw) +{ + struct lan966x_gck *gck = to_lan966x_gck(hw); + u32 val = readl(gck->reg); + + return FIELD_GET(GCK_SRC_SEL, val); +} + +static int lan966x_gck_set_parent(struct clk_hw *hw, u8 index) +{ + struct lan966x_gck *gck = to_lan966x_gck(hw); + u32 val = readl(gck->reg); + + val &= ~GCK_SRC_SEL; + val |= FIELD_PREP(GCK_SRC_SEL, index); + writel(val, gck->reg); + + return 0; +} + +static const struct clk_ops lan966x_gck_ops = { + .enable = lan966x_gck_enable, + .disable = lan966x_gck_disable, + .set_rate = lan966x_gck_set_rate, + .round_rate = lan966x_gck_round_rate, + .recalc_rate = lan966x_gck_recalc_rate, + .determine_rate = lan966x_gck_determine_rate, + .set_parent = lan966x_gck_set_parent, + .get_parent = lan966x_gck_get_parent, +}; + +static struct clk_hw *lan966x_gck_clk_register(struct device *dev, int i) +{ + struct lan966x_gck *priv; + int ret; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return ERR_PTR(-ENOMEM); + + priv->reg = base + (i * 4); + priv->hw.init = &init; + ret = devm_clk_hw_register(dev, &priv->hw); + if (ret) + return ERR_PTR(ret); + + return &priv->hw; +}; + +static int lan966x_clk_probe(struct platform_device *pdev) +{ + struct clk_hw_onecell_data *hw_data; + struct device *dev = &pdev->dev; + int i, ret; + + hw_data = devm_kzalloc(dev, sizeof(*hw_data), GFP_KERNEL); + if (!hw_data) + return -ENOMEM; + + base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(base)) + return PTR_ERR(base); + + init.ops = &lan966x_gck_ops; + + hw_data->num = N_CLOCKS; + + for (i = 0; i < N_CLOCKS; i++) { + init.name = clk_names[i]; + hw_data->hws[i] = lan966x_gck_clk_register(dev, i); + if (IS_ERR(hw_data->hws[i])) { + dev_err(dev, "failed to register %s clock\n", + init.name); + return ret; + } + } + + return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, hw_data); +} + +static const struct of_device_id lan966x_clk_dt_ids[] = { + { .compatible = "microchip,lan966x-gck", }, + { } +}; +MODULE_DEVICE_TABLE(of, lan966x_clk_dt_ids); + +static struct platform_driver lan966x_clk_driver = { + .probe = lan966x_clk_probe, + .driver = { + .name = "lan966x-clk", + .of_match_table = lan966x_clk_dt_ids, + }, +}; +builtin_platform_driver(lan966x_clk_driver); + +MODULE_AUTHOR("Kavyasree Kotagiri "); +MODULE_DESCRIPTION("LAN966X clock driver"); +MODULE_LICENSE("GPL v2"); -- 2.17.1