From 28010cf66b2ce602f45e7e2f7080fadee65ce9a3 Mon Sep 17 00:00:00 2001
From: liweihao <cn.liweihao@gmail.com>
Date: Wed, 18 Mar 2020 14:20:01 +0800
Subject: [PATCH 2/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0earlycon=E6=94=AF?=
 =?UTF-8?q?=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 arch/arm/boot/dts/foo-evk.dts   |  4 ++++
 arch/arm/configs/foo_defconfig  |  5 ++++-
 drivers/tty/serial/Kconfig      |  9 +++++++++
 drivers/tty/serial/Makefile     |  1 +
 drivers/tty/serial/foo-serial.c | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 drivers/tty/serial/foo-serial.c

diff --git a/arch/arm/boot/dts/foo-evk.dts b/arch/arm/boot/dts/foo-evk.dts
index 0069e0579..c79ab93b2 100644
--- a/arch/arm/boot/dts/foo-evk.dts
+++ b/arch/arm/boot/dts/foo-evk.dts
@@ -9,4 +9,8 @@
 	memory {
 		reg = <0x30000000 0x04000000>;
 	};
+
+	chosen {
+		bootargs = "earlycon=foo_serial,0x50000000 console=ttyS0";
+	};
 };
diff --git a/arch/arm/configs/foo_defconfig b/arch/arm/configs/foo_defconfig
index ef93820b2..a02ef6fcd 100644
--- a/arch/arm/configs/foo_defconfig
+++ b/arch/arm/configs/foo_defconfig
@@ -5,10 +5,12 @@ CONFIG_PREEMPT_VOLUNTARY=y
 CONFIG_IKCONFIG=y
 CONFIG_IKCONFIG_PROC=y
 CONFIG_CGROUPS=y
+CONFIG_NAMESPACES=y
 # CONFIG_UTS_NS is not set
 # CONFIG_IPC_NS is not set
 # CONFIG_PID_NS is not set
 CONFIG_BLK_DEV_INITRD=y
+CONFIG_EXPERT=y
 CONFIG_PERF_EVENTS=y
 # CONFIG_COMPAT_BRK is not set
 CONFIG_ARCH_MULTI_V4T=y
@@ -40,6 +42,7 @@ CONFIG_INPUT_EVDEV=y
 # CONFIG_INPUT_MOUSE is not set
 # CONFIG_SERIO is not set
 # CONFIG_LEGACY_PTYS is not set
+CONFIG_SERIAL_FOO=y
 # CONFIG_HW_RANDOM is not set
 CONFIG_I2C=y
 # CONFIG_I2C_COMPAT is not set
@@ -85,7 +88,7 @@ CONFIG_DEBUG_INFO=y
 CONFIG_FRAME_WARN=2048
 CONFIG_UNUSED_SYMBOLS=y
 CONFIG_MAGIC_SYSRQ=y
-CONFIG_DEBUG_KERNEL=y
+CONFIG_DEBUG_MEMORY_INIT=y
 CONFIG_SOFTLOCKUP_DETECTOR=y
 CONFIG_PROVE_LOCKING=y
 CONFIG_BLK_DEV_IO_TRACE=y
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index df8bd0c7b..0c4895929 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -1276,6 +1276,15 @@ config SERIAL_MXS_AUART_CONSOLE
 	help
 	  Enable a MXS AUART port to be the system console.
 
+config SERIAL_FOO
+	tristate "foo serial port support"
+	depends on ARCH_FOO
+	select SERIAL_CORE
+	select SERIAL_CORE_CONSOLE
+	select SERIAL_EARLYCON
+	help
+	  Enable a serial port to be the system console.
+
 config SERIAL_XILINX_PS_UART
 	tristate "Cadence (Xilinx Zynq) UART support"
 	depends on OF
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
index daac67561..02557654b 100644
--- a/drivers/tty/serial/Makefile
+++ b/drivers/tty/serial/Makefile
@@ -72,6 +72,7 @@ obj-$(CONFIG_SERIAL_VT8500) += vt8500_serial.o
 obj-$(CONFIG_SERIAL_IFX6X60)  	+= ifx6x60.o
 obj-$(CONFIG_SERIAL_PCH_UART)	+= pch_uart.o
 obj-$(CONFIG_SERIAL_MXS_AUART) += mxs-auart.o
+obj-$(CONFIG_SERIAL_FOO)	+= foo-serial.o
 obj-$(CONFIG_SERIAL_LANTIQ)	+= lantiq.o
 obj-$(CONFIG_SERIAL_XILINX_PS_UART) += xilinx_uartps.o
 obj-$(CONFIG_SERIAL_SIRFSOC) += sirfsoc_uart.o
diff --git a/drivers/tty/serial/foo-serial.c b/drivers/tty/serial/foo-serial.c
new file mode 100644
index 000000000..14b5a21b4
--- /dev/null
+++ b/drivers/tty/serial/foo-serial.c
@@ -0,0 +1,33 @@
+#include <linux/kernel.h>
+#include <linux/console.h>
+#include <linux/init.h>
+#include <linux/serial_core.h>
+
+#include <asm/io.h>
+#include <asm/early_ioremap.h>
+
+#define UART_UTRSTAT	(0x10)
+#define UART_UTXH	(0x20)
+
+static void foo_serial_putc(struct uart_port *port, int ch)
+{
+	while (!(readl(port->membase + UART_UTRSTAT) & 0x2));
+	writeb(ch, port->membase + UART_UTXH);
+}
+
+static void earlycon_foo_write(struct console *con, const char *s, unsigned n)
+{
+	struct earlycon_device *dev = con->data;
+
+	uart_console_write(&dev->port, s, n, foo_serial_putc);
+}
+
+int __init earlycon_foo_setup(struct earlycon_device *dev, const char *opt)
+{
+	if (!dev->port.membase)
+		return -ENODEV;
+
+	dev->con->write = earlycon_foo_write;
+	return 0;
+}
+EARLYCON_DECLARE(foo_serial, earlycon_foo_setup);
\ No newline at end of file
-- 
2.20.1

