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