1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
5 * Copyright 2009 Intel Corporation; author H. Peter Anvin
7 * This file is part of the Linux kernel, and is made available under
8 * the terms of the GNU General Public License version 2.
10 * ----------------------------------------------------------------------- */
13 * Very simple screen and serial I/O
18 #define DEFAULT_SERIAL_PORT 0x3f8 /* ttyS0 */
20 static int early_serial_base;
26 #define TXR 0 /* Transmit register (WRITE) */
27 #define RXR 0 /* Receive register (READ) */
28 #define IER 1 /* Interrupt Enable */
29 #define IIR 2 /* Interrupt ID */
30 #define FCR 2 /* FIFO control */
31 #define LCR 3 /* Line control */
32 #define MCR 4 /* Modem control */
33 #define LSR 5 /* Line Status */
34 #define MSR 6 /* Modem Status */
35 #define DLL 0 /* Divisor Latch Low */
36 #define DLH 1 /* Divisor latch High */
38 #define DEFAULT_BAUD 9600
41 * These functions are in .inittext so they can be used to signal
42 * error during initialization.
45 static void __attribute__((section(".inittext"))) serial_putchar(int ch)
47 unsigned timeout = 0xffff;
49 while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
52 outb(ch, early_serial_base + TXR);
55 static void __attribute__((section(".inittext"))) bios_putchar(int ch)
64 intcall(0x10, &ireg, NULL);
67 void __attribute__((section(".inittext"))) putchar(int ch)
70 putchar('\r'); /* \n -> \r\n */
74 if (early_serial_base != 0)
78 void __attribute__((section(".inittext"))) puts(const char *str)
85 * Read the CMOS clock through the BIOS, and return the
89 static u8 gettime(void)
91 struct biosregs ireg, oreg;
95 intcall(0x1a, &ireg, &oreg);
101 * Read from the keyboard
105 struct biosregs ireg, oreg;
108 /* ireg.ah = 0x00; */
109 intcall(0x16, &ireg, &oreg);
114 static int kbd_pending(void)
116 struct biosregs ireg, oreg;
120 intcall(0x16, &ireg, &oreg);
122 return !(oreg.eflags & X86_EFLAGS_ZF);
134 int getchar_timeout(void)
152 return 0; /* Timeout! */
155 static void early_serial_init(int baud)
160 outb(0x3, early_serial_base + LCR); /* 8n1 */
161 outb(0, early_serial_base + IER); /* no interrupt */
162 outb(0, early_serial_base + FCR); /* no fifo */
163 outb(0x3, early_serial_base + MCR); /* DTR + RTS */
165 divisor = 115200 / baud;
166 c = inb(early_serial_base + LCR);
167 outb(c | DLAB, early_serial_base + LCR);
168 outb(divisor & 0xff, early_serial_base + DLL);
169 outb((divisor >> 8) & 0xff, early_serial_base + DLH);
170 outb(c & ~DLAB, early_serial_base + LCR);
173 static int parse_earlyprintk(void)
175 int baud = DEFAULT_BAUD;
179 if (cmdline_find_option("earlyprintk", arg, sizeof arg) > 0) {
182 if (!strncmp(arg, "serial", 6)) {
183 early_serial_base = DEFAULT_SERIAL_PORT;
190 if (!strncmp(arg, "ttyS", 4)) {
191 static const int bases[] = { 0x3f8, 0x2f8 };
194 if (!strncmp(arg + pos, "ttyS", 4))
197 if (arg[pos++] == '1')
200 early_serial_base = bases[port];
206 baud = simple_strtoull(arg + pos, &e, 0);
207 if (baud == 0 || arg + pos == e)
214 #define BASE_BAUD (1843200/16)
215 static unsigned int probe_baud(int port)
217 unsigned char lcr, dll, dlh;
220 lcr = inb(port + LCR);
221 outb(lcr | DLAB, port + LCR);
222 dll = inb(port + DLL);
223 dlh = inb(port + DLH);
224 outb(lcr, port + LCR);
225 quot = (dlh << 8) | dll;
227 return BASE_BAUD / quot;
230 static int parse_console_uart8250(void)
232 char optstr[64], *options;
233 int baud = DEFAULT_BAUD;
236 * console=uart8250,io,0x3f8,115200n8
237 * need to make sure it is last one console !
239 if (cmdline_find_option("console", optstr, sizeof optstr) <= 0)
244 if (!strncmp(options, "uart8250,io,", 12))
245 early_serial_base = simple_strtoull(options + 12, &options, 0);
246 else if (!strncmp(options, "uart,io,", 8))
247 early_serial_base = simple_strtoull(options + 8, &options, 0);
251 if (options && (options[0] == ','))
252 baud = simple_strtoull(options + 1, &options, 0);
254 baud = probe_baud(early_serial_base);
259 void console_init(void)
263 baud = parse_earlyprintk();
265 if (!early_serial_base)
266 baud = parse_console_uart8250();
268 if (early_serial_base != 0)
269 early_serial_init(baud);