Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3554182imw; Mon, 11 Jul 2022 10:47:32 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tXFCmCdMJ1F+Bj6NZ5+818vakkB1oWVd7bqwuHnZoZqrjisizPTs3KCWVcW5cSzHEKBYJI X-Received: by 2002:a63:df15:0:b0:411:51f2:cc2a with SMTP id u21-20020a63df15000000b0041151f2cc2amr17019742pgg.533.1657561651943; Mon, 11 Jul 2022 10:47:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657561651; cv=none; d=google.com; s=arc-20160816; b=Rwv+83sFeVXiG7iknEn8D2P6WWlA+RThjAjJnjn+fYE68SojmHzt/2WqcnLnI6583E vAo6oDLgb7SI0X+9QMBsHk3wqezCy495pLtfjwGykq7GWzOm+McpUdeNrGsv30TYA49B r4QL1tvFQLkPMJ/NdkrHILjUcEM8Kjkt0XyjhemUsKQQVCmyx6QSoMfXJ/uKrVT6PyzH ITqXjlNI6EHX8aEQpCDt6ieuNPZG9Zc/bfO4QQ/FyzmwpiIV967L52X7RB0Bfzu7lOoM GQ5qfIp6bhEYmziRBgY1hl5QHnr0+mN3KK431d4gGfxgnXTXOElyB9sTz/HvfBCA4lPE JxGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=Kc4cDivDUaBoL2zBqpgtHP8w9cqA4NEFOrZFebj4Rs4=; b=yzD5w/Zf6vKCfgXJ36fT8gyELA+O0jEcykuL3RVSHOJa8Y/GwRX6I1lNs5CfVc4nTr 3+nVZaAPKgtnwpUtNH6axoFuJ5GIVBeS9Z7w3jYEXAP3Ywl8WfAC00UZe19C7gawOVC3 tZyPhm9KGTSJ8NbZnwnzkhA6QIvSFGsvkE/5FnJiztLPQGVB5xxiSyQK91Z0aetTORy5 cE5/zfLIkIHIHMjVYrORjYiqa6ByTxYTO4RZhkyZmgnix8lcKuuE9I/iXx0He2e5eZpd VbEBvZD4UIeuE9lcAioxkHWnT9FCeLB2lh7Oe82D6/0ddES0EDBTuBb5iqkb9JFrNZQu 91TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcdkim header.b=ryon+qW5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mz16-20020a17090b379000b001e858081883si11995305pjb.64.2022.07.11.10.47.16; Mon, 11 Jul 2022 10:47:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcdkim header.b=ryon+qW5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230329AbiGKReF (ORCPT + 99 others); Mon, 11 Jul 2022 13:34:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229654AbiGKRdz (ORCPT ); Mon, 11 Jul 2022 13:33:55 -0400 Received: from alexa-out.qualcomm.com (alexa-out.qualcomm.com [129.46.98.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDF0B64D5; Mon, 11 Jul 2022 10:33:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; i=@quicinc.com; q=dns/txt; s=qcdkim; t=1657560831; x=1689096831; h=from:to:cc:subject:date:message-id; bh=Kc4cDivDUaBoL2zBqpgtHP8w9cqA4NEFOrZFebj4Rs4=; b=ryon+qW5rYr+gJJg43+E9fpvI02dncxxxMDj5TrYt0G2UU6IcSzOK0rR wZmnVDqKlJu7/dg41+kdGw9hUVyVD/WCqtmTLXkmgp61+tLxcPbQB+eSJ 4gyCR0rff4/ruy609hgxkSYbEx0wZPtE48q0GYz7zHHZhhUSMnKArXYxF c=; Received: from ironmsg09-lv.qualcomm.com ([10.47.202.153]) by alexa-out.qualcomm.com with ESMTP; 11 Jul 2022 10:33:51 -0700 X-QCInternal: smtphost Received: from ironmsg02-blr.qualcomm.com ([10.86.208.131]) by ironmsg09-lv.qualcomm.com with ESMTP/TLS/AES256-SHA; 11 Jul 2022 10:33:50 -0700 X-QCInternal: smtphost Received: from hu-vnivarth-hyd.qualcomm.com (HELO hu-sgudaval-hyd.qualcomm.com) ([10.213.111.166]) by ironmsg02-blr.qualcomm.com with ESMTP; 11 Jul 2022 23:03:33 +0530 Received: by hu-sgudaval-hyd.qualcomm.com (Postfix, from userid 3994820) id D832B40EF; Mon, 11 Jul 2022 23:03:32 +0530 (+0530) From: Vijaya Krishna Nivarthi To: agross@kernel.org, bjorn.andersson@linaro.org, konrad.dybcio@somainline.org, gregkh@linuxfoundation.org, jirislaby@kernel.org, linux-arm-msm@vger.kernel.org, linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org Cc: quic_msavaliy@quicinc.com, dianders@chromium.org, mka@chromium.org, swboyd@chromium.org, Vijaya Krishna Nivarthi Subject: [V4] tty: serial: qcom-geni-serial: Fix get_clk_div_rate() which otherwise could return a sub-optimal clock rate. Date: Mon, 11 Jul 2022 23:03:28 +0530 Message-Id: <1657560808-9795-1-git-send-email-quic_vnivarth@quicinc.com> X-Mailer: git-send-email 2.7.4 X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham 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 In the logic around call to clk_round_rate(), for some corner conditions, get_clk_div_rate() could return an sub-optimal clock rate. Also, if an exact clock rate was not found lowest clock was being returned. Search for suitable clock rate in 2 steps a) exact match or within 2% tolerance b) within 5% tolerance This also takes care of corner conditions. Fixes: c2194bc999d4 ("tty: serial: qcom-geni-serial: Remove uart frequency table. Instead, find suitable frequency with call to clk_round_rate") Signed-off-by: Vijaya Krishna Nivarthi --- v4: replaced pr_dbg calls with dev_dbg v3: simplified algorithm further, fixed robot compile warnings v2: removed minor optimisations to make more readable v1: intial patch contained slightly complicated logic --- drivers/tty/serial/qcom_geni_serial.c | 89 +++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 35 deletions(-) diff --git a/drivers/tty/serial/qcom_geni_serial.c b/drivers/tty/serial/qcom_geni_serial.c index 2e23b65..f88b042 100644 --- a/drivers/tty/serial/qcom_geni_serial.c +++ b/drivers/tty/serial/qcom_geni_serial.c @@ -943,52 +943,71 @@ static int qcom_geni_serial_startup(struct uart_port *uport) return 0; } -static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud, - unsigned int sampling_rate, unsigned int *clk_div) +static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk, + unsigned int *clk_div, unsigned int percent_tol) { - unsigned long ser_clk; - unsigned long desired_clk; - unsigned long freq, prev; + unsigned long freq; unsigned long div, maxdiv; - int64_t mult; - - desired_clk = baud * sampling_rate; - if (!desired_clk) { - pr_err("%s: Invalid frequency\n", __func__); - return 0; - } + u64 mult; + unsigned long offset, abs_tol, achieved; + abs_tol = div_u64((u64)desired_clk * percent_tol, 100); maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT; - prev = 0; - - for (div = 1; div <= maxdiv; div++) { - mult = div * desired_clk; - if (mult > ULONG_MAX) + div = 1; + while (div <= maxdiv) { + mult = (u64)div * desired_clk; + if (mult != (unsigned long)mult) break; - freq = clk_round_rate(clk, (unsigned long)mult); - if (!(freq % desired_clk)) { - ser_clk = freq; - break; - } + offset = div * abs_tol; + freq = clk_round_rate(clk, mult - offset); - if (!prev) - ser_clk = freq; - else if (prev == freq) + /* Can only get lower if we're done */ + if (freq < mult - offset) break; - prev = freq; + /* + * Re-calculate div in case rounding skipped rates but we + * ended up at a good one, then check for a match. + */ + div = DIV_ROUND_CLOSEST(freq, desired_clk); + achieved = DIV_ROUND_CLOSEST(freq, div); + if (achieved <= desired_clk + abs_tol && + achieved >= desired_clk - abs_tol) { + *clk_div = div; + return freq; + } + + div = DIV_ROUND_UP(freq, desired_clk); } - if (!ser_clk) { - pr_err("%s: Can't find matching DFS entry for baud %d\n", - __func__, baud); - return ser_clk; + return 0; +} + +static unsigned long get_clk_div_rate(struct clk *clk, struct device *dev, + unsigned int baud, unsigned int sampling_rate, unsigned int *clk_div) +{ + unsigned long ser_clk; + unsigned long desired_clk; + + desired_clk = baud * sampling_rate; + if (!desired_clk) { + dev_dbg(dev, "Invalid frequency\n"); + return 0; } - *clk_div = ser_clk / desired_clk; - if (!(*clk_div)) - *clk_div = 1; + /* + * try to find a clock rate within 2% tolerance, then within + */ + ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2); + if (!ser_clk) + ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5); + + if (!ser_clk) + dev_err(dev, "Couldn't find suitable clock rate for %d\n", desired_clk); + else + dev_dbg(dev, "desired_clk-%d, ser_clk-%d, clk_div-%d\n", + desired_clk, ser_clk, *clk_div); return ser_clk; } @@ -1021,8 +1040,8 @@ static void qcom_geni_serial_set_termios(struct uart_port *uport, if (ver >= QUP_SE_VERSION_2_5) sampling_rate /= 2; - clk_rate = get_clk_div_rate(port->se.clk, baud, - sampling_rate, &clk_div); + clk_rate = get_clk_div_rate(port->se.clk, port->se.dev, baud, + sampling_rate, &clk_div); if (!clk_rate) goto out_restart_rx; -- Qualcomm INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by the Linux Foundation.