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