blob: 681226f99513b079ba70fb1ef4699fc1237898a8 [file] [log] [blame]
Prasad Kummarib72494e2023-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 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 Bollapalli6775ec42024-03-19 22:19:28 +053034#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari3ded36f2024-03-18 10:14:31 +053035 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053036 !IS_TFA_IN_OCM(BL31_BASE)))
37static dt_uart_info_t dt_uart_info;
38#endif
39
Prasad Kummari80232f72024-03-14 15:19:10 +053040/**
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053041 * register_console() - Registers the uart with console list.
42 * @consoleh: Console holder structure with UART base address,
43 * UART clock, UART buad rate, flags & console type
Prasad Kummari80232f72024-03-14 15:19:10 +053044 * @console: Pointer to the console information structure.
Prasad Kummari80232f72024-03-14 15:19:10 +053045 */
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053046static void register_console(const console_holder *consoleh, console_t *console)
Prasad Kummari80232f72024-03-14 15:19:10 +053047{
48 int32_t rc = 0;
49
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053050 switch (consoleh->console_type) {
Prasad Kummari80232f72024-03-14 15:19:10 +053051#if defined(PLAT_zynqmp)
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053052 case CONSOLE_CDNS:
53 rc = console_cdns_register(consoleh->base,
54 consoleh->clk,
55 consoleh->baud_rate,
Prasad Kummari80232f72024-03-14 15:19:10 +053056 console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053057 break;
58#else
59 case CONSOLE_PL011:
60 rc = console_pl011_register(consoleh->base,
61 consoleh->clk,
62 consoleh->baud_rate,
Prasad Kummari80232f72024-03-14 15:19:10 +053063 console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053064 break;
Prasad Kummari80232f72024-03-14 15:19:10 +053065#endif
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053066 case CONSOLE_DCC:
Prasad Kummari80232f72024-03-14 15:19:10 +053067 rc = console_dcc_register(console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053068 break;
69 default:
Prasad Kummari80232f72024-03-14 15:19:10 +053070 INFO("Invalid console type\n");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053071 break;
Prasad Kummari80232f72024-03-14 15:19:10 +053072 }
73
74 if (rc == 0) {
75 panic();
76 }
77
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053078 console_set_scope(console, consoleh->console_scope);
Prasad Kummari80232f72024-03-14 15:19:10 +053079}
Prasad Kummari80232f72024-03-14 15:19:10 +053080
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053081#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari3ded36f2024-03-18 10:14:31 +053082 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
83 !IS_TFA_IN_OCM(BL31_BASE)))
Prasad Kummarib72494e2023-09-19 22:15:05 +053084/**
85 * get_baudrate() - Get the baudrate form DTB.
86 * @dtb: Address of the Device Tree Blob (DTB).
87 *
88 * Return: On success returns the baudrate; on failure returns an error.
89 */
90static int32_t get_baudrate(void *dtb)
91{
92 int node;
93 int32_t ret = 0;
94 const char *prop, *path;
95 char *end;
96 int32_t baud_rate = 0;
97
98 node = fdt_path_offset(dtb, "/secure-chosen");
99 if (node < 0) {
100 node = fdt_path_offset(dtb, "/chosen");
101 if (node < 0) {
102 ret = -FDT_ERR_NOTFOUND;
103 goto error;
104 }
105 }
106
107 prop = fdt_getprop(dtb, node, "stdout-path", NULL);
108 if (prop == NULL) {
109 ret = -FDT_ERR_NOTFOUND;
110 goto error;
111 }
112
113 /* Parse string serial0:115200n8 */
114 path = strchr(prop, ':');
115 if (!path) {
116 ret = -FDT_ERR_NOTFOUND;
117 goto error;
118 } else {
119
120 baud_rate = strtoul(path + 1, &end, 10);
121 if (baud_rate == 0 && end == path) {
122 ERROR("Conversion error occurred: %d\n", baud_rate);
123 ret = -FDT_ERR_NOTFOUND;
124 goto error;
125 }
126 ret = baud_rate;
127 }
128
129error:
130 return ret;
131}
132
133/**
134 * get_node_status() - Get the DTB node status.
135 * @dtb: Address of the Device Tree Blob (DTB).
136 * @node: Node address in the device tree.
137 *
138 * Return: On success, it returns 1; on failure, it returns an 0.
139 */
140static uint32_t get_node_status(void *dtb, int node)
141{
142 const char *status_cell;
143 uint32_t status = 0;
144
145 status_cell = fdt_getprop(dtb, node, "status", NULL);
146 if (!status_cell || strcmp(status_cell, "okay") == 0) {
147 status = 1;
148 } else {
149 status = 0;
150 }
151
152 return status;
153}
154
155/**
156 * fdt_add_uart_info() - Add DTB information to a UART structure.
157 * @info: Pointer to the UART information structure.
158 * @node: Node address in the device tree.
159 * @dtb: Address of the Device Tree Blob(DTB).
160 *
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530161 * Return: On success, it returns 0; on failure, it returns -1 or -FDT_ERR_NOTFOUND.
Prasad Kummarib72494e2023-09-19 22:15:05 +0530162 */
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530163static int32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb)
Prasad Kummarib72494e2023-09-19 22:15:05 +0530164{
165 uintptr_t base_addr;
166 const char *com;
Prasad Kummari4e5ebe52023-11-08 16:50:03 +0530167 int32_t ret = 0;
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530168 uint32_t status;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530169
170 com = fdt_getprop(dtb, node, "compatible", NULL);
171 if (com != NULL) {
172 strlcpy(info->compatible, com, sizeof(info->compatible));
173 } else {
174 ERROR("Compatible property not found in DTB node\n");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530175 ret = -FDT_ERR_NOTFOUND;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530176 goto error;
177 }
178
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530179 status = get_node_status(dtb, node);
180 if (status == 0) {
Prasad Kummaric143b572024-03-08 14:26:25 +0530181 ERROR("Uart node is disabled in DTB\n");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530182 ret = -FDT_ERR_NOTFOUND;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530183 goto error;
184 }
185
Prasad Kummaric143b572024-03-08 14:26:25 +0530186 if (strncmp(info->compatible, DT_UART_DCC_COMPAT, strlen(DT_UART_DCC_COMPAT)) != 0) {
187 ret = fdt_get_reg_props_by_index(dtb, node, 0, &base_addr, NULL);
188 if (ret >= 0) {
189 info->base = base_addr;
190 } else {
191 ERROR("Failed to retrieve base address. Error code: %d\n", ret);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530192 ret = -FDT_ERR_NOTFOUND;
Prasad Kummaric143b572024-03-08 14:26:25 +0530193 goto error;
194 }
195
196 info->baud_rate = get_baudrate(dtb);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530197
198 if (strncmp(info->compatible, DT_UART_CAD_COMPAT,
199 strlen(DT_UART_CAD_COMPAT)) == 0) {
200 info->console_type = CONSOLE_CDNS;
201 } else if (strncmp(info->compatible, DT_UART_PL011_COMPAT,
202 strlen(DT_UART_PL011_COMPAT)) == 0) {
203 info->console_type = CONSOLE_PL011;
204 } else {
205 ERROR("Incompatible uart node in DTB\n");
206 ret = -FDT_ERR_NOTFOUND;
207 }
208 } else {
209 info->console_type = CONSOLE_DCC;
Prasad Kummaric143b572024-03-08 14:26:25 +0530210 }
Prasad Kummarib72494e2023-09-19 22:15:05 +0530211
212error:
213 return ret;
214}
215
216/**
217 * fdt_get_uart_info() - Get the uart information form DTB.
218 * @info: Pointer to the UART information structure.
219 *
220 * Return: On success, it returns 0; on failure, it returns an error+reason.
221 */
222static int fdt_get_uart_info(dt_uart_info_t *info)
223{
Prasad Kummari4e5ebe52023-11-08 16:50:03 +0530224 int node = 0, ret = 0;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530225 void *dtb = (void *)XILINX_OF_BOARD_DTB_ADDR;
226
Prasad Kummari5eafc832023-11-03 16:47:26 +0530227 ret = is_valid_dtb(dtb);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530228 if (ret < 0) {
229 ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530230 goto error;
231 }
232
233 node = fdt_get_stdout_node_offset(dtb);
234 if (node < 0) {
235 ERROR("DT get stdout node failed : %d\n", node);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530236 goto error;
237 }
238
239 ret = fdt_add_uart_info(info, node, dtb);
240 if (ret < 0) {
241 ERROR("Failed to add DT UART info: %d\n", ret);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530242 goto error;
243 }
244
245error:
246 return ret;
247}
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530248#endif
Prasad Kummaric143b572024-03-08 14:26:25 +0530249
Prasad Kummarib72494e2023-09-19 22:15:05 +0530250void setup_console(void)
251{
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530252 /* This is hardcoded console setup just in case that DTB console fails */
253 boot_hd_console.base = (uintptr_t)UART_BASE;
254 boot_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530255 boot_hd_console.clk = get_uart_clk();
256 boot_hd_console.console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH;
257 boot_hd_console.console_type = UART_TYPE;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530258
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530259 /* For DT code decoding uncomment console registration below */
260 /* register_console(&boot_hd_console, &boot_console); */
261
262#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530263 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
264 !IS_TFA_IN_OCM(BL31_BASE)))
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530265 /* Parse DTB console for UART information */
266 if (fdt_get_uart_info(&dt_uart_info) == 0) {
267 if (CONSOLE_IS(dtb)) {
268 boot_hd_console.base = dt_uart_info.base;
269 boot_hd_console.baud_rate = dt_uart_info.baud_rate;
270 boot_hd_console.console_type = dt_uart_info.console_type;
271 }
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530272 } else {
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530273 ERROR("Failed to initialize DT console or console node is disabled\n");
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530274 }
275#endif
276
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530277 /* Initialize the boot console */
278 register_console(&boot_hd_console, &boot_console);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530279
Michal Simek4e035e02024-09-10 14:53:18 +0200280 INFO("BL31: Early console setup\n");
281
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530282#ifdef CONSOLE_RUNTIME
Michal Simek4e035e02024-09-10 14:53:18 +0200283#if (RT_CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
284 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
285 !IS_TFA_IN_OCM(BL31_BASE)))
Michal Simekb954eb42024-09-10 15:55:04 +0200286 rt_hd_console.base = dt_uart_info.base;
287 rt_hd_console.baud_rate = dt_uart_info.baud_rate;
288 rt_hd_console.console_type = dt_uart_info.console_type;
Michal Simek4e035e02024-09-10 14:53:18 +0200289#else
290 rt_hd_console.base = (uintptr_t)RT_UART_BASE;
291 rt_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
292 rt_hd_console.console_type = RT_UART_TYPE;
293#endif
Prasad Kummarib72494e2023-09-19 22:15:05 +0530294
Michal Simek4e035e02024-09-10 14:53:18 +0200295 if ((rt_hd_console.console_type == boot_hd_console.console_type) &&
296 (rt_hd_console.base == boot_hd_console.base)) {
297 console_set_scope(&boot_console,
298 CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH | CONSOLE_FLAG_RUNTIME);
299 INFO("Successfully initialized runtime console\n");
300 } else {
301 rt_hd_console.clk = get_uart_clk();
302 rt_hd_console.console_scope = CONSOLE_FLAG_RUNTIME;
303
304 register_console(&rt_hd_console, &runtime_console);
305 INFO("Successfully initialized new runtime console\n");
Prasad Kummarib72494e2023-09-19 22:15:05 +0530306 }
Prasad Kummarib72494e2023-09-19 22:15:05 +0530307#endif
Prasad Kummarib72494e2023-09-19 22:15:05 +0530308}
Michal Simekb954eb42024-09-10 15:55:04 +0200309#else
310void setup_console(void)
311{
312}
313#endif