From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751159AbdEaOSO (ORCPT ); Wed, 31 May 2017 10:18:14 -0400 Received: from mail-he1eur01on0081.outbound.protection.outlook.com ([104.47.0.81]:26976 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751022AbdEaOSK (ORCPT ); Wed, 31 May 2017 10:18:10 -0400 From: "A.S. Dong" To: Andy Shevchenko 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" Subject: RE: [PATCH 6/6] tty: serial: lpuart: add a more accurate baud rate calculation method Thread-Topic: [PATCH 6/6] tty: serial: lpuart: add a more accurate baud rate calculation method Thread-Index: AQHSyJkTP2kvMIG3AE6J+8TQN3PcYKII+rCAgAVBh5A= Date: Wed, 31 May 2017 14:18:04 +0000 Message-ID: References: <1494316248-24052-1-git-send-email-aisheng.dong@nxp.com> <1494316248-24052-7-git-send-email-aisheng.dong@nxp.com> In-Reply-To: Accept-Language: zh-CN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: gmail.com; dkim=none (message not signed) header.d=none;gmail.com; dmarc=none action=none header.from=nxp.com; x-originating-ip: [192.158.241.86] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;HE1PR0401MB2266;7:8DnLLg+2YJOiMTK6M2K7WlkBM0k5oVfAPPM64aPBeJpbAy78z5XMrJULt1b0oKnP3PMQNFPrEZb5Aivdt0fiKfnY/rfymlHdlt6myMe4YbsHimikIXQUqq4Dc7GMhU2eK1MepIA3lToI9RX9dJR/AGqw9DR6Yu/uBW52z8bIA/E4j0l9HE40Bf5QK0RpQEvgfUkk1lC9QnKktp0wS/eS2Ts5o8Id76/2kMwAMQrcHQOt2Jp0XfbA2hdiMYU4s3WqEMyIq+jyxIUkPsQj+FTV2ltRaor3s71igotJml3XI0mlhLNCiXq23qcq3khnhCVpAhWiCU9Se+dWECjev01Hgg== x-forefront-antispam-report: SFV:SKI;SCL:-1SFV:NSPM;SFS:(10009020)(6009001)(39410400002)(39400400002)(39450400003)(39840400002)(39850400002)(39860400002)(24454002)(13464003)(377454003)(66066001)(54356999)(76176999)(6116002)(102836003)(3846002)(86362001)(2950100002)(6916009)(7696004)(50986999)(8936002)(229853002)(81166006)(5250100002)(33656002)(8676002)(2900100001)(55016002)(305945005)(7736002)(99286003)(54906002)(9686003)(38730400002)(6436002)(74316002)(6506006)(39060400002)(110136004)(4326008)(53936002)(6246003)(2906002)(3280700002)(189998001)(3660700001)(478600001)(5660300001)(25786009)(53546009)(14454004);DIR:OUT;SFP:1101;SCL:1;SRVR:HE1PR0401MB2266;H:AM3PR04MB306.eurprd04.prod.outlook.com;FPR:;SPF:None;MLV:ovrnspm;PTR:InfoNoRecords;LANG:en; x-ms-traffictypediagnostic: HE1PR0401MB2266: x-ms-office365-filtering-correlation-id: a95838c1-4d5c-4756-8158-08d4a82fdab7 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(2017030254075)(48565401081)(201703131423075)(201703031133081);SRVR:HE1PR0401MB2266; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(9452136761055)(185117386973197); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(5005006)(8121501046)(10201501046)(93006095)(93001095)(3002001)(6055026)(6041248)(20161123564025)(20161123560025)(20161123562025)(20161123555025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(6072148);SRVR:HE1PR0401MB2266;BCL:0;PCL:0;RULEID:;SRVR:HE1PR0401MB2266; x-forefront-prvs: 0324C2C0E2 spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-originalarrivaltime: 31 May 2017 14:18:04.9528 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0401MB2266 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 base64 to 8bit by mail.home.local id v4VEIU7W028525 Hi Andy, > -----Original Message----- > From: Andy Shevchenko [mailto:andy.shevchenko@gmail.com] > Sent: Sunday, May 28, 2017 8:04 AM > 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 > Subject: Re: [PATCH 6/6] tty: serial: lpuart: add a more accurate baud > rate calculation method > > On Tue, May 9, 2017 at 10:50 AM, Dong Aisheng wrote: > > On new LPUART versions, the oversampling ratio for the receiver can be > > changed from 4x (00011) to 32x (11111) which could help us get a more > > accurate baud rate divider. > > > > 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. > > > +lpuart32_serial_setbrg(struct lpuart_port *sport, unsigned int > > +baudrate) { > > + 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 _think_ you may simplify this and avoid for-loop if you reconsider > approach. > The algorithm is that we have to iterate all possible OSCs and find the one with minimum baud_diff. I'm not sure what alternative approach did you mean? But there is indeed a optimization way, see below. > > + /* calculate the temporary sbr value */ > > + tmp_sbr = (clk / (baudrate * tmp_osr)); > > + if (tmp_sbr == 0) > > + tmp_sbr = 1; > > + > > + /* > > + * calculate the baud rate difference based on the > temporary > > + * osr and sbr values > > + */ > > > + tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate; > > (32 - 4 + 1) times division is called... > Yes. > > + > > + /* select best values between sbr and sbr+1 */ > > + tmp = clk / (tmp_osr * (tmp_sbr + 1)); > > + if (tmp_diff > (baudrate - tmp)) { > > + tmp_diff = baudrate - tmp; > > + tmp_sbr++; > > + } > > + > > + if (tmp_diff <= baud_diff) { > > + baud_diff = tmp_diff; > > + osr = tmp_osr; > > + sbr = tmp_sbr; To optimize the looping, we probably could do: If (!daud_diff) Break; > > + } > > + } > > > + /* handle buadrate outside acceptable rate */ > > + if (baud_diff > ((baudrate / 100) * 3)) > > + dev_warn(sport->port.dev, > > + "unacceptable baud rate difference of more > > + than 3%%\n"); > > Shouldn't you fall back to previous setting? > Hmmm.. Is there defined semantic to do that or is there any other ones doing that way? I see most drivers not doing that. > > + > > + tmp = lpuart32_read(sport->port.membase + UARTBAUD); > > + > > > + if ((osr > 3) && (osr < 8)) > > Isn't it > > if (osr ^ BIT(2) < BIT(2)) > > ? > That is obvious hard to understand and I'd rather keep a more explicit way. > > + tmp |= UARTBAUD_BOTHEDGE; > > > +} > > > + if (of_device_is_compatible(port->dev->of_node, "fsl,imx7ulp- > lpuart")) { > > + lpuart32_serial_setbrg(sport, baud); > > > + } else { > > + sbr = sport->port.uartclk / (16 * baud); > > + bd &= ~UARTBAUD_SBR_MASK; > > + bd |= sbr & UARTBAUD_SBR_MASK; > > + bd |= UARTBAUD_BOTHEDGE; > > + bd &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE); > > + lpuart32_write(bd, sport->port.membase + UARTBAUD); > > + } > > Perhaps it makes sense to split this to a helper function as well (in a > separate patch). > That will be removed according to Stefan's suggestion to get LS platforms Start to test. Regards Dong Aisheng