x86_64: allow serial port baud rate to be configured
This commit is contained in:
@@ -18,7 +18,7 @@ extern char serial_recv_byte(int device);
|
|||||||
|
|
||||||
extern int serial_rcvd(int device);
|
extern int serial_rcvd(int device);
|
||||||
|
|
||||||
extern void serialcon_init(void);
|
extern void serialcon_init(int baud);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ int ml_init(uintptr_t arg)
|
|||||||
|
|
||||||
bootstrap_cpu_init();
|
bootstrap_cpu_init();
|
||||||
vgacon_init();
|
vgacon_init();
|
||||||
serialcon_init();
|
serialcon_init(115200);
|
||||||
clock_calibrate(500);
|
clock_calibrate(500);
|
||||||
|
|
||||||
print_kernel_banner();
|
print_kernel_banner();
|
||||||
|
|||||||
@@ -49,12 +49,44 @@ void serialcon_write(struct console *con, const char *s, unsigned int len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_serial_port(int port)
|
static int get_baud_divisor(int baud)
|
||||||
{
|
{
|
||||||
|
int freq = 115200;
|
||||||
|
int best_baud = -1;
|
||||||
|
int best_div = -1;
|
||||||
|
|
||||||
|
for (int i = 1; i < 254; i++) {
|
||||||
|
int this_baud = freq / i;
|
||||||
|
|
||||||
|
if (this_baud == baud) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (best_baud == -1) {
|
||||||
|
best_baud = this_baud;
|
||||||
|
best_div = i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this_baud < baud && best_baud > baud) {
|
||||||
|
/* TODO pick divisor that gives the closest baud rate */
|
||||||
|
return best_div;
|
||||||
|
}
|
||||||
|
|
||||||
|
best_baud = this_baud;
|
||||||
|
best_div = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return best_div;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_serial_port(int port, int baud)
|
||||||
|
{
|
||||||
|
int baud_div = get_baud_divisor(baud);
|
||||||
outportb(port + 1, 0x00); // Disable all interrupts
|
outportb(port + 1, 0x00); // Disable all interrupts
|
||||||
outportb(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
outportb(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
||||||
outportb(port + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
outportb(port + 0, baud_div); // Set divisor
|
||||||
outportb(port + 1, 0x00); // (hi byte)
|
outportb(port + 1, 0x00);
|
||||||
outportb(port + 3, 0x03); // 8 bits, no parity, one stop bit
|
outportb(port + 3, 0x03); // 8 bits, no parity, one stop bit
|
||||||
outportb(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
outportb(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
||||||
outportb(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
outportb(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
||||||
@@ -85,8 +117,8 @@ static struct console serialcon = {
|
|||||||
.c_lock = SPIN_LOCK_INIT,
|
.c_lock = SPIN_LOCK_INIT,
|
||||||
};
|
};
|
||||||
|
|
||||||
void serialcon_init(void)
|
void serialcon_init(int baud)
|
||||||
{
|
{
|
||||||
init_serial_port(COM1);
|
init_serial_port(COM1, baud);
|
||||||
console_register(&serialcon);
|
console_register(&serialcon);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user