2 * The serial port interface routines implement a simple polled i/o
3 * interface to a standard serial port. Due to the space restrictions
4 * for the boot blocks, no BIOS support is used (since BIOS requires
5 * expensive real/protected mode switches), instead the rudimentary
6 * BIOS support is duplicated here.
8 * The base address and speed for the i/o port are passed from the
9 * Makefile in the COMCONSOLE and CONSPEED preprocessor macros. The
10 * line control parameters are currently hard-coded to 8 bits, no
11 * parity, 1 stop bit (8N1). This can be changed in init_serial().
19 #include "config/serial.h"
21 /* Set default values if none specified */
24 #define COMCONSOLE 0x3f8
44 #define UART_BASE ( COMCONSOLE )
47 #define UART_BAUD ( COMSPEED )
49 #if ((115200%UART_BAUD) != 0)
50 #error Bad ttys0 baud rate
53 #define COMBRD (115200/UART_BAUD)
55 /* Line Control Settings */
56 #define UART_LCS ( ( ( (COMDATA) - 5 ) << 0 ) | \
57 ( ( (COMPARITY) ) << 3 ) | \
58 ( ( (COMSTOP) - 1 ) << 2 ) )
75 #define UART_LSR_TEMPT 0x40 /* Transmitter empty */
76 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
77 #define UART_LSR_BI 0x10 /* Break interrupt indicator */
78 #define UART_LSR_FE 0x08 /* Frame error indicator */
79 #define UART_LSR_PE 0x04 /* Parity error indicator */
80 #define UART_LSR_OE 0x02 /* Overrun error indicator */
81 #define UART_LSR_DR 0x01 /* Receiver data ready */
87 #define uart_readb(addr) readb((addr))
88 #define uart_writeb(val,addr) writeb((val),(addr))
90 #define uart_readb(addr) inb((addr))
91 #define uart_writeb(val,addr) outb((val),(addr))
94 struct console_driver serial_console;
97 * void serial_putc(int ch);
98 * Write character `ch' to port UART_BASE.
100 static void serial_putc ( int ch ) {
103 i = 1000; /* timeout */
105 status = uart_readb(UART_BASE + UART_LSR);
106 if (status & UART_LSR_THRE) {
107 /* TX buffer emtpy */
108 uart_writeb(ch, UART_BASE + UART_TBR);
116 * int serial_getc(void);
117 * Read a character from port UART_BASE.
119 static int serial_getc ( void ) {
123 status = uart_readb(UART_BASE + UART_LSR);
124 } while((status & 1) == 0);
125 ch = uart_readb(UART_BASE + UART_RBR); /* fetch (first) character */
126 ch &= 0x7f; /* remove any parity bits we get */
127 if (ch == 0x7f) { /* Make DEL... look like BS */
134 * int serial_ischar(void);
135 * If there is a character in the input buffer of port UART_BASE,
136 * return nonzero; otherwise return 0.
138 static int serial_ischar ( void ) {
140 status = uart_readb(UART_BASE + UART_LSR); /* line status reg; */
141 return status & 1; /* rx char available */
145 * int serial_init(void);
146 * Initialize port UART_BASE to speed COMSPEED, line settings 8N1.
148 static void serial_init ( void ) {
157 lcs = uart_readb(UART_BASE + UART_LCR) & 0x7f;
158 uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
159 divisor = (uart_readb(UART_BASE + UART_DLM) << 8) | uart_readb(UART_BASE + UART_DLL);
160 uart_writeb(lcs, UART_BASE + UART_LCR);
163 /* Set Baud Rate Divisor to COMSPEED, and test to see if the
164 * serial port appears to be present.
166 uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
167 uart_writeb(0xaa, UART_BASE + UART_DLL);
168 if (uart_readb(UART_BASE + UART_DLL) != 0xaa)
170 uart_writeb(0x55, UART_BASE + UART_DLL);
171 if (uart_readb(UART_BASE + UART_DLL) != 0x55)
173 uart_writeb(divisor & 0xff, UART_BASE + UART_DLL);
174 if (uart_readb(UART_BASE + UART_DLL) != (divisor & 0xff))
176 uart_writeb(0xaa, UART_BASE + UART_DLM);
177 if (uart_readb(UART_BASE + UART_DLM) != 0xaa)
179 uart_writeb(0x55, UART_BASE + UART_DLM);
180 if (uart_readb(UART_BASE + UART_DLM) != 0x55)
182 uart_writeb((divisor >> 8) & 0xff, UART_BASE + UART_DLM);
183 if (uart_readb(UART_BASE + UART_DLM) != ((divisor >> 8) & 0xff))
185 uart_writeb(lcs, UART_BASE + UART_LCR);
187 /* disable interrupts */
188 uart_writeb(0x0, UART_BASE + UART_IER);
191 uart_writeb(0x00, UART_BASE + UART_FCR);
193 /* Set clear to send, so flow control works... */
194 uart_writeb((1<<1), UART_BASE + UART_MCR);
197 /* Flush the input buffer. */
200 * throw away (unconditionally the first time)
202 uart_readb(UART_BASE + UART_RBR);
203 /* line status reg */
204 status = uart_readb(UART_BASE + UART_LSR);
205 } while(status & UART_LSR_DR);
206 serial_console.disabled = 0;
212 * void serial_fini(void);
213 * Cleanup our use of the serial port, in particular flush the
214 * output buffer so we don't accidentially loose characters.
216 static void serial_fini ( void ) {
218 if (serial_console.disabled) {
219 /* no serial interface */
222 /* Flush the output buffer to avoid dropping characters,
223 * if we are reinitializing the serial port.
225 i = 10000; /* timeout */
227 status = uart_readb(UART_BASE + UART_LSR);
228 } while((--i > 0) && !(status & UART_LSR_TEMPT));
229 /* Don't mark it as disabled; it's still usable */
232 struct console_driver serial_console __console_driver = {
233 .putchar = serial_putc,
234 .getchar = serial_getc,
235 .iskey = serial_ischar,
239 INIT_FN ( INIT_CONSOLE, serial_init, NULL, serial_fini );