xref: /OK3568_Linux_fs/kernel/drivers/tty/serial/earlycon.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2014 Linaro Ltd.
4*4882a593Smuzhiyun  * Author: Rob Herring <robh@kernel.org>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Based on 8250 earlycon:
7*4882a593Smuzhiyun  * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
8*4882a593Smuzhiyun  *	Bjorn Helgaas <bjorn.helgaas@hp.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/console.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/io.h>
17*4882a593Smuzhiyun #include <linux/serial_core.h>
18*4882a593Smuzhiyun #include <linux/sizes.h>
19*4882a593Smuzhiyun #include <linux/of.h>
20*4882a593Smuzhiyun #include <linux/of_fdt.h>
21*4882a593Smuzhiyun #include <linux/acpi.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #ifdef CONFIG_FIX_EARLYCON_MEM
24*4882a593Smuzhiyun #include <asm/fixmap.h>
25*4882a593Smuzhiyun #endif
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <asm/serial.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static struct console early_con = {
30*4882a593Smuzhiyun 	.name =		"uart",		/* fixed up at earlycon registration */
31*4882a593Smuzhiyun 	.flags =	CON_PRINTBUFFER | CON_BOOT,
32*4882a593Smuzhiyun 	.index =	0,
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static struct earlycon_device early_console_dev = {
36*4882a593Smuzhiyun 	.con = &early_con,
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun 
earlycon_map(resource_size_t paddr,size_t size)39*4882a593Smuzhiyun static void __iomem * __init earlycon_map(resource_size_t paddr, size_t size)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	void __iomem *base;
42*4882a593Smuzhiyun #ifdef CONFIG_FIX_EARLYCON_MEM
43*4882a593Smuzhiyun 	set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
44*4882a593Smuzhiyun 	base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
45*4882a593Smuzhiyun 	base += paddr & ~PAGE_MASK;
46*4882a593Smuzhiyun #else
47*4882a593Smuzhiyun 	base = ioremap(paddr, size);
48*4882a593Smuzhiyun #endif
49*4882a593Smuzhiyun 	if (!base)
50*4882a593Smuzhiyun 		pr_err("%s: Couldn't map %pa\n", __func__, &paddr);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	return base;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
earlycon_init(struct earlycon_device * device,const char * name)55*4882a593Smuzhiyun static void __init earlycon_init(struct earlycon_device *device,
56*4882a593Smuzhiyun 				 const char *name)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	struct console *earlycon = device->con;
59*4882a593Smuzhiyun 	const char *s;
60*4882a593Smuzhiyun 	size_t len;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	/* scan backwards from end of string for first non-numeral */
63*4882a593Smuzhiyun 	for (s = name + strlen(name);
64*4882a593Smuzhiyun 	     s > name && s[-1] >= '0' && s[-1] <= '9';
65*4882a593Smuzhiyun 	     s--)
66*4882a593Smuzhiyun 		;
67*4882a593Smuzhiyun 	if (*s)
68*4882a593Smuzhiyun 		earlycon->index = simple_strtoul(s, NULL, 10);
69*4882a593Smuzhiyun 	len = s - name;
70*4882a593Smuzhiyun 	strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
71*4882a593Smuzhiyun 	earlycon->data = &early_console_dev;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
earlycon_print_info(struct earlycon_device * device)74*4882a593Smuzhiyun static void __init earlycon_print_info(struct earlycon_device *device)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	struct console *earlycon = device->con;
77*4882a593Smuzhiyun 	struct uart_port *port = &device->port;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
80*4882a593Smuzhiyun 	    port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
81*4882a593Smuzhiyun 		pr_info("%s%d at MMIO%s %pa (options '%s')\n",
82*4882a593Smuzhiyun 			earlycon->name, earlycon->index,
83*4882a593Smuzhiyun 			(port->iotype == UPIO_MEM) ? "" :
84*4882a593Smuzhiyun 			(port->iotype == UPIO_MEM16) ? "16" :
85*4882a593Smuzhiyun 			(port->iotype == UPIO_MEM32) ? "32" : "32be",
86*4882a593Smuzhiyun 			&port->mapbase, device->options);
87*4882a593Smuzhiyun 	else
88*4882a593Smuzhiyun 		pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
89*4882a593Smuzhiyun 			earlycon->name, earlycon->index,
90*4882a593Smuzhiyun 			port->iobase, device->options);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
parse_options(struct earlycon_device * device,char * options)93*4882a593Smuzhiyun static int __init parse_options(struct earlycon_device *device, char *options)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun 	struct uart_port *port = &device->port;
96*4882a593Smuzhiyun 	int length;
97*4882a593Smuzhiyun 	resource_size_t addr;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
100*4882a593Smuzhiyun 		return -EINVAL;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	switch (port->iotype) {
103*4882a593Smuzhiyun 	case UPIO_MEM:
104*4882a593Smuzhiyun 		port->mapbase = addr;
105*4882a593Smuzhiyun 		break;
106*4882a593Smuzhiyun 	case UPIO_MEM16:
107*4882a593Smuzhiyun 		port->regshift = 1;
108*4882a593Smuzhiyun 		port->mapbase = addr;
109*4882a593Smuzhiyun 		break;
110*4882a593Smuzhiyun 	case UPIO_MEM32:
111*4882a593Smuzhiyun 	case UPIO_MEM32BE:
112*4882a593Smuzhiyun 		port->regshift = 2;
113*4882a593Smuzhiyun 		port->mapbase = addr;
114*4882a593Smuzhiyun 		break;
115*4882a593Smuzhiyun 	case UPIO_PORT:
116*4882a593Smuzhiyun 		port->iobase = addr;
117*4882a593Smuzhiyun 		break;
118*4882a593Smuzhiyun 	default:
119*4882a593Smuzhiyun 		return -EINVAL;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	if (options) {
123*4882a593Smuzhiyun 		device->baud = simple_strtoul(options, NULL, 0);
124*4882a593Smuzhiyun 		length = min(strcspn(options, " ") + 1,
125*4882a593Smuzhiyun 			     (size_t)(sizeof(device->options)));
126*4882a593Smuzhiyun 		strlcpy(device->options, options, length);
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return 0;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
register_earlycon(char * buf,const struct earlycon_id * match)132*4882a593Smuzhiyun static int __init register_earlycon(char *buf, const struct earlycon_id *match)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	int err;
135*4882a593Smuzhiyun 	struct uart_port *port = &early_console_dev.port;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/* On parsing error, pass the options buf to the setup function */
138*4882a593Smuzhiyun 	if (buf && !parse_options(&early_console_dev, buf))
139*4882a593Smuzhiyun 		buf = NULL;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	spin_lock_init(&port->lock);
142*4882a593Smuzhiyun 	port->uartclk = BASE_BAUD * 16;
143*4882a593Smuzhiyun 	if (port->mapbase)
144*4882a593Smuzhiyun 		port->membase = earlycon_map(port->mapbase, 64);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	earlycon_init(&early_console_dev, match->name);
147*4882a593Smuzhiyun 	err = match->setup(&early_console_dev, buf);
148*4882a593Smuzhiyun 	earlycon_print_info(&early_console_dev);
149*4882a593Smuzhiyun 	if (err < 0)
150*4882a593Smuzhiyun 		return err;
151*4882a593Smuzhiyun 	if (!early_console_dev.con->write)
152*4882a593Smuzhiyun 		return -ENODEV;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	register_console(early_console_dev.con);
155*4882a593Smuzhiyun 	return 0;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /**
159*4882a593Smuzhiyun  *	setup_earlycon - match and register earlycon console
160*4882a593Smuzhiyun  *	@buf:	earlycon param string
161*4882a593Smuzhiyun  *
162*4882a593Smuzhiyun  *	Registers the earlycon console matching the earlycon specified
163*4882a593Smuzhiyun  *	in the param string @buf. Acceptable param strings are of the form
164*4882a593Smuzhiyun  *	   <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
165*4882a593Smuzhiyun  *	   <name>,0x<addr>,<options>
166*4882a593Smuzhiyun  *	   <name>,<options>
167*4882a593Smuzhiyun  *	   <name>
168*4882a593Smuzhiyun  *
169*4882a593Smuzhiyun  *	Only for the third form does the earlycon setup() method receive the
170*4882a593Smuzhiyun  *	<options> string in the 'options' parameter; all other forms set
171*4882a593Smuzhiyun  *	the parameter to NULL.
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  *	Returns 0 if an attempt to register the earlycon was made,
174*4882a593Smuzhiyun  *	otherwise negative error code
175*4882a593Smuzhiyun  */
setup_earlycon(char * buf)176*4882a593Smuzhiyun int __init setup_earlycon(char *buf)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	const struct earlycon_id **p_match;
179*4882a593Smuzhiyun 	bool empty_compatible = true;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (!buf || !buf[0])
182*4882a593Smuzhiyun 		return -EINVAL;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	if (early_con.flags & CON_ENABLED)
185*4882a593Smuzhiyun 		return -EALREADY;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun again:
188*4882a593Smuzhiyun 	for (p_match = __earlycon_table; p_match < __earlycon_table_end;
189*4882a593Smuzhiyun 	     p_match++) {
190*4882a593Smuzhiyun 		const struct earlycon_id *match = *p_match;
191*4882a593Smuzhiyun 		size_t len = strlen(match->name);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		if (strncmp(buf, match->name, len))
194*4882a593Smuzhiyun 			continue;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		/* prefer entries with empty compatible */
197*4882a593Smuzhiyun 		if (empty_compatible && *match->compatible)
198*4882a593Smuzhiyun 			continue;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		if (buf[len]) {
201*4882a593Smuzhiyun 			if (buf[len] != ',')
202*4882a593Smuzhiyun 				continue;
203*4882a593Smuzhiyun 			buf += len + 1;
204*4882a593Smuzhiyun 		} else
205*4882a593Smuzhiyun 			buf = NULL;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 		return register_earlycon(buf, match);
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	if (empty_compatible) {
211*4882a593Smuzhiyun 		empty_compatible = false;
212*4882a593Smuzhiyun 		goto again;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	return -ENOENT;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun  * This defers the initialization of the early console until after ACPI has
220*4882a593Smuzhiyun  * been initialized.
221*4882a593Smuzhiyun  */
222*4882a593Smuzhiyun bool earlycon_acpi_spcr_enable __initdata;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun /* early_param wrapper for setup_earlycon() */
param_setup_earlycon(char * buf)225*4882a593Smuzhiyun static int __init param_setup_earlycon(char *buf)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	int err;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	/* Just 'earlycon' is a valid param for devicetree and ACPI SPCR. */
230*4882a593Smuzhiyun 	if (!buf || !buf[0]) {
231*4882a593Smuzhiyun 		if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
232*4882a593Smuzhiyun 			earlycon_acpi_spcr_enable = true;
233*4882a593Smuzhiyun 			return 0;
234*4882a593Smuzhiyun 		} else if (!buf) {
235*4882a593Smuzhiyun 			return early_init_dt_scan_chosen_stdout();
236*4882a593Smuzhiyun 		}
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	err = setup_earlycon(buf);
240*4882a593Smuzhiyun 	if (err == -ENOENT || err == -EALREADY)
241*4882a593Smuzhiyun 		return 0;
242*4882a593Smuzhiyun 	return err;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun early_param("earlycon", param_setup_earlycon);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun #ifdef CONFIG_OF_EARLY_FLATTREE
247*4882a593Smuzhiyun 
of_setup_earlycon(const struct earlycon_id * match,unsigned long node,const char * options)248*4882a593Smuzhiyun int __init of_setup_earlycon(const struct earlycon_id *match,
249*4882a593Smuzhiyun 			     unsigned long node,
250*4882a593Smuzhiyun 			     const char *options)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	int err;
253*4882a593Smuzhiyun 	struct uart_port *port = &early_console_dev.port;
254*4882a593Smuzhiyun 	const __be32 *val;
255*4882a593Smuzhiyun 	bool big_endian;
256*4882a593Smuzhiyun 	u64 addr;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	spin_lock_init(&port->lock);
259*4882a593Smuzhiyun 	port->iotype = UPIO_MEM;
260*4882a593Smuzhiyun 	addr = of_flat_dt_translate_address(node);
261*4882a593Smuzhiyun 	if (addr == OF_BAD_ADDR) {
262*4882a593Smuzhiyun 		pr_warn("[%s] bad address\n", match->name);
263*4882a593Smuzhiyun 		return -ENXIO;
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 	port->mapbase = addr;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	val = of_get_flat_dt_prop(node, "reg-offset", NULL);
268*4882a593Smuzhiyun 	if (val)
269*4882a593Smuzhiyun 		port->mapbase += be32_to_cpu(*val);
270*4882a593Smuzhiyun 	port->membase = earlycon_map(port->mapbase, SZ_4K);
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	val = of_get_flat_dt_prop(node, "reg-shift", NULL);
273*4882a593Smuzhiyun 	if (val)
274*4882a593Smuzhiyun 		port->regshift = be32_to_cpu(*val);
275*4882a593Smuzhiyun 	big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
276*4882a593Smuzhiyun 		(IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
277*4882a593Smuzhiyun 		 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
278*4882a593Smuzhiyun 	val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
279*4882a593Smuzhiyun 	if (val) {
280*4882a593Smuzhiyun 		switch (be32_to_cpu(*val)) {
281*4882a593Smuzhiyun 		case 1:
282*4882a593Smuzhiyun 			port->iotype = UPIO_MEM;
283*4882a593Smuzhiyun 			break;
284*4882a593Smuzhiyun 		case 2:
285*4882a593Smuzhiyun 			port->iotype = UPIO_MEM16;
286*4882a593Smuzhiyun 			break;
287*4882a593Smuzhiyun 		case 4:
288*4882a593Smuzhiyun 			port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
289*4882a593Smuzhiyun 			break;
290*4882a593Smuzhiyun 		default:
291*4882a593Smuzhiyun 			pr_warn("[%s] unsupported reg-io-width\n", match->name);
292*4882a593Smuzhiyun 			return -EINVAL;
293*4882a593Smuzhiyun 		}
294*4882a593Smuzhiyun 	}
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	val = of_get_flat_dt_prop(node, "current-speed", NULL);
297*4882a593Smuzhiyun 	if (val)
298*4882a593Smuzhiyun 		early_console_dev.baud = be32_to_cpu(*val);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	val = of_get_flat_dt_prop(node, "clock-frequency", NULL);
301*4882a593Smuzhiyun 	if (val)
302*4882a593Smuzhiyun 		port->uartclk = be32_to_cpu(*val);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (options) {
305*4882a593Smuzhiyun 		early_console_dev.baud = simple_strtoul(options, NULL, 0);
306*4882a593Smuzhiyun 		strlcpy(early_console_dev.options, options,
307*4882a593Smuzhiyun 			sizeof(early_console_dev.options));
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 	earlycon_init(&early_console_dev, match->name);
310*4882a593Smuzhiyun 	err = match->setup(&early_console_dev, options);
311*4882a593Smuzhiyun 	earlycon_print_info(&early_console_dev);
312*4882a593Smuzhiyun 	if (err < 0)
313*4882a593Smuzhiyun 		return err;
314*4882a593Smuzhiyun 	if (!early_console_dev.con->write)
315*4882a593Smuzhiyun 		return -ENODEV;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	register_console(early_console_dev.con);
319*4882a593Smuzhiyun 	return 0;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun #endif /* CONFIG_OF_EARLY_FLATTREE */
323