fdt_subr.c revision 1.16 1 /* $NetBSD: fdt_subr.c,v 1.16 2017/07/08 12:36:51 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.16 2017/07/08 12:36:51 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 static struct fdt_conslist fdt_console_list =
42 TAILQ_HEAD_INITIALIZER(fdt_console_list);
43
44 bool
45 fdtbus_set_data(const void *data)
46 {
47 KASSERT(fdt_data == NULL);
48 if (fdt_check_header(data) != 0) {
49 return false;
50 }
51 fdt_data = data;
52 return true;
53 }
54
55 const void *
56 fdtbus_get_data(void)
57 {
58 return fdt_data;
59 }
60
61 int
62 fdtbus_offset2phandle(int offset)
63 {
64 if (offset < 0)
65 return 0;
66
67 return offset + fdt_off_dt_struct(fdt_data);
68 }
69
70 int
71 fdtbus_phandle2offset(int phandle)
72 {
73 const int dtoff = fdt_off_dt_struct(fdt_data);
74
75 if (phandle == -1)
76 phandle = dtoff;
77
78 if (phandle < dtoff)
79 return -1;
80
81 return phandle - dtoff;
82 }
83
84
85 static int
86 fdtbus_get_addr_cells(int phandle)
87 {
88 uint32_t addr_cells;
89
90 if (of_getprop_uint32(phandle, "#address-cells", &addr_cells))
91 addr_cells = 2;
92
93 return addr_cells;
94 }
95
96 static int
97 fdtbus_get_size_cells(int phandle)
98 {
99 uint32_t size_cells;
100
101 if (of_getprop_uint32(phandle, "#size-cells", &size_cells))
102 size_cells = 0;
103
104 return size_cells;
105 }
106
107 int
108 fdtbus_get_phandle(int phandle, const char *prop)
109 {
110 u_int phandle_ref;
111 u_int *buf;
112 int len;
113
114 len = OF_getproplen(phandle, prop);
115 if (len < sizeof(phandle_ref))
116 return -1;
117
118 buf = kmem_alloc(len, KM_SLEEP);
119
120 if (OF_getprop(phandle, prop, buf, len) != len) {
121 kmem_free(buf, len);
122 return -1;
123 }
124
125 phandle_ref = fdt32_to_cpu(buf[0]);
126 kmem_free(buf, len);
127
128 return fdtbus_get_phandle_from_native(phandle_ref);
129 }
130
131 int
132 fdtbus_get_phandle_from_native(int phandle)
133 {
134 const int off = fdt_node_offset_by_phandle(fdt_data, phandle);
135 if (off < 0) {
136 return -1;
137 }
138 return fdtbus_offset2phandle(off);
139 }
140
141 bool
142 fdtbus_get_path(int phandle, char *buf, size_t buflen)
143 {
144 const int off = fdtbus_phandle2offset(phandle);
145 if (off < 0) {
146 return false;
147 }
148 if (fdt_get_path(fdt_data, off, buf, (int)buflen) != 0) {
149 return false;
150 }
151 return true;
152 }
153
154 static uint64_t
155 fdtbus_get_cells(const uint8_t *buf, int cells)
156 {
157 switch (cells) {
158 case 0: return 0;
159 case 1: return be32dec(buf);
160 case 2: return be64dec(buf);
161 default: panic("fdtbus_get_cells: bad cells val %d\n", cells);
162 }
163 }
164
165 static uint64_t
166 fdtbus_decode_range(int phandle, uint64_t paddr)
167 {
168 const int parent = OF_parent(phandle);
169 if (parent == -1)
170 return paddr;
171 const uint8_t *buf;
172 int len;
173
174 buf = fdt_getprop(fdtbus_get_data(),
175 fdtbus_phandle2offset(phandle), "ranges", &len);
176 if (buf == NULL)
177 return paddr;
178
179 if (len == 0) {
180 /* pass through to parent */
181 return fdtbus_decode_range(parent, paddr);
182 }
183
184 const int addr_cells = fdtbus_get_addr_cells(phandle);
185 const int size_cells = fdtbus_get_size_cells(phandle);
186 const int paddr_cells = fdtbus_get_addr_cells(OF_parent(parent));
187 if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
188 return paddr;
189
190 while (len > 0) {
191 uint64_t cba, pba, cl;
192 cba = fdtbus_get_cells(buf, addr_cells);
193 buf += addr_cells * 4;
194 pba = fdtbus_get_cells(buf, paddr_cells);
195 buf += paddr_cells * 4;
196 cl = fdtbus_get_cells(buf, size_cells);
197 buf += size_cells * 4;
198
199 if (paddr >= cba && paddr < cba + cl)
200 return fdtbus_decode_range(parent, pba) + (paddr - cba);
201
202 len -= (addr_cells + paddr_cells + size_cells) * 4;
203 }
204
205 /* No mapping found */
206 return paddr;
207 }
208
209 int
210 fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
211 {
212 uint64_t addr, size;
213 int error;
214
215 error = fdtbus_get_reg64(phandle, index, &addr, &size);
216 if (error)
217 return error;
218
219 if (sizeof(bus_addr_t) == 4 && (addr + size) > 0x100000000)
220 return ERANGE;
221
222 if (paddr)
223 *paddr = (bus_addr_t)addr;
224 if (psize)
225 *psize = (bus_size_t)size;
226
227 return 0;
228 }
229
230 int
231 fdtbus_get_reg64(int phandle, u_int index, uint64_t *paddr, uint64_t *psize)
232 {
233 uint64_t addr, size;
234 const uint8_t *buf;
235 int len;
236
237 const int addr_cells = fdtbus_get_addr_cells(OF_parent(phandle));
238 const int size_cells = fdtbus_get_size_cells(OF_parent(phandle));
239 if (addr_cells == -1 || size_cells == -1)
240 return EINVAL;
241
242 buf = fdt_getprop(fdtbus_get_data(),
243 fdtbus_phandle2offset(phandle), "reg", &len);
244 if (buf == NULL || len <= 0)
245 return EINVAL;
246
247 const u_int reglen = size_cells * 4 + addr_cells * 4;
248 if (reglen == 0)
249 return EINVAL;
250
251 if (index >= len / reglen)
252 return ENXIO;
253
254 buf += index * reglen;
255 addr = fdtbus_get_cells(buf, addr_cells);
256 buf += addr_cells * 4;
257 size = fdtbus_get_cells(buf, size_cells);
258
259 if (paddr) {
260 *paddr = fdtbus_decode_range(OF_parent(phandle), addr);
261 const char *name = fdt_get_name(fdtbus_get_data(),
262 fdtbus_phandle2offset(phandle), NULL);
263 aprint_debug("fdt: [%s] decoded addr #%u: %llx -> %llx\n",
264 name, index, addr, *paddr);
265 }
266 if (psize)
267 *psize = size;
268
269 return 0;
270 }
271
272 const struct fdt_console *
273 fdtbus_get_console(void)
274 {
275 static const struct fdt_console_info *booted_console = NULL;
276
277 if (booted_console == NULL) {
278 __link_set_decl(fdt_consoles, struct fdt_console_info);
279 struct fdt_console_info * const *info;
280 const struct fdt_console_info *best_info = NULL;
281 const int phandle = fdtbus_get_stdout_phandle();
282 int best_match = 0;
283
284 __link_set_foreach(info, fdt_consoles) {
285 const int match = (*info)->ops->match(phandle);
286 if (match > best_match) {
287 best_match = match;
288 best_info = *info;
289 }
290 }
291
292 booted_console = best_info;
293 }
294
295 return booted_console == NULL ? NULL : booted_console->ops;
296 }
297
298 const char *
299 fdtbus_get_stdout_path(void)
300 {
301 const char *prop;
302
303 const int off = fdt_path_offset(fdtbus_get_data(), "/chosen");
304 if (off < 0)
305 return NULL;
306
307 prop = fdt_getprop(fdtbus_get_data(), off, "stdout-path", NULL);
308 if (prop != NULL)
309 return prop;
310
311 /* If the stdout-path property is not found, assume serial0 */
312 return "serial0:115200n8";
313 }
314
315 int
316 fdtbus_get_stdout_phandle(void)
317 {
318 const char *prop, *p;
319 int off, len;
320
321 prop = fdtbus_get_stdout_path();
322 if (prop == NULL)
323 return -1;
324
325 p = strchr(prop, ':');
326 len = p == NULL ? strlen(prop) : (p - prop);
327 if (*prop != '/') {
328 /* Alias */
329 prop = fdt_get_alias_namelen(fdtbus_get_data(), prop, len);
330 if (prop == NULL)
331 return -1;
332 len = strlen(prop);
333 }
334 off = fdt_path_offset_namelen(fdtbus_get_data(), prop, len);
335 if (off < 0)
336 return -1;
337
338 return fdtbus_offset2phandle(off);
339 }
340
341 int
342 fdtbus_get_stdout_speed(void)
343 {
344 const char *prop, *p;
345
346 prop = fdtbus_get_stdout_path();
347 if (prop == NULL)
348 return -1;
349
350 p = strchr(prop, ':');
351 if (p == NULL)
352 return -1;
353
354 return (int)strtoul(p + 1, NULL, 10);
355 }
356
357 tcflag_t
358 fdtbus_get_stdout_flags(void)
359 {
360 const char *prop, *p;
361 tcflag_t flags = TTYDEF_CFLAG;
362 char *ep;
363
364 prop = fdtbus_get_stdout_path();
365 if (prop == NULL)
366 return flags;
367
368 p = strchr(prop, ':');
369 if (p == NULL)
370 return flags;
371
372 ep = NULL;
373 (void)strtoul(p + 1, &ep, 10);
374 if (ep == NULL)
375 return flags;
376
377 /* <baud>{<parity>{<bits>{<flow>}}} */
378 while (*ep) {
379 switch (*ep) {
380 /* parity */
381 case 'n': flags &= ~(PARENB|PARODD); break;
382 case 'e': flags &= ~PARODD; flags |= PARENB; break;
383 case 'o': flags |= (PARENB|PARODD); break;
384 /* bits */
385 case '5': flags &= ~CSIZE; flags |= CS5; break;
386 case '6': flags &= ~CSIZE; flags |= CS6; break;
387 case '7': flags &= ~CSIZE; flags |= CS7; break;
388 case '8': flags &= ~CSIZE; flags |= CS8; break;
389 /* flow */
390 case 'r': flags |= CRTSCTS; break;
391 }
392 ep++;
393 }
394
395 return flags;
396 }
397
398 bool
399 fdtbus_status_okay(int phandle)
400 {
401 const int off = fdtbus_phandle2offset(phandle);
402
403 const char *prop = fdt_getprop(fdtbus_get_data(), off, "status", NULL);
404 if (prop == NULL)
405 return true;
406
407 return strncmp(prop, "ok", 2) == 0;
408 }
409
410 const void *
411 fdtbus_get_prop(int phandle, const char *prop, int *plen)
412 {
413 const int off = fdtbus_phandle2offset(phandle);
414
415 return fdt_getprop(fdtbus_get_data(), off, prop, plen);
416 }
417
418 const char *
419 fdtbus_get_string(int phandle, const char *prop)
420 {
421 const int off = fdtbus_phandle2offset(phandle);
422
423 return fdt_getprop(fdtbus_get_data(), off, prop, NULL);
424 }
425
426 const char *
427 fdtbus_get_string_index(int phandle, const char *prop, u_int index)
428 {
429 const char *names, *name;
430 int len, cur;
431
432 if ((len = OF_getproplen(phandle, prop)) < 0)
433 return NULL;
434
435 names = fdtbus_get_string(phandle, prop);
436
437 for (name = names, cur = 0; len > 0;
438 name += strlen(name) + 1, cur++) {
439 if (index == cur)
440 return name;
441 }
442
443 return NULL;
444 }
445