diff options
author | Richard Braun <rbraun@sceen.net> | 2017-05-19 19:31:30 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2017-05-19 19:31:30 +0200 |
commit | 9772e159b4e3b22933f08487cb38d73f0f247c4c (patch) | |
tree | 7df894707a5789cc557335f07758416df04aea0a | |
parent | 805ba95708f577ea3cfb59f9851ea61ef45abc85 (diff) |
x86/uart: new module
-rw-r--r-- | arch/x86/Makefrag.am | 4 | ||||
-rw-r--r-- | arch/x86/machine/biosmem.c | 13 | ||||
-rw-r--r-- | arch/x86/machine/biosmem.h | 14 | ||||
-rw-r--r-- | arch/x86/machine/boot.c | 3 | ||||
-rw-r--r-- | arch/x86/machine/uart.c | 216 | ||||
-rw-r--r-- | arch/x86/machine/uart.h | 34 |
6 files changed, 283 insertions, 1 deletions
diff --git a/arch/x86/Makefrag.am b/arch/x86/Makefrag.am index eb140bc6..d5f1583a 100644 --- a/arch/x86/Makefrag.am +++ b/arch/x86/Makefrag.am @@ -60,6 +60,8 @@ x15_SOURCES += \ arch/x86/machine/trap_asm.S \ arch/x86/machine/trap.c \ arch/x86/machine/trap.h \ - arch/x86/machine/types.h + arch/x86/machine/types.h \ + arch/x86/machine/uart.c \ + arch/x86/machine/uart.h endif X86 diff --git a/arch/x86/machine/biosmem.c b/arch/x86/machine/biosmem.c index 35e015b0..65329a06 100644 --- a/arch/x86/machine/biosmem.c +++ b/arch/x86/machine/biosmem.c @@ -95,6 +95,11 @@ static struct biosmem_map_entry biosmem_map[BIOSMEM_MAX_MAP_SIZE * 2] static unsigned int biosmem_map_size __bootdata; /* + * Temporary copy of the BIOS Data Area. + */ +static char biosmem_bda[BIOSMEM_BDA_SIZE] __bootdata; + +/* * Contiguous block of physical memory. */ struct biosmem_zone { @@ -649,6 +654,8 @@ biosmem_bootstrap(const struct multiboot_raw_info *mbi) phys_addr_t phys_start, phys_end; int error; + boot_memcpy(biosmem_bda, (const void *)BIOSMEM_BDA_ADDR, BIOSMEM_BDA_SIZE); + if (mbi->flags & MULTIBOOT_LOADER_MMAP) { biosmem_map_build(mbi); } else { @@ -738,6 +745,12 @@ biosmem_bootalloc(unsigned int nr_pages) return boot_memset((void *)addr, 0, size); } +const void * +biosmem_get_bda(void) +{ + return biosmem_bda; +} + phys_addr_t __boot biosmem_directmap_end(void) { diff --git a/arch/x86/machine/biosmem.h b/arch/x86/machine/biosmem.h index 19ec2cda..d877af4c 100644 --- a/arch/x86/machine/biosmem.h +++ b/arch/x86/machine/biosmem.h @@ -24,6 +24,12 @@ #include <machine/types.h> /* + * BIOS Data Area. + */ +#define BIOSMEM_BDA_ADDR 0x400 +#define BIOSMEM_BDA_SIZE 0x100 + +/* * Address where the address of the Extended BIOS Data Area segment can be * found. */ @@ -83,6 +89,14 @@ void biosmem_bootstrap(const struct multiboot_raw_info *mbi); void * biosmem_bootalloc(unsigned int nr_pages); /* + * Return a pointer to a copy of the BIOS Data Area. + * + * The region denoted by the returned address is of size BIOSMEM_BDA_SIZE, + * and is only valid during initialization. + */ +const void * biosmem_get_bda(void); + +/* * Return the limit of physical memory that can be directly mapped. */ phys_addr_t biosmem_directmap_end(void); diff --git a/arch/x86/machine/boot.c b/arch/x86/machine/boot.c index 28ed46e7..18306009 100644 --- a/arch/x86/machine/boot.c +++ b/arch/x86/machine/boot.c @@ -73,6 +73,7 @@ #include <machine/pmap.h> #include <machine/strace.h> #include <machine/trap.h> +#include <machine/uart.h> #include <vm/vm_kmem.h> #include <vm/vm_setup.h> @@ -467,9 +468,11 @@ boot_main(void) thread_bootstrap(); console_setup(); cga_setup(); + uart_setup(); printf_setup(); boot_show_version(); arg_info(); + uart_info(); pmap_bootstrap(); sref_bootstrap(); cpu_check(cpu_current()); diff --git a/arch/x86/machine/uart.c b/arch/x86/machine/uart.c new file mode 100644 index 00000000..6277ae14 --- /dev/null +++ b/arch/x86/machine/uart.c @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2017 Richard Braun. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * + * TODO Make serial line parameters configurable. + * TODO 16550A support. + * TODO Interrupt probing and handling. + */ + +#include <stdint.h> +#include <stdio.h> + +#include <kern/assert.h> +#include <kern/console.h> +#include <kern/init.h> +#include <kern/macros.h> +#include <machine/biosmem.h> +#include <machine/io.h> +#include <machine/uart.h> + +#define UART_BDA_COM1_OFFSET 0 + +#define UART_REG_THR 0 +#define UART_REG_DLL 0 +#define UART_REG_IER 1 +#define UART_REG_DLH 1 +#define UART_REG_LCR 3 +#define UART_REG_MCR 4 +#define UART_REG_LSR 5 +#define UART_NR_REGS 6 + +#define UART_THR_EMPTY 0x20 + +#define UART_LCR_8BITS 0x03 +#define UART_LCR_1S 0x00 +#define UART_LCR_NP 0x00 +#define UART_LCR_BEN 0x40 +#define UART_LCR_DLAB 0x80 + +#define UART_MCR_DTR 0x01 +#define UART_MCR_RTS 0x02 +#define UART_MCR_AUX2 0x04 + +#define UART_MAX_DEVS 4 + +struct uart { + struct console console; + uint16_t port; + uint16_t intr; +}; + +static struct uart uart_devs[UART_MAX_DEVS]; + +static uint16_t uart_intrs[UART_MAX_DEVS] = { 4, 3, 4, 3 }; + +static uint16_t +uart_get_addr(const struct uart *uart, uint16_t reg) +{ + assert(reg < UART_NR_REGS); + return uart->port + reg; +} + +static uint8_t +uart_read(struct uart *uart, uint16_t reg) +{ + return io_read_byte(uart_get_addr(uart, reg)); +} + +static void +uart_write(struct uart *uart, uint16_t reg, uint8_t byte) +{ + io_write_byte(uart_get_addr(uart, reg), byte); +} + +static void +uart_set(struct uart *uart, uint16_t reg, uint8_t mask) +{ + uint16_t addr; + uint8_t byte; + + addr = uart_get_addr(uart, reg); + byte = io_read_byte(addr); + byte |= mask; + io_write_byte(addr, byte); +} + +static void +uart_clear(struct uart *uart, uint16_t reg, uint8_t mask) +{ + uint16_t addr; + uint8_t byte; + + addr = uart_get_addr(uart, reg); + byte = io_read_byte(addr); + byte &= ~mask; + io_write_byte(addr, byte); +} + +static void +uart_tx_wait(struct uart *uart) +{ + uint8_t byte; + + for (;;) { + byte = uart_read(uart, UART_REG_LSR); + + if (byte & UART_THR_EMPTY) { + break; + } + } +} + +static struct uart * +uart_get_dev(size_t i) +{ + assert(i < ARRAY_SIZE(uart_devs)); + return &uart_devs[i]; +} + +static struct uart * +uart_get_from_console(struct console *console) +{ + return structof(console, struct uart, console); +} + +static void +uart_write_char_common(struct uart *uart, char c) +{ + uart_tx_wait(uart); + uart_write(uart, UART_REG_THR, (uint8_t)c); +} + +static void +uart_write_char(struct uart *uart, char c) +{ + if (c == '\n') { + uart_write_char_common(uart, '\r'); + } + + uart_write_char_common(uart, c); +} + +static void +uart_console_putc(struct console *console, char c) +{ + uart_write_char(uart_get_from_console(console), c); +} + +static void __init +uart_init(struct uart *uart, uint16_t port, uint16_t intr) +{ + uint8_t byte; + + uart->port = port; + uart->intr = intr; + + uart_write(uart, UART_REG_IER, 0); + uart_write(uart, UART_REG_MCR, UART_MCR_AUX2 | UART_MCR_RTS | UART_MCR_DTR); + + uart_set(uart, UART_REG_LCR, UART_LCR_DLAB); + uart_write(uart, UART_REG_DLH, 0); + uart_write(uart, UART_REG_DLL, 1); + uart_clear(uart, UART_REG_LCR, UART_LCR_DLAB); + + byte = UART_LCR_8BITS | UART_LCR_NP | UART_LCR_1S | UART_LCR_BEN; + uart_write(uart, UART_REG_LCR, byte); + + console_init(&uart->console, uart_console_putc); + console_register(&uart->console); +} + +void __init +uart_setup(void) +{ + const uint16_t *ptr; + size_t i; + + ptr = biosmem_get_bda() + UART_BDA_COM1_OFFSET; + + for (i = 0; i < UART_MAX_DEVS; i++) { + if (ptr[i] == 0) { + continue; + } + + uart_init(uart_get_dev(i), ptr[i], uart_intrs[i]); + } +} + +void __init +uart_info(void) +{ + const struct uart *uart; + size_t i; + + for (i = 0; i < ARRAY_SIZE(uart_devs); i++) { + uart = uart_get_dev(i); + + if (uart->port != 0) { + printf("uart%zu: port:%#x irq:%u\n", i, (unsigned int)uart->port, + (unsigned int)uart->intr); + } + } +} diff --git a/arch/x86/machine/uart.h b/arch/x86/machine/uart.h new file mode 100644 index 00000000..9cb7cbf3 --- /dev/null +++ b/arch/x86/machine/uart.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2017 Richard Braun. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * + * Tiny 8250 UART driver. + */ + +#ifndef _X86_UART_H +#define _X86_UART_H + +/* + * Initialize the uart module. + */ +void uart_setup(void); + +/* + * Display device information. + */ +void uart_info(void); + +#endif /* _X86_UART_H */ |