Path: blob/master/arch/x86/boot/early_serial_console.c
10818 views
#include "boot.h"12#define DEFAULT_SERIAL_PORT 0x3f8 /* ttyS0 */34#define XMTRDY 0x2056#define DLAB 0x8078#define TXR 0 /* Transmit register (WRITE) */9#define RXR 0 /* Receive register (READ) */10#define IER 1 /* Interrupt Enable */11#define IIR 2 /* Interrupt ID */12#define FCR 2 /* FIFO control */13#define LCR 3 /* Line control */14#define MCR 4 /* Modem control */15#define LSR 5 /* Line Status */16#define MSR 6 /* Modem Status */17#define DLL 0 /* Divisor Latch Low */18#define DLH 1 /* Divisor latch High */1920#define DEFAULT_BAUD 96002122static void early_serial_init(int port, int baud)23{24unsigned char c;25unsigned divisor;2627outb(0x3, port + LCR); /* 8n1 */28outb(0, port + IER); /* no interrupt */29outb(0, port + FCR); /* no fifo */30outb(0x3, port + MCR); /* DTR + RTS */3132divisor = 115200 / baud;33c = inb(port + LCR);34outb(c | DLAB, port + LCR);35outb(divisor & 0xff, port + DLL);36outb((divisor >> 8) & 0xff, port + DLH);37outb(c & ~DLAB, port + LCR);3839early_serial_base = port;40}4142static void parse_earlyprintk(void)43{44int baud = DEFAULT_BAUD;45char arg[32];46int pos = 0;47int port = 0;4849if (cmdline_find_option("earlyprintk", arg, sizeof arg) > 0) {50char *e;5152if (!strncmp(arg, "serial", 6)) {53port = DEFAULT_SERIAL_PORT;54pos += 6;55}5657if (arg[pos] == ',')58pos++;5960/*61* make sure we have62* "serial,0x3f8,115200"63* "serial,ttyS0,115200"64* "ttyS0,115200"65*/66if (pos == 7 && !strncmp(arg + pos, "0x", 2)) {67port = simple_strtoull(arg + pos, &e, 16);68if (port == 0 || arg + pos == e)69port = DEFAULT_SERIAL_PORT;70else71pos = e - arg;72} else if (!strncmp(arg + pos, "ttyS", 4)) {73static const int bases[] = { 0x3f8, 0x2f8 };74int idx = 0;7576if (!strncmp(arg + pos, "ttyS", 4))77pos += 4;7879if (arg[pos++] == '1')80idx = 1;8182port = bases[idx];83}8485if (arg[pos] == ',')86pos++;8788baud = simple_strtoull(arg + pos, &e, 0);89if (baud == 0 || arg + pos == e)90baud = DEFAULT_BAUD;91}9293if (port)94early_serial_init(port, baud);95}9697#define BASE_BAUD (1843200/16)98static unsigned int probe_baud(int port)99{100unsigned char lcr, dll, dlh;101unsigned int quot;102103lcr = inb(port + LCR);104outb(lcr | DLAB, port + LCR);105dll = inb(port + DLL);106dlh = inb(port + DLH);107outb(lcr, port + LCR);108quot = (dlh << 8) | dll;109110return BASE_BAUD / quot;111}112113static void parse_console_uart8250(void)114{115char optstr[64], *options;116int baud = DEFAULT_BAUD;117int port = 0;118119/*120* console=uart8250,io,0x3f8,115200n8121* need to make sure it is last one console !122*/123if (cmdline_find_option("console", optstr, sizeof optstr) <= 0)124return;125126options = optstr;127128if (!strncmp(options, "uart8250,io,", 12))129port = simple_strtoull(options + 12, &options, 0);130else if (!strncmp(options, "uart,io,", 8))131port = simple_strtoull(options + 8, &options, 0);132else133return;134135if (options && (options[0] == ','))136baud = simple_strtoull(options + 1, &options, 0);137else138baud = probe_baud(port);139140if (port)141early_serial_init(port, baud);142}143144void console_init(void)145{146parse_earlyprintk();147148if (!early_serial_base)149parse_console_uart8250();150}151152153