blob: 5a5ca565d8b9e5d0c1d37b3a4d18bc901ceb01d0 [file] [log] [blame]
Yann Gautier9aea69e2018-07-24 17:13:36 +02001/*
Lionel Debievebc2d88d2019-11-04 14:31:38 +01002 * Copyright (c) 2017-2022, 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
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <common/debug.h>
Andre Przywaracc99f3f2020-03-26 12:51:21 +000011#include <common/fdt_wrappers.h>
Yann Gautier296e1ab2021-09-17 16:08:12 +020012#include <drivers/st/regulator.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <drivers/st/stm32_gpio.h>
Yann Gautier296e1ab2021-09-17 16:08:12 +020014#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015
Yann Gautier296e1ab2021-09-17 16:08:12 +020016#include <platform_def.h>
Yann Gautieree8f5422019-02-14 11:13:25 +010017#include <stm32mp_dt.h>
18
Yann Gautier05773eb2020-08-24 11:51:50 +020019static void *fdt;
Yann Gautier9aea69e2018-07-24 17:13:36 +020020
21/*******************************************************************************
22 * This function checks device tree file with its header.
Yann Gautier038bff22019-01-17 19:17:47 +010023 * Returns 0 on success and a negative FDT error code on failure.
Yann Gautier9aea69e2018-07-24 17:13:36 +020024 ******************************************************************************/
Yann Gautier05773eb2020-08-24 11:51:50 +020025int dt_open_and_check(uintptr_t dt_addr)
Yann Gautier9aea69e2018-07-24 17:13:36 +020026{
Yann Gautier05773eb2020-08-24 11:51:50 +020027 int ret;
Yann Gautier9aea69e2018-07-24 17:13:36 +020028
Yann Gautier05773eb2020-08-24 11:51:50 +020029 ret = fdt_check_header((void *)dt_addr);
Yann Gautier9aea69e2018-07-24 17:13:36 +020030 if (ret == 0) {
Yann Gautier05773eb2020-08-24 11:51:50 +020031 fdt = (void *)dt_addr;
Yann Gautier9aea69e2018-07-24 17:13:36 +020032 }
33
34 return ret;
35}
36
37/*******************************************************************************
38 * This function gets the address of the DT.
39 * If DT is OK, fdt_addr is filled with DT address.
40 * Returns 1 if success, 0 otherwise.
41 ******************************************************************************/
42int fdt_get_address(void **fdt_addr)
43{
Yann Gautier05773eb2020-08-24 11:51:50 +020044 if (fdt == NULL) {
45 return 0;
Yann Gautier9aea69e2018-07-24 17:13:36 +020046 }
47
Yann Gautier05773eb2020-08-24 11:51:50 +020048 *fdt_addr = fdt;
49
50 return 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +020051}
52
53/*******************************************************************************
54 * This function check the presence of a node (generic use of fdt library).
Yann Gautier038bff22019-01-17 19:17:47 +010055 * Returns true if present, else return false.
Yann Gautier9aea69e2018-07-24 17:13:36 +020056 ******************************************************************************/
57bool fdt_check_node(int node)
58{
59 int len;
60 const char *cchar;
61
62 cchar = fdt_get_name(fdt, node, &len);
63
64 return (cchar != NULL) && (len >= 0);
65}
66
67/*******************************************************************************
Yann Gautier038bff22019-01-17 19:17:47 +010068 * This function return global node status (generic use of fdt library).
Yann Gautier9aea69e2018-07-24 17:13:36 +020069 ******************************************************************************/
Yann Gautieree8f5422019-02-14 11:13:25 +010070uint8_t fdt_get_status(int node)
Yann Gautier9aea69e2018-07-24 17:13:36 +020071{
Yann Gautieree8f5422019-02-14 11:13:25 +010072 uint8_t status = DT_DISABLED;
Yann Gautier9aea69e2018-07-24 17:13:36 +020073 const char *cchar;
74
Yann Gautier1c959332021-03-10 14:07:34 +010075 cchar = fdt_getprop(fdt, node, "status", NULL);
Yann Gautier038bff22019-01-17 19:17:47 +010076 if ((cchar == NULL) ||
Yann Gautier1c959332021-03-10 14:07:34 +010077 (strncmp(cchar, "okay", strlen("okay")) == 0)) {
Yann Gautier038bff22019-01-17 19:17:47 +010078 status |= DT_NON_SECURE;
Yann Gautier9aea69e2018-07-24 17:13:36 +020079 }
80
Yann Gautier1c959332021-03-10 14:07:34 +010081 cchar = fdt_getprop(fdt, node, "secure-status", NULL);
Yann Gautier9aea69e2018-07-24 17:13:36 +020082 if (cchar == NULL) {
Yann Gautier038bff22019-01-17 19:17:47 +010083 if (status == DT_NON_SECURE) {
84 status |= DT_SECURE;
85 }
Yann Gautier1c959332021-03-10 14:07:34 +010086 } else if (strncmp(cchar, "okay", strlen("okay")) == 0) {
Yann Gautier038bff22019-01-17 19:17:47 +010087 status |= DT_SECURE;
Yann Gautier9aea69e2018-07-24 17:13:36 +020088 }
89
Yann Gautier038bff22019-01-17 19:17:47 +010090 return status;
Yann Gautier9aea69e2018-07-24 17:13:36 +020091}
92
Yann Gautier2260b842020-03-11 17:17:51 +010093#if ENABLE_ASSERTIONS
Yann Gautier9aea69e2018-07-24 17:13:36 +020094/*******************************************************************************
Lionel Debieveb0899eb2019-09-24 17:41:11 +020095 * This function returns the address cells from the node parent.
96 * Returns:
97 * - #address-cells value if success.
98 * - invalid value if error.
99 * - a default value if undefined #address-cells property as per libfdt
100 * implementation.
101 ******************************************************************************/
Yann Gautier2260b842020-03-11 17:17:51 +0100102static int fdt_get_node_parent_address_cells(int node)
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200103{
104 int parent;
105
106 parent = fdt_parent_offset(fdt, node);
107 if (parent < 0) {
108 return -FDT_ERR_NOTFOUND;
109 }
110
111 return fdt_address_cells(fdt, parent);
112}
Yann Gautier2260b842020-03-11 17:17:51 +0100113#endif
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200114
115/*******************************************************************************
Yann Gautier69035a82018-07-05 16:48:16 +0200116 * This function gets the stdout pin configuration information from the DT.
117 * And then calls the sub-function to treat it and set GPIO registers.
Yann Gautier038bff22019-01-17 19:17:47 +0100118 * Returns 0 on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200119 ******************************************************************************/
120int dt_set_stdout_pinctrl(void)
121{
122 int node;
123
Andre Przywaraeec91922020-04-09 11:27:21 +0100124 node = fdt_get_stdout_node_offset(fdt);
Yann Gautier69035a82018-07-05 16:48:16 +0200125 if (node < 0) {
126 return -FDT_ERR_NOTFOUND;
127 }
128
129 return dt_set_pinctrl_config(node);
130}
131
132/*******************************************************************************
133 * This function fills the generic information from a given node.
134 ******************************************************************************/
135void dt_fill_device_info(struct dt_node_info *info, int node)
136{
137 const fdt32_t *cuint;
138
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200139 assert(fdt_get_node_parent_address_cells(node) == 1);
140
Yann Gautier69035a82018-07-05 16:48:16 +0200141 cuint = fdt_getprop(fdt, node, "reg", NULL);
142 if (cuint != NULL) {
143 info->base = fdt32_to_cpu(*cuint);
144 } else {
145 info->base = 0;
146 }
147
148 cuint = fdt_getprop(fdt, node, "clocks", NULL);
149 if (cuint != NULL) {
150 cuint++;
151 info->clock = (int)fdt32_to_cpu(*cuint);
152 } else {
153 info->clock = -1;
154 }
155
156 cuint = fdt_getprop(fdt, node, "resets", NULL);
157 if (cuint != NULL) {
158 cuint++;
159 info->reset = (int)fdt32_to_cpu(*cuint);
160 } else {
161 info->reset = -1;
162 }
163
Yann Gautier038bff22019-01-17 19:17:47 +0100164 info->status = fdt_get_status(node);
Yann Gautier69035a82018-07-05 16:48:16 +0200165}
166
167/*******************************************************************************
168 * This function retrieve the generic information from DT.
Yann Gautier038bff22019-01-17 19:17:47 +0100169 * Returns node on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200170 ******************************************************************************/
171int dt_get_node(struct dt_node_info *info, int offset, const char *compat)
172{
173 int node;
174
175 node = fdt_node_offset_by_compatible(fdt, offset, compat);
176 if (node < 0) {
177 return -FDT_ERR_NOTFOUND;
178 }
179
180 dt_fill_device_info(info, node);
181
182 return node;
183}
184
185/*******************************************************************************
186 * This function gets the UART instance info of stdout from the DT.
Yann Gautier038bff22019-01-17 19:17:47 +0100187 * Returns node on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200188 ******************************************************************************/
189int dt_get_stdout_uart_info(struct dt_node_info *info)
190{
191 int node;
192
Andre Przywaraeec91922020-04-09 11:27:21 +0100193 node = fdt_get_stdout_node_offset(fdt);
Yann Gautier69035a82018-07-05 16:48:16 +0200194 if (node < 0) {
195 return -FDT_ERR_NOTFOUND;
196 }
197
198 dt_fill_device_info(info, node);
199
200 return node;
201}
202
203/*******************************************************************************
Yann Gautier4e267842021-09-29 11:31:09 +0200204 * This function returns the node offset matching compatible string in the DT,
205 * and also matching the reg property with the given address.
206 * Returns value on success, and error value on failure.
207 ******************************************************************************/
208int dt_match_instance_by_compatible(const char *compatible, uintptr_t address)
209{
210 int node;
211
212 fdt_for_each_compatible_node(fdt, node, compatible) {
213 const fdt32_t *cuint;
214
215 assert(fdt_get_node_parent_address_cells(node) == 1);
216
217 cuint = fdt_getprop(fdt, node, "reg", NULL);
218 if (cuint == NULL) {
219 continue;
220 }
221
222 if ((uintptr_t)fdt32_to_cpu(*cuint) == address) {
223 return node;
224 }
225 }
226
227 return -FDT_ERR_NOTFOUND;
228}
229
230/*******************************************************************************
Yann Gautiercaf575b2018-07-24 17:18:19 +0200231 * This function gets DDR size information from the DT.
Yann Gautier038bff22019-01-17 19:17:47 +0100232 * Returns value in bytes on success, and 0 on failure.
Yann Gautiercaf575b2018-07-24 17:18:19 +0200233 ******************************************************************************/
234uint32_t dt_get_ddr_size(void)
235{
Lionel Debieve003972c2020-09-24 16:01:12 +0200236 static uint32_t size;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200237 int node;
238
Lionel Debieve003972c2020-09-24 16:01:12 +0200239 if (size != 0U) {
240 return size;
241 }
242
Yann Gautiercaf575b2018-07-24 17:18:19 +0200243 node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
244 if (node < 0) {
245 INFO("%s: Cannot read DDR node in DT\n", __func__);
Yann Gautier038bff22019-01-17 19:17:47 +0100246 return 0;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200247 }
248
Lionel Debieve003972c2020-09-24 16:01:12 +0200249 size = fdt_read_uint32_default(fdt, node, "st,mem-size", 0U);
250
251 flush_dcache_range((uintptr_t)&size, sizeof(uint32_t));
252
253 return size;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200254}
255
256/*******************************************************************************
Yann Gautier3edc7c32019-05-20 19:17:08 +0200257 * This function gets PWR VDD regulator voltage information from the DT.
258 * Returns value in microvolts on success, and 0 on failure.
259 ******************************************************************************/
260uint32_t dt_get_pwr_vdd_voltage(void)
261{
Yann Gautier296e1ab2021-09-17 16:08:12 +0200262 struct rdev *regul = dt_get_vdd_regulator();
263 uint16_t min;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200264
Yann Gautier296e1ab2021-09-17 16:08:12 +0200265 if (regul == NULL) {
Yann Gautier3edc7c32019-05-20 19:17:08 +0200266 return 0;
267 }
268
Yann Gautier296e1ab2021-09-17 16:08:12 +0200269 regulator_get_range(regul, &min, NULL);
Yann Gautier3edc7c32019-05-20 19:17:08 +0200270
Yann Gautier296e1ab2021-09-17 16:08:12 +0200271 return (uint32_t)min * 1000U;
272}
Yann Gautier3edc7c32019-05-20 19:17:08 +0200273
Yann Gautier296e1ab2021-09-17 16:08:12 +0200274/*******************************************************************************
275 * This function retrieves VDD supply regulator from DT.
276 * Returns an rdev taken from supply node, NULL otherwise.
277 ******************************************************************************/
278struct rdev *dt_get_vdd_regulator(void)
279{
280 int node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT);
281
Yann Gautier3edc7c32019-05-20 19:17:08 +0200282 if (node < 0) {
Yann Gautier296e1ab2021-09-17 16:08:12 +0200283 return NULL;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200284 }
285
Yann Gautier296e1ab2021-09-17 16:08:12 +0200286 return regulator_get_by_supply_name(fdt, node, "vdd");
287}
288
289/*******************************************************************************
290 * This function retrieves CPU supply regulator from DT.
291 * Returns an rdev taken from supply node, NULL otherwise.
292 ******************************************************************************/
293struct rdev *dt_get_cpu_regulator(void)
294{
295 int node = fdt_path_offset(fdt, "/cpus/cpu@0");
296
297 if (node < 0) {
298 return NULL;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200299 }
300
Yann Gautier296e1ab2021-09-17 16:08:12 +0200301 return regulator_get_by_supply_name(fdt, node, "cpu");
Yann Gautier3edc7c32019-05-20 19:17:08 +0200302}
303
304/*******************************************************************************
Yann Gautier69035a82018-07-05 16:48:16 +0200305 * This function retrieves board model from DT
306 * Returns string taken from model node, NULL otherwise
307 ******************************************************************************/
308const char *dt_get_board_model(void)
309{
310 int node = fdt_path_offset(fdt, "/");
311
312 if (node < 0) {
313 return NULL;
314 }
315
316 return (const char *)fdt_getprop(fdt, node, "model", NULL);
317}
Etienne Carriered81dadf2020-04-25 11:14:45 +0200318
319/*******************************************************************************
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100320 * dt_find_otp_name: get OTP ID and length in DT.
321 * name: sub-node name to look up.
322 * otp: pointer to read OTP number or NULL.
323 * otp_len: pointer to read OTP length in bits or NULL.
324 * return value: 0 if no error, an FDT error value otherwise.
325 ******************************************************************************/
326int dt_find_otp_name(const char *name, uint32_t *otp, uint32_t *otp_len)
327{
328 int node;
Patrick Delaunay82ece5c2022-03-01 09:56:03 +0100329 int len;
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100330 const fdt32_t *cuint;
331
332 if ((name == NULL) || (otp == NULL)) {
333 return -FDT_ERR_BADVALUE;
334 }
335
Patrick Delaunay82ece5c2022-03-01 09:56:03 +0100336 node = fdt_node_offset_by_compatible(fdt, -1, DT_BSEC_COMPAT);
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100337 if (node < 0) {
338 return node;
339 }
340
Patrick Delaunay82ece5c2022-03-01 09:56:03 +0100341 node = fdt_subnode_offset(fdt, node, name);
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100342 if (node < 0) {
Patrick Delaunay82ece5c2022-03-01 09:56:03 +0100343 ERROR("nvmem node %s not found\n", name);
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100344 return node;
345 }
346
347 cuint = fdt_getprop(fdt, node, "reg", &len);
348 if ((cuint == NULL) || (len != (2 * (int)sizeof(uint32_t)))) {
Patrick Delaunay82ece5c2022-03-01 09:56:03 +0100349 ERROR("Malformed nvmem node %s: ignored\n", name);
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100350 return -FDT_ERR_BADVALUE;
351 }
352
353 if (fdt32_to_cpu(*cuint) % sizeof(uint32_t)) {
Patrick Delaunay82ece5c2022-03-01 09:56:03 +0100354 ERROR("Misaligned nvmem %s element: ignored\n", name);
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100355 return -FDT_ERR_BADVALUE;
356 }
357
358 if (otp != NULL) {
359 *otp = fdt32_to_cpu(*cuint) / sizeof(uint32_t);
360 }
361
362 if (otp_len != NULL) {
363 cuint++;
364 *otp_len = fdt32_to_cpu(*cuint) * CHAR_BIT;
365 }
366
367 return 0;
368}
369
370/*******************************************************************************
Etienne Carriered81dadf2020-04-25 11:14:45 +0200371 * This function gets the pin count for a GPIO bank based from the FDT.
372 * It also checks node consistency.
373 ******************************************************************************/
374int fdt_get_gpio_bank_pin_count(unsigned int bank)
375{
376 int pinctrl_node;
377 int node;
378 uint32_t bank_offset;
379
380 pinctrl_node = stm32_get_gpio_bank_pinctrl_node(fdt, bank);
381 if (pinctrl_node < 0) {
382 return -FDT_ERR_NOTFOUND;
383 }
384
385 bank_offset = stm32_get_gpio_bank_offset(bank);
386
387 fdt_for_each_subnode(node, fdt, pinctrl_node) {
388 const fdt32_t *cuint;
Yann Gautier47516aa2022-11-21 11:45:04 +0100389 int pin_count = 0;
Fabien Dessenne41ab6522021-09-21 11:32:30 +0200390 int len;
391 int i;
Etienne Carriered81dadf2020-04-25 11:14:45 +0200392
393 if (fdt_getprop(fdt, node, "gpio-controller", NULL) == NULL) {
394 continue;
395 }
396
397 cuint = fdt_getprop(fdt, node, "reg", NULL);
398 if (cuint == NULL) {
399 continue;
400 }
401
402 if (fdt32_to_cpu(*cuint) != bank_offset) {
403 continue;
404 }
405
406 if (fdt_get_status(node) == DT_DISABLED) {
407 return 0;
408 }
409
Fabien Dessenne41ab6522021-09-21 11:32:30 +0200410 /* Parse gpio-ranges with its 4 parameters */
411 cuint = fdt_getprop(fdt, node, "gpio-ranges", &len);
412 len /= sizeof(*cuint);
413 if ((len % 4) != 0) {
414 return -FDT_ERR_BADVALUE;
415 }
416
417 /* Get the last defined gpio line (offset + nb of pins) */
Yann Gautier47516aa2022-11-21 11:45:04 +0100418 for (i = 0; i < len; i += 4) {
419 pin_count = MAX(pin_count, (int)(fdt32_to_cpu(cuint[i + 1]) +
420 fdt32_to_cpu(cuint[i + 3])));
Etienne Carriered81dadf2020-04-25 11:14:45 +0200421 }
422
Fabien Dessenne41ab6522021-09-21 11:32:30 +0200423 return pin_count;
Etienne Carriered81dadf2020-04-25 11:14:45 +0200424 }
425
426 return 0;
427}