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>
18 #include <linux/sizes.h>
19 #include <linux/mod_devicetable.h>
21 #ifdef CONFIG_FIX_EARLYCON_MEM
22 #include <asm/fixmap.h>
25 #include <asm/serial.h>
27 static struct console early_con = {
28 .name = "uart", /* 8250 console switch requires this name */
29 .flags = CON_PRINTBUFFER | CON_BOOT,
33 static struct earlycon_device early_console_dev = {
37 static const struct of_device_id __earlycon_of_table_sentinel
38 __used __section(__earlycon_of_table_end);
40 static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
43 #ifdef CONFIG_FIX_EARLYCON_MEM
44 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
45 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
46 base += paddr & ~PAGE_MASK;
48 base = ioremap(paddr, size);
51 pr_err("%s: Couldn't map 0x%llx\n", __func__,
52 (unsigned long long)paddr);
57 static int __init parse_options(struct earlycon_device *device, char *options)
59 struct uart_port *port = &device->port;
63 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
66 switch (port->iotype) {
68 port->regshift = 2; /* fall-through */
79 port->uartclk = BASE_BAUD * 16;
82 device->baud = simple_strtoul(options, NULL, 0);
83 length = min(strcspn(options, " ") + 1,
84 (size_t)(sizeof(device->options)));
85 strlcpy(device->options, options, length);
88 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM32)
89 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
90 (port->iotype == UPIO_MEM32) ? "32" : "",
91 (unsigned long long)port->mapbase,
94 pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
101 int __init setup_earlycon(char *buf, const char *match,
102 int (*setup)(struct earlycon_device *, const char *))
106 struct uart_port *port = &early_console_dev.port;
108 if (!buf || !match || !setup)
112 if (strncmp(buf, match, len))
114 if (buf[len] && (buf[len] != ','))
119 err = parse_options(&early_console_dev, buf);
120 /* On parsing error, pass the options buf to the setup function */
125 port->membase = earlycon_map(port->mapbase, 64);
127 early_console_dev.con->data = &early_console_dev;
128 err = setup(&early_console_dev, buf);
131 if (!early_console_dev.con->write)
134 register_console(early_console_dev.con);
138 int __init of_setup_earlycon(unsigned long addr,
139 int (*setup)(struct earlycon_device *, const char *))
142 struct uart_port *port = &early_console_dev.port;
144 port->iotype = UPIO_MEM;
145 port->mapbase = addr;
146 port->uartclk = BASE_BAUD * 16;
147 port->membase = earlycon_map(addr, SZ_4K);
149 early_console_dev.con->data = &early_console_dev;
150 err = setup(&early_console_dev, NULL);
153 if (!early_console_dev.con->write)
157 register_console(early_console_dev.con);