refactor(xilinx): console registration through console holder structure

Refactored register_console using console holder structure as
input. Structure holds console scope and console type as additional
members. These modifications enhance code readability and
maintainability, contributing to a clearer and more sustainable
codebase for future development.

Change-Id: I7fcc1accfdecdacc205d427a80031536c456638e
Signed-off-by: Maheedhar Bollapalli <MaheedharSai.Bollapalli@amd.com>
diff --git a/plat/xilinx/common/include/plat_console.h b/plat/xilinx/common/include/plat_console.h
index b38f347..9f212ec 100644
--- a/plat/xilinx/common/include/plat_console.h
+++ b/plat/xilinx/common/include/plat_console.h
@@ -8,24 +8,31 @@
 #define PLAT_DT_UART_H
 
 #define DT_UART_DCC_COMPAT	"arm,dcc"
+#define DT_UART_CAD_COMPAT	"xlnx,zynqmp-uart"
+#define DT_UART_PL011_COMPAT	"arm,pl011"
 
-#if defined(PLAT_zynqmp)
-#define DT_UART_COMPAT	"xlnx,zynqmp-uart"
-#else
-#define DT_UART_COMPAT	"arm,pl011"
-#endif
-
-/*Default console type is either CADENCE0 or CADENCE1 or PL011_0 or PL011_1
+/* Default console type is either CADENCE0 or CADENCE1 or PL011_0 or PL011_1
  * Debug console type is DCC
- **/
-#define PLAT_XLNX_CONSOLE_TYPE_DEFAULT 0
-#define PLAT_XLNX_CONSOLE_TYPE_DEBUG   1
+ */
+#define CONSOLE_CDNS	0
+#define CONSOLE_PL011   1
+#define CONSOLE_DCC	2
+
+typedef struct console_hd {
+	uint8_t console_state;
+	uint32_t clk;
+	uint32_t baud_rate;
+	uintptr_t base;
+	uint32_t console_scope;
+	uint8_t console_type;
+} console_holder;
 
 typedef struct dt_uart_info_s {
 	char compatible[30];
 	uintptr_t base;
 	uint32_t baud_rate;
 	int32_t status;
+	uint8_t console_type;
 } dt_uart_info_t;
 
 void setup_console(void);
diff --git a/plat/xilinx/common/plat_console.c b/plat/xilinx/common/plat_console.c
index f253534..5192ac0 100644
--- a/plat/xilinx/common/plat_console.c
+++ b/plat/xilinx/common/plat_console.c
@@ -24,55 +24,62 @@
 #include <plat_private.h>
 
 static console_t boot_console;
+static console_holder boot_hd_console;
+#if defined(CONSOLE_RUNTIME)
+static console_t runtime_console;
+static console_holder rt_hd_console;
+#endif
 
-#if (CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
+#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
 	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
-				   !IS_TFA_IN_OCM(BL31_BASE))) || defined(CONSOLE_RUNTIME)
+				   !IS_TFA_IN_OCM(BL31_BASE)))
+static dt_uart_info_t dt_uart_info;
+#endif
+
 /**
- * register_console() - Registers the runtime uart with console list.
- * @uart_base: UART base address
- * @clock: UART clock.
- * @baud_rate: UART buad rate
+ * register_console() - Registers the uart with console list.
+ * @consoleh: Console holder structure with UART base address,
+ *  UART clock, UART buad rate, flags & console type
  * @console: Pointer to the console information structure.
- * @flags: console flags.
  */
-static void register_console(uintptr_t uart_base, uint32_t clock,
-		uint32_t baud_rate, console_t *console,
-		uint32_t flags, uint8_t console_type)
+static void register_console(const console_holder *consoleh, console_t *console)
 {
 	int32_t rc = 0;
 
-	if (console_type == PLAT_XLNX_CONSOLE_TYPE_DEFAULT) {
+	switch (consoleh->console_type) {
 #if defined(PLAT_zynqmp)
-		rc = console_cdns_register(uart_base,
-				clock,
-				baud_rate,
+	case CONSOLE_CDNS:
+		rc = console_cdns_register(consoleh->base,
+				consoleh->clk,
+				consoleh->baud_rate,
 				console);
-#elif defined(PLAT_versal) || defined(PLAT_versal_net) || defined(PLAT_versal2)
-		rc = console_pl011_register(uart_base,
-				clock,
-				baud_rate,
+		break;
+#else
+	case CONSOLE_PL011:
+		rc = console_pl011_register(consoleh->base,
+				consoleh->clk,
+				consoleh->baud_rate,
 				console);
+		break;
 #endif
-	} else if (console_type == PLAT_XLNX_CONSOLE_TYPE_DEBUG) {
+	case CONSOLE_DCC:
 		rc = console_dcc_register(console);
-	} else {
+		break;
+	default:
 		INFO("Invalid console type\n");
+		break;
 	}
 
 	if (rc == 0) {
 		panic();
 	}
 
-	console_set_scope(console, flags);
+	console_set_scope(console, consoleh->console_scope);
 }
-#endif
-
 
-#if (CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
+#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
 	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
 				   !IS_TFA_IN_OCM(BL31_BASE)))
-static console_t dt_console;
 /**
  * get_baudrate() - Get the baudrate form DTB.
  * @dtb: Address of the Device Tree Blob (DTB).
@@ -150,27 +157,28 @@
  * @node: Node address in the device tree.
  * @dtb: Address of the Device Tree Blob(DTB).
  *
- * Return: On success, it returns 1; on failure, it returns an 0.
+ * Return: On success, it returns 0; on failure, it returns -1 or -FDT_ERR_NOTFOUND.
  */
-static uint32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb)
+static int32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb)
 {
 	uintptr_t base_addr;
 	const char *com;
 	int32_t ret = 0;
+	uint32_t status;
 
 	com = fdt_getprop(dtb, node, "compatible", NULL);
 	if (com != NULL) {
 		strlcpy(info->compatible, com, sizeof(info->compatible));
 	} else {
 		ERROR("Compatible property not found in DTB node\n");
-		ret  = -FDT_ERR_NOTFOUND;
+		ret = -FDT_ERR_NOTFOUND;
 		goto error;
 	}
 
-	info->status = get_node_status(dtb, node);
-	if (info->status == 0) {
+	status = get_node_status(dtb, node);
+	if (status == 0) {
 		ERROR("Uart node is disabled in DTB\n");
-		ret  = -FDT_ERR_NOTFOUND;
+		ret = -FDT_ERR_NOTFOUND;
 		goto error;
 	}
 
@@ -180,11 +188,24 @@
 			info->base = base_addr;
 		} else {
 			ERROR("Failed to retrieve base address. Error code: %d\n", ret);
-			ret  = -FDT_ERR_NOTFOUND;
+			ret = -FDT_ERR_NOTFOUND;
 			goto error;
 		}
 
 		info->baud_rate = get_baudrate(dtb);
+
+		if (strncmp(info->compatible, DT_UART_CAD_COMPAT,
+					strlen(DT_UART_CAD_COMPAT)) == 0) {
+			info->console_type = CONSOLE_CDNS;
+		} else if (strncmp(info->compatible, DT_UART_PL011_COMPAT,
+					strlen(DT_UART_PL011_COMPAT)) == 0) {
+			info->console_type = CONSOLE_PL011;
+		} else {
+			ERROR("Incompatible uart node in DTB\n");
+			ret = -FDT_ERR_NOTFOUND;
+		}
+	} else {
+		info->console_type = CONSOLE_DCC;
 	}
 
 error:
@@ -223,262 +244,99 @@
 error:
 	return ret;
 }
-
-/**
- * check_fdt_uart_info() - Check early uart info with DTB uart info.
- * @info: Pointer to the UART information structure.
- *
- * Return: On success, it returns 0; on failure, it returns an error+reason.
- */
-static int32_t check_fdt_uart_info(dt_uart_info_t *info)
-{
-	int32_t ret = 0;
-
-	if (info->status == 0) {
-		ret = -ENODEV;
-		goto error;
-	}
-
-	if ((info->base == boot_console.base) &&
-	   (info->baud_rate == UART_BAUDRATE) && !CONSOLE_IS(dcc)) {
-		ret = -ENODEV;
-		goto error;
-	}
-
-error:
-	return ret;
-}
-
-/**
- * console_end() - Unregister the console_t instance form the console list.
- * @console: Pointer to the console information structure.
- */
-static void console_end(console_t *console)
-{
-	if (CONSOLE_IS(dcc)) {
-		console_dcc_unregister(console);
-	} else {
-		if (console != NULL) {
-			console_flush();
-			(void)console_unregister(console);
-		}
-	}
-}
-
-/**
- * parse_uart_info() - Parse UART information from Device Tree Blob.
- * @uart_info: Pointer to the UART information structure.
- *
- * Return: On success, it returns 0; on failure, it returns an error+reason;
- */
-static int32_t parse_uart_info(dt_uart_info_t *uart_info)
-{
-	int32_t rc = fdt_get_uart_info(uart_info);
-
-	if (rc < 0) {
-		rc = -FDT_ERR_NOTFOUND;
-	}
-	return rc;
-}
-
-/**
- * handle_dt_console() - Registers the DT console
- * @uart_info: Pointer to the UART information structure.
- * @console: Pointer to the console information structure.
- * @clock: UART clock.
- * @end_console: Pointer to the console information structure.
- */
-static void handle_dt_console(dt_uart_info_t *uart_info, console_t *console,
-		uint32_t clock, console_t *end_console)
-{
-	register_console(uart_info->base, clock, uart_info->baud_rate,
-			console,  CONSOLE_FLAG_BOOT |
-			CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH,
-			PLAT_XLNX_CONSOLE_TYPE_DEFAULT);
-	console_end(end_console);
-	INFO("DTB console setup\n");
-}
-
-
-/**
- * handle_dcc_console() - Registers the DCC console
- * @console: Pointer to the console information structure.
- */
-static void handle_dcc_console(console_t *console)
-{
-	int32_t rc = console_dcc_register(console);
-
-	if (rc == 0) {
-		panic();
-	}
-	console_end(console);
-}
+#endif
 
+#if defined(CONSOLE_RUNTIME)
 /**
- * dt_console_init() - Initializes the DT console information.
- * @uart_info: Pointer to the UART information structure.
- * @console: Pointer to the console information structure.
- * @clock: UART clock.
+ * get_rt_console() - Retrieves console data for runtime console.
  *
- * Return: On success, it returns 0; on failure, it returns an error+reason;
+ * Return: returns  0 if consoles differ else returns 1 if
+ * dt node is disabled or consoles match.
  */
-static int32_t dt_console_init(dt_uart_info_t *uart_info,
-			  console_t *console,
-			  uint32_t clock)
-{
-	int32_t rc = 0;
-
-	/* Parse UART information from Device Tree Blob (DTB) */
-	rc = parse_uart_info(uart_info);
-	if (rc < 0) {
-		goto error;
-	}
-
-	rc = check_fdt_uart_info(uart_info);
-	if (rc < 0) {
-		goto error;
-	}
-
-	rc = check_fdt_uart_info(uart_info);
-	if (rc < 0) {
-		goto error;
-	}
-
-	if (strncmp(uart_info->compatible, DT_UART_COMPAT,
-		    strlen(DT_UART_COMPAT)) == 0) {
-		handle_dt_console(uart_info, &dt_console, clock, console);
-	} else if (strncmp(uart_info->compatible, DT_UART_DCC_COMPAT,
-			strlen(DT_UART_DCC_COMPAT)) == 0) {
-		handle_dcc_console(console);
-	} else {
-		WARN("BL31: No console device found in DT.\n");
-	}
-
-error:
-	return rc;
-}
-#endif
-
-#if defined(CONSOLE_RUNTIME)
-void console_runtime_init(void)
+static uint32_t get_rt_console(void)
 {
-	uint32_t uart_clk = get_uart_clk();
-	static console_t runtime_console;
-	uintptr_t rt_uart_base = 0;
-	uint32_t buad_rate = 0;
-
-#if (CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
-	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
-				   !IS_TFA_IN_OCM(BL31_BASE)))
-	console_t *console = &dt_console;
-#else
-	console_t *console = &boot_console;
-#endif
+	uint32_t rc = 0;
 
 #if (RT_CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
 	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
 				   !IS_TFA_IN_OCM(BL31_BASE)))
-	static dt_uart_info_t dt_info = {0};
-	uint32_t rc = parse_uart_info(&dt_info);
-
-	if (rc < 0) {
-		goto error;
+	/* DT based runtime console */
+	if ((dt_uart_info.base != 0) && (dt_uart_info.status != 0)) {
+		rt_hd_console.base = dt_uart_info.base;
+		rt_hd_console.baud_rate = dt_uart_info.baud_rate;
+		rt_hd_console.console_state = dt_uart_info.status;
+		if (rt_hd_console.base == boot_console.base) {
+			rc = 1;
+		}
 	} else {
-		rt_uart_base = dt_info.base;
-		buad_rate = dt_info.baud_rate;
-	}
-#elif defined(PLAT_zynqmp)
-	if (RT_CONSOLE_IS(cadence) || (RT_CONSOLE_IS(cadence1))) {
-		rt_uart_base = (uintptr_t)RT_UART_BASE;
-		buad_rate = (uint32_t)UART_BAUDRATE;
+		rc = 1;
 	}
 #else
-	if (RT_CONSOLE_IS(pl011) || (RT_CONSOLE_IS(pl011_1))) {
-		rt_uart_base = (uintptr_t)RT_UART_BASE;
-		buad_rate = (uint32_t)UART_BAUDRATE;
+	rt_hd_console.base = (uintptr_t)RT_UART_BASE;
+	rt_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
+	if (rt_hd_console.base != boot_console.base) {
+		rt_hd_console.console_state = 1;
+	} else {
+		rt_hd_console.console_state = 0;
+		rc = 1;
 	}
 #endif
-	/*skip console registration if runtime and boot console are same */
-	if (console->base != rt_uart_base) {
-		/* Remove runtime flag from boot console */
-		console_set_scope(console, CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH);
+	rt_hd_console.clk = get_uart_clk();
+	rt_hd_console.console_type = RT_UART_TYPE;
+	rt_hd_console.console_scope = CONSOLE_FLAG_RUNTIME;
+	return rc;
+}
 
-		register_console(rt_uart_base, uart_clk, buad_rate, &runtime_console,
-				 CONSOLE_FLAG_RUNTIME, PLAT_XLNX_CONSOLE_TYPE_DEFAULT);
+void console_runtime_init(void)
+{
+	/* skip console registration if runtime and boot console are same */
+	if (rt_hd_console.console_state) {
+		register_console(&rt_hd_console, &runtime_console);
 		INFO("Successfully initialized runtime console\n");
 	}
-
-#if (RT_CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
-	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
-				   !IS_TFA_IN_OCM(BL31_BASE)))
-error:
-	if (rc < 0) {
-		ERROR("Failed to parse uart info in runtime console\n");
-	}
-#endif
 }
 #endif
 
 void setup_console(void)
 {
-	int32_t rc;
-	uint32_t uart_clk = get_uart_clk();
+	/* This is hardcoded console setup just in case that DTB console fails */
+	boot_hd_console.base = (uintptr_t)UART_BASE;
+	boot_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
+	boot_hd_console.console_state = 1;
+	boot_hd_console.clk = get_uart_clk();
+	boot_hd_console.console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH;
+	boot_hd_console.console_type = UART_TYPE;
 
-#if (CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
+	/* For DT code decoding uncomment console registration below */
+	/* register_console(&boot_hd_console, &boot_console); */
+
+#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
 	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
 				   !IS_TFA_IN_OCM(BL31_BASE)))
-
-	static dt_uart_info_t uart_info = {0};
-
-	/* Initialize the DTB console using UART information from the DTB */
-	rc = dt_console_init(&uart_info, &boot_console, uart_clk);
-	if (rc < 0) {
-		ERROR("Failed to initialize DT console: %d\n", rc);
+	/* Parse DTB console for UART information  */
+	if (fdt_get_uart_info(&dt_uart_info) == 0) {
+		if (CONSOLE_IS(dtb)) {
+			boot_hd_console.base = dt_uart_info.base;
+			boot_hd_console.baud_rate = dt_uart_info.baud_rate;
+			boot_hd_console.console_type = dt_uart_info.console_type;
+		}
 	} else {
-		goto cnslsetup;
+		ERROR("Failed to initialize DT console or console node is disabled\n");
 	}
 #endif
 
-#if defined(PLAT_zynqmp)
-	if (CONSOLE_IS(cadence) || (CONSOLE_IS(cadence1))) {
-		rc = console_cdns_register(UART_BASE,
-					   uart_clk,
-					   UART_BAUDRATE,
-					   &boot_console);
-		if (rc == 0) {
-			panic();
-		}
+	/* Initialize the boot console */
+	register_console(&boot_hd_console, &boot_console);
 
-		console_set_scope(&boot_console, CONSOLE_FLAG_BOOT |
-				  CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH);
-	}
-#else
-	if (CONSOLE_IS(pl011) || (CONSOLE_IS(pl011_1))) {
-		/* Initialize the console to provide early debug support */
-		rc = console_pl011_register((uint32_t)UART_BASE,
-					   uart_clk,
-					   (uint32_t)UART_BAUDRATE,
-					   &boot_console);
-		if (rc == 0) {
-			panic();
-		}
+#ifdef CONSOLE_RUNTIME
+	int32_t rc;
+	uint32_t console_scope;
 
-		console_set_scope(&boot_console, CONSOLE_FLAG_BOOT |
-				  CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH);
-	}
-#endif
-	if (CONSOLE_IS(dcc)) {
-		/* Initialize the dcc console for debug */
-		rc = console_dcc_register(&boot_console);
-		if (rc == 0) {
-			panic();
-		}
+	rc = get_rt_console();
+	if (rc) {
+		console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH | CONSOLE_FLAG_RUNTIME;
+		console_set_scope(&boot_console, console_scope);
 	}
-
-#if (CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
-	(!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
-				   !IS_TFA_IN_OCM(BL31_BASE)))
-cnslsetup:
 #endif
 	INFO("BL31: Early console setup\n");
 }
diff --git a/plat/xilinx/zynqmp/include/zynqmp_def.h b/plat/xilinx/zynqmp/include/zynqmp_def.h
index 1144534..e2fd143 100644
--- a/plat/xilinx/zynqmp/include/zynqmp_def.h
+++ b/plat/xilinx/zynqmp/include/zynqmp_def.h
@@ -155,20 +155,30 @@
 #define ZYNQMP_UART1_BASE		U(0xFF010000)
 
 /* Boot console */
-#if CONSOLE_IS(cadence) || CONSOLE_IS(dcc) || CONSOLE_IS(dtb)
+#if CONSOLE_IS(cadence) || CONSOLE_IS(dtb)
 # define UART_BASE	ZYNQMP_UART0_BASE
+# define UART_TYPE	CONSOLE_CDNS
 #elif CONSOLE_IS(cadence1)
 # define UART_BASE	ZYNQMP_UART1_BASE
+# define UART_TYPE	CONSOLE_CDNS
+#elif CONSOLE_IS(dcc)
+# define UART_BASE	0x0
+# define UART_TYPE	CONSOLE_DCC
 #else
 # error "invalid ZYNQMP_CONSOLE"
 #endif
 
 /* Runtime console */
 #if defined(CONSOLE_RUNTIME)
-#if RT_CONSOLE_IS(cadence) || RT_CONSOLE_IS(dcc) || RT_CONSOLE_IS(dtb)
+#if RT_CONSOLE_IS(cadence) || RT_CONSOLE_IS(dtb)
 # define RT_UART_BASE	ZYNQMP_UART0_BASE
+# define RT_UART_TYPE	CONSOLE_CDNS
 #elif RT_CONSOLE_IS(cadence1)
 # define RT_UART_BASE	ZYNQMP_UART1_BASE
+# define RT_UART_TYPE	CONSOLE_CDNS
+#elif RT_CONSOLE_IS(dcc)
+# define RT_UART_BASE	0x0
+# define RT_UART_TYPE	CONSOLE_DCC
 #else
 # error "invalid CONSOLE_RUNTIME"
 #endif