2 * Early serial console for 8250/16550 devices
4 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
12 * and on early_printk.c by Andi Kleen.
14 * This is for use before the serial driver has initialized, in
15 * particular, before the UARTs have been discovered and named.
16 * Instead of specifying the console device as, e.g., "ttyS0",
17 * we locate the device directly by its MMIO or I/O port address.
19 * The user can specify the device directly, e.g.,
20 * earlycon=uart8250,io,0x3f8,9600n8
21 * earlycon=uart8250,mmio,0xff5e0000,115200n8
23 * console=uart8250,io,0x3f8,9600n8
24 * console=uart8250,mmio,0xff5e0000,115200n8
27 #include <linux/tty.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/serial_core.h>
31 #include <linux/serial_reg.h>
32 #include <linux/serial.h>
33 #include <linux/serial_8250.h>
35 #include <asm/serial.h>
36 #ifdef CONFIG_FIX_EARLYCON_MEM
37 #include <asm/pgtable.h>
38 #include <asm/fixmap.h>
42 #include <linux/kdb.h>
43 static int kdb_serial_line = -1;
44 #endif /* CONFIG_KDB */
46 struct early_serial8250_device {
47 struct uart_port port;
48 char options[16]; /* e.g., 115200n8 */
52 static struct early_serial8250_device early_device;
54 static unsigned int __init serial_in(struct uart_port *port, int offset)
56 if (port->iotype == UPIO_MEM)
57 return readb(port->membase + offset);
59 return inb(port->iobase + offset);
62 static void __init serial_out(struct uart_port *port, int offset, int value)
64 if (port->iotype == UPIO_MEM)
65 writeb(value, port->membase + offset);
67 outb(value, port->iobase + offset);
70 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
72 static void __init wait_for_xmitr(struct uart_port *port)
77 status = serial_in(port, UART_LSR);
78 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
84 static void __init serial_putc(struct uart_port *port, int c)
87 serial_out(port, UART_TX, c);
90 static void __init early_serial8250_write(struct console *console,
91 const char *s, unsigned int count)
93 struct uart_port *port = &early_device.port;
96 /* Save the IER and disable interrupts */
97 ier = serial_in(port, UART_IER);
98 serial_out(port, UART_IER, 0);
100 uart_console_write(port, s, count, serial_putc);
102 /* Wait for transmitter to become empty and restore the IER */
103 wait_for_xmitr(port);
104 serial_out(port, UART_IER, ier);
107 static unsigned int __init probe_baud(struct uart_port *port)
109 unsigned char lcr, dll, dlm;
112 lcr = serial_in(port, UART_LCR);
113 serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
114 dll = serial_in(port, UART_DLL);
115 dlm = serial_in(port, UART_DLM);
116 serial_out(port, UART_LCR, lcr);
118 quot = (dlm << 8) | dll;
119 return (port->uartclk / 16) / quot;
122 static void __init init_port(struct early_serial8250_device *device)
124 struct uart_port *port = &device->port;
125 unsigned int divisor;
128 serial_out(port, UART_LCR, 0x3); /* 8n1 */
129 serial_out(port, UART_IER, 0); /* no interrupt */
130 serial_out(port, UART_FCR, 0); /* no fifo */
131 serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
133 divisor = port->uartclk / (16 * device->baud);
134 c = serial_in(port, UART_LCR);
135 serial_out(port, UART_LCR, c | UART_LCR_DLAB);
136 serial_out(port, UART_DLL, divisor & 0xff);
137 serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
138 serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
141 static int __init parse_options(struct early_serial8250_device *device,
144 struct uart_port *port = &device->port;
150 port->uartclk = BASE_BAUD * 16;
151 if (!strncmp(options, "mmio,", 5)) {
152 port->iotype = UPIO_MEM;
153 port->mapbase = simple_strtoul(options + 5, &options, 0);
154 #ifdef CONFIG_FIX_EARLYCON_MEM
155 set_fixmap_nocache(FIX_EARLYCON_MEM_BASE,
156 port->mapbase & PAGE_MASK);
158 (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
159 port->membase += port->mapbase & ~PAGE_MASK;
161 port->membase = ioremap(port->mapbase, 64);
162 if (!port->membase) {
163 printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n",
165 (unsigned long long)port->mapbase);
170 } else if (!strncmp(options, "io,", 3)) {
171 port->iotype = UPIO_PORT;
172 port->iobase = simple_strtoul(options + 3, &options, 0);
177 options = strchr(options, ',');
180 device->baud = simple_strtoul(options, NULL, 0);
181 length = min(strcspn(options, " "), sizeof(device->options));
182 strncpy(device->options, options, length);
184 device->baud = probe_baud(port);
185 snprintf(device->options, sizeof(device->options), "%u",
189 printk(KERN_INFO "Early serial console at %s 0x%llx (options '%s')\n",
190 mmio ? "MMIO" : "I/O port",
191 mmio ? (unsigned long long) port->mapbase
192 : (unsigned long long) port->iobase,
197 static struct console early_serial8250_console __initdata = {
199 .write = early_serial8250_write,
200 .flags = CON_PRINTBUFFER | CON_BOOT,
204 static int __init early_serial8250_setup(char *options)
206 struct early_serial8250_device *device = &early_device;
209 if (device->port.membase || device->port.iobase)
212 err = parse_options(device, options);
220 int __init setup_early_serial8250_console(char *cmdline)
225 options = strstr(cmdline, "uart8250,");
227 options = strstr(cmdline, "uart,");
232 options = strchr(cmdline, ',') + 1;
233 err = early_serial8250_setup(options);
237 register_console(&early_serial8250_console);
241 * Remember the line number of the first serial
242 * console. We'll make this the kdb serial console too.
244 if (kdb_serial_line == -1) {
245 kdb_serial_line = early_serial8250_console.index;
246 kdb_serial.io_type = early_device.port.iotype;
247 switch (early_device.port.iotype) {
249 #ifdef SERIAL_IO_MEM32
250 case SERIAL_IO_MEM32:
252 kdb_serial.iobase = (unsigned long)(early_device.port.membase);
253 kdb_serial.ioreg_shift = early_device.port.regshift;
256 kdb_serial.iobase = early_device.port.iobase;
257 kdb_serial.ioreg_shift = 0;
261 #endif /* CONFIG_KDB */
266 int serial8250_find_port_for_earlycon(void)
268 struct early_serial8250_device *device = &early_device;
269 struct uart_port *port = &device->port;
273 if (!device->port.membase && !device->port.iobase)
276 line = serial8250_find_port(port);
280 ret = update_console_cmdline("uart", 8250,
281 "ttyS", line, device->options);
283 ret = update_console_cmdline("uart", 0,
284 "ttyS", line, device->options);
289 early_param("earlycon", setup_early_serial8250_console);