Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp482583pxb; Wed, 8 Sep 2021 05:48:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBYbejJQ9nnp/WcckJftXSzS6llGWH0Q/W9DiQBHH+cA4Q8DlawOhNqz1CgnaTE3852xDK X-Received: by 2002:a92:c144:: with SMTP id b4mr2814316ilh.67.1631105317730; Wed, 08 Sep 2021 05:48:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631105317; cv=none; d=google.com; s=arc-20160816; b=sevZmmtvwMyztmL+oD4R9BJQaHCBYw7QwRB8ega7ZtamG6Q76Hbhl5q6E49E3MMozF Su3TQUU1nPTvaCvFFEMBaK1kIcit2nKn6KO3lto+9IBJfqtBWw6/6SuDq7hSD6Wnhvfn FfBWpCSmDJHaP3WJEs25JJK4XxMZJT2d6QZdV6d+lgXyW045D+NFnKv3NAcK91MVXrJl C0Sst5hQG1O/e3CPRLPLAo7hZ+mbvzjGMUcnexprah17GnnIoD0nQeYxUetxMkrM7XjA joFttQ/ZWC/43wA9UifoA0cwFxO8WrIxfKjssJR3gj2aOB2uqAgxtqz0Y/CoHMiyzjKS 7WvA== 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=sCxKv+w+fXE2Tfe7i4Zzh1l2l9czR+DUJb0UynaUluk=; b=s4mA71nCPPxRocdB5LJPnEnv/9AWho4fF+BVybK7XWTYiYYiNO7lVnywBkPcqEB7zl JYrmd1C0+2GspW19iRS09n5KV0eYQ3pu+0Fv18iBob/gMHaMyS3ePwR35kySM0PFQCYI nK+VUdXoY5sdgo4dJu61iygT2fFf3vfILz/D7lcIBvDjC6roULsX1KF+ndxsLHM1BWIb nG7PUWqokdv7SiykExdHRsl89qFomyzrRrpcBCspDNpmqQR5S9YrF3/ONvC6Vp5Ff5FD gs2miPu0JdI35o5Ja4zACu9yD1i/11RWnTSyFmNykhY/m2xdnzYJi9xgbQB77XckguOi 6+lA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@microchip.com header.s=mchp header.b=dqnKVenU; 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 192si1575273ioc.91.2021.09.08.05.48.25; Wed, 08 Sep 2021 05:48:37 -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=dqnKVenU; 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 S1351602AbhIHLuW (ORCPT + 99 others); Wed, 8 Sep 2021 07:50:22 -0400 Received: from esa.microchip.iphmx.com ([68.232.154.123]:15976 "EHLO esa.microchip.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349298AbhIHLuL (ORCPT ); Wed, 8 Sep 2021 07:50:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1631101744; x=1662637744; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=/HGcvjBRlRWmlO7DpcZMdq/0nGMUZASrevuJOV+aBGA=; b=dqnKVenU9RCkWKr2r0Jnrw6XMmiP2b5cgbZq0ByQLX5+dzOIdOAhrCII QUpXm6rHh8Xgy8KVH3stJBXh5zdkWAe80BMIZ5KgK42prQlncxQ+13Kou qxhg91eLK8NnHL7LbCOhxVhoyJ9pF+Re4uB6/Ch3sRBQTrrXzdqwjEiJV uvO3pWMIwg5uFOKZ1mwfAnn4a7/a3eSMlRLIlKlfMYcagVZxxPYBXXSl8 RZ4qQPlFaKH/7+7pjG83zv+dO59b1+6hAF5IullkCWd5DSU1CENPG0sLx xoUJVTxhF1PkXMXqFSYmDUgdqSOcfYdZYVVHiVC7R57ZfwUuy1rsoed9P g==; IronPort-SDR: oCDQ7A6eX7ttNYxTDSmz/FyhlwFFW1w8eJnbRc5sUrKxjJt0m67MO1q3Kj8Y5WnulopK43trMu AOdztl1qD9nZ+kcmCb5HhGGT4yK6Y8Dvvf6w9LWcn1ozViw+XpoT29/ZW64WsuD+oGktsbNu7e ken7fexlikeeIYLZdJoC9c/7UhHTmp0TGhCLPztk8/X2RGuavhF0lBoUVGvZljLiwfB1mPoTSn 318Z9G3SnFHP7+BfDC0FA7WSbiX8YQt9Q2kSzpDgG3SQWTQjG4o10S6SWqxCu2OzfOytLRqplX j7QZJG8iJHmbeowDN+JC6+Q5 X-IronPort-AV: E=Sophos;i="5.85,277,1624345200"; d="scan'208";a="131094788" Received: from smtpout.microchip.com (HELO email.microchip.com) ([198.175.253.82]) by esa2.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 08 Sep 2021 04:49:03 -0700 Received: from chn-vm-ex02.mchp-main.com (10.10.85.144) 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; Wed, 8 Sep 2021 04:49:03 -0700 Received: from kavya-HP-Compaq-6000-Pro-SFF-PC.microchip.com (10.10.115.15) by chn-vm-ex02.mchp-main.com (10.10.85.144) with Microsoft SMTP Server id 15.1.2176.14 via Frontend Transport; Wed, 8 Sep 2021 04:48:59 -0700 From: Kavyasree Kotagiri To: , , CC: , , , , , , Subject: [PATCH v2 2/3] clk: lan966x: Add lan966x SoC clock driver Date: Wed, 8 Sep 2021 17:18:43 +0530 Message-ID: <20210908114844.22131-3-kavyasree.kotagiri@microchip.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210908114844.22131-1-kavyasree.kotagiri@microchip.com> References: <20210908114844.22131-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 --- v1 -> v2: - No changes. drivers/clk/clk-lan966x.c | 235 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 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..4492be90cecf --- /dev/null +++ b/drivers/clk/clk-lan966x.c @@ -0,0 +1,235 @@ +// 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 struct clk_init_data init; +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; + const char *parent_names[3]; + 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; + init.num_parents = 3; + + for (i = 0; i < init.num_parents; ++i) { + parent_names[i] = of_clk_get_parent_name(pdev->dev.of_node, i); + if (!parent_names[i]) + return -EINVAL; + } + + init.parent_names = parent_names; + 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