drop one more version of doing serial uart output differently.
[coreboot.git] / src / include / console / console.h
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2003 Eric Biederman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #ifndef CONSOLE_CONSOLE_H_
21 #define CONSOLE_CONSOLE_H_
22
23 #include <stdint.h>
24 #include <console/loglevel.h>
25
26 #ifndef __PRE_RAM__
27 void console_tx_byte(unsigned char byte);
28 void console_tx_flush(void);
29 unsigned char console_rx_byte(void);
30 int console_tst_byte(void);
31 #if CONFIG_USBDEBUG
32 #include <usbdebug.h>
33 #endif
34
35 struct console_driver {
36         void (*init)(void);
37         void (*tx_byte)(unsigned char byte);
38         void (*tx_flush)(void);
39         unsigned char (*rx_byte)(void);
40         int (*tst_byte)(void);
41 };
42
43 #define __console       __attribute__((used, __section__ (".rodata.console_drivers")))
44
45 /* Defined by the linker... */
46 extern struct console_driver console_drivers[];
47 extern struct console_driver econsole_drivers[];
48
49 extern int console_loglevel;
50 #else
51 /* __PRE_RAM__ */
52 /* Using a global varible can cause problems when we reset the stack
53  * from cache as ram to ram. If we make this a define USE_SHARED_STACK
54  * we could use the same code on all architectures.
55  */
56 #define console_loglevel CONFIG_DEFAULT_CONSOLE_LOGLEVEL
57 #endif
58
59 #ifndef __ROMCC__
60 void console_init(void);
61 void post_code(u8 value);
62 void __attribute__ ((noreturn)) die(const char *msg);
63 int do_printk(int msg_level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
64
65 #undef WE_CLEANED_UP_ALL_SIDE_EFFECTS
66 /* We saw some strange effects in the past like coreboot crashing while
67  * disabling cache as ram for a maximum console log level of 6 and above while
68  * it worked fine without. In order to catch such issues reliably we are
69  * always doing a function call to do_printk with the full number of arguments.
70  * Our favorite reason to do it this way was:
71  *   disable_car();
72  *   printk(BIOS_DEBUG, "CAR disabled\n"); // oops, garbage stack pointer
73  *   move_stack();
74  * This slightly increases the code size and some unprinted strings will end
75  * up in the final coreboot binary (most of them compressed). If you want to
76  * avoid this, do a
77  * #define WE_CLEANED_UP_ALL_SIDE_EFFECTS
78  */
79 #ifdef WE_CLEANED_UP_ALL_SIDE_EFFECTS
80
81 #define printk(LEVEL, fmt, args...)                             \
82         do {                                                    \
83                 if (CONFIG_MAXIMUM_CONSOLE_LOGLEVEL >= LEVEL) { \
84                         do_printk(LEVEL, fmt, ##args);          \
85                 }                                               \
86         } while(0)
87
88 #else
89
90 #define printk(LEVEL, fmt, args...)                             \
91         do {                                                    \
92                 if (CONFIG_MAXIMUM_CONSOLE_LOGLEVEL >= LEVEL) { \
93                         do_printk(LEVEL, fmt, ##args);          \
94                 } else {                                        \
95                         do_printk(BIOS_NEVER, fmt, ##args);     \
96                 }                                               \
97         } while(0)
98 #endif
99
100 #define print_emerg(STR)         printk(BIOS_EMERG,  "%s", (STR))
101 #define print_alert(STR)         printk(BIOS_ALERT,  "%s", (STR))
102 #define print_crit(STR)          printk(BIOS_CRIT,   "%s", (STR))
103 #define print_err(STR)           printk(BIOS_ERR,    "%s", (STR))
104 #define print_warning(STR)       printk(BIOS_WARNING,"%s", (STR))
105 #define print_notice(STR)        printk(BIOS_NOTICE, "%s", (STR))
106 #define print_info(STR)          printk(BIOS_INFO,   "%s", (STR))
107 #define print_debug(STR)         printk(BIOS_DEBUG,  "%s", (STR))
108 #define print_spew(STR)          printk(BIOS_SPEW,   "%s", (STR))
109
110 #define print_emerg_char(CH)     printk(BIOS_EMERG,  "%c", (CH))
111 #define print_alert_char(CH)     printk(BIOS_ALERT,  "%c", (CH))
112 #define print_crit_char(CH)      printk(BIOS_CRIT,   "%c", (CH))
113 #define print_err_char(CH)       printk(BIOS_ERR,    "%c", (CH))
114 #define print_warning_char(CH)   printk(BIOS_WARNING,"%c", (CH))
115 #define print_notice_char(CH)    printk(BIOS_NOTICE, "%c", (CH))
116 #define print_info_char(CH)      printk(BIOS_INFO,   "%c", (CH))
117 #define print_debug_char(CH)     printk(BIOS_DEBUG,  "%c", (CH))
118 #define print_spew_char(CH)      printk(BIOS_SPEW,   "%c", (CH))
119
120 #define print_emerg_hex8(HEX)    printk(BIOS_EMERG,  "%02x",  (HEX))
121 #define print_alert_hex8(HEX)    printk(BIOS_ALERT,  "%02x",  (HEX))
122 #define print_crit_hex8(HEX)     printk(BIOS_CRIT,   "%02x",  (HEX))
123 #define print_err_hex8(HEX)      printk(BIOS_ERR,    "%02x",  (HEX))
124 #define print_warning_hex8(HEX)  printk(BIOS_WARNING,"%02x",  (HEX))
125 #define print_notice_hex8(HEX)   printk(BIOS_NOTICE, "%02x",  (HEX))
126 #define print_info_hex8(HEX)     printk(BIOS_INFO,   "%02x",  (HEX))
127 #define print_debug_hex8(HEX)    printk(BIOS_DEBUG,  "%02x",  (HEX))
128 #define print_spew_hex8(HEX)     printk(BIOS_SPEW,   "%02x",  (HEX))
129
130 #define print_emerg_hex16(HEX)   printk(BIOS_EMERG,  "%04x", (HEX))
131 #define print_alert_hex16(HEX)   printk(BIOS_ALERT,  "%04x", (HEX))
132 #define print_crit_hex16(HEX)    printk(BIOS_CRIT,   "%04x", (HEX))
133 #define print_err_hex16(HEX)     printk(BIOS_ERR,    "%04x", (HEX))
134 #define print_warning_hex16(HEX) printk(BIOS_WARNING,"%04x", (HEX))
135 #define print_notice_hex16(HEX)  printk(BIOS_NOTICE, "%04x", (HEX))
136 #define print_info_hex16(HEX)    printk(BIOS_INFO,   "%04x", (HEX))
137 #define print_debug_hex16(HEX)   printk(BIOS_DEBUG,  "%04x", (HEX))
138 #define print_spew_hex16(HEX)    printk(BIOS_SPEW,   "%04x", (HEX))
139
140 #define print_emerg_hex32(HEX)   printk(BIOS_EMERG,  "%08x", (HEX))
141 #define print_alert_hex32(HEX)   printk(BIOS_ALERT,  "%08x", (HEX))
142 #define print_crit_hex32(HEX)    printk(BIOS_CRIT,   "%08x", (HEX))
143 #define print_err_hex32(HEX)     printk(BIOS_ERR,    "%08x", (HEX))
144 #define print_warning_hex32(HEX) printk(BIOS_WARNING,"%08x", (HEX))
145 #define print_notice_hex32(HEX)  printk(BIOS_NOTICE, "%08x", (HEX))
146 #define print_info_hex32(HEX)    printk(BIOS_INFO,   "%08x", (HEX))
147 #define print_debug_hex32(HEX)   printk(BIOS_DEBUG,  "%08x", (HEX))
148 #define print_spew_hex32(HEX)    printk(BIOS_SPEW,   "%08x", (HEX))
149 #else
150
151 #if CONFIG_CONSOLE_SERIAL8250
152 #include "lib/uart8259.c"
153 #endif
154 #if CONFIG_CONSOLE_NE2K
155 #include "lib/ne2k.c"
156 #endif
157
158 /* __ROMCC__ */
159 static void __console_tx_byte(unsigned char byte)
160 {
161 #if CONFIG_CONSOLE_SERIAL8250
162         uart8250_tx_byte(CONFIG_TTYS0_BASE, byte);
163 #endif
164 #if CONFIG_CONSOLE_NE2K
165         ne2k_append_data_byte(byte, CONFIG_CONSOLE_NE2K_IO_PORT);
166 #endif
167 }
168
169 static void __console_tx_nibble(unsigned nibble)
170 {
171         unsigned char digit;
172         digit = nibble + '0';
173         if (digit > '9') {
174                 digit += 39;
175         }
176         __console_tx_byte(digit);
177 }
178
179 static void __console_tx_char(int loglevel, unsigned char byte)
180 {
181         if (console_loglevel >= loglevel) {
182 #if CONFIG_CONSOLE_SERIAL8250
183                 uart8250_tx_byte(CONFIG_TTYS0_BASE, byte);
184 #endif
185 #if CONFIG_CONSOLE_NE2K
186                 ne2k_append_data_byte(byte, CONFIG_CONSOLE_NE2K_IO_PORT);
187                 ne2k_transmit(CONFIG_CONSOLE_NE2K_IO_PORT);
188 #endif
189         }
190 }
191
192 static void __console_tx_hex8(int loglevel, unsigned char value)
193 {
194         if (console_loglevel >= loglevel) {
195                 __console_tx_nibble((value >>  4U) & 0x0fU);
196                 __console_tx_nibble(value & 0x0fU);
197         }
198 #if CONFIG_CONSOLE_NE2K
199                 ne2k_transmit(CONFIG_CONSOLE_NE2K_IO_PORT);
200 #endif
201 }
202
203 static void __console_tx_hex16(int loglevel, unsigned short value)
204 {
205         if (console_loglevel >= loglevel) {
206                 __console_tx_nibble((value >> 12U) & 0x0fU);
207                 __console_tx_nibble((value >>  8U) & 0x0fU);
208                 __console_tx_nibble((value >>  4U) & 0x0fU);
209                 __console_tx_nibble(value & 0x0fU);
210         }
211 #if CONFIG_CONSOLE_NE2K
212                 ne2k_transmit(CONFIG_CONSOLE_NE2K_IO_PORT);
213 #endif
214 }
215
216 static void __console_tx_hex32(int loglevel, unsigned int value)
217 {
218         if (console_loglevel >= loglevel) {
219                 __console_tx_nibble((value >> 28U) & 0x0fU);
220                 __console_tx_nibble((value >> 24U) & 0x0fU);
221                 __console_tx_nibble((value >> 20U) & 0x0fU);
222                 __console_tx_nibble((value >> 16U) & 0x0fU);
223                 __console_tx_nibble((value >> 12U) & 0x0fU);
224                 __console_tx_nibble((value >>  8U) & 0x0fU);
225                 __console_tx_nibble((value >>  4U) & 0x0fU);
226                 __console_tx_nibble(value & 0x0fU);
227         }
228 #if CONFIG_CONSOLE_NE2K
229                 ne2k_transmit(CONFIG_CONSOLE_NE2K_IO_PORT);
230 #endif
231 }
232
233 static void __console_tx_string(int loglevel, const char *str)
234 {
235         if (console_loglevel >= loglevel) {
236                 unsigned char ch;
237                 while((ch = *str++) != '\0') {
238                         if (ch == '\n')
239                                 __console_tx_byte('\r');
240                         __console_tx_byte(ch);
241                 }
242 #if CONFIG_CONSOLE_NE2K
243                 ne2k_transmit(CONFIG_CONSOLE_NE2K_IO_PORT);
244 #endif
245         }
246 }
247
248 #define FUNCTIONS_FOR_PRINT
249 #ifdef  FUNCTIONS_FOR_PRINT
250 static void print_emerg_char(unsigned char byte) { __console_tx_char(BIOS_EMERG, byte); }
251 static void print_emerg_hex8(unsigned char value){ __console_tx_hex8(BIOS_EMERG, value); }
252 static void print_emerg_hex16(unsigned short value){ __console_tx_hex16(BIOS_EMERG, value); }
253 static void print_emerg_hex32(unsigned int value) { __console_tx_hex32(BIOS_EMERG, value); }
254 static void print_emerg(const char *str) { __console_tx_string(BIOS_EMERG, str); }
255
256 static void print_alert_char(unsigned char byte) { __console_tx_char(BIOS_ALERT, byte); }
257 static void print_alert_hex8(unsigned char value) { __console_tx_hex8(BIOS_ALERT, value); }
258 static void print_alert_hex16(unsigned short value){ __console_tx_hex16(BIOS_ALERT, value); }
259 static void print_alert_hex32(unsigned int value) { __console_tx_hex32(BIOS_ALERT, value); }
260 static void print_alert(const char *str) { __console_tx_string(BIOS_ALERT, str); }
261
262 static void print_crit_char(unsigned char byte) { __console_tx_char(BIOS_CRIT, byte); }
263 static void print_crit_hex8(unsigned char value) { __console_tx_hex8(BIOS_CRIT, value); }
264 static void print_crit_hex16(unsigned short value){ __console_tx_hex16(BIOS_CRIT, value); }
265 static void print_crit_hex32(unsigned int value) { __console_tx_hex32(BIOS_CRIT, value); }
266 static void print_crit(const char *str) { __console_tx_string(BIOS_CRIT, str); }
267
268 static void print_err_char(unsigned char byte) { __console_tx_char(BIOS_ERR, byte); }
269 static void print_err_hex8(unsigned char value) { __console_tx_hex8(BIOS_ERR, value); }
270 static void print_err_hex16(unsigned short value){ __console_tx_hex16(BIOS_ERR, value); }
271 static void print_err_hex32(unsigned int value) { __console_tx_hex32(BIOS_ERR, value); }
272 static void print_err(const char *str) { __console_tx_string(BIOS_ERR, str); }
273
274 static void print_warning_char(unsigned char byte) { __console_tx_char(BIOS_WARNING, byte); }
275 static void print_warning_hex8(unsigned char value) { __console_tx_hex8(BIOS_WARNING, value); }
276 static void print_warning_hex16(unsigned short value){ __console_tx_hex16(BIOS_WARNING, value); }
277 static void print_warning_hex32(unsigned int value) { __console_tx_hex32(BIOS_WARNING, value); }
278 static void print_warning(const char *str) { __console_tx_string(BIOS_WARNING, str); }
279
280 static void print_notice_char(unsigned char byte) { __console_tx_char(BIOS_NOTICE, byte); }
281 static void print_notice_hex8(unsigned char value) { __console_tx_hex8(BIOS_NOTICE, value); }
282 static void print_notice_hex16(unsigned short value){ __console_tx_hex16(BIOS_NOTICE, value); }
283 static void print_notice_hex32(unsigned int value) { __console_tx_hex32(BIOS_NOTICE, value); }
284 static void print_notice(const char *str) { __console_tx_string(BIOS_NOTICE, str); }
285
286 static void print_info_char(unsigned char byte) { __console_tx_char(BIOS_INFO, byte); }
287 static void print_info_hex8(unsigned char value) { __console_tx_hex8(BIOS_INFO, value); }
288 static void print_info_hex16(unsigned short value){ __console_tx_hex16(BIOS_INFO, value); }
289 static void print_info_hex32(unsigned int value) { __console_tx_hex32(BIOS_INFO, value); }
290 static void print_info(const char *str) { __console_tx_string(BIOS_INFO, str); }
291
292 static void print_debug_char(unsigned char byte) { __console_tx_char(BIOS_DEBUG, byte); }
293 static void print_debug_hex8(unsigned char value) { __console_tx_hex8(BIOS_DEBUG, value); }
294 static void print_debug_hex16(unsigned short value){ __console_tx_hex16(BIOS_DEBUG, value); }
295 static void print_debug_hex32(unsigned int value) { __console_tx_hex32(BIOS_DEBUG, value); }
296 static void print_debug(const char *str) { __console_tx_string(BIOS_DEBUG, str); }
297
298 static void print_spew_char(unsigned char byte) { __console_tx_char(BIOS_SPEW, byte); }
299 static void print_spew_hex8(unsigned char value) { __console_tx_hex8(BIOS_SPEW, value); }
300 static void print_spew_hex16(unsigned short value){ __console_tx_hex16(BIOS_SPEW, value); }
301 static void print_spew_hex32(unsigned int value) { __console_tx_hex32(BIOS_SPEW, value); }
302 static void print_spew(const char *str) { __console_tx_string(BIOS_SPEW, str); }
303
304 #else
305 #define print_emerg(STR)         __console_tx_string(BIOS_EMERG, STR)
306 #define print_alert(STR)         __console_tx_string(BIOS_ALERT, STR)
307 #define print_crit(STR)          __console_tx_string(BIOS_CRIT, STR)
308 #define print_err(STR)           __console_tx_string(BIOS_ERR, STR)
309 #define print_warning(STR)       __console_tx_string(BIOS_WARNING, STR)
310 #define print_notice(STR)        __console_tx_string(BIOS_NOTICE, STR)
311 #define print_info(STR)          __console_tx_string(BIOS_INFO, STR)
312 #define print_debug(STR)         __console_tx_string(BIOS_DEBUG, STR)
313 #define print_spew(STR)          __console_tx_string(BIOS_SPEW, STR)
314
315 #define print_emerg_char(CH)     __console_tx_char(BIOS_EMERG, CH)
316 #define print_alert_char(CH)     __console_tx_char(BIOS_ALERT, CH)
317 #define print_crit_char(CH)      __console_tx_char(BIOS_CRIT, CH)
318 #define print_err_char(CH)       __console_tx_char(BIOS_ERR, CH)
319 #define print_warning_char(CH)   __console_tx_char(BIOS_WARNING, CH)
320 #define print_notice_char(CH)    __console_tx_char(BIOS_NOTICE, CH)
321 #define print_info_char(CH)      __console_tx_char(BIOS_INFO, CH)
322 #define print_debug_char(CH)     __console_tx_char(BIOS_DEBUG, CH)
323 #define print_spew_char(CH)      __console_tx_char(BIOS_SPEW, CH)
324
325 #define print_emerg_hex8(HEX)    __console_tx_hex8(BIOS_EMERG, HEX)
326 #define print_alert_hex8(HEX)    __console_tx_hex8(BIOS_ALERT, HEX)
327 #define print_crit_hex8(HEX)     __console_tx_hex8(BIOS_CRIT, HEX)
328 #define print_err_hex8(HEX)      __console_tx_hex8(BIOS_ERR, HEX)
329 #define print_warning_hex8(HEX)  __console_tx_hex8(BIOS_WARNING, HEX)
330 #define print_notice_hex8(HEX)   __console_tx_hex8(BIOS_NOTICE, HEX)
331 #define print_info_hex8(HEX)     __console_tx_hex8(BIOS_INFO, HEX)
332 #define print_debug_hex8(HEX)    __console_tx_hex8(BIOS_DEBUG, HEX)
333 #define print_spew_hex8(HEX)     __console_tx_hex8(BIOS_SPEW, HEX)
334
335 #define print_emerg_hex16(HEX)   __console_tx_hex16(BIOS_EMERG, HEX)
336 #define print_alert_hex16(HEX)   __console_tx_hex16(BIOS_ALERT, HEX)
337 #define print_crit_hex16(HEX)    __console_tx_hex16(BIOS_CRIT, HEX)
338 #define print_err_hex16(HEX)     __console_tx_hex16(BIOS_ERR, HEX)
339 #define print_warning_hex16(HEX) __console_tx_hex16(BIOS_WARNING, HEX)
340 #define print_notice_hex16(HEX)  __console_tx_hex16(BIOS_NOTICE, HEX)
341 #define print_info_hex16(HEX)    __console_tx_hex16(BIOS_INFO, HEX)
342 #define print_debug_hex16(HEX)   __console_tx_hex16(BIOS_DEBUG, HEX)
343 #define print_spew_hex16(HEX)    __console_tx_hex16(BIOS_SPEW, HEX)
344
345 #define print_emerg_hex32(HEX)   __console_tx_hex32(BIOS_EMERG, HEX)
346 #define print_alert_hex32(HEX)   __console_tx_hex32(BIOS_ALERT, HEX)
347 #define print_crit_hex32(HEX)    __console_tx_hex32(BIOS_CRIT, HEX)
348 #define print_err_hex32(HEX)     __console_tx_hex32(BIOS_ERR, HEX)
349 #define print_warning_hex32(HEX) __console_tx_hex32(BIOS_WARNING, HEX)
350 #define print_notice_hex32(HEX)  __console_tx_hex32(BIOS_NOTICE, HEX)
351 #define print_info_hex32(HEX)    __console_tx_hex32(BIOS_INFO, HEX)
352 #define print_debug_hex32(HEX)   __console_tx_hex32(BIOS_DEBUG, HEX)
353 #define print_spew_hex32(HEX)    __console_tx_hex32(BIOS_SPEW, HEX)
354 #endif
355
356 #endif
357
358 #ifdef __ROMCC__
359 /* if included by romcc, include the sources, too. romcc can't use prototypes */
360 #include <console/console.c>
361 #include <console/post.c>
362 #include <console/die.c>
363 #endif
364
365 #endif /* CONSOLE_CONSOLE_H_ */