2022-04-08 14:12:37

by Valentin Caron

[permalink] [raw]
Subject: [PATCH V2 3/3] serial: stm32: add earlycon support

Add early console support in stm32 uart driver.

Signed-off-by: Alexandre Torgue <[email protected]>
Signed-off-by: Valentin Caron <[email protected]>
---
.../admin-guide/kernel-parameters.txt | 6 +++
drivers/tty/serial/Kconfig | 1 +
drivers/tty/serial/stm32-usart.c | 51 +++++++++++++++++++
3 files changed, 58 insertions(+)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 3f1cc5e317ed..e941c3351c7a 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1264,6 +1264,12 @@
address must be provided, and the serial port must
already be setup and configured.

+ stm32,<addr>
+ Use early console provided by ST Microelectronics
+ serial driver for STM32 SoCs. A valid base address
+ must be provided, and the serial port must already
+ be setup and configured.
+
earlyprintk= [X86,SH,ARM,M68k,S390]
earlyprintk=vga
earlyprintk=sclp
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=y
select SERIAL_CORE_CONSOLE
+ select SERIAL_EARLYCON

config SERIAL_MVEBU_UART
bool "Marvell EBU serial port support"
diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-usart.c
index 4307c822afe4..65988e6efaa2 100644
--- a/drivers/tty/serial/stm32-usart.c
+++ b/drivers/tty/serial/stm32-usart.c
@@ -1766,6 +1766,57 @@ static struct console stm32_console = {
#define STM32_SERIAL_CONSOLE NULL
#endif /* CONFIG_SERIAL_STM32_CONSOLE */

+#ifdef CONFIG_SERIAL_EARLYCON
+static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
+{
+ struct stm32_usart_info *info = 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 = console->data;
+ struct uart_port *port = &device->port;
+
+ uart_console_write(port, s, count, early_stm32_usart_console_putchar);
+}
+
+static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options)
+{
+ if (!(device->port.membase || device->port.iobase))
+ return -ENODEV;
+ device->port.private_data = &stm32h7_info;
+ device->con->write = early_stm32_serial_write;
+ return 0;
+}
+
+static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options)
+{
+ if (!(device->port.membase || device->port.iobase))
+ return -ENODEV;
+ device->port.private_data = &stm32f7_info;
+ device->con->write = early_stm32_serial_write;
+ return 0;
+}
+
+static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options)
+{
+ if (!(device->port.membase || device->port.iobase))
+ return -ENODEV;
+ device->port.private_data = &stm32f4_info;
+ device->con->write = 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 = {
.driver_name = DRIVER_NAME,
.dev_name = STM32_SERIAL_NAME,
--
2.25.1


2022-04-12 23:55:22

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH V2 3/3] serial: stm32: add earlycon support

Hi Valentin,

On Fri, Apr 8, 2022 at 3:14 PM Valentin Caron
<[email protected]> wrote:
> Add early console support in stm32 uart driver.
>
> Signed-off-by: Alexandre Torgue <[email protected]>
> Signed-off-by: Valentin Caron <[email protected]>

Thanks for your patch!

> --- a/Documentation/admin-guide/kernel-parameters.txt
> +++ b/Documentation/admin-guide/kernel-parameters.txt
> @@ -1264,6 +1264,12 @@
> address must be provided, and the serial port must
> already be setup and configured.
>
> + stm32,<addr>
> + Use early console provided by ST Microelectronics
> + serial driver for STM32 SoCs. A valid base address
> + must be provided, and the serial port must already
> + be setup and configured.

Why do you need this parameter?

Given this driver uses DT, can't it figure out the serial port address
from chosen/stdout-path?

+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);

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

2022-04-16 00:55:06

by Valentin Caron

[permalink] [raw]
Subject: Re: [PATCH V2 3/3] serial: stm32: add earlycon support

On 4/11/22 16:59, Geert Uytterhoeven wrote:
> Hi Valentin,
>
> On Fri, Apr 8, 2022 at 3:14 PM Valentin Caron
> <[email protected]> wrote:
>> Add early console support in stm32 uart driver.
>>
>> Signed-off-by: Alexandre Torgue <[email protected]>
>> Signed-off-by: Valentin Caron <[email protected]>
> Thanks for your patch!
>
>> --- a/Documentation/admin-guide/kernel-parameters.txt
>> +++ b/Documentation/admin-guide/kernel-parameters.txt
>> @@ -1264,6 +1264,12 @@
>> address must be provided, and the serial port must
>> already be setup and configured.
>>
>> + stm32,<addr>
>> + Use early console provided by ST Microelectronics
>> + serial driver for STM32 SoCs. A valid base address
>> + must be provided, and the serial port must already
>> + be setup and configured.
> Why do you need this parameter?
>
> Given this driver uses DT, can't it figure out the serial port address
> from chosen/stdout-path?
>
> +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);
>
> Gr{oetje,eeting}s,
>
> Geert
>
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
>
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
> -- Linus Torvalds

Hi Geert,

I took the example of other serial drivers.
Both methods work (with earlycon/stdout-path and with
earlycon=stm32,0xXXXXXXX)
but your right, the second will probably never used on this driver.

Should I remove it ?

Thanks,
Valentin

2022-04-16 02:16:35

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH V2 3/3] serial: stm32: add earlycon support

Hi Valentin,

On Thu, Apr 14, 2022 at 2:17 PM Valentin CARON
<[email protected]> wrote:
> On 4/11/22 16:59, Geert Uytterhoeven wrote:
> > On Fri, Apr 8, 2022 at 3:14 PM Valentin Caron
> > <[email protected]> wrote:
> >> Add early console support in stm32 uart driver.
> >>
> >> Signed-off-by: Alexandre Torgue <[email protected]>
> >> Signed-off-by: Valentin Caron <[email protected]>
> > Thanks for your patch!
> >
> >> --- a/Documentation/admin-guide/kernel-parameters.txt
> >> +++ b/Documentation/admin-guide/kernel-parameters.txt
> >> @@ -1264,6 +1264,12 @@
> >> address must be provided, and the serial port must
> >> already be setup and configured.
> >>
> >> + stm32,<addr>
> >> + Use early console provided by ST Microelectronics
> >> + serial driver for STM32 SoCs. A valid base address
> >> + must be provided, and the serial port must already
> >> + be setup and configured.
> > Why do you need this parameter?
> >
> > Given this driver uses DT, can't it figure out the serial port address
> > from chosen/stdout-path?
>
> I took the example of other serial drivers.
> Both methods work (with earlycon/stdout-path and with
> earlycon=stm32,0xXXXXXXX)
> but your right, the second will probably never used on this driver.
>
> Should I remove it ?

I think you should. The less platform-specific kernel parameters,
the better. And the less bad examples to copy from.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds