forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
73 lines (60 loc) · 1.38 KB
/
uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Intel 8250 serial port (UART).
#include "types.h"
#include "defs.h"
#include "param.h"
#include "traps.h"
#include "spinlock.h"
#include "fs.h"
#include "file.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"
#define COM1 0x3f8
static int uart; // is there a uart?
void
uartinit(void) {
char *p;
// Turn off the FIFO
outb(COM1 + 2, 0);
// 9600 baud, 8 data bits, 1 stop bit, parity off.
outb(COM1 + 3, 0x80); // Unlock divisor
outb(COM1 + 0, 115200 / 9600);
outb(COM1 + 1, 0);
outb(COM1 + 3, 0x03); // Lock divisor, 8 data bits.
outb(COM1 + 4, 0);
outb(COM1 + 1, 0x01); // Enable receive interrupts.
// If status is 0xFF, no serial port.
if (inb(COM1 + 5) == 0xFF)
return;
uart = 1;
// Acknowledge pre-existing interrupt conditions;
// enable interrupts.
inb(COM1 + 2);
inb(COM1 + 0);
picenable(IRQ_COM1);
ioapicenable(IRQ_COM1, 0);
// Announce that we're here.
for (p = "xv6...\n"; *p; p++)
uartputc(*p);
}
void
uartputc(int c) {
int i;
if (!uart)
return;
for (i = 0; i < 128 && !(inb(COM1 + 5) & 0x20); i++)
microdelay(10);
outb(COM1 + 0, c);
}
static int
uartgetc(void) {
if (!uart)
return -1;
if (!(inb(COM1 + 5) & 0x01))
return -1;
return inb(COM1 + 0);
}
void
uartintr(void) {
consoleintr(uartgetc);
}