- Remove all of the annoying $Id strings
[coreboot.git] / src / lib / uart8250.c
1 /* Should support 8250, 16450, 16550, 16550A type uarts */
2 #include <arch/io.h>
3 #include <uart8250.h>
4
5 /* Data */
6 #define UART_RBR 0x00
7 #define UART_TBR 0x00
8
9 /* Control */
10 #define UART_IER 0x01
11 #define UART_IIR 0x02
12 #define UART_FCR 0x02
13 #define UART_LCR 0x03
14 #define UART_MCR 0x04
15 #define UART_DLL 0x00
16 #define UART_DLM 0x01
17
18 /* Status */
19 #define UART_LSR 0x05
20 #define UART_MSR 0x06
21 #define UART_SCR 0x07
22
23 static inline int uart8250_can_tx_byte(unsigned base_port)
24 {
25         return inb(base_port + UART_LSR) & 0x20;
26 }
27
28 static inline void uart8250_wait_to_tx_byte(unsigned base_port)
29 {
30         while(!uart8250_can_tx_byte(base_port))
31                 ;
32 }
33
34 static inline void uart8250_wait_until_sent(unsigned base_port)
35 {
36         while(!(inb(base_port + UART_LSR) & 0x40)) 
37                 ;
38 }
39
40 void uart8250_tx_byte(unsigned base_port, unsigned char data)
41 {
42         uart8250_wait_to_tx_byte(base_port);
43         outb(data, base_port + UART_TBR);
44         /* Make certain the data clears the fifos */
45         uart8250_wait_until_sent(base_port);
46 }
47
48 void uart8250_init(unsigned base_port, unsigned divisor, unsigned lcs)
49 {
50         lcs &= 0x7f;
51         /* disable interrupts */
52         outb(0x0, base_port + UART_IER);
53         /* enable fifo's */
54         outb(0x01, base_port + UART_FCR);
55         /* Set Baud Rate Divisor to 12 ==> 115200 Baud */
56         outb(0x80 | lcs, base_port + UART_LCR);
57         outb(divisor & 0xFF,   base_port + UART_DLL);
58         outb((divisor >> 8) & 0xFF,    base_port + UART_DLM);
59         outb(lcs, base_port + UART_LCR);
60 }