cce43ca2fe5e17b9316613cf1bfae0c4a621939a
[coreboot.git] / payloads / libpayload / drivers / serial.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  * Copyright (C) 2008 Ulf Jordan <jordan@chalmers.se>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #include <config.h>
32 #include <libpayload.h>
33
34 #define IOBASE lib_sysinfo.ser_ioport
35
36 #ifdef CONFIG_SERIAL_SET_SPEED
37 #define DIVISOR (115200 / CONFIG_SERIAL_BAUD_RATE)
38 #endif
39
40 void serial_init(void)
41 {
42 #ifdef CONFIG_SERIAL_SET_SPEED
43         unsigned char reg;
44
45         /* Disable interrupts. */
46         outb(0, IOBASE + 0x01);
47
48         /* Assert RTS and DTR. */
49         outb(3, IOBASE + 0x04);
50
51         /* Set the divisor latch. */
52         reg = inb(IOBASE + 0x03);
53         outb(reg | 0x80, IOBASE + 0x03);
54
55         /* Write the divisor. */
56         outb(DIVISOR & 0xFF, IOBASE);
57         outb(DIVISOR >> 8 & 0xFF, IOBASE + 1);
58
59         /* Restore the previous value of the divisor. */
60         outb(reg &= ~0x80, IOBASE + 0x03);
61 #endif
62 }
63
64 void serial_putchar(unsigned char c)
65 {
66         while ((inb(IOBASE + 0x05) & 0x20) == 0) ;
67         outb(c, IOBASE);
68 }
69
70 int serial_havechar(void)
71 {
72         return inb(IOBASE + 0x05) & 0x01;
73 }
74
75 int serial_getchar(void)
76 {
77         while (!serial_havechar()) ;
78         return (int)inb(IOBASE);
79 }
80
81 /*  These are thinly veiled vt100 functions used by curses */
82
83 #define VT100_CLEAR       "\e[H\e[J"
84 #define VT100_SBOLD       "\e[1m"
85 #define VT100_EBOLD       "\e[m"
86 #define VT100_CURSOR_ADDR "\e[%d;%dH"
87 /* The following smacs/rmacs are actually for xterm; a real vt100 has
88    enacs=\E(B\E)0, smacs=^N, rmacs=^O.  */
89 #define VT100_SMACS       "\e(0"
90 #define VT100_RMACS       "\e(B"
91 /* A vt100 doesn't do color, setaf/setab below are from xterm-color. */
92 #define VT100_SET_COLOR   "\e[3%d;4%dm"
93
94 static void serial_putcmd(char *str)
95 {
96         while(*str)
97                 serial_putchar(*(str++));
98 }
99
100 void serial_clear(void)
101 {
102         serial_putcmd(VT100_CLEAR);
103 }
104
105 void serial_start_bold(void)
106 {
107         serial_putcmd(VT100_SBOLD);
108 }
109
110 void serial_end_bold(void)
111 {
112         serial_putcmd(VT100_EBOLD);
113 }
114
115 void serial_start_altcharset(void)
116 {
117         serial_putcmd(VT100_SMACS);
118 }
119
120 void serial_end_altcharset(void)
121 {
122         serial_putcmd(VT100_RMACS);
123 }
124
125 /**
126  * Set the foreground and background colors on the serial console.
127  *
128  * @param fg Foreground color number.
129  * @param bg Background color number.
130  */
131 void serial_set_color(short fg, short bg)
132 {
133         char buffer[32];
134         snprintf(buffer, sizeof(buffer), VT100_SET_COLOR, fg, bg);
135         serial_putcmd(buffer);
136 }
137
138 void serial_set_cursor(int y, int x)
139 {
140         char buffer[32];
141         snprintf(buffer, sizeof(buffer), VT100_CURSOR_ADDR, y + 1, x + 1);
142         serial_putcmd(buffer);
143 }