blob: 60987593cb9e0baf3d514b222891f0d6bc7cd3a9 [file] [log] [blame]
Yann Gautier9aea69e2018-07-24 17:13:36 +02001/*
2 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <debug.h>
9#include <libfdt.h>
10#include <platform_def.h>
11#include <stm32mp1_clk.h>
12#include <stm32mp1_clkfunc.h>
13#include <stm32mp1_dt.h>
14
15#define DT_GPIO_BANK_SHIFT 12
16#define DT_GPIO_BANK_MASK 0x1F000U
17#define DT_GPIO_PIN_SHIFT 8
18#define DT_GPIO_PIN_MASK 0xF00U
19#define DT_GPIO_MODE_MASK 0xFFU
20
21static int fdt_checked;
22
23static void *fdt = (void *)(uintptr_t)STM32MP1_DTB_BASE;
24
25/*******************************************************************************
26 * This function checks device tree file with its header.
27 * Returns 0 if success, and a negative value else.
28 ******************************************************************************/
29int dt_open_and_check(void)
30{
31 int ret = fdt_check_header(fdt);
32
33 if (ret == 0) {
34 fdt_checked = 1;
35 }
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{
47 if (fdt_checked == 1) {
48 *fdt_addr = fdt;
49 }
50
51 return fdt_checked;
52}
53
54/*******************************************************************************
55 * This function check the presence of a node (generic use of fdt library).
56 * Returns true if present, false else.
57 ******************************************************************************/
58bool fdt_check_node(int node)
59{
60 int len;
61 const char *cchar;
62
63 cchar = fdt_get_name(fdt, node, &len);
64
65 return (cchar != NULL) && (len >= 0);
66}
67
68/*******************************************************************************
69 * This function check the status of a node (generic use of fdt library).
70 * Returns true if "okay" or missing, false else.
71 ******************************************************************************/
72bool fdt_check_status(int node)
73{
74 int len;
75 const char *cchar;
76
77 cchar = fdt_getprop(fdt, node, "status", &len);
78 if (cchar == NULL) {
79 return true;
80 }
81
82 return strncmp(cchar, "okay", (size_t)len) == 0;
83}
84
85/*******************************************************************************
86 * This function check the secure-status of a node (generic use of fdt library).
87 * Returns true if "okay" or missing, false else.
88 ******************************************************************************/
89bool fdt_check_secure_status(int node)
90{
91 int len;
92 const char *cchar;
93
94 cchar = fdt_getprop(fdt, node, "secure-status", &len);
95 if (cchar == NULL) {
96 return true;
97 }
98
99 return strncmp(cchar, "okay", (size_t)len) == 0;
100}
101
102/*******************************************************************************
103 * This function reads a value of a node property (generic use of fdt
104 * library).
105 * Returns value if success, and a default value if property not found.
106 * Default value is passed as parameter.
107 ******************************************************************************/
108uint32_t fdt_read_uint32_default(int node, const char *prop_name,
109 uint32_t dflt_value)
110{
111 const fdt32_t *cuint;
112 int lenp;
113
114 cuint = fdt_getprop(fdt, node, prop_name, &lenp);
115 if (cuint == NULL) {
116 return dflt_value;
117 }
118
119 return fdt32_to_cpu(*cuint);
120}
121
122/*******************************************************************************
123 * This function reads a series of parameters in a node property
124 * (generic use of fdt library).
125 * It reads the values inside the device tree, from property name and node.
126 * The number of parameters is also indicated as entry parameter.
127 * Returns 0 if success, and a negative value else.
128 * If success, values are stored at the third parameter address.
129 ******************************************************************************/
130int fdt_read_uint32_array(int node, const char *prop_name, uint32_t *array,
131 uint32_t count)
132{
133 const fdt32_t *cuint;
134 int len;
135 uint32_t i;
136
137 cuint = fdt_getprop(fdt, node, prop_name, &len);
138 if (cuint == NULL) {
139 return -FDT_ERR_NOTFOUND;
140 }
141
142 if ((uint32_t)len != (count * sizeof(uint32_t))) {
143 return -FDT_ERR_BADLAYOUT;
144 }
145
146 for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) {
147 *array = fdt32_to_cpu(*cuint);
148 array++;
149 cuint++;
150 }
151
152 return 0;
153}