xref: /rk3399_ARM-atf/plat/st/common/stm32mp_dt.c (revision cd9c92cd16b1beb6199ae7a7c01effb0d49ab448)
1c9d75b3cSYann Gautier /*
2*cd9c92cdSNicolas Le Bayon  * Copyright (c) 2017-2024, 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);
820ebaf222SYann Gautier 	if (((cchar == NULL) && (status == DT_NON_SECURE)) ||
830ebaf222SYann 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  ******************************************************************************/
2312a4abe0bSSebastien PASDELOUP size_t dt_get_ddr_size(void)
232c9d75b3cSYann Gautier {
2332a4abe0bSSebastien PASDELOUP 	static size_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__);
2432a4abe0bSSebastien PASDELOUP 		return 0U;
244c9d75b3cSYann Gautier 	}
245c9d75b3cSYann Gautier 
246*cd9c92cdSNicolas Le Bayon #ifdef __aarch64__
247*cd9c92cdSNicolas Le Bayon 	size = (size_t)fdt_read_uint64_default(fdt, node, "st,mem-size", 0ULL);
248*cd9c92cdSNicolas Le Bayon #else /* __aarch64__ */
2492a4abe0bSSebastien PASDELOUP 	size = (size_t)fdt_read_uint32_default(fdt, node, "st,mem-size", 0U);
250*cd9c92cdSNicolas Le Bayon #endif /* __aarch64__ */
25191ffc1deSLionel Debieve 
2522a4abe0bSSebastien PASDELOUP 	flush_dcache_range((uintptr_t)&size, sizeof(size_t));
25391ffc1deSLionel Debieve 
25491ffc1deSLionel Debieve 	return size;
255c9d75b3cSYann Gautier }
256c9d75b3cSYann Gautier 
257c9d75b3cSYann Gautier /*******************************************************************************
258f33b2433SYann Gautier  * This function gets PWR VDD regulator voltage information from the DT.
259f33b2433SYann Gautier  * Returns value in microvolts on success, and 0 on failure.
260f33b2433SYann Gautier  ******************************************************************************/
261f33b2433SYann Gautier uint32_t dt_get_pwr_vdd_voltage(void)
262f33b2433SYann Gautier {
263c39c658eSYann Gautier 	struct rdev *regul = dt_get_vdd_regulator();
264c39c658eSYann Gautier 	uint16_t min;
265f33b2433SYann Gautier 
266c39c658eSYann Gautier 	if (regul == NULL) {
267c39c658eSYann Gautier 		return 0;
268c39c658eSYann Gautier 	}
269c39c658eSYann Gautier 
270c39c658eSYann Gautier 	regulator_get_range(regul, &min, NULL);
271c39c658eSYann Gautier 
272c39c658eSYann Gautier 	return (uint32_t)min * 1000U;
273c39c658eSYann Gautier }
274c39c658eSYann Gautier 
275c39c658eSYann Gautier /*******************************************************************************
276c39c658eSYann Gautier  * This function retrieves VDD supply regulator from DT.
277c39c658eSYann Gautier  * Returns an rdev taken from supply node, NULL otherwise.
278c39c658eSYann Gautier  ******************************************************************************/
279c39c658eSYann Gautier struct rdev *dt_get_vdd_regulator(void)
280c39c658eSYann Gautier {
281c39c658eSYann Gautier 	int node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT);
282c39c658eSYann Gautier 
283f33b2433SYann Gautier 	if (node < 0) {
284c39c658eSYann Gautier 		return NULL;
285f33b2433SYann Gautier 	}
286f33b2433SYann Gautier 
287c39c658eSYann Gautier 	return regulator_get_by_supply_name(fdt, node, "vdd");
288f33b2433SYann Gautier }
289f33b2433SYann Gautier 
290c39c658eSYann Gautier /*******************************************************************************
291c39c658eSYann Gautier  * This function retrieves CPU supply regulator from DT.
292c39c658eSYann Gautier  * Returns an rdev taken from supply node, NULL otherwise.
293c39c658eSYann Gautier  ******************************************************************************/
294c39c658eSYann Gautier struct rdev *dt_get_cpu_regulator(void)
295c39c658eSYann Gautier {
296c39c658eSYann Gautier 	int node = fdt_path_offset(fdt, "/cpus/cpu@0");
297f33b2433SYann Gautier 
298f33b2433SYann Gautier 	if (node < 0) {
299c39c658eSYann Gautier 		return NULL;
300f33b2433SYann Gautier 	}
301f33b2433SYann Gautier 
302c39c658eSYann Gautier 	return regulator_get_by_supply_name(fdt, node, "cpu");
303f33b2433SYann Gautier }
304f33b2433SYann Gautier 
305f33b2433SYann Gautier /*******************************************************************************
306c9d75b3cSYann Gautier  * This function retrieves board model from DT
307c9d75b3cSYann Gautier  * Returns string taken from model node, NULL otherwise
308c9d75b3cSYann Gautier  ******************************************************************************/
309c9d75b3cSYann Gautier const char *dt_get_board_model(void)
310c9d75b3cSYann Gautier {
311c9d75b3cSYann Gautier 	int node = fdt_path_offset(fdt, "/");
312c9d75b3cSYann Gautier 
313c9d75b3cSYann Gautier 	if (node < 0) {
314c9d75b3cSYann Gautier 		return NULL;
315c9d75b3cSYann Gautier 	}
316c9d75b3cSYann Gautier 
317c9d75b3cSYann Gautier 	return (const char *)fdt_getprop(fdt, node, "model", NULL);
318c9d75b3cSYann Gautier }
319ccc199edSEtienne Carriere 
320ccc199edSEtienne Carriere /*******************************************************************************
321ae3ce8b2SLionel Debieve  * dt_find_otp_name: get OTP ID and length in DT.
322ae3ce8b2SLionel Debieve  * name: sub-node name to look up.
323ae3ce8b2SLionel Debieve  * otp: pointer to read OTP number or NULL.
324ae3ce8b2SLionel Debieve  * otp_len: pointer to read OTP length in bits or NULL.
325ae3ce8b2SLionel Debieve  * return value: 0 if no error, an FDT error value otherwise.
326ae3ce8b2SLionel Debieve  ******************************************************************************/
327ae3ce8b2SLionel Debieve int dt_find_otp_name(const char *name, uint32_t *otp, uint32_t *otp_len)
328ae3ce8b2SLionel Debieve {
329ae3ce8b2SLionel Debieve 	int node;
330c5bf1b09SPatrick Delaunay 	int len;
331ae3ce8b2SLionel Debieve 	const fdt32_t *cuint;
332ae3ce8b2SLionel Debieve 
333ae3ce8b2SLionel Debieve 	if ((name == NULL) || (otp == NULL)) {
334ae3ce8b2SLionel Debieve 		return -FDT_ERR_BADVALUE;
335ae3ce8b2SLionel Debieve 	}
336ae3ce8b2SLionel Debieve 
337c5bf1b09SPatrick Delaunay 	node = fdt_node_offset_by_compatible(fdt, -1, DT_BSEC_COMPAT);
338ae3ce8b2SLionel Debieve 	if (node < 0) {
339ae3ce8b2SLionel Debieve 		return node;
340ae3ce8b2SLionel Debieve 	}
341ae3ce8b2SLionel Debieve 
342c5bf1b09SPatrick Delaunay 	node = fdt_subnode_offset(fdt, node, name);
343ae3ce8b2SLionel Debieve 	if (node < 0) {
344c5bf1b09SPatrick Delaunay 		ERROR("nvmem node %s not found\n", name);
345ae3ce8b2SLionel Debieve 		return node;
346ae3ce8b2SLionel Debieve 	}
347ae3ce8b2SLionel Debieve 
348ae3ce8b2SLionel Debieve 	cuint = fdt_getprop(fdt, node, "reg", &len);
349ae3ce8b2SLionel Debieve 	if ((cuint == NULL) || (len != (2 * (int)sizeof(uint32_t)))) {
350c5bf1b09SPatrick Delaunay 		ERROR("Malformed nvmem node %s: ignored\n", name);
351ae3ce8b2SLionel Debieve 		return -FDT_ERR_BADVALUE;
352ae3ce8b2SLionel Debieve 	}
353ae3ce8b2SLionel Debieve 
35445d2d495SYann Gautier 	if ((fdt32_to_cpu(*cuint) % sizeof(uint32_t)) != 0U) {
355c5bf1b09SPatrick Delaunay 		ERROR("Misaligned nvmem %s element: ignored\n", name);
356ae3ce8b2SLionel Debieve 		return -FDT_ERR_BADVALUE;
357ae3ce8b2SLionel Debieve 	}
358ae3ce8b2SLionel Debieve 
359ae3ce8b2SLionel Debieve 	if (otp != NULL) {
360ae3ce8b2SLionel Debieve 		*otp = fdt32_to_cpu(*cuint) / sizeof(uint32_t);
361ae3ce8b2SLionel Debieve 	}
362ae3ce8b2SLionel Debieve 
363ae3ce8b2SLionel Debieve 	if (otp_len != NULL) {
364ae3ce8b2SLionel Debieve 		cuint++;
365ae3ce8b2SLionel Debieve 		*otp_len = fdt32_to_cpu(*cuint) * CHAR_BIT;
366ae3ce8b2SLionel Debieve 	}
367ae3ce8b2SLionel Debieve 
368ae3ce8b2SLionel Debieve 	return 0;
369ae3ce8b2SLionel Debieve }
370ae3ce8b2SLionel Debieve 
371ae3ce8b2SLionel Debieve /*******************************************************************************
372ccc199edSEtienne Carriere  * This function gets the pin count for a GPIO bank based from the FDT.
373ccc199edSEtienne Carriere  * It also checks node consistency.
374ccc199edSEtienne Carriere  ******************************************************************************/
375ccc199edSEtienne Carriere int fdt_get_gpio_bank_pin_count(unsigned int bank)
376ccc199edSEtienne Carriere {
377ccc199edSEtienne Carriere 	int pinctrl_node;
378ccc199edSEtienne Carriere 	int node;
379ccc199edSEtienne Carriere 	uint32_t bank_offset;
380ccc199edSEtienne Carriere 
381ccc199edSEtienne Carriere 	pinctrl_node = stm32_get_gpio_bank_pinctrl_node(fdt, bank);
382ccc199edSEtienne Carriere 	if (pinctrl_node < 0) {
383ccc199edSEtienne Carriere 		return -FDT_ERR_NOTFOUND;
384ccc199edSEtienne Carriere 	}
385ccc199edSEtienne Carriere 
386ccc199edSEtienne Carriere 	bank_offset = stm32_get_gpio_bank_offset(bank);
387ccc199edSEtienne Carriere 
388ccc199edSEtienne Carriere 	fdt_for_each_subnode(node, fdt, pinctrl_node) {
389ccc199edSEtienne Carriere 		const fdt32_t *cuint;
390e7d75448SYann Gautier 		int pin_count = 0;
391d0f2cf3bSFabien Dessenne 		int len;
392d0f2cf3bSFabien Dessenne 		int i;
393ccc199edSEtienne Carriere 
394ccc199edSEtienne Carriere 		if (fdt_getprop(fdt, node, "gpio-controller", NULL) == NULL) {
395ccc199edSEtienne Carriere 			continue;
396ccc199edSEtienne Carriere 		}
397ccc199edSEtienne Carriere 
398ccc199edSEtienne Carriere 		cuint = fdt_getprop(fdt, node, "reg", NULL);
399ccc199edSEtienne Carriere 		if (cuint == NULL) {
400ccc199edSEtienne Carriere 			continue;
401ccc199edSEtienne Carriere 		}
402ccc199edSEtienne Carriere 
403ccc199edSEtienne Carriere 		if (fdt32_to_cpu(*cuint) != bank_offset) {
404ccc199edSEtienne Carriere 			continue;
405ccc199edSEtienne Carriere 		}
406ccc199edSEtienne Carriere 
407ccc199edSEtienne Carriere 		if (fdt_get_status(node) == DT_DISABLED) {
408ccc199edSEtienne Carriere 			return 0;
409ccc199edSEtienne Carriere 		}
410ccc199edSEtienne Carriere 
411d0f2cf3bSFabien Dessenne 		/* Parse gpio-ranges with its 4 parameters */
412d0f2cf3bSFabien Dessenne 		cuint = fdt_getprop(fdt, node, "gpio-ranges", &len);
413d0f2cf3bSFabien Dessenne 		len /= sizeof(*cuint);
414d0f2cf3bSFabien Dessenne 		if ((len % 4) != 0) {
415d0f2cf3bSFabien Dessenne 			return -FDT_ERR_BADVALUE;
416ccc199edSEtienne Carriere 		}
417ccc199edSEtienne Carriere 
418d0f2cf3bSFabien Dessenne 		/* Get the last defined gpio line (offset + nb of pins) */
419e7d75448SYann Gautier 		for (i = 0; i < len; i += 4) {
420e7d75448SYann Gautier 			pin_count = MAX(pin_count, (int)(fdt32_to_cpu(cuint[i + 1]) +
421e7d75448SYann Gautier 							 fdt32_to_cpu(cuint[i + 3])));
422d0f2cf3bSFabien Dessenne 		}
423d0f2cf3bSFabien Dessenne 
424d0f2cf3bSFabien Dessenne 		return pin_count;
425ccc199edSEtienne Carriere 	}
426ccc199edSEtienne Carriere 
427ccc199edSEtienne Carriere 	return 0;
428ccc199edSEtienne Carriere }
429