1 #include "devices/serial.h"
3 #include "devices/intq.h"
4 #include "devices/timer.h"
5 #include "threads/io.h"
6 #include "threads/interrupt.h"
7 #include "threads/synch.h"
8 #include "threads/thread.h"
10 /* Register definitions for the 16550A UART used in PCs.
11 The 16550A has a lot more going on than shown here, but this
14 Refer to [PC16650D] for hardware information. */
16 /* I/O port base address for the first serial port. */
19 /* DLAB=0 registers. */
20 #define RBR_REG (IO_BASE + 0) /* Receiver Buffer Reg. (read-only). */
21 #define THR_REG (IO_BASE + 0) /* Transmitter Holding Reg. (write-only). */
22 #define IER_REG (IO_BASE + 1) /* Interrupt Enable Reg. (read-only). */
23 #define FCR_REG (IO_BASE + 2) /* FIFO Control Reg. (write-only). */
24 #define LCR_REG (IO_BASE + 3) /* Line Control Register. */
25 #define MCR_REG (IO_BASE + 4) /* MODEM Control Register. */
26 #define LSR_REG (IO_BASE + 5) /* Line Status Register (read-only). */
28 /* DLAB=1 registers. */
29 #define LS_REG (IO_BASE + 0) /* Divisor Latch (LSB). */
30 #define MS_REG (IO_BASE + 1) /* Divisor Latch (MSB). */
32 /* Interrupt Enable Register bits. */
33 #define IER_XMIT 0x02 /* Interrupt when transmit finishes. */
35 /* Line Control Register bits. */
36 #define LCR_N81 0x03 /* No parity, 8 data bits, 1 stop bit. */
37 #define LCR_DLAB 0x80 /* Divisor Latch Access Bit (DLAB). */
39 /* MODEM Control Register. */
40 #define MCR_OUT2 0x08 /* Output line 2. */
42 /* Line Status Register. */
43 #define LSR_THRE 0x20 /* THR Empty. */
45 /* Transmission mode. */
46 static enum { UNINIT, POLL, QUEUE } mode;
48 /* Data to be transmitted. */
49 static struct intq txq;
51 static void set_serial (int bps);
52 static void putc_poll (uint8_t);
53 static void write_ier (void);
54 static intr_handler_func serial_interrupt;
56 /* Initializes the serial port device for polling mode.
57 Polling mode busy-waits for the serial port to become free
58 before writing to it. It's slow, but until interrupts have
59 been initialized it's all we can do. */
61 serial_init_poll (void)
63 ASSERT (mode == UNINIT);
64 outb (IER_REG, 0); /* Turn off all interrupts. */
65 outb (FCR_REG, 0); /* Disable FIFO. */
66 set_serial (115200); /* 115.2 kbps, N-8-1. */
67 outb (MCR_REG, MCR_OUT2); /* Turn on OUT2 output line. */
72 /* Initializes the serial port device for queued interrupt-driven
73 I/O. With interrupt-driven I/O we don't waste CPU time
74 waiting for the serial device to become ready. */
76 serial_init_queue (void)
78 ASSERT (mode == POLL);
79 intr_register_ext (0x20 + 4, serial_interrupt, "serial");
83 /* Sends BYTE to the serial port. */
85 serial_putc (uint8_t byte)
87 enum intr_level old_level = intr_disable ();
91 /* If we're not set up for interrupt-driven I/O yet,
92 use dumb polling to transmit a byte. */
97 /* Otherwise, queue a byte and update the interrupt enable
99 if (old_level == INTR_OFF && intq_full (&txq))
101 /* Interrupts are off and the transmit queue is full.
102 If we wanted to wait for the queue to empty,
103 we'd have to reenable interrupts.
104 That's impolite, so we'll send a character via
106 putc_poll (intq_getc (&txq));
109 intq_putc (&txq, byte);
113 intr_set_level (old_level);
116 /* Flushes anything in the serial buffer out the port in polling
121 enum intr_level old_level = intr_disable ();
122 while (!intq_empty (&txq))
123 putc_poll (intq_getc (&txq));
124 intr_set_level (old_level);
127 /* Configures the serial port for BPS bits per second. */
131 int baud_base = 1843200 / 16; /* Base rate of 16550A. */
132 uint16_t divisor = baud_base / bps; /* Clock rate divisor. */
134 ASSERT (bps >= 300 && bps <= 115200);
137 outb (LCR_REG, LCR_N81 | LCR_DLAB);
140 outb (LS_REG, divisor & 0xff);
141 outb (MS_REG, divisor >> 8);
144 outb (LCR_REG, LCR_N81);
147 /* Update interrupt enable register.
148 If our transmit queue is empty, turn off transmit interrupt. */
152 outb (IER_REG, intq_empty (&txq) ? 0 : IER_XMIT);
155 /* Polls the serial port until it's ready,
156 and then transmits BYTE. */
158 putc_poll (uint8_t byte)
160 ASSERT (intr_get_level () == INTR_OFF);
162 while ((inb (LSR_REG) & LSR_THRE) == 0)
164 outb (THR_REG, byte);
167 /* Serial interrupt handler.
168 As long as we have a byte to transmit,
169 and the hardware is ready to accept a byte for transmission,
171 Then update interrupt enable register based on queue
174 serial_interrupt (struct intr_frame *f UNUSED)
176 while (!intq_empty (&txq) && (inb (LSR_REG) & LSR_THRE) != 0)
177 outb (THR_REG, intq_getc (&txq));