- Update to 2.6.25-rc3.
[linux-flexiantxendom0-3.2.10.git] / drivers / serial / 8250_early.c
1 /*
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.,
20  *      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
25  */
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>
33 #include <linux/serial_8250.h>
34 #include <asm/io.h>
35 #include <asm/serial.h>
36 #ifdef CONFIG_FIX_EARLYCON_MEM
37 #include <asm/pgtable.h>
38 #include <asm/fixmap.h>
39 #endif
40
41 #ifdef  CONFIG_KDB
42 #include <linux/kdb.h>
43 static int  kdb_serial_line = -1;
44 #endif  /* CONFIG_KDB */
45
46 struct early_serial8250_device {
47         struct uart_port port;
48         char options[16];               /* e.g., 115200n8 */
49         unsigned int baud;
50 };
51
52 static struct early_serial8250_device early_device;
53
54 static unsigned int __init serial_in(struct uart_port *port, int offset)
55 {
56         if (port->iotype == UPIO_MEM)
57                 return readb(port->membase + offset);
58         else
59                 return inb(port->iobase + offset);
60 }
61
62 static void __init serial_out(struct uart_port *port, int offset, int value)
63 {
64         if (port->iotype == UPIO_MEM)
65                 writeb(value, port->membase + offset);
66         else
67                 outb(value, port->iobase + offset);
68 }
69
70 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
71
72 static void __init wait_for_xmitr(struct uart_port *port)
73 {
74         unsigned int status;
75
76         for (;;) {
77                 status = serial_in(port, UART_LSR);
78                 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
79                         return;
80                 cpu_relax();
81         }
82 }
83
84 static void __init serial_putc(struct uart_port *port, int c)
85 {
86         wait_for_xmitr(port);
87         serial_out(port, UART_TX, c);
88 }
89
90 static void __init early_serial8250_write(struct console *console,
91                                         const char *s, unsigned int count)
92 {
93         struct uart_port *port = &early_device.port;
94         unsigned int ier;
95
96         /* Save the IER and disable interrupts */
97         ier = serial_in(port, UART_IER);
98         serial_out(port, UART_IER, 0);
99
100         uart_console_write(port, s, count, serial_putc);
101
102         /* Wait for transmitter to become empty and restore the IER */
103         wait_for_xmitr(port);
104         serial_out(port, UART_IER, ier);
105 }
106
107 static unsigned int __init probe_baud(struct uart_port *port)
108 {
109         unsigned char lcr, dll, dlm;
110         unsigned int quot;
111
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);
117
118         quot = (dlm << 8) | dll;
119         return (port->uartclk / 16) / quot;
120 }
121
122 static void __init init_port(struct early_serial8250_device *device)
123 {
124         struct uart_port *port = &device->port;
125         unsigned int divisor;
126         unsigned char c;
127
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 */
132
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);
139 }
140
141 static int __init parse_options(struct early_serial8250_device *device,
142                                                                 char *options)
143 {
144         struct uart_port *port = &device->port;
145         int mmio, length;
146
147         if (!options)
148                 return -ENODEV;
149
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);
157                 port->membase =
158                         (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
159                 port->membase += port->mapbase & ~PAGE_MASK;
160 #else
161                 port->membase = ioremap(port->mapbase, 64);
162                 if (!port->membase) {
163                         printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n",
164                                 __FUNCTION__,
165                                (unsigned long long)port->mapbase);
166                         return -ENOMEM;
167                 }
168 #endif
169                 mmio = 1;
170         } else if (!strncmp(options, "io,", 3)) {
171                 port->iotype = UPIO_PORT;
172                 port->iobase = simple_strtoul(options + 3, &options, 0);
173                 mmio = 0;
174         } else
175                 return -EINVAL;
176
177         options = strchr(options, ',');
178         if (options) {
179                 options++;
180                 device->baud = simple_strtoul(options, NULL, 0);
181                 length = min(strcspn(options, " "), sizeof(device->options));
182                 strncpy(device->options, options, length);
183         } else {
184                 device->baud = probe_baud(port);
185                 snprintf(device->options, sizeof(device->options), "%u",
186                         device->baud);
187         }
188
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,
193                 device->options);
194         return 0;
195 }
196
197 static struct console early_serial8250_console __initdata = {
198         .name   = "uart",
199         .write  = early_serial8250_write,
200         .flags  = CON_PRINTBUFFER | CON_BOOT,
201         .index  = -1,
202 };
203
204 static int __init early_serial8250_setup(char *options)
205 {
206         struct early_serial8250_device *device = &early_device;
207         int err;
208
209         if (device->port.membase || device->port.iobase)
210                 return 0;
211
212         err = parse_options(device, options);
213         if (err < 0)
214                 return err;
215
216         init_port(device);
217         return 0;
218 }
219
220 int __init setup_early_serial8250_console(char *cmdline)
221 {
222         char *options;
223         int err;
224
225         options = strstr(cmdline, "uart8250,");
226         if (!options) {
227                 options = strstr(cmdline, "uart,");
228                 if (!options)
229                         return 0;
230         }
231
232         options = strchr(cmdline, ',') + 1;
233         err = early_serial8250_setup(options);
234         if (err < 0)
235                 return err;
236
237         register_console(&early_serial8250_console);
238
239 #ifdef  CONFIG_KDB
240         /*
241          * Remember the line number of the first serial
242          * console.  We'll make this the kdb serial console too.
243          */
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) {
248                 case SERIAL_IO_MEM:
249 #ifdef  SERIAL_IO_MEM32
250                 case SERIAL_IO_MEM32:
251 #endif
252                         kdb_serial.iobase = (unsigned long)(early_device.port.membase);
253                         kdb_serial.ioreg_shift = early_device.port.regshift;
254                         break;
255                 default:
256                         kdb_serial.iobase = early_device.port.iobase;
257                         kdb_serial.ioreg_shift = 0;
258                         break;
259                 }
260         }
261 #endif  /* CONFIG_KDB */
262
263         return 0;
264 }
265
266 int serial8250_find_port_for_earlycon(void)
267 {
268         struct early_serial8250_device *device = &early_device;
269         struct uart_port *port = &device->port;
270         int line;
271         int ret;
272
273         if (!device->port.membase && !device->port.iobase)
274                 return -ENODEV;
275
276         line = serial8250_find_port(port);
277         if (line < 0)
278                 return -ENODEV;
279
280         ret = update_console_cmdline("uart", 8250,
281                              "ttyS", line, device->options);
282         if (ret < 0)
283                 ret = update_console_cmdline("uart", 0,
284                                      "ttyS", line, device->options);
285
286         return ret;
287 }
288
289 early_param("earlycon", setup_early_serial8250_console);