From patchwork Mon Dec 14 18:57:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geert Uytterhoeven X-Patchwork-Id: 7846771 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5F7C89F32E for ; Mon, 14 Dec 2015 18:57:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 65BCC202E9 for ; Mon, 14 Dec 2015 18:57:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5E32B20361 for ; Mon, 14 Dec 2015 18:57:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932137AbbLNS5p (ORCPT ); Mon, 14 Dec 2015 13:57:45 -0500 Received: from albert.telenet-ops.be ([195.130.137.90]:55508 "EHLO albert.telenet-ops.be" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753343AbbLNS5o (ORCPT ); Mon, 14 Dec 2015 13:57:44 -0500 Received: from ayla.of.borg ([84.195.106.123]) by albert.telenet-ops.be with bizsmtp id tWxi1r00X2fm56U06Wxipl; Mon, 14 Dec 2015 19:57:43 +0100 Received: from ramsan.of.borg ([192.168.97.29] helo=ramsan) by ayla.of.borg with esmtp (Exim 4.82) (envelope-from ) id 1a8YJa-0002ts-8c; Mon, 14 Dec 2015 19:57:42 +0100 Received: from geert by ramsan with local (Exim 4.82) (envelope-from ) id 1a8YJd-0000Hn-Mh; Mon, 14 Dec 2015 19:57:45 +0100 From: Geert Uytterhoeven To: Greg Kroah-Hartman , Simon Horman , Magnus Damm , Yoshinori Sato Cc: linux-serial@vger.kernel.org, linux-sh@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH v3 15/27] serial: sh-sci: Merge sci_scbrr_calc() and sci_baud_calc_hscif() Date: Mon, 14 Dec 2015 19:57:24 +0100 Message-Id: <1450119456-964-16-git-send-email-geert+renesas@glider.be> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1450119456-964-1-git-send-email-geert+renesas@glider.be> References: <1450119456-964-1-git-send-email-geert+renesas@glider.be> Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP For low bit rates, the for-loop that reduces the divider returned by sci_scbrr_calc() and picks the clock select value may terminate without finding suitable values, leading to out-of-range divider and clock select values. sci_baud_calc_hscif() doesn't suffer from this problem, as it correctly uses clamp(). Since there are only two relevant differences between HSCIF and other variants w.r.t. bit rate configuration (fixed vs. variable sample rate, and an additional factor of two), sci_scbrr_calc() and sci_baud_calc_hscif() can be merged, fixing the issue with out-of-range values. Signed-off-by: Geert Uytterhoeven Acked-by: Greg Kroah-Hartman --- v3: - Add Acked-by, - Rebased. --- drivers/tty/serial/sh-sci.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c index c3a193616484a62c..d89d4b7576cf3a15 100644 --- a/drivers/tty/serial/sh-sci.c +++ b/drivers/tty/serial/sh-sci.c @@ -1860,20 +1860,24 @@ static void sci_shutdown(struct uart_port *port) sci_free_irq(s); } -static unsigned int sci_scbrr_calc(struct sci_port *s, unsigned int bps, - unsigned long freq) +/* calculate sample rate, BRR, and clock select */ +static void sci_scbrr_calc(struct sci_port *s, unsigned int bps, + unsigned long freq, int *brr, unsigned int *srr, + unsigned int *cks) { - return DIV_ROUND_CLOSEST(freq, s->sampling_rate * bps) - 1; -} - -/* calculate sample rate, BRR, and clock select for HSCIF */ -static void sci_baud_calc_hscif(struct sci_port *s, unsigned int bps, - unsigned long freq, int *brr, - unsigned int *srr, unsigned int *cks) -{ - unsigned int sr, br, prediv, scrate, c; + unsigned int min_sr, max_sr, shift, sr, br, prediv, scrate, c; int err, min_err = INT_MAX; + if (s->sampling_rate) { + min_sr = max_sr = s->sampling_rate; + shift = 0; + } else { + /* HSCIF has a variable sample rate */ + min_sr = 8; + max_sr = 32; + shift = 1; + } + /* * Find the combination of sample rate and clock select with the * smallest deviation from the desired baud rate. @@ -1889,10 +1893,10 @@ static void sci_baud_calc_hscif(struct sci_port *s, unsigned int bps, * (|D - 0.5| / N * (1 + F))| * NOTE: Usually, treat D for 0.5, F is 0 by this calculation. */ - for (sr = 32; sr >= 8; sr--) { + for (sr = max_sr; sr >= min_sr; sr--) { for (c = 0; c <= 3; c++) { /* integerized formulas from HSCIF documentation */ - prediv = sr * (1 << (2 * c + 1)); + prediv = sr * (1 << (2 * c + shift)); /* * We need to calculate: @@ -1974,16 +1978,8 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios, max_baud = port->uartclk ? port->uartclk / 16 : 115200; baud = uart_get_baud_rate(port, termios, old, 0, max_baud); - if (likely(baud && port->uartclk)) { - if (s->cfg->type == PORT_HSCIF) { - sci_baud_calc_hscif(s, baud, port->uartclk, &t, &srr, - &cks); - } else { - t = sci_scbrr_calc(s, baud, port->uartclk); - for (cks = 0; t >= 256 && cks <= 3; cks++) - t >>= 2; - } - } + if (likely(baud && port->uartclk)) + sci_scbrr_calc(s, baud, port->uartclk, &t, &srr, &cks); sci_port_enable(s);