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