1 #include <geekos/serial.h>
2 #include <geekos/reboot.h>
3 #include <geekos/gdt.h>
4 #include <geekos/idt.h>
9 unsigned short serial_io_addr = 0;
12 static void Serial_Interrupt_Handler(struct Interrupt_State * state) {
18 irq_id = In_Byte(serial_io_addr + 2);
21 if ((irq_id & 0x04) != 0) {
22 rcv_byte = In_Byte(serial_io_addr + 0);
24 if (rcv_byte == 'k') {
25 SerialPrint("Restarting Machine\r\n");
26 machine_real_restart();
27 } else if (rcv_byte=='d') {
28 SerialPrint("Dumping Machine State\n");
29 Dump_Interrupt_State(state);
35 SerialPrint("Unreserved serial byte: %d (%c)\r\n", rcv_byte, rcv_byte);
42 void InitSerialAddr(unsigned short io_addr) {
43 serial_io_addr = io_addr;
45 Print("Initializing Polled Serial Output on COM1 - 115200 N81 noflow\n");
46 // io_adr = 0x3F8; /* 3F8=COM1, 2F8=COM2, 3E8=COM3, 2E8=COM4 */
47 Out_Byte(io_addr + 3, 0x80);
48 // 115200 /* 115200 / 12 = 9600 baud */
49 Out_Byte(io_addr + 0, 1);
50 Out_Byte(io_addr + 1, 0);
52 Out_Byte(io_addr + 3, 0x03);
53 /* all interrupts disabled */
54 // Out_Byte(io_addr + 1, 0);
55 Out_Byte(io_addr + 1, 0x01);
56 /* turn off FIFO, if any */
57 Out_Byte(io_addr + 2, 0);
58 /* loopback off, interrupts (Out2) off, Out1/RTS/DTR off */
59 // Out_Byte(io_addr + 4, 0);
60 // enable interrupts (bit 3)
61 Out_Byte(io_addr + 4, 0x08);
65 inline static void SerialPutChar(unsigned char c) {
67 // static unsigned short io_adr;
68 if (serial_io_addr==0) {
74 /* wait for transmitter ready */
75 while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
78 Out_Byte(serial_io_addr + 0, '\r');
79 /* wait for transmitter ready */
81 while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
84 Out_Byte(serial_io_addr + 0, c);
89 void SerialPutLineN(char * line, int len) {
91 for (i = 0; i < len && line[i] != 0; i++) {
92 SerialPutChar(line[i]);
97 void SerialPutLine(char * line) {
99 for (i = 0; line[i]!= 0; i++) {
100 SerialPutChar(line[i]);
105 void SerialPrintHex(unsigned char x)
110 SerialPrint("%x", z);
112 SerialPrint("%x", z);
115 void SerialMemDump(unsigned char *start, int n)
119 for (i=0;i<n;i+=16) {
120 SerialPrint("%8x", (unsigned)(start+i));
121 for (j=i; j<i+16 && j<n; j+=2) {
123 SerialPrintHex(*((unsigned char *)(start+j)));
125 SerialPrintHex(*((unsigned char *)(start+j+1)));
129 for (j=i; j<i+16 && j<n;j++) {
130 SerialPrint("%c", ((start[j]>=32) && (start[j]<=126)) ? start[j] : '.');
137 static struct Output_Sink serial_output_sink;
138 static void Serial_Emit(struct Output_Sink * o, int ch) {
139 SerialPutChar((unsigned char)ch);
141 static void Serial_Finish(struct Output_Sink * o) { return; }
144 static void __inline__ SerialPrintInternal(const char * format, va_list ap) {
145 Format_Output(&serial_output_sink, format, ap);
149 void SerialPrint(const char * format, ...) {
151 bool iflag = Begin_Int_Atomic();
153 va_start(args, format);
154 SerialPrintInternal(format, args);
157 End_Int_Atomic(iflag);
160 void SerialPrintList(const char * format, va_list ap) {
161 bool iflag = Begin_Int_Atomic();
162 SerialPrintInternal(format, ap);
163 End_Int_Atomic(iflag);
170 Print("Initialzing Serial\n");
172 serial_output_sink.Emit = &Serial_Emit;
173 serial_output_sink.Finish = &Serial_Finish;
175 Install_IRQ(COM1_IRQ, Serial_Interrupt_Handler);
176 Enable_IRQ(COM1_IRQ);
177 InitSerialAddr(DEFAULT_SERIAL_ADDR);