1 /* 2 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <errno.h> 9 10 #include <libfdt.h> 11 12 #include <platform_def.h> 13 14 #include <common/debug.h> 15 #include <common/fdt_wrappers.h> 16 #include <drivers/st/stm32_gpio.h> 17 #include <drivers/st/stm32mp1_ddr.h> 18 #include <drivers/st/stm32mp1_ram.h> 19 20 #include <stm32mp_dt.h> 21 22 static int fdt_checked; 23 24 static void *fdt = (void *)(uintptr_t)STM32MP_DTB_BASE; 25 26 /******************************************************************************* 27 * This function checks device tree file with its header. 28 * Returns 0 on success and a negative FDT error code on failure. 29 ******************************************************************************/ 30 int dt_open_and_check(void) 31 { 32 int ret = fdt_check_header(fdt); 33 34 if (ret == 0) { 35 fdt_checked = 1; 36 } 37 38 return ret; 39 } 40 41 /******************************************************************************* 42 * This function gets the address of the DT. 43 * If DT is OK, fdt_addr is filled with DT address. 44 * Returns 1 if success, 0 otherwise. 45 ******************************************************************************/ 46 int fdt_get_address(void **fdt_addr) 47 { 48 if (fdt_checked == 1) { 49 *fdt_addr = fdt; 50 } 51 52 return fdt_checked; 53 } 54 55 /******************************************************************************* 56 * This function check the presence of a node (generic use of fdt library). 57 * Returns true if present, else return false. 58 ******************************************************************************/ 59 bool fdt_check_node(int node) 60 { 61 int len; 62 const char *cchar; 63 64 cchar = fdt_get_name(fdt, node, &len); 65 66 return (cchar != NULL) && (len >= 0); 67 } 68 69 /******************************************************************************* 70 * This function return global node status (generic use of fdt library). 71 ******************************************************************************/ 72 uint8_t fdt_get_status(int node) 73 { 74 uint8_t status = DT_DISABLED; 75 int len; 76 const char *cchar; 77 78 cchar = fdt_getprop(fdt, node, "status", &len); 79 if ((cchar == NULL) || 80 (strncmp(cchar, "okay", (size_t)len) == 0)) { 81 status |= DT_NON_SECURE; 82 } 83 84 cchar = fdt_getprop(fdt, node, "secure-status", &len); 85 if (cchar == NULL) { 86 if (status == DT_NON_SECURE) { 87 status |= DT_SECURE; 88 } 89 } else if (strncmp(cchar, "okay", (size_t)len) == 0) { 90 status |= DT_SECURE; 91 } 92 93 return status; 94 } 95 96 #if ENABLE_ASSERTIONS 97 /******************************************************************************* 98 * This function returns the address cells from the node parent. 99 * Returns: 100 * - #address-cells value if success. 101 * - invalid value if error. 102 * - a default value if undefined #address-cells property as per libfdt 103 * implementation. 104 ******************************************************************************/ 105 static int fdt_get_node_parent_address_cells(int node) 106 { 107 int parent; 108 109 parent = fdt_parent_offset(fdt, node); 110 if (parent < 0) { 111 return -FDT_ERR_NOTFOUND; 112 } 113 114 return fdt_address_cells(fdt, parent); 115 } 116 117 /******************************************************************************* 118 * This function returns the size cells from the node parent. 119 * Returns: 120 * - #size-cells value if success. 121 * - invalid value if error. 122 * - a default value if undefined #size-cells property as per libfdt 123 * implementation. 124 ******************************************************************************/ 125 static int fdt_get_node_parent_size_cells(int node) 126 { 127 int parent; 128 129 parent = fdt_parent_offset(fdt, node); 130 if (parent < 0) { 131 return -FDT_ERR_NOTFOUND; 132 } 133 134 return fdt_size_cells(fdt, parent); 135 } 136 #endif 137 138 /******************************************************************************* 139 * This function reads a value of a node property (generic use of fdt 140 * library). 141 * Returns value if success, and a default value if property not found. 142 * Default value is passed as parameter. 143 ******************************************************************************/ 144 uint32_t fdt_read_uint32_default(int node, const char *prop_name, 145 uint32_t dflt_value) 146 { 147 const fdt32_t *cuint; 148 int lenp; 149 150 cuint = fdt_getprop(fdt, node, prop_name, &lenp); 151 if (cuint == NULL) { 152 return dflt_value; 153 } 154 155 return fdt32_to_cpu(*cuint); 156 } 157 158 /******************************************************************************* 159 * This function fills reg node info (base & size) with an index found by 160 * checking the reg-names node. 161 * Returns 0 on success and a negative FDT error code on failure. 162 ******************************************************************************/ 163 int fdt_get_reg_props_by_name(int node, const char *name, uintptr_t *base, 164 size_t *size) 165 { 166 const fdt32_t *cuint; 167 int index, len; 168 169 assert((fdt_get_node_parent_address_cells(node) == 1) && 170 (fdt_get_node_parent_size_cells(node) == 1)); 171 172 index = fdt_stringlist_search(fdt, node, "reg-names", name); 173 if (index < 0) { 174 return index; 175 } 176 177 cuint = fdt_getprop(fdt, node, "reg", &len); 178 if (cuint == NULL) { 179 return -FDT_ERR_NOTFOUND; 180 } 181 182 if ((index * (int)sizeof(uint32_t)) > len) { 183 return -FDT_ERR_BADVALUE; 184 } 185 186 cuint += index << 1; 187 if (base != NULL) { 188 *base = fdt32_to_cpu(*cuint); 189 } 190 cuint++; 191 if (size != NULL) { 192 *size = fdt32_to_cpu(*cuint); 193 } 194 195 return 0; 196 } 197 198 /******************************************************************************* 199 * This function gets the stdout path node. 200 * It reads the value indicated inside the device tree. 201 * Returns node offset on success and a negative FDT error code on failure. 202 ******************************************************************************/ 203 static int dt_get_stdout_node_offset(void) 204 { 205 int node; 206 const char *cchar; 207 208 node = fdt_path_offset(fdt, "/secure-chosen"); 209 if (node < 0) { 210 node = fdt_path_offset(fdt, "/chosen"); 211 if (node < 0) { 212 return -FDT_ERR_NOTFOUND; 213 } 214 } 215 216 cchar = fdt_getprop(fdt, node, "stdout-path", NULL); 217 if (cchar == NULL) { 218 return -FDT_ERR_NOTFOUND; 219 } 220 221 node = -FDT_ERR_NOTFOUND; 222 if (strchr(cchar, (int)':') != NULL) { 223 const char *name; 224 char *str = (char *)cchar; 225 int len = 0; 226 227 while (strncmp(":", str, 1)) { 228 len++; 229 str++; 230 } 231 232 name = fdt_get_alias_namelen(fdt, cchar, len); 233 234 if (name != NULL) { 235 node = fdt_path_offset(fdt, name); 236 } 237 } else { 238 node = fdt_path_offset(fdt, cchar); 239 } 240 241 return node; 242 } 243 244 /******************************************************************************* 245 * This function gets the stdout pin configuration information from the DT. 246 * And then calls the sub-function to treat it and set GPIO registers. 247 * Returns 0 on success and a negative FDT error code on failure. 248 ******************************************************************************/ 249 int dt_set_stdout_pinctrl(void) 250 { 251 int node; 252 253 node = dt_get_stdout_node_offset(); 254 if (node < 0) { 255 return -FDT_ERR_NOTFOUND; 256 } 257 258 return dt_set_pinctrl_config(node); 259 } 260 261 /******************************************************************************* 262 * This function fills the generic information from a given node. 263 ******************************************************************************/ 264 void dt_fill_device_info(struct dt_node_info *info, int node) 265 { 266 const fdt32_t *cuint; 267 268 assert(fdt_get_node_parent_address_cells(node) == 1); 269 270 cuint = fdt_getprop(fdt, node, "reg", NULL); 271 if (cuint != NULL) { 272 info->base = fdt32_to_cpu(*cuint); 273 } else { 274 info->base = 0; 275 } 276 277 cuint = fdt_getprop(fdt, node, "clocks", NULL); 278 if (cuint != NULL) { 279 cuint++; 280 info->clock = (int)fdt32_to_cpu(*cuint); 281 } else { 282 info->clock = -1; 283 } 284 285 cuint = fdt_getprop(fdt, node, "resets", NULL); 286 if (cuint != NULL) { 287 cuint++; 288 info->reset = (int)fdt32_to_cpu(*cuint); 289 } else { 290 info->reset = -1; 291 } 292 293 info->status = fdt_get_status(node); 294 } 295 296 /******************************************************************************* 297 * This function retrieve the generic information from DT. 298 * Returns node on success and a negative FDT error code on failure. 299 ******************************************************************************/ 300 int dt_get_node(struct dt_node_info *info, int offset, const char *compat) 301 { 302 int node; 303 304 node = fdt_node_offset_by_compatible(fdt, offset, compat); 305 if (node < 0) { 306 return -FDT_ERR_NOTFOUND; 307 } 308 309 dt_fill_device_info(info, node); 310 311 return node; 312 } 313 314 /******************************************************************************* 315 * This function gets the UART instance info of stdout from the DT. 316 * Returns node on success and a negative FDT error code on failure. 317 ******************************************************************************/ 318 int dt_get_stdout_uart_info(struct dt_node_info *info) 319 { 320 int node; 321 322 node = dt_get_stdout_node_offset(); 323 if (node < 0) { 324 return -FDT_ERR_NOTFOUND; 325 } 326 327 dt_fill_device_info(info, node); 328 329 return node; 330 } 331 332 /******************************************************************************* 333 * This function gets DDR size information from the DT. 334 * Returns value in bytes on success, and 0 on failure. 335 ******************************************************************************/ 336 uint32_t dt_get_ddr_size(void) 337 { 338 int node; 339 340 node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT); 341 if (node < 0) { 342 INFO("%s: Cannot read DDR node in DT\n", __func__); 343 return 0; 344 } 345 346 return fdt_read_uint32_default(node, "st,mem-size", 0); 347 } 348 349 /******************************************************************************* 350 * This function gets DDRCTRL base address information from the DT. 351 * Returns value on success, and 0 on failure. 352 ******************************************************************************/ 353 uintptr_t dt_get_ddrctrl_base(void) 354 { 355 int node; 356 uint32_t array[4]; 357 358 node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT); 359 if (node < 0) { 360 INFO("%s: Cannot read DDR node in DT\n", __func__); 361 return 0; 362 } 363 364 assert((fdt_get_node_parent_address_cells(node) == 1) && 365 (fdt_get_node_parent_size_cells(node) == 1)); 366 367 if (fdt_read_uint32_array(fdt, node, "reg", 4, array) < 0) { 368 return 0; 369 } 370 371 return array[0]; 372 } 373 374 /******************************************************************************* 375 * This function gets DDRPHYC base address information from the DT. 376 * Returns value on success, and 0 on failure. 377 ******************************************************************************/ 378 uintptr_t dt_get_ddrphyc_base(void) 379 { 380 int node; 381 uint32_t array[4]; 382 383 node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT); 384 if (node < 0) { 385 INFO("%s: Cannot read DDR node in DT\n", __func__); 386 return 0; 387 } 388 389 assert((fdt_get_node_parent_address_cells(node) == 1) && 390 (fdt_get_node_parent_size_cells(node) == 1)); 391 392 if (fdt_read_uint32_array(fdt, node, "reg", 4, array) < 0) { 393 return 0; 394 } 395 396 return array[2]; 397 } 398 399 /******************************************************************************* 400 * This function gets PWR base address information from the DT. 401 * Returns value on success, and 0 on failure. 402 ******************************************************************************/ 403 uintptr_t dt_get_pwr_base(void) 404 { 405 int node; 406 const fdt32_t *cuint; 407 408 node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT); 409 if (node < 0) { 410 INFO("%s: Cannot read PWR node in DT\n", __func__); 411 return 0; 412 } 413 414 assert(fdt_get_node_parent_address_cells(node) == 1); 415 416 cuint = fdt_getprop(fdt, node, "reg", NULL); 417 if (cuint == NULL) { 418 return 0; 419 } 420 421 return fdt32_to_cpu(*cuint); 422 } 423 424 /******************************************************************************* 425 * This function gets PWR VDD regulator voltage information from the DT. 426 * Returns value in microvolts on success, and 0 on failure. 427 ******************************************************************************/ 428 uint32_t dt_get_pwr_vdd_voltage(void) 429 { 430 int node, pwr_regulators_node; 431 const fdt32_t *cuint; 432 433 node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT); 434 if (node < 0) { 435 INFO("%s: Cannot read PWR node in DT\n", __func__); 436 return 0; 437 } 438 439 pwr_regulators_node = fdt_subnode_offset(fdt, node, "pwr-regulators"); 440 if (pwr_regulators_node < 0) { 441 INFO("%s: Cannot read pwr-regulators node in DT\n", __func__); 442 return 0; 443 } 444 445 cuint = fdt_getprop(fdt, pwr_regulators_node, "vdd-supply", NULL); 446 if (cuint == NULL) { 447 return 0; 448 } 449 450 node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); 451 if (node < 0) { 452 return 0; 453 } 454 455 cuint = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL); 456 if (cuint == NULL) { 457 return 0; 458 } 459 460 return fdt32_to_cpu(*cuint); 461 } 462 463 /******************************************************************************* 464 * This function gets SYSCFG base address information from the DT. 465 * Returns value on success, and 0 on failure. 466 ******************************************************************************/ 467 uintptr_t dt_get_syscfg_base(void) 468 { 469 int node; 470 const fdt32_t *cuint; 471 472 node = fdt_node_offset_by_compatible(fdt, -1, DT_SYSCFG_COMPAT); 473 if (node < 0) { 474 INFO("%s: Cannot read SYSCFG node in DT\n", __func__); 475 return 0; 476 } 477 478 assert(fdt_get_node_parent_address_cells(node) == 1); 479 480 cuint = fdt_getprop(fdt, node, "reg", NULL); 481 if (cuint == NULL) { 482 return 0; 483 } 484 485 return fdt32_to_cpu(*cuint); 486 } 487 488 /******************************************************************************* 489 * This function retrieves board model from DT 490 * Returns string taken from model node, NULL otherwise 491 ******************************************************************************/ 492 const char *dt_get_board_model(void) 493 { 494 int node = fdt_path_offset(fdt, "/"); 495 496 if (node < 0) { 497 return NULL; 498 } 499 500 return (const char *)fdt_getprop(fdt, node, "model", NULL); 501 } 502