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
);
34 #include <ipxe/init.h>
35 #include <ipxe/uart.h>
36 #include <ipxe/console.h>
37 #include <ipxe/serial.h>
38 #include <config/console.h>
39 #include <config/serial.h>
41 /* Set default console usage if applicable */
42 #if ! ( defined ( CONSOLE_SERIAL ) && CONSOLE_EXPLICIT ( CONSOLE_SERIAL ) )
44 #define CONSOLE_SERIAL ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
47 /* UART port number */
49 #define CONSOLE_PORT COMCONSOLE
51 #define CONSOLE_PORT 0
56 #define CONSOLE_BAUD 0
58 #define CONSOLE_BAUD COMSPEED
61 /* UART line control register value */
65 #define CONSOLE_LCR UART_LCR_WPS ( COMDATA, COMPARITY, COMSTOP )
68 /** Serial console UART */
69 struct uart serial_console
;
72 * Print a character to serial console
74 * @v character Character to be printed
76 static void serial_putchar ( int character
) {
78 /* Do nothing if we have no UART */
79 if ( ! serial_console
.base
)
82 /* Transmit character */
83 uart_transmit ( &serial_console
, character
);
87 * Get character from serial console
89 * @ret character Character read from console
91 static int serial_getchar ( void ) {
94 /* Do nothing if we have no UART */
95 if ( ! serial_console
.base
)
98 /* Wait for data to be ready */
99 while ( ! uart_data_ready ( &serial_console
) ) {}
102 data
= uart_receive ( &serial_console
);
104 /* Strip any high bit and convert DEL to backspace */
113 * Check for character ready to read from serial console
115 * @ret True Character available to read
116 * @ret False No character available to read
118 static int serial_iskey ( void ) {
120 /* Do nothing if we have no UART */
121 if ( ! serial_console
.base
)
125 return uart_data_ready ( &serial_console
);
128 /** Serial console */
129 struct console_driver serial_console_driver __console_driver
= {
130 .putchar
= serial_putchar
,
131 .getchar
= serial_getchar
,
132 .iskey
= serial_iskey
,
133 .usage
= CONSOLE_SERIAL
,
136 /** Initialise serial console */
137 static void serial_init ( void ) {
140 /* Do nothing if we have no default port */
141 if ( ! CONSOLE_PORT
)
145 if ( ( rc
= uart_select ( &serial_console
, CONSOLE_PORT
) ) != 0 ) {
146 DBG ( "Could not select UART %d: %s\n",
147 CONSOLE_PORT
, strerror ( rc
) );
151 /* Initialise UART */
152 if ( ( rc
= uart_init ( &serial_console
, CONSOLE_BAUD
,
153 CONSOLE_LCR
) ) != 0 ) {
154 DBG ( "Could not initialise UART %d baud %d LCR %#02x: %s\n",
155 CONSOLE_PORT
, CONSOLE_BAUD
, CONSOLE_LCR
, strerror ( rc
));
161 * Shut down serial console
163 * @v flags Shutdown flags
165 static void serial_shutdown ( int flags __unused
) {
167 /* Do nothing if we have no UART */
168 if ( ! serial_console
.base
)
171 /* Flush any pending output */
172 uart_flush ( &serial_console
);
174 /* Leave console enabled; it's still usable */
177 /** Serial console initialisation function */
178 struct init_fn serial_console_init_fn
__init_fn ( INIT_CONSOLE
) = {
179 .initialise
= serial_init
,
182 /** Serial console startup function */
183 struct startup_fn serial_startup_fn
__startup_fn ( STARTUP_EARLY
) = {
185 .shutdown
= serial_shutdown
,