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