hazel/kernel/serial.c

45 lines
1.5 KiB
C
Raw Normal View History

2024-06-27 18:36:32 -04:00
#include <kernel/serial.h>
#include <kernel/io.h>
#include <kernel/kernel.h>
uint8_t serial_port_init(int port) {
// Test port by seeing if scratch register can store a value
uint8_t old_scratch = inb(port + 7);
outb(port + 7, old_scratch + 1);
if (inb(port + 7) != old_scratch + 1)
return 0;
outb(port + 7, old_scratch);
outb(port + 1, 0x00); // Disable all interrupts
outb(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(port + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(port + 1, 0x00); // (hi byte)
outb(port + 3, 0x03); // 8 bits, no parity, one stop bit
outb(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
outb(port + 4, 0x1E); // Set in loopback mode, test the serial chip
outb(port + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
// Check if serial is faulty (i.e: not same byte as sent)
if(inb(port + 0) != 0xAE)
return 0;
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
outb(port + 4, 0x0F);
return 1;
}
void serial_putc(int port, const char c) {
// Poll until transmit is empty
while(!IS_TRANSMIT_EMPTY(port)) cpu_relax;
outb(port, c);
}
void serial_puts(int port, const char *str) {
while (*str) {
serial_putc(port, str[0]);
str++;
}
}