[sundance] Add reset completion check
[gpxe.git] / src / core / serial.c
blob5b3be39c346b4139f7587f796234c4bcdf936cf5
1 /*
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().
14 #include "stddef.h"
15 #include <gpxe/init.h>
16 #include <gpxe/io.h>
17 #include <unistd.h>
18 #include <gpxe/serial.h>
19 #include "config/serial.h"
21 /* Set default values if none specified */
23 #ifndef COMCONSOLE
24 #define COMCONSOLE 0x3f8
25 #endif
27 #ifndef COMSPEED
28 #define COMSPEED 9600
29 #endif
31 #ifndef COMDATA
32 #define COMDATA 8
33 #endif
35 #ifndef COMPARITY
36 #define COMPARITY 0
37 #endif
39 #ifndef COMSTOP
40 #define COMSTOP 1
41 #endif
43 #undef UART_BASE
44 #define UART_BASE ( COMCONSOLE )
46 #undef UART_BAUD
47 #define UART_BAUD ( COMSPEED )
49 #if ((115200%UART_BAUD) != 0)
50 #error Bad ttys0 baud rate
51 #endif
53 #define COMBRD (115200/UART_BAUD)
55 /* Line Control Settings */
56 #define UART_LCS ( ( ( (COMDATA) - 5 ) << 0 ) | \
57 ( ( (COMPARITY) ) << 3 ) | \
58 ( ( (COMSTOP) - 1 ) << 2 ) )
60 /* Data */
61 #define UART_RBR 0x00
62 #define UART_TBR 0x00
64 /* Control */
65 #define UART_IER 0x01
66 #define UART_IIR 0x02
67 #define UART_FCR 0x02
68 #define UART_LCR 0x03
69 #define UART_MCR 0x04
70 #define UART_DLL 0x00
71 #define UART_DLM 0x01
73 /* Status */
74 #define UART_LSR 0x05
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 */
83 #define UART_MSR 0x06
84 #define UART_SCR 0x07
86 #if defined(UART_MEM)
87 #define uart_readb(addr) readb((addr))
88 #define uart_writeb(val,addr) writeb((val),(addr))
89 #else
90 #define uart_readb(addr) inb((addr))
91 #define uart_writeb(val,addr) outb((val),(addr))
92 #endif
95 * void serial_putc(int ch);
96 * Write character `ch' to port UART_BASE.
98 void serial_putc ( int ch ) {
99 int i;
100 int status;
101 i = 1000; /* timeout */
102 while(--i > 0) {
103 status = uart_readb(UART_BASE + UART_LSR);
104 if (status & UART_LSR_THRE) {
105 /* TX buffer emtpy */
106 uart_writeb(ch, UART_BASE + UART_TBR);
107 break;
109 mdelay(2);
114 * int serial_getc(void);
115 * Read a character from port UART_BASE.
117 int serial_getc ( void ) {
118 int status;
119 int ch;
120 do {
121 status = uart_readb(UART_BASE + UART_LSR);
122 } while((status & 1) == 0);
123 ch = uart_readb(UART_BASE + UART_RBR); /* fetch (first) character */
124 ch &= 0x7f; /* remove any parity bits we get */
125 if (ch == 0x7f) { /* Make DEL... look like BS */
126 ch = 0x08;
128 return ch;
132 * int serial_ischar(void);
133 * If there is a character in the input buffer of port UART_BASE,
134 * return nonzero; otherwise return 0.
136 int serial_ischar ( void ) {
137 int status;
138 status = uart_readb(UART_BASE + UART_LSR); /* line status reg; */
139 return status & 1; /* rx char available */
143 * int serial_init(void);
144 * Initialize port UART_BASE to speed COMSPEED, line settings 8N1.
146 static void serial_init ( void ) {
147 int status;
148 int divisor, lcs;
150 DBG ( "Serial port %#x initialising\n", UART_BASE );
152 divisor = COMBRD;
153 lcs = UART_LCS;
156 #ifdef COMPRESERVE
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);
161 #endif
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) {
169 DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
170 goto out;
172 uart_writeb(0x55, UART_BASE + UART_DLL);
173 if (uart_readb(UART_BASE + UART_DLL) != 0x55) {
174 DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
175 goto out;
177 uart_writeb(divisor & 0xff, UART_BASE + UART_DLL);
178 if (uart_readb(UART_BASE + UART_DLL) != (divisor & 0xff)) {
179 DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
180 goto out;
182 uart_writeb(0xaa, UART_BASE + UART_DLM);
183 if (uart_readb(UART_BASE + UART_DLM) != 0xaa) {
184 DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
185 goto out;
187 uart_writeb(0x55, UART_BASE + UART_DLM);
188 if (uart_readb(UART_BASE + UART_DLM) != 0x55) {
189 DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
190 goto out;
192 uart_writeb((divisor >> 8) & 0xff, UART_BASE + UART_DLM);
193 if (uart_readb(UART_BASE + UART_DLM) != ((divisor >> 8) & 0xff)) {
194 DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
195 goto out;
197 uart_writeb(lcs, UART_BASE + UART_LCR);
199 /* disable interrupts */
200 uart_writeb(0x0, UART_BASE + UART_IER);
202 /* disable fifo's */
203 uart_writeb(0x00, UART_BASE + UART_FCR);
205 /* Set clear to send, so flow control works... */
206 uart_writeb((1<<1), UART_BASE + UART_MCR);
209 /* Flush the input buffer. */
210 do {
211 /* rx buffer reg
212 * throw away (unconditionally the first time)
214 (void) uart_readb(UART_BASE + UART_RBR);
215 /* line status reg */
216 status = uart_readb(UART_BASE + UART_LSR);
217 } while(status & UART_LSR_DR);
218 out:
219 return;
223 * void serial_fini(void);
224 * Cleanup our use of the serial port, in particular flush the
225 * output buffer so we don't accidentially lose characters.
227 static void serial_fini ( int flags __unused ) {
228 int i, status;
229 /* Flush the output buffer to avoid dropping characters,
230 * if we are reinitializing the serial port.
232 i = 10000; /* timeout */
233 do {
234 status = uart_readb(UART_BASE + UART_LSR);
235 } while((--i > 0) && !(status & UART_LSR_TEMPT));
236 /* Don't mark it as disabled; it's still usable */
240 * Serial driver initialisation function
242 * Initialise serial port early on so that it is available to capture
243 * early debug messages.
245 struct init_fn serial_init_fn __init_fn ( INIT_SERIAL ) = {
246 .initialise = serial_init,
249 /** Serial driver startup function */
250 struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
251 .shutdown = serial_fini,