fdt_subr.c revision 1.10 1 /* $NetBSD: fdt_subr.c,v 1.10 2017/04/29 12:38:26 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.10 2017/04/29 12:38:26 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35
36 #include <libfdt.h>
37 #include <dev/fdt/fdtvar.h>
38
39 static const void *fdt_data;
40
41 bool
42 fdtbus_set_data(const void *data)
43 {
44 KASSERT(fdt_data == NULL);
45 if (fdt_check_header(data) != 0) {
46 return false;
47 }
48 fdt_data = data;
49 return true;
50 }
51
52 const void *
53 fdtbus_get_data(void)
54 {
55 return fdt_data;
56 }
57
58 int
59 fdtbus_offset2phandle(int offset)
60 {
61 if (offset < 0)
62 return 0;
63
64 return offset + fdt_off_dt_struct(fdt_data);
65 }
66
67 int
68 fdtbus_phandle2offset(int phandle)
69 {
70 const int dtoff = fdt_off_dt_struct(fdt_data);
71
72 if (phandle == -1)
73 phandle = dtoff;
74
75 if (phandle < dtoff)
76 return -1;
77
78 return phandle - dtoff;
79 }
80
81
82 static int
83 fdtbus_get_addr_cells(int phandle)
84 {
85 uint32_t addr_cells;
86
87 const int parent = OF_parent(phandle);
88 if (parent == -1)
89 return -1;
90
91 if (of_getprop_uint32(parent, "#address-cells", &addr_cells))
92 addr_cells = 2;
93
94 return addr_cells;
95 }
96
97 static int
98 fdtbus_get_size_cells(int phandle)
99 {
100 uint32_t size_cells;
101
102 const int parent = OF_parent(phandle);
103 if (parent == -1)
104 return -1;
105
106 if (of_getprop_uint32(parent, "#size-cells", &size_cells))
107 size_cells = 0;
108
109 return size_cells;
110 }
111
112 int
113 fdtbus_get_phandle(int phandle, const char *prop)
114 {
115 u_int phandle_ref;
116 u_int *buf;
117 int len;
118
119 len = OF_getproplen(phandle, prop);
120 if (len < sizeof(phandle_ref))
121 return -1;
122
123 buf = kmem_alloc(len, KM_SLEEP);
124
125 if (OF_getprop(phandle, prop, buf, len) != len) {
126 kmem_free(buf, len);
127 return -1;
128 }
129
130 phandle_ref = fdt32_to_cpu(buf[0]);
131 kmem_free(buf, len);
132
133 return fdtbus_get_phandle_from_native(phandle_ref);
134 }
135
136 int
137 fdtbus_get_phandle_from_native(int phandle)
138 {
139 const int off = fdt_node_offset_by_phandle(fdt_data, phandle);
140 if (off < 0) {
141 return -1;
142 }
143 return fdtbus_offset2phandle(off);
144 }
145
146 bool
147 fdtbus_get_path(int phandle, char *buf, size_t buflen)
148 {
149 const int off = fdtbus_phandle2offset(phandle);
150 if (off < 0) {
151 return false;
152 }
153 if (fdt_get_path(fdt_data, off, buf, (int)buflen) != 0) {
154 return false;
155 }
156 return true;
157 }
158
159 int
160 fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
161 {
162 bus_addr_t addr;
163 bus_size_t size;
164 const uint8_t *buf;
165 int len;
166
167 const int addr_cells = fdtbus_get_addr_cells(phandle);
168 const int size_cells = fdtbus_get_size_cells(phandle);
169 if (addr_cells == -1 || size_cells == -1)
170 return -1;
171
172 buf = fdt_getprop(fdtbus_get_data(),
173 fdtbus_phandle2offset(phandle), "reg", &len);
174 if (buf == NULL || len <= 0)
175 return -1;
176
177 const u_int reglen = size_cells * 4 + addr_cells * 4;
178 if (reglen == 0)
179 return -1;
180
181 if (index >= len / reglen)
182 return -1;
183
184 switch (addr_cells) {
185 case 0:
186 addr = 0;
187 break;
188 case 1:
189 addr = be32dec(&buf[index * reglen + 0]);
190 break;
191 case 2:
192 addr = be64dec(&buf[index * reglen + 0]);
193 break;
194 default:
195 panic("fdtbus_get_reg: unsupported addr_cells %d", addr_cells);
196 }
197
198 switch (size_cells) {
199 case 0:
200 size = 0;
201 break;
202 case 1:
203 size = be32dec(&buf[index * reglen + addr_cells * 4]);
204 break;
205 case 2:
206 size = be64dec(&buf[index * reglen + addr_cells * 4]);
207 break;
208 default:
209 panic("fdtbus_get_reg: unsupported size_cells %d", size_cells);
210 }
211
212 if (paddr)
213 *paddr = addr;
214 if (psize)
215 *psize = size;
216
217 return 0;
218 }
219
220 const char *
221 fdtbus_get_stdout_path(void)
222 {
223 const char *prop;
224
225 const int off = fdt_path_offset(fdtbus_get_data(), "/chosen");
226 if (off < 0)
227 return NULL;
228
229 prop = fdt_getprop(fdtbus_get_data(), off, "stdout-path", NULL);
230 if (prop != NULL)
231 return prop;
232
233 /* If the stdout-path property is not found, assume serial0 */
234 return "serial0:115200n8";
235 }
236
237 int
238 fdtbus_get_stdout_phandle(void)
239 {
240 const char *prop, *p;
241 int off, len;
242
243 prop = fdtbus_get_stdout_path();
244 if (prop == NULL)
245 return -1;
246
247 p = strchr(prop, ':');
248 len = p == NULL ? strlen(prop) : (p - prop);
249 if (*prop != '/') {
250 /* Alias */
251 prop = fdt_get_alias_namelen(fdtbus_get_data(), prop, len);
252 if (prop == NULL)
253 return -1;
254 len = strlen(prop);
255 }
256 off = fdt_path_offset_namelen(fdtbus_get_data(), prop, len);
257 if (off < 0)
258 return -1;
259
260 return fdtbus_offset2phandle(off);
261 }
262
263 int
264 fdtbus_get_stdout_speed(void)
265 {
266 const char *prop, *p;
267
268 prop = fdtbus_get_stdout_path();
269 if (prop == NULL)
270 return -1;
271
272 p = strchr(prop, ':');
273 if (p == NULL)
274 return -1;
275
276 return (int)strtoul(p + 1, NULL, 10);
277 }
278
279 bool
280 fdtbus_status_okay(int phandle)
281 {
282 const int off = fdtbus_phandle2offset(phandle);
283
284 const char *prop = fdt_getprop(fdtbus_get_data(), off, "status", NULL);
285 if (prop == NULL)
286 return true;
287
288 return strncmp(prop, "ok", 2) == 0;
289 }
290