mirror of
https://github.com/ipxe/ipxe
synced 2026-04-04 03:00:20 +03:00
[uart] Allow for the existence of non-16550 UARTs
Remove the assumption that all platforms use a fixed number of 16550 UARTs identifiable by a simple numeric index. Create an abstraction allowing for dynamic instantiation and registration of any number of arbitrary UART models. The common case of the serial console on x86 uses a single fixed UART specified at compile time. Avoid unnecessarily dragging in the dynamic instantiation code in this use case by allowing COMCONSOLE to refer to a single static UART object representing the relevant port. When selecting a UART by command-line argument (as used in the "gdbstub serial <port>" command), allow the UART to be specified as either a numeric index (to retain backwards compatiblity) or a case-insensitive port name such as "COM2". Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -25,20 +25,12 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <ipxe/uart.h>
|
||||
#include <ipxe/gdbstub.h>
|
||||
#include <ipxe/gdbserial.h>
|
||||
#include <config/serial.h>
|
||||
|
||||
/* UART port number */
|
||||
#ifdef COMCONSOLE
|
||||
#define GDBSERIAL_PORT COMCONSOLE
|
||||
#else
|
||||
#define GDBSERIAL_PORT 0
|
||||
#endif
|
||||
|
||||
/* UART baud rate */
|
||||
#ifdef COMPRESERVE
|
||||
#define GDBSERIAL_BAUD 0
|
||||
@@ -47,37 +39,30 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
#endif
|
||||
|
||||
/** GDB serial UART */
|
||||
static struct uart gdbserial_uart;
|
||||
static struct uart *gdbserial_uart;
|
||||
|
||||
struct gdb_transport serial_gdb_transport __gdb_transport;
|
||||
|
||||
static size_t gdbserial_recv ( char *buf, size_t len ) {
|
||||
|
||||
assert ( len > 0 );
|
||||
while ( ! uart_data_ready ( &gdbserial_uart ) ) {}
|
||||
buf[0] = uart_receive ( &gdbserial_uart );
|
||||
while ( ! uart_data_ready ( gdbserial_uart ) ) {}
|
||||
buf[0] = uart_receive ( gdbserial_uart );
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void gdbserial_send ( const char *buf, size_t len ) {
|
||||
|
||||
while ( len-- > 0 ) {
|
||||
uart_transmit ( &gdbserial_uart, *buf++ );
|
||||
uart_transmit ( gdbserial_uart, *buf++ );
|
||||
}
|
||||
}
|
||||
|
||||
static int gdbserial_init ( int argc, char **argv ) {
|
||||
unsigned int port;
|
||||
char *endp;
|
||||
const char *port;
|
||||
|
||||
if ( argc == 0 ) {
|
||||
port = GDBSERIAL_PORT;
|
||||
} else if ( argc == 1 ) {
|
||||
port = strtoul ( argv[0], &endp, 10 );
|
||||
if ( *endp ) {
|
||||
printf ( "serial: invalid port\n" );
|
||||
return 1;
|
||||
}
|
||||
if ( argc == 1 ) {
|
||||
port = argv[0];
|
||||
} else {
|
||||
printf ( "serial: syntax <port>\n" );
|
||||
return 1;
|
||||
@@ -98,14 +83,19 @@ struct gdb_transport serial_gdb_transport __gdb_transport = {
|
||||
.send = gdbserial_send,
|
||||
};
|
||||
|
||||
struct gdb_transport * gdbserial_configure ( unsigned int port,
|
||||
struct gdb_transport * gdbserial_configure ( const char *name,
|
||||
unsigned int baud ) {
|
||||
int rc;
|
||||
|
||||
if ( ( rc = uart_select ( &gdbserial_uart, port ) ) != 0 )
|
||||
return NULL;
|
||||
uart_put ( gdbserial_uart );
|
||||
gdbserial_uart = NULL;
|
||||
|
||||
if ( ( rc = uart_init ( &gdbserial_uart, baud ) ) != 0 )
|
||||
gdbserial_uart = uart_find ( name );
|
||||
if ( ! gdbserial_uart )
|
||||
return NULL;
|
||||
uart_get ( gdbserial_uart );
|
||||
|
||||
if ( ( rc = uart_init ( gdbserial_uart, baud ) ) != 0 )
|
||||
return NULL;
|
||||
|
||||
return &serial_gdb_transport;
|
||||
|
||||
@@ -35,6 +35,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
#include <ipxe/uart.h>
|
||||
#include <ipxe/console.h>
|
||||
#include <ipxe/serial.h>
|
||||
#include <ipxe/ns16550.h>
|
||||
#include <config/console.h>
|
||||
#include <config/serial.h>
|
||||
|
||||
@@ -44,22 +45,21 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
#define CONSOLE_SERIAL ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
|
||||
#endif
|
||||
|
||||
/* UART port number */
|
||||
#ifdef COMCONSOLE
|
||||
#define CONSOLE_PORT COMCONSOLE
|
||||
#else
|
||||
#define CONSOLE_PORT 0
|
||||
/* Serial console UART */
|
||||
#ifndef COMCONSOLE
|
||||
#define COMCONSOLE NULL
|
||||
#endif
|
||||
|
||||
/* UART baud rate */
|
||||
#ifdef COMPRESERVE
|
||||
#define CONSOLE_BAUD 0
|
||||
#else
|
||||
#define CONSOLE_BAUD COMSPEED
|
||||
/* Serial console baud rate */
|
||||
#ifndef COMSPEED
|
||||
#define COMSPEED 0
|
||||
#endif
|
||||
|
||||
/** Serial console UART */
|
||||
struct uart serial_console;
|
||||
/** Default serial console UART */
|
||||
static struct uart * const default_serial_console = COMCONSOLE;
|
||||
|
||||
/** Active serial console UART */
|
||||
struct uart *serial_console;
|
||||
|
||||
/**
|
||||
* Print a character to serial console
|
||||
@@ -69,11 +69,11 @@ struct uart serial_console;
|
||||
static void serial_putchar ( int character ) {
|
||||
|
||||
/* Do nothing if we have no UART */
|
||||
if ( ! serial_console.base )
|
||||
if ( ! serial_console )
|
||||
return;
|
||||
|
||||
/* Transmit character */
|
||||
uart_transmit ( &serial_console, character );
|
||||
uart_transmit ( serial_console, character );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,14 +85,14 @@ static int serial_getchar ( void ) {
|
||||
uint8_t data;
|
||||
|
||||
/* Do nothing if we have no UART */
|
||||
if ( ! serial_console.base )
|
||||
if ( ! serial_console )
|
||||
return 0;
|
||||
|
||||
/* Wait for data to be ready */
|
||||
while ( ! uart_data_ready ( &serial_console ) ) {}
|
||||
while ( ! uart_data_ready ( serial_console ) ) {}
|
||||
|
||||
/* Receive data */
|
||||
data = uart_receive ( &serial_console );
|
||||
data = uart_receive ( serial_console );
|
||||
|
||||
/* Strip any high bit and convert DEL to backspace */
|
||||
data &= 0x7f;
|
||||
@@ -111,11 +111,11 @@ static int serial_getchar ( void ) {
|
||||
static int serial_iskey ( void ) {
|
||||
|
||||
/* Do nothing if we have no UART */
|
||||
if ( ! serial_console.base )
|
||||
if ( ! serial_console )
|
||||
return 0;
|
||||
|
||||
/* Check UART */
|
||||
return uart_data_ready ( &serial_console );
|
||||
return uart_data_ready ( serial_console );
|
||||
}
|
||||
|
||||
/** Serial console */
|
||||
@@ -128,25 +128,23 @@ struct console_driver serial_console_driver __console_driver = {
|
||||
|
||||
/** Initialise serial console */
|
||||
static void serial_init ( void ) {
|
||||
struct uart *uart = default_serial_console;
|
||||
int rc;
|
||||
|
||||
/* Do nothing if we have no default port */
|
||||
if ( ! CONSOLE_PORT )
|
||||
if ( ! uart )
|
||||
return;
|
||||
|
||||
/* Select UART */
|
||||
if ( ( rc = uart_select ( &serial_console, CONSOLE_PORT ) ) != 0 ) {
|
||||
DBG ( "Could not select UART %d: %s\n",
|
||||
CONSOLE_PORT, strerror ( rc ) );
|
||||
return;
|
||||
}
|
||||
|
||||
/* Initialise UART */
|
||||
if ( ( rc = uart_init ( &serial_console, CONSOLE_BAUD ) ) != 0 ) {
|
||||
DBG ( "Could not initialise UART %d baud %d: %s\n",
|
||||
CONSOLE_PORT, CONSOLE_BAUD, strerror ( rc ) );
|
||||
if ( ( rc = uart_init ( uart, COMSPEED ) ) != 0 ) {
|
||||
DBGC ( uart, "SERIAL could not initialise %s baud %d: %s\n",
|
||||
uart->name, COMSPEED, strerror ( rc ) );
|
||||
return;
|
||||
}
|
||||
|
||||
/* Record UART as serial console */
|
||||
serial_console = uart;
|
||||
DBGC ( uart, "SERIAL using %s\n", uart->name );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,11 +155,11 @@ static void serial_init ( void ) {
|
||||
static void serial_shutdown ( int flags __unused ) {
|
||||
|
||||
/* Do nothing if we have no UART */
|
||||
if ( ! serial_console.base )
|
||||
if ( ! serial_console )
|
||||
return;
|
||||
|
||||
/* Flush any pending output */
|
||||
uart_flush ( &serial_console );
|
||||
uart_flush ( serial_console );
|
||||
|
||||
/* Leave console enabled; it's still usable */
|
||||
}
|
||||
|
||||
186
src/core/uart.c
186
src/core/uart.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
* Copyright (C) 2025 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
@@ -25,125 +25,139 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
/** @file
|
||||
*
|
||||
* 16550-compatible UART
|
||||
* Generic UARTs
|
||||
*
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <strings.h>
|
||||
#include <errno.h>
|
||||
#include <ipxe/uart.h>
|
||||
|
||||
/** Timeout for transmit holding register to become empty */
|
||||
#define UART_THRE_TIMEOUT_MS 100
|
||||
/** List of registered UARTs */
|
||||
LIST_HEAD ( uarts );
|
||||
|
||||
/** Timeout for transmitter to become empty */
|
||||
#define UART_TEMT_TIMEOUT_MS 1000
|
||||
static void null_uart_transmit ( struct uart *uart __unused,
|
||||
uint8_t byte __unused ) {
|
||||
}
|
||||
|
||||
static int null_uart_data_ready ( struct uart *uart __unused ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t null_uart_receive ( struct uart *uart __unused ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int null_uart_init ( struct uart *uart __unused,
|
||||
unsigned int baud __unused ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void null_uart_flush ( struct uart *uart __unused ) {
|
||||
}
|
||||
|
||||
/** Null UART operations */
|
||||
struct uart_operations null_uart_operations = {
|
||||
.transmit = null_uart_transmit,
|
||||
.data_ready = null_uart_data_ready,
|
||||
.receive = null_uart_receive,
|
||||
.init = null_uart_init,
|
||||
.flush = null_uart_flush,
|
||||
};
|
||||
|
||||
/**
|
||||
* Transmit data
|
||||
* Allocate UART
|
||||
*
|
||||
* @v uart UART
|
||||
* @v data Data
|
||||
* @v priv_len Length of private data
|
||||
* @ret uart UART, or NULL on error
|
||||
*/
|
||||
void uart_transmit ( struct uart *uart, uint8_t data ) {
|
||||
unsigned int i;
|
||||
uint8_t lsr;
|
||||
struct uart * alloc_uart ( size_t priv_len ) {
|
||||
struct uart *uart;
|
||||
|
||||
/* Wait for transmitter holding register to become empty */
|
||||
for ( i = 0 ; i < UART_THRE_TIMEOUT_MS ; i++ ) {
|
||||
lsr = uart_read ( uart, UART_LSR );
|
||||
if ( lsr & UART_LSR_THRE )
|
||||
break;
|
||||
mdelay ( 1 );
|
||||
}
|
||||
/* Allocate and initialise UART */
|
||||
uart = zalloc ( sizeof ( *uart ) + priv_len );
|
||||
if ( ! uart )
|
||||
return NULL;
|
||||
uart->priv = ( ( ( void * ) uart ) + sizeof ( *uart ) );
|
||||
|
||||
/* Transmit data (even if we timed out) */
|
||||
uart_write ( uart, UART_THR, data );
|
||||
return uart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush data
|
||||
* Register fixed UARTs (when not provided by platform)
|
||||
*
|
||||
* @v uart UART
|
||||
*/
|
||||
void uart_flush ( struct uart *uart ) {
|
||||
unsigned int i;
|
||||
uint8_t lsr;
|
||||
|
||||
/* Wait for transmitter and receiver to become empty */
|
||||
for ( i = 0 ; i < UART_TEMT_TIMEOUT_MS ; i++ ) {
|
||||
uart_read ( uart, UART_RBR );
|
||||
lsr = uart_read ( uart, UART_LSR );
|
||||
if ( ( lsr & UART_LSR_TEMT ) && ! ( lsr & UART_LSR_DR ) )
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for existence of UART
|
||||
*
|
||||
* @v uart UART
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
int uart_exists ( struct uart *uart ) {
|
||||
|
||||
/* Fail if no UART port is defined */
|
||||
if ( ! uart->base )
|
||||
return -ENODEV;
|
||||
|
||||
/* Fail if UART scratch register seems not to be present */
|
||||
uart_write ( uart, UART_SCR, 0x18 );
|
||||
if ( uart_read ( uart, UART_SCR ) != 0x18 )
|
||||
return -ENODEV;
|
||||
uart_write ( uart, UART_SCR, 0xae );
|
||||
if ( uart_read ( uart, UART_SCR ) != 0xae )
|
||||
return -ENODEV;
|
||||
__weak int uart_register_fixed ( void ) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise UART
|
||||
* Register UART
|
||||
*
|
||||
* @v uart UART
|
||||
* @v baud Baud rate, or zero to leave unchanged
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
int uart_init ( struct uart *uart, unsigned int baud ) {
|
||||
uint8_t dlm;
|
||||
uint8_t dll;
|
||||
int uart_register ( struct uart *uart ) {
|
||||
|
||||
/* Add to list of registered UARTs */
|
||||
uart_get ( uart );
|
||||
list_add_tail ( &uart->list, &uarts );
|
||||
DBGC ( uart, "UART %s registered\n", uart->name );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister UART
|
||||
*
|
||||
* @v uart UART
|
||||
*/
|
||||
void uart_unregister ( struct uart *uart ) {
|
||||
|
||||
/* Remove from list of registered UARTs */
|
||||
list_del ( &uart->list );
|
||||
uart_put ( uart );
|
||||
}
|
||||
|
||||
/**
|
||||
* Find named UART
|
||||
*
|
||||
* @v name UART name
|
||||
* @ret uart UART, or NULL if not found
|
||||
*/
|
||||
struct uart * uart_find ( const char *name ) {
|
||||
struct uart *uart;
|
||||
unsigned int index;
|
||||
char *endp;
|
||||
int rc;
|
||||
|
||||
/* Check for existence of UART */
|
||||
if ( ( rc = uart_exists ( uart ) ) != 0 )
|
||||
return rc;
|
||||
|
||||
/* Configure divisor and line control register, if applicable */
|
||||
uart_write ( uart, UART_LCR, ( UART_LCR_8N1 | UART_LCR_DLAB ) );
|
||||
if ( baud ) {
|
||||
uart->divisor = ( UART_MAX_BAUD / baud );
|
||||
dlm = ( ( uart->divisor >> 8 ) & 0xff );
|
||||
dll = ( ( uart->divisor >> 0 ) & 0xff );
|
||||
uart_write ( uart, UART_DLM, dlm );
|
||||
uart_write ( uart, UART_DLL, dll );
|
||||
} else {
|
||||
dlm = uart_read ( uart, UART_DLM );
|
||||
dll = uart_read ( uart, UART_DLL );
|
||||
uart->divisor = ( ( dlm << 8 ) | dll );
|
||||
/* Register fixed platform UARTs if not already registered */
|
||||
if ( list_empty ( &uarts ) ) {
|
||||
if ( ( rc = uart_register_fixed() ) != 0 ) {
|
||||
DBGC ( &uarts, "UART could not register fixed UARTs: "
|
||||
"%s\n", strerror ( rc ) );
|
||||
/* Continue anyway */
|
||||
}
|
||||
}
|
||||
uart_write ( uart, UART_LCR, UART_LCR_8N1 );
|
||||
|
||||
/* Disable interrupts */
|
||||
uart_write ( uart, UART_IER, 0 );
|
||||
/* Try parsing name as a numeric index */
|
||||
index = strtoul ( name, &endp, 10 );
|
||||
|
||||
/* Enable FIFOs */
|
||||
uart_write ( uart, UART_FCR, UART_FCR_FE );
|
||||
/* Find matching UART, if any */
|
||||
list_for_each_entry ( uart, &uarts, list ) {
|
||||
|
||||
/* Assert DTR and RTS */
|
||||
uart_write ( uart, UART_MCR, ( UART_MCR_DTR | UART_MCR_RTS ) );
|
||||
/* Check for a matching name */
|
||||
if ( strcasecmp ( name, uart->name ) == 0 )
|
||||
return uart;
|
||||
|
||||
/* Flush any stale data */
|
||||
uart_flush ( uart );
|
||||
/* Check for a matching numeric index */
|
||||
if ( ( *endp == '\0' ) && ( index-- == 0 ) )
|
||||
return uart;
|
||||
}
|
||||
|
||||
return 0;
|
||||
DBGC ( &uarts, "UART %s not found\n", name );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user