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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/console.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
20 #include <linux/serial_core.h>
21 #include <linux/sizes.h>
22 #include <linux/mod_devicetable.h>
24 #ifdef CONFIG_FIX_EARLYCON_MEM
25 #include <asm/fixmap.h>
28 #include <asm/serial.h>
30 static struct console early_con = {
31 .name = "uart", /* 8250 console switch requires this name */
32 .flags = CON_PRINTBUFFER | CON_BOOT,
36 static struct earlycon_device early_console_dev = {
40 extern struct earlycon_id __earlycon_table[];
41 static const struct earlycon_id __earlycon_table_sentinel
42 __used __section(__earlycon_table_end);
44 static const struct of_device_id __earlycon_of_table_sentinel
45 __used __section(__earlycon_of_table_end);
47 static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
50 #ifdef CONFIG_FIX_EARLYCON_MEM
51 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
52 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
53 base += paddr & ~PAGE_MASK;
55 base = ioremap(paddr, size);
58 pr_err("%s: Couldn't map 0x%llx\n", __func__,
59 (unsigned long long)paddr);
64 static int __init parse_options(struct earlycon_device *device, char *options)
66 struct uart_port *port = &device->port;
70 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
73 switch (port->iotype) {
94 device->baud = simple_strtoul(options, NULL, 0);
95 length = min(strcspn(options, " ") + 1,
96 (size_t)(sizeof(device->options)));
97 strlcpy(device->options, options, length);
100 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
101 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
102 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
103 (port->iotype == UPIO_MEM) ? "" :
104 (port->iotype == UPIO_MEM16) ? "16" :
105 (port->iotype == UPIO_MEM32) ? "32" : "32be",
106 (unsigned long long)port->mapbase,
109 pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
116 static int __init register_earlycon(char *buf, const struct earlycon_id *match)
119 struct uart_port *port = &early_console_dev.port;
121 /* On parsing error, pass the options buf to the setup function */
122 if (buf && !parse_options(&early_console_dev, buf))
125 spin_lock_init(&port->lock);
126 port->uartclk = BASE_BAUD * 16;
128 port->membase = earlycon_map(port->mapbase, 64);
130 early_console_dev.con->data = &early_console_dev;
131 err = match->setup(&early_console_dev, buf);
134 if (!early_console_dev.con->write)
137 register_console(early_console_dev.con);
142 * setup_earlycon - match and register earlycon console
143 * @buf: earlycon param string
145 * Registers the earlycon console matching the earlycon specified
146 * in the param string @buf. Acceptable param strings are of the form
147 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
148 * <name>,0x<addr>,<options>
152 * Only for the third form does the earlycon setup() method receive the
153 * <options> string in the 'options' parameter; all other forms set
154 * the parameter to NULL.
156 * Returns 0 if an attempt to register the earlycon was made,
157 * otherwise negative error code
159 int __init setup_earlycon(char *buf)
161 const struct earlycon_id *match;
166 if (early_con.flags & CON_ENABLED)
169 for (match = __earlycon_table; match->name[0]; match++) {
170 size_t len = strlen(match->name);
172 if (strncmp(buf, match->name, len))
182 return register_earlycon(buf, match);
188 /* early_param wrapper for setup_earlycon() */
189 static int __init param_setup_earlycon(char *buf)
194 * Just 'earlycon' is a valid param for devicetree earlycons;
195 * don't generate a warning from parse_early_params() in that case
200 err = setup_earlycon(buf);
201 if (err == -ENOENT || err == -EALREADY)
205 early_param("earlycon", param_setup_earlycon);
207 int __init of_setup_earlycon(unsigned long addr,
208 int (*setup)(struct earlycon_device *, const char *))
211 struct uart_port *port = &early_console_dev.port;
213 spin_lock_init(&port->lock);
214 port->iotype = UPIO_MEM;
215 port->mapbase = addr;
216 port->uartclk = BASE_BAUD * 16;
217 port->membase = earlycon_map(addr, SZ_4K);
219 early_console_dev.con->data = &early_console_dev;
220 err = setup(&early_console_dev, NULL);
223 if (!early_console_dev.con->write)
227 register_console(early_console_dev.con);