From nobody Mon May 11 01:32:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3F9FC433F5 for ; Tue, 19 Apr 2022 08:54:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350192AbiDSI45 (ORCPT ); Tue, 19 Apr 2022 04:56:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350181AbiDSI4z (ORCPT ); Tue, 19 Apr 2022 04:56:55 -0400 Received: from mx07-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D86DE22BC1; Tue, 19 Apr 2022 01:54:12 -0700 (PDT) Received: from pps.filterd (m0046660.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23J7mPMJ031599; Tue, 19 Apr 2022 10:53:50 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=selector1; bh=2TdqCQA3HZJgjbQgS4ng8USah1H+t/xStkf042B0x94=; b=kZcqNamzq6IR++gU1x2mp3vOcRGEv6Dz1jLwtnFhO6IauwBm3SwU/PZo2wIbvHSrbo8V DSmquKRilSLdtoieiXx0hrVSdh/GkhuyO//S/v1tsamcY2H4K0vBL4V/XE7vQDVDqIuy FUaHe3aIzuaBBEXoJmwg2VgHOoZtMoPBWB1yg4Ti8ZWeuD+YvpHJjfSK7TE90UxmfNkP FGHJwEMb6ALx3trwuGvI26fFMh04SAYa7QTdH6vxWadySrpmuRBrDOlgR65t3L5vSmo0 CSfbLv3pXZiDocm91oxyp6hCTYrtwpm5+euj+tcohti6Lj++28RDxOxq/ZXqlY9eaVNh Sw== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3fh09refje-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 19 Apr 2022 10:53:50 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 43AD8100034; Tue, 19 Apr 2022 10:53:47 +0200 (CEST) Received: from Webmail-eu.st.com (sfhdag2node2.st.com [10.75.127.5]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 3C1992053D5; Tue, 19 Apr 2022 10:53:47 +0200 (CEST) Received: from localhost (10.75.127.48) by SFHDAG2NODE2.st.com (10.75.127.5) with Microsoft SMTP Server (TLS) id 15.0.1497.26; Tue, 19 Apr 2022 10:53:46 +0200 From: Valentin Caron To: Greg Kroah-Hartman CC: Geert Uytterhoeven , Jiri Slaby , Maxime Coquelin , Alexandre Torgue , Valentin Caron , , , , Subject: [PATCH V3 1/3] serial: stm32: remove infinite loop possibility in putchar function Date: Tue, 19 Apr 2022 10:53:28 +0200 Message-ID: <20220419085330.1178925-2-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220419085330.1178925-1-valentin.caron@foss.st.com> References: <20220419085330.1178925-1-valentin.caron@foss.st.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.75.127.48] X-ClientProxiedBy: SFHDAG2NODE1.st.com (10.75.127.4) To SFHDAG2NODE2.st.com (10.75.127.5) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-04-19_03,2022-04-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Rework stm32_usart_console_putchar() function in order to anticipate the case where the character can never be sent. Signed-off-by: Valentin Caron --- drivers/tty/serial/stm32-usart.c | 12 +++++++++--- drivers/tty/serial/stm32-usart.h | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-us= art.c index f886976daef6..9910a18779af 100644 --- a/drivers/tty/serial/stm32-usart.c +++ b/drivers/tty/serial/stm32-usart.c @@ -1640,10 +1640,16 @@ static void stm32_usart_console_putchar(struct uart= _port *port, unsigned char ch { struct stm32_port *stm32_port =3D to_stm32_port(port); const struct stm32_usart_offsets *ofs =3D &stm32_port->info->ofs; + u32 isr; + int ret; =20 - while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) - cpu_relax(); - + ret =3D readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr, + (isr & USART_SR_TXE), 100, + STM32_USART_TIMEOUT_USEC); + if (ret !=3D 0) { + dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret); + return; + } writel_relaxed(ch, port->membase + ofs->tdr); } =20 diff --git a/drivers/tty/serial/stm32-usart.h b/drivers/tty/serial/stm32-us= art.h index feab952aec16..d734c4a5fd24 100644 --- a/drivers/tty/serial/stm32-usart.h +++ b/drivers/tty/serial/stm32-usart.h @@ -251,6 +251,8 @@ struct stm32_usart_info stm32h7_info =3D { #define RX_BUF_P (RX_BUF_L / 2) /* dma rx buffer period */ #define TX_BUF_L RX_BUF_L /* dma tx buffer length */ =20 +#define STM32_USART_TIMEOUT_USEC USEC_PER_SEC /* 1s timeout in =C2=B5s */ + struct stm32_port { struct uart_port port; struct clk *clk; --=20 2.25.1 From nobody Mon May 11 01:32:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7AC7BC433F5 for ; Tue, 19 Apr 2022 08:54:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350214AbiDSI5F (ORCPT ); Tue, 19 Apr 2022 04:57:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345296AbiDSI44 (ORCPT ); Tue, 19 Apr 2022 04:56:56 -0400 Received: from mx07-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70E3B22B33; Tue, 19 Apr 2022 01:54:14 -0700 (PDT) Received: from pps.filterd (m0046661.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23J7rmWg019632; Tue, 19 Apr 2022 10:53:50 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=Ek6ROpQJskaYH424nnCvQ5wW6eKZ/b58EoHa1wLhBcw=; b=Oq3lVeA87za3QRRKxj1OBsXCZyG5mc8nmVQFleUwYH4qmy7o64O8ARoLOdIvGfwkxFSh 4DGQP0hgQbdAA6baKe70l96FXG1ItgSWsFPCdj5wYXLVEXx6g8X5Q1aMAnrAtoZYhGno OHfYpSNFFoi5i5ApZ96WLtonrXWpEs4e1OMSXMpEEXJVPPu5tnj43cS7SO4imYQGugtO luBuWytvFCCNpo01NoCfmilVDV2EC3pAMCF0t4upHU5x1Kr5aVp6mdOo5dtUpPz3dhEz SKnI2/pZqPBId5OAjUYL5VUMKevjfS0rHIwdQcPV+M6Cn5wn9d7oc9eAXXgYzuOg5kBr dg== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3fh09kpfpw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 19 Apr 2022 10:53:50 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id CB6DC100038; Tue, 19 Apr 2022 10:53:47 +0200 (CEST) Received: from Webmail-eu.st.com (sfhdag2node2.st.com [10.75.127.5]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id C21C62053D6; Tue, 19 Apr 2022 10:53:47 +0200 (CEST) Received: from localhost (10.75.127.51) by SFHDAG2NODE2.st.com (10.75.127.5) with Microsoft SMTP Server (TLS) id 15.0.1497.26; Tue, 19 Apr 2022 10:53:47 +0200 From: Valentin Caron To: Greg Kroah-Hartman CC: Geert Uytterhoeven , Jiri Slaby , Maxime Coquelin , Alexandre Torgue , Valentin Caron , , , , Subject: [PATCH V3 2/3] serial: stm32: add KGDB support Date: Tue, 19 Apr 2022 10:53:29 +0200 Message-ID: <20220419085330.1178925-3-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220419085330.1178925-1-valentin.caron@foss.st.com> References: <20220419085330.1178925-1-valentin.caron@foss.st.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.75.127.51] X-ClientProxiedBy: SFHDAG2NODE1.st.com (10.75.127.4) To SFHDAG2NODE2.st.com (10.75.127.5) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-04-19_03,2022-04-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add support for KGDB in stm32 serial driver by implementing characters polling callbacks (poll_init, poll_get_char and poll_put_char). Signed-off-by: Erwan Le Ray Signed-off-by: Jean Philippe Romain Signed-off-by: Valentin Caron --- drivers/tty/serial/stm32-usart.c | 37 ++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-us= art.c index 9910a18779af..8aefb286bd88 100644 --- a/drivers/tty/serial/stm32-usart.c +++ b/drivers/tty/serial/stm32-usart.c @@ -37,6 +37,7 @@ =20 static void stm32_usart_stop_tx(struct uart_port *port); static void stm32_usart_transmit_chars(struct uart_port *port); +static void __maybe_unused stm32_usart_console_putchar(struct uart_port *p= ort, unsigned char ch); =20 static inline struct stm32_port *to_stm32_port(struct uart_port *port) { @@ -1217,6 +1218,33 @@ static void stm32_usart_pm(struct uart_port *port, u= nsigned int state, } } =20 +#if defined(CONFIG_CONSOLE_POLL) + + /* Callbacks for characters polling in debug context (i.e. KGDB). */ +static int stm32_usart_poll_init(struct uart_port *port) +{ + struct stm32_port *stm32_port =3D to_stm32_port(port); + + return clk_prepare_enable(stm32_port->clk); +} + +static int stm32_usart_poll_get_char(struct uart_port *port) +{ + struct stm32_port *stm32_port =3D to_stm32_port(port); + const struct stm32_usart_offsets *ofs =3D &stm32_port->info->ofs; + + if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE)) + return NO_POLL_CHAR; + + return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask; +} + +static void stm32_usart_poll_put_char(struct uart_port *port, unsigned cha= r ch) +{ + stm32_usart_console_putchar(port, ch); +} +#endif /* CONFIG_CONSOLE_POLL */ + static const struct uart_ops stm32_uart_ops =3D { .tx_empty =3D stm32_usart_tx_empty, .set_mctrl =3D stm32_usart_set_mctrl, @@ -1238,6 +1266,11 @@ static const struct uart_ops stm32_uart_ops =3D { .request_port =3D stm32_usart_request_port, .config_port =3D stm32_usart_config_port, .verify_port =3D stm32_usart_verify_port, +#if defined(CONFIG_CONSOLE_POLL) + .poll_init =3D stm32_usart_poll_init, + .poll_get_char =3D stm32_usart_poll_get_char, + .poll_put_char =3D stm32_usart_poll_put_char, +#endif /* CONFIG_CONSOLE_POLL */ }; =20 /* @@ -1635,8 +1668,7 @@ static int stm32_usart_serial_remove(struct platform_= device *pdev) return 0; } =20 -#ifdef CONFIG_SERIAL_STM32_CONSOLE -static void stm32_usart_console_putchar(struct uart_port *port, unsigned c= har ch) +static void __maybe_unused stm32_usart_console_putchar(struct uart_port *p= ort, unsigned char ch) { struct stm32_port *stm32_port =3D to_stm32_port(port); const struct stm32_usart_offsets *ofs =3D &stm32_port->info->ofs; @@ -1653,6 +1685,7 @@ static void stm32_usart_console_putchar(struct uart_p= ort *port, unsigned char ch writel_relaxed(ch, port->membase + ofs->tdr); } =20 +#ifdef CONFIG_SERIAL_STM32_CONSOLE static void stm32_usart_console_write(struct console *co, const char *s, unsigned int cnt) { --=20 2.25.1 From nobody Mon May 11 01:32:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91691C433EF for ; Tue, 19 Apr 2022 08:54:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350221AbiDSI5I (ORCPT ); Tue, 19 Apr 2022 04:57:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350187AbiDSI44 (ORCPT ); Tue, 19 Apr 2022 04:56:56 -0400 Received: from mx07-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50E9422B19; Tue, 19 Apr 2022 01:54:14 -0700 (PDT) Received: from pps.filterd (m0046661.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23J7qMxT019642; Tue, 19 Apr 2022 10:53:50 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=PmNWK/G60m8uo7lwEsmyR6Xq46F+sObo1bwuEDT0XE8=; b=SxJeaShrIX/myy4DuDAbnnV6ji+0iFymXGHJxhAQ2JoZSELXBHCdM54eGtGKBtvFmhfb J3gxQESWWzMDKrqJ9W92ohgucGCQhV6h+yt2ZM0yihqLGx6zRG3k2pfiJ9qrlt0z10TB bkQBjKBIVm5T8af4SQfz4kvivI3bHcP3pbNTageSo/OkTTextNYrToX/Zf6rivG1BC7m KEEPipPenmd5nrq0iMnTRn0KGlIzeWC2nhpRh1wTF8Ozf69UAowOeVij2Mn3po08feaA atqVnhQpPFEdG/droQ0FOc82rNwhFD2iO/EeGGW+RxBZ4Od1Y3uKORmFfkvtgVwNjfrp ug== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3fh09kpfpx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 19 Apr 2022 10:53:50 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 60EFE100039; Tue, 19 Apr 2022 10:53:48 +0200 (CEST) Received: from Webmail-eu.st.com (sfhdag2node2.st.com [10.75.127.5]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 5B7CE2053D3; Tue, 19 Apr 2022 10:53:48 +0200 (CEST) Received: from localhost (10.75.127.51) by SFHDAG2NODE2.st.com (10.75.127.5) with Microsoft SMTP Server (TLS) id 15.0.1497.26; Tue, 19 Apr 2022 10:53:47 +0200 From: Valentin Caron To: Greg Kroah-Hartman CC: Geert Uytterhoeven , Jiri Slaby , Maxime Coquelin , Alexandre Torgue , Valentin Caron , , , , Subject: [PATCH V3 3/3] serial: stm32: add earlycon support Date: Tue, 19 Apr 2022 10:53:30 +0200 Message-ID: <20220419085330.1178925-4-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220419085330.1178925-1-valentin.caron@foss.st.com> References: <20220419085330.1178925-1-valentin.caron@foss.st.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.75.127.51] X-ClientProxiedBy: SFHDAG2NODE3.st.com (10.75.127.6) To SFHDAG2NODE2.st.com (10.75.127.5) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-04-19_03,2022-04-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add early console support in stm32 uart driver. Signed-off-by: Alexandre Torgue Signed-off-by: Valentin Caron --- drivers/tty/serial/Kconfig | 1 + drivers/tty/serial/stm32-usart.c | 51 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig index 6949e883ffab..ed59de8d2e11 100644 --- a/drivers/tty/serial/Kconfig +++ b/drivers/tty/serial/Kconfig @@ -1443,6 +1443,7 @@ config SERIAL_STM32_CONSOLE bool "Support for console on STM32" depends on SERIAL_STM32=3Dy select SERIAL_CORE_CONSOLE + select SERIAL_EARLYCON =20 config SERIAL_MVEBU_UART bool "Marvell EBU serial port support" diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-us= art.c index 8aefb286bd88..f75691e72729 100644 --- a/drivers/tty/serial/stm32-usart.c +++ b/drivers/tty/serial/stm32-usart.c @@ -1761,6 +1761,57 @@ static struct console stm32_console =3D { #define STM32_SERIAL_CONSOLE NULL #endif /* CONFIG_SERIAL_STM32_CONSOLE */ =20 +#ifdef CONFIG_SERIAL_EARLYCON +static void early_stm32_usart_console_putchar(struct uart_port *port, unsi= gned char ch) +{ + struct stm32_usart_info *info =3D port->private_data; + + while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE)) + cpu_relax(); + + writel_relaxed(ch, port->membase + info->ofs.tdr); +} + +static void early_stm32_serial_write(struct console *console, const char *= s, unsigned int count) +{ + struct earlycon_device *device =3D console->data; + struct uart_port *port =3D &device->port; + + uart_console_write(port, s, count, early_stm32_usart_console_putchar); +} + +static int __init early_stm32_h7_serial_setup(struct earlycon_device *devi= ce, const char *options) +{ + if (!(device->port.membase || device->port.iobase)) + return -ENODEV; + device->port.private_data =3D &stm32h7_info; + device->con->write =3D early_stm32_serial_write; + return 0; +} + +static int __init early_stm32_f7_serial_setup(struct earlycon_device *devi= ce, const char *options) +{ + if (!(device->port.membase || device->port.iobase)) + return -ENODEV; + device->port.private_data =3D &stm32f7_info; + device->con->write =3D early_stm32_serial_write; + return 0; +} + +static int __init early_stm32_f4_serial_setup(struct earlycon_device *devi= ce, const char *options) +{ + if (!(device->port.membase || device->port.iobase)) + return -ENODEV; + device->port.private_data =3D &stm32f4_info; + device->con->write =3D early_stm32_serial_write; + return 0; +} + +OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup); +OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup); +OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup); +#endif /* CONFIG_SERIAL_EARLYCON */ + static struct uart_driver stm32_usart_driver =3D { .driver_name =3D DRIVER_NAME, .dev_name =3D STM32_SERIAL_NAME, --=20 2.25.1