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