Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1 | /* |
Sebastien PASDELOUP | 1c2d89b | 2023-06-05 09:56:49 +0200 | [diff] [blame] | 2 | * Copyright (c) 2017-2023, 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 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <common/debug.h> |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 11 | #include <common/fdt_wrappers.h> |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 12 | #include <drivers/st/regulator.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | #include <drivers/st/stm32_gpio.h> |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 14 | #include <libfdt.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 15 | |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 16 | #include <platform_def.h> |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 17 | #include <stm32mp_dt.h> |
| 18 | |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 19 | static void *fdt; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 20 | |
| 21 | /******************************************************************************* |
| 22 | * This function checks device tree file with its header. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 23 | * Returns 0 on success and a negative FDT error code on failure. |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 24 | ******************************************************************************/ |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 25 | int dt_open_and_check(uintptr_t dt_addr) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 26 | { |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 27 | int ret; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 28 | |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 29 | ret = fdt_check_header((void *)dt_addr); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 30 | if (ret == 0) { |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 31 | fdt = (void *)dt_addr; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | return ret; |
| 35 | } |
| 36 | |
| 37 | /******************************************************************************* |
| 38 | * This function gets the address of the DT. |
| 39 | * If DT is OK, fdt_addr is filled with DT address. |
| 40 | * Returns 1 if success, 0 otherwise. |
| 41 | ******************************************************************************/ |
| 42 | int fdt_get_address(void **fdt_addr) |
| 43 | { |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 44 | if (fdt == NULL) { |
| 45 | return 0; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 48 | *fdt_addr = fdt; |
| 49 | |
| 50 | return 1; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /******************************************************************************* |
| 54 | * 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] | 55 | * Returns true if present, else return false. |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 56 | ******************************************************************************/ |
| 57 | bool fdt_check_node(int node) |
| 58 | { |
| 59 | int len; |
| 60 | const char *cchar; |
| 61 | |
| 62 | cchar = fdt_get_name(fdt, node, &len); |
| 63 | |
| 64 | return (cchar != NULL) && (len >= 0); |
| 65 | } |
| 66 | |
| 67 | /******************************************************************************* |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 68 | * This function return global node status (generic use of fdt library). |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 69 | ******************************************************************************/ |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 70 | uint8_t fdt_get_status(int node) |
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 status = DT_DISABLED; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 73 | const char *cchar; |
| 74 | |
Yann Gautier | 1c95933 | 2021-03-10 14:07:34 +0100 | [diff] [blame] | 75 | cchar = fdt_getprop(fdt, node, "status", NULL); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 76 | if ((cchar == NULL) || |
Yann Gautier | 1c95933 | 2021-03-10 14:07:34 +0100 | [diff] [blame] | 77 | (strncmp(cchar, "okay", strlen("okay")) == 0)) { |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 78 | status |= DT_NON_SECURE; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Yann Gautier | 1c95933 | 2021-03-10 14:07:34 +0100 | [diff] [blame] | 81 | cchar = fdt_getprop(fdt, node, "secure-status", NULL); |
Yann Gautier | 6768c07 | 2022-11-24 19:16:46 +0100 | [diff] [blame] | 82 | if (((cchar == NULL) && (status == DT_NON_SECURE)) || |
| 83 | ((cchar != NULL) && (strncmp(cchar, "okay", strlen("okay")) == 0))) { |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 84 | status |= DT_SECURE; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 87 | return status; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 90 | #if ENABLE_ASSERTIONS |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 91 | /******************************************************************************* |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 92 | * This function returns the address cells from the node parent. |
| 93 | * Returns: |
| 94 | * - #address-cells value if success. |
| 95 | * - invalid value if error. |
| 96 | * - a default value if undefined #address-cells property as per libfdt |
| 97 | * implementation. |
| 98 | ******************************************************************************/ |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 99 | static int fdt_get_node_parent_address_cells(int node) |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 100 | { |
| 101 | int parent; |
| 102 | |
| 103 | parent = fdt_parent_offset(fdt, node); |
| 104 | if (parent < 0) { |
| 105 | return -FDT_ERR_NOTFOUND; |
| 106 | } |
| 107 | |
| 108 | return fdt_address_cells(fdt, parent); |
| 109 | } |
Yann Gautier | 2260b84 | 2020-03-11 17:17:51 +0100 | [diff] [blame] | 110 | #endif |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 111 | |
| 112 | /******************************************************************************* |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 113 | * This function gets the stdout pin configuration information from the DT. |
| 114 | * 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] | 115 | * Returns 0 on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 116 | ******************************************************************************/ |
| 117 | int dt_set_stdout_pinctrl(void) |
| 118 | { |
| 119 | int node; |
| 120 | |
Andre Przywara | eec9192 | 2020-04-09 11:27:21 +0100 | [diff] [blame] | 121 | node = fdt_get_stdout_node_offset(fdt); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 122 | if (node < 0) { |
| 123 | return -FDT_ERR_NOTFOUND; |
| 124 | } |
| 125 | |
| 126 | return dt_set_pinctrl_config(node); |
| 127 | } |
| 128 | |
| 129 | /******************************************************************************* |
| 130 | * This function fills the generic information from a given node. |
| 131 | ******************************************************************************/ |
| 132 | void dt_fill_device_info(struct dt_node_info *info, int node) |
| 133 | { |
| 134 | const fdt32_t *cuint; |
| 135 | |
Lionel Debieve | b0899eb | 2019-09-24 17:41:11 +0200 | [diff] [blame] | 136 | assert(fdt_get_node_parent_address_cells(node) == 1); |
| 137 | |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 138 | cuint = fdt_getprop(fdt, node, "reg", NULL); |
| 139 | if (cuint != NULL) { |
| 140 | info->base = fdt32_to_cpu(*cuint); |
| 141 | } else { |
| 142 | info->base = 0; |
| 143 | } |
| 144 | |
| 145 | cuint = fdt_getprop(fdt, node, "clocks", NULL); |
| 146 | if (cuint != NULL) { |
| 147 | cuint++; |
| 148 | info->clock = (int)fdt32_to_cpu(*cuint); |
| 149 | } else { |
| 150 | info->clock = -1; |
| 151 | } |
| 152 | |
| 153 | cuint = fdt_getprop(fdt, node, "resets", NULL); |
| 154 | if (cuint != NULL) { |
| 155 | cuint++; |
| 156 | info->reset = (int)fdt32_to_cpu(*cuint); |
| 157 | } else { |
| 158 | info->reset = -1; |
| 159 | } |
| 160 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 161 | info->status = fdt_get_status(node); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /******************************************************************************* |
| 165 | * This function retrieve the generic information from DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 166 | * Returns node on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 167 | ******************************************************************************/ |
| 168 | int dt_get_node(struct dt_node_info *info, int offset, const char *compat) |
| 169 | { |
| 170 | int node; |
| 171 | |
| 172 | node = fdt_node_offset_by_compatible(fdt, offset, compat); |
| 173 | if (node < 0) { |
| 174 | return -FDT_ERR_NOTFOUND; |
| 175 | } |
| 176 | |
| 177 | dt_fill_device_info(info, node); |
| 178 | |
| 179 | return node; |
| 180 | } |
| 181 | |
| 182 | /******************************************************************************* |
| 183 | * This function gets the UART instance info of stdout from the DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 184 | * Returns node on success and a negative FDT error code on failure. |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 185 | ******************************************************************************/ |
| 186 | int dt_get_stdout_uart_info(struct dt_node_info *info) |
| 187 | { |
| 188 | int node; |
| 189 | |
Andre Przywara | eec9192 | 2020-04-09 11:27:21 +0100 | [diff] [blame] | 190 | node = fdt_get_stdout_node_offset(fdt); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 191 | if (node < 0) { |
| 192 | return -FDT_ERR_NOTFOUND; |
| 193 | } |
| 194 | |
| 195 | dt_fill_device_info(info, node); |
| 196 | |
| 197 | return node; |
| 198 | } |
| 199 | |
| 200 | /******************************************************************************* |
Yann Gautier | 4e26784 | 2021-09-29 11:31:09 +0200 | [diff] [blame] | 201 | * This function returns the node offset matching compatible string in the DT, |
| 202 | * and also matching the reg property with the given address. |
| 203 | * Returns value on success, and error value on failure. |
| 204 | ******************************************************************************/ |
| 205 | int dt_match_instance_by_compatible(const char *compatible, uintptr_t address) |
| 206 | { |
| 207 | int node; |
| 208 | |
| 209 | fdt_for_each_compatible_node(fdt, node, compatible) { |
| 210 | const fdt32_t *cuint; |
| 211 | |
| 212 | assert(fdt_get_node_parent_address_cells(node) == 1); |
| 213 | |
| 214 | cuint = fdt_getprop(fdt, node, "reg", NULL); |
| 215 | if (cuint == NULL) { |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | if ((uintptr_t)fdt32_to_cpu(*cuint) == address) { |
| 220 | return node; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return -FDT_ERR_NOTFOUND; |
| 225 | } |
| 226 | |
| 227 | /******************************************************************************* |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 228 | * This function gets DDR size information from the DT. |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 229 | * Returns value in bytes on success, and 0 on failure. |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 230 | ******************************************************************************/ |
Sebastien PASDELOUP | 1c2d89b | 2023-06-05 09:56:49 +0200 | [diff] [blame] | 231 | size_t dt_get_ddr_size(void) |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 232 | { |
Sebastien PASDELOUP | 1c2d89b | 2023-06-05 09:56:49 +0200 | [diff] [blame] | 233 | static size_t size; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 234 | int node; |
| 235 | |
Lionel Debieve | 003972c | 2020-09-24 16:01:12 +0200 | [diff] [blame] | 236 | if (size != 0U) { |
| 237 | return size; |
| 238 | } |
| 239 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 240 | node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT); |
| 241 | if (node < 0) { |
| 242 | INFO("%s: Cannot read DDR node in DT\n", __func__); |
Sebastien PASDELOUP | 1c2d89b | 2023-06-05 09:56:49 +0200 | [diff] [blame] | 243 | return 0U; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Sebastien PASDELOUP | 1c2d89b | 2023-06-05 09:56:49 +0200 | [diff] [blame] | 246 | size = (size_t)fdt_read_uint32_default(fdt, node, "st,mem-size", 0U); |
Lionel Debieve | 003972c | 2020-09-24 16:01:12 +0200 | [diff] [blame] | 247 | |
Sebastien PASDELOUP | 1c2d89b | 2023-06-05 09:56:49 +0200 | [diff] [blame] | 248 | flush_dcache_range((uintptr_t)&size, sizeof(size_t)); |
Lionel Debieve | 003972c | 2020-09-24 16:01:12 +0200 | [diff] [blame] | 249 | |
| 250 | return size; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /******************************************************************************* |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 254 | * This function gets PWR VDD regulator voltage information from the DT. |
| 255 | * Returns value in microvolts on success, and 0 on failure. |
| 256 | ******************************************************************************/ |
| 257 | uint32_t dt_get_pwr_vdd_voltage(void) |
| 258 | { |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 259 | struct rdev *regul = dt_get_vdd_regulator(); |
| 260 | uint16_t min; |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 261 | |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 262 | if (regul == NULL) { |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 266 | regulator_get_range(regul, &min, NULL); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 267 | |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 268 | return (uint32_t)min * 1000U; |
| 269 | } |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 270 | |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 271 | /******************************************************************************* |
| 272 | * This function retrieves VDD supply regulator from DT. |
| 273 | * Returns an rdev taken from supply node, NULL otherwise. |
| 274 | ******************************************************************************/ |
| 275 | struct rdev *dt_get_vdd_regulator(void) |
| 276 | { |
| 277 | int node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT); |
| 278 | |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 279 | if (node < 0) { |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 280 | return NULL; |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 283 | return regulator_get_by_supply_name(fdt, node, "vdd"); |
| 284 | } |
| 285 | |
| 286 | /******************************************************************************* |
| 287 | * This function retrieves CPU supply regulator from DT. |
| 288 | * Returns an rdev taken from supply node, NULL otherwise. |
| 289 | ******************************************************************************/ |
| 290 | struct rdev *dt_get_cpu_regulator(void) |
| 291 | { |
| 292 | int node = fdt_path_offset(fdt, "/cpus/cpu@0"); |
| 293 | |
| 294 | if (node < 0) { |
| 295 | return NULL; |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Yann Gautier | 296e1ab | 2021-09-17 16:08:12 +0200 | [diff] [blame] | 298 | return regulator_get_by_supply_name(fdt, node, "cpu"); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /******************************************************************************* |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 302 | * This function retrieves board model from DT |
| 303 | * Returns string taken from model node, NULL otherwise |
| 304 | ******************************************************************************/ |
| 305 | const char *dt_get_board_model(void) |
| 306 | { |
| 307 | int node = fdt_path_offset(fdt, "/"); |
| 308 | |
| 309 | if (node < 0) { |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | return (const char *)fdt_getprop(fdt, node, "model", NULL); |
| 314 | } |
Etienne Carriere | d81dadf | 2020-04-25 11:14:45 +0200 | [diff] [blame] | 315 | |
| 316 | /******************************************************************************* |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 317 | * dt_find_otp_name: get OTP ID and length in DT. |
| 318 | * name: sub-node name to look up. |
| 319 | * otp: pointer to read OTP number or NULL. |
| 320 | * otp_len: pointer to read OTP length in bits or NULL. |
| 321 | * return value: 0 if no error, an FDT error value otherwise. |
| 322 | ******************************************************************************/ |
| 323 | int dt_find_otp_name(const char *name, uint32_t *otp, uint32_t *otp_len) |
| 324 | { |
| 325 | int node; |
Patrick Delaunay | 82ece5c | 2022-03-01 09:56:03 +0100 | [diff] [blame] | 326 | int len; |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 327 | const fdt32_t *cuint; |
| 328 | |
| 329 | if ((name == NULL) || (otp == NULL)) { |
| 330 | return -FDT_ERR_BADVALUE; |
| 331 | } |
| 332 | |
Patrick Delaunay | 82ece5c | 2022-03-01 09:56:03 +0100 | [diff] [blame] | 333 | node = fdt_node_offset_by_compatible(fdt, -1, DT_BSEC_COMPAT); |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 334 | if (node < 0) { |
| 335 | return node; |
| 336 | } |
| 337 | |
Patrick Delaunay | 82ece5c | 2022-03-01 09:56:03 +0100 | [diff] [blame] | 338 | node = fdt_subnode_offset(fdt, node, name); |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 339 | if (node < 0) { |
Patrick Delaunay | 82ece5c | 2022-03-01 09:56:03 +0100 | [diff] [blame] | 340 | ERROR("nvmem node %s not found\n", name); |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 341 | return node; |
| 342 | } |
| 343 | |
| 344 | cuint = fdt_getprop(fdt, node, "reg", &len); |
| 345 | if ((cuint == NULL) || (len != (2 * (int)sizeof(uint32_t)))) { |
Patrick Delaunay | 82ece5c | 2022-03-01 09:56:03 +0100 | [diff] [blame] | 346 | ERROR("Malformed nvmem node %s: ignored\n", name); |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 347 | return -FDT_ERR_BADVALUE; |
| 348 | } |
| 349 | |
Yann Gautier | 409b382 | 2022-11-21 13:26:37 +0100 | [diff] [blame] | 350 | if ((fdt32_to_cpu(*cuint) % sizeof(uint32_t)) != 0U) { |
Patrick Delaunay | 82ece5c | 2022-03-01 09:56:03 +0100 | [diff] [blame] | 351 | ERROR("Misaligned nvmem %s element: ignored\n", name); |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 352 | return -FDT_ERR_BADVALUE; |
| 353 | } |
| 354 | |
| 355 | if (otp != NULL) { |
| 356 | *otp = fdt32_to_cpu(*cuint) / sizeof(uint32_t); |
| 357 | } |
| 358 | |
| 359 | if (otp_len != NULL) { |
| 360 | cuint++; |
| 361 | *otp_len = fdt32_to_cpu(*cuint) * CHAR_BIT; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /******************************************************************************* |
Etienne Carriere | d81dadf | 2020-04-25 11:14:45 +0200 | [diff] [blame] | 368 | * This function gets the pin count for a GPIO bank based from the FDT. |
| 369 | * It also checks node consistency. |
| 370 | ******************************************************************************/ |
| 371 | int fdt_get_gpio_bank_pin_count(unsigned int bank) |
| 372 | { |
| 373 | int pinctrl_node; |
| 374 | int node; |
| 375 | uint32_t bank_offset; |
| 376 | |
| 377 | pinctrl_node = stm32_get_gpio_bank_pinctrl_node(fdt, bank); |
| 378 | if (pinctrl_node < 0) { |
| 379 | return -FDT_ERR_NOTFOUND; |
| 380 | } |
| 381 | |
| 382 | bank_offset = stm32_get_gpio_bank_offset(bank); |
| 383 | |
| 384 | fdt_for_each_subnode(node, fdt, pinctrl_node) { |
| 385 | const fdt32_t *cuint; |
Yann Gautier | 47516aa | 2022-11-21 11:45:04 +0100 | [diff] [blame] | 386 | int pin_count = 0; |
Fabien Dessenne | 41ab652 | 2021-09-21 11:32:30 +0200 | [diff] [blame] | 387 | int len; |
| 388 | int i; |
Etienne Carriere | d81dadf | 2020-04-25 11:14:45 +0200 | [diff] [blame] | 389 | |
| 390 | if (fdt_getprop(fdt, node, "gpio-controller", NULL) == NULL) { |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | cuint = fdt_getprop(fdt, node, "reg", NULL); |
| 395 | if (cuint == NULL) { |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | if (fdt32_to_cpu(*cuint) != bank_offset) { |
| 400 | continue; |
| 401 | } |
| 402 | |
| 403 | if (fdt_get_status(node) == DT_DISABLED) { |
| 404 | return 0; |
| 405 | } |
| 406 | |
Fabien Dessenne | 41ab652 | 2021-09-21 11:32:30 +0200 | [diff] [blame] | 407 | /* Parse gpio-ranges with its 4 parameters */ |
| 408 | cuint = fdt_getprop(fdt, node, "gpio-ranges", &len); |
| 409 | len /= sizeof(*cuint); |
| 410 | if ((len % 4) != 0) { |
| 411 | return -FDT_ERR_BADVALUE; |
| 412 | } |
| 413 | |
| 414 | /* Get the last defined gpio line (offset + nb of pins) */ |
Yann Gautier | 47516aa | 2022-11-21 11:45:04 +0100 | [diff] [blame] | 415 | for (i = 0; i < len; i += 4) { |
| 416 | pin_count = MAX(pin_count, (int)(fdt32_to_cpu(cuint[i + 1]) + |
| 417 | fdt32_to_cpu(cuint[i + 3]))); |
Etienne Carriere | d81dadf | 2020-04-25 11:14:45 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Fabien Dessenne | 41ab652 | 2021-09-21 11:32:30 +0200 | [diff] [blame] | 420 | return pin_count; |
Etienne Carriere | d81dadf | 2020-04-25 11:14:45 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return 0; |
| 424 | } |