From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751192AbdFBRL2 (ORCPT ); Fri, 2 Jun 2017 13:11:28 -0400 Received: from mail-qk0-f193.google.com ([209.85.220.193]:33095 "EHLO mail-qk0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750955AbdFBRL0 (ORCPT ); Fri, 2 Jun 2017 13:11:26 -0400 MIME-Version: 1.0 In-Reply-To: References: <1494316248-24052-1-git-send-email-aisheng.dong@nxp.com> <1494316248-24052-7-git-send-email-aisheng.dong@nxp.com> From: Andy Shevchenko Date: Fri, 2 Jun 2017 20:11:24 +0300 Message-ID: Subject: Re: [PATCH 6/6] tty: serial: lpuart: add a more accurate baud rate calculation method To: "A.S. Dong" Cc: "linux-serial@vger.kernel.org" , "linux-kernel@vger.kernel.org" , linux-arm Mailing List , Greg Kroah-Hartman , Jiri Slaby , Andy Duan , Stefan Agner , Mingkai Hu , "Y.B. Lu" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by mail.home.local id v52HBVwu024050 On Wed, May 31, 2017 at 5:18 PM, A.S. Dong wrote: >> On Tue, May 9, 2017 at 10:50 AM, Dong Aisheng wrote: By some reason my previous message went privately. It didn't have anything major anyway and here I'm suggesting optimization of finding factors of the formula in use. See below. >> > + u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp; >> > + u32 clk = sport->port.uartclk; >> > + >> > + /* >> > + * The idea is to use the best OSR (over-sampling rate) possible. >> > + * Note, OSR is typically hard-set to 16 in other LPUART >> instantiations. >> > + * Loop to find the best OSR value possible, one that generates >> minimum >> > + * baud_diff iterate through the rest of the supported values of >> OSR. >> > + * >> > + * Calculation Formula: >> > + * Baud Rate = baud clock / ((OSR+1) × SBR) >> > + */ >> > + baud_diff = baudrate; >> > + osr = 0; >> > + sbr = 0; >> > + >> >> > + for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) { I missed one thing, what happened by default to OSR? What is the value in use? >> I _think_ you may simplify this and avoid for-loop if you reconsider >> approach. > But there is indeed a optimization way, see below. > To optimize the looping, we probably could do: > If (!baud_diff) > Break; It's a small one, we may have more interesting approach. So, the algo is the following: Assume the ranges like this: OSR = [4 ... 32] SBR = [2 ... 8192] Then: 1. Get ratio factor as ratio = CLK / desired baud rate 2. If ratio < 8192 * 9 / 2, just use (ratio / 4, 4) as (OSR, SBR) setting. (Needs clarification on OSR < 4) 3. if ratio >= 8192 * 31, just use those two numbers (8192, 31). You can't do anything better there. 4. Otherwise, get a minimum required factor of OSR osr_min = ratio / 8192 5. Start your loop from osr_min + 1 to 31. 6 (optional). Of course you may not consider baud_diff > osr_min, it's I suppose obvious P.S. Note, all divisions by 2^n are just simple right shifts. Diffs are calculated as multiplication of OSR and SBR in comparison to ratio. One division so far. -- With Best Regards, Andy Shevchenko