blob: 4dc990896330af9883b72bd96ee17ea00fdeb154 [file] [log] [blame]
Yann Gautier9aea69e2018-07-24 17:13:36 +02001/*
Yann Gautier1c959332021-03-10 14:07:34 +01002 * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
Yann Gautier9aea69e2018-07-24 17:13:36 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Yann Gautier038bff22019-01-17 19:17:47 +01008#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
Yann Gautier9aea69e2018-07-24 17:13:36 +020010#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
Yann Gautier9aea69e2018-07-24 17:13:36 +020012#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013
14#include <common/debug.h>
Andre Przywaracc99f3f2020-03-26 12:51:21 +000015#include <common/fdt_wrappers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <drivers/st/stm32_gpio.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <drivers/st/stm32mp1_ddr.h>
18#include <drivers/st/stm32mp1_ram.h>
19
Yann Gautieree8f5422019-02-14 11:13:25 +010020#include <stm32mp_dt.h>
21
Yann Gautier05773eb2020-08-24 11:51:50 +020022static void *fdt;
Yann Gautier9aea69e2018-07-24 17:13:36 +020023
24/*******************************************************************************
25 * This function checks device tree file with its header.
Yann Gautier038bff22019-01-17 19:17:47 +010026 * Returns 0 on success and a negative FDT error code on failure.
Yann Gautier9aea69e2018-07-24 17:13:36 +020027 ******************************************************************************/
Yann Gautier05773eb2020-08-24 11:51:50 +020028int dt_open_and_check(uintptr_t dt_addr)
Yann Gautier9aea69e2018-07-24 17:13:36 +020029{
Yann Gautier05773eb2020-08-24 11:51:50 +020030 int ret;
Yann Gautier9aea69e2018-07-24 17:13:36 +020031
Yann Gautier05773eb2020-08-24 11:51:50 +020032 ret = fdt_check_header((void *)dt_addr);
Yann Gautier9aea69e2018-07-24 17:13:36 +020033 if (ret == 0) {
Yann Gautier05773eb2020-08-24 11:51:50 +020034 fdt = (void *)dt_addr;
Yann Gautier9aea69e2018-07-24 17:13:36 +020035 }
36
37 return ret;
38}
39
40/*******************************************************************************
41 * This function gets the address of the DT.
42 * If DT is OK, fdt_addr is filled with DT address.
43 * Returns 1 if success, 0 otherwise.
44 ******************************************************************************/
45int fdt_get_address(void **fdt_addr)
46{
Yann Gautier05773eb2020-08-24 11:51:50 +020047 if (fdt == NULL) {
48 return 0;
Yann Gautier9aea69e2018-07-24 17:13:36 +020049 }
50
Yann Gautier05773eb2020-08-24 11:51:50 +020051 *fdt_addr = fdt;
52
53 return 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +020054}
55
56/*******************************************************************************
57 * This function check the presence of a node (generic use of fdt library).
Yann Gautier038bff22019-01-17 19:17:47 +010058 * Returns true if present, else return false.
Yann Gautier9aea69e2018-07-24 17:13:36 +020059 ******************************************************************************/
60bool fdt_check_node(int node)
61{
62 int len;
63 const char *cchar;
64
65 cchar = fdt_get_name(fdt, node, &len);
66
67 return (cchar != NULL) && (len >= 0);
68}
69
70/*******************************************************************************
Yann Gautier038bff22019-01-17 19:17:47 +010071 * This function return global node status (generic use of fdt library).
Yann Gautier9aea69e2018-07-24 17:13:36 +020072 ******************************************************************************/
Yann Gautieree8f5422019-02-14 11:13:25 +010073uint8_t fdt_get_status(int node)
Yann Gautier9aea69e2018-07-24 17:13:36 +020074{
Yann Gautieree8f5422019-02-14 11:13:25 +010075 uint8_t status = DT_DISABLED;
Yann Gautier9aea69e2018-07-24 17:13:36 +020076 const char *cchar;
77
Yann Gautier1c959332021-03-10 14:07:34 +010078 cchar = fdt_getprop(fdt, node, "status", NULL);
Yann Gautier038bff22019-01-17 19:17:47 +010079 if ((cchar == NULL) ||
Yann Gautier1c959332021-03-10 14:07:34 +010080 (strncmp(cchar, "okay", strlen("okay")) == 0)) {
Yann Gautier038bff22019-01-17 19:17:47 +010081 status |= DT_NON_SECURE;
Yann Gautier9aea69e2018-07-24 17:13:36 +020082 }
83
Yann Gautier1c959332021-03-10 14:07:34 +010084 cchar = fdt_getprop(fdt, node, "secure-status", NULL);
Yann Gautier9aea69e2018-07-24 17:13:36 +020085 if (cchar == NULL) {
Yann Gautier038bff22019-01-17 19:17:47 +010086 if (status == DT_NON_SECURE) {
87 status |= DT_SECURE;
88 }
Yann Gautier1c959332021-03-10 14:07:34 +010089 } else if (strncmp(cchar, "okay", strlen("okay")) == 0) {
Yann Gautier038bff22019-01-17 19:17:47 +010090 status |= DT_SECURE;
Yann Gautier9aea69e2018-07-24 17:13:36 +020091 }
92
Yann Gautier038bff22019-01-17 19:17:47 +010093 return status;
Yann Gautier9aea69e2018-07-24 17:13:36 +020094}
95
Yann Gautier2260b842020-03-11 17:17:51 +010096#if ENABLE_ASSERTIONS
Yann Gautier9aea69e2018-07-24 17:13:36 +020097/*******************************************************************************
Lionel Debieveb0899eb2019-09-24 17:41:11 +020098 * 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 ******************************************************************************/
Yann Gautier2260b842020-03-11 17:17:51 +0100105static int fdt_get_node_parent_address_cells(int node)
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200106{
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}
Yann Gautier2260b842020-03-11 17:17:51 +0100116#endif
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200117
118/*******************************************************************************
Yann Gautier69035a82018-07-05 16:48:16 +0200119 * 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.
Yann Gautier038bff22019-01-17 19:17:47 +0100121 * Returns 0 on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200122 ******************************************************************************/
123int dt_set_stdout_pinctrl(void)
124{
125 int node;
126
Andre Przywaraeec91922020-04-09 11:27:21 +0100127 node = fdt_get_stdout_node_offset(fdt);
Yann Gautier69035a82018-07-05 16:48:16 +0200128 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 ******************************************************************************/
138void dt_fill_device_info(struct dt_node_info *info, int node)
139{
140 const fdt32_t *cuint;
141
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200142 assert(fdt_get_node_parent_address_cells(node) == 1);
143
Yann Gautier69035a82018-07-05 16:48:16 +0200144 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
Yann Gautier038bff22019-01-17 19:17:47 +0100167 info->status = fdt_get_status(node);
Yann Gautier69035a82018-07-05 16:48:16 +0200168}
169
170/*******************************************************************************
171 * This function retrieve the generic information from DT.
Yann Gautier038bff22019-01-17 19:17:47 +0100172 * Returns node on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200173 ******************************************************************************/
174int 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.
Yann Gautier038bff22019-01-17 19:17:47 +0100190 * Returns node on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200191 ******************************************************************************/
192int dt_get_stdout_uart_info(struct dt_node_info *info)
193{
194 int node;
195
Andre Przywaraeec91922020-04-09 11:27:21 +0100196 node = fdt_get_stdout_node_offset(fdt);
Yann Gautier69035a82018-07-05 16:48:16 +0200197 if (node < 0) {
198 return -FDT_ERR_NOTFOUND;
199 }
200
201 dt_fill_device_info(info, node);
202
203 return node;
204}
205
206/*******************************************************************************
Yann Gautier4e267842021-09-29 11:31:09 +0200207 * This function returns the node offset matching compatible string in the DT,
208 * and also matching the reg property with the given address.
209 * Returns value on success, and error value on failure.
210 ******************************************************************************/
211int dt_match_instance_by_compatible(const char *compatible, uintptr_t address)
212{
213 int node;
214
215 fdt_for_each_compatible_node(fdt, node, compatible) {
216 const fdt32_t *cuint;
217
218 assert(fdt_get_node_parent_address_cells(node) == 1);
219
220 cuint = fdt_getprop(fdt, node, "reg", NULL);
221 if (cuint == NULL) {
222 continue;
223 }
224
225 if ((uintptr_t)fdt32_to_cpu(*cuint) == address) {
226 return node;
227 }
228 }
229
230 return -FDT_ERR_NOTFOUND;
231}
232
233/*******************************************************************************
Yann Gautiercaf575b2018-07-24 17:18:19 +0200234 * This function gets DDR size information from the DT.
Yann Gautier038bff22019-01-17 19:17:47 +0100235 * Returns value in bytes on success, and 0 on failure.
Yann Gautiercaf575b2018-07-24 17:18:19 +0200236 ******************************************************************************/
237uint32_t dt_get_ddr_size(void)
238{
Lionel Debieve003972c2020-09-24 16:01:12 +0200239 static uint32_t size;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200240 int node;
241
Lionel Debieve003972c2020-09-24 16:01:12 +0200242 if (size != 0U) {
243 return size;
244 }
245
Yann Gautiercaf575b2018-07-24 17:18:19 +0200246 node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
247 if (node < 0) {
248 INFO("%s: Cannot read DDR node in DT\n", __func__);
Yann Gautier038bff22019-01-17 19:17:47 +0100249 return 0;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200250 }
251
Lionel Debieve003972c2020-09-24 16:01:12 +0200252 size = fdt_read_uint32_default(fdt, node, "st,mem-size", 0U);
253
254 flush_dcache_range((uintptr_t)&size, sizeof(uint32_t));
255
256 return size;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200257}
258
259/*******************************************************************************
Yann Gautier3edc7c32019-05-20 19:17:08 +0200260 * This function gets PWR VDD regulator voltage information from the DT.
261 * Returns value in microvolts on success, and 0 on failure.
262 ******************************************************************************/
263uint32_t dt_get_pwr_vdd_voltage(void)
264{
265 int node, pwr_regulators_node;
266 const fdt32_t *cuint;
267
268 node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT);
269 if (node < 0) {
270 INFO("%s: Cannot read PWR node in DT\n", __func__);
271 return 0;
272 }
273
274 pwr_regulators_node = fdt_subnode_offset(fdt, node, "pwr-regulators");
Yann Gautierdf473922020-03-18 14:35:27 +0100275 if (pwr_regulators_node < 0) {
Yann Gautier3edc7c32019-05-20 19:17:08 +0200276 INFO("%s: Cannot read pwr-regulators node in DT\n", __func__);
277 return 0;
278 }
279
280 cuint = fdt_getprop(fdt, pwr_regulators_node, "vdd-supply", NULL);
281 if (cuint == NULL) {
282 return 0;
283 }
284
285 node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
286 if (node < 0) {
287 return 0;
288 }
289
290 cuint = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL);
291 if (cuint == NULL) {
292 return 0;
293 }
294
295 return fdt32_to_cpu(*cuint);
296}
297
298/*******************************************************************************
Yann Gautier69035a82018-07-05 16:48:16 +0200299 * This function retrieves board model from DT
300 * Returns string taken from model node, NULL otherwise
301 ******************************************************************************/
302const char *dt_get_board_model(void)
303{
304 int node = fdt_path_offset(fdt, "/");
305
306 if (node < 0) {
307 return NULL;
308 }
309
310 return (const char *)fdt_getprop(fdt, node, "model", NULL);
311}
Etienne Carriered81dadf2020-04-25 11:14:45 +0200312
313/*******************************************************************************
314 * This function gets the pin count for a GPIO bank based from the FDT.
315 * It also checks node consistency.
316 ******************************************************************************/
317int fdt_get_gpio_bank_pin_count(unsigned int bank)
318{
319 int pinctrl_node;
320 int node;
321 uint32_t bank_offset;
322
323 pinctrl_node = stm32_get_gpio_bank_pinctrl_node(fdt, bank);
324 if (pinctrl_node < 0) {
325 return -FDT_ERR_NOTFOUND;
326 }
327
328 bank_offset = stm32_get_gpio_bank_offset(bank);
329
330 fdt_for_each_subnode(node, fdt, pinctrl_node) {
331 const fdt32_t *cuint;
332
333 if (fdt_getprop(fdt, node, "gpio-controller", NULL) == NULL) {
334 continue;
335 }
336
337 cuint = fdt_getprop(fdt, node, "reg", NULL);
338 if (cuint == NULL) {
339 continue;
340 }
341
342 if (fdt32_to_cpu(*cuint) != bank_offset) {
343 continue;
344 }
345
346 if (fdt_get_status(node) == DT_DISABLED) {
347 return 0;
348 }
349
350 cuint = fdt_getprop(fdt, node, "ngpios", NULL);
351 if (cuint == NULL) {
352 return -FDT_ERR_NOTFOUND;
353 }
354
355 return (int)fdt32_to_cpu(*cuint);
356 }
357
358 return 0;
359}