Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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., |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 33 | #include <linux/serial_8250.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/io.h> |
| 35 | #include <asm/serial.h> |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_FIX_EARLYCON_MEM |
| 37 | #include <asm/pgtable.h> |
| 38 | #include <asm/fixmap.h> |
| 39 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 41 | struct early_serial8250_device { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct uart_port port; |
| 43 | char options[16]; /* e.g., 115200n8 */ |
| 44 | unsigned int baud; |
| 45 | }; |
| 46 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 47 | static struct early_serial8250_device early_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | static 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 | |
| 57 | static 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 | |
| 67 | static 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 Lu | bf2cdef | 2007-11-05 14:50:53 -0800 | [diff] [blame] | 79 | static void __init serial_putc(struct uart_port *port, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | wait_for_xmitr(port); |
| 82 | serial_out(port, UART_TX, c); |
| 83 | } |
| 84 | |
Alan Cox | ce2e204 | 2008-02-08 04:18:49 -0800 | [diff] [blame] | 85 | static void __init early_serial8250_write(struct console *console, |
| 86 | const char *s, unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | struct uart_port *port = &early_device.port; |
| 89 | unsigned int ier; |
| 90 | |
| 91 | /* Save the IER and disable interrupts */ |
| 92 | ier = serial_in(port, UART_IER); |
| 93 | serial_out(port, UART_IER, 0); |
| 94 | |
Yinghai Lu | bf2cdef | 2007-11-05 14:50:53 -0800 | [diff] [blame] | 95 | uart_console_write(port, s, count, serial_putc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | /* Wait for transmitter to become empty and restore the IER */ |
| 98 | wait_for_xmitr(port); |
| 99 | serial_out(port, UART_IER, ier); |
| 100 | } |
| 101 | |
| 102 | static unsigned int __init probe_baud(struct uart_port *port) |
| 103 | { |
| 104 | unsigned char lcr, dll, dlm; |
| 105 | unsigned int quot; |
| 106 | |
| 107 | lcr = serial_in(port, UART_LCR); |
| 108 | serial_out(port, UART_LCR, lcr | UART_LCR_DLAB); |
| 109 | dll = serial_in(port, UART_DLL); |
| 110 | dlm = serial_in(port, UART_DLM); |
| 111 | serial_out(port, UART_LCR, lcr); |
| 112 | |
| 113 | quot = (dlm << 8) | dll; |
| 114 | return (port->uartclk / 16) / quot; |
| 115 | } |
| 116 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 117 | static void __init init_port(struct early_serial8250_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | struct uart_port *port = &device->port; |
| 120 | unsigned int divisor; |
| 121 | unsigned char c; |
| 122 | |
| 123 | serial_out(port, UART_LCR, 0x3); /* 8n1 */ |
| 124 | serial_out(port, UART_IER, 0); /* no interrupt */ |
| 125 | serial_out(port, UART_FCR, 0); /* no fifo */ |
| 126 | serial_out(port, UART_MCR, 0x3); /* DTR + RTS */ |
| 127 | |
| 128 | divisor = port->uartclk / (16 * device->baud); |
| 129 | c = serial_in(port, UART_LCR); |
| 130 | serial_out(port, UART_LCR, c | UART_LCR_DLAB); |
| 131 | serial_out(port, UART_DLL, divisor & 0xff); |
| 132 | serial_out(port, UART_DLM, (divisor >> 8) & 0xff); |
| 133 | serial_out(port, UART_LCR, c & ~UART_LCR_DLAB); |
| 134 | } |
| 135 | |
Alan Cox | ce2e204 | 2008-02-08 04:18:49 -0800 | [diff] [blame] | 136 | static int __init parse_options(struct early_serial8250_device *device, |
| 137 | char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | struct uart_port *port = &device->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | int mmio, length; |
| 141 | |
| 142 | if (!options) |
| 143 | return -ENODEV; |
| 144 | |
| 145 | port->uartclk = BASE_BAUD * 16; |
| 146 | if (!strncmp(options, "mmio,", 5)) { |
| 147 | port->iotype = UPIO_MEM; |
| 148 | port->mapbase = simple_strtoul(options + 5, &options, 0); |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 149 | #ifdef CONFIG_FIX_EARLYCON_MEM |
Alan Cox | ce2e204 | 2008-02-08 04:18:49 -0800 | [diff] [blame] | 150 | set_fixmap_nocache(FIX_EARLYCON_MEM_BASE, |
| 151 | port->mapbase & PAGE_MASK); |
| 152 | port->membase = |
| 153 | (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE); |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 154 | port->membase += port->mapbase & ~PAGE_MASK; |
| 155 | #else |
Alan Cox | 6f441fe | 2008-05-01 04:34:59 -0700 | [diff] [blame] | 156 | port->membase = ioremap_nocache(port->mapbase, 64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (!port->membase) { |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 158 | printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n", |
Harvey Harrison | 71cc2c2 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 159 | __func__, |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 160 | (unsigned long long)port->mapbase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return -ENOMEM; |
| 162 | } |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 163 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | mmio = 1; |
| 165 | } else if (!strncmp(options, "io,", 3)) { |
| 166 | port->iotype = UPIO_PORT; |
| 167 | port->iobase = simple_strtoul(options + 3, &options, 0); |
| 168 | mmio = 0; |
| 169 | } else |
| 170 | return -EINVAL; |
| 171 | |
Alan Cox | ce2e204 | 2008-02-08 04:18:49 -0800 | [diff] [blame] | 172 | options = strchr(options, ','); |
| 173 | if (options) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | options++; |
Ben Dooks | b2281ab | 2005-11-03 21:07:37 +0000 | [diff] [blame] | 175 | device->baud = simple_strtoul(options, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | length = min(strcspn(options, " "), sizeof(device->options)); |
| 177 | strncpy(device->options, options, length); |
| 178 | } else { |
| 179 | device->baud = probe_baud(port); |
| 180 | snprintf(device->options, sizeof(device->options), "%u", |
| 181 | device->baud); |
| 182 | } |
| 183 | |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 184 | printk(KERN_INFO "Early serial console at %s 0x%llx (options '%s')\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | mmio ? "MMIO" : "I/O port", |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 186 | mmio ? (unsigned long long) port->mapbase |
Alan Cox | ce2e204 | 2008-02-08 04:18:49 -0800 | [diff] [blame] | 187 | : (unsigned long long) port->iobase, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | device->options); |
| 189 | return 0; |
| 190 | } |
| 191 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 192 | static struct console early_serial8250_console __initdata = { |
| 193 | .name = "uart", |
| 194 | .write = early_serial8250_write, |
| 195 | .flags = CON_PRINTBUFFER | CON_BOOT, |
| 196 | .index = -1, |
| 197 | }; |
| 198 | |
| 199 | static int __init early_serial8250_setup(char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 201 | struct early_serial8250_device *device = &early_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | int err; |
| 203 | |
| 204 | if (device->port.membase || device->port.iobase) |
| 205 | return 0; |
| 206 | |
Alan Cox | ce2e204 | 2008-02-08 04:18:49 -0800 | [diff] [blame] | 207 | err = parse_options(device, options); |
| 208 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return err; |
| 210 | |
| 211 | init_port(device); |
| 212 | return 0; |
| 213 | } |
| 214 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 215 | int __init setup_early_serial8250_console(char *cmdline) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | char *options; |
| 218 | int err; |
| 219 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 220 | options = strstr(cmdline, "uart8250,"); |
| 221 | if (!options) { |
| 222 | options = strstr(cmdline, "uart,"); |
| 223 | if (!options) |
| 224 | return 0; |
| 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | options = strchr(cmdline, ',') + 1; |
Alan Cox | ce2e204 | 2008-02-08 04:18:49 -0800 | [diff] [blame] | 228 | err = early_serial8250_setup(options); |
| 229 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 232 | register_console(&early_serial8250_console); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | return 0; |
| 235 | } |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 236 | |
Daniel Ritz | b6b1d87 | 2007-08-03 16:07:43 +0200 | [diff] [blame] | 237 | int serial8250_find_port_for_earlycon(void) |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 238 | { |
| 239 | struct early_serial8250_device *device = &early_device; |
| 240 | struct uart_port *port = &device->port; |
| 241 | int line; |
| 242 | int ret; |
| 243 | |
| 244 | if (!device->port.membase && !device->port.iobase) |
| 245 | return -ENODEV; |
| 246 | |
| 247 | line = serial8250_find_port(port); |
| 248 | if (line < 0) |
| 249 | return -ENODEV; |
| 250 | |
| 251 | ret = update_console_cmdline("uart", 8250, |
| 252 | "ttyS", line, device->options); |
| 253 | if (ret < 0) |
| 254 | ret = update_console_cmdline("uart", 0, |
| 255 | "ttyS", line, device->options); |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | early_param("earlycon", setup_early_serial8250_console); |