1baaecf2a1c2224d844b8fd65d9c0a9631f3d1ea
[coreboot.git] / src / cpu / x86 / smm / smiutil.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008-2009 coresystems GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; version 2 of
9  * 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,
19  * MA 02110-1301 USA
20  */
21
22 #include <arch/io.h>
23 #include <arch/romcc_io.h>
24 #include <console/console.h>
25 #include <cpu/x86/cache.h>
26 #include <cpu/x86/smm.h>
27
28 /* ********************* smi_util ************************* */
29
30 /* Data */
31 #define UART_RBR 0x00
32 #define UART_TBR 0x00
33
34 /* Control */
35 #define UART_IER 0x01
36 #define UART_IIR 0x02
37 #define UART_FCR 0x02
38 #define UART_LCR 0x03
39 #define UART_MCR 0x04
40 #define UART_DLL 0x00
41 #define UART_DLM 0x01
42
43 /* Status */
44 #define UART_LSR 0x05
45 #define UART_MSR 0x06
46 #define UART_SCR 0x07
47
48 #ifndef CONFIG_TTYS0_BASE
49 #define CONFIG_TTYS0_BASE 0x3f8
50 #endif
51
52 #ifndef CONFIG_TTYS0_BAUD
53 #define CONFIG_TTYS0_BAUD 115200
54 #endif
55
56 #ifndef CONFIG_TTYS0_DIV
57 #define CONFIG_TTYS0_DIV        (115200/CONFIG_TTYS0_BAUD)
58 #endif
59
60 /* Line Control Settings */
61 #ifndef CONFIG_TTYS0_LCS
62 /* Set 8bit, 1 stop bit, no parity */
63 #define CONFIG_TTYS0_LCS        0x3
64 #endif
65
66 #define UART_LCS        CONFIG_TTYS0_LCS
67
68 static int uart_can_tx_byte(void)
69 {
70         return inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x20;
71 }
72
73 static void uart_wait_to_tx_byte(void)
74 {
75         while(!uart_can_tx_byte()) 
76         ;
77 }
78
79 static void uart_wait_until_sent(void)
80 {
81         while(!(inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x40))
82         ; 
83 }
84
85 static void uart_tx_byte(unsigned char data)
86 {
87         uart_wait_to_tx_byte();
88         outb(data, CONFIG_TTYS0_BASE + UART_TBR);
89         /* Make certain the data clears the fifos */
90         uart_wait_until_sent();
91 }
92
93 void console_tx_flush(void)
94 {
95         uart_wait_to_tx_byte();
96 }
97
98 void console_tx_byte(unsigned char byte)
99 {
100         if (byte == '\n')
101                 uart_tx_byte('\r');
102         uart_tx_byte(byte);
103 }
104
105 void uart_init(void)
106 {
107         /* disable interrupts */
108         outb(0x0, CONFIG_TTYS0_BASE + UART_IER);
109         /* enable fifo's */
110         outb(0x01, CONFIG_TTYS0_BASE + UART_FCR);
111         /* Set Baud Rate Divisor to 12 ==> 115200 Baud */
112         outb(0x80 | UART_LCS, CONFIG_TTYS0_BASE + UART_LCR);
113         outb(CONFIG_TTYS0_DIV & 0xFF,   CONFIG_TTYS0_BASE + UART_DLL);
114         outb((CONFIG_TTYS0_DIV >> 8) & 0xFF,    CONFIG_TTYS0_BASE + UART_DLM);
115         outb(UART_LCS, CONFIG_TTYS0_BASE + UART_LCR);
116 }
117
118 void console_init(void)
119 {
120 #if CONFIG_DEBUG_SMI
121         console_loglevel = CONFIG_DEFAULT_CONSOLE_LOGLEVEL;
122         uart_init();
123 #else
124         console_loglevel = 1;
125 #endif
126 }
127
128 /* ********************* smi_util ************************* */