xref: /rk3399_ARM-atf/plat/xilinx/common/plat_console.c (revision cca2b86597c288c165cf66ab1c116e0d64ed2cd6)
1 /*
2  * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <common/debug.h>
13 #include <common/fdt_fixup.h>
14 #include <common/fdt_wrappers.h>
15 #include <drivers/arm/dcc.h>
16 #include <drivers/arm/pl011.h>
17 #include <drivers/cadence/cdns_uart.h>
18 #include <drivers/console.h>
19 #include <libfdt.h>
20 #include <plat_console.h>
21 #include <plat_fdt.h>
22 
23 #include <platform_def.h>
24 #include <plat_private.h>
25 
26 static console_t boot_console;
27 static console_holder boot_hd_console;
28 #if defined(CONSOLE_RUNTIME)
29 static console_t runtime_console;
30 static console_holder rt_hd_console;
31 #endif
32 
33 #if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
34 	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
35 				   !IS_TFA_IN_OCM(BL31_BASE)))
36 static dt_uart_info_t dt_uart_info;
37 #endif
38 
39 /**
40  * register_console() - Registers the uart with console list.
41  * @consoleh: Console holder structure with UART base address,
42  *  UART clock, UART buad rate, flags & console type
43  * @console: Pointer to the console information structure.
44  */
45 static void register_console(const console_holder *consoleh, console_t *console)
46 {
47 	int32_t rc = 0;
48 
49 	switch (consoleh->console_type) {
50 #if defined(PLAT_zynqmp)
51 	case CONSOLE_CDNS:
52 		rc = console_cdns_register(consoleh->base,
53 				consoleh->clk,
54 				consoleh->baud_rate,
55 				console);
56 		break;
57 #else
58 	case CONSOLE_PL011:
59 		rc = console_pl011_register(consoleh->base,
60 				consoleh->clk,
61 				consoleh->baud_rate,
62 				console);
63 		break;
64 #endif
65 	case CONSOLE_DCC:
66 		rc = console_dcc_register(console);
67 		break;
68 	default:
69 		INFO("Invalid console type\n");
70 		break;
71 	}
72 
73 	if (rc == 0) {
74 		panic();
75 	}
76 
77 	console_set_scope(console, consoleh->console_scope);
78 }
79 
80 #if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
81 	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
82 				   !IS_TFA_IN_OCM(BL31_BASE)))
83 /**
84  * get_baudrate() - Get the baudrate form DTB.
85  * @dtb: Address of the Device Tree Blob (DTB).
86  *
87  * Return: On success returns the baudrate; on failure returns an error.
88  */
89 static int32_t get_baudrate(void *dtb)
90 {
91 	int node;
92 	int32_t ret = 0;
93 	const char *prop, *path;
94 	char *end;
95 	int32_t baud_rate = 0;
96 
97 	node = fdt_path_offset(dtb, "/secure-chosen");
98 	if (node < 0) {
99 		node = fdt_path_offset(dtb, "/chosen");
100 		if (node < 0) {
101 			ret = -FDT_ERR_NOTFOUND;
102 			goto error;
103 		}
104 	}
105 
106 	prop = fdt_getprop(dtb, node, "stdout-path", NULL);
107 	if (prop == NULL) {
108 		ret = -FDT_ERR_NOTFOUND;
109 		goto error;
110 	}
111 
112 	/* Parse string serial0:115200n8 */
113 	path = strchr(prop, ':');
114 	if (!path) {
115 		ret = -FDT_ERR_NOTFOUND;
116 		goto error;
117 	} else {
118 
119 		baud_rate = strtoul(path + 1, &end, 10);
120 		if (baud_rate == 0 && end == path) {
121 			ERROR("Conversion error occurred: %d\n", baud_rate);
122 			ret = -FDT_ERR_NOTFOUND;
123 			goto error;
124 		}
125 		ret = baud_rate;
126 	}
127 
128 error:
129 	return ret;
130 }
131 
132 /**
133  * get_node_status() - Get the DTB node status.
134  * @dtb: Address of the Device Tree Blob (DTB).
135  * @node: Node address in the device tree.
136  *
137  * Return: On success, it returns 1; on failure, it returns an 0.
138  */
139 static uint32_t get_node_status(void *dtb, int node)
140 {
141 	const char *status_cell;
142 	uint32_t status = 0;
143 
144 	status_cell = fdt_getprop(dtb, node, "status", NULL);
145 	if (!status_cell || strcmp(status_cell, "okay") == 0) {
146 		status = 1;
147 	} else {
148 		status = 0;
149 	}
150 
151 	return status;
152 }
153 
154 /**
155  * fdt_add_uart_info() - Add DTB information to a UART structure.
156  * @info: Pointer to the UART information structure.
157  * @node: Node address in the device tree.
158  * @dtb: Address of the Device Tree Blob(DTB).
159  *
160  * Return: On success, it returns 0; on failure, it returns -1 or -FDT_ERR_NOTFOUND.
161  */
162 static int32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb)
163 {
164 	uintptr_t base_addr;
165 	const char *com;
166 	int32_t ret = 0;
167 	uint32_t status;
168 
169 	com = fdt_getprop(dtb, node, "compatible", NULL);
170 	if (com != NULL) {
171 		strlcpy(info->compatible, com, sizeof(info->compatible));
172 	} else {
173 		ERROR("Compatible property not found in DTB node\n");
174 		ret = -FDT_ERR_NOTFOUND;
175 		goto error;
176 	}
177 
178 	status = get_node_status(dtb, node);
179 	if (status == 0) {
180 		ERROR("Uart node is disabled in DTB\n");
181 		ret = -FDT_ERR_NOTFOUND;
182 		goto error;
183 	}
184 
185 	if (strncmp(info->compatible, DT_UART_DCC_COMPAT, strlen(DT_UART_DCC_COMPAT)) != 0) {
186 		ret = fdt_get_reg_props_by_index(dtb, node, 0, &base_addr, NULL);
187 		if (ret >= 0) {
188 			info->base = base_addr;
189 		} else {
190 			ERROR("Failed to retrieve base address. Error code: %d\n", ret);
191 			ret = -FDT_ERR_NOTFOUND;
192 			goto error;
193 		}
194 
195 		info->baud_rate = get_baudrate(dtb);
196 
197 		if (strncmp(info->compatible, DT_UART_CAD_COMPAT,
198 					strlen(DT_UART_CAD_COMPAT)) == 0) {
199 			info->console_type = CONSOLE_CDNS;
200 		} else if (strncmp(info->compatible, DT_UART_PL011_COMPAT,
201 					strlen(DT_UART_PL011_COMPAT)) == 0) {
202 			info->console_type = CONSOLE_PL011;
203 		} else {
204 			ERROR("Incompatible uart node in DTB\n");
205 			ret = -FDT_ERR_NOTFOUND;
206 		}
207 	} else {
208 		info->console_type = CONSOLE_DCC;
209 	}
210 
211 error:
212 	return ret;
213 }
214 
215 /**
216  * fdt_get_uart_info() - Get the uart information form DTB.
217  * @info: Pointer to the UART information structure.
218  *
219  * Return: On success, it returns 0; on failure, it returns an error+reason.
220  */
221 static int fdt_get_uart_info(dt_uart_info_t *info)
222 {
223 	int node = 0, ret = 0;
224 	void *dtb = (void *)XILINX_OF_BOARD_DTB_ADDR;
225 
226 	ret = is_valid_dtb(dtb);
227 	if (ret < 0) {
228 		ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
229 		goto error;
230 	}
231 
232 	node = fdt_get_stdout_node_offset(dtb);
233 	if (node < 0) {
234 		ERROR("DT get stdout node failed : %d\n", node);
235 		goto error;
236 	}
237 
238 	ret = fdt_add_uart_info(info, node, dtb);
239 	if (ret < 0) {
240 		ERROR("Failed to add DT UART info: %d\n", ret);
241 		goto error;
242 	}
243 
244 error:
245 	return ret;
246 }
247 #endif
248 
249 void setup_console(void)
250 {
251 	/* This is hardcoded console setup just in case that DTB console fails */
252 	boot_hd_console.base = (uintptr_t)UART_BASE;
253 	boot_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
254 	boot_hd_console.clk = get_uart_clk();
255 	boot_hd_console.console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH;
256 	boot_hd_console.console_type = UART_TYPE;
257 
258 	/* For DT code decoding uncomment console registration below */
259 	/* register_console(&boot_hd_console, &boot_console); */
260 
261 #if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
262 	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
263 				   !IS_TFA_IN_OCM(BL31_BASE)))
264 	/* Parse DTB console for UART information  */
265 	if (fdt_get_uart_info(&dt_uart_info) == 0) {
266 		if (CONSOLE_IS(dtb)) {
267 			boot_hd_console.base = dt_uart_info.base;
268 			boot_hd_console.baud_rate = dt_uart_info.baud_rate;
269 			boot_hd_console.console_type = dt_uart_info.console_type;
270 		}
271 	} else {
272 		ERROR("Failed to initialize DT console or console node is disabled\n");
273 	}
274 #endif
275 
276 	/* Initialize the boot console */
277 	register_console(&boot_hd_console, &boot_console);
278 
279 	INFO("BL31: Early console setup\n");
280 
281 #ifdef CONSOLE_RUNTIME
282 #if (RT_CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
283 	       (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
284 					!IS_TFA_IN_OCM(BL31_BASE)))
285 	/* DT based runtime console */
286 	if (dt_uart_info.console_type != CONSOLE_NONE) {
287 		rt_hd_console.base = dt_uart_info.base;
288 		rt_hd_console.baud_rate = dt_uart_info.baud_rate;
289 		rt_hd_console.console_type = dt_uart_info.console_type;
290 	}
291 #else
292 	rt_hd_console.base = (uintptr_t)RT_UART_BASE;
293 	rt_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
294 	rt_hd_console.console_type = RT_UART_TYPE;
295 #endif
296 
297 	if ((rt_hd_console.console_type == boot_hd_console.console_type) &&
298 			(rt_hd_console.base == boot_hd_console.base)) {
299 		console_set_scope(&boot_console,
300 				CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH | CONSOLE_FLAG_RUNTIME);
301 		INFO("Successfully initialized runtime console\n");
302 	} else {
303 		rt_hd_console.clk = get_uart_clk();
304 		rt_hd_console.console_scope = CONSOLE_FLAG_RUNTIME;
305 
306 		register_console(&rt_hd_console, &runtime_console);
307 		INFO("Successfully initialized new runtime console\n");
308 	}
309 #endif
310 }
311