1 /* 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> 21 #include <plat_fdt.h> 22 23 #include <platform_def.h> 24 #include <plat_private.h> 25 26 static console_t boot_console; 27 static console_holder boot_hd_console; 28 #if defined(CONSOLE_RUNTIME) 29 static console_t runtime_console; 30 static console_holder rt_hd_console; 31 #endif 32 33 #if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \ 34 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \ 35 !IS_TFA_IN_OCM(BL31_BASE))) 36 static dt_uart_info_t dt_uart_info; 37 #endif 38 39 /** 40 * 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 43 * @console: Pointer to the console information structure. 44 */ 45 static void register_console(const console_holder *consoleh, console_t *console) 46 { 47 int32_t rc = 0; 48 49 switch (consoleh->console_type) { 50 #if defined(PLAT_zynqmp) 51 case CONSOLE_CDNS: 52 rc = console_cdns_register(consoleh->base, 53 consoleh->clk, 54 consoleh->baud_rate, 55 console); 56 break; 57 #else 58 case CONSOLE_PL011: 59 rc = console_pl011_register(consoleh->base, 60 consoleh->clk, 61 consoleh->baud_rate, 62 console); 63 break; 64 #endif 65 case CONSOLE_DCC: 66 rc = console_dcc_register(console); 67 break; 68 default: 69 INFO("Invalid console type\n"); 70 break; 71 } 72 73 if (rc == 0) { 74 panic(); 75 } 76 77 console_set_scope(console, consoleh->console_scope); 78 } 79 80 #if ((CONSOLE_IS(dtb) || RT_CONSOLE_IS(dtb)) && defined(XILINX_OF_BOARD_DTB_ADDR)) && \ 81 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \ 82 !IS_TFA_IN_OCM(BL31_BASE))) 83 /** 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 */ 89 static 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 128 error: 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 */ 139 static 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 * 160 * Return: On success, it returns 0; on failure, it returns -1 or -FDT_ERR_NOTFOUND. 161 */ 162 static int32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb) 163 { 164 uintptr_t base_addr; 165 const char *com; 166 int32_t ret = 0; 167 uint32_t status; 168 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"); 174 ret = -FDT_ERR_NOTFOUND; 175 goto error; 176 } 177 178 status = get_node_status(dtb, node); 179 if (status == 0) { 180 ERROR("Uart node is disabled in DTB\n"); 181 ret = -FDT_ERR_NOTFOUND; 182 goto error; 183 } 184 185 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); 191 ret = -FDT_ERR_NOTFOUND; 192 goto error; 193 } 194 195 info->baud_rate = get_baudrate(dtb); 196 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; 209 } 210 211 error: 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 */ 221 static int fdt_get_uart_info(dt_uart_info_t *info) 222 { 223 int node = 0, ret = 0; 224 void *dtb = (void *)XILINX_OF_BOARD_DTB_ADDR; 225 226 ret = is_valid_dtb(dtb); 227 if (ret < 0) { 228 ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret); 229 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); 235 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); 241 goto error; 242 } 243 244 error: 245 return ret; 246 } 247 #endif 248 249 #if defined(CONSOLE_RUNTIME) 250 /** 251 * get_rt_console() - Retrieves console data for runtime console. 252 * 253 * Return: returns 0 if consoles differ else returns 1 if 254 * dt node is disabled or consoles match. 255 */ 256 static uint32_t get_rt_console(void) 257 { 258 uint32_t rc = 0; 259 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))) 263 /* 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 } 271 } else { 272 rc = 1; 273 } 274 #else 275 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; 282 } 283 #endif 284 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 } 289 290 void 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); 295 INFO("Successfully initialized runtime console\n"); 296 } 297 } 298 #endif 299 300 void setup_console(void) 301 { 302 /* 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; 309 310 /* 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)) && \ 314 (!defined(PLAT_zynqmp) || (defined(PLAT_zynqmp) && \ 315 !IS_TFA_IN_OCM(BL31_BASE))) 316 /* 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 } 323 } else { 324 ERROR("Failed to initialize DT console or console node is disabled\n"); 325 } 326 #endif 327 328 /* Initialize the boot console */ 329 register_console(&boot_hd_console, &boot_console); 330 331 #ifdef CONSOLE_RUNTIME 332 int32_t rc; 333 uint32_t console_scope; 334 335 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); 339 } 340 #endif 341 INFO("BL31: Early console setup\n"); 342 } 343