Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1551357ioo; Fri, 27 May 2022 10:55:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNk/q/cSzyF/CEiE/xJXgsIRAoMnq/X/k5JPnqkWK6YXM7IyxNI9/cx73p+Ga/iyfUnsGr X-Received: by 2002:a05:6402:2554:b0:42b:703c:513e with SMTP id l20-20020a056402255400b0042b703c513emr25687771edb.58.1653674105642; Fri, 27 May 2022 10:55:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653674105; cv=none; d=google.com; s=arc-20160816; b=fhfnStWpftJ7+zC7fYJN1wLcdJOFwARmhMcLUvhs6vcNu3rUD7illdgk880sBWrqRF /2ZOqExyZSfqAqd8bMSUwBCQetC34/5NgRYLWpFjlBqI4HJRbvuk+MLNrNhr+6/ONIiJ 6K2jrOErDpMF7a7/Mrt1P/tFVCTfTetULQPmFDyyt34hnHSZEaFJuhT7SVIM8ZwGpC19 NnG2viY19ROBGrt2UZq+dKldSqT4GaLpcc+n4E8wsEY3k33DgNgriKtF//7ZX9pWviWF 4EjooTgRYt07G9m0NXZMmVOaTZKfaUVt94KlAvtgb1owZo+3ncmPKC5a+sBZdRU8gNCi 628g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=5FSJoV8fyODoIeus7C+ZOqbM6V+rdyM5MW/EB0jGZhg=; b=sr040i687CgpIzPALUVqdyVaIjjZAtx5KIV21wsS6QM5v/Y9VEHgdPihtzPUDxYYT/ ylD1y0scXYh65zsljZZcu6HEog5Sq5wYIUmvrN80ztnR8+jCfRrsjQ3z+IRvoxdxg500 gaKC6u2VXilGzU/6DHzMyd3+kjfGTsC9Q9/T16Paw6wzlOpult/WHPrWZBVtrjzqpvcn /D+pJ4AJ6aeWM5GNmZUMssLrUE0qZLLoHX8uePEYxeHoDn8wcKyrg9WZAEi+VNodOO/d 6JJTyH2JcgTcV4xwVkmB7vTarGM/NivA+HLKT9jp/tja8KY1Zx0G26v/JnOKWyimYrpB wbvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=lZ4R+09B; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=m0AVBa17; 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=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ga39-20020a1709070c2700b006fef80f6e27si1692795ejc.950.2022.05.27.10.54.38; Fri, 27 May 2022 10:55:05 -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=@suse.de header.s=susede2_rsa header.b=lZ4R+09B; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=m0AVBa17; 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=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243451AbiE0Kfc (ORCPT + 99 others); Fri, 27 May 2022 06:35:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230355AbiE0Kf3 (ORCPT ); Fri, 27 May 2022 06:35:29 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACBA21078BA; Fri, 27 May 2022 03:35:28 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 5CD621F8B5; Fri, 27 May 2022 10:35:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1653647727; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5FSJoV8fyODoIeus7C+ZOqbM6V+rdyM5MW/EB0jGZhg=; b=lZ4R+09BI7oCUROykElkZtExU3MC2o5/GA4qjwqbfJRif0pw+SFgCoaFWOMplVJRTW1ZId /PKDZY87B/Xa6gqeHA1h6GjAvgcOdDndJcjO+sGvX+7eQs9KgrhIP83w6rWD37t+6+HSq4 18R21vZzmYyc2mMtDRp+sLU1pXdhqIc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1653647727; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5FSJoV8fyODoIeus7C+ZOqbM6V+rdyM5MW/EB0jGZhg=; b=m0AVBa177TUk1M+dEEB3xnUQW0JFaICGQzEjpFN0I3LCXaYi39kc4XS3Q3Z2CnrD3h9xeR rkdV/zmWQ0P7J3AA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 3448A13A84; Fri, 27 May 2022 10:35:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id g0zaC2+pkGKOcAAAMHmgww (envelope-from ); Fri, 27 May 2022 10:35:27 +0000 From: "Ivan T. Ivanov" To: Michael Turquette , Stephen Boyd , Florian Fainelli , Ray Jui , Scott Branden , Broadcom internal kernel review list , Nicolas Saenz Julienne , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: "Ivan T. Ivanov" , Phil Elwell , kernel test robot , linux-clk@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [PATCH v3] clk: bcm2835: Round UART input clock up Date: Fri, 27 May 2022 13:29:00 +0300 Message-Id: <20220527102900.144894-1-iivanov@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,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 It was reported that RPi3[1] and RPi Zero 2W boards have issues with the Bluetooth. It turns out that when switching from initial to operation speed host and device no longer can talk each other because host uses incorrect UART baud rate. The UART driver used in this case is amba-pl011. Original fix, see below Github link[2], was inside pl011 module, but somehow it didn't look as the right place to fix. Beside that this original rounding function is not exactly perfect for all possible clock values. So I deiced to move the hack to the platform which actually need it. The UART clock is initialised to be as close to the requested frequency as possible without exceeding it. Now that there is a clock manager that returns the actual frequencies, an expected 48MHz clock is reported as 47999625. If the requested baud rate == requested clock/16, there is no headroom and the slight reduction in actual clock rate results in failure. If increasing a clock by less than 0.1% changes it from ..999.. to ..000.., round it up. [1] https://bugzilla.suse.com/show_bug.cgi?id=1188238 [2] https://github.com/raspberrypi/linux/commit/ab3f1b39537f6d3825b8873006fbe2fc5ff057b7 Cc: Phil Elwell Signed-off-by: Ivan T. Ivanov --- Changes since v2 * Added more information in commit message * Changed hand crafted round function with the one form math.h Changes since v1 Make bcm2835_clock_round() static to fix following warning when compiling for riscv: drivers/clk/bcm/clk-bcm2835.c:997:15: warning: no previous prototype for 'bcm2835_clock_round' [-Wmissing-prototypes] Reported-by: kernel test robot drivers/clk/bcm/clk-bcm2835.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/drivers/clk/bcm/clk-bcm2835.c b/drivers/clk/bcm/clk-bcm2835.c index 48a1eb9f2d55..cee59990a57b 100644 --- a/drivers/clk/bcm/clk-bcm2835.c +++ b/drivers/clk/bcm/clk-bcm2835.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -502,6 +503,8 @@ struct bcm2835_clock_data { bool low_jitter; u32 tcnt_mux; + + bool round_up; }; struct bcm2835_gate_data { @@ -993,12 +996,31 @@ static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock, return temp; } +static unsigned long bcm2835_clock_round(unsigned long clk) +{ + unsigned long scaler; + + scaler = 1; + while (scaler * 100000 < clk) + scaler *= 10; + + /* + * If increasing a clock by less than 0.1% changes it + * from ..999.. to ..000.., round up. + */ + if ((clk + scaler - 1) / scaler % 1000 == 0) + clk = roundup(clk, scaler); + + return clk; +} + static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw, unsigned long parent_rate) { struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); struct bcm2835_cprman *cprman = clock->cprman; const struct bcm2835_clock_data *data = clock->data; + unsigned long rate; u32 div; if (data->int_bits == 0 && data->frac_bits == 0) @@ -1006,7 +1028,12 @@ static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw, div = cprman_read(cprman, data->div_reg); - return bcm2835_clock_rate_from_divisor(clock, parent_rate, div); + rate = bcm2835_clock_rate_from_divisor(clock, parent_rate, div); + + if (data->round_up) + rate = bcm2835_clock_round(rate); + + return rate; } static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock) @@ -2143,7 +2170,8 @@ static const struct bcm2835_clk_desc clk_desc_array[] = { .div_reg = CM_UARTDIV, .int_bits = 10, .frac_bits = 12, - .tcnt_mux = 28), + .tcnt_mux = 28, + .round_up = true), /* TV encoder clock. Only operating frequency is 108Mhz. */ [BCM2835_CLOCK_VEC] = REGISTER_PER_CLK( -- 2.35.3