2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
33 #include <ipxe/init.h>
34 #include <ipxe/uart.h>
35 #include <ipxe/console.h>
36 #include <ipxe/serial.h>
37 #include <config/console.h>
38 #include <config/serial.h>
40 /* Set default console usage if applicable */
41 #if ! ( defined ( CONSOLE_SERIAL ) && CONSOLE_EXPLICIT ( CONSOLE_SERIAL ) )
43 #define CONSOLE_SERIAL ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
46 /* UART port number */
48 #define CONSOLE_PORT COMCONSOLE
50 #define CONSOLE_PORT 0
55 #define CONSOLE_BAUD 0
57 #define CONSOLE_BAUD COMSPEED
60 /* UART line control register value */
64 #define CONSOLE_LCR UART_LCR_WPS ( COMDATA, COMPARITY, COMSTOP )
67 /** Serial console UART */
68 struct uart serial_console;
71 * Print a character to serial console
73 * @v character Character to be printed
75 static void serial_putchar ( int character ) {
77 /* Do nothing if we have no UART */
78 if ( ! serial_console.base )
81 /* Transmit character */
82 uart_transmit ( &serial_console, character );
86 * Get character from serial console
88 * @ret character Character read from console
90 static int serial_getchar ( void ) {
93 /* Do nothing if we have no UART */
94 if ( ! serial_console.base )
97 /* Wait for data to be ready */
98 while ( ! uart_data_ready ( &serial_console ) ) {}
101 data = uart_receive ( &serial_console );
103 /* Strip any high bit and convert DEL to backspace */
112 * Check for character ready to read from serial console
114 * @ret True Character available to read
115 * @ret False No character available to read
117 static int serial_iskey ( void ) {
119 /* Do nothing if we have no UART */
120 if ( ! serial_console.base )
124 return uart_data_ready ( &serial_console );
127 /** Serial console */
128 struct console_driver serial_console_driver __console_driver = {
129 .putchar = serial_putchar,
130 .getchar = serial_getchar,
131 .iskey = serial_iskey,
132 .usage = CONSOLE_SERIAL,
135 /** Initialise serial console */
136 static void serial_init ( void ) {
139 /* Do nothing if we have no default port */
140 if ( ! CONSOLE_PORT )
144 if ( ( rc = uart_select ( &serial_console, CONSOLE_PORT ) ) != 0 ) {
145 DBG ( "Could not select UART %d: %s\n",
146 CONSOLE_PORT, strerror ( rc ) );
150 /* Initialise UART */
151 if ( ( rc = uart_init ( &serial_console, CONSOLE_BAUD,
152 CONSOLE_LCR ) ) != 0 ) {
153 DBG ( "Could not initialise UART %d baud %d LCR %#02x: %s\n",
154 CONSOLE_PORT, CONSOLE_BAUD, CONSOLE_LCR, strerror ( rc ));
160 * Shut down serial console
162 * @v flags Shutdown flags
164 static void serial_shutdown ( int flags __unused ) {
166 /* Do nothing if we have no UART */
167 if ( ! serial_console.base )
170 /* Flush any pending output */
171 uart_flush ( &serial_console );
173 /* Leave console enabled; it's still usable */
176 /** Serial console initialisation function */
177 struct init_fn serial_console_init_fn __init_fn ( INIT_CONSOLE ) = {
178 .initialise = serial_init,
181 /** Serial console startup function */
182 struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
183 .shutdown = serial_shutdown,