1 /* (c) 2008, Peter Dinda <pdinda@northwestern.edu> */
2 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
3 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
5 #include <geekos/serial.h>
6 #include <geekos/reboot.h>
7 #include <geekos/gdt.h>
8 #include <geekos/idt.h>
9 #include <geekos/fmtout.h>
14 unsigned short serial_io_addr = 0;
15 uint_t serial_print_level;
17 static void Serial_Interrupt_Handler(struct Interrupt_State * state) {
23 irq_id = In_Byte(serial_io_addr + 2);
26 if ((irq_id & 0x04) != 0) {
27 rcv_byte = In_Byte(serial_io_addr + 0);
29 if (rcv_byte == 'k') {
30 SerialPrint("Restarting Machine\r\n");
31 machine_real_restart();
32 } else if (rcv_byte=='d') {
33 SerialPrint("Dumping Machine State\n");
34 Dump_Interrupt_State(state);
40 SerialPrint("Unreserved serial byte: %d (%c)\r\n", rcv_byte, rcv_byte);
48 void InitSerialAddr(unsigned short io_addr) {
49 serial_io_addr = io_addr;
51 Print("Initializing Polled Serial Output on COM1 - 115200 N81 noflow\n");
52 // io_adr = 0x3F8; /* 3F8=COM1, 2F8=COM2, 3E8=COM3, 2E8=COM4 */
53 Out_Byte(io_addr + 3, 0x80);
54 // 115200 /* 115200 / 12 = 9600 baud */
55 Out_Byte(io_addr + 0, 1);
56 Out_Byte(io_addr + 1, 0);
58 Out_Byte(io_addr + 3, 0x03);
59 /* all interrupts disabled */
60 // Out_Byte(io_addr + 1, 0);
61 Out_Byte(io_addr + 1, 0x01);
62 /* turn off FIFO, if any */
63 Out_Byte(io_addr + 2, 0);
64 /* loopback off, interrupts (Out2) off, Out1/RTS/DTR off */
65 // Out_Byte(io_addr + 4, 0);
66 // enable interrupts (bit 3)
67 Out_Byte(io_addr + 4, 0x08);
73 void SerialPutChar(unsigned char c) {
75 // static unsigned short io_adr;
76 if (serial_io_addr==0) {
82 /* wait for transmitter ready */
83 while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
86 Out_Byte(serial_io_addr + 0, '\r');
87 /* wait for transmitter ready */
89 while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
92 Out_Byte(serial_io_addr + 0, c);
97 void SerialPutLineN(char * line, int len) {
99 for (i = 0; i < len && line[i] != 0; i++) {
100 SerialPutChar(line[i]);
105 void SerialPutLine(char * line) {
107 for (i = 0; line[i]!= 0; i++) {
108 SerialPutChar(line[i]);
113 void SerialPrintHex(unsigned char x)
118 SerialPrint("%x", z);
120 SerialPrint("%x", z);
123 void SerialMemDump(unsigned char *start, int n)
127 for (i=0;i<n;i+=16) {
128 SerialPrint("%8x", (unsigned)(start+i));
129 for (j=i; j<i+16 && j<n; j+=2) {
131 SerialPrintHex(*((unsigned char *)(start+j)));
133 SerialPrintHex(*((unsigned char *)(start+j+1)));
137 for (j=i; j<i+16 && j<n;j++) {
138 SerialPrint("%c", ((start[j]>=32) && (start[j]<=126)) ? start[j] : '.');
145 static struct Output_Sink serial_output_sink;
146 static void Serial_Emit(struct Output_Sink * o, int ch) {
147 SerialPutChar((unsigned char)ch);
149 static void Serial_Finish(struct Output_Sink * o) { return; }
152 static void __inline__ SerialPrintInternal(const char * format, va_list ap) {
153 Format_Output(&serial_output_sink, format, ap);
157 void SerialPrint(const char * format, ...) {
159 bool iflag = Begin_Int_Atomic();
161 va_start(args, format);
162 SerialPrintInternal(format, args);
165 End_Int_Atomic(iflag);
168 void SerialPrintList(const char * format, va_list ap) {
169 bool iflag = Begin_Int_Atomic();
170 SerialPrintInternal(format, ap);
171 End_Int_Atomic(iflag);
178 void SerialPrintLevel(int level, const char * format, ...) {
179 if (level > serial_print_level) {
181 bool iflag = Begin_Int_Atomic();
183 va_start(args, format);
184 SerialPrintInternal(format, args);
187 End_Int_Atomic(iflag);
196 serial_print_level = SERIAL_PRINT_DEBUG_LEVEL;
198 Print("Initialzing Serial\n");
200 serial_output_sink.Emit = &Serial_Emit;
201 serial_output_sink.Finish = &Serial_Finish;
203 Install_IRQ(COM1_IRQ, Serial_Interrupt_Handler);
204 Enable_IRQ(COM1_IRQ);
205 InitSerialAddr(DEFAULT_SERIAL_ADDR);