1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <libpayload-config.h>
32#include <libpayload.h>
33
34#define IOBASE lib_sysinfo.ser_ioport
35#define MEMBASE (phys_to_virt(lib_sysinfo.ser_base))
36#define DIVISOR(x) (115200 / x)
37
38#ifdef CONFIG_SERIAL_SET_SPEED
39static void serial_io_hardware_init(int port, int speed, int word_bits, int parity, int stop_bits)
40{
41 unsigned char reg;
42
43
44
45
46 outb(0, port + 0x01);
47
48
49 outb(3, port + 0x04);
50
51
52 reg = inb(port + 0x03);
53 outb(reg | 0x80, port + 0x03);
54
55
56 outb(DIVISOR(speed) & 0xFF, port);
57 outb(DIVISOR(speed) >> 8 & 0xFF, port + 1);
58
59
60 outb(reg & ~0x80, port + 0x03);
61}
62
63static void serial_mem_hardware_init(int port, int speed, int word_bits, int parity, int stop_bits)
64{
65 unsigned char reg;
66
67
68
69
70 writeb(0, MEMBASE + 0x01);
71
72
73 writeb(3, MEMBASE + 0x04);
74
75
76 reg = readb(MEMBASE + 0x03);
77 writeb(reg | 0x80, MEMBASE + 0x03);
78
79
80 writeb(DIVISOR(speed) & 0xFF, MEMBASE);
81 writeb(DIVISOR(speed) >> 8 & 0xFF, MEMBASE + 1);
82
83
84 writeb(reg & ~0x80, MEMBASE + 0x03);
85}
86#endif
87
88static struct console_input_driver consin = {
89 .havekey = serial_havechar,
90 .getchar = serial_getchar
91};
92
93static struct console_output_driver consout = {
94 .putchar = serial_putchar
95};
96
97void serial_init(void)
98{
99 pcidev_t oxpcie_dev;
100 if (pci_find_device(0x1415, 0xc158, &oxpcie_dev)) {
101 lib_sysinfo.ser_base = pci_read_resource(oxpcie_dev, 0) + 0x1000;
102 } else {
103 lib_sysinfo.ser_base = 0;
104 }
105
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);
109 else
110 serial_io_hardware_init(IOBASE, CONFIG_SERIAL_BAUD_RATE, 8, 0, 1);
111#endif
112 console_add_input_driver(&consin);
113 console_add_output_driver(&consout);
114}
115
116static void serial_io_putchar(unsigned int c)
117{
118 c &= 0xff;
119 while ((inb(IOBASE + 0x05) & 0x20) == 0) ;
120 outb(c, IOBASE);
121}
122
123static int serial_io_havechar(void)
124{
125 return inb(IOBASE + 0x05) & 0x01;
126}
127
128static int serial_io_getchar(void)
129{
130 while (!serial_io_havechar()) ;
131 return (int)inb(IOBASE);
132}
133
134static void serial_mem_putchar(unsigned int c)
135{
136 c &= 0xff;
137 while ((readb(MEMBASE + 0x05) & 0x20) == 0) ;
138 writeb(c, MEMBASE);
139}
140
141static int serial_mem_havechar(void)
142{
143 return readb(MEMBASE + 0x05) & 0x01;
144}
145
146static int serial_mem_getchar(void)
147{
148 while (!serial_mem_havechar()) ;
149 return (int)readb(MEMBASE);
150}
151
152
153void serial_putchar(unsigned int c)
154{
155 if (lib_sysinfo.ser_base)
156 serial_mem_putchar(c);
157 else
158 serial_io_putchar(c);
159}
160
161int serial_havechar(void)
162{
163 if (lib_sysinfo.ser_base)
164 return serial_mem_havechar();
165 else
166 return serial_io_havechar();
167}
168
169int serial_getchar(void)
170{
171 if (lib_sysinfo.ser_base)
172 return serial_mem_getchar();
173 else
174 return serial_io_getchar();
175}
176
177
178
179#define VT100_CLEAR "\e[H\e[J"
180
181
182
183
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
192
193#define VT100_SMACS "\e(0"
194#define VT100_RMACS "\e(B"
195
196#define VT100_SET_COLOR "\e[3%d;4%dm"
197
198static void serial_putcmd(char *str)
199{
200 while(*str)
201 serial_putchar(*(str++));
202}
203
204void serial_clear(void)
205{
206 serial_putcmd(VT100_CLEAR);
207}
208
209void serial_start_bold(void)
210{
211 serial_putcmd(VT100_SBOLD);
212}
213
214void serial_end_bold(void)
215{
216 serial_putcmd(VT100_EBOLD);
217}
218
219void serial_start_reverse(void)
220{
221 serial_putcmd(VT100_SREVERSE);
222}
223
224void serial_end_reverse(void)
225{
226 serial_putcmd(VT100_EREVERSE);
227}
228
229void serial_start_altcharset(void)
230{
231 serial_putcmd(VT100_SMACS);
232}
233
234void serial_end_altcharset(void)
235{
236 serial_putcmd(VT100_RMACS);
237}
238
239
240
241
242
243
244
245void serial_set_color(short fg, short bg)
246{
247 char buffer[32];
248 snprintf(buffer, sizeof(buffer), VT100_SET_COLOR, fg, bg);
249 serial_putcmd(buffer);
250}
251
252void serial_set_cursor(int y, int x)
253{
254 char buffer[32];
255 snprintf(buffer, sizeof(buffer), VT100_CURSOR_ADDR, y + 1, x + 1);
256 serial_putcmd(buffer);
257}
258
259void serial_cursor_enable(int state)
260{
261 if (state)
262 serial_putcmd(VT100_CURSOR_ON);
263 else
264 serial_putcmd(VT100_CURSOR_OFF);
265}
266