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