2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) 2008 Ulf Jordan <jordan@chalmers.se>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <libpayload-config.h>
32 #include <libpayload.h>
34 #define IOBASE lib_sysinfo.ser_ioport
35 #define MEMBASE (phys_to_virt(lib_sysinfo.ser_base))
36 #define DIVISOR(x) (115200 / x)
38 #ifdef CONFIG_SERIAL_SET_SPEED
39 static void serial_io_hardware_init(int port, int speed, int word_bits, int parity, int stop_bits)
43 /* We will assume 8n1 for now. Does anyone use anything else these days? */
45 /* Disable interrupts. */
48 /* Assert RTS and DTR. */
51 /* Set the divisor latch. */
52 reg = inb(port + 0x03);
53 outb(reg | 0x80, port + 0x03);
55 /* Write the divisor. */
56 outb(DIVISOR(speed) & 0xFF, port);
57 outb(DIVISOR(speed) >> 8 & 0xFF, port + 1);
59 /* Restore the previous value of the divisor. */
60 outb(reg & ~0x80, port + 0x03);
63 static void serial_mem_hardware_init(int port, int speed, int word_bits, int parity, int stop_bits)
67 /* We will assume 8n1 for now. Does anyone use anything else these days? */
69 /* Disable interrupts. */
70 writeb(0, MEMBASE + 0x01);
72 /* Assert RTS and DTR. */
73 writeb(3, MEMBASE + 0x04);
75 /* Set the divisor latch. */
76 reg = readb(MEMBASE + 0x03);
77 writeb(reg | 0x80, MEMBASE + 0x03);
79 /* Write the divisor. */
80 writeb(DIVISOR(speed) & 0xFF, MEMBASE);
81 writeb(DIVISOR(speed) >> 8 & 0xFF, MEMBASE + 1);
83 /* Restore the previous value of the divisor. */
84 writeb(reg & ~0x80, MEMBASE + 0x03);
88 static struct console_input_driver consin = {
89 .havekey = serial_havechar,
90 .getchar = serial_getchar
93 static struct console_output_driver consout = {
94 .putchar = serial_putchar
97 void serial_init(void)
100 if (pci_find_device(0x1415, 0xc158, &oxpcie_dev)) {
101 lib_sysinfo.ser_base = pci_read_resource(oxpcie_dev, 0) + 0x1000;
103 lib_sysinfo.ser_base = 0;
106 #ifdef CONFIG_SERIAL_SET_SPEED
107 if (lib_sysinfo.ser_base)
108 serial_mem_hardware_init(IOBASE, CONFIG_SERIAL_BAUD_RATE, 8, 0, 1);
110 serial_io_hardware_init(IOBASE, CONFIG_SERIAL_BAUD_RATE, 8, 0, 1);
112 console_add_input_driver(&consin);
113 console_add_output_driver(&consout);
116 static void serial_io_putchar(unsigned int c)
119 while ((inb(IOBASE + 0x05) & 0x20) == 0) ;
123 static int serial_io_havechar(void)
125 return inb(IOBASE + 0x05) & 0x01;
128 static int serial_io_getchar(void)
130 while (!serial_io_havechar()) ;
131 return (int)inb(IOBASE);
134 static void serial_mem_putchar(unsigned int c)
137 while ((readb(MEMBASE + 0x05) & 0x20) == 0) ;
141 static int serial_mem_havechar(void)
143 return readb(MEMBASE + 0x05) & 0x01;
146 static int serial_mem_getchar(void)
148 while (!serial_mem_havechar()) ;
149 return (int)readb(MEMBASE);
153 void serial_putchar(unsigned int c)
155 if (lib_sysinfo.ser_base)
156 serial_mem_putchar(c);
158 serial_io_putchar(c);
161 int serial_havechar(void)
163 if (lib_sysinfo.ser_base)
164 return serial_mem_havechar();
166 return serial_io_havechar();
169 int serial_getchar(void)
171 if (lib_sysinfo.ser_base)
172 return serial_mem_getchar();
174 return serial_io_getchar();
177 /* These are thinly veiled vt100 functions used by curses */
179 #define VT100_CLEAR "\e[H\e[J"
180 /* These defines will fail if you use bold and reverse at the same time.
181 * Switching off one of them will switch off both. tinycurses knows about
182 * this and does the right thing.
184 #define VT100_SBOLD "\e[1m"
185 #define VT100_EBOLD "\e[m"
186 #define VT100_SREVERSE "\e[7m"
187 #define VT100_EREVERSE "\e[m"
188 #define VT100_CURSOR_ADDR "\e[%d;%dH"
189 #define VT100_CURSOR_ON "\e[?25l"
190 #define VT100_CURSOR_OFF "\e[?25h"
191 /* The following smacs/rmacs are actually for xterm; a real vt100 has
192 enacs=\E(B\E)0, smacs=^N, rmacs=^O. */
193 #define VT100_SMACS "\e(0"
194 #define VT100_RMACS "\e(B"
195 /* A vt100 doesn't do color, setaf/setab below are from xterm-color. */
196 #define VT100_SET_COLOR "\e[3%d;4%dm"
198 static void serial_putcmd(const char *str)
201 serial_putchar(*(str++));
204 void serial_clear(void)
206 serial_putcmd(VT100_CLEAR);
209 void serial_start_bold(void)
211 serial_putcmd(VT100_SBOLD);
214 void serial_end_bold(void)
216 serial_putcmd(VT100_EBOLD);
219 void serial_start_reverse(void)
221 serial_putcmd(VT100_SREVERSE);
224 void serial_end_reverse(void)
226 serial_putcmd(VT100_EREVERSE);
229 void serial_start_altcharset(void)
231 serial_putcmd(VT100_SMACS);
234 void serial_end_altcharset(void)
236 serial_putcmd(VT100_RMACS);
240 * Set the foreground and background colors on the serial console.
242 * @param fg Foreground color number.
243 * @param bg Background color number.
245 void serial_set_color(short fg, short bg)
248 snprintf(buffer, sizeof(buffer), VT100_SET_COLOR, fg, bg);
249 serial_putcmd(buffer);
252 void serial_set_cursor(int y, int x)
255 snprintf(buffer, sizeof(buffer), VT100_CURSOR_ADDR, y + 1, x + 1);
256 serial_putcmd(buffer);
259 void serial_cursor_enable(int state)
262 serial_putcmd(VT100_CURSOR_ON);
264 serial_putcmd(VT100_CURSOR_OFF);