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