17 #define BUFFER_SIZE 4096
19 enum serial_port_regs {
35 # define DEBUG_PORT 0x03f8 /* I/O base address */
38 static const uint16_t debug_base = DEBUG_PORT;
40 static void debug_putc(char c)
45 while ((inb(debug_base + LSR) & 0x20) == 0)
47 outb(c, debug_base + THR);
50 void vdprintf(const char *format, va_list ap)
53 char buffer[BUFFER_SIZE];
55 static bool debug_init = false;
56 static bool debug_ok = false;
58 rv = vsnprintf(buffer, BUFFER_SIZE, format, ap);
63 if (rv > BUFFER_SIZE - 1)
67 * This unconditionally outputs to a serial port at 0x3f8 regardless of
68 * if one is enabled or not (this means we don't have to enable the real
69 * serial console and therefore get conflicting output.)
71 if (__unlikely(!debug_init)) {
72 uint8_t dll, dlm, lcr;
78 /* Initialize the serial port to 115200 n81 with FIFOs enabled */
79 outb(0x83, debug_base + LCR);
80 outb(0x01, debug_base + DLL);
81 outb(0x00, debug_base + DLM);
82 (void)inb(debug_base + IER); /* Synchronize */
83 dll = inb(debug_base + DLL);
84 dlm = inb(debug_base + DLM);
85 lcr = inb(debug_base + LCR);
87 outb(0x03, debug_base + LCR);
88 (void)inb(debug_base + IER); /* Synchronize */
90 outb(0x00, debug_base + IER);
91 (void)inb(debug_base + IER); /* Synchronize */
95 if (dll != 0x01 || dlm != 0x00 || lcr != 0x83) {
96 /* No serial port present */
100 outb(0x01, debug_base + FCR);
101 (void)inb(debug_base + IER); /* Synchronize */
102 if (inb(debug_base + IIR) < 0xc0) {
103 outb(0x00, debug_base + FCR); /* Disable non-functional FIFOs */
104 (void)inb(debug_base + IER); /* Synchronize */