/* SPDX-License-Identifier: GPL-2.0-or-later */ /* Copyright (C) 2026 Richard Qin */ /* * uart.cc — 16550a UART driver (early console) * * Implements the early UART output mapping required by panic and kernel_main. */ #include #include #include #include #include static uint64 g_uart_pa = 0; static uint64 g_uart_size = 0; extern "C" void early_putc(char c) { if (!g_uart_pa) return; volatile auto* uart = reinterpret_cast(pa_to_va(phys_addr(g_uart_pa)).raw); *uart = static_cast(c); } extern "C" void early_puts(const char* s) { while (*s) early_putc(*s++); } void console_early_init(uint64 fdt_paddr) { uint64 pa = 0; uint64 size = 0; FdtManager::early_scan_uart(fdt_paddr, &pa, &size); if (pa != 0) { g_uart_pa = pa; if (size != 0) g_uart_size = size; } } void console_init() { /* * Once vmm_init() activates the new page table, the 1GB identity map is dropped. * The UART must be explicitly registered into the active page table. */ if (g_uart_pa != 0 && g_uart_size != 0) { vmm_map_kernel_mmio(g_uart_pa, g_uart_size); } }