Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp69596imu; Tue, 8 Jan 2019 14:52:19 -0800 (PST) X-Google-Smtp-Source: ALg8bN5NGXRtn8AS+vHqc0UFTWdcCdzpeRuuGEbsLjCKxPUCSa5cP1Ez3ulX8SZxvSWHLoXeGK3o X-Received: by 2002:a17:902:f082:: with SMTP id go2mr3637441plb.115.1546987939002; Tue, 08 Jan 2019 14:52:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546987938; cv=none; d=google.com; s=arc-20160816; b=HZixlsBSJbNF7UGNiUBs40Ix5P+GQHg5+TSZTNLfp76itO6DhSX1Q2QhTb3GL1Wqfh BuRFwbHWU0AnsuodNSPj+bc03mXEtRePpbBUkpkq9YFHkAFCBQNjzMdeI0xmLiGqGKo4 LjBeqeIqDEegtP/9qLyw4mQQgSOITEQe35MOUoZXogqh4jNRIhTzAf2uE9n9jirFrjS8 83cM/gtg+hUgth/iLHl2gv1nNMS+XkIVtfYJfaQ+hEFUsPRTXNtyVSUKILK263XYMjh3 3T0BNkfBEiqNZumXoXPVP6btYJSOh52voMA9MZqmXo+RaAYybadbwlqK8rRtGJYolaOz HBxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=M2c5gHd+PxHWdbs3Q9kahMM3EsAcDLbHhgvS3KlxQ/A=; b=hz4X5tNFryNxN/cV5++jmyo3UptdSXDQUljjv1zPvSZ7xxtKOLgQMi4g6zdmlcKxtN o71wYQiEKsWoAw9o0mo6jWenY7rfn92Jjyj7h4nWGGPzBOVnQIfCzV7wCYlUZPZK9Idb poy3YCpYZ3CiKWNb2Hg6aNWfCQSPa0jUoBSnLBFOBqZxxmZeiKHbhvKGU7749IKupJZO h4KEPjkF3xqjEaswJeyJAEcBiusL6qS8cOO9h244XjN0MheJQ9cM/ryrypIsBnolp2I8 zPAD4/xfF48n5nUa0W3ndG/mihflMvRbalJWhTIdDJlEkkRI/W5c2W05Evf7QgI7QRIi fm9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KIO5ELaB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 3si34941008plq.138.2019.01.08.14.52.03; Tue, 08 Jan 2019 14:52:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KIO5ELaB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729946AbfAHUIv (ORCPT + 99 others); Tue, 8 Jan 2019 15:08:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:33394 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729579AbfAHT1T (ORCPT ); Tue, 8 Jan 2019 14:27:19 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5B47A20883; Tue, 8 Jan 2019 19:27:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1546975639; bh=fStCbmvejQ5Ut/mV1TBU45k5TJ5W6IpNXoRTxad5qnw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KIO5ELaBAUKqeoSwJFUvDH28flHjzkwhn0fROLM/e6Vbn8yLkHbwumByuozytQn57 O8XRFzXoej/8hFVyGPv5dxSQ3OGM2mw97XPW3PHBfXg2veTUUr0GReVL8xB43/V8t6 yy8qBZmXHoZcSQ/sHszpMn4O4AAcpfwiWEsrZX8M= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Nikolaj Fogh , Johan Hovold , Sasha Levin , linux-usb@vger.kernel.org Subject: [PATCH AUTOSEL 4.20 025/117] USB: serial: ftdi_sio: use rounding when calculating baud rate divisors Date: Tue, 8 Jan 2019 14:24:53 -0500 Message-Id: <20190108192628.121270-25-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190108192628.121270-1-sashal@kernel.org> References: <20190108192628.121270-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nikolaj Fogh [ Upstream commit 6abd837104a3a8e1cda64fc4d7675f6c3ece9d8b ] Improve baud-rate generation by using rounding-to-closest instead of truncation in divisor calculation. Results have been verified by logic analyzer on an FT232RT (232BM) chip. The following table shows the wanted baud rate, the baud rate obtained with the old method (truncation), with the new method (rounding) and the baud rate generated by the windows 10 driver. The numbers in parentheses is the error. +- Wanted --+------ Old -------+------ New -------+------ Win -------+ |   9600  |   9600 (0.00%)  |   9604 (0.05%)  |   9605 (0.05%)  | |   19200   |   19200 (0.00%)  |   19199 (0.01%)  |   19198 (0.01%)  | |   38400   |   38395 (0.01%)  |   38431 (0.08%)  |   38394 (0.02%)  | |   57600   |   57725 (0.22%)  |   57540 (0.10%)  |   57673 (0.13%)  | |  115200   |  115307 (0.09%)  |  115330 (0.11%)  |  115320 (0.10%)  | |  921600   |  919963 (0.18%)  |  920386 (0.13%)  |  920810 (0.09%)  | |  961200   |  996512 (3.67%)  |  956480 (0.49%)  |  956937 (0.44%)  | +-----------+------------------+------------------+------------------+ The error due to noise in the measurements is in the order of a few tenths of a %. As can be seen, the baud rate is significantly improved for some rates (e.g. 961200), and corresponds to the output given by the windows driver. The theoretical baud rate has been calculated for all baud rates from 1 to 3M, and as expected, the error is centered around 0, with a triangle shape instead of a sawtooth, so the maximum error is decreased to half. Signed-off-by: Nikolaj Fogh [ johan: edit commit message slightly ] Signed-off-by: Johan Hovold Signed-off-by: Sasha Levin --- drivers/usb/serial/ftdi_sio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c index 609198d9594c..1ab2a6191013 100644 --- a/drivers/usb/serial/ftdi_sio.c +++ b/drivers/usb/serial/ftdi_sio.c @@ -1130,7 +1130,7 @@ static unsigned short int ftdi_232am_baud_base_to_divisor(int baud, int base) { unsigned short int divisor; /* divisor shifted 3 bits to the left */ - int divisor3 = base / 2 / baud; + int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud); if ((divisor3 & 0x7) == 7) divisor3++; /* round x.7/8 up to x+1 */ divisor = divisor3 >> 3; @@ -1156,7 +1156,7 @@ static u32 ftdi_232bm_baud_base_to_divisor(int baud, int base) static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 }; u32 divisor; /* divisor shifted 3 bits to the left */ - int divisor3 = base / 2 / baud; + int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud); divisor = divisor3 >> 3; divisor |= (u32)divfrac[divisor3 & 0x7] << 14; /* Deal with special cases for highest baud rates. */ @@ -1179,7 +1179,7 @@ static u32 ftdi_2232h_baud_base_to_divisor(int baud, int base) int divisor3; /* hi-speed baud rate is 10-bit sampling instead of 16-bit */ - divisor3 = base * 8 / (baud * 10); + divisor3 = DIV_ROUND_CLOSEST(8 * base, 10 * baud); divisor = divisor3 >> 3; divisor |= (u32)divfrac[divisor3 & 0x7] << 14; -- 2.19.1