added base src
[xv6-db.git] / uart.c
blob576e254db9b3be297510b49911f02e8fa3c482cd
1 // Intel 8250 serial port (UART).
3 #include "types.h"
4 #include "defs.h"
5 #include "param.h"
6 #include "traps.h"
7 #include "spinlock.h"
8 #include "fs.h"
9 #include "file.h"
10 #include "mmu.h"
11 #include "proc.h"
12 #include "x86.h"
14 #define COM1 0x3f8
16 static int uart; // is there a uart?
18 void
19 uartinit(void)
21 char *p;
23 // Turn off the FIFO
24 outb(COM1+2, 0);
26 // 9600 baud, 8 data bits, 1 stop bit, parity off.
27 outb(COM1+3, 0x80); // Unlock divisor
28 outb(COM1+0, 115200/9600);
29 outb(COM1+1, 0);
30 outb(COM1+3, 0x03); // Lock divisor, 8 data bits.
31 outb(COM1+4, 0);
32 outb(COM1+1, 0x01); // Enable receive interrupts.
34 // If status is 0xFF, no serial port.
35 if(inb(COM1+5) == 0xFF)
36 return;
37 uart = 1;
39 // Acknowledge pre-existing interrupt conditions;
40 // enable interrupts.
41 inb(COM1+2);
42 inb(COM1+0);
43 picenable(IRQ_COM1);
44 ioapicenable(IRQ_COM1, 0);
46 // Announce that we're here.
47 for(p="xv6...\n"; *p; p++)
48 uartputc(*p);
51 void
52 uartputc(int c)
54 int i;
56 if(!uart)
57 return;
58 for(i = 0; i < 128 && !(inb(COM1+5) & 0x20); i++)
59 microdelay(10);
60 outb(COM1+0, c);
63 static int
64 uartgetc(void)
66 if(!uart)
67 return -1;
68 if(!(inb(COM1+5) & 0x01))
69 return -1;
70 return inb(COM1+0);
73 void
74 uartintr(void)
76 consoleintr(uartgetc);