blob: 617a345508620b0601af8bc0ce982fd8329f3b04 [file] [log] [blame]
Prasad Kummarib72494e2023-09-19 22:15:05 +05301/*
Maheedhar Bollapalli73365f42024-12-04 04:12:53 +00002 * Copyright (c) 2023-2025, Advanced Micro Devices, Inc. All rights reserved.
Prasad Kummarib72494e2023-09-19 22:15:05 +05303 *
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 Kummari5eafc832023-11-03 16:47:26 +053021#include <plat_fdt.h>
Prasad Kummarib72494e2023-09-19 22:15:05 +053022
23#include <platform_def.h>
24#include <plat_private.h>
25
Michal Simekb954eb42024-09-10 15:55:04 +020026#if !(CONSOLE_IS(none))
Prasad Kummaria1b9b452024-03-07 15:28:11 +053027static console_t boot_console;
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053028static console_holder boot_hd_console;
29#if defined(CONSOLE_RUNTIME)
30static console_t runtime_console;
31static console_holder rt_hd_console;
32#endif
Prasad Kummarib72494e2023-09-19 22:15:05 +053033
Maheedhar Bollapalli496ca1c2024-12-04 04:05:04 +000034#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && (XLNX_DT_CFG == 1))
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053035static dt_uart_info_t dt_uart_info;
36#endif
37
Prasad Kummari80232f72024-03-14 15:19:10 +053038/**
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053039 * register_console() - Registers the uart with console list.
40 * @consoleh: Console holder structure with UART base address,
41 * UART clock, UART buad rate, flags & console type
Prasad Kummari80232f72024-03-14 15:19:10 +053042 * @console: Pointer to the console information structure.
Prasad Kummari80232f72024-03-14 15:19:10 +053043 */
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053044static void register_console(const console_holder *consoleh, console_t *console)
Prasad Kummari80232f72024-03-14 15:19:10 +053045{
46 int32_t rc = 0;
47
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053048 switch (consoleh->console_type) {
Prasad Kummari80232f72024-03-14 15:19:10 +053049#if defined(PLAT_zynqmp)
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053050 case CONSOLE_CDNS:
51 rc = console_cdns_register(consoleh->base,
52 consoleh->clk,
53 consoleh->baud_rate,
Prasad Kummari80232f72024-03-14 15:19:10 +053054 console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053055 break;
56#else
57 case CONSOLE_PL011:
58 rc = console_pl011_register(consoleh->base,
59 consoleh->clk,
60 consoleh->baud_rate,
Prasad Kummari80232f72024-03-14 15:19:10 +053061 console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053062 break;
Prasad Kummari80232f72024-03-14 15:19:10 +053063#endif
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053064 case CONSOLE_DCC:
Prasad Kummari80232f72024-03-14 15:19:10 +053065 rc = console_dcc_register(console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053066 break;
67 default:
Prasad Kummari80232f72024-03-14 15:19:10 +053068 INFO("Invalid console type\n");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053069 break;
Prasad Kummari80232f72024-03-14 15:19:10 +053070 }
71
72 if (rc == 0) {
73 panic();
74 }
75
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053076 console_set_scope(console, consoleh->console_scope);
Prasad Kummari80232f72024-03-14 15:19:10 +053077}
Prasad Kummari80232f72024-03-14 15:19:10 +053078
Maheedhar Bollapalli496ca1c2024-12-04 04:05:04 +000079#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && (XLNX_DT_CFG == 1))
Prasad Kummarib72494e2023-09-19 22:15:05 +053080/**
81 * get_baudrate() - Get the baudrate form DTB.
82 * @dtb: Address of the Device Tree Blob (DTB).
83 *
84 * Return: On success returns the baudrate; on failure returns an error.
85 */
86static int32_t get_baudrate(void *dtb)
87{
88 int node;
89 int32_t ret = 0;
90 const char *prop, *path;
91 char *end;
92 int32_t baud_rate = 0;
93
94 node = fdt_path_offset(dtb, "/secure-chosen");
95 if (node < 0) {
96 node = fdt_path_offset(dtb, "/chosen");
97 if (node < 0) {
98 ret = -FDT_ERR_NOTFOUND;
99 goto error;
100 }
101 }
102
103 prop = fdt_getprop(dtb, node, "stdout-path", NULL);
104 if (prop == NULL) {
105 ret = -FDT_ERR_NOTFOUND;
106 goto error;
107 }
108
109 /* Parse string serial0:115200n8 */
110 path = strchr(prop, ':');
111 if (!path) {
112 ret = -FDT_ERR_NOTFOUND;
113 goto error;
114 } else {
115
116 baud_rate = strtoul(path + 1, &end, 10);
117 if (baud_rate == 0 && end == path) {
118 ERROR("Conversion error occurred: %d\n", baud_rate);
119 ret = -FDT_ERR_NOTFOUND;
120 goto error;
121 }
122 ret = baud_rate;
123 }
124
125error:
126 return ret;
127}
128
129/**
130 * get_node_status() - Get the DTB node status.
131 * @dtb: Address of the Device Tree Blob (DTB).
132 * @node: Node address in the device tree.
133 *
134 * Return: On success, it returns 1; on failure, it returns an 0.
135 */
136static uint32_t get_node_status(void *dtb, int node)
137{
138 const char *status_cell;
139 uint32_t status = 0;
140
141 status_cell = fdt_getprop(dtb, node, "status", NULL);
142 if (!status_cell || strcmp(status_cell, "okay") == 0) {
143 status = 1;
144 } else {
145 status = 0;
146 }
147
148 return status;
149}
150
151/**
152 * fdt_add_uart_info() - Add DTB information to a UART structure.
153 * @info: Pointer to the UART information structure.
154 * @node: Node address in the device tree.
155 * @dtb: Address of the Device Tree Blob(DTB).
156 *
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530157 * Return: On success, it returns 0; on failure, it returns -1 or -FDT_ERR_NOTFOUND.
Prasad Kummarib72494e2023-09-19 22:15:05 +0530158 */
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530159static int32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb)
Prasad Kummarib72494e2023-09-19 22:15:05 +0530160{
161 uintptr_t base_addr;
162 const char *com;
Prasad Kummari4e5ebe52023-11-08 16:50:03 +0530163 int32_t ret = 0;
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530164 uint32_t status;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530165
166 com = fdt_getprop(dtb, node, "compatible", NULL);
167 if (com != NULL) {
168 strlcpy(info->compatible, com, sizeof(info->compatible));
169 } else {
170 ERROR("Compatible property not found in DTB node\n");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530171 ret = -FDT_ERR_NOTFOUND;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530172 goto error;
173 }
174
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530175 status = get_node_status(dtb, node);
176 if (status == 0) {
Prasad Kummaric143b572024-03-08 14:26:25 +0530177 ERROR("Uart node is disabled in DTB\n");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530178 ret = -FDT_ERR_NOTFOUND;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530179 goto error;
180 }
181
Prasad Kummaric143b572024-03-08 14:26:25 +0530182 if (strncmp(info->compatible, DT_UART_DCC_COMPAT, strlen(DT_UART_DCC_COMPAT)) != 0) {
183 ret = fdt_get_reg_props_by_index(dtb, node, 0, &base_addr, NULL);
184 if (ret >= 0) {
185 info->base = base_addr;
186 } else {
187 ERROR("Failed to retrieve base address. Error code: %d\n", ret);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530188 ret = -FDT_ERR_NOTFOUND;
Prasad Kummaric143b572024-03-08 14:26:25 +0530189 goto error;
190 }
191
192 info->baud_rate = get_baudrate(dtb);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530193
194 if (strncmp(info->compatible, DT_UART_CAD_COMPAT,
195 strlen(DT_UART_CAD_COMPAT)) == 0) {
196 info->console_type = CONSOLE_CDNS;
197 } else if (strncmp(info->compatible, DT_UART_PL011_COMPAT,
198 strlen(DT_UART_PL011_COMPAT)) == 0) {
199 info->console_type = CONSOLE_PL011;
200 } else {
201 ERROR("Incompatible uart node in DTB\n");
202 ret = -FDT_ERR_NOTFOUND;
203 }
204 } else {
205 info->console_type = CONSOLE_DCC;
Prasad Kummaric143b572024-03-08 14:26:25 +0530206 }
Prasad Kummarib72494e2023-09-19 22:15:05 +0530207
208error:
209 return ret;
210}
211
212/**
213 * fdt_get_uart_info() - Get the uart information form DTB.
214 * @info: Pointer to the UART information structure.
215 *
216 * Return: On success, it returns 0; on failure, it returns an error+reason.
217 */
218static int fdt_get_uart_info(dt_uart_info_t *info)
219{
Prasad Kummari4e5ebe52023-11-08 16:50:03 +0530220 int node = 0, ret = 0;
Maheedhar Bollapalli73365f42024-12-04 04:12:53 +0000221 void *dtb = (void *)plat_retrieve_dt_addr();
Prasad Kummarib72494e2023-09-19 22:15:05 +0530222
Prasad Kummari5eafc832023-11-03 16:47:26 +0530223 ret = is_valid_dtb(dtb);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530224 if (ret < 0) {
225 ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530226 goto error;
227 }
228
229 node = fdt_get_stdout_node_offset(dtb);
230 if (node < 0) {
231 ERROR("DT get stdout node failed : %d\n", node);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530232 goto error;
233 }
234
235 ret = fdt_add_uart_info(info, node, dtb);
236 if (ret < 0) {
237 ERROR("Failed to add DT UART info: %d\n", ret);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530238 goto error;
239 }
240
241error:
242 return ret;
243}
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530244#endif
Prasad Kummaric143b572024-03-08 14:26:25 +0530245
Prasad Kummarib72494e2023-09-19 22:15:05 +0530246void setup_console(void)
247{
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530248 /* This is hardcoded console setup just in case that DTB console fails */
249 boot_hd_console.base = (uintptr_t)UART_BASE;
250 boot_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530251 boot_hd_console.clk = get_uart_clk();
252 boot_hd_console.console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH;
253 boot_hd_console.console_type = UART_TYPE;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530254
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530255 /* For DT code decoding uncomment console registration below */
256 /* register_console(&boot_hd_console, &boot_console); */
257
Maheedhar Bollapalli496ca1c2024-12-04 04:05:04 +0000258#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && (XLNX_DT_CFG == 1))
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530259 /* Parse DTB console for UART information */
260 if (fdt_get_uart_info(&dt_uart_info) == 0) {
261 if (CONSOLE_IS(dtb)) {
262 boot_hd_console.base = dt_uart_info.base;
263 boot_hd_console.baud_rate = dt_uart_info.baud_rate;
264 boot_hd_console.console_type = dt_uart_info.console_type;
265 }
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530266 } else {
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530267 ERROR("Failed to initialize DT console or console node is disabled\n");
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530268 }
269#endif
270
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530271 /* Initialize the boot console */
272 register_console(&boot_hd_console, &boot_console);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530273
Michal Simek4e035e02024-09-10 14:53:18 +0200274 INFO("BL31: Early console setup\n");
275
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530276#ifdef CONSOLE_RUNTIME
Michal Simek4e035e02024-09-10 14:53:18 +0200277 rt_hd_console.base = (uintptr_t)RT_UART_BASE;
278 rt_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
279 rt_hd_console.console_type = RT_UART_TYPE;
Maheedhar Bollapallidb9c6362025-02-05 04:21:54 +0000280
281#if (RT_CONSOLE_IS(dtb) && (XLNX_DT_CFG == 1))
282 if (dt_uart_info.base != 0U) {
283 rt_hd_console.base = dt_uart_info.base;
284 rt_hd_console.baud_rate = dt_uart_info.baud_rate;
285 rt_hd_console.console_type = dt_uart_info.console_type;
286 }
Michal Simek4e035e02024-09-10 14:53:18 +0200287#endif
Prasad Kummarib72494e2023-09-19 22:15:05 +0530288
Michal Simek4e035e02024-09-10 14:53:18 +0200289 if ((rt_hd_console.console_type == boot_hd_console.console_type) &&
290 (rt_hd_console.base == boot_hd_console.base)) {
291 console_set_scope(&boot_console,
292 CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH | CONSOLE_FLAG_RUNTIME);
293 INFO("Successfully initialized runtime console\n");
294 } else {
295 rt_hd_console.clk = get_uart_clk();
296 rt_hd_console.console_scope = CONSOLE_FLAG_RUNTIME;
297
298 register_console(&rt_hd_console, &runtime_console);
299 INFO("Successfully initialized new runtime console\n");
Prasad Kummarib72494e2023-09-19 22:15:05 +0530300 }
Prasad Kummarib72494e2023-09-19 22:15:05 +0530301#endif
Prasad Kummarib72494e2023-09-19 22:15:05 +0530302}
Michal Simekb954eb42024-09-10 15:55:04 +0200303#else
304void setup_console(void)
305{
306}
307#endif