79 lines
1.7 KiB
C
79 lines
1.7 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/common.h>
|
|
#include <kernel/device.h>
|
|
|
|
#include <drivers/serial.h>
|
|
#include <drivers/pio.h>
|
|
|
|
#include <drivers/tty.h>
|
|
|
|
#include <lib/stdio.h>
|
|
|
|
struct serial_port {
|
|
dev_t dev;
|
|
int port;
|
|
};
|
|
|
|
static int num_serial_ports = 0;
|
|
static struct serial_port serial_ports[32];
|
|
|
|
static dev_t 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 1;
|
|
}
|
|
|
|
outb(port | 1, 0x01); /* Enable interrupts */
|
|
outb(port | 4, 0x0F); /* Enter normal operation */
|
|
|
|
char name[256];
|
|
|
|
snprintf(name, sizeof(name) - 1, "tty%04x", port);
|
|
name[sizeof(name) - 1] = 0;
|
|
|
|
dev_t serial_device = device_create(&serial_ports[num_serial_ports], DEV_CLASS_NATIVE_SERIAL, name);
|
|
|
|
if (num_serial_ports + 1 < ARRAY_SIZE(serial_ports)) {
|
|
serial_ports[num_serial_ports].dev = serial_device;
|
|
serial_ports[num_serial_ports].port = port;
|
|
num_serial_ports += 1;
|
|
}
|
|
|
|
return serial_device;
|
|
}
|
|
|
|
dev_t arch_serial_init()
|
|
{
|
|
dev_t com1 = init_serial_port(COM1);
|
|
return com1;
|
|
}
|
|
|
|
void arch_serial_write(struct tty *tty, char c)
|
|
{
|
|
/* Wait for line to be clear */
|
|
struct serial_port *serial_port = tty->serial_device_private;
|
|
while ((inb(serial_port->port + 5) & 0x20) == 0)
|
|
;
|
|
outb(serial_port->port, c);
|
|
}
|
|
|
|
char arch_serial_read(struct tty *tty)
|
|
{
|
|
struct serial_port *serial_port = tty->serial_device_private;
|
|
|
|
while ((inb(serial_port->port + 5) & 1) == 0)
|
|
;
|
|
|
|
return inb(serial_port->port);
|
|
}
|