guard against the case that CONFIG_WAIT_BEFORE_CPUS_INIT is not defined at all.
[coreboot.git] / src / pc80 / serial.c
1 #include <lib.h> /* Prototypes */
2 #include <arch/io.h>
3 #include "pc80/mc146818rtc.h"
4 #if CONFIG_USE_OPTION_TABLE
5 #include "option_table.h"
6 #endif
7
8 /* Base Address */
9 #ifndef CONFIG_TTYS0_BASE
10 #define CONFIG_TTYS0_BASE 0x3f8
11 #endif
12
13 #ifndef CONFIG_TTYS0_BAUD
14 #define CONFIG_TTYS0_BAUD 115200
15 #endif
16
17 #if ((115200%CONFIG_TTYS0_BAUD) != 0)
18 #error Bad ttys0 baud rate
19 #endif
20
21 #ifndef CONFIG_TTYS0_DIV
22 #define CONFIG_TTYS0_DIV        (115200/CONFIG_TTYS0_BAUD)
23 #endif
24
25 /* Line Control Settings */
26 #ifndef CONFIG_TTYS0_LCS
27 /* Set 8bit, 1 stop bit, no parity */
28 #define CONFIG_TTYS0_LCS        0x3
29 #endif
30
31 #define UART_LCS        CONFIG_TTYS0_LCS
32
33
34 #if CONFIG_CACHE_AS_RAM == 0
35
36 /* Data */
37 #define UART_RBR 0x00
38 #define UART_TBR 0x00
39
40 /* Control */
41 #define UART_IER 0x01
42 #define UART_IIR 0x02
43 #define UART_FCR 0x02
44 #define UART_LCR 0x03
45 #define UART_MCR 0x04
46 #define UART_DLL 0x00
47 #define UART_DLM 0x01
48
49 /* Status */
50 #define UART_LSR 0x05
51 #define UART_MSR 0x06
52 #define UART_SCR 0x07
53
54 static int uart_can_tx_byte(void)
55 {
56         return inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x20;
57 }
58
59 static void uart_wait_to_tx_byte(void)
60 {
61         while(!uart_can_tx_byte())
62         ;
63 }
64
65 static void uart_wait_until_sent(void)
66 {
67         while(!(inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x40))
68         ;
69 }
70
71 static void uart_tx_byte(unsigned char data)
72 {
73         uart_wait_to_tx_byte();
74         outb(data, CONFIG_TTYS0_BASE + UART_TBR);
75         /* Make certain the data clears the fifos */
76         uart_wait_until_sent();
77 }
78
79 void uart_init(void)
80 {
81         /* disable interrupts */
82         outb(0x0, CONFIG_TTYS0_BASE + UART_IER);
83         /* enable fifo's */
84         outb(0x01, CONFIG_TTYS0_BASE + UART_FCR);
85         /* Set Baud Rate Divisor to 12 ==> 115200 Baud */
86         outb(0x80 | UART_LCS, CONFIG_TTYS0_BASE + UART_LCR);
87 #if CONFIG_USE_OPTION_TABLE
88         static const unsigned char divisor[] = { 1,2,3,6,12,24,48,96 };
89         unsigned ttys0_div, ttys0_index;
90         ttys0_index = read_option(CMOS_VSTART_baud_rate, CMOS_VLEN_baud_rate, 0);
91         ttys0_index &= 7;
92         ttys0_div = divisor[ttys0_index];
93         outb(ttys0_div & 0xff, CONFIG_TTYS0_BASE + UART_DLL);
94         outb(0, CONFIG_TTYS0_BASE + UART_DLM);
95 #else
96         outb(CONFIG_TTYS0_DIV & 0xFF,   CONFIG_TTYS0_BASE + UART_DLL);
97         outb((CONFIG_TTYS0_DIV >> 8) & 0xFF,    CONFIG_TTYS0_BASE + UART_DLM);
98 #endif
99         outb(UART_LCS, CONFIG_TTYS0_BASE + UART_LCR);
100 }
101
102 #else
103 /* CONFIG_CACHE_AS_RAM == 1 */
104
105 extern void uart8250_init(unsigned base_port, unsigned divisor, unsigned lcs);
106 void uart_init(void)
107 {
108 #if CONFIG_USE_OPTION_TABLE
109         static const unsigned char divisor[] = { 1,2,3,6,12,24,48,96 };
110         unsigned ttys0_div, ttys0_index;
111         ttys0_index = read_option(CMOS_VSTART_baud_rate, CMOS_VLEN_baud_rate, 0);
112         ttys0_index &= 7;
113         ttys0_div = divisor[ttys0_index];
114         uart8250_init(CONFIG_TTYS0_BASE, ttys0_div, UART_LCS);
115 #else
116         uart8250_init(CONFIG_TTYS0_BASE, CONFIG_TTYS0_DIV, UART_LCS);
117 #endif
118 }
119 #endif