Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp694160ybx; Thu, 7 Nov 2019 01:08:17 -0800 (PST) X-Google-Smtp-Source: APXvYqyBU2cyIDpoeco4MHBMyoDoLe3xHdTbNQr5LOoZrfYsSame12GuKl4jjvnBQG45WNOOGpWz X-Received: by 2002:a17:906:2615:: with SMTP id h21mr1970847ejc.212.1573117697583; Thu, 07 Nov 2019 01:08:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1573117697; cv=pass; d=google.com; s=arc-20160816; b=uXrInI7G9QQe+w5dPtNn/qX1/xCOauT5OFgLKyNvks9lEaPrOazE/DiWokQ80F9ECj T9sdqzr3JzwNiL24ZXP8aNIfN4INvTEXMMPa9VgAdIBA0HDCBITBQppQftjpaSwQifj4 IRNohHaaI3o/BRRYA21qOFVP5+NiSBuxGXDkheA4PA/jX/SainK4uFGsk5W9Y0TNJxD3 zBlSRd4bHHDMBIxVBzMNDpT5hJjT+D7vLN7Lv5oUfssL4TfHvXUQTfY+k167eTP3YvOk 3odc4ARh7F6Zht0go0RlbiQBnx3odXUDvWOo5HXWslFxuL1VSNcrYolMf62jjNL1CGdq Zxog== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=vsoMbPhZthgvzW3XntHdKGNDxOqGfz5Pmkpyen+X4nI=; b=ij3W7aAO2a5c2xFCUYOjRvF1H8ZRj7sWPHjK3kYMIdRf18ECXjmcbco77Weh8doml3 MY7kmSuqNeAA8Kx5FhDsTwqM3zeEoT9Ov3iuttkK9ZX/R3tRrssCvYVDjQl9BSZCph/S vSx8YQ8YuV7QzqKQ6Zt6mYUMj2071/i04Ecn5nERJHsaPjmTLCWawIy0Moerr66N+Wkf OSBWNHsp0OgvFyGsM+zQP8eyhs0kV55X3lI7TymZpED7uD/V3I5MLqMUDOqvTVhTVkTP BZwGXJ6SPI+Wvj2WHdELrnJ0S/ey6AvtiA+o8QOBk8GFbNekPY3dxSek7HB11jrWrWmy 0IeQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@xilinx.onmicrosoft.com header.s=selector2-xilinx-onmicrosoft-com header.b=jDvYt0DS; arc=pass (i=1 spf=pass spfdomain=xilinx.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 g53si945087edb.313.2019.11.07.01.07.53; Thu, 07 Nov 2019 01:08:17 -0800 (PST) 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; dkim=pass header.i=@xilinx.onmicrosoft.com header.s=selector2-xilinx-onmicrosoft-com header.b=jDvYt0DS; arc=pass (i=1 spf=pass spfdomain=xilinx.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 S1733249AbfKGJHI (ORCPT + 99 others); Thu, 7 Nov 2019 04:07:08 -0500 Received: from mail-eopbgr740050.outbound.protection.outlook.com ([40.107.74.50]:22560 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726800AbfKGJHH (ORCPT ); Thu, 7 Nov 2019 04:07:07 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U1bgPd9O1pIUYq6FKn4N02mAiKIah54kJN6D8Cu/hNLnXNX/fLyqwYiv6jJqFRKEgjiteDFYfzOyLHg4j7Sy2i1l0LIwInf91SKOStAKNzqE2t+3h59Eav5xp1AL2PvxEYldvsA6nZRGuNZCXSW62LcbtiLpAVO7zoPd+pAF92hwKUP4fRJUF6BNEFbEas4xUDKUoYFvTj/r8dItHsRNvPeeG0J/hRJlbUL9fP38ndrAoOA+EIxs2gjsaiqYonc3XapEMPwtCvoLuMa0EsntmVPTi/IRCaSFkIqviu2TjTvxOYat4/4PctoofbG+o5hZygN6k/iizOd++DqGd8Ac9g== 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=vsoMbPhZthgvzW3XntHdKGNDxOqGfz5Pmkpyen+X4nI=; b=c0IEsdrvzuqg4QDxerD8YDb4ZhIqkaeHiyNbS4KkqJbViLAeY6ySWrMQOZeU7t8Lw4/gIcHkdR5LWIxFcZPT3WLcwLAb4/+YeFQRs8aU5ylw1t8miJrEkKnP1PF3WMeYr9+LTzNNhaasl04ogEj06SMcQVygPsdxLL/tnJlzoDv78JiQbWMy8TZwA0spTjs8NG+yFEtj1YXO1fihCY5SJo8m2z41XkXbhufFQeKX91u26ps8mrDwD1SaZUELNA8W305SwGVi/qX62U0DEsp5kHd7jOusiOudW5Ct0WNYME2WEV/fMFOXoJ9HrIFetp4pK4s4ccv65V3PM57Q68MCgA== 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=vsoMbPhZthgvzW3XntHdKGNDxOqGfz5Pmkpyen+X4nI=; b=jDvYt0DSyclckn5O7nNkBKzYCSclsMwJHFHnrcTf0gUJbM43G2XMsWVR0xikce5Zdh+o4Kk6TM0bhK92rDpZE28J0m7HkhF6yG4Q7aykggQk82BNLsPpAyoKymR9cJA5/+U+NLMWsWrlaZ/WeVzENw4RQQcWP1jwQc6q5qs4xZ0= Received: from CY4PR02CA0006.namprd02.prod.outlook.com (2603:10b6:903:18::16) by SN6PR02MB5133.namprd02.prod.outlook.com (2603:10b6:805:68::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2430.20; Thu, 7 Nov 2019 09:07:01 +0000 Received: from SN1NAM02FT017.eop-nam02.prod.protection.outlook.com (2a01:111:f400:7e44::209) by CY4PR02CA0006.outlook.office365.com (2603:10b6:903:18::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2430.20 via Frontend Transport; Thu, 7 Nov 2019 09:07:01 +0000 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 SN1NAM02FT017.mail.protection.outlook.com (10.152.72.115) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.2430.20 via Frontend Transport; Thu, 7 Nov 2019 09:07:00 +0000 Received: from unknown-38-66.xilinx.com ([149.199.38.66] helo=xsj-pvapsmtp01) by xsj-pvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1iSdkq-0001bY-Ce; Thu, 07 Nov 2019 01:07:00 -0800 Received: from [127.0.0.1] (helo=localhost) by xsj-pvapsmtp01 with smtp (Exim 4.63) (envelope-from ) id 1iSdkl-0002Ij-9j; Thu, 07 Nov 2019 01:06:55 -0800 Received: from [172.19.2.91] (helo=xsjjollys50.xilinx.com) by xsj-pvapsmtp01 with esmtp (Exim 4.63) (envelope-from ) id 1iSdkf-0002IK-3v; Thu, 07 Nov 2019 01:06:49 -0800 From: Rajan Vaja To: mturquette@baylibre.com, sboyd@kernel.org, michal.simek@xilinx.com, m.tretter@pengutronix.de, jollys@xilinx.com, nava.manne@xilinx.com, tejas.patel@xilinx.com Cc: linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Rajan Vaja Subject: [PATCH] clk: zynqmp: Fix divider calculation Date: Thu, 7 Nov 2019 01:06:14 -0800 Message-Id: <1573117574-9316-1-git-send-email-rajan.vaja@xilinx.com> X-Mailer: git-send-email 2.7.4 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-Forefront-Antispam-Report: CIP:149.199.60.83;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(4636009)(136003)(396003)(346002)(39850400004)(376002)(199004)(189003)(70206006)(50466002)(70586007)(48376002)(305945005)(8676002)(2906002)(26005)(426003)(50226002)(336012)(106002)(316002)(51416003)(81156014)(81166006)(7696005)(9786002)(356004)(6666004)(44832011)(6636002)(186003)(8936002)(36756003)(2616005)(14444005)(486006)(16586007)(478600001)(107886003)(36386004)(126002)(47776003)(4326008)(5660300002)(476003);DIR:OUT;SFP:1101;SCL:1;SRVR:SN6PR02MB5133;H:xsj-pvapsmtpgw01;FPR:;SPF:Pass;LANG:en;PTR:unknown-60-83.xilinx.com;A:1;MX:1; MIME-Version: 1.0 Content-Type: text/plain X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4457ebd4-273d-4c05-e0b7-08d76361d990 X-MS-TrafficTypeDiagnostic: SN6PR02MB5133: X-Microsoft-Antispam-PRVS: X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-MS-Oob-TLC-OOBClassifiers: OLM:1332; X-Forefront-PRVS: 0214EB3F68 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kl3Qq7w/FV7Y8qnqn096w7vVPN0FzcXUe+OnbE/Wn3ae7SpoeX6gBZ0KA3Yek6Vo7fYoly54PM6ng4gaOy+qcX7f8MEk1/U24p69unSnKQwPdZfKpfb5U4XhpGDVTzuzmZXq9bn8Pk3PYnhPpaQEOw2qKV9+d9arTJ8wjlXIlJGDvLaatccVTv41C0VWRfFWc09aEBdofWqNY8arpEYoegiLb24/f+CUGdFnDLJlkplQ6NNPgCnY0B0wmbWP6haqxOq3fAOppT6zE6uPd9H2eHFOTmcEaGLVoQvz8Srs1RiA0IEAYowgcQZuUq05GNOMhzSFKIX3veL3B47s/Hd8KbvfX1JsmCgshlhCGivyzR8FUgc6DV9FWkLjrut35HfTIbp4+rJNVIwAemK3yNfTmByg5nBUw18bdFQ0FocnUJl0RU81OM+3Qyjz5XdOXu00 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Nov 2019 09:07:00.7861 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4457ebd4-273d-4c05-e0b7-08d76361d990 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-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR02MB5133 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Linux doesn't know maximum value of divisor that it can support. zynqmp_clk_divider_round_rate() returns actual divider value after calculating from parent rate and desired rate, even though that rate is not supported by single divider of hardware. It is also possible that such divisor value can be achieved through 2 different dividers. As, Linux tries to set such divisor value(out of range) in single divider set divider is getting failed. Fix the same by computing best possible combination of two divisors which provides more accurate clock rate. Signed-off-by: Rajan Vaja Signed-off-by: Michal Simek Signed-off-by: Tejas Patel --- drivers/clk/zynqmp/divider.c | 62 +++++++++++++++++++++++++++++++++++- include/linux/firmware/xlnx-zynqmp.h | 3 +- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/drivers/clk/zynqmp/divider.c b/drivers/clk/zynqmp/divider.c index d8f5b70d..d2be24e 100644 --- a/drivers/clk/zynqmp/divider.c +++ b/drivers/clk/zynqmp/divider.c @@ -2,7 +2,7 @@ /* * Zynq UltraScale+ MPSoC Divider support * - * Copyright (C) 2016-2018 Xilinx + * Copyright (C) 2016-2019 Xilinx * * Adjustable divider clock implementation */ @@ -41,6 +41,7 @@ struct zynqmp_clk_divider { bool is_frac; u32 clk_id; u32 div_type; + u32 max_div; }; static inline int zynqmp_divider_get_val(unsigned long parent_rate, @@ -88,6 +89,34 @@ static unsigned long zynqmp_clk_divider_recalc_rate(struct clk_hw *hw, return DIV_ROUND_UP_ULL(parent_rate, value); } +static void zynqmp_compute_divider(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate, + u32 max_div, + int *bestdiv) +{ + int div1; + int div2; + long error = LONG_MAX; + struct clk_hw *parent_hw = clk_hw_get_parent(hw); + struct zynqmp_clk_divider *pdivider = to_zynqmp_clk_divider(parent_hw); + + if (!pdivider) + return; + + *bestdiv = 1; + for (div1 = 1; div1 <= pdivider->max_div; div1++) { + for (div2 = 1; div2 <= max_div; div2++) { + long new_error = ((parent_rate / div1) / div2) - rate; + + if (abs(new_error) < abs(error)) { + *bestdiv = div2; + error = new_error; + } + } + } +} + /** * zynqmp_clk_divider_round_rate() - Round rate of divider clock * @hw: handle between common and hardware-specific interfaces @@ -125,8 +154,21 @@ static long zynqmp_clk_divider_round_rate(struct clk_hw *hw, bestdiv = zynqmp_divider_get_val(*prate, rate); + /* + * In case of two divisors, compute best divider values and return + * divider2 value based on compute value. div1 will be automatically + * set to optimum based on required total divider value. + */ + if (div_type == TYPE_DIV2 && + (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { + zynqmp_compute_divider(hw, rate, *prate, + divider->max_div, &bestdiv); + } + if ((clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) && divider->is_frac) bestdiv = rate % *prate ? 1 : bestdiv; + + bestdiv = min_t(u32, bestdiv, divider->max_div); *prate = rate * bestdiv; return rate; @@ -195,6 +237,9 @@ struct clk_hw *zynqmp_clk_register_divider(const char *name, struct clk_hw *hw; struct clk_init_data init; int ret; + const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops(); + struct zynqmp_pm_query_data qdata = {0}; + u32 ret_payload[PAYLOAD_ARG_CNT]; /* allocate the divider */ div = kzalloc(sizeof(*div), GFP_KERNEL); @@ -215,6 +260,21 @@ struct clk_hw *zynqmp_clk_register_divider(const char *name, div->clk_id = clk_id; div->div_type = nodes->type; + /* + * To achieve best possible rate, maximum limit of divider is required + * while computation. Get maximum supported divisor from firmware. To + * maintain backward compatibility assign maximum possible value(0xFFFF) + * if query for max divisor is not successful. + */ + qdata.qid = PM_QID_CLOCK_GET_MAX_DIVISOR; + qdata.arg1 = clk_id; + qdata.arg2 = nodes->type; + ret = eemi_ops->query_data(qdata, ret_payload); + if (ret) + div->max_div = 0XFFFF; + else + div->max_div = ret_payload[1]; + hw = &div->hw; ret = clk_hw_register(NULL, hw); if (ret) { diff --git a/include/linux/firmware/xlnx-zynqmp.h b/include/linux/firmware/xlnx-zynqmp.h index 778abbb..1edb6e9 100644 --- a/include/linux/firmware/xlnx-zynqmp.h +++ b/include/linux/firmware/xlnx-zynqmp.h @@ -2,7 +2,7 @@ /* * Xilinx Zynq MPSoC Firmware layer * - * Copyright (C) 2014-2018 Xilinx + * Copyright (C) 2014-2019 Xilinx * * Michal Simek * Davorin Mista @@ -105,6 +105,7 @@ enum pm_query_id { PM_QID_CLOCK_GET_PARENTS, PM_QID_CLOCK_GET_ATTRIBUTES, PM_QID_CLOCK_GET_NUM_CLOCKS = 12, + PM_QID_CLOCK_GET_MAX_DIVISOR, }; enum zynqmp_pm_reset_action { -- 2.7.4