blob: 6486fc086b1cd753da5c53a2f2944840a4c6f51e [file] [log] [blame]
Kevin O'Connor4cd522e2013-11-29 12:14:34 -05001// Low-level serial (and serial-like) device access.
2//
3// Copyright (C) 2008-1013 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "config.h" // CONFIG_DEBUG_SERIAL
8#include "fw/paravirt.h" // RunningOnQEMU
9#include "output.h" // dprintf
10#include "serialio.h" // serial_debug_preinit
11#include "x86.h" // outb
12
13
14/****************************************************************
15 * Serial port debug output
16 ****************************************************************/
17
18#define DEBUG_TIMEOUT 100000
19
20// Setup the debug serial port for output.
21void
22serial_debug_preinit(void)
23{
24 if (!CONFIG_DEBUG_SERIAL)
25 return;
26 // setup for serial logging: 8N1
27 u8 oldparam, newparam = 0x03;
28 oldparam = inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LCR);
29 outb(newparam, CONFIG_DEBUG_SERIAL_PORT+SEROFF_LCR);
30 // Disable irqs
31 u8 oldier, newier = 0;
32 oldier = inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_IER);
33 outb(newier, CONFIG_DEBUG_SERIAL_PORT+SEROFF_IER);
34
35 if (oldparam != newparam || oldier != newier)
36 dprintf(1, "Changing serial settings was %x/%x now %x/%x\n"
37 , oldparam, oldier, newparam, newier);
38}
39
40// Write a character to the serial port.
41static void
42serial_debug(char c)
43{
44 if (!CONFIG_DEBUG_SERIAL)
45 return;
46 int timeout = DEBUG_TIMEOUT;
47 while ((inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LSR) & 0x20) != 0x20)
48 if (!timeout--)
49 // Ran out of time.
50 return;
51 outb(c, CONFIG_DEBUG_SERIAL_PORT+SEROFF_DATA);
52}
53
54void
55serial_debug_putc(char c)
56{
57 if (c == '\n')
58 serial_debug('\r');
59 serial_debug(c);
60}
61
62// Make sure all serial port writes have been completely sent.
63void
64serial_debug_flush(void)
65{
66 if (!CONFIG_DEBUG_SERIAL)
67 return;
68 int timeout = DEBUG_TIMEOUT;
69 while ((inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LSR) & 0x60) != 0x60)
70 if (!timeout--)
71 // Ran out of time.
72 return;
73}
74
75
76/****************************************************************
77 * QEMU debug port
78 ****************************************************************/
79
80u16 DebugOutputPort VARFSEG = 0x402;
81
82// Write a character to the special debugging port.
83void
84qemu_debug_putc(char c)
85{
86 if (CONFIG_DEBUG_IO && runningOnQEMU())
87 // Send character to debug port.
88 outb(c, GET_GLOBAL(DebugOutputPort));
89}