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 static int find_chosen_node(void *fdt) 46 { 47 if (!fdt) 48 return -1; 49 50 int offset = fdt_path_offset(fdt, "/secure-chosen"); 51 52 if (offset < 0) 53 offset = fdt_path_offset(fdt, "/chosen"); 54 55 return offset; 56 } 57 58 /* 59 * Check if the /secure-chosen node in the DT contains an stdout-path value 60 * for which we have a compatible driver. If so, switch the console to 61 * this device. 62 */ 63 void configure_console_from_dt(void) 64 { 65 const struct dt_driver *dt_drv; 66 const struct serial_driver *sdrv; 67 const struct fdt_property *prop; 68 struct serial_chip *dev; 69 char *stdout_data; 70 const char *uart; 71 const char *parms = NULL; 72 void *fdt; 73 int offs; 74 char *p; 75 76 /* Probe console from secure DT and fallback to non-secure DT */ 77 fdt = get_embedded_dt(); 78 offs = find_chosen_node(fdt); 79 if (offs < 0) { 80 fdt = get_external_dt(); 81 offs = find_chosen_node(fdt); 82 } 83 if (offs < 0) { 84 DMSG("No console directive from DTB"); 85 return; 86 } 87 88 prop = fdt_get_property(fdt, offs, "stdout-path", NULL); 89 if (!prop) { 90 /* 91 * A secure-chosen or chosen node is present but defined 92 * no stdout-path property: no console expected 93 */ 94 IMSG("Switching off console"); 95 register_serial_console(NULL); 96 return; 97 } 98 99 stdout_data = strdup(prop->data); 100 if (!stdout_data) 101 return; 102 p = strchr(stdout_data, ':'); 103 if (p) { 104 *p = '\0'; 105 parms = p + 1; 106 } 107 108 /* stdout-path may refer to an alias */ 109 uart = fdt_get_alias(fdt, stdout_data); 110 if (!uart) { 111 /* Not an alias, assume we have a node path */ 112 uart = stdout_data; 113 } 114 offs = fdt_path_offset(fdt, uart); 115 if (offs < 0) 116 goto out; 117 118 dt_drv = dt_find_compatible_driver(fdt, offs); 119 if (!dt_drv) 120 goto out; 121 122 sdrv = (const struct serial_driver *)dt_drv->driver; 123 if (!sdrv) 124 goto out; 125 dev = sdrv->dev_alloc(); 126 if (!dev) 127 goto out; 128 /* 129 * If the console is the same as the early console, dev_init() might 130 * clear pending data. Flush to avoid that. 131 */ 132 console_flush(); 133 if (sdrv->dev_init(dev, fdt, offs, parms) < 0) { 134 sdrv->dev_free(dev); 135 goto out; 136 } 137 138 IMSG("Switching console to device: %s", uart); 139 register_serial_console(dev); 140 out: 141 free(stdout_data); 142 } 143 144 #endif /* CFG_DT */ 145