drop excessive newline in uart8250.c
[coreboot.git] / src / lib / uart8250.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2003 Eric Biederman
5  * Copyright (C) 2006-2010 coresystems GmbH
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <arch/io.h>
22 #include <uart8250.h>
23 #include <pc80/mc146818rtc.h>
24 #if CONFIG_USE_OPTION_TABLE
25 #include "option_table.h"
26 #endif
27
28 /* Should support 8250, 16450, 16550, 16550A type UARTs */
29
30 static inline int uart8250_can_tx_byte(unsigned base_port)
31 {
32         return inb(base_port + UART_LSR) & UART_MSR_DSR;
33 }
34
35 static inline void uart8250_wait_to_tx_byte(unsigned base_port)
36 {
37         while(!uart8250_can_tx_byte(base_port))
38                 ;
39 }
40
41 static inline void uart8250_wait_until_sent(unsigned base_port)
42 {
43         while(!(inb(base_port + UART_LSR) & UART_LSR_TEMT))
44                 ;
45 }
46
47 void uart8250_tx_byte(unsigned base_port, unsigned char data)
48 {
49         uart8250_wait_to_tx_byte(base_port);
50         outb(data, base_port + UART_TBR);
51         /* Make certain the data clears the fifos */
52         uart8250_wait_until_sent(base_port);
53 }
54
55 int uart8250_can_rx_byte(unsigned base_port)
56 {
57         return inb(base_port + UART_LSR) & UART_LSR_DR;
58 }
59
60 unsigned char uart8250_rx_byte(unsigned base_port)
61 {
62         while(!uart8250_can_rx_byte(base_port))
63                 ;
64         return inb(base_port + UART_RBR);
65 }
66
67 void uart8250_init(unsigned base_port, unsigned divisor)
68 {
69         /* Disable interrupts */
70         outb(0x0, base_port + UART_IER);
71         /* Enable FIFOs */
72         outb(UART_FCR_FIFO_EN, base_port + UART_FCR);
73
74         /* assert DTR and RTS so the other end is happy */
75         outb(UART_MCR_DTR | UART_MCR_RTS, base_port + UART_MCR);
76
77         /* DLAB on */
78         outb(UART_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART_LCR);
79
80         /* Set Baud Rate Divisor. 12 ==> 115200 Baud */
81         outb(divisor & 0xFF,   base_port + UART_DLL);
82         outb((divisor >> 8) & 0xFF,    base_port + UART_DLM);
83
84         /* Set to 3 for 8N1 */
85         outb(CONFIG_TTYS0_LCS, base_port + UART_LCR);
86 }
87
88 #if defined(__PRE_RAM__) || defined(__SMM__)
89 void uart_init(void)
90 {
91 #if CONFIG_USE_OPTION_TABLE && !defined(__SMM__)
92         static const unsigned char divisor[] = { 1, 2, 3, 6, 12, 24, 48, 96 };
93         unsigned ttys0_div, ttys0_index;
94         ttys0_index = read_option(CMOS_VSTART_baud_rate, CMOS_VLEN_baud_rate, 0);
95         ttys0_index &= 7;
96         ttys0_div = divisor[ttys0_index];
97
98         uart8250_init(CONFIG_TTYS0_BASE, ttys0_div);
99 #else
100         uart8250_init(CONFIG_TTYS0_BASE, CONFIG_TTYS0_DIV);
101 #endif
102 }
103 #endif