From patchwork Wed Jan 23 16:49:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1030070 X-Patchwork-Delegate: uboot@andestech.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.b="OmSSUWRS"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sharedspace.onmicrosoft.com header.i=@sharedspace.onmicrosoft.com header.b="gC+BRyLc"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 43lBJY2Hhvz9s3l for ; Thu, 24 Jan 2019 03:56:28 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 750EDC21D72; Wed, 23 Jan 2019 16:55:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=KHOP_BIG_TO_CC, RCVD_IN_DNSWL_BLOCKED, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id AAA30C21E16; Wed, 23 Jan 2019 16:52:21 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 001BCC21E39; Wed, 23 Jan 2019 16:50:03 +0000 (UTC) Received: from esa3.hgst.iphmx.com (esa3.hgst.iphmx.com [216.71.153.141]) by lists.denx.de (Postfix) with ESMTPS id ED869C21DFA for ; Wed, 23 Jan 2019 16:49:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1548262199; x=1579798199; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=jki6yc4Dgywc+/2UFctRcM6KIGbue7NLrZzh+PGks1I=; b=OmSSUWRStaRoWpgwusyZFEB2KZ7G907fqIKkWuBHdxSfidLxBGb0E3in 1UKHkHN+Imt04dR2agr45e49i9vpTd31Db2QqR1g5m6V7VXCux3UNh+9O 9+W14JWLIEbFUzhfjC6C1/eL8QFUdwz3gqfiyiV03iMKmeV2nAcUIcf+G yuMN4u44J5W+C7++UeXxPymaZhf5qfPL6WQ0joG83t1OCBlplaKsBBo1P BPkxROzUQLhYQO1JfAk/G0MY5LOc2Jwz6E5pgL2Lxng3alATD9lOvazks UEF/ec6aIvebh0eImWI7avb/fAEUUKHBd7hx8Y0DrPt0zWKsgz+1SQ8Jy w==; X-IronPort-AV: E=Sophos;i="5.56,512,1539619200"; d="scan'208";a="104534187" Received: from mail-bn3nam04lp2053.outbound.protection.outlook.com (HELO NAM04-BN3-obe.outbound.protection.outlook.com) ([104.47.46.53]) by ob1.hgst.iphmx.com with ESMTP; 24 Jan 2019 00:49:55 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector1-wdc-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FqeSzEs6ykL19UgS2gTj9tz1EZqeYicmQtYCOl+rl+Y=; b=gC+BRyLcquJBKnaL4iMo9TFMD1XjI1AFG5DVhvM+PxLTbVyf1pGzyiz/oZHXNAbI/5im1VaMS7QhQn4Z8PmrsHXFt+D3LSS+mx3J+2OXkUkiL88tvYguYz9oeGhq3ep0EP0OWVDZODSRSSKYX/O71Rl3swxy/BBLis2GDP4T238= Received: from BYAPR04MB5303.namprd04.prod.outlook.com (20.178.49.148) by BYAPR04MB5110.namprd04.prod.outlook.com (52.135.235.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1558.16; Wed, 23 Jan 2019 16:49:49 +0000 Received: from BYAPR04MB5303.namprd04.prod.outlook.com ([fe80::add1:92cc:65d7:5d94]) by BYAPR04MB5303.namprd04.prod.outlook.com ([fe80::add1:92cc:65d7:5d94%2]) with mapi id 15.20.1537.031; Wed, 23 Jan 2019 16:49:49 +0000 From: Anup Patel To: Rick Chen , Bin Meng , Joe Hershberger , Michal Simek , Michal Simek , Lukas Auer , Masahiro Yamada , Simon Glass Thread-Topic: [PATCH v4 11/15] drivers: serial_sifive: Fix baud rate calculation Thread-Index: AQHUszunvHGpasWBu0eBAgsGS6kmOA== Date: Wed, 23 Jan 2019 16:49:49 +0000 Message-ID: <20190123164808.128994-12-anup.patel@wdc.com> References: <20190123164808.128994-1-anup.patel@wdc.com> In-Reply-To: <20190123164808.128994-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MA1PR01CA0077.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00::17) To BYAPR04MB5303.namprd04.prod.outlook.com (2603:10b6:a03:c8::20) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [49.207.49.227] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; BYAPR04MB5110; 6:RRfsxGz5jwOO5bvRbbPGqRbHXrRQ1juhl59QeoiVLXNtT+lakZScahMM0ZU19djKZsBsPaAfY6OA+N/D+BJxGjJSGOu6P6V+T9X4dRf0ztwmIDD0zLMeOczEDkVygjl6Afk467sNZHUIhOrbPaNyu6FZibLCtB7K0XveuCoEtJFmTc/0ID2lyVBHxnXdjkwPPtiIiHhM042n74XG6UjYHQ42vBB2wqLMH1KkD0O9WrsGiTwQfMdFppVHRADP9pP9T9WePxrCk05yl3oRTEArrs/ZB3G9GRexFAEZDc7YmnYu5/fKRBRDAop3ohYWuyLh4/FadKTlbbe+pDk0DwYXrmfzF3tdR3Y+DABWsmZJv3z95WLtzsnTJzOJenXXmGLxO5tNx6jZjL8gh0zYI0/WCr472ZtlambQIXi7LBaJmhlFYHxk4AeSMbi59WMVvZw8jEp5pUD2RxZ1rfa+nIvHiA==; 5:YIqa1s5DXeieMMuiq5shku+XrLxlLk/hrKBs2qhyjdoGmo5C7xIA5naykHGZ5+rp1qlkLn38+PCnAVCPGICvAisJLZuNSl3beppXGiyyjF+lRo9b1f/43yCoWSBC5Z6o/OkFjW8nESbdH+5uE03JdqADk1N2ndr7uQJEEUYJlEQYEbneJOMwbEvnke9HU6kvKFqKOg6XQ9t+XGTSQaCMRA==; 7:DYw0YdNAj6zfOirgphQFTVroW7LklXX1YxJ47404u92edt+DMvPUcYwlLFtzZNgEn89x1PhZO3CB2qv1/sbbtDbin14q5J6WBhZp6HwmnrjpL04a3ZPNZ/PI3ZtzNEmaVbRx6qdJ7oXX1vo/asQdng== x-ms-office365-filtering-correlation-id: d2440aa7-e0db-48c9-f3d5-08d68152c9d9 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(5600110)(711020)(4605077)(4618075)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020); SRVR:BYAPR04MB5110; x-ms-traffictypediagnostic: BYAPR04MB5110: wdcipoutbound: EOP-TRUE x-microsoft-antispam-prvs: x-forefront-prvs: 0926B0E013 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(366004)(346002)(39860400002)(136003)(376002)(396003)(189003)(199004)(99286004)(2906002)(110136005)(7416002)(486006)(50226002)(54906003)(316002)(52116002)(6512007)(76176011)(53936002)(36756003)(97736004)(71200400001)(71190400001)(476003)(86362001)(8936002)(14454004)(1076003)(68736007)(2616005)(102836004)(386003)(6506007)(25786009)(11346002)(66066001)(3846002)(106356001)(6116002)(55236004)(72206003)(44832011)(81166006)(81156014)(6486002)(478600001)(39060400002)(14444005)(256004)(26005)(4326008)(7736002)(6436002)(305945005)(186003)(105586002)(446003)(8676002); DIR:OUT; SFP:1102; SCL:1; SRVR:BYAPR04MB5110; H:BYAPR04MB5303.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: faPGSv8X+aEhMJgr7/wAM2Z/jK2UT9lecx2TXNNFma7KvAGwnPOyGwCLVIPELgcTb3TeoKWD9rXXgLiT3c5bBDlTKy798BEghUjXdK+KD/VTMFmp2uyhuaN5jqQkGPw9mg4Ttj04OcSAz6bt4t4teOA3Wr+RnpvxWlE7epW0ePF0D03HW7WehQkArRKafduYCWCJZRfpENSctEPcaq26VgXtZUCdjmQIJwiXo8LIF8eWqWSNkp2nt69tQc3ti995f1iToGfubC/9dIBRiR4k8CzqJthbChda7JVH1BuARhm7HmCQz/zhMd1VbIzOHuhIWoxtV1rNs1dzhd+Ic1l6ZW794sCs0VkvPKNzFBiFE08Or+Uyd/25/deSTB2qPYU7K/s1egLprniWCW3YthhkQKVPLf7Lw8ooNCE74d+8EXg= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: d2440aa7-e0db-48c9-f3d5-08d68152c9d9 X-MS-Exchange-CrossTenant-originalarrivaltime: 23 Jan 2019 16:49:45.4813 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR04MB5110 Cc: Palmer Dabbelt , U-Boot Mailing List , Alexander Graf , Christoph Hellwig , Paul Walmsley Subject: [U-Boot] [PATCH v4 11/15] drivers: serial_sifive: Fix baud rate calculation X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Atish Patra Compute the baud rate multipler with more precision. Signed-off-by: Atish Patra Signed-off-by: Anup Patel Reviewed-by: Alexander Graf Reviewed-by: Lukas Auer --- drivers/serial/serial_sifive.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/drivers/serial/serial_sifive.c b/drivers/serial/serial_sifive.c index 341728a690..ea4d35d48c 100644 --- a/drivers/serial/serial_sifive.c +++ b/drivers/serial/serial_sifive.c @@ -33,16 +33,40 @@ struct uart_sifive { }; struct sifive_uart_platdata { - unsigned int clock; + unsigned long clock; int saved_input_char; struct uart_sifive *regs; }; +/** + * Find minimum divisor divides in_freq to max_target_hz; + * Based on uart driver n SiFive FSBL. + * + * f_baud = f_in / (div + 1) => div = (f_in / f_baud) - 1 + * The nearest integer solution requires rounding up as to not exceed + * max_target_hz. + * div = ceil(f_in / f_baud) - 1 + * = floor((f_in - 1 + f_baud) / f_baud) - 1 + * This should not overflow as long as (f_in - 1 + f_baud) does not exceed + * 2^32 - 1, which is unlikely since we represent frequencies in kHz. + */ +static inline unsigned int uart_min_clk_divisor(unsigned long in_freq, + unsigned long max_target_hz) +{ + unsigned long quotient = + (in_freq + max_target_hz - 1) / (max_target_hz); + /* Avoid underflow */ + if (quotient == 0) + return 0; + else + return quotient - 1; +} + /* Set up the baud rate in gd struct */ static void _sifive_serial_setbrg(struct uart_sifive *regs, unsigned long clock, unsigned long baud) { - writel((u32)((clock / baud) - 1), ®s->div); + writel((uart_min_clk_divisor(clock, baud)), ®s->div); } static void _sifive_serial_init(struct uart_sifive *regs)