From patchwork Fri Apr 7 15:34:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Brunet X-Patchwork-Id: 9669685 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A96F2602A0 for ; Fri, 7 Apr 2017 15:35:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9A4B1285E8 for ; Fri, 7 Apr 2017 15:35:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8F35128609; Fri, 7 Apr 2017 15:35:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 47FC8285E8 for ; Fri, 7 Apr 2017 15:35:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=qZ6BTXI0SAsL4T8pkDfbdxD6cfd6E78I7Sh+wr/yPho=; b=m58pXX6yHHpW7VI5NRNJYg+bEm ZEGnW6XkFPmuw6zW7M+yD2onteEaO77QqLRUQtnaFJklRxJkGW4iDi1oPhHIgb3/GLRu/tdCBDGvh jWEU1COnCJCajrhEvq05UQoGnQlpdCQLmZ/82HEhAeXG4R/8CaGeH6KnB9r1/xzQSlBo+KgMip1eQ J7Zo42OE0NMqb1zKdTyVccPgZ2Rj1QE7WGRbxH9XhaU8qQeNek+WzT7HlPek4Ur/povHpcF/JqzOu 2ny/MBXItjqeZ1ZGzwJp71L7qLl7fzuPoBJ7P79oP75gmILatPIbJIukc4ZKpSwG1/Wd66zVQrNNW r0CBEdlA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cwVvM-0002WC-Cg; Fri, 07 Apr 2017 15:35:44 +0000 Received: from mail-wm0-x233.google.com ([2a00:1450:400c:c09::233]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cwVud-0000RH-TD for linux-amlogic@lists.infradead.org; Fri, 07 Apr 2017 15:35:04 +0000 Received: by mail-wm0-x233.google.com with SMTP id u2so1521259wmu.0 for ; Fri, 07 Apr 2017 08:34:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Ht6GMuQQmSPG1X5Hx+W5AMidZ0tYpSME4SpUgWPbP/Y=; b=EpmxIqI/a2Dy8TbsaZdWAdiebJqLkO5bh2jDfsZeLHkCiDm/1kCiRV/5dLy0aVapq5 Y2niOH7D2ZzoH4uD8nzolljdc5z5G+IwlzCHFA51xrNFY3VOApakDGaftzKI1vWXv+f1 2SEnqs/e4rPJ2IgZAX3hocxcEG6HVRCsOw6BPlA94ZFCjFV7uPoxdRsdab4Ln4qHvjIg xdwJghfEZfiZ6EWH69rvxNczyXcn/fJrPANQX0MYL4cyUTr5bqp7aw+DX3UCRUa860IY XRskKpCDA5sIzGUs7xxheOFWaedwFwr30ak6CM6uxN6lReXwkfyhU9UGOITCLJxL2hEo qXnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Ht6GMuQQmSPG1X5Hx+W5AMidZ0tYpSME4SpUgWPbP/Y=; b=h+hNswQCrI+4pizcq/XAnQYsgcThj05p8Iu7bQ07hJynK+L6CKLwb3HFlRvmLyCrxJ 9AFb7PhTqiAyp+80mRCVqxd24lA/tGJn1ajGuk5BmTOw2Op2imfiZqzqYTWJimqsoVUJ kwUD/VkIsRuy2yecOCfqID2Gdjpv7qHUb5neXN5CHI9D1w8FRwJAv1Es4lnKIfl3kPvu obuXE5fM6yMSa4amFEGOmPkPwlQWSGJ9OD9YClq2uPUgg6gyWj04zMGlRBuz1QNilwbB hxTNT4rMPhw4D7dIETuv+mAk+daC+cRh1bN3lB+t6G2p1BvVJciWZNjsr9RwYZENzDWO tQAA== X-Gm-Message-State: AFeK/H2VSnTf2LupwIv2yPZNTdMzrAX9fjn7Gg0RaAu3Y+N1DZEvHJQHg5k2a5gU3wm2kTGt X-Received: by 10.28.146.12 with SMTP id u12mr30044151wmd.142.1491579278051; Fri, 07 Apr 2017 08:34:38 -0700 (PDT) Received: from localhost.localdomain ([90.63.244.31]) by smtp.googlemail.com with ESMTPSA id 82sm3655870wmg.0.2017.04.07.08.34.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Apr 2017 08:34:37 -0700 (PDT) From: Jerome Brunet To: Martin Blumenstingl , Neil Armstrong , Kevin Hilman , Michael Turquette , Stephen Boyd Subject: [PATCH v2 1/2] clk: meson: mpll: fix division by zero in rate_from_params Date: Fri, 7 Apr 2017 17:34:32 +0200 Message-Id: <20170407153433.18640-2-jbrunet@baylibre.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170407153433.18640-1-jbrunet@baylibre.com> References: <20170407153433.18640-1-jbrunet@baylibre.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170407_083500_096410_ED665CBD X-CRM114-Status: GOOD ( 14.93 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: carlo@caione.org, linux-amlogic@lists.infradead.org, linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Jerome Brunet MIME-Version: 1.0 Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Martin Blumenstingl According to the public datasheet all register bits in HHI_MPLL_CNTL7, HHI_MPLL_CNTL8 and HHI_MPLL_CNTL9 default to zero. On all GX SoCs these seem to be initialized by the bootloader to some default value. However, on my Meson8 board they are not initialized, leading to a division by zero in rate_from_params as the math is: (parent_rate * SDM_DEN) / ((SDM_DEN * 0) + 0) According to the datasheet, the minimum n2 value is 4. The rate provided by the clock when n2 is less than this minimum is unpredictable. In such case, we report an error. Although the rate_from_params function was only introduced recently the original bug has been there for much longer. It was only exposed recently when the MPLL clocks were added to the Meson8b clock driver. Fixes: 1c50da4f27 ("clk: meson: add mpll support") Signed-off-by: Martin Blumenstingl Signed-off-by: Jerome Brunet Reviewed-by: Neil Armstrong --- drivers/clk/meson/clk-mpll.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/drivers/clk/meson/clk-mpll.c b/drivers/clk/meson/clk-mpll.c index 540dabe5adad..d9462b505dcc 100644 --- a/drivers/clk/meson/clk-mpll.c +++ b/drivers/clk/meson/clk-mpll.c @@ -65,18 +65,21 @@ #include "clkc.h" #define SDM_DEN 16384 -#define SDM_MIN 1 -#define SDM_MAX 16383 #define N2_MIN 4 #define N2_MAX 511 #define to_meson_clk_mpll(_hw) container_of(_hw, struct meson_clk_mpll, hw) -static unsigned long rate_from_params(unsigned long parent_rate, +static long rate_from_params(unsigned long parent_rate, unsigned long sdm, unsigned long n2) { - return (parent_rate * SDM_DEN) / ((SDM_DEN * n2) + sdm); + unsigned long divisor = (SDM_DEN * n2) + sdm; + + if (n2 < N2_MIN) + return -EINVAL; + + return (parent_rate * SDM_DEN) / divisor; } static void params_from_rate(unsigned long requested_rate, @@ -89,17 +92,13 @@ static void params_from_rate(unsigned long requested_rate, if (div < N2_MIN) { *n2 = N2_MIN; - *sdm = SDM_MIN; + *sdm = 0; } else if (div > N2_MAX) { *n2 = N2_MAX; - *sdm = SDM_MAX; + *sdm = SDM_DEN - 1; } else { *n2 = div; *sdm = DIV_ROUND_UP(rem * SDM_DEN, requested_rate); - if (*sdm < SDM_MIN) - *sdm = SDM_MIN; - else if (*sdm > SDM_MAX) - *sdm = SDM_MAX; } } @@ -109,6 +108,7 @@ static unsigned long mpll_recalc_rate(struct clk_hw *hw, struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw); struct parm *p; unsigned long reg, sdm, n2; + long rate; p = &mpll->sdm; reg = readl(mpll->base + p->reg_off); @@ -118,7 +118,11 @@ static unsigned long mpll_recalc_rate(struct clk_hw *hw, reg = readl(mpll->base + p->reg_off); n2 = PARM_GET(p->width, p->shift, reg); - return rate_from_params(parent_rate, sdm, n2); + rate = rate_from_params(parent_rate, sdm, n2); + if (rate < 0) + return 0; + + return rate; } static long mpll_round_rate(struct clk_hw *hw,