[PATCH v1 2/6] x86/boot: Introduce helpers for serial I/O

From: Andy Shevchenko
Date: Sun Jan 14 2018 - 09:34:08 EST


As preparatory to enable earlyprintk on non-standard ports on x86,
introduce serial_in() and serial_out() helpers to perform serial I/O.

No functional change intended.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
---
arch/x86/boot/boot.h | 2 +
arch/x86/boot/compressed/early_serial_console.c | 3 ++
arch/x86/boot/compressed/misc.c | 4 +-
arch/x86/boot/compressed/misc.h | 4 ++
arch/x86/boot/early_serial_console.c | 60 +++++++++++++++++--------
arch/x86/boot/tty.c | 7 ++-
6 files changed, 57 insertions(+), 23 deletions(-)

diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h
index d9f8279774f6..7e6ac187275c 100644
--- a/arch/x86/boot/boot.h
+++ b/arch/x86/boot/boot.h
@@ -299,6 +299,8 @@ int check_knl_erratum(void);
int validate_cpu(void);

/* early_serial_console.c */
+extern unsigned int (*serial_in)(unsigned long addr, int offset);
+extern void (*serial_out)(unsigned long addr, int offset, int value);
extern unsigned long early_serial_base;
void console_init(void);

diff --git a/arch/x86/boot/compressed/early_serial_console.c b/arch/x86/boot/compressed/early_serial_console.c
index 5e3a66478754..4b6269624b59 100644
--- a/arch/x86/boot/compressed/early_serial_console.c
+++ b/arch/x86/boot/compressed/early_serial_console.c
@@ -1,5 +1,8 @@
#include "misc.h"

+unsigned int (*serial_in)(unsigned long addr, int offset);
+void (*serial_out)(unsigned long addr, int offset, int value);
+
unsigned long early_serial_base;

#include "../early_serial_console.c"
diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c
index 98761a1576ce..0b16675c2523 100644
--- a/arch/x86/boot/compressed/misc.c
+++ b/arch/x86/boot/compressed/misc.c
@@ -97,10 +97,10 @@ static void serial_putchar(int ch)
{
unsigned timeout = 0xffff;

- while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
+ while ((serial_in(early_serial_base, LSR) & XMTRDY) == 0 && --timeout)
cpu_relax();

- outb(ch, early_serial_base + TXR);
+ serial_out(early_serial_base, TXR, ch);
}

void __putstr(const char *s)
diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h
index d36d4398a6b0..266e6b93788c 100644
--- a/arch/x86/boot/compressed/misc.h
+++ b/arch/x86/boot/compressed/misc.h
@@ -101,9 +101,13 @@ static inline void finalize_identity_maps(void)

#ifdef CONFIG_EARLY_PRINTK
/* early_serial_console.c */
+extern unsigned int (*serial_in)(unsigned long addr, int offset);
+extern void (*serial_out)(unsigned long addr, int offset, int value);
extern unsigned long early_serial_base;
void console_init(void);
#else
+static unsigned int (*serial_in)(unsigned long addr, int offset);
+static void (*serial_out)(unsigned long addr, int offset, int value);
static const unsigned long early_serial_base;
static inline void console_init(void)
{ }
diff --git a/arch/x86/boot/early_serial_console.c b/arch/x86/boot/early_serial_console.c
index a5aec142c91a..8b091919b3b9 100644
--- a/arch/x86/boot/early_serial_console.c
+++ b/arch/x86/boot/early_serial_console.c
@@ -23,22 +23,46 @@

#define DEFAULT_BAUD 9600

-static void early_serial_init(unsigned long port, int baud)
+static unsigned int io_serial_in(unsigned long addr, int offset)
+{
+ return inb(addr + offset);
+}
+
+static void io_serial_out(unsigned long addr, int offset, int value)
+{
+ outb(value, addr + offset);
+}
+
+static void early_serial_configure(unsigned long port, int baud)
{
unsigned char c;
unsigned divisor;

- outb(0x3, port + LCR); /* 8n1 */
- outb(0, port + IER); /* no interrupt */
- outb(0, port + FCR); /* no fifo */
- outb(0x3, port + MCR); /* DTR + RTS */
+ serial_out(port, LCR, 0x3); /* 8n1 */
+ serial_out(port, IER, 0); /* no interrupt */
+ serial_out(port, FCR, 0); /* no fifo */
+ serial_out(port, MCR, 0x3); /* DTR + RTS */

divisor = 115200 / baud;
- c = inb(port + LCR);
- outb(c | DLAB, port + LCR);
- outb(divisor & 0xff, port + DLL);
- outb((divisor >> 8) & 0xff, port + DLH);
- outb(c & ~DLAB, port + LCR);
+ c = serial_in(port, LCR);
+ serial_out(port, LCR, c | DLAB);
+ serial_out(port, DLL, divisor & 0xff);
+ serial_out(port, DLH, (divisor >> 8) & 0xff);
+ serial_out(port, LCR, c & ~DLAB);
+}
+
+static void early_serial_init(unsigned long port, int baud)
+{
+ /* Assign serial I/O accessors */
+ if (port) {
+ /* These will always be IO based ports */
+ serial_in = io_serial_in;
+ serial_out = io_serial_out;
+ } else {
+ return;
+ }
+
+ early_serial_configure(port, baud);

early_serial_base = port;
}
@@ -94,8 +118,7 @@ static void parse_earlyprintk(void)
baud = DEFAULT_BAUD;
}

- if (port)
- early_serial_init(port, baud);
+ early_serial_init(port, baud);
}

#define BASE_BAUD (1843200/16)
@@ -104,11 +127,11 @@ static unsigned int probe_baud(int port)
unsigned char lcr, dll, dlh;
unsigned int quot;

- lcr = inb(port + LCR);
- outb(lcr | DLAB, port + LCR);
- dll = inb(port + DLL);
- dlh = inb(port + DLH);
- outb(lcr, port + LCR);
+ lcr = serial_in(port, LCR);
+ serial_out(port, LCR, lcr | DLAB);
+ dll = serial_in(port, DLL);
+ dlh = serial_in(port, DLH);
+ serial_out(port, LCR, lcr);
quot = (dlh << 8) | dll;

return BASE_BAUD / quot;
@@ -141,8 +164,7 @@ static void parse_console_uart8250(void)
else
baud = probe_baud(port);

- if (port)
- early_serial_init(port, baud);
+ early_serial_init(port, baud);
}

void console_init(void)
diff --git a/arch/x86/boot/tty.c b/arch/x86/boot/tty.c
index d1d34c6ca153..b6aa9db65cda 100644
--- a/arch/x86/boot/tty.c
+++ b/arch/x86/boot/tty.c
@@ -15,6 +15,9 @@

#include "boot.h"

+unsigned int (*serial_in)(unsigned long addr, int offset);
+void (*serial_out)(unsigned long addr, int offset, int value);
+
unsigned long early_serial_base;

#define XMTRDY 0x20
@@ -31,10 +34,10 @@ static void __attribute__((section(".inittext"))) serial_putchar(int ch)
{
unsigned timeout = 0xffff;

- while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
+ while ((serial_in(early_serial_base, LSR) & XMTRDY) == 0 && --timeout)
cpu_relax();

- outb(ch, early_serial_base + TXR);
+ serial_out(early_serial_base, TXR, ch);
}

static void __attribute__((section(".inittext"))) bios_putchar(int ch)
--
2.15.1