From patchwork Mon Jul 18 17:20:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 1657583 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=oV+bLfd7; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=nUwjVNSC; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Lmpdb19KGz9sFw for ; Tue, 19 Jul 2022 03:20:51 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ergjE1DhTmwOvAtxWXJO6IgqxED2qzttGmM0s0o9vPs=; b=oV+bLfd731CZCu TGQbvFhtINdunR7POeSSxMyDaKY2gsGWXxfPBR9Vz+tKF0ISIkIgyr63wj2iKUil0ZIjWxXvMI2xK LzfJNYW+4HW8PU2VkHn0v4L/MLtWhuX5hcC5a6vQM371Pr1T9y43IJHVVgNvXRVdvf3x4/3FjHvL7 ifw0OivCU2+dmfiQ8vSkzELveOWCJRqZr2EneAs04rcZkgcWwXvhd0elSFkfOnQSf08L6ZUvTA/3u jg3wYejajPKqXmywk0Z+vy6K651NUZ9AFsO/OXBJBlOdY5LlJt9Kj88DcWrOjEPd/M+WOwnonYDqb tYj9X+ifjuCZEsO9tMIw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oDUQ9-00H79R-VE; Mon, 18 Jul 2022 17:20:37 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oDUQ7-00H74I-RA for opensbi@lists.infradead.org; Mon, 18 Jul 2022 17:20:37 +0000 Received: by mail-ed1-x52f.google.com with SMTP id m13so6087376edc.5 for ; Mon, 18 Jul 2022 10:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SCenqIe+X0qFq6vri6yMmvoVN+BVjkKEImVR6UIDhq0=; b=nUwjVNSCY6YwtONrzk7tpI1jZaIPRMlFALSMlIYMHfBCw+FuLRSSXdo8ZtFYCXM1O0 OV7pRABW5fyARsXRWMXIaFSJ+ogx5eGrDfgZVA2RLCxx55+ZsQDVWfYCuyO5Fe4/vG4V NEP12sNi/uq6AOgHgIfSOANr70Wmn24Qh1MKV7Ew9Fj7F7eANcxomTpeHWjVuSl4nUQa 1ec0mNK9KkGQHEAuu4jqSMo4THphz7etzQtBdUjQ0KRoVM74cSW0SCAajJQWZPkDIQcl ZaKZq4K3STpfrA7IhWRfRHr2672ToY+hxA+5SVUteXrhq1fMWIf7PD8rx5N/hTxOa4ZT QeEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SCenqIe+X0qFq6vri6yMmvoVN+BVjkKEImVR6UIDhq0=; b=prl9VOoJE6mCGCPnZk1jlEgY0TYsuNvuoa8tEvxRt2UCgIoilFMoN/hBR4bGrAEIAr auvfAxAFhBHu1Q6wJUiWQPyxgKpOyq1bugwPC0hNpvcMlqJ0F9w6/mTRmMHkXk81yIEH yVBbYSBbiugihDPJWChcaphcsxkMVrcr+tohDhHlW5SzMUN5i0xSI0jbOWq6LS9TtWLw Ccz18GDLglPoXlCoajJduZeLNshuH6iVpZRfqQToj0f+OQhicHGypVZVdEZQyzh1eLLx 3brz61P29kNiSS5QDlIpA5pwS/zRhaUZwsitRRdWdPvg/vwFE11xnhFs744s9rXMaLJ0 g7iA== X-Gm-Message-State: AJIora8/vwyHRG6JCf83hDhwRFItdU56kGM8GbBU8UiCNRePJQ73xDJq aC1QK7Cn2W9yHUGimZU0tutAOFEMfAK//Q== X-Google-Smtp-Source: AGRyM1tfed8MjBxs29yIxht59R1wZ1eTIsv/LI+9iiaxtEVy0+/VbZ3vteKTXGXXsd151C7zUdgrbw== X-Received: by 2002:a05:6402:34ce:b0:43a:a4bb:27a6 with SMTP id w14-20020a05640234ce00b0043aa4bb27a6mr38097620edc.158.1658164835019; Mon, 18 Jul 2022 10:20:35 -0700 (PDT) Received: from localhost (cst2-173-67.cust.vodafone.cz. [31.30.173.67]) by smtp.gmail.com with ESMTPSA id d25-20020aa7c1d9000000b0043a75f62155sm8961866edp.86.2022.07.18.10.20.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 10:20:34 -0700 (PDT) From: Andrew Jones To: opensbi@lists.infradead.org Cc: Atish Patra , Alistair Francis , Anup Patel , junliang.tan@linux.starfivetech.com Subject: [PATCH 4/4] lib: utils/serial: Ensure baudrate is non-zero before using Date: Mon, 18 Jul 2022 19:20:28 +0200 Message-Id: <20220718172028.2006166-5-ajones@ventanamicro.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220718172028.2006166-1-ajones@ventanamicro.com> References: <20220718172028.2006166-1-ajones@ventanamicro.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220718_102035_919990_DBCB46C3 X-CRM114-Status: GOOD ( 13.40 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: RISC-V doesn't generate exceptions on divide-by-zero, but the result, all bits set, is not likely what people expect either. In all cases where we divide by baudrate there's a chance it's zero (when t [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:52f listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org RISC-V doesn't generate exceptions on divide-by-zero, but the result, all bits set, is not likely what people expect either. In all cases where we divide by baudrate there's a chance it's zero (when the DT it came from is "bad"). To avoid difficult to debug situations, leave baudrate dependent registers alone when baudrate is zero, as, also in all cases, it appears we can skip initialization of those registers and still [hopefully] have a functioning UART. Signed-off-by: Andrew Jones Reviewed-by: Anup Patel --- lib/utils/serial/gaisler-uart.c | 2 +- lib/utils/serial/shakti-uart.c | 8 ++++++-- lib/utils/serial/sifive-uart.c | 2 +- lib/utils/serial/uart8250.c | 7 +++++-- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/utils/serial/gaisler-uart.c b/lib/utils/serial/gaisler-uart.c index 5f30ee43b719..eec75cca0c98 100644 --- a/lib/utils/serial/gaisler-uart.c +++ b/lib/utils/serial/gaisler-uart.c @@ -70,7 +70,7 @@ int gaisler_uart_init(unsigned long base, u32 in_freq, u32 baudrate) uart_base = (volatile char *)base; /* Configure baudrate */ - if (in_freq) + if (in_freq && baudrate) set_reg(UART_REG_SCALER, in_freq / (baudrate * 8 + 7)); ctrl = get_reg(UART_REG_CTRL); diff --git a/lib/utils/serial/shakti-uart.c b/lib/utils/serial/shakti-uart.c index 5f2fe75c9f33..2f09f141b9cf 100644 --- a/lib/utils/serial/shakti-uart.c +++ b/lib/utils/serial/shakti-uart.c @@ -47,8 +47,12 @@ static struct sbi_console_device shakti_console = { int shakti_uart_init(unsigned long base, u32 in_freq, u32 baudrate) { uart_base = (volatile char *)base; - u16 baud = (u16)(in_freq/(16 * baudrate)); - writew(baud, uart_base + REG_BAUD); + u16 baud; + + if (baudrate) { + baud = (u16)(in_freq / (16 * baudrate)); + writew(baud, uart_base + REG_BAUD); + } sbi_console_set_device(&shakti_console); diff --git a/lib/utils/serial/sifive-uart.c b/lib/utils/serial/sifive-uart.c index 7078611a5274..3581d47a6d4b 100644 --- a/lib/utils/serial/sifive-uart.c +++ b/lib/utils/serial/sifive-uart.c @@ -97,7 +97,7 @@ int sifive_uart_init(unsigned long base, u32 in_freq, u32 baudrate) uart_baudrate = baudrate; /* Configure baudrate */ - if (in_freq) + if (in_freq && baudrate) set_reg(UART_REG_DIV, uart_min_clk_divisor(in_freq, baudrate)); /* Disable interrupts */ diff --git a/lib/utils/serial/uart8250.c b/lib/utils/serial/uart8250.c index 38ea11af4d31..99bf1bf733f2 100644 --- a/lib/utils/serial/uart8250.c +++ b/lib/utils/serial/uart8250.c @@ -93,7 +93,7 @@ static struct sbi_console_device uart8250_console = { int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, u32 reg_width, u32 reg_offset) { - u16 bdiv; + u16 bdiv = 0; uart8250_base = (volatile char *)base + reg_offset; uart8250_reg_shift = reg_shift; @@ -101,7 +101,10 @@ int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, uart8250_in_freq = in_freq; uart8250_baudrate = baudrate; - bdiv = (uart8250_in_freq + 8 * uart8250_baudrate) / (16 * uart8250_baudrate); + if (uart8250_baudrate) { + bdiv = (uart8250_in_freq + 8 * uart8250_baudrate) / + (16 * uart8250_baudrate); + } /* Disable all interrupts */ set_reg(UART_IER_OFFSET, 0x00);