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>
23 #ifdef CONFIG_FIX_EARLYCON_MEM
24 #include <asm/fixmap.h>
27 #include <asm/serial.h>
29 static struct console early_con
= {
30 .name
= "uart", /* fixed up at earlycon registration */
31 .flags
= CON_PRINTBUFFER
| CON_BOOT
,
35 static struct earlycon_device early_console_dev
= {
39 static void __iomem
* __init
earlycon_map(unsigned long paddr
, size_t size
)
42 #ifdef CONFIG_FIX_EARLYCON_MEM
43 set_fixmap_io(FIX_EARLYCON_MEM_BASE
, paddr
& PAGE_MASK
);
44 base
= (void __iomem
*)__fix_to_virt(FIX_EARLYCON_MEM_BASE
);
45 base
+= paddr
& ~PAGE_MASK
;
47 base
= ioremap(paddr
, size
);
50 pr_err("%s: Couldn't map 0x%llx\n", __func__
,
51 (unsigned long long)paddr
);
56 static void __init
earlycon_init(struct earlycon_device
*device
,
59 struct console
*earlycon
= device
->con
;
63 /* scan backwards from end of string for first non-numeral */
64 for (s
= name
+ strlen(name
);
65 s
> name
&& s
[-1] >= '0' && s
[-1] <= '9';
69 earlycon
->index
= simple_strtoul(s
, NULL
, 10);
71 strlcpy(earlycon
->name
, name
, min(len
+ 1, sizeof(earlycon
->name
)));
72 earlycon
->data
= &early_console_dev
;
75 static int __init
parse_options(struct earlycon_device
*device
, char *options
)
77 struct uart_port
*port
= &device
->port
;
81 if (uart_parse_earlycon(options
, &port
->iotype
, &addr
, &options
))
84 switch (port
->iotype
) {
105 device
->baud
= simple_strtoul(options
, NULL
, 0);
106 length
= min(strcspn(options
, " ") + 1,
107 (size_t)(sizeof(device
->options
)));
108 strlcpy(device
->options
, options
, length
);
111 if (port
->iotype
== UPIO_MEM
|| port
->iotype
== UPIO_MEM16
||
112 port
->iotype
== UPIO_MEM32
|| port
->iotype
== UPIO_MEM32BE
)
113 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
114 (port
->iotype
== UPIO_MEM
) ? "" :
115 (port
->iotype
== UPIO_MEM16
) ? "16" :
116 (port
->iotype
== UPIO_MEM32
) ? "32" : "32be",
117 (unsigned long long)port
->mapbase
,
120 pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
127 static int __init
register_earlycon(char *buf
, const struct earlycon_id
*match
)
130 struct uart_port
*port
= &early_console_dev
.port
;
132 /* On parsing error, pass the options buf to the setup function */
133 if (buf
&& !parse_options(&early_console_dev
, buf
))
136 spin_lock_init(&port
->lock
);
137 port
->uartclk
= BASE_BAUD
* 16;
139 port
->membase
= earlycon_map(port
->mapbase
, 64);
141 earlycon_init(&early_console_dev
, match
->name
);
142 err
= match
->setup(&early_console_dev
, buf
);
145 if (!early_console_dev
.con
->write
)
148 register_console(early_console_dev
.con
);
153 * setup_earlycon - match and register earlycon console
154 * @buf: earlycon param string
156 * Registers the earlycon console matching the earlycon specified
157 * in the param string @buf. Acceptable param strings are of the form
158 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
159 * <name>,0x<addr>,<options>
163 * Only for the third form does the earlycon setup() method receive the
164 * <options> string in the 'options' parameter; all other forms set
165 * the parameter to NULL.
167 * Returns 0 if an attempt to register the earlycon was made,
168 * otherwise negative error code
170 int __init
setup_earlycon(char *buf
)
172 const struct earlycon_id
*match
;
177 if (early_con
.flags
& CON_ENABLED
)
180 for (match
= __earlycon_table
; match
< __earlycon_table_end
; match
++) {
181 size_t len
= strlen(match
->name
);
183 if (strncmp(buf
, match
->name
, len
))
193 return register_earlycon(buf
, match
);
199 /* early_param wrapper for setup_earlycon() */
200 static int __init
param_setup_earlycon(char *buf
)
205 * Just 'earlycon' is a valid param for devicetree earlycons;
206 * don't generate a warning from parse_early_params() in that case
211 err
= setup_earlycon(buf
);
212 if (err
== -ENOENT
|| err
== -EALREADY
)
216 early_param("earlycon", param_setup_earlycon
);
218 #ifdef CONFIG_OF_EARLY_FLATTREE
220 int __init
of_setup_earlycon(unsigned long addr
,
221 const struct earlycon_id
*match
,
225 struct uart_port
*port
= &early_console_dev
.port
;
227 spin_lock_init(&port
->lock
);
228 port
->iotype
= UPIO_MEM
;
229 port
->mapbase
= addr
;
230 port
->uartclk
= BASE_BAUD
* 16;
231 port
->membase
= earlycon_map(addr
, SZ_4K
);
234 strlcpy(early_console_dev
.options
, options
,
235 sizeof(early_console_dev
.options
));
237 earlycon_init(&early_console_dev
, match
->name
);
238 err
= match
->setup(&early_console_dev
, options
);
241 if (!early_console_dev
.con
->write
)
245 register_console(early_console_dev
.con
);
249 #endif /* CONFIG_OF_EARLY_FLATTREE */