Prasad Kummari | b72494e | 2023-09-19 22:15:05 +0530 | [diff] [blame] | 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 | |
| 22 | #include <platform_def.h> |
| 23 | #include <plat_private.h> |
| 24 | |
| 25 | static console_t console; |
| 26 | |
| 27 | #if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE)) |
| 28 | /** |
| 29 | * get_baudrate() - Get the baudrate form DTB. |
| 30 | * @dtb: Address of the Device Tree Blob (DTB). |
| 31 | * |
| 32 | * Return: On success returns the baudrate; on failure returns an error. |
| 33 | */ |
| 34 | static int32_t get_baudrate(void *dtb) |
| 35 | { |
| 36 | int node; |
| 37 | int32_t ret = 0; |
| 38 | const char *prop, *path; |
| 39 | char *end; |
| 40 | int32_t baud_rate = 0; |
| 41 | |
| 42 | node = fdt_path_offset(dtb, "/secure-chosen"); |
| 43 | if (node < 0) { |
| 44 | node = fdt_path_offset(dtb, "/chosen"); |
| 45 | if (node < 0) { |
| 46 | ret = -FDT_ERR_NOTFOUND; |
| 47 | goto error; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | prop = fdt_getprop(dtb, node, "stdout-path", NULL); |
| 52 | if (prop == NULL) { |
| 53 | ret = -FDT_ERR_NOTFOUND; |
| 54 | goto error; |
| 55 | } |
| 56 | |
| 57 | /* Parse string serial0:115200n8 */ |
| 58 | path = strchr(prop, ':'); |
| 59 | if (!path) { |
| 60 | ret = -FDT_ERR_NOTFOUND; |
| 61 | goto error; |
| 62 | } else { |
| 63 | |
| 64 | baud_rate = strtoul(path + 1, &end, 10); |
| 65 | if (baud_rate == 0 && end == path) { |
| 66 | ERROR("Conversion error occurred: %d\n", baud_rate); |
| 67 | ret = -FDT_ERR_NOTFOUND; |
| 68 | goto error; |
| 69 | } |
| 70 | ret = baud_rate; |
| 71 | } |
| 72 | |
| 73 | error: |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * get_node_status() - Get the DTB node status. |
| 79 | * @dtb: Address of the Device Tree Blob (DTB). |
| 80 | * @node: Node address in the device tree. |
| 81 | * |
| 82 | * Return: On success, it returns 1; on failure, it returns an 0. |
| 83 | */ |
| 84 | static uint32_t get_node_status(void *dtb, int node) |
| 85 | { |
| 86 | const char *status_cell; |
| 87 | uint32_t status = 0; |
| 88 | |
| 89 | status_cell = fdt_getprop(dtb, node, "status", NULL); |
| 90 | if (!status_cell || strcmp(status_cell, "okay") == 0) { |
| 91 | status = 1; |
| 92 | } else { |
| 93 | status = 0; |
| 94 | } |
| 95 | |
| 96 | return status; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * fdt_add_uart_info() - Add DTB information to a UART structure. |
| 101 | * @info: Pointer to the UART information structure. |
| 102 | * @node: Node address in the device tree. |
| 103 | * @dtb: Address of the Device Tree Blob(DTB). |
| 104 | * |
| 105 | * Return: On success, it returns 1; on failure, it returns an 0. |
| 106 | */ |
| 107 | static uint32_t fdt_add_uart_info(dt_uart_info_t *info, int node, void *dtb) |
| 108 | { |
| 109 | uintptr_t base_addr; |
| 110 | const char *com; |
| 111 | uint32_t ret = 0; |
| 112 | |
| 113 | com = fdt_getprop(dtb, node, "compatible", NULL); |
| 114 | if (com != NULL) { |
| 115 | strlcpy(info->compatible, com, sizeof(info->compatible)); |
| 116 | } else { |
| 117 | ERROR("Compatible property not found in DTB node\n"); |
| 118 | ret = -FDT_ERR_NOTFOUND; |
| 119 | goto error; |
| 120 | } |
| 121 | |
| 122 | ret = fdt_get_reg_props_by_index(dtb, node, 0, &base_addr, NULL); |
| 123 | if (ret >= 0) { |
| 124 | info->base = base_addr; |
| 125 | } else { |
| 126 | ERROR("Failed to retrieve base address. Error code: %d\n", ret); |
| 127 | ret = -FDT_ERR_NOTFOUND; |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | info->status = get_node_status(dtb, node); |
| 132 | info->baud_rate = get_baudrate(dtb); |
| 133 | |
| 134 | error: |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * fdt_get_uart_info() - Get the uart information form DTB. |
| 140 | * @info: Pointer to the UART information structure. |
| 141 | * |
| 142 | * Return: On success, it returns 0; on failure, it returns an error+reason. |
| 143 | */ |
| 144 | static int fdt_get_uart_info(dt_uart_info_t *info) |
| 145 | { |
| 146 | int node, ret = 0; |
| 147 | void *dtb = (void *)XILINX_OF_BOARD_DTB_ADDR; |
| 148 | |
| 149 | if (fdt_check_header(dtb) != 0) { |
| 150 | ERROR("Can't read DT at %p\n", dtb); |
| 151 | ret = -FDT_ERR_NOTFOUND; |
| 152 | goto error; |
| 153 | } |
| 154 | |
| 155 | ret = fdt_open_into(dtb, dtb, XILINX_OF_BOARD_DTB_MAX_SIZE); |
| 156 | if (ret < 0) { |
| 157 | ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret); |
| 158 | ret = -FDT_ERR_NOTFOUND; |
| 159 | goto error; |
| 160 | } |
| 161 | |
| 162 | node = fdt_get_stdout_node_offset(dtb); |
| 163 | if (node < 0) { |
| 164 | ERROR("DT get stdout node failed : %d\n", node); |
| 165 | ret = -FDT_ERR_NOTFOUND; |
| 166 | goto error; |
| 167 | } |
| 168 | |
| 169 | ret = fdt_add_uart_info(info, node, dtb); |
| 170 | if (ret < 0) { |
| 171 | ERROR("Failed to add DT UART info: %d\n", ret); |
| 172 | ret = -FDT_ERR_NOTFOUND; |
| 173 | goto error; |
| 174 | } |
| 175 | |
| 176 | error: |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * check_fdt_uart_info() - Check early uart info with DTB uart info. |
| 182 | * @info: Pointer to the UART information structure. |
| 183 | * |
| 184 | * Return: On success, it returns 0; on failure, it returns an error+reason. |
| 185 | */ |
| 186 | static int check_fdt_uart_info(dt_uart_info_t *info) |
| 187 | { |
| 188 | uint32_t ret = 0; |
| 189 | |
| 190 | if (info->status == 0) { |
| 191 | ret = -ENODEV; |
| 192 | goto error; |
| 193 | } |
| 194 | |
| 195 | if ((info->base == console.base) && |
| 196 | (info->baud_rate == UART_BAUDRATE) && !CONSOLE_IS(dcc)) { |
| 197 | ret = -ENODEV; |
| 198 | goto error; |
| 199 | } |
| 200 | |
| 201 | error: |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * console_boot_end() - Unregister the console_t instance form the console list. |
| 207 | * @boot_console: Pointer to the console information structure. |
| 208 | */ |
| 209 | static void console_boot_end(console_t *boot_console) |
| 210 | { |
| 211 | if (CONSOLE_IS(dcc)) { |
| 212 | console_dcc_unregister(); |
| 213 | } else { |
| 214 | console_flush(); |
| 215 | (void)console_unregister(boot_console); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * setup_runtime_console() - Registers the runtime uart with console list. |
| 221 | * @clock: UART clock. |
| 222 | * @info: Pointer to the UART information structure. |
| 223 | */ |
| 224 | static void setup_runtime_console(uint32_t clock, dt_uart_info_t *info) |
| 225 | { |
| 226 | static console_t bl31_runtime_console; |
| 227 | uint32_t rc; |
| 228 | |
| 229 | #if defined(PLAT_zynqmp) |
| 230 | rc = console_cdns_register(info->base, |
| 231 | clock, |
| 232 | info->baud_rate, |
| 233 | &bl31_runtime_console); |
| 234 | #else |
| 235 | rc = console_pl011_register(info->base, |
| 236 | clock, |
| 237 | info->baud_rate, |
| 238 | &bl31_runtime_console); |
| 239 | #endif |
| 240 | if (rc == 0) { |
| 241 | panic(); |
| 242 | } |
| 243 | |
| 244 | console_set_scope(&bl31_runtime_console, |
| 245 | CONSOLE_FLAG_BOOT | CONSOLE_FLAG_RUNTIME | |
| 246 | CONSOLE_FLAG_CRASH); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /** |
| 251 | * runtime_console_init() - Initializes the run time console information. |
| 252 | * @uart_info: Pointer to the UART information structure. |
| 253 | * @bl31_boot_console: Pointer to the console information structure. |
| 254 | * @clock: UART clock. |
| 255 | * |
| 256 | * Return: On success, it returns 0; on failure, it returns an error+reason; |
| 257 | */ |
| 258 | static int32_t runtime_console_init(dt_uart_info_t *uart_info, |
| 259 | console_t *bl31_boot_console, |
| 260 | uint32_t clock) |
| 261 | { |
| 262 | int32_t rc = 0; |
| 263 | |
| 264 | /* Parse UART information from Device Tree Blob (DTB) */ |
| 265 | rc = fdt_get_uart_info(uart_info); |
| 266 | if (rc < 0) { |
| 267 | rc = -FDT_ERR_NOTFOUND; |
| 268 | } |
| 269 | |
| 270 | if (strncmp(uart_info->compatible, DT_UART_COMPAT, |
| 271 | strlen(DT_UART_COMPAT)) == 0) { |
| 272 | |
| 273 | if (check_fdt_uart_info(uart_info) == 0) { |
| 274 | setup_runtime_console(clock, uart_info); |
| 275 | console_boot_end(bl31_boot_console); |
| 276 | INFO("Runtime console setup\n"); |
| 277 | } else { |
| 278 | INFO("Early console and DTB console are same\n"); |
| 279 | } |
| 280 | } else if (strncmp(uart_info->compatible, DT_UART_DCC_COMPAT, |
| 281 | strlen(DT_UART_DCC_COMPAT)) == 0) { |
| 282 | rc = console_dcc_register(); |
| 283 | if (rc == 0) { |
| 284 | panic(); |
| 285 | } |
| 286 | console_boot_end(bl31_boot_console); |
| 287 | } else { |
| 288 | WARN("BL31: No console device found in DT.\n"); |
| 289 | } |
| 290 | |
| 291 | return rc; |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | void setup_console(void) |
| 296 | { |
| 297 | uint32_t rc; |
| 298 | uint32_t uart_clk = get_uart_clk(); |
| 299 | |
| 300 | #if defined(PLAT_zynqmp) |
| 301 | if (CONSOLE_IS(cadence) || (CONSOLE_IS(cadence1))) { |
| 302 | rc = console_cdns_register(UART_BASE, |
| 303 | uart_clk, |
| 304 | UART_BAUDRATE, |
| 305 | &console); |
| 306 | if (rc == 0) { |
| 307 | panic(); |
| 308 | } |
| 309 | |
| 310 | console_set_scope(&console, CONSOLE_FLAG_BOOT | |
| 311 | CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH); |
| 312 | } |
| 313 | #else |
| 314 | if (CONSOLE_IS(pl011) || (CONSOLE_IS(pl011_1))) { |
| 315 | /* Initialize the console to provide early debug support */ |
| 316 | rc = console_pl011_register((uint32_t)UART_BASE, |
| 317 | uart_clk, |
| 318 | (uint32_t)UART_BAUDRATE, |
| 319 | &console); |
| 320 | if (rc == 0) { |
| 321 | panic(); |
| 322 | } |
| 323 | |
| 324 | console_set_scope(&console, CONSOLE_FLAG_BOOT | |
| 325 | CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH); |
| 326 | } |
| 327 | #endif |
| 328 | if (CONSOLE_IS(dcc)) { |
| 329 | /* Initialize the dcc console for debug */ |
| 330 | rc = console_dcc_register(); |
| 331 | if (rc == 0) { |
| 332 | panic(); |
| 333 | } |
| 334 | } |
| 335 | INFO("BL31: Early console setup\n"); |
| 336 | |
| 337 | #if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE)) |
| 338 | static dt_uart_info_t uart_info = {0}; |
| 339 | |
| 340 | /* Initialize the runtime console using UART information from the DTB */ |
| 341 | rc = runtime_console_init(&uart_info, &console, uart_clk); |
| 342 | if (rc < 0) { |
| 343 | ERROR("Failed to initialize runtime console: %d\n", rc); |
| 344 | } |
| 345 | #endif |
| 346 | } |