Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | 1c95933 | 2021-03-10 14:07:34 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 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 | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 22 | static void *fdt; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 23 | |
| 24 | /******************************************************************************* |
| 25 | * This function checks device tree file with its header. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 26 | * Returns 0 on success and a negative FDT error code on failure. |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 27 | ******************************************************************************/ |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 28 | int dt_open_and_check(uintptr_t dt_addr) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 29 | { |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 30 | int ret; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 31 | |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 32 | ret = fdt_check_header((void *)dt_addr); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 33 | if (ret == 0) { |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 34 | fdt = (void *)dt_addr; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | return ret; |
| 38 | } |
| 39 | |
| 40 | /******************************************************************************* |
| 41 | * This function gets the address of the DT. |
| 42 | * If DT is OK, fdt_addr is filled with DT address. |
| 43 | * Returns 1 if success, 0 otherwise. |
| 44 | ******************************************************************************/ |
| 45 | int fdt_get_address(void **fdt_addr) |
| 46 | { |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 47 | if (fdt == NULL) { |
| 48 | return 0; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 51 | *fdt_addr = fdt; |
| 52 | |
| 53 | return 1; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /******************************************************************************* |
| 57 | * 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] | 58 | * Returns true if present, else return false. |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 59 | ******************************************************************************/ |
| 60 | bool fdt_check_node(int node) |
| 61 | { |
| 62 | int len; |
| 63 | const char *cchar; |
| 64 | |
| 65 | cchar = fdt_get_name(fdt, node, &len); |
| 66 | |
| 67 | return (cchar != NULL) && (len >= 0); |
| 68 | } |
| 69 | |
| 70 | /******************************************************************************* |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 71 | * This function return global node status (generic use of fdt library). |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 72 | ******************************************************************************/ |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 73 | uint8_t fdt_get_status(int node) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 74 | { |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 75 | uint8_t status = DT_DISABLED; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 76 | const char *cchar; |
| 77 | |
Yann Gautier | 1c95933 | 2021-03-10 14:07:34 +0100 | [diff] [blame] | 78 | cchar = fdt_getprop(fdt, node, "status", NULL); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 79 | if ((cchar == NULL) || |
Yann Gautier | 1c95933 | 2021-03-10 14:07:34 +0100 | [diff] [blame] | 80 | (strncmp(cchar, "okay", strlen("okay")) == 0)) { |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 81 | status |= DT_NON_SECURE; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Yann Gautier | 1c95933 | 2021-03-10 14:07:34 +0100 | [diff] [blame] | 84 | cchar = fdt_getprop(fdt, node, "secure-status", NULL); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 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 | } |
Yann Gautier | 1c95933 | 2021-03-10 14:07:34 +0100 | [diff] [blame] | 89 | } else if (strncmp(cchar, "okay", strlen("okay")) == 0) { |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 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 | } |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 116 | #endif |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 117 | |
| 118 | /******************************************************************************* |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 119 | * This function gets the stdout pin configuration information from the DT. |
| 120 | * 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] | 121 | * Returns 0 on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 122 | ******************************************************************************/ |
| 123 | int dt_set_stdout_pinctrl(void) |
| 124 | { |
| 125 | int node; |
| 126 | |
Andre Przywara | eec9192 | 2020-04-09 11:27:21 +0100 | [diff] [blame] | 127 | node = fdt_get_stdout_node_offset(fdt); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 128 | if (node < 0) { |
| 129 | return -FDT_ERR_NOTFOUND; |
| 130 | } |
| 131 | |
| 132 | return dt_set_pinctrl_config(node); |
| 133 | } |
| 134 | |
| 135 | /******************************************************************************* |
| 136 | * This function fills the generic information from a given node. |
| 137 | ******************************************************************************/ |
| 138 | void dt_fill_device_info(struct dt_node_info *info, int node) |
| 139 | { |
| 140 | const fdt32_t *cuint; |
| 141 | |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 142 | assert(fdt_get_node_parent_address_cells(node) == 1); |
| 143 | |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 144 | cuint = fdt_getprop(fdt, node, "reg", NULL); |
| 145 | if (cuint != NULL) { |
| 146 | info->base = fdt32_to_cpu(*cuint); |
| 147 | } else { |
| 148 | info->base = 0; |
| 149 | } |
| 150 | |
| 151 | cuint = fdt_getprop(fdt, node, "clocks", NULL); |
| 152 | if (cuint != NULL) { |
| 153 | cuint++; |
| 154 | info->clock = (int)fdt32_to_cpu(*cuint); |
| 155 | } else { |
| 156 | info->clock = -1; |
| 157 | } |
| 158 | |
| 159 | cuint = fdt_getprop(fdt, node, "resets", NULL); |
| 160 | if (cuint != NULL) { |
| 161 | cuint++; |
| 162 | info->reset = (int)fdt32_to_cpu(*cuint); |
| 163 | } else { |
| 164 | info->reset = -1; |
| 165 | } |
| 166 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 167 | info->status = fdt_get_status(node); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /******************************************************************************* |
| 171 | * This function retrieve the generic information from DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 172 | * Returns node on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 173 | ******************************************************************************/ |
| 174 | int dt_get_node(struct dt_node_info *info, int offset, const char *compat) |
| 175 | { |
| 176 | int node; |
| 177 | |
| 178 | node = fdt_node_offset_by_compatible(fdt, offset, compat); |
| 179 | if (node < 0) { |
| 180 | return -FDT_ERR_NOTFOUND; |
| 181 | } |
| 182 | |
| 183 | dt_fill_device_info(info, node); |
| 184 | |
| 185 | return node; |
| 186 | } |
| 187 | |
| 188 | /******************************************************************************* |
| 189 | * This function gets the UART instance info of stdout from the DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 190 | * Returns node on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 191 | ******************************************************************************/ |
| 192 | int dt_get_stdout_uart_info(struct dt_node_info *info) |
| 193 | { |
| 194 | int node; |
| 195 | |
Andre Przywara | eec9192 | 2020-04-09 11:27:21 +0100 | [diff] [blame] | 196 | node = fdt_get_stdout_node_offset(fdt); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 197 | if (node < 0) { |
| 198 | return -FDT_ERR_NOTFOUND; |
| 199 | } |
| 200 | |
| 201 | dt_fill_device_info(info, node); |
| 202 | |
| 203 | return node; |
| 204 | } |
| 205 | |
| 206 | /******************************************************************************* |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 207 | * This function gets DDR size information from the DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 208 | * Returns value in bytes on success, and 0 on failure. |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 209 | ******************************************************************************/ |
| 210 | uint32_t dt_get_ddr_size(void) |
| 211 | { |
Lionel Debieve | 003972c | 2020-09-24 16:01:12 +0200 | [diff] [blame] | 212 | static uint32_t size; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 213 | int node; |
| 214 | |
Lionel Debieve | 003972c | 2020-09-24 16:01:12 +0200 | [diff] [blame] | 215 | if (size != 0U) { |
| 216 | return size; |
| 217 | } |
| 218 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 219 | node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT); |
| 220 | if (node < 0) { |
| 221 | INFO("%s: Cannot read DDR node in DT\n", __func__); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 222 | return 0; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Lionel Debieve | 003972c | 2020-09-24 16:01:12 +0200 | [diff] [blame] | 225 | size = fdt_read_uint32_default(fdt, node, "st,mem-size", 0U); |
| 226 | |
| 227 | flush_dcache_range((uintptr_t)&size, sizeof(uint32_t)); |
| 228 | |
| 229 | return size; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /******************************************************************************* |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 233 | * This function gets PWR VDD regulator voltage information from the DT. |
| 234 | * Returns value in microvolts on success, and 0 on failure. |
| 235 | ******************************************************************************/ |
| 236 | uint32_t dt_get_pwr_vdd_voltage(void) |
| 237 | { |
| 238 | int node, pwr_regulators_node; |
| 239 | const fdt32_t *cuint; |
| 240 | |
| 241 | node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT); |
| 242 | if (node < 0) { |
| 243 | INFO("%s: Cannot read PWR node in DT\n", __func__); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | pwr_regulators_node = fdt_subnode_offset(fdt, node, "pwr-regulators"); |
Yann Gautier | df47392 | 2020-03-18 14:35:27 +0100 | [diff] [blame] | 248 | if (pwr_regulators_node < 0) { |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 249 | INFO("%s: Cannot read pwr-regulators node in DT\n", __func__); |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | cuint = fdt_getprop(fdt, pwr_regulators_node, "vdd-supply", NULL); |
| 254 | if (cuint == NULL) { |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); |
| 259 | if (node < 0) { |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | cuint = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL); |
| 264 | if (cuint == NULL) { |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | return fdt32_to_cpu(*cuint); |
| 269 | } |
| 270 | |
| 271 | /******************************************************************************* |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 272 | * This function retrieves board model from DT |
| 273 | * Returns string taken from model node, NULL otherwise |
| 274 | ******************************************************************************/ |
| 275 | const char *dt_get_board_model(void) |
| 276 | { |
| 277 | int node = fdt_path_offset(fdt, "/"); |
| 278 | |
| 279 | if (node < 0) { |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | return (const char *)fdt_getprop(fdt, node, "model", NULL); |
| 284 | } |
Etienne Carriere | d81dadf | 2020-04-25 11:14:45 +0200 | [diff] [blame] | 285 | |
| 286 | /******************************************************************************* |
| 287 | * This function gets the pin count for a GPIO bank based from the FDT. |
| 288 | * It also checks node consistency. |
| 289 | ******************************************************************************/ |
| 290 | int fdt_get_gpio_bank_pin_count(unsigned int bank) |
| 291 | { |
| 292 | int pinctrl_node; |
| 293 | int node; |
| 294 | uint32_t bank_offset; |
| 295 | |
| 296 | pinctrl_node = stm32_get_gpio_bank_pinctrl_node(fdt, bank); |
| 297 | if (pinctrl_node < 0) { |
| 298 | return -FDT_ERR_NOTFOUND; |
| 299 | } |
| 300 | |
| 301 | bank_offset = stm32_get_gpio_bank_offset(bank); |
| 302 | |
| 303 | fdt_for_each_subnode(node, fdt, pinctrl_node) { |
| 304 | const fdt32_t *cuint; |
| 305 | |
| 306 | if (fdt_getprop(fdt, node, "gpio-controller", NULL) == NULL) { |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | cuint = fdt_getprop(fdt, node, "reg", NULL); |
| 311 | if (cuint == NULL) { |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | if (fdt32_to_cpu(*cuint) != bank_offset) { |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | if (fdt_get_status(node) == DT_DISABLED) { |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | cuint = fdt_getprop(fdt, node, "ngpios", NULL); |
| 324 | if (cuint == NULL) { |
| 325 | return -FDT_ERR_NOTFOUND; |
| 326 | } |
| 327 | |
| 328 | return (int)fdt32_to_cpu(*cuint); |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |