First pass at x86_64. Mostly consists of branching i386 stuff over.
[newos.git] / kernel / arch / x86_64 / arch_dbg_console.c
blob488923310c2cb72ef75a85626488c1474d27b48c
1 /*
2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 /*
6 ** Modified 2001/09/05 by Rob Judd<judd@ob-wan.com>
7 */
8 #include <kernel/kernel.h>
9 #include <kernel/int.h>
10 #include <kernel/arch/cpu.h>
11 #include <kernel/arch/dbg_console.h>
13 #include <boot/stage2.h>
15 #include <string.h>
17 #if _SERIAL_DBG_PORT == 1
18 static const int serial_ioport_base = 0x3f8;
19 #elif _SERIAL_DBG_PORT == 2
20 static const int serial_ioport_base = 0x2f8;
21 #elif _SERIAL_DBG_PORT == 0xe9
22 #define BOCHS_E9_HACK 1
23 #else
24 #error _SERIAL_DBG_PORT set to unsupported com port
25 #endif
27 static const int dbg_baud_rate = 115200;
29 int arch_dbg_con_init(kernel_args *ka)
31 #if !BOCHS_E9_HACK
32 short divisor = 115200 / dbg_baud_rate;
34 out8(0x80, serial_ioport_base+3); /* set up to load divisor latch */
35 out8(divisor & 0xf, serial_ioport_base); /* LSB */
36 out8(divisor >> 8, serial_ioport_base+1); /* MSB */
37 out8(3, serial_ioport_base+3); /* 8N1 */
38 #endif
39 return NO_ERROR;
42 int arch_dbg_con_init2(kernel_args *ka)
44 return NO_ERROR;
47 char arch_dbg_con_read(void)
49 #if BOCHS_E9_HACK
50 return in8(0xe9);
51 #else
52 while ((in8(serial_ioport_base+5) & 1) == 0)
54 return in8(serial_ioport_base);
55 #endif
58 static void _arch_dbg_con_putch(const char c)
60 #if BOCHS_E9_HACK
61 out8(c, 0xe9);
62 #else
64 while ((in8(serial_ioport_base+5) & 0x20) == 0)
66 out8(c, serial_ioport_base);
68 #endif
71 char arch_dbg_con_putch(const char c)
73 if (c == '\n') {
74 _arch_dbg_con_putch('\r');
75 _arch_dbg_con_putch('\n');
76 } else if (c != '\r')
77 _arch_dbg_con_putch(c);
79 return c;
82 void arch_dbg_con_puts(const char *s)
84 while(*s != '\0') {
85 arch_dbg_con_putch(*s);
86 s++;
90 ssize_t arch_dbg_con_write(const void *buf, ssize_t len)
92 const char *cbuf = (const char *)buf;
93 ssize_t ret = len;
95 while(len > 0) {
96 arch_dbg_con_putch(*cbuf);
97 cbuf++;
98 len--;
100 return ret;