2 * Copyright (C) 2014 Linaro Ltd.
3 * Author: Rob Herring <robh@kernel.org>
5 * Based on 8250 earlycon:
6 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/console.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
17 #include <linux/serial_core.h>
19 #ifdef CONFIG_FIX_EARLYCON_MEM
20 #include <asm/fixmap.h>
23 #include <asm/serial.h>
25 static struct console early_con = {
26 .name = "uart", /* 8250 console switch requires this name */
27 .flags = CON_PRINTBUFFER | CON_BOOT,
31 static struct earlycon_device early_console_dev = {
35 static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
38 #ifdef CONFIG_FIX_EARLYCON_MEM
39 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
40 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
41 base += paddr & ~PAGE_MASK;
43 base = ioremap(paddr, size);
46 pr_err("%s: Couldn't map 0x%llx\n", __func__,
47 (unsigned long long)paddr);
52 static int __init parse_options(struct earlycon_device *device,
55 struct uart_port *port = &device->port;
56 int mmio, mmio32, length, ret;
62 mmio = !strncmp(options, "mmio,", 5);
63 mmio32 = !strncmp(options, "mmio32,", 7);
65 port->iotype = (mmio ? UPIO_MEM : UPIO_MEM32);
66 options += mmio ? 5 : 7;
67 ret = kstrtoul(options, 0, &addr);
73 } else if (!strncmp(options, "io,", 3)) {
74 port->iotype = UPIO_PORT;
76 ret = kstrtoul(options, 0, &addr);
81 } else if (!strncmp(options, "0x", 2)) {
82 port->iotype = UPIO_MEM;
83 ret = kstrtoul(options, 0, &addr);
91 port->uartclk = BASE_BAUD * 16;
93 options = strchr(options, ',');
96 ret = kstrtouint(options, 0, &device->baud);
99 length = min(strcspn(options, " ") + 1,
100 (size_t)(sizeof(device->options)));
101 strlcpy(device->options, options, length);
105 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
107 (unsigned long long)port->mapbase,
110 pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
117 int __init setup_earlycon(char *buf, const char *match,
118 int (*setup)(struct earlycon_device *, const char *))
122 struct uart_port *port = &early_console_dev.port;
124 if (!buf || !match || !setup)
128 if (strncmp(buf, match, len))
130 if (buf[len] && (buf[len] != ','))
135 err = parse_options(&early_console_dev, buf);
136 /* On parsing error, pass the options buf to the setup function */
141 port->membase = earlycon_map(port->mapbase, 64);
143 early_console_dev.con->data = &early_console_dev;
144 err = setup(&early_console_dev, buf);
147 if (!early_console_dev.con->write)
150 register_console(early_console_dev.con);