xref: /optee_os/core/kernel/console.c (revision 817466cb476de705a8e3dabe1ef165fe27a18c2f)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017, Linaro Limited
4  */
5 
6 #include <console.h>
7 #include <compiler.h>
8 #include <drivers/serial.h>
9 #include <kernel/generic_boot.h>
10 #include <kernel/panic.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #ifdef CFG_DT
15 #include <kernel/dt.h>
16 #include <libfdt.h>
17 #endif
18 
19 static struct serial_chip *serial_console;
20 
21 void __weak console_putc(int ch)
22 {
23 	if (!serial_console)
24 		return;
25 
26 	if (ch == '\n')
27 		serial_console->ops->putc(serial_console, '\r');
28 	serial_console->ops->putc(serial_console, ch);
29 }
30 
31 void __weak console_flush(void)
32 {
33 	if (!serial_console)
34 		return;
35 
36 	serial_console->ops->flush(serial_console);
37 }
38 
39 void register_serial_console(struct serial_chip *chip)
40 {
41 	serial_console = chip;
42 }
43 
44 #ifdef CFG_DT
45 
46 /*
47  * Check if the /secure-chosen node in the DT contains an stdout-path value
48  * for which we have a compatible driver. If so, switch the console to
49  * this device.
50  */
51 void configure_console_from_dt(void)
52 {
53 	const struct dt_driver *dt_drv;
54 	const struct serial_driver *sdrv;
55 	const struct fdt_property *prop;
56 	struct serial_chip *dev;
57 	char *stdout_data;
58 	const char *uart;
59 	const char *parms = NULL;
60 	void *fdt = get_dt_blob();
61 	int offs;
62 	char *p;
63 
64 	if (!fdt)
65 		return;
66 
67 	offs = fdt_path_offset(fdt, "/secure-chosen");
68 	if (offs < 0)
69 		return;
70 	prop = fdt_get_property(fdt, offs, "stdout-path", NULL);
71 	if (!prop) {
72 		/*
73 		 * /secure-chosen node present but no stdout-path property
74 		 * means we don't want any console output
75 		 */
76 		IMSG("Switching off console");
77 		register_serial_console(NULL);
78 		return;
79 	}
80 
81 	stdout_data = strdup(prop->data);
82 	if (!stdout_data)
83 		return;
84 	p = strchr(stdout_data, ':');
85 	if (p) {
86 		*p = '\0';
87 		parms = p + 1;
88 	}
89 
90 	/* stdout-path may refer to an alias */
91 	uart = fdt_get_alias(fdt, stdout_data);
92 	if (!uart) {
93 		/* Not an alias, assume we have a node path */
94 		uart = stdout_data;
95 	}
96 	offs = fdt_path_offset(fdt, uart);
97 	if (offs < 0)
98 		goto out;
99 
100 	dt_drv = dt_find_compatible_driver(fdt, offs);
101 	if (!dt_drv)
102 		goto out;
103 
104 	sdrv = (const struct serial_driver *)dt_drv->driver;
105 	if (!sdrv)
106 		goto out;
107 	dev = sdrv->dev_alloc();
108 	if (!dev)
109 		goto out;
110 	/*
111 	 * If the console is the same as the early console, dev_init() might
112 	 * clear pending data. Flush to avoid that.
113 	 */
114 	console_flush();
115 	if (sdrv->dev_init(dev, fdt, offs, parms) < 0) {
116 		sdrv->dev_free(dev);
117 		goto out;
118 	}
119 
120 	IMSG("Switching console to device: %s", uart);
121 	register_serial_console(dev);
122 out:
123 	free(stdout_data);
124 }
125 
126 #endif /* CFG_DT */
127