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