From patchwork Thu Jan 17 11:03:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1026535 X-Patchwork-Delegate: uboot@andestech.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=brainfault.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=brainfault-org.20150623.gappssmtp.com header.i=@brainfault-org.20150623.gappssmtp.com header.b="QBRHzhpr"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 43gLtV09sYz9s3l for ; Thu, 17 Jan 2019 22:09:05 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 9480EC22047; Thu, 17 Jan 2019 11:05:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=KHOP_BIG_TO_CC, RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id DD691C220AF; Thu, 17 Jan 2019 11:05:07 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 21F2EC220E6; Thu, 17 Jan 2019 11:04:57 +0000 (UTC) Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by lists.denx.de (Postfix) with ESMTPS id 39360C220E1 for ; Thu, 17 Jan 2019 11:04:53 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id t13so4288694pgr.11 for ; Thu, 17 Jan 2019 03:04:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PeTZXaUKsZN6GcBHpSJaDgthKSns/aTg4CgwHCeNdZY=; b=QBRHzhprc6zZTHShtTrnR6Q8h2lZAjgS1R+5bSPkLcLdDDgKFW0UTAl7uEZawehuE5 7LArxsIL64Let7xc627eXYW+W6aXjCstMZHVJn1X0T0XzcsaYscqH8uTtPEZJcZys1cK /SFikQrlAlb5Yt0+bPvYUovYCYtPNSxjMy2OxuWd2Uk3xF5QTcsW9c0XU0Sq+f5YDoQh cwVkNTwp0VF9tcG64okPcPLJDUGTvsweFv6z48mS+j/5+9A9cJ4qwnqlBgQ5PDKsrlQW Gyy3IjYF4Dii/KhqgefWe5Ki1rWSt7VnXNtc2ClAhkev+ZL/75+pEwrEY+JNRmudfOYe vJdA== 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=PeTZXaUKsZN6GcBHpSJaDgthKSns/aTg4CgwHCeNdZY=; b=owZh9YhC+IwFD/iwSVQmQ0D+RdeGLWq1/avnm/A6eF8Fmub1GiOK/aKH//NhScZOjW wQHbBrbWSVp6EJ2qdnqT6zXVf5vDqg6zGtkVcXUG73wGhgGKhyUu0NBsh3UgiQqUMgfk 10wboFmk927IEIsIvGU/k63yWGokKUcDIJyTDkzmhGpdBOhcPfLhfCFnNNm0Z2WaTON3 232b9UnMIGem8+r+4M/XqQVoZ2ku3qH8dwEm1hKR2YagoFTQYjsw0lgkIG1VeWj5yZX+ CedcnU+r4LgOEqO7FVQEsR+tdL9sBZoMKzrFBq34wOzI9k3MZFWk5EnF3ltP7hi4xEr7 V3tw== X-Gm-Message-State: AJcUukeT8RWaQV8HIncVzZftF4pWXhMED+nQTzbajU0qvTQvpRQFT83Y xSOCgE7R2v7bDYdfEEVGt7uw/g== X-Google-Smtp-Source: ALg8bN46wUqX4OdjqDgI5vWqEPEratDVQfsuZfYfKQjow4W9vErKDdZjXu/ZvdhcKrmnKqJKiW9xww== X-Received: by 2002:aa7:8354:: with SMTP id z20mr14329525pfm.81.1547723091644; Thu, 17 Jan 2019 03:04:51 -0800 (PST) Received: from localhost.localdomain ([106.51.16.164]) by smtp.gmail.com with ESMTPSA id h15sm1688142pgl.43.2019.01.17.03.04.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Jan 2019 03:04:50 -0800 (PST) From: Anup Patel To: Rick Chen , Bin Meng , Joe Hershberger , Lukas Auer , Masahiro Yamada , Simon Glass Date: Thu, 17 Jan 2019 16:33:53 +0530 Message-Id: <20190117110356.36753-9-anup@brainfault.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190117110356.36753-1-anup@brainfault.org> References: <20190117110356.36753-1-anup@brainfault.org> Cc: Palmer Dabbelt , U-Boot Mailing List , Alexander Graf , Christoph Hellwig , Paul Walmsley Subject: [U-Boot] [PATCH 08/11] drivers: serial_sifive: Fix baud rate calculation X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Atish Patra Compute the baud rate multipler with more precision. Signed-off-by: Atish Patra --- drivers/serial/serial_sifive.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/drivers/serial/serial_sifive.c b/drivers/serial/serial_sifive.c index 341728a690..ea4d35d48c 100644 --- a/drivers/serial/serial_sifive.c +++ b/drivers/serial/serial_sifive.c @@ -33,16 +33,40 @@ struct uart_sifive { }; struct sifive_uart_platdata { - unsigned int clock; + unsigned long clock; int saved_input_char; struct uart_sifive *regs; }; +/** + * Find minimum divisor divides in_freq to max_target_hz; + * Based on uart driver n SiFive FSBL. + * + * f_baud = f_in / (div + 1) => div = (f_in / f_baud) - 1 + * The nearest integer solution requires rounding up as to not exceed + * max_target_hz. + * div = ceil(f_in / f_baud) - 1 + * = floor((f_in - 1 + f_baud) / f_baud) - 1 + * This should not overflow as long as (f_in - 1 + f_baud) does not exceed + * 2^32 - 1, which is unlikely since we represent frequencies in kHz. + */ +static inline unsigned int uart_min_clk_divisor(unsigned long in_freq, + unsigned long max_target_hz) +{ + unsigned long quotient = + (in_freq + max_target_hz - 1) / (max_target_hz); + /* Avoid underflow */ + if (quotient == 0) + return 0; + else + return quotient - 1; +} + /* Set up the baud rate in gd struct */ static void _sifive_serial_setbrg(struct uart_sifive *regs, unsigned long clock, unsigned long baud) { - writel((u32)((clock / baud) - 1), ®s->div); + writel((uart_min_clk_divisor(clock, baud)), ®s->div); } static void _sifive_serial_init(struct uart_sifive *regs)