From 756aea59d99741416e83f788d9788194c13422d2 Mon Sep 17 00:00:00 2001 From: Jerome Forissier Date: Fri, 17 Feb 2017 08:38:22 +0100 Subject: core: add common implementation for console_putc() and console_flush() Since most platforms now use the same console_putc() and console_flush(), move them to core/kernel/console.c. Make them __weak so that platforms may still provide their own. The common code expects the platforms to initialize whatever serial device from console_init() and call register_console(). Signed-off-by: Jerome Forissier Reviewed-by: Jens Wiklander Reviewed-by: Etienne Carriere --- core/arch/arm/plat-d02/main.c | 17 +----------- core/arch/arm/plat-hikey/main.c | 17 +----------- core/arch/arm/plat-imx/main.c | 17 +----------- core/arch/arm/plat-ls/main.c | 17 +----------- core/arch/arm/plat-mediatek/main.c | 17 +----------- core/arch/arm/plat-rcar/main.c | 18 +----------- core/arch/arm/plat-rpi3/main.c | 18 ++---------- core/arch/arm/plat-sprd/console.c | 7 +---- core/arch/arm/plat-sunxi/console.c | 15 +--------- core/arch/arm/plat-ti/console.c | 17 +----------- core/arch/arm/plat-vexpress/main.c | 17 +----------- core/arch/arm/plat-zynq7k/main.c | 17 +----------- core/arch/arm/plat-zynqmp/main.c | 17 +----------- core/include/console.h | 3 ++ core/kernel/console.c | 56 ++++++++++++++++++++++++++++++++++++++ core/kernel/sub.mk | 1 + 16 files changed, 74 insertions(+), 197 deletions(-) create mode 100644 core/kernel/console.c (limited to 'core') diff --git a/core/arch/arm/plat-d02/main.c b/core/arch/arm/plat-d02/main.c index ec1d574e..2cd339b5 100644 --- a/core/arch/arm/plat-d02/main.c +++ b/core/arch/arm/plat-d02/main.c @@ -69,20 +69,5 @@ void console_init(void) { hi16xx_uart_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } diff --git a/core/arch/arm/plat-hikey/main.c b/core/arch/arm/plat-hikey/main.c index 0149e1b5..8d72f388 100644 --- a/core/arch/arm/plat-hikey/main.c +++ b/core/arch/arm/plat-hikey/main.c @@ -84,22 +84,7 @@ void console_init(void) { pl011_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } vaddr_t nsec_periph_base(paddr_t pa) diff --git a/core/arch/arm/plat-imx/main.c b/core/arch/arm/plat-imx/main.c index 816f4e8a..36dc11b4 100644 --- a/core/arch/arm/plat-imx/main.c +++ b/core/arch/arm/plat-imx/main.c @@ -131,22 +131,7 @@ void plat_cpu_reset_late(void) void console_init(void) { imx_uart_init(&console_data, CONSOLE_UART_BASE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } void main_init_gic(void) diff --git a/core/arch/arm/plat-ls/main.c b/core/arch/arm/plat-ls/main.c index fd0fd540..7f8d523d 100644 --- a/core/arch/arm/plat-ls/main.c +++ b/core/arch/arm/plat-ls/main.c @@ -124,22 +124,7 @@ void plat_cpu_reset_late(void) void console_init(void) { ns16550_init(&console_data, CONSOLE_UART_BASE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } void main_init_gic(void) diff --git a/core/arch/arm/plat-mediatek/main.c b/core/arch/arm/plat-mediatek/main.c index 2553ae3c..62180fcb 100644 --- a/core/arch/arm/plat-mediatek/main.c +++ b/core/arch/arm/plat-mediatek/main.c @@ -66,20 +66,5 @@ void console_init(void) { serial8250_uart_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } diff --git a/core/arch/arm/plat-rcar/main.c b/core/arch/arm/plat-rcar/main.c index f6f38283..6a7e3320 100644 --- a/core/arch/arm/plat-rcar/main.c +++ b/core/arch/arm/plat-rcar/main.c @@ -70,21 +70,5 @@ static void main_fiq(void) void console_init(void) { scif_uart_init(&console_data, CONSOLE_UART_BASE); + register_serial_console(&console_data.chip); } - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); -} - diff --git a/core/arch/arm/plat-rpi3/main.c b/core/arch/arm/plat-rpi3/main.c index ccfa9642..8a714cb7 100644 --- a/core/arch/arm/plat-rpi3/main.c +++ b/core/arch/arm/plat-rpi3/main.c @@ -60,24 +60,10 @@ const struct thread_handlers *generic_boot_get_handlers(void) { return &handlers; } + void console_init(void) { serial8250_uart_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } diff --git a/core/arch/arm/plat-sprd/console.c b/core/arch/arm/plat-sprd/console.c index 54771c74..ae56d956 100644 --- a/core/arch/arm/plat-sprd/console.c +++ b/core/arch/arm/plat-sprd/console.c @@ -34,6 +34,7 @@ static struct sprd_uart_data console_data __early_bss; void console_init(void) { sprd_uart_init(&console_data, CONSOLE_UART_BASE); + register_serial_console(&console_data.chip); } void console_putc(int ch) @@ -43,9 +44,3 @@ void console_putc(int ch) cons->ops->putc(cons, ch & 0xff); } -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); -} diff --git a/core/arch/arm/plat-sunxi/console.c b/core/arch/arm/plat-sunxi/console.c index 499fcd88..c22bcd14 100644 --- a/core/arch/arm/plat-sunxi/console.c +++ b/core/arch/arm/plat-sunxi/console.c @@ -35,18 +35,5 @@ static struct sunxi_uart_data console_data __early_bss; void console_init(void) { sunxi_uart_init(&console_data, CONSOLE_UART_BASE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } diff --git a/core/arch/arm/plat-ti/console.c b/core/arch/arm/plat-ti/console.c index 34f7a7fa..8f4461bf 100644 --- a/core/arch/arm/plat-ti/console.c +++ b/core/arch/arm/plat-ti/console.c @@ -40,20 +40,5 @@ void console_init(void) { serial8250_uart_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } diff --git a/core/arch/arm/plat-vexpress/main.c b/core/arch/arm/plat-vexpress/main.c index 962cf13b..44eef24f 100644 --- a/core/arch/arm/plat-vexpress/main.c +++ b/core/arch/arm/plat-vexpress/main.c @@ -121,22 +121,7 @@ void console_init(void) { pl011_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } #ifdef IT_CONSOLE_UART diff --git a/core/arch/arm/plat-zynq7k/main.c b/core/arch/arm/plat-zynq7k/main.c index 0b29948c..2991b948 100644 --- a/core/arch/arm/plat-zynq7k/main.c +++ b/core/arch/arm/plat-zynq7k/main.c @@ -121,22 +121,7 @@ void plat_cpu_reset_late(void) void console_init(void) { cdns_uart_init(&console_data, CONSOLE_UART_BASE, 0, 0); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } vaddr_t pl310_base(void) diff --git a/core/arch/arm/plat-zynqmp/main.c b/core/arch/arm/plat-zynqmp/main.c index 8ae3366a..a00d1ae8 100644 --- a/core/arch/arm/plat-zynqmp/main.c +++ b/core/arch/arm/plat-zynqmp/main.c @@ -95,20 +95,5 @@ void console_init(void) { cdns_uart_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE); -} - -void console_putc(int ch) -{ - struct serial_chip *cons = &console_data.chip; - - if (ch == '\n') - cons->ops->putc(cons, '\r'); - cons->ops->putc(cons, ch); -} - -void console_flush(void) -{ - struct serial_chip *cons = &console_data.chip; - - cons->ops->flush(cons); + register_serial_console(&console_data.chip); } diff --git a/core/include/console.h b/core/include/console.h index 0fe8e49d..d8df7f15 100644 --- a/core/include/console.h +++ b/core/include/console.h @@ -32,5 +32,8 @@ void console_init(void); void console_putc(int ch); void console_flush(void); +struct serial_chip; +void register_serial_console(struct serial_chip *chip); + #endif /* CONSOLE_H */ diff --git a/core/kernel/console.c b/core/kernel/console.c new file mode 100644 index 00000000..2fce361b --- /dev/null +++ b/core/kernel/console.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2017, Linaro Limited + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +static struct serial_chip *serial_console __early_bss; + +void __weak console_putc(int ch) +{ + if (!serial_console) + return; + + if (ch == '\n') + serial_console->ops->putc(serial_console, '\r'); + serial_console->ops->putc(serial_console, ch); +} + +void __weak console_flush(void) +{ + if (!serial_console) + return; + + serial_console->ops->flush(serial_console); +} + +void register_serial_console(struct serial_chip *chip) +{ + serial_console = chip; +} diff --git a/core/kernel/sub.mk b/core/kernel/sub.mk index aa00ae59..963e0782 100644 --- a/core/kernel/sub.mk +++ b/core/kernel/sub.mk @@ -1,4 +1,5 @@ srcs-y += assert.c +srcs-y += console.c srcs-y += tee_ta_manager.c srcs-y += tee_misc.c srcs-y += panic.c -- cgit v1.2.3