1 #include <geekos/serial.h>
2 #include <geekos/reboot.h>
3 #include <geekos/gdt.h>
4 #include <geekos/idt.h>
5 #include <geekos/fmtout.h>
10 unsigned short serial_io_addr = 0;
11 uint_t serial_print_level;
13 static void Serial_Interrupt_Handler(struct Interrupt_State * state) {
19 irq_id = In_Byte(serial_io_addr + 2);
22 if ((irq_id & 0x04) != 0) {
23 rcv_byte = In_Byte(serial_io_addr + 0);
25 if (rcv_byte == 'k') {
26 SerialPrint("Restarting Machine\r\n");
27 machine_real_restart();
28 } else if (rcv_byte=='d') {
29 SerialPrint("Dumping Machine State\n");
30 Dump_Interrupt_State(state);
36 SerialPrint("Unreserved serial byte: %d (%c)\r\n", rcv_byte, rcv_byte);
44 void InitSerialAddr(unsigned short io_addr) {
45 serial_io_addr = io_addr;
47 Print("Initializing Polled Serial Output on COM1 - 115200 N81 noflow\n");
48 // io_adr = 0x3F8; /* 3F8=COM1, 2F8=COM2, 3E8=COM3, 2E8=COM4 */
49 Out_Byte(io_addr + 3, 0x80);
50 // 115200 /* 115200 / 12 = 9600 baud */
51 Out_Byte(io_addr + 0, 1);
52 Out_Byte(io_addr + 1, 0);
54 Out_Byte(io_addr + 3, 0x03);
55 /* all interrupts disabled */
56 // Out_Byte(io_addr + 1, 0);
57 Out_Byte(io_addr + 1, 0x01);
58 /* turn off FIFO, if any */
59 Out_Byte(io_addr + 2, 0);
60 /* loopback off, interrupts (Out2) off, Out1/RTS/DTR off */
61 // Out_Byte(io_addr + 4, 0);
62 // enable interrupts (bit 3)
63 Out_Byte(io_addr + 4, 0x08);
69 void SerialPutChar(unsigned char c) {
71 // static unsigned short io_adr;
72 if (serial_io_addr==0) {
78 /* wait for transmitter ready */
79 while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
82 Out_Byte(serial_io_addr + 0, '\r');
83 /* wait for transmitter ready */
85 while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
88 Out_Byte(serial_io_addr + 0, c);
93 void SerialPutLineN(char * line, int len) {
95 for (i = 0; i < len && line[i] != 0; i++) {
96 SerialPutChar(line[i]);
101 void SerialPutLine(char * line) {
103 for (i = 0; line[i]!= 0; i++) {
104 SerialPutChar(line[i]);
109 void SerialPrintHex(unsigned char x)
114 SerialPrint("%x", z);
116 SerialPrint("%x", z);
119 void SerialMemDump(unsigned char *start, int n)
123 for (i=0;i<n;i+=16) {
124 SerialPrint("%8x", (unsigned)(start+i));
125 for (j=i; j<i+16 && j<n; j+=2) {
127 SerialPrintHex(*((unsigned char *)(start+j)));
129 SerialPrintHex(*((unsigned char *)(start+j+1)));
133 for (j=i; j<i+16 && j<n;j++) {
134 SerialPrint("%c", ((start[j]>=32) && (start[j]<=126)) ? start[j] : '.');
141 static struct Output_Sink serial_output_sink;
142 static void Serial_Emit(struct Output_Sink * o, int ch) {
143 SerialPutChar((unsigned char)ch);
145 static void Serial_Finish(struct Output_Sink * o) { return; }
148 static void __inline__ SerialPrintInternal(const char * format, va_list ap) {
149 Format_Output(&serial_output_sink, format, ap);
153 void SerialPrint(const char * format, ...) {
155 bool iflag = Begin_Int_Atomic();
157 va_start(args, format);
158 SerialPrintInternal(format, args);
161 End_Int_Atomic(iflag);
164 void SerialPrintList(const char * format, va_list ap) {
165 bool iflag = Begin_Int_Atomic();
166 SerialPrintInternal(format, ap);
167 End_Int_Atomic(iflag);
174 void SerialPrintLevel(int level, const char * format, ...) {
175 if (level > serial_print_level) {
177 bool iflag = Begin_Int_Atomic();
179 va_start(args, format);
180 SerialPrintInternal(format, args);
183 End_Int_Atomic(iflag);
192 serial_print_level = SERIAL_PRINT_DEBUG_LEVEL;
194 Print("Initialzing Serial\n");
196 serial_output_sink.Emit = &Serial_Emit;
197 serial_output_sink.Finish = &Serial_Finish;
199 Install_IRQ(COM1_IRQ, Serial_Interrupt_Handler);
200 Enable_IRQ(COM1_IRQ);
201 InitSerialAddr(DEFAULT_SERIAL_ADDR);