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
13#if defined(CONFIG_DEBUG_BUILD) || defined(CONFIG_PRINTING)
14void serial_init(uint16_t port)
15{
16    while (!(in8(port + 5) & 0x60)); /* wait until not busy */
17
18    out8(port + 1, 0x00); /* disable generating interrupts */
19    out8(port + 3, 0x80); /* line control register: command: set divisor */
20    out8(port,     0x01); /* set low byte of divisor to 0x01 = 115200 baud */
21    out8(port + 1, 0x00); /* set high byte of divisor to 0x00 */
22    out8(port + 3, 0x03); /* line control register: set 8 bit, no parity, 1 stop bit */
23    out8(port + 4, 0x0b); /* modem control register: set DTR/RTS/OUT2 */
24
25    in8(port);     /* clear receiver port */
26    in8(port + 5); /* clear line status port */
27    in8(port + 6); /* clear modem status port */
28}
29
30void putDebugChar(unsigned char a)
31{
32    while (x86KSdebugPort && (in8(x86KSdebugPort + 5) & 0x20) == 0);
33    out8(x86KSdebugPort, a);
34}
35
36#endif /* CONFIG_PRINTING || CONFIG_DEBUG_BUILD */
37
38#ifdef CONFIG_DEBUG_BUILD
39unsigned char getDebugChar(void)
40{
41    while ((in8(x86KSdebugPort + 5) & 1) == 0);
42    return in8(x86KSdebugPort);
43}
44#endif /* CONFIG_DEBUG_BUILD */
45