Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp1220092pxa; Fri, 28 Aug 2020 07:05:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxT26eeY62UkIp/w7lNUk/krYkVACozZJx6iAIdmex3Zj7lnOyswHr/xMx+jp7zAikIDTOS X-Received: by 2002:a05:6402:212:: with SMTP id t18mr2133305edv.124.1598623539246; Fri, 28 Aug 2020 07:05:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1598623539; cv=pass; d=google.com; s=arc-20160816; b=VBYiUsNmH3fQZUSpC3ipMnZPSNJgBiv+iVRwk49/aMwRrz8vFT4wp4Y0pZQM5V5ZRP irCiVQKv3SJ2QSZjYx1TjWKbtv94sgbTCvVfHOP7YVvivRiHuyoZNaIkjoNDj3FtizFa GAg5fHM7a9/y6er4LRTiN+BdhnjeQP3arsolM4fdiNfX5aJ6pSDEV48kd6D9YORMrIb4 VGy9SCtoTHRPfC7dJBMS+E2VvVKRKRdyDJUtoxJ5b0HKflk6j/RMEOGaVWoOyUlttPI3 3ESTv2SamrA3nsQNGbtvjka8xKMverM40fKG4fKj69r5k+mmG2HgjpGLY9Rg0RzQeRWL 3ChA== ARC-Message-Signature: i=2; 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:dkim-signature; bh=AMXxih5zdv5AJGuaTL6vfN7YXDcbwH7Q2C92U2ZlbaU=; b=lhC2VVF8SPGwq+ZtBlyeJiFce8VcsogUXhI3Z2Y7d84jDgrypA+EiQtZSpTdo9aCMa 7T812hk6w70t2UEoRKvdpt+t2znZpxWtd0YN2I9Ftj8B5YTgyJEAr2Typ3WP7clXNyUG Om7kf1zdnAFxHl3Y9JKUO3HCAHWUMJ42MiJ2K8rfg2gSEs9OAZerPa+ztvBfPMkSZgef ztUA1Ho2UHmRiKckOhWuOLzx3j+cIeSsS3i3FEEEIBEtw1M1eRNu1UwGj92zAQuwieNc F1fO6lXNirREVQTd/5/FDk52GumT2jRBpIvs4fedn6+FtlOdBT8isiw4WMQGI2Yxsfbc eODA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@xilinx.onmicrosoft.com header.s=selector2-xilinx-onmicrosoft-com header.b=UnFvEsaR; arc=pass (i=1 spf=pass spfdomain=xilinx.com); 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g26si618205edr.126.2020.08.28.07.05.13; Fri, 28 Aug 2020 07:05:39 -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=pass header.i=@xilinx.onmicrosoft.com header.s=selector2-xilinx-onmicrosoft-com header.b=UnFvEsaR; arc=pass (i=1 spf=pass spfdomain=xilinx.com); 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726851AbgH1OBs (ORCPT + 99 others); Fri, 28 Aug 2020 10:01:48 -0400 Received: from mail-dm6nam10on2062.outbound.protection.outlook.com ([40.107.93.62]:29664 "EHLO NAM10-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725857AbgH1N7z (ORCPT ); Fri, 28 Aug 2020 09:59:55 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VbDEr6ZPHYvt4CLTT6xfLeYi5n39EtYSah8Ap+NLKx9XAQ1VG2FE/MFPaoMgybHeotNbvuruRTa0+AhK5o67XTyT85kN8chZIjZwCfcvSG7edY0uo2wrSlmuC4Qb9XBZ1knpR57g9QcshhYG+PoBMn46pK5RzZ4BoWx9DG5Nk39EaOKdnAxBvcchSwWnon/McERxRR1T1ZUW/FC6xwMW+F6iyeGw20M446kVG6s3fXOvGKD0cttDOGzZYxAVKAi48YDrXabJflQGHRk2Va6wKJWcgsRVsABuz6d5nW3QBs0vm7g3QsIrdOvcrK1fl/wd51bd8Vbu24HNs0NoBvx+Rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AMXxih5zdv5AJGuaTL6vfN7YXDcbwH7Q2C92U2ZlbaU=; b=N0SJJESLCTlbG9YfbLUPZFK3j1SPBDcDW3grCiZGjsmkvq6eU/2pu0KCYHyfY2IcW+RZgx48+3D7TDqtEVg4mnmDg6U0CK2Ge6HD3Fqw1c5su72NkO9YJ4bHEs0LPu4W3ooa1D3+6fJyr761xXMG6XAkBfOZ5K7LBZ1VJfgmrPHIastRzGxcWINsIzqBhSypcliEliX4Uo2Uk50IVmG0AxsIU6mFmekjG0OlXC9TG/yjumYN6+N4J5S2HdjTl6hQZ9eZaHtKaoLrYLBcjdFSyOvPu0yPIYaV+8UK92q8zBog/WIl33HP43CgEz0Ecgf6TwAogkq12vHDxq4rGVu8aA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 149.199.60.83) smtp.rcpttodomain=baylibre.com smtp.mailfrom=xilinx.com; dmarc=bestguesspass action=none header.from=xilinx.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xilinx.onmicrosoft.com; s=selector2-xilinx-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AMXxih5zdv5AJGuaTL6vfN7YXDcbwH7Q2C92U2ZlbaU=; b=UnFvEsaROFpmSi0ifX07YEnf4x+lfLiZQZiqqetN+FhYw9m8JMPluLZLY5wqBE7wBzBxVJwJ/MpAqtqKmn3fLq77EvQ45nhq6eUN3YEGwm4VdIKlZHAOMiKc4AWqHIAioXHkTSJBEeqLgJpCDSAOR355B611LwTM0AneeBRSoZk= Received: from SN1PR12CA0076.namprd12.prod.outlook.com (2603:10b6:802:20::47) by MN2PR02MB6926.namprd02.prod.outlook.com (2603:10b6:208:201::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3326.19; Fri, 28 Aug 2020 13:40:33 +0000 Received: from SN1NAM02FT060.eop-nam02.prod.protection.outlook.com (2603:10b6:802:20:cafe::86) by SN1PR12CA0076.outlook.office365.com (2603:10b6:802:20::47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3326.19 via Frontend Transport; Fri, 28 Aug 2020 13:40:33 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 149.199.60.83) smtp.mailfrom=xilinx.com; baylibre.com; dkim=none (message not signed) header.d=none;baylibre.com; dmarc=bestguesspass action=none header.from=xilinx.com; Received-SPF: Pass (protection.outlook.com: domain of xilinx.com designates 149.199.60.83 as permitted sender) receiver=protection.outlook.com; client-ip=149.199.60.83; helo=xsj-pvapsmtpgw01; Received: from xsj-pvapsmtpgw01 (149.199.60.83) by SN1NAM02FT060.mail.protection.outlook.com (10.152.72.192) with Microsoft SMTP Server id 15.20.3326.21 via Frontend Transport; Fri, 28 Aug 2020 13:40:33 +0000 Received: from [149.199.38.66] (port=59848 helo=smtp.xilinx.com) by xsj-pvapsmtpgw01 with esmtp (Exim 4.90) (envelope-from ) id 1kBebn-0004sl-2A; Fri, 28 Aug 2020 06:39:59 -0700 Received: from [127.0.0.1] (helo=localhost) by smtp.xilinx.com with smtp (Exim 4.63) (envelope-from ) id 1kBecL-0006Ll-3O; Fri, 28 Aug 2020 06:40:33 -0700 Received: from xsj-pvapsmtp01 (xsj-mail.xilinx.com [149.199.38.66]) by xsj-smtp-dlp1.xlnx.xilinx.com (8.13.8/8.13.1) with ESMTP id 07SDeLjg028568; Fri, 28 Aug 2020 06:40:22 -0700 Received: from [10.140.6.59] (helo=xhdshubhraj40.xilinx.com) by xsj-pvapsmtp01 with esmtp (Exim 4.63) (envelope-from ) id 1kBec9-0005it-Ld; Fri, 28 Aug 2020 06:40:21 -0700 From: Shubhrajyoti Datta To: linux-clk@vger.kernel.org Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, devel@driverdev.osuosl.org, sboyd@kernel.org, robh+dt@kernel.org, gregkh@linuxfoundation.org, mturquette@baylibre.com, Shubhrajyoti Datta , Chirag Parekh Subject: [PATCH v6 4/8] clk: clock-wizard: Add support for dynamic reconfiguration Date: Fri, 28 Aug 2020 19:09:52 +0530 Message-Id: <1598621996-31040-5-git-send-email-shubhrajyoti.datta@xilinx.com> X-Mailer: git-send-email 2.1.1 In-Reply-To: <1598621996-31040-1-git-send-email-shubhrajyoti.datta@xilinx.com> References: <1598621996-31040-1-git-send-email-shubhrajyoti.datta@xilinx.com> X-RCIS-Action: ALLOW X-TM-AS-Product-Ver: IMSS-7.1.0.1224-8.2.0.1013-23620.005 X-TM-AS-User-Approved-Sender: Yes;Yes X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-MS-PublicTrafficType: Email MIME-Version: 1.0 Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: a123f2f9-ec81-4e87-8124-08d84b57f007 X-MS-TrafficTypeDiagnostic: MN2PR02MB6926: X-Microsoft-Antispam-PRVS: X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-MS-Oob-TLC-OOBClassifiers: OLM:1201; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Q6p7qBtllTUB1djGJphVxgYrh2XZAtc3drFObGXMNNw2w9P/oaMcUo9adX3+DihFqs0iFzYt7dpiadq0MxiL8ThFYZX29ZTCRUrHfksJs6hpxV6eOVaaiZ6UwshKdrtbrmtWp8h3Eqf9dYcN1QJZxyxTHqYUT3vEcbXyCtq4S5asgVBsG8/WLsiuBmToqLQDwgAzpCQ68QqqYicaXR9p0jJSwVhBCVYlwSrrFpS5yexzoAqykoydjgdnw76k1HJb8X1IC7L6lO7MlWgkp/OuN/bJoZOw8GHqoyUSyRFSpC318GGeGSVFhQ42USZnycBuA20h8ilzBUrERE5emWUyR0Nia5RmI7LzoFioMFkXM7h4nIAxi7IinrUEX30I9A1+os+FLghyCWQArBdp0cjcvA== X-Forefront-Antispam-Report: CIP:149.199.60.83;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:xsj-pvapsmtpgw01;PTR:unknown-60-83.xilinx.com;CAT:NONE;SFS:(136003)(39860400002)(396003)(376002)(346002)(46966005)(5660300002)(36756003)(2616005)(478600001)(186003)(6666004)(70206006)(83380400001)(70586007)(82310400002)(44832011)(82740400003)(47076004)(7696005)(6916009)(426003)(336012)(9786002)(8676002)(8936002)(4326008)(107886003)(316002)(54906003)(2906002)(81166007)(26005)(356005);DIR:OUT;SFP:1101; X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Aug 2020 13:40:33.4076 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a123f2f9-ec81-4e87-8124-08d84b57f007 X-MS-Exchange-CrossTenant-Id: 657af505-d5df-48d0-8300-c31994686c5c X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=657af505-d5df-48d0-8300-c31994686c5c;Ip=[149.199.60.83];Helo=[xsj-pvapsmtpgw01] X-MS-Exchange-CrossTenant-AuthSource: SN1NAM02FT060.eop-nam02.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR02MB6926 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The patch adds support for dynamic reconfiguration of clock output rate. Output clocks are registered as dividers and set rate callback function is used for dynamic reconfiguration. Based on the initial work from Chirag. Signed-off-by: Chirag Parekh Signed-off-by: Shubhrajyoti Datta --- v6: Remove the typecast. use min for capping frequency. use polled timeout drivers/clk/clk-xlnx-clock-wizard.c | 185 ++++++++++++++++++++++++++++++++++-- 1 file changed, 179 insertions(+), 6 deletions(-) diff --git a/drivers/clk/clk-xlnx-clock-wizard.c b/drivers/clk/clk-xlnx-clock-wizard.c index d6577c8..8dfcec8 100644 --- a/drivers/clk/clk-xlnx-clock-wizard.c +++ b/drivers/clk/clk-xlnx-clock-wizard.c @@ -17,6 +17,7 @@ #include #include #include +#include #define WZRD_NUM_OUTPUTS 7 #define WZRD_ACLK_MAX_FREQ 250000000UL @@ -31,8 +32,23 @@ #define WZRD_DIVCLK_DIVIDE_SHIFT 0 #define WZRD_DIVCLK_DIVIDE_MASK (0xff << WZRD_DIVCLK_DIVIDE_SHIFT) #define WZRD_CLKOUT_DIVIDE_SHIFT 0 +#define WZRD_CLKOUT_DIVIDE_WIDTH 8 #define WZRD_CLKOUT_DIVIDE_MASK (0xff << WZRD_DIVCLK_DIVIDE_SHIFT) +#define WZRD_DR_MAX_INT_DIV_VALUE 255 +#define WZRD_DR_NUM_RETRIES 10000 +#define WZRD_DR_STATUS_REG_OFFSET 0x04 +#define WZRD_DR_LOCK_BIT_MASK 0x00000001 +#define WZRD_DR_INIT_REG_OFFSET 0x25C +#define WZRD_DR_DIV_TO_PHASE_OFFSET 4 +#define WZRD_DR_BEGIN_DYNA_RECONF 0x03 + +/* Get the mask from width */ +#define div_mask(width) ((1 << (width)) - 1) + +/* Extract divider instance from clock hardware instance */ +#define to_clk_wzrd_divider(_hw) container_of(_hw, struct clk_wzrd_divider, hw) + enum clk_wzrd_int_clks { wzrd_clk_mul, wzrd_clk_mul_div, @@ -64,6 +80,29 @@ struct clk_wzrd { bool suspended; }; +/** + * struct clk_wzrd_divider - clock divider specific to clk_wzrd + * + * @hw: handle between common and hardware-specific interfaces + * @base: base address of register containing the divider + * @offset: offset address of register containing the divider + * @shift: shift to the divider bit field + * @width: width of the divider bit field + * @flags: clk_wzrd divider flags + * @table: array of value/divider pairs, last entry should have div = 0 + * @lock: register lock + */ +struct clk_wzrd_divider { + struct clk_hw hw; + void __iomem *base; + u16 offset; + u8 shift; + u8 width; + u8 flags; + const struct clk_div_table *table; + spinlock_t *lock; /* divider lock */ +}; + #define to_clk_wzrd(_nb) container_of(_nb, struct clk_wzrd, nb) /* maximum frequencies for input/output clocks per speed grade */ @@ -73,6 +112,136 @@ static const unsigned long clk_wzrd_max_freq[] = { 1066000000UL }; +/* spin lock variable for clk_wzrd */ +static DEFINE_SPINLOCK(clkwzrd_lock); + +static unsigned long clk_wzrd_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw); + void __iomem *div_addr = divider->base + divider->offset; + unsigned int val; + + val = readl(div_addr) >> divider->shift; + val &= div_mask(divider->width); + + return divider_recalc_rate(hw, parent_rate, val, divider->table, + divider->flags, divider->width); +} + +static int clk_wzrd_dynamic_reconfig(struct clk_hw *hw, unsigned long rate, + unsigned long parent_rate) +{ + int err = 0; + u32 value; + struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw); + void __iomem *div_addr = divider->base + divider->offset; + + value = DIV_ROUND_CLOSEST(parent_rate, rate); + + /* Cap the value to max */ + min(value, (u32)WZRD_DR_MAX_INT_DIV_VALUE); + + /* Set divisor and clear phase offset */ + writel(value, div_addr); + writel(0x00, div_addr + WZRD_DR_DIV_TO_PHASE_OFFSET); + + /* Check status register */ + err = readl_poll_timeout(divider->base + WZRD_DR_STATUS_REG_OFFSET, value, + value & WZRD_DR_LOCK_BIT_MASK, + WZRD_USEC_POLL, WZRD_TIMEOUT_POLL); + if (err) + return err; + + /* Initiate reconfiguration */ + writel(WZRD_DR_BEGIN_DYNA_RECONF, + divider->base + WZRD_DR_INIT_REG_OFFSET); + + /* Check status register */ + err = readl_poll_timeout(divider->base + WZRD_DR_STATUS_REG_OFFSET, value, + value & WZRD_DR_LOCK_BIT_MASK, + WZRD_USEC_POLL, WZRD_TIMEOUT_POLL); + + return err; +} + +static long clk_wzrd_round_rate(struct clk_hw *hw, unsigned long rate, + unsigned long *prate) +{ + u8 div; + + /* + * since we don't change parent rate we just round rate to closest + * achievable + */ + div = DIV_ROUND_CLOSEST(*prate, rate); + + return (*prate / div); +} + +static const struct clk_ops clk_wzrd_clk_divider_ops = { + .round_rate = clk_wzrd_round_rate, + .set_rate = clk_wzrd_dynamic_reconfig, + .recalc_rate = clk_wzrd_recalc_rate, +}; + +static struct clk *clk_wzrd_register_divider(struct device *dev, + const char *name, + const char *parent_name, + unsigned long flags, + void __iomem *base, u16 offset, + u8 shift, u8 width, + u8 clk_divider_flags, + const struct clk_div_table *table, + spinlock_t *lock) +{ + struct clk_wzrd_divider *div; + struct clk_hw *hw; + struct clk_init_data init; + int ret; + + if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { + if (width + shift > 16) { + pr_warn("divider value exceeds LOWORD field\n"); + return ERR_PTR(-EINVAL); + } + } + + /* allocate the divider */ + div = kzalloc(sizeof(*div), GFP_KERNEL); + if (!div) + return ERR_PTR(-ENOMEM); + + init.name = name; + if (clk_divider_flags & CLK_DIVIDER_READ_ONLY) + init.ops = &clk_divider_ro_ops; + else + init.ops = &clk_wzrd_clk_divider_ops; + init.flags = flags; + init.parent_names = (parent_name ? &parent_name : NULL); + init.num_parents = (parent_name ? 1 : 0); + + /* struct clk_divider assignments */ + div->base = base; + div->offset = offset; + div->shift = shift; + div->width = width; + div->flags = clk_divider_flags; + div->lock = lock; + div->hw.init = &init; + div->table = table; + + /* register the clock */ + hw = &div->hw; + ret = clk_hw_register(dev, hw); + if (ret) { + kfree(div); + hw = ERR_PTR(ret); + } + + return hw->clk; +} + static int clk_wzrd_clk_notifier(struct notifier_block *nb, unsigned long event, void *data) { @@ -225,7 +394,8 @@ static int clk_wzrd_probe(struct platform_device *pdev) clk_wzrd->clks_internal[wzrd_clk_mul_div] = clk_register_fixed_factor (&pdev->dev, clk_name, __clk_get_name(clk_wzrd->clks_internal[wzrd_clk_mul]), - 0, 1, reg); + flags, ctrl_reg, 0, 8, CLK_DIVIDER_ONE_BASED | + CLK_DIVIDER_ALLOW_ZERO, &clkwzrd_lock); if (IS_ERR(clk_wzrd->clks_internal[wzrd_clk_mul_div])) { dev_err(&pdev->dev, "unable to register divider clock\n"); ret = PTR_ERR(clk_wzrd->clks_internal[wzrd_clk_mul_div]); @@ -243,11 +413,14 @@ static int clk_wzrd_probe(struct platform_device *pdev) ret = -EINVAL; goto err_rm_int_clks; } - reg = readl(clk_wzrd->base + WZRD_CLK_CFG_REG(2) + i * 12); - reg &= WZRD_CLKOUT_DIVIDE_MASK; - reg >>= WZRD_CLKOUT_DIVIDE_SHIFT; - clk_wzrd->clkout[i] = clk_register_fixed_factor - (&pdev->dev, clkout_name, clk_name, 0, 1, reg); + clk_wzrd->clkout[i] = clk_wzrd_register_divider(&pdev->dev, + clkout_name, + clk_name, 0, + clk_wzrd->base, (WZRD_CLK_CFG_REG(2) + i * 12), + WZRD_CLKOUT_DIVIDE_SHIFT, + WZRD_CLKOUT_DIVIDE_WIDTH, + CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO, + NULL, &clkwzrd_lock); if (IS_ERR(clk_wzrd->clkout[i])) { int j; -- 2.1.1