blob: 5192ac0d21dbf1341d480e4a8eec8ba3cea4316b [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
Prasad Kummaria1b9b452024-03-07 15:28:11 +053026static console_t boot_console;
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053027static console_holder boot_hd_console;
28#if defined(CONSOLE_RUNTIME)
29static console_t runtime_console;
30static console_holder rt_hd_console;
31#endif
Prasad Kummarib72494e2023-09-19 22:15:05 +053032
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053033#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari3ded36f2024-03-18 10:14:31 +053034 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053035 !IS_TFA_IN_OCM(BL31_BASE)))
36static dt_uart_info_t dt_uart_info;
37#endif
38
Prasad Kummari80232f72024-03-14 15:19:10 +053039/**
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053040 * 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
Prasad Kummari80232f72024-03-14 15:19:10 +053043 * @console: Pointer to the console information structure.
Prasad Kummari80232f72024-03-14 15:19:10 +053044 */
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053045static void register_console(const console_holder *consoleh, console_t *console)
Prasad Kummari80232f72024-03-14 15:19:10 +053046{
47 int32_t rc = 0;
48
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053049 switch (consoleh->console_type) {
Prasad Kummari80232f72024-03-14 15:19:10 +053050#if defined(PLAT_zynqmp)
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053051 case CONSOLE_CDNS:
52 rc = console_cdns_register(consoleh->base,
53 consoleh->clk,
54 consoleh->baud_rate,
Prasad Kummari80232f72024-03-14 15:19:10 +053055 console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053056 break;
57#else
58 case CONSOLE_PL011:
59 rc = console_pl011_register(consoleh->base,
60 consoleh->clk,
61 consoleh->baud_rate,
Prasad Kummari80232f72024-03-14 15:19:10 +053062 console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053063 break;
Prasad Kummari80232f72024-03-14 15:19:10 +053064#endif
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053065 case CONSOLE_DCC:
Prasad Kummari80232f72024-03-14 15:19:10 +053066 rc = console_dcc_register(console);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053067 break;
68 default:
Prasad Kummari80232f72024-03-14 15:19:10 +053069 INFO("Invalid console type\n");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053070 break;
Prasad Kummari80232f72024-03-14 15:19:10 +053071 }
72
73 if (rc == 0) {
74 panic();
75 }
76
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053077 console_set_scope(console, consoleh->console_scope);
Prasad Kummari80232f72024-03-14 15:19:10 +053078}
Prasad Kummari80232f72024-03-14 15:19:10 +053079
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +053080#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari3ded36f2024-03-18 10:14:31 +053081 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
82 !IS_TFA_IN_OCM(BL31_BASE)))
Prasad Kummarib72494e2023-09-19 22:15:05 +053083/**
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 */
89static 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
128error:
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 */
139static 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 *
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530160 * Return: On success, it returns 0; on failure, it returns -1 or -FDT_ERR_NOTFOUND.
Prasad Kummarib72494e2023-09-19 22:15:05 +0530161 */
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530162static int32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb)
Prasad Kummarib72494e2023-09-19 22:15:05 +0530163{
164 uintptr_t base_addr;
165 const char *com;
Prasad Kummari4e5ebe52023-11-08 16:50:03 +0530166 int32_t ret = 0;
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530167 uint32_t status;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530168
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");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530174 ret = -FDT_ERR_NOTFOUND;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530175 goto error;
176 }
177
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530178 status = get_node_status(dtb, node);
179 if (status == 0) {
Prasad Kummaric143b572024-03-08 14:26:25 +0530180 ERROR("Uart node is disabled in DTB\n");
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530181 ret = -FDT_ERR_NOTFOUND;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530182 goto error;
183 }
184
Prasad Kummaric143b572024-03-08 14:26:25 +0530185 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);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530191 ret = -FDT_ERR_NOTFOUND;
Prasad Kummaric143b572024-03-08 14:26:25 +0530192 goto error;
193 }
194
195 info->baud_rate = get_baudrate(dtb);
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530196
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;
Prasad Kummaric143b572024-03-08 14:26:25 +0530209 }
Prasad Kummarib72494e2023-09-19 22:15:05 +0530210
211error:
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 */
221static int fdt_get_uart_info(dt_uart_info_t *info)
222{
Prasad Kummari4e5ebe52023-11-08 16:50:03 +0530223 int node = 0, ret = 0;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530224 void *dtb = (void *)XILINX_OF_BOARD_DTB_ADDR;
225
Prasad Kummari5eafc832023-11-03 16:47:26 +0530226 ret = is_valid_dtb(dtb);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530227 if (ret < 0) {
228 ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530229 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);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530235 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);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530241 goto error;
242 }
243
244error:
245 return ret;
246}
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530247#endif
Prasad Kummaric143b572024-03-08 14:26:25 +0530248
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530249#if defined(CONSOLE_RUNTIME)
Prasad Kummaric143b572024-03-08 14:26:25 +0530250/**
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530251 * get_rt_console() - Retrieves console data for runtime console.
Prasad Kummarib72494e2023-09-19 22:15:05 +0530252 *
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530253 * Return: returns 0 if consoles differ else returns 1 if
254 * dt node is disabled or consoles match.
Prasad Kummarib72494e2023-09-19 22:15:05 +0530255 */
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530256static uint32_t get_rt_console(void)
Prasad Kummari80232f72024-03-14 15:19:10 +0530257{
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530258 uint32_t rc = 0;
Prasad Kummari80232f72024-03-14 15:19:10 +0530259
260#if (RT_CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
261 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
262 !IS_TFA_IN_OCM(BL31_BASE)))
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530263 /* DT based runtime console */
264 if ((dt_uart_info.base != 0) && (dt_uart_info.status != 0)) {
265 rt_hd_console.base = dt_uart_info.base;
266 rt_hd_console.baud_rate = dt_uart_info.baud_rate;
267 rt_hd_console.console_state = dt_uart_info.status;
268 if (rt_hd_console.base == boot_console.base) {
269 rc = 1;
270 }
Prasad Kummari80232f72024-03-14 15:19:10 +0530271 } else {
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530272 rc = 1;
Prasad Kummari80232f72024-03-14 15:19:10 +0530273 }
274#else
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530275 rt_hd_console.base = (uintptr_t)RT_UART_BASE;
276 rt_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
277 if (rt_hd_console.base != boot_console.base) {
278 rt_hd_console.console_state = 1;
279 } else {
280 rt_hd_console.console_state = 0;
281 rc = 1;
Prasad Kummari80232f72024-03-14 15:19:10 +0530282 }
283#endif
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530284 rt_hd_console.clk = get_uart_clk();
285 rt_hd_console.console_type = RT_UART_TYPE;
286 rt_hd_console.console_scope = CONSOLE_FLAG_RUNTIME;
287 return rc;
288}
Prasad Kummari80232f72024-03-14 15:19:10 +0530289
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530290void console_runtime_init(void)
291{
292 /* skip console registration if runtime and boot console are same */
293 if (rt_hd_console.console_state) {
294 register_console(&rt_hd_console, &runtime_console);
Prasad Kummari80232f72024-03-14 15:19:10 +0530295 INFO("Successfully initialized runtime console\n");
296 }
Prasad Kummari80232f72024-03-14 15:19:10 +0530297}
298#endif
299
Prasad Kummarib72494e2023-09-19 22:15:05 +0530300void setup_console(void)
301{
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530302 /* This is hardcoded console setup just in case that DTB console fails */
303 boot_hd_console.base = (uintptr_t)UART_BASE;
304 boot_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
305 boot_hd_console.console_state = 1;
306 boot_hd_console.clk = get_uart_clk();
307 boot_hd_console.console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH;
308 boot_hd_console.console_type = UART_TYPE;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530309
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530310 /* For DT code decoding uncomment console registration below */
311 /* register_console(&boot_hd_console, &boot_console); */
312
313#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530314 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
315 !IS_TFA_IN_OCM(BL31_BASE)))
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530316 /* Parse DTB console for UART information */
317 if (fdt_get_uart_info(&dt_uart_info) == 0) {
318 if (CONSOLE_IS(dtb)) {
319 boot_hd_console.base = dt_uart_info.base;
320 boot_hd_console.baud_rate = dt_uart_info.baud_rate;
321 boot_hd_console.console_type = dt_uart_info.console_type;
322 }
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530323 } else {
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530324 ERROR("Failed to initialize DT console or console node is disabled\n");
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530325 }
326#endif
327
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530328 /* Initialize the boot console */
329 register_console(&boot_hd_console, &boot_console);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530330
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530331#ifdef CONSOLE_RUNTIME
332 int32_t rc;
333 uint32_t console_scope;
Prasad Kummarib72494e2023-09-19 22:15:05 +0530334
Maheedhar Bollapalli6775ec42024-03-19 22:19:28 +0530335 rc = get_rt_console();
336 if (rc) {
337 console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH | CONSOLE_FLAG_RUNTIME;
338 console_set_scope(&boot_console, console_scope);
Prasad Kummarib72494e2023-09-19 22:15:05 +0530339 }
Prasad Kummarib72494e2023-09-19 22:15:05 +0530340#endif
Prasad Kummari3ded36f2024-03-18 10:14:31 +0530341 INFO("BL31: Early console setup\n");
Prasad Kummarib72494e2023-09-19 22:15:05 +0530342}