vbos/amd64/drivers/serial.c
Michael Shaw c4ec0bdf21 tty: move tty/ from drivers/ to kernel/
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-12 11:05:30 -04:00

72 lines
1.4 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/device.h>
#include <kernel/slab.h>
#include <drivers/serial.h>
#include <drivers/pio.h>
#include <kernel/tty.h>
#include <lib/stdio.h>
void init_serial_port(int port)
{
outb(port | 1, 0x00);
outb(port | 3, 0x80);
outb(port | 0, 0x01);
outb(port | 1, 0x00);
outb(port | 3, 0x03);
outb(port | 2, 0xC7);
outb(port | 4, 0x0B);
outb(port | 4, 0x1E);
outb(port | 0, 0xAE); /* Enable loopback mode */
if (inb(port | 0) != 0xAE) { /* Loopback test */
kwarn("Serial port 0x%04x loopback test failed\n", port);
return;
}
outb(port | 1, 0x01); /* Enable interrupts */
outb(port | 4, 0x0F); /* Enter normal operation */
}
void arch_serial_write(struct tty *tty, char c)
{
/* Wait for line to be clear */
size_t port = (size_t)tty->priv;
while ((inb(port + 5) & 0x20) == 0)
;
outb(port, c);
}
char arch_serial_read(struct tty *tty)
{
size_t port = (size_t)tty->priv;
while ((inb(port + 5) & 1) == 0)
;
return inb(port);
}
int serial_port_tty_init(struct tty **out)
{
init_serial_port(COM1);
*out = kalloc(sizeof(struct tty), ALLOC_KERN);
if (!(*out))
return -ENOMEM;
(*out)[0].priv = (void *)COM1;
(*out)[0].serial_ops.serial_getchar = arch_serial_read;
(*out)[0].serial_ops.serial_putchar = arch_serial_write;
dev_t com1 = device_create((*out + 0), DEV_CLASS_TTY, "ttyS0");
(*out)[0].dev = com1;
return 1;
}