Files
magenta/arch/x86_64/serial.c
T

273 lines
5.7 KiB
C
Raw Normal View History

#include <arch/irq.h>
#include <arch/ports.h>
#include <arch/serial.h>
#include <kernel/libc/stdio.h>
#include <kernel/printk.h>
#include <kernel/ringbuf.h>
#define SERIAL_BUFFER_SIZE 1024
RINGBUF_DECLARE(com1_queue, SERIAL_BUFFER_SIZE);
RINGBUF_DECLARE(com2_queue, SERIAL_BUFFER_SIZE);
RINGBUF_DECLARE(com3_queue, SERIAL_BUFFER_SIZE);
RINGBUF_DECLARE(com4_queue, SERIAL_BUFFER_SIZE);
static const uint16_t port_addresses[] = {
[COM1] = 0x3F8,
[COM2] = 0x2F8,
[COM3] = 0x3E8,
[COM4] = 0x2E8,
};
static int port_active[] = {
[COM1] = 0,
[COM2] = 0,
[COM3] = 0,
[COM4] = 0,
};
static struct ringbuf *port_buffer[] = {
[COM1] = &com1_queue,
[COM2] = &com2_queue,
[COM3] = &com3_queue,
[COM4] = &com4_queue,
};
static int transmit_empty(int port)
{
return inportb(port_addresses[port] + 5) & 0x20;
}
static int serial_received(int port)
{
return inportb(port_addresses[port] + 5) & 0x1;
}
void serial_send_byte(int port, char out)
{
uint16_t address = port_addresses[port];
volatile unsigned int _count = 0;
while (!transmit_empty(port)) {
_count++;
}
(void)_count;
outportb(address, out);
if (port == COM1) {
outportb(0xe9, out);
}
while (!transmit_empty(port)) {
_count++;
}
}
char serial_recv_byte(int port)
{
uint16_t address = port_addresses[port];
volatile unsigned int _count = 0;
while (!serial_received(port)) {
_count++;
}
(void)_count;
char c = inportb(address);
outportb(address + 5, inportb(address + 5) & ~0x1);
return c;
}
void serial_putchar(int port, char ch)
{
if (ch == '\n') {
serial_send_byte(port, '\r');
}
serial_send_byte(port, ch);
}
void serialcon_write(struct console *con, const char *s, unsigned int len)
{
for (unsigned int i = 0; i < len; i++) {
serial_putchar(COM1, s[i]);
}
}
static int get_baud_divisor(int baud)
{
int freq = 115200;
int best_baud = -1;
int best_div = -1;
for (int i = 1; i < 254; i++) {
int this_baud = freq / i;
if (this_baud == baud) {
return i;
}
if (best_baud == -1) {
best_baud = this_baud;
best_div = i;
continue;
}
if (this_baud < baud && best_baud > baud) {
/* TODO pick divisor that gives the closest baud rate */
return best_div;
}
best_baud = this_baud;
best_div = i;
}
return best_div;
}
static void init_serial_port(int port, int baud)
{
uint16_t address = port_addresses[port];
int baud_div = get_baud_divisor(baud);
outportb(address + 1, 0x00); // Disable all interrupts
outportb(address + 3, 0x80); // Enable DLAB (set baud rate divisor)
outportb(address + 0, baud_div); // Set divisor
outportb(address + 1, 0x00);
outportb(address + 3, 0x03); // 8 bits, no parity, one stop bit
outportb(
address + 2,
0xC7); // Enable FIFO, clear them, with 14-byte threshold
outportb(address + 4, 0x0B); // IRQs enabled, RTS/DSR set
outportb(
address + 4,
0x1E); // Set in loopback mode, test the serial chip
outportb(address + 0, 0xAE); // Test serial chip (send byte 0xAE and
// check if serial returns same byte)
volatile unsigned int q = 0;
while (!serial_received(port)) {
q++;
}
(void)q;
// Check if serial is faulty (i.e: not same byte as sent)
if (inportb(address + 0) != 0xAE) {
printk("serial: COM%d is faulty", port);
return;
}
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
outportb(address + 1, 0x01);
outportb(address + 4, 0x0F);
port_active[port] = 1;
printk("serial: COM%d initialised", port);
}
kern_status_t serial_read(int device, void *buf, size_t max, size_t *nr_read)
{
struct ringbuf *src = port_buffer[device];
unsigned long flags;
ringbuf_lock(src, &flags);
kern_status_t status = ringbuf_read(src, buf, max, nr_read, &flags);
ringbuf_unlock(src, flags);
return status;
}
kern_status_t serial_write(
int device,
const void *buf,
size_t count,
size_t *nr_written)
{
if (!port_active[device]) {
return KERN_BAD_STATE;
}
const unsigned char *p = buf;
size_t w = 0;
for (size_t i = 0; i < count; i++) {
serial_send_byte(device, p[i]);
w++;
}
*nr_written = w;
return KERN_OK;
}
static struct console serialcon = {
.c_name = "serialcon",
.c_flags = CON_BOOT,
.c_write = serialcon_write,
.c_lock = SPIN_LOCK_INIT,
};
static int serial_irq_com1_com3(void)
{
unsigned long flags;
if (port_active[COM1] && serial_received(COM1)) {
unsigned char c = serial_recv_byte(COM1);
ringbuf_lock(&com1_queue, &flags);
ringbuf_write(&com1_queue, &c, 1, NULL, &flags);
ringbuf_unlock(&com1_queue, flags);
}
if (port_active[COM3] && serial_received(COM3)) {
unsigned char c = serial_recv_byte(COM3);
ringbuf_lock(&com3_queue, &flags);
ringbuf_write(&com3_queue, &c, 1, NULL, &flags);
ringbuf_unlock(&com3_queue, flags);
}
return 0;
}
static int serial_irq_com2_com4(void)
{
unsigned long flags;
if (port_active[COM2] && serial_received(COM2)) {
unsigned char c = serial_recv_byte(COM2);
ringbuf_lock(&com2_queue, &flags);
ringbuf_write(&com2_queue, &c, 1, NULL, &flags);
ringbuf_unlock(&com2_queue, flags);
}
if (port_active[COM4] && serial_received(COM4)) {
unsigned char c = serial_recv_byte(COM4);
ringbuf_lock(&com4_queue, &flags);
ringbuf_write(&com4_queue, &c, 1, NULL, &flags);
ringbuf_unlock(&com4_queue, flags);
}
return 0;
}
static struct irq_hook irq_hook_com1_com3 = {
.irq_callback = serial_irq_com1_com3,
};
static struct irq_hook irq_hook_com2_com4 = {
.irq_callback = serial_irq_com2_com4,
};
void serial_init(int baud)
{
hook_irq(IRQ4, &irq_hook_com1_com3);
hook_irq(IRQ3, &irq_hook_com2_com4);
init_serial_port(COM1, baud);
init_serial_port(COM2, baud);
init_serial_port(COM3, baud);
init_serial_port(COM4, baud);
}
void early_serialcon_init(void)
{
console_register(&serialcon);
}