fdt_subr.c revision 1.17 1 1.17 jmcneill /* $NetBSD: fdt_subr.c,v 1.17 2017/07/19 20:18:07 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.17 jmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.17 2017/07/19 20:18:07 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill
35 1.1 jmcneill #include <libfdt.h>
36 1.1 jmcneill #include <dev/fdt/fdtvar.h>
37 1.1 jmcneill
38 1.3 jmcneill static const void *fdt_data;
39 1.3 jmcneill
40 1.12 jmcneill static struct fdt_conslist fdt_console_list =
41 1.12 jmcneill TAILQ_HEAD_INITIALIZER(fdt_console_list);
42 1.12 jmcneill
43 1.3 jmcneill bool
44 1.3 jmcneill fdtbus_set_data(const void *data)
45 1.3 jmcneill {
46 1.3 jmcneill KASSERT(fdt_data == NULL);
47 1.3 jmcneill if (fdt_check_header(data) != 0) {
48 1.3 jmcneill return false;
49 1.3 jmcneill }
50 1.3 jmcneill fdt_data = data;
51 1.3 jmcneill return true;
52 1.3 jmcneill }
53 1.3 jmcneill
54 1.3 jmcneill const void *
55 1.3 jmcneill fdtbus_get_data(void)
56 1.3 jmcneill {
57 1.3 jmcneill return fdt_data;
58 1.3 jmcneill }
59 1.3 jmcneill
60 1.3 jmcneill int
61 1.3 jmcneill fdtbus_offset2phandle(int offset)
62 1.3 jmcneill {
63 1.3 jmcneill if (offset < 0)
64 1.3 jmcneill return 0;
65 1.3 jmcneill
66 1.3 jmcneill return offset + fdt_off_dt_struct(fdt_data);
67 1.3 jmcneill }
68 1.3 jmcneill
69 1.3 jmcneill int
70 1.3 jmcneill fdtbus_phandle2offset(int phandle)
71 1.3 jmcneill {
72 1.3 jmcneill const int dtoff = fdt_off_dt_struct(fdt_data);
73 1.3 jmcneill
74 1.3 jmcneill if (phandle == -1)
75 1.3 jmcneill phandle = dtoff;
76 1.3 jmcneill
77 1.3 jmcneill if (phandle < dtoff)
78 1.3 jmcneill return -1;
79 1.3 jmcneill
80 1.3 jmcneill return phandle - dtoff;
81 1.3 jmcneill }
82 1.3 jmcneill
83 1.3 jmcneill
84 1.1 jmcneill static int
85 1.1 jmcneill fdtbus_get_addr_cells(int phandle)
86 1.1 jmcneill {
87 1.4 jmcneill uint32_t addr_cells;
88 1.1 jmcneill
89 1.13 jmcneill if (of_getprop_uint32(phandle, "#address-cells", &addr_cells))
90 1.1 jmcneill addr_cells = 2;
91 1.1 jmcneill
92 1.1 jmcneill return addr_cells;
93 1.1 jmcneill }
94 1.1 jmcneill
95 1.1 jmcneill static int
96 1.1 jmcneill fdtbus_get_size_cells(int phandle)
97 1.1 jmcneill {
98 1.4 jmcneill uint32_t size_cells;
99 1.1 jmcneill
100 1.13 jmcneill if (of_getprop_uint32(phandle, "#size-cells", &size_cells))
101 1.1 jmcneill size_cells = 0;
102 1.1 jmcneill
103 1.1 jmcneill return size_cells;
104 1.1 jmcneill }
105 1.1 jmcneill
106 1.1 jmcneill int
107 1.1 jmcneill fdtbus_get_phandle(int phandle, const char *prop)
108 1.1 jmcneill {
109 1.1 jmcneill u_int phandle_ref;
110 1.17 jmcneill const u_int *buf;
111 1.1 jmcneill int len;
112 1.1 jmcneill
113 1.17 jmcneill buf = fdt_getprop(fdtbus_get_data(),
114 1.17 jmcneill fdtbus_phandle2offset(phandle), prop, &len);
115 1.17 jmcneill if (buf == NULL || len < sizeof(phandle_ref))
116 1.1 jmcneill return -1;
117 1.1 jmcneill
118 1.17 jmcneill phandle_ref = be32dec(buf);
119 1.1 jmcneill
120 1.5 jmcneill return fdtbus_get_phandle_from_native(phandle_ref);
121 1.5 jmcneill }
122 1.5 jmcneill
123 1.5 jmcneill int
124 1.5 jmcneill fdtbus_get_phandle_from_native(int phandle)
125 1.5 jmcneill {
126 1.5 jmcneill const int off = fdt_node_offset_by_phandle(fdt_data, phandle);
127 1.1 jmcneill if (off < 0) {
128 1.1 jmcneill return -1;
129 1.1 jmcneill }
130 1.3 jmcneill return fdtbus_offset2phandle(off);
131 1.1 jmcneill }
132 1.1 jmcneill
133 1.6 jmcneill bool
134 1.6 jmcneill fdtbus_get_path(int phandle, char *buf, size_t buflen)
135 1.6 jmcneill {
136 1.6 jmcneill const int off = fdtbus_phandle2offset(phandle);
137 1.6 jmcneill if (off < 0) {
138 1.6 jmcneill return false;
139 1.6 jmcneill }
140 1.6 jmcneill if (fdt_get_path(fdt_data, off, buf, (int)buflen) != 0) {
141 1.6 jmcneill return false;
142 1.6 jmcneill }
143 1.6 jmcneill return true;
144 1.6 jmcneill }
145 1.6 jmcneill
146 1.13 jmcneill static uint64_t
147 1.13 jmcneill fdtbus_get_cells(const uint8_t *buf, int cells)
148 1.13 jmcneill {
149 1.13 jmcneill switch (cells) {
150 1.13 jmcneill case 0: return 0;
151 1.13 jmcneill case 1: return be32dec(buf);
152 1.13 jmcneill case 2: return be64dec(buf);
153 1.13 jmcneill default: panic("fdtbus_get_cells: bad cells val %d\n", cells);
154 1.13 jmcneill }
155 1.13 jmcneill }
156 1.13 jmcneill
157 1.13 jmcneill static uint64_t
158 1.13 jmcneill fdtbus_decode_range(int phandle, uint64_t paddr)
159 1.13 jmcneill {
160 1.13 jmcneill const int parent = OF_parent(phandle);
161 1.13 jmcneill if (parent == -1)
162 1.13 jmcneill return paddr;
163 1.13 jmcneill const uint8_t *buf;
164 1.13 jmcneill int len;
165 1.13 jmcneill
166 1.13 jmcneill buf = fdt_getprop(fdtbus_get_data(),
167 1.13 jmcneill fdtbus_phandle2offset(phandle), "ranges", &len);
168 1.13 jmcneill if (buf == NULL)
169 1.13 jmcneill return paddr;
170 1.13 jmcneill
171 1.13 jmcneill if (len == 0) {
172 1.13 jmcneill /* pass through to parent */
173 1.13 jmcneill return fdtbus_decode_range(parent, paddr);
174 1.13 jmcneill }
175 1.13 jmcneill
176 1.13 jmcneill const int addr_cells = fdtbus_get_addr_cells(phandle);
177 1.13 jmcneill const int size_cells = fdtbus_get_size_cells(phandle);
178 1.13 jmcneill const int paddr_cells = fdtbus_get_addr_cells(OF_parent(parent));
179 1.13 jmcneill if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
180 1.13 jmcneill return paddr;
181 1.13 jmcneill
182 1.13 jmcneill while (len > 0) {
183 1.13 jmcneill uint64_t cba, pba, cl;
184 1.13 jmcneill cba = fdtbus_get_cells(buf, addr_cells);
185 1.13 jmcneill buf += addr_cells * 4;
186 1.13 jmcneill pba = fdtbus_get_cells(buf, paddr_cells);
187 1.13 jmcneill buf += paddr_cells * 4;
188 1.13 jmcneill cl = fdtbus_get_cells(buf, size_cells);
189 1.13 jmcneill buf += size_cells * 4;
190 1.13 jmcneill
191 1.13 jmcneill if (paddr >= cba && paddr < cba + cl)
192 1.13 jmcneill return fdtbus_decode_range(parent, pba) + (paddr - cba);
193 1.13 jmcneill
194 1.13 jmcneill len -= (addr_cells + paddr_cells + size_cells) * 4;
195 1.13 jmcneill }
196 1.13 jmcneill
197 1.13 jmcneill /* No mapping found */
198 1.13 jmcneill return paddr;
199 1.13 jmcneill }
200 1.13 jmcneill
201 1.1 jmcneill int
202 1.1 jmcneill fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
203 1.1 jmcneill {
204 1.11 jmcneill uint64_t addr, size;
205 1.11 jmcneill int error;
206 1.11 jmcneill
207 1.11 jmcneill error = fdtbus_get_reg64(phandle, index, &addr, &size);
208 1.11 jmcneill if (error)
209 1.11 jmcneill return error;
210 1.11 jmcneill
211 1.11 jmcneill if (sizeof(bus_addr_t) == 4 && (addr + size) > 0x100000000)
212 1.11 jmcneill return ERANGE;
213 1.11 jmcneill
214 1.11 jmcneill if (paddr)
215 1.11 jmcneill *paddr = (bus_addr_t)addr;
216 1.11 jmcneill if (psize)
217 1.11 jmcneill *psize = (bus_size_t)size;
218 1.11 jmcneill
219 1.11 jmcneill return 0;
220 1.11 jmcneill }
221 1.11 jmcneill
222 1.11 jmcneill int
223 1.11 jmcneill fdtbus_get_reg64(int phandle, u_int index, uint64_t *paddr, uint64_t *psize)
224 1.11 jmcneill {
225 1.11 jmcneill uint64_t addr, size;
226 1.7 jmcneill const uint8_t *buf;
227 1.1 jmcneill int len;
228 1.1 jmcneill
229 1.13 jmcneill const int addr_cells = fdtbus_get_addr_cells(OF_parent(phandle));
230 1.13 jmcneill const int size_cells = fdtbus_get_size_cells(OF_parent(phandle));
231 1.1 jmcneill if (addr_cells == -1 || size_cells == -1)
232 1.11 jmcneill return EINVAL;
233 1.1 jmcneill
234 1.7 jmcneill buf = fdt_getprop(fdtbus_get_data(),
235 1.7 jmcneill fdtbus_phandle2offset(phandle), "reg", &len);
236 1.7 jmcneill if (buf == NULL || len <= 0)
237 1.11 jmcneill return EINVAL;
238 1.7 jmcneill
239 1.1 jmcneill const u_int reglen = size_cells * 4 + addr_cells * 4;
240 1.1 jmcneill if (reglen == 0)
241 1.11 jmcneill return EINVAL;
242 1.1 jmcneill
243 1.7 jmcneill if (index >= len / reglen)
244 1.11 jmcneill return ENXIO;
245 1.1 jmcneill
246 1.13 jmcneill buf += index * reglen;
247 1.13 jmcneill addr = fdtbus_get_cells(buf, addr_cells);
248 1.13 jmcneill buf += addr_cells * 4;
249 1.13 jmcneill size = fdtbus_get_cells(buf, size_cells);
250 1.13 jmcneill
251 1.13 jmcneill if (paddr) {
252 1.13 jmcneill *paddr = fdtbus_decode_range(OF_parent(phandle), addr);
253 1.13 jmcneill const char *name = fdt_get_name(fdtbus_get_data(),
254 1.13 jmcneill fdtbus_phandle2offset(phandle), NULL);
255 1.13 jmcneill aprint_debug("fdt: [%s] decoded addr #%u: %llx -> %llx\n",
256 1.13 jmcneill name, index, addr, *paddr);
257 1.1 jmcneill }
258 1.1 jmcneill if (psize)
259 1.1 jmcneill *psize = size;
260 1.1 jmcneill
261 1.1 jmcneill return 0;
262 1.1 jmcneill }
263 1.8 jmcneill
264 1.12 jmcneill const struct fdt_console *
265 1.12 jmcneill fdtbus_get_console(void)
266 1.12 jmcneill {
267 1.12 jmcneill static const struct fdt_console_info *booted_console = NULL;
268 1.12 jmcneill
269 1.12 jmcneill if (booted_console == NULL) {
270 1.12 jmcneill __link_set_decl(fdt_consoles, struct fdt_console_info);
271 1.12 jmcneill struct fdt_console_info * const *info;
272 1.12 jmcneill const struct fdt_console_info *best_info = NULL;
273 1.12 jmcneill const int phandle = fdtbus_get_stdout_phandle();
274 1.12 jmcneill int best_match = 0;
275 1.12 jmcneill
276 1.12 jmcneill __link_set_foreach(info, fdt_consoles) {
277 1.12 jmcneill const int match = (*info)->ops->match(phandle);
278 1.12 jmcneill if (match > best_match) {
279 1.12 jmcneill best_match = match;
280 1.12 jmcneill best_info = *info;
281 1.12 jmcneill }
282 1.12 jmcneill }
283 1.12 jmcneill
284 1.12 jmcneill booted_console = best_info;
285 1.12 jmcneill }
286 1.12 jmcneill
287 1.12 jmcneill return booted_console == NULL ? NULL : booted_console->ops;
288 1.12 jmcneill }
289 1.12 jmcneill
290 1.8 jmcneill const char *
291 1.8 jmcneill fdtbus_get_stdout_path(void)
292 1.8 jmcneill {
293 1.9 jmcneill const char *prop;
294 1.9 jmcneill
295 1.8 jmcneill const int off = fdt_path_offset(fdtbus_get_data(), "/chosen");
296 1.8 jmcneill if (off < 0)
297 1.8 jmcneill return NULL;
298 1.9 jmcneill
299 1.9 jmcneill prop = fdt_getprop(fdtbus_get_data(), off, "stdout-path", NULL);
300 1.9 jmcneill if (prop != NULL)
301 1.9 jmcneill return prop;
302 1.9 jmcneill
303 1.9 jmcneill /* If the stdout-path property is not found, assume serial0 */
304 1.9 jmcneill return "serial0:115200n8";
305 1.8 jmcneill }
306 1.8 jmcneill
307 1.8 jmcneill int
308 1.8 jmcneill fdtbus_get_stdout_phandle(void)
309 1.8 jmcneill {
310 1.8 jmcneill const char *prop, *p;
311 1.8 jmcneill int off, len;
312 1.8 jmcneill
313 1.8 jmcneill prop = fdtbus_get_stdout_path();
314 1.8 jmcneill if (prop == NULL)
315 1.8 jmcneill return -1;
316 1.8 jmcneill
317 1.8 jmcneill p = strchr(prop, ':');
318 1.8 jmcneill len = p == NULL ? strlen(prop) : (p - prop);
319 1.8 jmcneill if (*prop != '/') {
320 1.8 jmcneill /* Alias */
321 1.8 jmcneill prop = fdt_get_alias_namelen(fdtbus_get_data(), prop, len);
322 1.8 jmcneill if (prop == NULL)
323 1.8 jmcneill return -1;
324 1.8 jmcneill len = strlen(prop);
325 1.8 jmcneill }
326 1.8 jmcneill off = fdt_path_offset_namelen(fdtbus_get_data(), prop, len);
327 1.8 jmcneill if (off < 0)
328 1.8 jmcneill return -1;
329 1.8 jmcneill
330 1.8 jmcneill return fdtbus_offset2phandle(off);
331 1.8 jmcneill }
332 1.8 jmcneill
333 1.8 jmcneill int
334 1.8 jmcneill fdtbus_get_stdout_speed(void)
335 1.8 jmcneill {
336 1.8 jmcneill const char *prop, *p;
337 1.8 jmcneill
338 1.8 jmcneill prop = fdtbus_get_stdout_path();
339 1.8 jmcneill if (prop == NULL)
340 1.8 jmcneill return -1;
341 1.8 jmcneill
342 1.8 jmcneill p = strchr(prop, ':');
343 1.8 jmcneill if (p == NULL)
344 1.8 jmcneill return -1;
345 1.8 jmcneill
346 1.8 jmcneill return (int)strtoul(p + 1, NULL, 10);
347 1.8 jmcneill }
348 1.10 jmcneill
349 1.12 jmcneill tcflag_t
350 1.12 jmcneill fdtbus_get_stdout_flags(void)
351 1.12 jmcneill {
352 1.12 jmcneill const char *prop, *p;
353 1.12 jmcneill tcflag_t flags = TTYDEF_CFLAG;
354 1.12 jmcneill char *ep;
355 1.12 jmcneill
356 1.12 jmcneill prop = fdtbus_get_stdout_path();
357 1.12 jmcneill if (prop == NULL)
358 1.12 jmcneill return flags;
359 1.12 jmcneill
360 1.12 jmcneill p = strchr(prop, ':');
361 1.12 jmcneill if (p == NULL)
362 1.12 jmcneill return flags;
363 1.12 jmcneill
364 1.12 jmcneill ep = NULL;
365 1.12 jmcneill (void)strtoul(p + 1, &ep, 10);
366 1.12 jmcneill if (ep == NULL)
367 1.12 jmcneill return flags;
368 1.12 jmcneill
369 1.12 jmcneill /* <baud>{<parity>{<bits>{<flow>}}} */
370 1.12 jmcneill while (*ep) {
371 1.12 jmcneill switch (*ep) {
372 1.12 jmcneill /* parity */
373 1.12 jmcneill case 'n': flags &= ~(PARENB|PARODD); break;
374 1.12 jmcneill case 'e': flags &= ~PARODD; flags |= PARENB; break;
375 1.12 jmcneill case 'o': flags |= (PARENB|PARODD); break;
376 1.12 jmcneill /* bits */
377 1.12 jmcneill case '5': flags &= ~CSIZE; flags |= CS5; break;
378 1.12 jmcneill case '6': flags &= ~CSIZE; flags |= CS6; break;
379 1.12 jmcneill case '7': flags &= ~CSIZE; flags |= CS7; break;
380 1.12 jmcneill case '8': flags &= ~CSIZE; flags |= CS8; break;
381 1.12 jmcneill /* flow */
382 1.12 jmcneill case 'r': flags |= CRTSCTS; break;
383 1.12 jmcneill }
384 1.12 jmcneill ep++;
385 1.12 jmcneill }
386 1.12 jmcneill
387 1.12 jmcneill return flags;
388 1.12 jmcneill }
389 1.12 jmcneill
390 1.10 jmcneill bool
391 1.10 jmcneill fdtbus_status_okay(int phandle)
392 1.10 jmcneill {
393 1.10 jmcneill const int off = fdtbus_phandle2offset(phandle);
394 1.10 jmcneill
395 1.10 jmcneill const char *prop = fdt_getprop(fdtbus_get_data(), off, "status", NULL);
396 1.10 jmcneill if (prop == NULL)
397 1.10 jmcneill return true;
398 1.10 jmcneill
399 1.10 jmcneill return strncmp(prop, "ok", 2) == 0;
400 1.10 jmcneill }
401 1.14 jmcneill
402 1.15 jmcneill const void *
403 1.15 jmcneill fdtbus_get_prop(int phandle, const char *prop, int *plen)
404 1.15 jmcneill {
405 1.15 jmcneill const int off = fdtbus_phandle2offset(phandle);
406 1.15 jmcneill
407 1.15 jmcneill return fdt_getprop(fdtbus_get_data(), off, prop, plen);
408 1.15 jmcneill }
409 1.15 jmcneill
410 1.14 jmcneill const char *
411 1.14 jmcneill fdtbus_get_string(int phandle, const char *prop)
412 1.14 jmcneill {
413 1.14 jmcneill const int off = fdtbus_phandle2offset(phandle);
414 1.14 jmcneill
415 1.14 jmcneill return fdt_getprop(fdtbus_get_data(), off, prop, NULL);
416 1.14 jmcneill }
417 1.16 jmcneill
418 1.16 jmcneill const char *
419 1.16 jmcneill fdtbus_get_string_index(int phandle, const char *prop, u_int index)
420 1.16 jmcneill {
421 1.16 jmcneill const char *names, *name;
422 1.16 jmcneill int len, cur;
423 1.16 jmcneill
424 1.16 jmcneill if ((len = OF_getproplen(phandle, prop)) < 0)
425 1.16 jmcneill return NULL;
426 1.16 jmcneill
427 1.16 jmcneill names = fdtbus_get_string(phandle, prop);
428 1.16 jmcneill
429 1.16 jmcneill for (name = names, cur = 0; len > 0;
430 1.16 jmcneill name += strlen(name) + 1, cur++) {
431 1.16 jmcneill if (index == cur)
432 1.16 jmcneill return name;
433 1.16 jmcneill }
434 1.16 jmcneill
435 1.16 jmcneill return NULL;
436 1.16 jmcneill }
437