blob: ea715715714497880853918c36d222c6b19f9624 [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>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <drivers/st/stm32mp1_ddr.h>
15#include <drivers/st/stm32mp1_ram.h>
Yann Gautier296e1ab2021-09-17 16:08:12 +020016#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017
Yann Gautier296e1ab2021-09-17 16:08:12 +020018#include <platform_def.h>
Yann Gautieree8f5422019-02-14 11:13:25 +010019#include <stm32mp_dt.h>
20
Yann Gautier05773eb2020-08-24 11:51:50 +020021static void *fdt;
Yann Gautier9aea69e2018-07-24 17:13:36 +020022
23/*******************************************************************************
24 * This function checks device tree file with its header.
Yann Gautier038bff22019-01-17 19:17:47 +010025 * Returns 0 on success and a negative FDT error code on failure.
Yann Gautier9aea69e2018-07-24 17:13:36 +020026 ******************************************************************************/
Yann Gautier05773eb2020-08-24 11:51:50 +020027int dt_open_and_check(uintptr_t dt_addr)
Yann Gautier9aea69e2018-07-24 17:13:36 +020028{
Yann Gautier05773eb2020-08-24 11:51:50 +020029 int ret;
Yann Gautier9aea69e2018-07-24 17:13:36 +020030
Yann Gautier05773eb2020-08-24 11:51:50 +020031 ret = fdt_check_header((void *)dt_addr);
Yann Gautier9aea69e2018-07-24 17:13:36 +020032 if (ret == 0) {
Yann Gautier05773eb2020-08-24 11:51:50 +020033 fdt = (void *)dt_addr;
Yann Gautier9aea69e2018-07-24 17:13:36 +020034 }
35
36 return ret;
37}
38
39/*******************************************************************************
40 * This function gets the address of the DT.
41 * If DT is OK, fdt_addr is filled with DT address.
42 * Returns 1 if success, 0 otherwise.
43 ******************************************************************************/
44int fdt_get_address(void **fdt_addr)
45{
Yann Gautier05773eb2020-08-24 11:51:50 +020046 if (fdt == NULL) {
47 return 0;
Yann Gautier9aea69e2018-07-24 17:13:36 +020048 }
49
Yann Gautier05773eb2020-08-24 11:51:50 +020050 *fdt_addr = fdt;
51
52 return 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +020053}
54
55/*******************************************************************************
56 * This function check the presence of a node (generic use of fdt library).
Yann Gautier038bff22019-01-17 19:17:47 +010057 * Returns true if present, else return false.
Yann Gautier9aea69e2018-07-24 17:13:36 +020058 ******************************************************************************/
59bool 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/*******************************************************************************
Yann Gautier038bff22019-01-17 19:17:47 +010070 * This function return global node status (generic use of fdt library).
Yann Gautier9aea69e2018-07-24 17:13:36 +020071 ******************************************************************************/
Yann Gautieree8f5422019-02-14 11:13:25 +010072uint8_t fdt_get_status(int node)
Yann Gautier9aea69e2018-07-24 17:13:36 +020073{
Yann Gautieree8f5422019-02-14 11:13:25 +010074 uint8_t status = DT_DISABLED;
Yann Gautier9aea69e2018-07-24 17:13:36 +020075 const char *cchar;
76
Yann Gautier1c959332021-03-10 14:07:34 +010077 cchar = fdt_getprop(fdt, node, "status", NULL);
Yann Gautier038bff22019-01-17 19:17:47 +010078 if ((cchar == NULL) ||
Yann Gautier1c959332021-03-10 14:07:34 +010079 (strncmp(cchar, "okay", strlen("okay")) == 0)) {
Yann Gautier038bff22019-01-17 19:17:47 +010080 status |= DT_NON_SECURE;
Yann Gautier9aea69e2018-07-24 17:13:36 +020081 }
82
Yann Gautier1c959332021-03-10 14:07:34 +010083 cchar = fdt_getprop(fdt, node, "secure-status", NULL);
Yann Gautier9aea69e2018-07-24 17:13:36 +020084 if (cchar == NULL) {
Yann Gautier038bff22019-01-17 19:17:47 +010085 if (status == DT_NON_SECURE) {
86 status |= DT_SECURE;
87 }
Yann Gautier1c959332021-03-10 14:07:34 +010088 } else if (strncmp(cchar, "okay", strlen("okay")) == 0) {
Yann Gautier038bff22019-01-17 19:17:47 +010089 status |= DT_SECURE;
Yann Gautier9aea69e2018-07-24 17:13:36 +020090 }
91
Yann Gautier038bff22019-01-17 19:17:47 +010092 return status;
Yann Gautier9aea69e2018-07-24 17:13:36 +020093}
94
Yann Gautier2260b842020-03-11 17:17:51 +010095#if ENABLE_ASSERTIONS
Yann Gautier9aea69e2018-07-24 17:13:36 +020096/*******************************************************************************
Lionel Debieveb0899eb2019-09-24 17:41:11 +020097 * This function returns the address cells from the node parent.
98 * Returns:
99 * - #address-cells value if success.
100 * - invalid value if error.
101 * - a default value if undefined #address-cells property as per libfdt
102 * implementation.
103 ******************************************************************************/
Yann Gautier2260b842020-03-11 17:17:51 +0100104static int fdt_get_node_parent_address_cells(int node)
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200105{
106 int parent;
107
108 parent = fdt_parent_offset(fdt, node);
109 if (parent < 0) {
110 return -FDT_ERR_NOTFOUND;
111 }
112
113 return fdt_address_cells(fdt, parent);
114}
Yann Gautier2260b842020-03-11 17:17:51 +0100115#endif
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200116
117/*******************************************************************************
Yann Gautier69035a82018-07-05 16:48:16 +0200118 * This function gets the stdout pin configuration information from the DT.
119 * And then calls the sub-function to treat it and set GPIO registers.
Yann Gautier038bff22019-01-17 19:17:47 +0100120 * Returns 0 on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200121 ******************************************************************************/
122int dt_set_stdout_pinctrl(void)
123{
124 int node;
125
Andre Przywaraeec91922020-04-09 11:27:21 +0100126 node = fdt_get_stdout_node_offset(fdt);
Yann Gautier69035a82018-07-05 16:48:16 +0200127 if (node < 0) {
128 return -FDT_ERR_NOTFOUND;
129 }
130
131 return dt_set_pinctrl_config(node);
132}
133
134/*******************************************************************************
135 * This function fills the generic information from a given node.
136 ******************************************************************************/
137void dt_fill_device_info(struct dt_node_info *info, int node)
138{
139 const fdt32_t *cuint;
140
Lionel Debieveb0899eb2019-09-24 17:41:11 +0200141 assert(fdt_get_node_parent_address_cells(node) == 1);
142
Yann Gautier69035a82018-07-05 16:48:16 +0200143 cuint = fdt_getprop(fdt, node, "reg", NULL);
144 if (cuint != NULL) {
145 info->base = fdt32_to_cpu(*cuint);
146 } else {
147 info->base = 0;
148 }
149
150 cuint = fdt_getprop(fdt, node, "clocks", NULL);
151 if (cuint != NULL) {
152 cuint++;
153 info->clock = (int)fdt32_to_cpu(*cuint);
154 } else {
155 info->clock = -1;
156 }
157
158 cuint = fdt_getprop(fdt, node, "resets", NULL);
159 if (cuint != NULL) {
160 cuint++;
161 info->reset = (int)fdt32_to_cpu(*cuint);
162 } else {
163 info->reset = -1;
164 }
165
Yann Gautier038bff22019-01-17 19:17:47 +0100166 info->status = fdt_get_status(node);
Yann Gautier69035a82018-07-05 16:48:16 +0200167}
168
169/*******************************************************************************
170 * This function retrieve the generic information from DT.
Yann Gautier038bff22019-01-17 19:17:47 +0100171 * Returns node on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200172 ******************************************************************************/
173int dt_get_node(struct dt_node_info *info, int offset, const char *compat)
174{
175 int node;
176
177 node = fdt_node_offset_by_compatible(fdt, offset, compat);
178 if (node < 0) {
179 return -FDT_ERR_NOTFOUND;
180 }
181
182 dt_fill_device_info(info, node);
183
184 return node;
185}
186
187/*******************************************************************************
188 * This function gets the UART instance info of stdout from the DT.
Yann Gautier038bff22019-01-17 19:17:47 +0100189 * Returns node on success and a negative FDT error code on failure.
Yann Gautier69035a82018-07-05 16:48:16 +0200190 ******************************************************************************/
191int dt_get_stdout_uart_info(struct dt_node_info *info)
192{
193 int node;
194
Andre Przywaraeec91922020-04-09 11:27:21 +0100195 node = fdt_get_stdout_node_offset(fdt);
Yann Gautier69035a82018-07-05 16:48:16 +0200196 if (node < 0) {
197 return -FDT_ERR_NOTFOUND;
198 }
199
200 dt_fill_device_info(info, node);
201
202 return node;
203}
204
205/*******************************************************************************
Yann Gautier4e267842021-09-29 11:31:09 +0200206 * This function returns the node offset matching compatible string in the DT,
207 * and also matching the reg property with the given address.
208 * Returns value on success, and error value on failure.
209 ******************************************************************************/
210int dt_match_instance_by_compatible(const char *compatible, uintptr_t address)
211{
212 int node;
213
214 fdt_for_each_compatible_node(fdt, node, compatible) {
215 const fdt32_t *cuint;
216
217 assert(fdt_get_node_parent_address_cells(node) == 1);
218
219 cuint = fdt_getprop(fdt, node, "reg", NULL);
220 if (cuint == NULL) {
221 continue;
222 }
223
224 if ((uintptr_t)fdt32_to_cpu(*cuint) == address) {
225 return node;
226 }
227 }
228
229 return -FDT_ERR_NOTFOUND;
230}
231
232/*******************************************************************************
Yann Gautiercaf575b2018-07-24 17:18:19 +0200233 * This function gets DDR size information from the DT.
Yann Gautier038bff22019-01-17 19:17:47 +0100234 * Returns value in bytes on success, and 0 on failure.
Yann Gautiercaf575b2018-07-24 17:18:19 +0200235 ******************************************************************************/
236uint32_t dt_get_ddr_size(void)
237{
Lionel Debieve003972c2020-09-24 16:01:12 +0200238 static uint32_t size;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200239 int node;
240
Lionel Debieve003972c2020-09-24 16:01:12 +0200241 if (size != 0U) {
242 return size;
243 }
244
Yann Gautiercaf575b2018-07-24 17:18:19 +0200245 node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
246 if (node < 0) {
247 INFO("%s: Cannot read DDR node in DT\n", __func__);
Yann Gautier038bff22019-01-17 19:17:47 +0100248 return 0;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200249 }
250
Lionel Debieve003972c2020-09-24 16:01:12 +0200251 size = fdt_read_uint32_default(fdt, node, "st,mem-size", 0U);
252
253 flush_dcache_range((uintptr_t)&size, sizeof(uint32_t));
254
255 return size;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200256}
257
258/*******************************************************************************
Yann Gautier3edc7c32019-05-20 19:17:08 +0200259 * This function gets PWR VDD regulator voltage information from the DT.
260 * Returns value in microvolts on success, and 0 on failure.
261 ******************************************************************************/
262uint32_t dt_get_pwr_vdd_voltage(void)
263{
Yann Gautier296e1ab2021-09-17 16:08:12 +0200264 struct rdev *regul = dt_get_vdd_regulator();
265 uint16_t min;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200266
Yann Gautier296e1ab2021-09-17 16:08:12 +0200267 if (regul == NULL) {
Yann Gautier3edc7c32019-05-20 19:17:08 +0200268 return 0;
269 }
270
Yann Gautier296e1ab2021-09-17 16:08:12 +0200271 regulator_get_range(regul, &min, NULL);
Yann Gautier3edc7c32019-05-20 19:17:08 +0200272
Yann Gautier296e1ab2021-09-17 16:08:12 +0200273 return (uint32_t)min * 1000U;
274}
Yann Gautier3edc7c32019-05-20 19:17:08 +0200275
Yann Gautier296e1ab2021-09-17 16:08:12 +0200276/*******************************************************************************
277 * This function retrieves VDD supply regulator from DT.
278 * Returns an rdev taken from supply node, NULL otherwise.
279 ******************************************************************************/
280struct rdev *dt_get_vdd_regulator(void)
281{
282 int node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT);
283
Yann Gautier3edc7c32019-05-20 19:17:08 +0200284 if (node < 0) {
Yann Gautier296e1ab2021-09-17 16:08:12 +0200285 return NULL;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200286 }
287
Yann Gautier296e1ab2021-09-17 16:08:12 +0200288 return regulator_get_by_supply_name(fdt, node, "vdd");
289}
290
291/*******************************************************************************
292 * This function retrieves CPU supply regulator from DT.
293 * Returns an rdev taken from supply node, NULL otherwise.
294 ******************************************************************************/
295struct rdev *dt_get_cpu_regulator(void)
296{
297 int node = fdt_path_offset(fdt, "/cpus/cpu@0");
298
299 if (node < 0) {
300 return NULL;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200301 }
302
Yann Gautier296e1ab2021-09-17 16:08:12 +0200303 return regulator_get_by_supply_name(fdt, node, "cpu");
Yann Gautier3edc7c32019-05-20 19:17:08 +0200304}
305
306/*******************************************************************************
Yann Gautier69035a82018-07-05 16:48:16 +0200307 * This function retrieves board model from DT
308 * Returns string taken from model node, NULL otherwise
309 ******************************************************************************/
310const char *dt_get_board_model(void)
311{
312 int node = fdt_path_offset(fdt, "/");
313
314 if (node < 0) {
315 return NULL;
316 }
317
318 return (const char *)fdt_getprop(fdt, node, "model", NULL);
319}
Etienne Carriered81dadf2020-04-25 11:14:45 +0200320
321/*******************************************************************************
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100322 * dt_find_otp_name: get OTP ID and length in DT.
323 * name: sub-node name to look up.
324 * otp: pointer to read OTP number or NULL.
325 * otp_len: pointer to read OTP length in bits or NULL.
326 * return value: 0 if no error, an FDT error value otherwise.
327 ******************************************************************************/
328int dt_find_otp_name(const char *name, uint32_t *otp, uint32_t *otp_len)
329{
330 int node;
331 int index, len;
332 const fdt32_t *cuint;
333
334 if ((name == NULL) || (otp == NULL)) {
335 return -FDT_ERR_BADVALUE;
336 }
337
338 node = fdt_node_offset_by_compatible(fdt, -1, DT_NVMEM_LAYOUT_COMPAT);
339 if (node < 0) {
340 return node;
341 }
342
343 index = fdt_stringlist_search(fdt, node, "nvmem-cell-names", name);
344 if (index < 0) {
345 return index;
346 }
347
348 cuint = fdt_getprop(fdt, node, "nvmem-cells", &len);
349 if (cuint == NULL) {
350 return -FDT_ERR_NOTFOUND;
351 }
352
353 if ((index * (int)sizeof(uint32_t)) > len) {
354 return -FDT_ERR_BADVALUE;
355 }
356
357 cuint += index;
358
359 node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
360 if (node < 0) {
361 ERROR("Malformed nvmem_layout node: ignored\n");
362 return node;
363 }
364
365 cuint = fdt_getprop(fdt, node, "reg", &len);
366 if ((cuint == NULL) || (len != (2 * (int)sizeof(uint32_t)))) {
367 ERROR("Malformed nvmem_layout node: ignored\n");
368 return -FDT_ERR_BADVALUE;
369 }
370
371 if (fdt32_to_cpu(*cuint) % sizeof(uint32_t)) {
372 ERROR("Misaligned nvmem_layout element: ignored\n");
373 return -FDT_ERR_BADVALUE;
374 }
375
376 if (otp != NULL) {
377 *otp = fdt32_to_cpu(*cuint) / sizeof(uint32_t);
378 }
379
380 if (otp_len != NULL) {
381 cuint++;
382 *otp_len = fdt32_to_cpu(*cuint) * CHAR_BIT;
383 }
384
385 return 0;
386}
387
388/*******************************************************************************
Etienne Carriered81dadf2020-04-25 11:14:45 +0200389 * This function gets the pin count for a GPIO bank based from the FDT.
390 * It also checks node consistency.
391 ******************************************************************************/
392int fdt_get_gpio_bank_pin_count(unsigned int bank)
393{
394 int pinctrl_node;
395 int node;
396 uint32_t bank_offset;
397
398 pinctrl_node = stm32_get_gpio_bank_pinctrl_node(fdt, bank);
399 if (pinctrl_node < 0) {
400 return -FDT_ERR_NOTFOUND;
401 }
402
403 bank_offset = stm32_get_gpio_bank_offset(bank);
404
405 fdt_for_each_subnode(node, fdt, pinctrl_node) {
406 const fdt32_t *cuint;
Fabien Dessenne41ab6522021-09-21 11:32:30 +0200407 int pin_count;
408 int len;
409 int i;
Etienne Carriered81dadf2020-04-25 11:14:45 +0200410
411 if (fdt_getprop(fdt, node, "gpio-controller", NULL) == NULL) {
412 continue;
413 }
414
415 cuint = fdt_getprop(fdt, node, "reg", NULL);
416 if (cuint == NULL) {
417 continue;
418 }
419
420 if (fdt32_to_cpu(*cuint) != bank_offset) {
421 continue;
422 }
423
424 if (fdt_get_status(node) == DT_DISABLED) {
425 return 0;
426 }
427
Fabien Dessenne41ab6522021-09-21 11:32:30 +0200428 /* Parse gpio-ranges with its 4 parameters */
429 cuint = fdt_getprop(fdt, node, "gpio-ranges", &len);
430 len /= sizeof(*cuint);
431 if ((len % 4) != 0) {
432 return -FDT_ERR_BADVALUE;
433 }
434
435 /* Get the last defined gpio line (offset + nb of pins) */
436 pin_count = fdt32_to_cpu(*(cuint + 1)) + fdt32_to_cpu(*(cuint + 3));
437 for (i = 0; i < len / 4; i++) {
438 pin_count = MAX(pin_count, (int)(fdt32_to_cpu(*(cuint + 1)) +
439 fdt32_to_cpu(*(cuint + 3))));
440 cuint += 4;
Etienne Carriered81dadf2020-04-25 11:14:45 +0200441 }
442
Fabien Dessenne41ab6522021-09-21 11:32:30 +0200443 return pin_count;
Etienne Carriered81dadf2020-04-25 11:14:45 +0200444 }
445
446 return 0;
447}