blob: cf09ffb290c08aad0fb04c90102f80c13acc7ff0 [file] [log] [blame]
Prasad Kummari39234622023-09-19 22:15:05 +05301/*
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>
Prasad Kummarie2d9dfe2023-11-03 16:47:26 +053021#include <plat_fdt.h>
Prasad Kummari39234622023-09-19 22:15:05 +053022
23#include <platform_def.h>
24#include <plat_private.h>
25
Prasad Kummaria542b9c2024-03-07 15:28:11 +053026static console_t boot_console;
Prasad Kummari39234622023-09-19 22:15:05 +053027
28#if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE))
29/**
30 * get_baudrate() - Get the baudrate form DTB.
31 * @dtb: Address of the Device Tree Blob (DTB).
32 *
33 * Return: On success returns the baudrate; on failure returns an error.
34 */
35static int32_t get_baudrate(void *dtb)
36{
37 int node;
38 int32_t ret = 0;
39 const char *prop, *path;
40 char *end;
41 int32_t baud_rate = 0;
42
43 node = fdt_path_offset(dtb, "/secure-chosen");
44 if (node < 0) {
45 node = fdt_path_offset(dtb, "/chosen");
46 if (node < 0) {
47 ret = -FDT_ERR_NOTFOUND;
48 goto error;
49 }
50 }
51
52 prop = fdt_getprop(dtb, node, "stdout-path", NULL);
53 if (prop == NULL) {
54 ret = -FDT_ERR_NOTFOUND;
55 goto error;
56 }
57
58 /* Parse string serial0:115200n8 */
59 path = strchr(prop, ':');
60 if (!path) {
61 ret = -FDT_ERR_NOTFOUND;
62 goto error;
63 } else {
64
65 baud_rate = strtoul(path + 1, &end, 10);
66 if (baud_rate == 0 && end == path) {
67 ERROR("Conversion error occurred: %d\n", baud_rate);
68 ret = -FDT_ERR_NOTFOUND;
69 goto error;
70 }
71 ret = baud_rate;
72 }
73
74error:
75 return ret;
76}
77
78/**
79 * get_node_status() - Get the DTB node status.
80 * @dtb: Address of the Device Tree Blob (DTB).
81 * @node: Node address in the device tree.
82 *
83 * Return: On success, it returns 1; on failure, it returns an 0.
84 */
85static uint32_t get_node_status(void *dtb, int node)
86{
87 const char *status_cell;
88 uint32_t status = 0;
89
90 status_cell = fdt_getprop(dtb, node, "status", NULL);
91 if (!status_cell || strcmp(status_cell, "okay") == 0) {
92 status = 1;
93 } else {
94 status = 0;
95 }
96
97 return status;
98}
99
100/**
101 * fdt_add_uart_info() - Add DTB information to a UART structure.
102 * @info: Pointer to the UART information structure.
103 * @node: Node address in the device tree.
104 * @dtb: Address of the Device Tree Blob(DTB).
105 *
106 * Return: On success, it returns 1; on failure, it returns an 0.
107 */
108static uint32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb)
109{
110 uintptr_t base_addr;
111 const char *com;
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530112 int32_t ret = 0;
Prasad Kummari39234622023-09-19 22:15:05 +0530113
114 com = fdt_getprop(dtb, node, "compatible", NULL);
115 if (com != NULL) {
116 strlcpy(info->compatible, com, sizeof(info->compatible));
117 } else {
118 ERROR("Compatible property not found in DTB node\n");
119 ret = -FDT_ERR_NOTFOUND;
120 goto error;
121 }
122
123 ret = fdt_get_reg_props_by_index(dtb, node, 0, &base_addr, NULL);
124 if (ret >= 0) {
125 info->base = base_addr;
126 } else {
127 ERROR("Failed to retrieve base address. Error code: %d\n", ret);
128 ret = -FDT_ERR_NOTFOUND;
129 goto error;
130 }
131
132 info->status = get_node_status(dtb, node);
133 info->baud_rate = get_baudrate(dtb);
134
135error:
136 return ret;
137}
138
139/**
140 * fdt_get_uart_info() - Get the uart information form DTB.
141 * @info: Pointer to the UART information structure.
142 *
143 * Return: On success, it returns 0; on failure, it returns an error+reason.
144 */
145static int fdt_get_uart_info(dt_uart_info_t *info)
146{
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530147 int node = 0, ret = 0;
Prasad Kummari39234622023-09-19 22:15:05 +0530148 void *dtb = (void *)XILINX_OF_BOARD_DTB_ADDR;
149
Prasad Kummarie2d9dfe2023-11-03 16:47:26 +0530150 ret = is_valid_dtb(dtb);
Prasad Kummari39234622023-09-19 22:15:05 +0530151 if (ret < 0) {
152 ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
153 ret = -FDT_ERR_NOTFOUND;
154 goto error;
155 }
156
157 node = fdt_get_stdout_node_offset(dtb);
158 if (node < 0) {
159 ERROR("DT get stdout node failed : %d\n", node);
160 ret = -FDT_ERR_NOTFOUND;
161 goto error;
162 }
163
164 ret = fdt_add_uart_info(info, node, dtb);
165 if (ret < 0) {
166 ERROR("Failed to add DT UART info: %d\n", ret);
167 ret = -FDT_ERR_NOTFOUND;
168 goto error;
169 }
170
171error:
172 return ret;
173}
174
175/**
176 * check_fdt_uart_info() - Check early uart info with DTB uart info.
177 * @info: Pointer to the UART information structure.
178 *
179 * Return: On success, it returns 0; on failure, it returns an error+reason.
180 */
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530181static int32_t check_fdt_uart_info(dt_uart_info_t *info)
Prasad Kummari39234622023-09-19 22:15:05 +0530182{
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530183 int32_t ret = 0;
Prasad Kummari39234622023-09-19 22:15:05 +0530184
185 if (info->status == 0) {
186 ret = -ENODEV;
187 goto error;
188 }
189
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530190 if ((info->base == boot_console.base) &&
Prasad Kummari39234622023-09-19 22:15:05 +0530191 (info->baud_rate == UART_BAUDRATE) && !CONSOLE_IS(dcc)) {
192 ret = -ENODEV;
193 goto error;
194 }
195
196error:
197 return ret;
198}
199
200/**
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530201 * console_end() - Unregister the console_t instance form the console list.
202 * @console: Pointer to the console information structure.
Prasad Kummari39234622023-09-19 22:15:05 +0530203 */
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530204static void console_end(console_t *console)
Prasad Kummari39234622023-09-19 22:15:05 +0530205{
206 if (CONSOLE_IS(dcc)) {
207 console_dcc_unregister();
208 } else {
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530209 if (console != NULL) {
210 console_flush();
211 (void)console_unregister(console);
212 }
Prasad Kummari39234622023-09-19 22:15:05 +0530213 }
214}
215
216/**
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530217 * register_console() - Registers the runtime uart with console list.
218 * @uart_base: UART base address
Prasad Kummari39234622023-09-19 22:15:05 +0530219 * @clock: UART clock.
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530220 * @baud_rate: UART buad rate
221 * @console: Pointer to the console information structure.
222 * @flags: console flags.
Prasad Kummari39234622023-09-19 22:15:05 +0530223 */
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530224static void register_console(uintptr_t uart_base, uint32_t clock,
225 uint32_t baud_rate, console_t *console,
226 uint32_t flags)
Prasad Kummari39234622023-09-19 22:15:05 +0530227{
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530228 int32_t rc;
Prasad Kummari39234622023-09-19 22:15:05 +0530229
230#if defined(PLAT_zynqmp)
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530231 rc = console_cdns_register(uart_base,
Prasad Kummari39234622023-09-19 22:15:05 +0530232 clock,
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530233 baud_rate,
234 console);
Prasad Kummari39234622023-09-19 22:15:05 +0530235#else
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530236 rc = console_pl011_register(uart_base,
Prasad Kummari39234622023-09-19 22:15:05 +0530237 clock,
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530238 baud_rate,
239 console);
Prasad Kummari39234622023-09-19 22:15:05 +0530240#endif
241 if (rc == 0) {
242 panic();
243 }
244
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530245 console_set_scope(console, flags);
Prasad Kummari39234622023-09-19 22:15:05 +0530246}
247
Prasad Kummari39234622023-09-19 22:15:05 +0530248/**
Prasad Kummari00a68422024-03-07 15:10:04 +0530249 * dt_console_init() - Initializes the DT console information.
Prasad Kummari39234622023-09-19 22:15:05 +0530250 * @uart_info: Pointer to the UART information structure.
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530251 * @console: Pointer to the console information structure.
Prasad Kummari39234622023-09-19 22:15:05 +0530252 * @clock: UART clock.
253 *
254 * Return: On success, it returns 0; on failure, it returns an error+reason;
255 */
Prasad Kummari00a68422024-03-07 15:10:04 +0530256static int32_t dt_console_init(dt_uart_info_t *uart_info,
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530257 console_t *console,
Prasad Kummari39234622023-09-19 22:15:05 +0530258 uint32_t clock)
259{
260 int32_t rc = 0;
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530261 static console_t dt_console;
Prasad Kummari39234622023-09-19 22:15:05 +0530262
263 /* Parse UART information from Device Tree Blob (DTB) */
264 rc = fdt_get_uart_info(uart_info);
265 if (rc < 0) {
266 rc = -FDT_ERR_NOTFOUND;
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530267 goto error;
Prasad Kummari39234622023-09-19 22:15:05 +0530268 }
269
270 if (strncmp(uart_info->compatible, DT_UART_COMPAT,
271 strlen(DT_UART_COMPAT)) == 0) {
272
273 if (check_fdt_uart_info(uart_info) == 0) {
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530274 register_console(uart_info->base, clock,
275 uart_info->baud_rate, &dt_console,
276 CONSOLE_FLAG_BOOT | CONSOLE_FLAG_RUNTIME
277 | CONSOLE_FLAG_CRASH);
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530278 console_end(console);
Prasad Kummarif84a4c52024-03-07 16:01:55 +0530279 INFO("DTB console setup\n");
Prasad Kummari39234622023-09-19 22:15:05 +0530280 } else {
281 INFO("Early console and DTB console are same\n");
282 }
283 } else if (strncmp(uart_info->compatible, DT_UART_DCC_COMPAT,
284 strlen(DT_UART_DCC_COMPAT)) == 0) {
285 rc = console_dcc_register();
286 if (rc == 0) {
287 panic();
288 }
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530289 console_end(console);
Prasad Kummari39234622023-09-19 22:15:05 +0530290 } else {
291 WARN("BL31: No console device found in DT.\n");
292 }
293
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530294error:
Prasad Kummari39234622023-09-19 22:15:05 +0530295 return rc;
296}
297#endif
298
299void setup_console(void)
300{
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530301 int32_t rc;
Prasad Kummari39234622023-09-19 22:15:05 +0530302 uint32_t uart_clk = get_uart_clk();
303
304#if defined(PLAT_zynqmp)
305 if (CONSOLE_IS(cadence) || (CONSOLE_IS(cadence1))) {
306 rc = console_cdns_register(UART_BASE,
307 uart_clk,
308 UART_BAUDRATE,
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530309 &boot_console);
Prasad Kummari39234622023-09-19 22:15:05 +0530310 if (rc == 0) {
311 panic();
312 }
313
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530314 console_set_scope(&boot_console, CONSOLE_FLAG_BOOT |
Prasad Kummari39234622023-09-19 22:15:05 +0530315 CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH);
316 }
317#else
318 if (CONSOLE_IS(pl011) || (CONSOLE_IS(pl011_1))) {
319 /* Initialize the console to provide early debug support */
320 rc = console_pl011_register((uint32_t)UART_BASE,
321 uart_clk,
322 (uint32_t)UART_BAUDRATE,
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530323 &boot_console);
Prasad Kummari39234622023-09-19 22:15:05 +0530324 if (rc == 0) {
325 panic();
326 }
327
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530328 console_set_scope(&boot_console, CONSOLE_FLAG_BOOT |
Prasad Kummari39234622023-09-19 22:15:05 +0530329 CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH);
330 }
331#endif
332 if (CONSOLE_IS(dcc)) {
333 /* Initialize the dcc console for debug */
334 rc = console_dcc_register();
335 if (rc == 0) {
336 panic();
337 }
338 }
339 INFO("BL31: Early console setup\n");
340
341#if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE))
342 static dt_uart_info_t uart_info = {0};
343
Prasad Kummari00a68422024-03-07 15:10:04 +0530344 /* Initialize the DTB console using UART information from the DTB */
Prasad Kummaria542b9c2024-03-07 15:28:11 +0530345 rc = dt_console_init(&uart_info, &boot_console, uart_clk);
Prasad Kummari39234622023-09-19 22:15:05 +0530346 if (rc < 0) {
Prasad Kummari00a68422024-03-07 15:10:04 +0530347 ERROR("Failed to initialize DT console: %d\n", rc);
Prasad Kummari39234622023-09-19 22:15:05 +0530348 }
349#endif
350}