1 /*
2  * Copyright 2014, General Dynamics C4 Systems
3  *
4  * SPDX-License-Identifier: GPL-2.0-only
5  */
6 
7 #include <config.h>
8 #include <arch/kernel/boot_sys.h>
9 #include <arch/model/statedata.h>
10 #include <machine/io.h>
11 #include <plat/machine/io.h>
12 #include <drivers/uart.h>
13 
14 #if defined(CONFIG_DEBUG_BUILD) || defined(CONFIG_PRINTING)
serial_init(uint16_t port)15 void serial_init(uint16_t port)
16 {
17     while (!(in8(port + 5) & 0x60)); /* wait until not busy */
18 
19     out8(port + 1, 0x00); /* disable generating interrupts */
20     out8(port + 3, 0x80); /* line control register: command: set divisor */
21     out8(port,     0x01); /* set low byte of divisor to 0x01 = 115200 baud */
22     out8(port + 1, 0x00); /* set high byte of divisor to 0x00 */
23     out8(port + 3, 0x03); /* line control register: set 8 bit, no parity, 1 stop bit */
24     out8(port + 4, 0x0b); /* modem control register: set DTR/RTS/OUT2 */
25 
26     in8(port);     /* clear receiver port */
27     in8(port + 5); /* clear line status port */
28     in8(port + 6); /* clear modem status port */
29 }
30 #endif /* defined(CONFIG_DEBUG_BUILD) || defined(CONFIG_PRINTING) */
31 
32 #ifdef CONFIG_PRINTING
33 
34 /* there is no UART driver at drivers/serial/... for the pc99 platform, but we
35  * implement parts of the API here, so we can reuse the generic code.
36  */
uart_drv_putchar(unsigned char c)37 void uart_drv_putchar(
38     unsigned char c)
39 {
40     while (x86KSdebugPort && (in8(x86KSdebugPort + 5) & 0x20) == 0);
41     out8(x86KSdebugPort, c);
42 }
43 
kernel_putDebugChar(unsigned char c)44 void kernel_putDebugChar(unsigned char c)
45 {
46     /* this will take care of CR/LF handling and call uart_drv_putchar() */
47     uart_console_putchar(c);
48 }
49 
50 #endif /* CONFIG_PRINTING */
51 
52 #ifdef CONFIG_DEBUG_BUILD
kernel_getDebugChar(void)53 unsigned char kernel_getDebugChar(void)
54 {
55     while ((in8(x86KSdebugPort + 5) & 1) == 0);
56     return in8(x86KSdebugPort);
57 }
58 #endif /* CONFIG_DEBUG_BUILD */
59