xref: /rk3399_ARM-atf/plat/st/common/stm32mp_dt.c (revision 3dbbbca29e3c42a6f9976878f27e1f1fd75b5c8e)
1 /*
2  * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <errno.h>
9 
10 #include <libfdt.h>
11 
12 #include <platform_def.h>
13 
14 #include <common/debug.h>
15 #include <common/fdt_wrappers.h>
16 #include <drivers/st/stm32_gpio.h>
17 #include <drivers/st/stm32mp1_ddr.h>
18 #include <drivers/st/stm32mp1_ram.h>
19 
20 #include <stm32mp_dt.h>
21 
22 static int fdt_checked;
23 
24 static void *fdt = (void *)(uintptr_t)STM32MP_DTB_BASE;
25 
26 /*******************************************************************************
27  * This function checks device tree file with its header.
28  * Returns 0 on success and a negative FDT error code on failure.
29  ******************************************************************************/
30 int dt_open_and_check(void)
31 {
32 	int ret = fdt_check_header(fdt);
33 
34 	if (ret == 0) {
35 		fdt_checked = 1;
36 	}
37 
38 	return ret;
39 }
40 
41 /*******************************************************************************
42  * This function gets the address of the DT.
43  * If DT is OK, fdt_addr is filled with DT address.
44  * Returns 1 if success, 0 otherwise.
45  ******************************************************************************/
46 int fdt_get_address(void **fdt_addr)
47 {
48 	if (fdt_checked == 1) {
49 		*fdt_addr = fdt;
50 	}
51 
52 	return fdt_checked;
53 }
54 
55 /*******************************************************************************
56  * This function check the presence of a node (generic use of fdt library).
57  * Returns true if present, else return false.
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 /*******************************************************************************
70  * This function return global node status (generic use of fdt library).
71  ******************************************************************************/
72 uint8_t fdt_get_status(int node)
73 {
74 	uint8_t status = DT_DISABLED;
75 	int len;
76 	const char *cchar;
77 
78 	cchar = fdt_getprop(fdt, node, "status", &len);
79 	if ((cchar == NULL) ||
80 	    (strncmp(cchar, "okay", (size_t)len) == 0)) {
81 		status |= DT_NON_SECURE;
82 	}
83 
84 	cchar = fdt_getprop(fdt, node, "secure-status", &len);
85 	if (cchar == NULL) {
86 		if (status == DT_NON_SECURE) {
87 			status |= DT_SECURE;
88 		}
89 	} else if (strncmp(cchar, "okay", (size_t)len) == 0) {
90 		status |= DT_SECURE;
91 	}
92 
93 	return status;
94 }
95 
96 #if ENABLE_ASSERTIONS
97 /*******************************************************************************
98  * This function returns the address cells from the node parent.
99  * Returns:
100  * - #address-cells value if success.
101  * - invalid value if error.
102  * - a default value if undefined #address-cells property as per libfdt
103  *   implementation.
104  ******************************************************************************/
105 static int fdt_get_node_parent_address_cells(int node)
106 {
107 	int parent;
108 
109 	parent = fdt_parent_offset(fdt, node);
110 	if (parent < 0) {
111 		return -FDT_ERR_NOTFOUND;
112 	}
113 
114 	return fdt_address_cells(fdt, parent);
115 }
116 #endif
117 
118 /*******************************************************************************
119  * This function gets the stdout pin configuration information from the DT.
120  * And then calls the sub-function to treat it and set GPIO registers.
121  * Returns 0 on success and a negative FDT error code on failure.
122  ******************************************************************************/
123 int dt_set_stdout_pinctrl(void)
124 {
125 	int node;
126 
127 	node = fdt_get_stdout_node_offset(fdt);
128 	if (node < 0) {
129 		return -FDT_ERR_NOTFOUND;
130 	}
131 
132 	return dt_set_pinctrl_config(node);
133 }
134 
135 /*******************************************************************************
136  * This function fills the generic information from a given node.
137  ******************************************************************************/
138 void dt_fill_device_info(struct dt_node_info *info, int node)
139 {
140 	const fdt32_t *cuint;
141 
142 	assert(fdt_get_node_parent_address_cells(node) == 1);
143 
144 	cuint = fdt_getprop(fdt, node, "reg", NULL);
145 	if (cuint != NULL) {
146 		info->base = fdt32_to_cpu(*cuint);
147 	} else {
148 		info->base = 0;
149 	}
150 
151 	cuint = fdt_getprop(fdt, node, "clocks", NULL);
152 	if (cuint != NULL) {
153 		cuint++;
154 		info->clock = (int)fdt32_to_cpu(*cuint);
155 	} else {
156 		info->clock = -1;
157 	}
158 
159 	cuint = fdt_getprop(fdt, node, "resets", NULL);
160 	if (cuint != NULL) {
161 		cuint++;
162 		info->reset = (int)fdt32_to_cpu(*cuint);
163 	} else {
164 		info->reset = -1;
165 	}
166 
167 	info->status = fdt_get_status(node);
168 }
169 
170 /*******************************************************************************
171  * This function retrieve the generic information from DT.
172  * Returns node on success and a negative FDT error code on failure.
173  ******************************************************************************/
174 int dt_get_node(struct dt_node_info *info, int offset, const char *compat)
175 {
176 	int node;
177 
178 	node = fdt_node_offset_by_compatible(fdt, offset, compat);
179 	if (node < 0) {
180 		return -FDT_ERR_NOTFOUND;
181 	}
182 
183 	dt_fill_device_info(info, node);
184 
185 	return node;
186 }
187 
188 /*******************************************************************************
189  * This function gets the UART instance info of stdout from the DT.
190  * Returns node on success and a negative FDT error code on failure.
191  ******************************************************************************/
192 int dt_get_stdout_uart_info(struct dt_node_info *info)
193 {
194 	int node;
195 
196 	node = fdt_get_stdout_node_offset(fdt);
197 	if (node < 0) {
198 		return -FDT_ERR_NOTFOUND;
199 	}
200 
201 	dt_fill_device_info(info, node);
202 
203 	return node;
204 }
205 
206 /*******************************************************************************
207  * This function gets DDR size information from the DT.
208  * Returns value in bytes on success, and 0 on failure.
209  ******************************************************************************/
210 uint32_t dt_get_ddr_size(void)
211 {
212 	int node;
213 
214 	node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
215 	if (node < 0) {
216 		INFO("%s: Cannot read DDR node in DT\n", __func__);
217 		return 0;
218 	}
219 
220 	return fdt_read_uint32_default(fdt, node, "st,mem-size", 0);
221 }
222 
223 /*******************************************************************************
224  * This function gets PWR VDD regulator voltage information from the DT.
225  * Returns value in microvolts on success, and 0 on failure.
226  ******************************************************************************/
227 uint32_t dt_get_pwr_vdd_voltage(void)
228 {
229 	int node, pwr_regulators_node;
230 	const fdt32_t *cuint;
231 
232 	node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT);
233 	if (node < 0) {
234 		INFO("%s: Cannot read PWR node in DT\n", __func__);
235 		return 0;
236 	}
237 
238 	pwr_regulators_node = fdt_subnode_offset(fdt, node, "pwr-regulators");
239 	if (pwr_regulators_node < 0) {
240 		INFO("%s: Cannot read pwr-regulators node in DT\n", __func__);
241 		return 0;
242 	}
243 
244 	cuint = fdt_getprop(fdt, pwr_regulators_node, "vdd-supply", NULL);
245 	if (cuint == NULL) {
246 		return 0;
247 	}
248 
249 	node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
250 	if (node < 0) {
251 		return 0;
252 	}
253 
254 	cuint = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL);
255 	if (cuint == NULL) {
256 		return 0;
257 	}
258 
259 	return fdt32_to_cpu(*cuint);
260 }
261 
262 /*******************************************************************************
263  * This function retrieves board model from DT
264  * Returns string taken from model node, NULL otherwise
265  ******************************************************************************/
266 const char *dt_get_board_model(void)
267 {
268 	int node = fdt_path_offset(fdt, "/");
269 
270 	if (node < 0) {
271 		return NULL;
272 	}
273 
274 	return (const char *)fdt_getprop(fdt, node, "model", NULL);
275 }
276 
277 /*******************************************************************************
278  * This function gets the pin count for a GPIO bank based from the FDT.
279  * It also checks node consistency.
280  ******************************************************************************/
281 int fdt_get_gpio_bank_pin_count(unsigned int bank)
282 {
283 	int pinctrl_node;
284 	int node;
285 	uint32_t bank_offset;
286 
287 	pinctrl_node = stm32_get_gpio_bank_pinctrl_node(fdt, bank);
288 	if (pinctrl_node < 0) {
289 		return -FDT_ERR_NOTFOUND;
290 	}
291 
292 	bank_offset = stm32_get_gpio_bank_offset(bank);
293 
294 	fdt_for_each_subnode(node, fdt, pinctrl_node) {
295 		const fdt32_t *cuint;
296 
297 		if (fdt_getprop(fdt, node, "gpio-controller", NULL) == NULL) {
298 			continue;
299 		}
300 
301 		cuint = fdt_getprop(fdt, node, "reg", NULL);
302 		if (cuint == NULL) {
303 			continue;
304 		}
305 
306 		if (fdt32_to_cpu(*cuint) != bank_offset) {
307 			continue;
308 		}
309 
310 		if (fdt_get_status(node) == DT_DISABLED) {
311 			return 0;
312 		}
313 
314 		cuint = fdt_getprop(fdt, node, "ngpios", NULL);
315 		if (cuint == NULL) {
316 			return -FDT_ERR_NOTFOUND;
317 		}
318 
319 		return (int)fdt32_to_cpu(*cuint);
320 	}
321 
322 	return 0;
323 }
324