Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved. |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 8 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 10 | #include <libfdt.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 12 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <common/debug.h> |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 15 | #include <common/fdt_wrappers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #include <drivers/st/stm32_gpio.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | #include <drivers/st/stm32mp1_ddr.h> |
| 18 | #include <drivers/st/stm32mp1_ram.h> |
| 19 | |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 20 | #include <stm32mp_dt.h> |
| 21 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 22 | static int fdt_checked; |
| 23 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 24 | static void *fdt = (void *)(uintptr_t)STM32MP_DTB_BASE; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 25 | |
| 26 | /******************************************************************************* |
| 27 | * This function checks device tree file with its header. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 28 | * Returns 0 on success and a negative FDT error code on failure. |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 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). |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 57 | * Returns true if present, else return false. |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 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 | /******************************************************************************* |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 70 | * This function return global node status (generic use of fdt library). |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 71 | ******************************************************************************/ |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 72 | uint8_t fdt_get_status(int node) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 73 | { |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 74 | uint8_t status = DT_DISABLED; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 75 | int len; |
| 76 | const char *cchar; |
| 77 | |
| 78 | cchar = fdt_getprop(fdt, node, "status", &len); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 79 | if ((cchar == NULL) || |
| 80 | (strncmp(cchar, "okay", (size_t)len) == 0)) { |
| 81 | status |= DT_NON_SECURE; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 84 | cchar = fdt_getprop(fdt, node, "secure-status", &len); |
| 85 | if (cchar == NULL) { |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 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; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 93 | return status; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 96 | #if ENABLE_ASSERTIONS |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 97 | /******************************************************************************* |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 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 | ******************************************************************************/ |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 105 | static int fdt_get_node_parent_address_cells(int node) |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 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 | ******************************************************************************/ |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 125 | static int fdt_get_node_parent_size_cells(int node) |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 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 | } |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 136 | #endif |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 137 | |
| 138 | /******************************************************************************* |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 139 | * This function gets the stdout pin configuration information from the DT. |
| 140 | * And then calls the sub-function to treat it and set GPIO registers. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 141 | * Returns 0 on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 142 | ******************************************************************************/ |
| 143 | int dt_set_stdout_pinctrl(void) |
| 144 | { |
| 145 | int node; |
| 146 | |
Andre Przywara | eec9192 | 2020-04-09 11:27:21 +0100 | [diff] [blame] | 147 | node = fdt_get_stdout_node_offset(fdt); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 148 | if (node < 0) { |
| 149 | return -FDT_ERR_NOTFOUND; |
| 150 | } |
| 151 | |
| 152 | return dt_set_pinctrl_config(node); |
| 153 | } |
| 154 | |
| 155 | /******************************************************************************* |
| 156 | * This function fills the generic information from a given node. |
| 157 | ******************************************************************************/ |
| 158 | void dt_fill_device_info(struct dt_node_info *info, int node) |
| 159 | { |
| 160 | const fdt32_t *cuint; |
| 161 | |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 162 | assert(fdt_get_node_parent_address_cells(node) == 1); |
| 163 | |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 164 | cuint = fdt_getprop(fdt, node, "reg", NULL); |
| 165 | if (cuint != NULL) { |
| 166 | info->base = fdt32_to_cpu(*cuint); |
| 167 | } else { |
| 168 | info->base = 0; |
| 169 | } |
| 170 | |
| 171 | cuint = fdt_getprop(fdt, node, "clocks", NULL); |
| 172 | if (cuint != NULL) { |
| 173 | cuint++; |
| 174 | info->clock = (int)fdt32_to_cpu(*cuint); |
| 175 | } else { |
| 176 | info->clock = -1; |
| 177 | } |
| 178 | |
| 179 | cuint = fdt_getprop(fdt, node, "resets", NULL); |
| 180 | if (cuint != NULL) { |
| 181 | cuint++; |
| 182 | info->reset = (int)fdt32_to_cpu(*cuint); |
| 183 | } else { |
| 184 | info->reset = -1; |
| 185 | } |
| 186 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 187 | info->status = fdt_get_status(node); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /******************************************************************************* |
| 191 | * This function retrieve the generic information from DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 192 | * Returns node on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 193 | ******************************************************************************/ |
| 194 | int dt_get_node(struct dt_node_info *info, int offset, const char *compat) |
| 195 | { |
| 196 | int node; |
| 197 | |
| 198 | node = fdt_node_offset_by_compatible(fdt, offset, compat); |
| 199 | if (node < 0) { |
| 200 | return -FDT_ERR_NOTFOUND; |
| 201 | } |
| 202 | |
| 203 | dt_fill_device_info(info, node); |
| 204 | |
| 205 | return node; |
| 206 | } |
| 207 | |
| 208 | /******************************************************************************* |
| 209 | * This function gets the UART instance info of stdout from the DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 210 | * Returns node on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 211 | ******************************************************************************/ |
| 212 | int dt_get_stdout_uart_info(struct dt_node_info *info) |
| 213 | { |
| 214 | int node; |
| 215 | |
Andre Przywara | eec9192 | 2020-04-09 11:27:21 +0100 | [diff] [blame] | 216 | node = fdt_get_stdout_node_offset(fdt); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 217 | if (node < 0) { |
| 218 | return -FDT_ERR_NOTFOUND; |
| 219 | } |
| 220 | |
| 221 | dt_fill_device_info(info, node); |
| 222 | |
| 223 | return node; |
| 224 | } |
| 225 | |
| 226 | /******************************************************************************* |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 227 | * This function gets DDR size information from the DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 228 | * Returns value in bytes on success, and 0 on failure. |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 229 | ******************************************************************************/ |
| 230 | uint32_t dt_get_ddr_size(void) |
| 231 | { |
| 232 | int node; |
| 233 | |
| 234 | node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT); |
| 235 | if (node < 0) { |
| 236 | INFO("%s: Cannot read DDR node in DT\n", __func__); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 237 | return 0; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Andre Przywara | 2d5690c | 2020-03-26 11:50:33 +0000 | [diff] [blame] | 240 | return fdt_read_uint32_default(fdt, node, "st,mem-size", 0); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /******************************************************************************* |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 244 | * This function gets DDRCTRL base address information from the DT. |
| 245 | * Returns value on success, and 0 on failure. |
| 246 | ******************************************************************************/ |
| 247 | uintptr_t dt_get_ddrctrl_base(void) |
| 248 | { |
| 249 | int node; |
| 250 | uint32_t array[4]; |
| 251 | |
| 252 | node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT); |
| 253 | if (node < 0) { |
| 254 | INFO("%s: Cannot read DDR node in DT\n", __func__); |
| 255 | return 0; |
| 256 | } |
| 257 | |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 258 | assert((fdt_get_node_parent_address_cells(node) == 1) && |
| 259 | (fdt_get_node_parent_size_cells(node) == 1)); |
| 260 | |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 261 | if (fdt_read_uint32_array(fdt, node, "reg", 4, array) < 0) { |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | return array[0]; |
| 266 | } |
| 267 | |
| 268 | /******************************************************************************* |
| 269 | * This function gets DDRPHYC base address information from the DT. |
| 270 | * Returns value on success, and 0 on failure. |
| 271 | ******************************************************************************/ |
| 272 | uintptr_t dt_get_ddrphyc_base(void) |
| 273 | { |
| 274 | int node; |
| 275 | uint32_t array[4]; |
| 276 | |
| 277 | node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT); |
| 278 | if (node < 0) { |
| 279 | INFO("%s: Cannot read DDR node in DT\n", __func__); |
| 280 | return 0; |
| 281 | } |
| 282 | |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 283 | assert((fdt_get_node_parent_address_cells(node) == 1) && |
| 284 | (fdt_get_node_parent_size_cells(node) == 1)); |
| 285 | |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 286 | if (fdt_read_uint32_array(fdt, node, "reg", 4, array) < 0) { |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | return array[2]; |
| 291 | } |
| 292 | |
| 293 | /******************************************************************************* |
| 294 | * This function gets PWR base address information from the DT. |
| 295 | * Returns value on success, and 0 on failure. |
| 296 | ******************************************************************************/ |
| 297 | uintptr_t dt_get_pwr_base(void) |
| 298 | { |
| 299 | int node; |
| 300 | const fdt32_t *cuint; |
| 301 | |
| 302 | node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT); |
| 303 | if (node < 0) { |
| 304 | INFO("%s: Cannot read PWR node in DT\n", __func__); |
| 305 | return 0; |
| 306 | } |
| 307 | |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 308 | assert(fdt_get_node_parent_address_cells(node) == 1); |
| 309 | |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 310 | cuint = fdt_getprop(fdt, node, "reg", NULL); |
| 311 | if (cuint == NULL) { |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | return fdt32_to_cpu(*cuint); |
| 316 | } |
| 317 | |
| 318 | /******************************************************************************* |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 319 | * This function gets PWR VDD regulator voltage information from the DT. |
| 320 | * Returns value in microvolts on success, and 0 on failure. |
| 321 | ******************************************************************************/ |
| 322 | uint32_t dt_get_pwr_vdd_voltage(void) |
| 323 | { |
| 324 | int node, pwr_regulators_node; |
| 325 | const fdt32_t *cuint; |
| 326 | |
| 327 | node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT); |
| 328 | if (node < 0) { |
| 329 | INFO("%s: Cannot read PWR node in DT\n", __func__); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | pwr_regulators_node = fdt_subnode_offset(fdt, node, "pwr-regulators"); |
Yann Gautier | df47392 | 2020-03-18 14:35:27 +0100 | [diff] [blame] | 334 | if (pwr_regulators_node < 0) { |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 335 | INFO("%s: Cannot read pwr-regulators node in DT\n", __func__); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | cuint = fdt_getprop(fdt, pwr_regulators_node, "vdd-supply", NULL); |
| 340 | if (cuint == NULL) { |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); |
| 345 | if (node < 0) { |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | cuint = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL); |
| 350 | if (cuint == NULL) { |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | return fdt32_to_cpu(*cuint); |
| 355 | } |
| 356 | |
| 357 | /******************************************************************************* |
| 358 | * This function gets SYSCFG base address information from the DT. |
| 359 | * Returns value on success, and 0 on failure. |
| 360 | ******************************************************************************/ |
| 361 | uintptr_t dt_get_syscfg_base(void) |
| 362 | { |
| 363 | int node; |
| 364 | const fdt32_t *cuint; |
| 365 | |
| 366 | node = fdt_node_offset_by_compatible(fdt, -1, DT_SYSCFG_COMPAT); |
| 367 | if (node < 0) { |
| 368 | INFO("%s: Cannot read SYSCFG node in DT\n", __func__); |
| 369 | return 0; |
| 370 | } |
| 371 | |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 372 | assert(fdt_get_node_parent_address_cells(node) == 1); |
| 373 | |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 374 | cuint = fdt_getprop(fdt, node, "reg", NULL); |
| 375 | if (cuint == NULL) { |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | return fdt32_to_cpu(*cuint); |
| 380 | } |
| 381 | |
| 382 | /******************************************************************************* |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 383 | * This function retrieves board model from DT |
| 384 | * Returns string taken from model node, NULL otherwise |
| 385 | ******************************************************************************/ |
| 386 | const char *dt_get_board_model(void) |
| 387 | { |
| 388 | int node = fdt_path_offset(fdt, "/"); |
| 389 | |
| 390 | if (node < 0) { |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | return (const char *)fdt_getprop(fdt, node, "model", NULL); |
| 395 | } |