v2 initial commit
This commit is contained in:
parent
2c97dba9f7
commit
d2ef885e8d
118 changed files with 2420 additions and 9320 deletions
109
amd64/drivers/apic.c
Normal file
109
amd64/drivers/apic.c
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/mem.h>
|
||||
#include <drivers/apic.h>
|
||||
#include <arch/msr.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static uintptr_t ioapic_addr = 0xFEC00000;
|
||||
static uintptr_t lapic_addr = 0xFEE00000;
|
||||
static uint8_t lapic_id = 0;
|
||||
|
||||
static struct ioapic {
|
||||
uint32_t reg;
|
||||
uint32_t pad[3];
|
||||
uint32_t data;
|
||||
} volatile *ioapic;
|
||||
|
||||
uint32_t ioapic_read(uint32_t reg)
|
||||
{
|
||||
ioapic->reg = reg;
|
||||
return ioapic->data;
|
||||
}
|
||||
|
||||
void ioapic_write(uint32_t reg, uint32_t value)
|
||||
{
|
||||
ioapic->reg = reg;
|
||||
ioapic->data = value;
|
||||
}
|
||||
|
||||
void ioapic_redirect_insert(size_t irq, size_t isr, size_t low_active, size_t level_trigger)
|
||||
{
|
||||
/* Set the interrupt redirection entry */
|
||||
ioapic_write(0x10 + 2 * irq, (level_trigger << 15) | (low_active << 13) | isr);
|
||||
ioapic_write(0x10 + 2 * irq + 1, lapic_id << 24);
|
||||
}
|
||||
|
||||
uint32_t lapic_read(uintptr_t addr, unsigned int regoffset)
|
||||
{
|
||||
volatile uint32_t *lapic = (volatile uint32_t *)addr;
|
||||
return lapic[regoffset >> 2];
|
||||
}
|
||||
|
||||
uint32_t lapic_write(uintptr_t addr, unsigned int regoffset, uint32_t value)
|
||||
{
|
||||
volatile uint32_t *lapic = (volatile uint32_t *)addr;
|
||||
lapic[regoffset >> 2] = value;
|
||||
return lapic[regoffset >> 2];
|
||||
}
|
||||
|
||||
void apic_eoi()
|
||||
{
|
||||
lapic_write(lapic_addr, 0xB0, 0);
|
||||
}
|
||||
|
||||
void lapic_eoi()
|
||||
{
|
||||
lapic_write(lapic_addr, LAPIC_EOI, 0);
|
||||
}
|
||||
|
||||
void lapic_enable()
|
||||
{
|
||||
/* Set spurious interrupt vector to 0xFF and enable the LAPIC */
|
||||
lapic_write(lapic_addr, 0xF0, 0x1FF);
|
||||
}
|
||||
|
||||
uint8_t lapic_idno()
|
||||
{
|
||||
return lapic_read(lapic_addr, LAPIC_ID) >> 24;
|
||||
}
|
||||
|
||||
void apic_init()
|
||||
{
|
||||
ioapic_addr += hhdm_start;
|
||||
lapic_addr += hhdm_start;
|
||||
|
||||
ioapic = (void *)ioapic_addr;
|
||||
|
||||
uint32_t r = ioapic_read(0);
|
||||
r |= 0x04000000;
|
||||
ioapic_write(0, r);
|
||||
|
||||
lapic_id = lapic_read(lapic_addr, 0x20);
|
||||
ioapic_redirect_insert(4, 0x24, 0, 0);
|
||||
|
||||
lapic_enable();
|
||||
|
||||
uint64_t msr = rdmsr(MSR_IA32_APIC_BASE);
|
||||
msr |= 1 << 11;
|
||||
wrmsr(MSR_IA32_APIC_BASE, msr);
|
||||
|
||||
lapic_eoi();
|
||||
|
||||
krdy("APIC initialized\n");
|
||||
}
|
||||
|
||||
void apic_enable_timer()
|
||||
{
|
||||
/* set TPR to ensure interrupts are accepted */
|
||||
lapic_write(lapic_addr, LAPIC_TPR, 0);
|
||||
|
||||
/* set timer divisor */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER_DIV, 0x3);
|
||||
|
||||
/* set timer initial count */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER_INIT_COUNT, 0xFFF);
|
||||
|
||||
/* set timer vector and enable */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER, 0x20 | 0x20000);
|
||||
}
|
||||
28
amd64/drivers/pio.S
Normal file
28
amd64/drivers/pio.S
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
.intel_syntax noprefix
|
||||
|
||||
.global outb
|
||||
outb:
|
||||
mov ax, si
|
||||
mov dx, di
|
||||
out dx, al
|
||||
ret
|
||||
|
||||
.global inb
|
||||
inb:
|
||||
mov dx, di
|
||||
in al, dx
|
||||
ret
|
||||
|
||||
.global outw
|
||||
outw:
|
||||
mov ax, si
|
||||
mov dx, di
|
||||
out dx, ax
|
||||
ret
|
||||
|
||||
.global inw
|
||||
inw:
|
||||
mov dx, di
|
||||
in ax, dx
|
||||
ret
|
||||
79
amd64/drivers/serial.c
Normal file
79
amd64/drivers/serial.c
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/* 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);
|
||||
}
|
||||
Loading…
Reference in a new issue