]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
board: nuvoton: update console environment variable
authorJim Liu <jim.t90615@gmail.com>
Tue, 14 Nov 2023 08:51:59 +0000 (16:51 +0800)
committerTom Rini <trini@konsulko.com>
Thu, 23 Nov 2023 00:11:27 +0000 (19:11 -0500)
If CONFIG_SYS_SKIP_UART_INIT is enabled, calculate the
current baud rate and update the "console" environment
variable.

Signed-off-by: Jim Liu <JJLIU0@nuvoton.com>
board/nuvoton/arbel_evb/Kconfig
board/nuvoton/arbel_evb/arbel_evb.c
board/nuvoton/common/Kconfig [new file with mode: 0644]
board/nuvoton/common/Makefile [new file with mode: 0644]
board/nuvoton/common/uart.c [new file with mode: 0644]
board/nuvoton/common/uart.h [new file with mode: 0644]
board/nuvoton/poleg_evb/Kconfig
board/nuvoton/poleg_evb/poleg_evb.c

index 33c589f1fb3f7d4d2203a135da602de555122342..ed1c1ad8ee695322636c45cf085633f04507fda6 100644 (file)
@@ -15,4 +15,5 @@ config SYS_MEM_TOP_HIDE
        help
          Reserve memory for ECC/GFX/OPTEE/TIP/CP.
 
+source "board/nuvoton/common/Kconfig"
 endif
index 59e1a4256462d382324473c84b76acb9f40353b3..8fc56c18397689dc140fe109ab5ee6bf75adce84 100644 (file)
@@ -7,6 +7,7 @@
 #include <dm.h>
 #include <asm/io.h>
 #include <asm/arch/gcr.h>
+#include "../common/uart.h"
 
 #define SR_MII_CTRL_SWR_BIT15  15
 
@@ -90,3 +91,9 @@ int dram_init_banksize(void)
        return 0;
 }
 
+int last_stage_init(void)
+{
+       board_set_console();
+
+       return 0;
+}
diff --git a/board/nuvoton/common/Kconfig b/board/nuvoton/common/Kconfig
new file mode 100644 (file)
index 0000000..61de7bc
--- /dev/null
@@ -0,0 +1,9 @@
+if ARCH_NPCM
+
+config SYS_SKIP_UART_INIT
+       bool "Skip UART initialization"
+       depends on NPCM_SERIAL
+       help
+         Select this if the UART you want to use is already
+         initialized by the time U-Boot starts its execution.
+endif
diff --git a/board/nuvoton/common/Makefile b/board/nuvoton/common/Makefile
new file mode 100644 (file)
index 0000000..8fd83b2
--- /dev/null
@@ -0,0 +1 @@
+obj-$(CONFIG_SYS_SKIP_UART_INIT)       += uart.o
diff --git a/board/nuvoton/common/uart.c b/board/nuvoton/common/uart.c
new file mode 100644 (file)
index 0000000..b35c795
--- /dev/null
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2023 Nuvoton Technology Corp.
+ */
+
+#include <clk.h>
+#include <dm.h>
+#include <env.h>
+#include <serial.h>
+#include <linux/delay.h>
+
+#define UART_DLL       0x0
+#define UART_DLM       0x4
+#define UART_LCR       0xc
+#define LCR_DLAB       BIT(7)
+
+void board_set_console(void)
+{
+       const unsigned long baudrate_table[] = CFG_SYS_BAUDRATE_TABLE;
+       struct udevice *dev = gd->cur_serial_dev;
+       unsigned int baudrate, max_delta;
+       void __iomem *uart_reg;
+       struct clk clk;
+       char string[32];
+       u32 uart_clk;
+       u8 dll, dlm;
+       u16 divisor;
+       int ret, i;
+
+       if (!dev)
+               return;
+
+       uart_reg = dev_read_addr_ptr(dev);
+       ret = clk_get_by_index(dev, 0, &clk);
+       if (ret)
+               return;
+
+       uart_clk = clk_get_rate(&clk);
+       setbits_8(uart_reg + UART_LCR, LCR_DLAB);
+       dll = readb(uart_reg + UART_DLL);
+       dlm = readb(uart_reg + UART_DLM);
+       clrbits_8(uart_reg + UART_LCR, LCR_DLAB);
+       divisor = dll | (dlm << 8);
+       baudrate =  uart_clk / ((16 * (divisor + 2)));
+       for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
+               max_delta = baudrate_table[i] / 20;
+               if (abs(baudrate - baudrate_table[i]) < max_delta) {
+                       /* The baudrate is supported */
+                       gd->baudrate = baudrate_table[i];
+                       break;
+               }
+       }
+
+       if (i == ARRAY_SIZE(baudrate_table)) {
+               /* current baudrate is not suitable, set to default */
+               divisor = DIV_ROUND_CLOSEST(uart_clk, 16 * gd->baudrate) - 2;
+               setbits_8(uart_reg + UART_LCR, LCR_DLAB);
+               writeb(divisor & 0xff, uart_reg + UART_DLL);
+               writeb(divisor >> 8, uart_reg + UART_DLM);
+               clrbits_8(uart_reg + UART_LCR, LCR_DLAB);
+               udelay(100);
+               printf("\r\nUART(source %u): change baudrate from %u to %u\n",
+                      uart_clk, baudrate, uart_clk / ((16 * (divisor + 2))));
+       }
+
+       debug("Set env baudrate=%u\n", gd->baudrate);
+       snprintf(string, sizeof(string), "ttyS0,%un8", gd->baudrate);
+       env_set("console", string);
+
+}
diff --git a/board/nuvoton/common/uart.h b/board/nuvoton/common/uart.h
new file mode 100644 (file)
index 0000000..9cc8952
--- /dev/null
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2023 Nuvoton Technology Corp.
+ */
+
+#ifndef _NUVOTON_UART_H
+#define _NUVOTON_UART_H
+
+void board_set_console(void);
+
+#endif /* _NUVOTON_COMMON_H */
index d3f4c1dd8128ad19abfb72786bbfe6d93aa9705c..6f7f1ef1578e832b33cc5e739acff9837854bf3e 100644 (file)
@@ -22,4 +22,5 @@ config TARGET_POLEG_EVB
 
 endchoice
 
+source "board/nuvoton/common/Kconfig"
 endif
index 2052af6649a52c0d8924cc55cd16fc8cad7cbcbf..7421911a416beb13d6a96cf8c9d2bf8c73325b68 100644 (file)
@@ -10,6 +10,7 @@
 #include <asm/io.h>
 #include <asm/arch/gcr.h>
 #include <asm/mach-types.h>
+#include "../common/uart.h"
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -53,3 +54,10 @@ int dram_init(void)
 
        return 0;
 }
+
+int last_stage_init(void)
+{
+       board_set_console();
+
+       return 0;
+}