diff options
| author | Ian Moffett <ian@osmora.org> | 2025-12-21 17:36:33 -0500 |
|---|---|---|
| committer | Ian Moffett <ian@osmora.org> | 2025-12-21 17:36:33 -0500 |
| commit | 258c10c3c021ea275493d54ff39bd18adbb73291 (patch) | |
| tree | ad0088aae8063e4cc477b650bec2395bb536b48d | |
| parent | 82b0276b9267750d88c2f3f73f997c026dd4a4a2 (diff) | |
mos/x86_64: Add UART driver + MU interface
Signed-off-by: Ian Moffett <ian@osmora.org>
| -rw-r--r-- | mos/sys/arch/x86_64/Makefile | 11 | ||||
| -rw-r--r-- | mos/sys/arch/x86_64/io/uart.c | 58 | ||||
| -rw-r--r-- | mos/sys/inc/arch/x86_64/uart.h | 30 | ||||
| -rw-r--r-- | mos/sys/inc/mu/uart.h | 24 | ||||
| -rw-r--r-- | mos/sys/kern/Makefile | 1 | ||||
| -rw-r--r-- | mos/sys/kern/kern_init.c | 4 |
6 files changed, 126 insertions, 2 deletions
diff --git a/mos/sys/arch/x86_64/Makefile b/mos/sys/arch/x86_64/Makefile index c28b067..64dda3e 100644 --- a/mos/sys/arch/x86_64/Makefile +++ b/mos/sys/arch/x86_64/Makefile @@ -7,12 +7,16 @@ ASMFILES = $(shell find . -name "*.S") ASMOFILES = $(ASMFILES:.S=.S.o) MISC_OFILES = $(shell find ../../ -name "*.o") +CFILES = $(shell find . -name "*.c") +OFILES = $(CFILES:.c=.o) + CC = LD = SYS_CFLAGS = CFLAGS = \ - -I ../../target/inc/ \ + -I../../target/inc/ \ + -I../../inc/ \ -I../../../../usr/sdk/inc/ \ $(SYS_CFLAGS) @@ -20,8 +24,11 @@ LDFLAGS = -Tadmin/mos.lds MOS_OUT = ../../sys.mos .PHONY: all -all: $(ASMOFILES) +all: $(OFILES) $(ASMOFILES) $(LD) $(LDFLAGS) $(MISC_OFILES) -o $(MOS_OUT) +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + %.S.o: %.S $(CC) $(CFLAGS) -c $< -o $@ diff --git a/mos/sys/arch/x86_64/io/uart.c b/mos/sys/arch/x86_64/io/uart.c new file mode 100644 index 0000000..99c4645 --- /dev/null +++ b/mos/sys/arch/x86_64/io/uart.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025, Ian Moffett. + * Provided under the BSD-3 clause. + */ + +#include <sdk/param.h> +#include <sdk/types.h> +#include <mu/uart.h> +#include <md/uart.h> +#include <md/pio.h> + +#define DEFAULT_PORT UART_COM1 + +static USHORT commap[] = { + UART_COM1, UART_COM2, + UART_COM3, UART_COM4 +}; + +static void +init_uart(USHORT port) +{ + UBYTE reg; + + /* Clear DLAB bit */ + reg = md_inb(UART_LCR(port)); + reg &= ~UART_LCR_DLAB; + reg &= ~UART_PARITY_MASK; + reg |= UART_LCR_STOP; + md_outb(UART_LCR(port), reg); + + /* Disable interrupts */ + md_outb(UART_INTEN(port), 0); + + /* Set DLAB */ + reg = md_inb(UART_LCR(port)); + reg |= UART_LCR_DLAB; + md_outb(UART_LCR(port), reg); + + /* 38400 baud */ + md_outb(UART_DIVLOW(port), 0x03); + md_outb(UART_DIVHIGH(port), 0x00); +} + +void +mu_uart_write(const char *s, USIZE len) +{ + for (USIZE i = 0; i < len; ++i) { + md_outb(DEFAULT_PORT, s[i]); + } +} + +void +mu_uart_init(void) +{ + for (int i = 0; i < NELEM(commap); ++i) { + init_uart(commap[i]); + } +} diff --git a/mos/sys/inc/arch/x86_64/uart.h b/mos/sys/inc/arch/x86_64/uart.h new file mode 100644 index 0000000..1cb6793 --- /dev/null +++ b/mos/sys/inc/arch/x86_64/uart.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025, Ian Moffett. + * Provided under the BSD-3 clause. + */ + +#ifndef _MACHINE_UART_H_ +#define _MACHINE_UART_H_ 1 + +#include <sdk/param.h> + +/* COM ports */ +#define UART_COM1 0x3F8 +#define UART_COM2 0x2F8 +#define UART_COM3 0x3F8 +#define UART_COM4 0x2E8 +#define UART_REG_OFF(COM, REG) ((COM) + (REG)) + +/* Valid UART 8250 registers */ +#define UART_RXTX(COM) (COM) +#define UART_INTEN(COM) UART_REG_OFF((COM), 0x01) +#define UART_LCR(COM) UART_REG_OFF((COM), 0x03) + +#define UART_DIVLOW(COM) UART_REG_OFF((COM), 0x00) +#define UART_DIVHIGH(COM) UART_REG_OFF((COM), 0x01) + +#define UART_LCR_DLAB BIT(7) +#define UART_LCR_STOP BIT(2) +#define UART_PARITY_MASK (0x07 << 3) + +#endif /* !_MACHINE_UART_H_ */ diff --git a/mos/sys/inc/mu/uart.h b/mos/sys/inc/mu/uart.h new file mode 100644 index 0000000..b38c5f2 --- /dev/null +++ b/mos/sys/inc/mu/uart.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025, Ian Moffett. + * Provided under the BSD-3 clause. + */ + +#ifndef _MU_UART_H_ +#define _MU_UART_H_ 1 + +#include <sdk/types.h> + +/* + * Initialize available platform UARTs + */ +void mu_uart_init(void); + +/* + * Write to the default platform UART + * + * @s: Data to write + * @len: Length of data in bytes + */ +void mu_uart_writed(const char *s, USIZE len); + +#endif /* !_MU_UART_H_ */ diff --git a/mos/sys/kern/Makefile b/mos/sys/kern/Makefile index 5abbf16..6a79c9d 100644 --- a/mos/sys/kern/Makefile +++ b/mos/sys/kern/Makefile @@ -10,6 +10,7 @@ SYS_CFLAGS = CFLAGS = \ $(SYS_CFLAGS) \ -I../../../usr/sdk/inc/ \ + -I../inc/ \ -I../target/inc/ .PHONY: all diff --git a/mos/sys/kern/kern_init.c b/mos/sys/kern/kern_init.c index 62d73fb..80bfd28 100644 --- a/mos/sys/kern/kern_init.c +++ b/mos/sys/kern/kern_init.c @@ -3,10 +3,14 @@ * Provided under the BSD-3 clause. */ +#include <mu/uart.h> + /* Forward declaration */ void kern_main(void); void kern_main(void) { + /* Initialize platform UARTs */ + mu_uart_init(); } |
