blob: 1f16de719962829e181f4860040a83a6094dcfa7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Early serial console for 8250/16550 devices
3 *
4 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 *
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.
10 *
11 * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
12 * and on early_printk.c by Andi Kleen.
13 *
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.
18 *
19 * The user can specify the device directly, e.g.,
Yinghai Lu18a8bd92007-07-15 23:37:59 -070020 * earlycon=uart8250,io,0x3f8,9600n8
21 * earlycon=uart8250,mmio,0xff5e0000,115200n8
22 * or
23 * console=uart8250,io,0x3f8,9600n8
24 * console=uart8250,mmio,0xff5e0000,115200n8
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
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>
Yinghai Lu18a8bd92007-07-15 23:37:59 -070033#include <linux/serial_8250.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/io.h>
35#include <asm/serial.h>
Yinghai Lu18a8bd92007-07-15 23:37:59 -070036#ifdef CONFIG_FIX_EARLYCON_MEM
37#include <asm/pgtable.h>
38#include <asm/fixmap.h>
39#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Yinghai Lu18a8bd92007-07-15 23:37:59 -070041struct early_serial8250_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct uart_port port;
43 char options[16]; /* e.g., 115200n8 */
44 unsigned int baud;
45};
46
Yinghai Lu18a8bd92007-07-15 23:37:59 -070047static struct early_serial8250_device early_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static unsigned int __init serial_in(struct uart_port *port, int offset)
50{
51 if (port->iotype == UPIO_MEM)
52 return readb(port->membase + offset);
53 else
54 return inb(port->iobase + offset);
55}
56
57static void __init serial_out(struct uart_port *port, int offset, int value)
58{
59 if (port->iotype == UPIO_MEM)
60 writeb(value, port->membase + offset);
61 else
62 outb(value, port->iobase + offset);
63}
64
65#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
66
67static void __init wait_for_xmitr(struct uart_port *port)
68{
69 unsigned int status;
70
71 for (;;) {
72 status = serial_in(port, UART_LSR);
73 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
74 return;
75 cpu_relax();
76 }
77}
78
Yinghai Lubf2cdef2007-11-05 14:50:53 -080079static void __init serial_putc(struct uart_port *port, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 wait_for_xmitr(port);
82 serial_out(port, UART_TX, c);
83}
84
Yinghai Lu18a8bd92007-07-15 23:37:59 -070085static void __init early_serial8250_write(struct console *console, const char *s, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct uart_port *port = &early_device.port;
88 unsigned int ier;
89
90 /* Save the IER and disable interrupts */
91 ier = serial_in(port, UART_IER);
92 serial_out(port, UART_IER, 0);
93
Yinghai Lubf2cdef2007-11-05 14:50:53 -080094 uart_console_write(port, s, count, serial_putc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /* Wait for transmitter to become empty and restore the IER */
97 wait_for_xmitr(port);
98 serial_out(port, UART_IER, ier);
99}
100
101static unsigned int __init probe_baud(struct uart_port *port)
102{
103 unsigned char lcr, dll, dlm;
104 unsigned int quot;
105
106 lcr = serial_in(port, UART_LCR);
107 serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
108 dll = serial_in(port, UART_DLL);
109 dlm = serial_in(port, UART_DLM);
110 serial_out(port, UART_LCR, lcr);
111
112 quot = (dlm << 8) | dll;
113 return (port->uartclk / 16) / quot;
114}
115
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700116static void __init init_port(struct early_serial8250_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct uart_port *port = &device->port;
119 unsigned int divisor;
120 unsigned char c;
121
122 serial_out(port, UART_LCR, 0x3); /* 8n1 */
123 serial_out(port, UART_IER, 0); /* no interrupt */
124 serial_out(port, UART_FCR, 0); /* no fifo */
125 serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
126
127 divisor = port->uartclk / (16 * device->baud);
128 c = serial_in(port, UART_LCR);
129 serial_out(port, UART_LCR, c | UART_LCR_DLAB);
130 serial_out(port, UART_DLL, divisor & 0xff);
131 serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
132 serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
133}
134
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700135static int __init parse_options(struct early_serial8250_device *device, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct uart_port *port = &device->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int mmio, length;
139
140 if (!options)
141 return -ENODEV;
142
143 port->uartclk = BASE_BAUD * 16;
144 if (!strncmp(options, "mmio,", 5)) {
145 port->iotype = UPIO_MEM;
146 port->mapbase = simple_strtoul(options + 5, &options, 0);
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700147#ifdef CONFIG_FIX_EARLYCON_MEM
148 set_fixmap_nocache(FIX_EARLYCON_MEM_BASE, port->mapbase & PAGE_MASK);
149 port->membase = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
150 port->membase += port->mapbase & ~PAGE_MASK;
151#else
152 port->membase = ioremap(port->mapbase, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (!port->membase) {
Josh Boyer4f640ef2007-07-23 18:43:44 -0700154 printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n",
155 __FUNCTION__,
156 (unsigned long long)port->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -ENOMEM;
158 }
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 mmio = 1;
161 } else if (!strncmp(options, "io,", 3)) {
162 port->iotype = UPIO_PORT;
163 port->iobase = simple_strtoul(options + 3, &options, 0);
164 mmio = 0;
165 } else
166 return -EINVAL;
167
168 if ((options = strchr(options, ','))) {
169 options++;
Ben Dooksb2281ab2005-11-03 21:07:37 +0000170 device->baud = simple_strtoul(options, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 length = min(strcspn(options, " "), sizeof(device->options));
172 strncpy(device->options, options, length);
173 } else {
174 device->baud = probe_baud(port);
175 snprintf(device->options, sizeof(device->options), "%u",
176 device->baud);
177 }
178
Josh Boyer4f640ef2007-07-23 18:43:44 -0700179 printk(KERN_INFO "Early serial console at %s 0x%llx (options '%s')\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 mmio ? "MMIO" : "I/O port",
Josh Boyer4f640ef2007-07-23 18:43:44 -0700181 mmio ? (unsigned long long) port->mapbase
182 : (unsigned long long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 device->options);
184 return 0;
185}
186
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700187static struct console early_serial8250_console __initdata = {
188 .name = "uart",
189 .write = early_serial8250_write,
190 .flags = CON_PRINTBUFFER | CON_BOOT,
191 .index = -1,
192};
193
194static int __init early_serial8250_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700196 struct early_serial8250_device *device = &early_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int err;
198
199 if (device->port.membase || device->port.iobase)
200 return 0;
201
202 if ((err = parse_options(device, options)) < 0)
203 return err;
204
205 init_port(device);
206 return 0;
207}
208
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700209int __init setup_early_serial8250_console(char *cmdline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 char *options;
212 int err;
213
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700214 options = strstr(cmdline, "uart8250,");
215 if (!options) {
216 options = strstr(cmdline, "uart,");
217 if (!options)
218 return 0;
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 options = strchr(cmdline, ',') + 1;
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700222 if ((err = early_serial8250_setup(options)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700225 register_console(&early_serial8250_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 return 0;
228}
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700229
Daniel Ritzb6b1d872007-08-03 16:07:43 +0200230int serial8250_find_port_for_earlycon(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700231{
232 struct early_serial8250_device *device = &early_device;
233 struct uart_port *port = &device->port;
234 int line;
235 int ret;
236
237 if (!device->port.membase && !device->port.iobase)
238 return -ENODEV;
239
240 line = serial8250_find_port(port);
241 if (line < 0)
242 return -ENODEV;
243
244 ret = update_console_cmdline("uart", 8250,
245 "ttyS", line, device->options);
246 if (ret < 0)
247 ret = update_console_cmdline("uart", 0,
248 "ttyS", line, device->options);
249
250 return ret;
251}
252
253early_param("earlycon", setup_early_serial8250_console);