These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / core / serial.c
1 /*
2  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27  *
28  * Serial console
29  *
30  */
31
32 #include <stddef.h>
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>
39
40 /* Set default console usage if applicable */
41 #if ! ( defined ( CONSOLE_SERIAL ) && CONSOLE_EXPLICIT ( CONSOLE_SERIAL ) )
42 #undef CONSOLE_SERIAL
43 #define CONSOLE_SERIAL ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
44 #endif
45
46 /* UART port number */
47 #ifdef COMCONSOLE
48 #define CONSOLE_PORT COMCONSOLE
49 #else
50 #define CONSOLE_PORT 0
51 #endif
52
53 /* UART baud rate */
54 #ifdef COMPRESERVE
55 #define CONSOLE_BAUD 0
56 #else
57 #define CONSOLE_BAUD COMSPEED
58 #endif
59
60 /* UART line control register value */
61 #ifdef COMPRESERVE
62 #define CONSOLE_LCR 0
63 #else
64 #define CONSOLE_LCR UART_LCR_WPS ( COMDATA, COMPARITY, COMSTOP )
65 #endif
66
67 /** Serial console UART */
68 struct uart serial_console;
69
70 /**
71  * Print a character to serial console
72  *
73  * @v character         Character to be printed
74  */
75 static void serial_putchar ( int character ) {
76
77         /* Do nothing if we have no UART */
78         if ( ! serial_console.base )
79                 return;
80
81         /* Transmit character */
82         uart_transmit ( &serial_console, character );
83 }
84
85 /**
86  * Get character from serial console
87  *
88  * @ret character       Character read from console
89  */
90 static int serial_getchar ( void ) {
91         uint8_t data;
92
93         /* Do nothing if we have no UART */
94         if ( ! serial_console.base )
95                 return 0;
96
97         /* Wait for data to be ready */
98         while ( ! uart_data_ready ( &serial_console ) ) {}
99
100         /* Receive data */
101         data = uart_receive ( &serial_console );
102
103         /* Strip any high bit and convert DEL to backspace */
104         data &= 0x7f;
105         if ( data == 0x7f )
106                 data = 0x08;
107
108         return data;
109 }
110
111 /**
112  * Check for character ready to read from serial console
113  *
114  * @ret True            Character available to read
115  * @ret False           No character available to read
116  */
117 static int serial_iskey ( void ) {
118
119         /* Do nothing if we have no UART */
120         if ( ! serial_console.base )
121                 return 0;
122
123         /* Check UART */
124         return uart_data_ready ( &serial_console );
125 }
126
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,
133 };
134
135 /** Initialise serial console */
136 static void serial_init ( void ) {
137         int rc;
138
139         /* Do nothing if we have no default port */
140         if ( ! CONSOLE_PORT )
141                 return;
142
143         /* Select UART */
144         if ( ( rc = uart_select ( &serial_console, CONSOLE_PORT ) ) != 0 ) {
145                 DBG ( "Could not select UART %d: %s\n",
146                       CONSOLE_PORT, strerror ( rc ) );
147                 return;
148         }
149
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 ));
155                 return;
156         }
157 }
158
159 /**
160  * Shut down serial console
161  *
162  * @v flags             Shutdown flags
163  */
164 static void serial_shutdown ( int flags __unused ) {
165
166         /* Do nothing if we have no UART */
167         if ( ! serial_console.base )
168                 return;
169
170         /* Flush any pending output */
171         uart_flush ( &serial_console );
172
173         /* Leave console enabled; it's still usable */
174 }
175
176 /** Serial console initialisation function */
177 struct init_fn serial_console_init_fn __init_fn ( INIT_CONSOLE ) = {
178         .initialise = serial_init,
179 };
180
181 /** Serial console startup function */
182 struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
183         .shutdown = serial_shutdown,
184 };