blob: 63b604216352474a28bcf0b43d5429d1d4c74ee4 [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;
Maheedhar Bollapallid2e00ee2024-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 Kummari39234622023-09-19 22:15:05 +053032
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053033#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari09a02ce2024-03-18 10:14:31 +053034 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053035 !IS_TFA_IN_OCM(BL31_BASE)))
36static dt_uart_info_t dt_uart_info;
37#endif
38
Prasad Kummari4557ab62024-03-14 15:19:10 +053039/**
Maheedhar Bollapallid2e00ee2024-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 Kummari4557ab62024-03-14 15:19:10 +053043 * @console: Pointer to the console information structure.
Prasad Kummari4557ab62024-03-14 15:19:10 +053044 */
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053045static void register_console(const console_holder *consoleh, console_t *console)
Prasad Kummari4557ab62024-03-14 15:19:10 +053046{
47 int32_t rc = 0;
48
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053049 switch (consoleh->console_type) {
Prasad Kummari4557ab62024-03-14 15:19:10 +053050#if defined(PLAT_zynqmp)
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053051 case CONSOLE_CDNS:
52 rc = console_cdns_register(consoleh->base,
53 consoleh->clk,
54 consoleh->baud_rate,
Prasad Kummari4557ab62024-03-14 15:19:10 +053055 console);
Maheedhar Bollapallid2e00ee2024-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 Kummari4557ab62024-03-14 15:19:10 +053062 console);
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053063 break;
Prasad Kummari4557ab62024-03-14 15:19:10 +053064#endif
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053065 case CONSOLE_DCC:
Prasad Kummari4557ab62024-03-14 15:19:10 +053066 rc = console_dcc_register(console);
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053067 break;
68 default:
Prasad Kummari4557ab62024-03-14 15:19:10 +053069 INFO("Invalid console type\n");
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053070 break;
Prasad Kummari4557ab62024-03-14 15:19:10 +053071 }
72
73 if (rc == 0) {
74 panic();
75 }
76
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053077 console_set_scope(console, consoleh->console_scope);
Prasad Kummari4557ab62024-03-14 15:19:10 +053078}
Prasad Kummari4557ab62024-03-14 15:19:10 +053079
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +053080#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari09a02ce2024-03-18 10:14:31 +053081 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
82 !IS_TFA_IN_OCM(BL31_BASE)))
Prasad Kummari39234622023-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 Bollapallid2e00ee2024-03-19 22:19:28 +0530160 * Return: On success, it returns 0; on failure, it returns -1 or -FDT_ERR_NOTFOUND.
Prasad Kummari39234622023-09-19 22:15:05 +0530161 */
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530162static int32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb)
Prasad Kummari39234622023-09-19 22:15:05 +0530163{
164 uintptr_t base_addr;
165 const char *com;
Prasad Kummari8eb6a1d2023-11-08 16:50:03 +0530166 int32_t ret = 0;
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530167 uint32_t status;
Prasad Kummari39234622023-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 Bollapallid2e00ee2024-03-19 22:19:28 +0530174 ret = -FDT_ERR_NOTFOUND;
Prasad Kummari39234622023-09-19 22:15:05 +0530175 goto error;
176 }
177
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530178 status = get_node_status(dtb, node);
179 if (status == 0) {
Prasad Kummarid9892292024-03-08 14:26:25 +0530180 ERROR("Uart node is disabled in DTB\n");
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530181 ret = -FDT_ERR_NOTFOUND;
Prasad Kummari39234622023-09-19 22:15:05 +0530182 goto error;
183 }
184
Prasad Kummarid9892292024-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 Bollapallid2e00ee2024-03-19 22:19:28 +0530191 ret = -FDT_ERR_NOTFOUND;
Prasad Kummarid9892292024-03-08 14:26:25 +0530192 goto error;
193 }
194
195 info->baud_rate = get_baudrate(dtb);
Maheedhar Bollapallid2e00ee2024-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 Kummarid9892292024-03-08 14:26:25 +0530209 }
Prasad Kummari39234622023-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 Kummari8eb6a1d2023-11-08 16:50:03 +0530223 int node = 0, ret = 0;
Prasad Kummari39234622023-09-19 22:15:05 +0530224 void *dtb = (void *)XILINX_OF_BOARD_DTB_ADDR;
225
Prasad Kummarie2d9dfe2023-11-03 16:47:26 +0530226 ret = is_valid_dtb(dtb);
Prasad Kummari39234622023-09-19 22:15:05 +0530227 if (ret < 0) {
228 ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
Prasad Kummari39234622023-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 Kummari39234622023-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 Kummari39234622023-09-19 22:15:05 +0530241 goto error;
242 }
243
244error:
245 return ret;
246}
Prasad Kummari39234622023-09-19 22:15:05 +0530247#endif
248
249void setup_console(void)
250{
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530251 /* This is hardcoded console setup just in case that DTB console fails */
252 boot_hd_console.base = (uintptr_t)UART_BASE;
253 boot_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530254 boot_hd_console.clk = get_uart_clk();
255 boot_hd_console.console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH;
256 boot_hd_console.console_type = UART_TYPE;
Prasad Kummari39234622023-09-19 22:15:05 +0530257
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530258 /* For DT code decoding uncomment console registration below */
259 /* register_console(&boot_hd_console, &boot_console); */
260
261#if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
Prasad Kummari09a02ce2024-03-18 10:14:31 +0530262 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
263 !IS_TFA_IN_OCM(BL31_BASE)))
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530264 /* Parse DTB console for UART information */
265 if (fdt_get_uart_info(&dt_uart_info) == 0) {
266 if (CONSOLE_IS(dtb)) {
267 boot_hd_console.base = dt_uart_info.base;
268 boot_hd_console.baud_rate = dt_uart_info.baud_rate;
269 boot_hd_console.console_type = dt_uart_info.console_type;
270 }
Prasad Kummari09a02ce2024-03-18 10:14:31 +0530271 } else {
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530272 ERROR("Failed to initialize DT console or console node is disabled\n");
Prasad Kummari09a02ce2024-03-18 10:14:31 +0530273 }
274#endif
275
Maheedhar Bollapallid2e00ee2024-03-19 22:19:28 +0530276 /* Initialize the boot console */
277 register_console(&boot_hd_console, &boot_console);
Prasad Kummari39234622023-09-19 22:15:05 +0530278
Michal Simekcca2b862024-09-10 14:53:18 +0200279 INFO("BL31: Early console setup\n");
Prasad Kummari39234622023-09-19 22:15:05 +0530280
Michal Simekcca2b862024-09-10 14:53:18 +0200281#ifdef CONSOLE_RUNTIME
282#if (RT_CONSOLE_IS(dtb) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \
283 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \
284 !IS_TFA_IN_OCM(BL31_BASE)))
285 /* DT based runtime console */
286 if (dt_uart_info.console_type != CONSOLE_NONE) {
287 rt_hd_console.base = dt_uart_info.base;
288 rt_hd_console.baud_rate = dt_uart_info.baud_rate;
289 rt_hd_console.console_type = dt_uart_info.console_type;
290 }
291#else
292 rt_hd_console.base = (uintptr_t)RT_UART_BASE;
293 rt_hd_console.baud_rate = (uint32_t)UART_BAUDRATE;
294 rt_hd_console.console_type = RT_UART_TYPE;
295#endif
296
297 if ((rt_hd_console.console_type == boot_hd_console.console_type) &&
298 (rt_hd_console.base == boot_hd_console.base)) {
299 console_set_scope(&boot_console,
300 CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH | CONSOLE_FLAG_RUNTIME);
301 INFO("Successfully initialized runtime console\n");
302 } else {
303 rt_hd_console.clk = get_uart_clk();
304 rt_hd_console.console_scope = CONSOLE_FLAG_RUNTIME;
305
306 register_console(&rt_hd_console, &runtime_console);
307 INFO("Successfully initialized new runtime console\n");
Prasad Kummari39234622023-09-19 22:15:05 +0530308 }
309#endif
Prasad Kummari39234622023-09-19 22:15:05 +0530310}