Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755017AbdFLQSR (ORCPT ); Mon, 12 Jun 2017 12:18:17 -0400 Received: from mail-co1nam03on0057.outbound.protection.outlook.com ([104.47.40.57]:30528 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754900AbdFLPh5 (ORCPT ); Mon, 12 Jun 2017 11:37:57 -0400 Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=nxp.com; nxp.com; dkim=none (message not signed) header.d=none;nxp.com; dmarc=fail action=none header.from=nxp.com; From: Dong Aisheng To: CC: , , , , , , , , , , , Dong Aisheng Subject: [PATCH V3 7/7] tty: serial: lpuart: add a more accurate baud rate calculation method Date: Mon, 12 Jun 2017 23:37:28 +0800 Message-ID: <1497281848-12995-8-git-send-email-aisheng.dong@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1497281848-12995-1-git-send-email-aisheng.dong@nxp.com> References: <1497281848-12995-1-git-send-email-aisheng.dong@nxp.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-Matching-Connectors: 131417554695162600;(91ab9b29-cfa4-454e-5278-08d120cd25b8);() X-Forefront-Antispam-Report: CIP:192.88.168.50;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(336005)(39380400002)(39410400002)(39860400002)(39400400002)(39840400002)(39850400002)(39450400003)(2980300002)(1110001)(1109001)(339900001)(189002)(54534003)(199003)(9170700003)(2351001)(23676002)(50226002)(2870700001)(7416002)(356003)(498600001)(77096006)(53936002)(33646002)(106466001)(105606002)(104016004)(5660300001)(4326008)(76176999)(189998001)(38730400002)(110136004)(50466002)(5820100001)(47776003)(50986999)(6666003)(6916009)(8656002)(85426001)(54906002)(8936002)(2906002)(8676002)(2950100002)(81166006)(305945005)(39060400002)(86362001)(36756003);DIR:OUT;SFP:1101;SCL:1;SRVR:BLUPR03MB167;H:tx30smr01.am.freescale.net;FPR:;SPF:Fail;MLV:ovrnspm;MX:1;A:1;PTR:InfoDomainNonexistent;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;BL2FFO11FD055;1:jXuioey6Uh207YBdDHrO5jOMyYoBUo2Uubz62v8A5kOF1G5xEh7Ruo1g2m4hRtNi1Gye9digfwD5nz2dZMKsPrcac5+DuPnS9Y4ba7xBJFg2ZIfv3Tghs6d8FLGhm9g3eFL8cq/OIrWBU+f5e4gGJM6CF/9QC/WRcKhE5Xsyb6N/+rLQYxOYZ20cmvHyfOZ6aXYDQqc4CXmq8pG0SbBpYCvJplsI3DPSk84UzfQBWe4pqJFRDrQmo4X3f8M60wW5WPjwOPUCDDkmdBXEwoURP9nMQI0AAZfdLhITKkeHqb1dcMdaXQk0sD+hGiOIvxhuUEGVjW1ji0AbRcaqNN89BbhYdKFfOe7RWWb9FPmIZyHf12Tck/NTZSMXqoC4Tp9I+e/T4xy4h5+ZeQoyPNkoHld37hCgwvCuk+arfND7z3yYNrgVQ7jFvwwYe+Rcd2/SbotwfohS5PImgfcD33Q9TgcmVjYxiBMvJU2JZULdVTHHEAhn4Ah+JYy5GOGd6Uq8Co+fXWlRX1XeFnqCTzc73cTDjxN8Gau372gTSYEHnYWiLjAWz99+ygYUFZ1iehCywctEEnu0bnw3fX0VnRMXlQY3CRqIuBAYlcbY3U90osM4FIWLucdF9Q6bysTF/O2ooXye3zzhH77dmL4YdwsrXy0XRStVlnk2SMVTLNxnas4PldvbmqEYxQ3tYxXIvBEe X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BLUPR03MB167: X-MS-Office365-Filtering-Correlation-Id: 7879c53e-ee6b-4293-a98b-08d4b1a8fb4f X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(201703131430075)(201703131517081);SRVR:BLUPR03MB167; X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB167;3:H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kl+1B0pXK6WFQIi5zRf8V1dbmESeoWnoP7BJI1B9gWK0tl+ZytBBaXyF0WBgK3e+K84EwgZMs27/x+MXwFmaXQEB3LtQegDhXq4UrhKH6uMNZcG7LE6c/UwBtnDaxdwbRHMoeXGjAGwA X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6095135)(601004)(2401047)(8121501046)(5005006)(13018025)(13016025)(10201501046)(3002001)(100000703101)(100105400095)(93006095)(93001095)(6055026)(6096035)(20161123559100)(20161123563025)(201703131430075)(201703131441075)(201703131448075)(201703131433075)(201703161259150)(20161123561025)(20161123565025)(20161123556025)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:BLUPR03MB167;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(400006)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:BLUPR03MB167; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCTFVQUjAzTUIxNjc7NDpUQm0zTG1iUFZCaG1tdGpHUFVkMHVySHFsSmNj?= =?utf-8?B?YlladVRNcUVreTZaWkpsMjZ2V0NITW45Uy9IeE0xYjNEb2E1V0V0L2VVVlpS?= =?utf-8?B?ekRRR3kzd08wWTFRREk4MCtFMHJ6VjBGakQ3bzJhQXoyc2pWR1pnZXRKNmY3?= =?utf-8?B?MWNDSzNLb2x5QlJmNU1JdjVBL1B4TnlWK2F4RWJzUU9URHIvUlgyc05aeGxT?= =?utf-8?B?OFlPSXhqTTJaNFlITTZrMEEybFp3TzVBOHhJaFQrUDJKY1NYUVV1YzN3eWlw?= =?utf-8?B?ZGkwQi9NQmo3OG1hR1FFUHc3VHVQcExLNFk1SFVSNDdHR1gvWGc2anN1U1pZ?= =?utf-8?B?SVF4bmI5N01wZjFxZlN1dHk2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OmrU5VXLPqKnakjxEADTgrBbAQwhBObhr8XznKaf4QK//KUeX/3s1rkqtAvSL7HeNnSQ6yaf1DpRxQim2gSVSZ79BDvxXIVDkJLpO+eVWcCa+Q== X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB167;5:9BFy/wuwuNKvWM+XWXDM9nRA0UKmTOBQpSDoM5z1+x/SdKd76CH7XFAN66GCfbZ/1GuzcW8F5hEGKerqgE/ZduFXfEDG16ioRo4Gx6Uv3Wps0ZPpMLRV/fjtz26ENqbvUC+TDOXgKQXwdnpZhHcPnkZHZJzxjESiDXKYwTQBMg5U89z1+ON5b62EP5me+Y3ablRijje8bNn3NJOiztKId6JhfcgRYk+R7hX/AKSUq643plFkcuiKlbL5t+rDUjdmY5GdDEBcJveN4wv6A+7SyYqfKGNsDiR+MufTXgYBWfumlJqa7TVnS5xeg/ogVMxKLSlQHgelvbo5L0FCuFI8KnHqJkxRVl7Ay9MkneLzaE4v/6qgvB6nPHUsUv6strppZeruPz13WU9NP+8OZ4O/DG0ufGoNisWFYM++3lvUm+qw3eA37T3Z4qtBesf4+wS9URPO+FGT8zmcA6/3bUACqmAfm5PDzgPgErCV6eoNLA5XhHI10YGWmLk1e5sVs8QV8/84Ze2RFr7UWoB9r9yW+A==;24:Atagmxyn6mdCral6Pf19a8GraRfBcW+oLed5EM3zXqI5QALFWjshsptitjgxAEGTRDc/j6VqGP6IYjOI2hF5AyL3/e/Y7X+Z8u6GySOkhEI= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB167;7:qvqdthtdtHwOewPIwxggth60qOBZ/F2MJI9Al38pu4vpzIwDHzUxVdl+HO6sGAyMc3Seg29PdjvVi/kv/m0UoOKa/IVZ8IoXY0C7sqQLkoITzknyahxJVS+ALckoExBYcyANauRx5tp3mttFOmm7NV8ehB4sW3NTi98v1kejEtSR4aPEAP2ZqJps16MukiiX+d39WDYWyQDe7BOEZ5M8dj/AeB3HuL19QpNlPWB/Ukw71/3Eu9AyaP4HwfBm+12fRuTszTqG0HHIIBIsZQSgQ3J4ZSKBx8piT/GnWrHtzVVzdBtpdco9e3Mgkjv7K9YtskA0OWm0BSo2YtrP/cQkeA== X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2017 15:37:49.2978 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e;Ip=[192.88.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR03MB167 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4512 Lines: 142 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. Cc: Greg Kroah-Hartman Cc: Jiri Slaby Cc: Stefan Agner Cc: Mingkai Hu Cc: Yangbo Lu Acked-by: Fugang Duan Signed-off-by: Dong Aisheng --- ChangeLog: v2->v3: * enable new baud rate calculation method by default to get Layerscape platforms run and test. * break out in case we already find the minum baud_diff v1->v2: * No changes --- drivers/tty/serial/fsl_lpuart.c | 79 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c index 6a725cb..a146289 100644 --- a/drivers/tty/serial/fsl_lpuart.c +++ b/drivers/tty/serial/fsl_lpuart.c @@ -140,6 +140,8 @@ #define UARTBAUD_SBNS 0x00002000 #define UARTBAUD_SBR 0x00000000 #define UARTBAUD_SBR_MASK 0x1fff +#define UARTBAUD_OSR_MASK 0x1f +#define UARTBAUD_OSR_SHIFT 24 #define UARTSTAT_LBKDIF 0x80000000 #define UARTSTAT_RXEDGIF 0x40000000 @@ -1511,6 +1513,75 @@ lpuart_set_termios(struct uart_port *port, struct ktermios *termios, } static void +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++) { + /* 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; + + /* 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; + + if (!baud_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"); + + tmp = lpuart32_read(&sport->port, UARTBAUD); + + if ((osr > 3) && (osr < 8)) + tmp |= UARTBAUD_BOTHEDGE; + + tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT); + tmp |= (((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT); + + tmp &= ~UARTBAUD_SBR_MASK; + tmp |= sbr & UARTBAUD_SBR_MASK; + + tmp &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE); + + lpuart32_write(&sport->port, tmp, UARTBAUD); +} + +static void lpuart32_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old) { @@ -1519,7 +1590,6 @@ lpuart32_set_termios(struct uart_port *port, struct ktermios *termios, unsigned long ctrl, old_ctrl, bd, modem; unsigned int baud; unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8; - unsigned int sbr; ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL); bd = lpuart32_read(&sport->port, UARTBAUD); @@ -1616,12 +1686,7 @@ lpuart32_set_termios(struct uart_port *port, struct ktermios *termios, lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE), UARTCTRL); - 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(&sport->port, bd, UARTBAUD); + lpuart32_serial_setbrg(sport, baud); lpuart32_write(&sport->port, modem, UARTMODIR); lpuart32_write(&sport->port, ctrl, UARTCTRL); /* restore control register */ -- 2.7.4