fdt_subr.c revision 1.41 1 1.41 thorpej /* $NetBSD: fdt_subr.c,v 1.41 2025/09/06 22:53:49 thorpej 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.41 thorpej __KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.41 2025/09/06 22:53:49 thorpej Exp $");
31 1.24 skrll
32 1.24 skrll #include "opt_fdt.h"
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <libfdt.h>
38 1.1 jmcneill #include <dev/fdt/fdtvar.h>
39 1.33 thorpej #include <dev/fdt/fdt_private.h>
40 1.1 jmcneill
41 1.38 jmcneill #ifndef FDT_DEFAULT_STDOUT_PATH
42 1.38 jmcneill #define FDT_DEFAULT_STDOUT_PATH "serial0:115200n8"
43 1.38 jmcneill #endif
44 1.38 jmcneill
45 1.3 jmcneill static const void *fdt_data;
46 1.3 jmcneill
47 1.3 jmcneill bool
48 1.32 thorpej fdtbus_init(const void *data)
49 1.3 jmcneill {
50 1.3 jmcneill KASSERT(fdt_data == NULL);
51 1.3 jmcneill if (fdt_check_header(data) != 0) {
52 1.3 jmcneill return false;
53 1.3 jmcneill }
54 1.3 jmcneill fdt_data = data;
55 1.33 thorpej
56 1.3 jmcneill return true;
57 1.3 jmcneill }
58 1.3 jmcneill
59 1.3 jmcneill const void *
60 1.3 jmcneill fdtbus_get_data(void)
61 1.3 jmcneill {
62 1.3 jmcneill return fdt_data;
63 1.3 jmcneill }
64 1.3 jmcneill
65 1.3 jmcneill int
66 1.3 jmcneill fdtbus_offset2phandle(int offset)
67 1.3 jmcneill {
68 1.3 jmcneill if (offset < 0)
69 1.3 jmcneill return 0;
70 1.3 jmcneill
71 1.3 jmcneill return offset + fdt_off_dt_struct(fdt_data);
72 1.3 jmcneill }
73 1.3 jmcneill
74 1.3 jmcneill int
75 1.3 jmcneill fdtbus_phandle2offset(int phandle)
76 1.3 jmcneill {
77 1.3 jmcneill const int dtoff = fdt_off_dt_struct(fdt_data);
78 1.3 jmcneill
79 1.3 jmcneill if (phandle == -1)
80 1.3 jmcneill phandle = dtoff;
81 1.3 jmcneill
82 1.3 jmcneill if (phandle < dtoff)
83 1.3 jmcneill return -1;
84 1.3 jmcneill
85 1.3 jmcneill return phandle - dtoff;
86 1.3 jmcneill }
87 1.3 jmcneill
88 1.20 skrll static bool fdtbus_decoderegprop = true;
89 1.20 skrll
90 1.20 skrll void
91 1.20 skrll fdtbus_set_decoderegprop(bool decode)
92 1.20 skrll {
93 1.20 skrll fdtbus_decoderegprop = decode;
94 1.20 skrll }
95 1.3 jmcneill
96 1.34 jmcneill int
97 1.1 jmcneill fdtbus_get_addr_cells(int phandle)
98 1.1 jmcneill {
99 1.4 jmcneill uint32_t addr_cells;
100 1.1 jmcneill
101 1.13 jmcneill if (of_getprop_uint32(phandle, "#address-cells", &addr_cells))
102 1.1 jmcneill addr_cells = 2;
103 1.1 jmcneill
104 1.1 jmcneill return addr_cells;
105 1.1 jmcneill }
106 1.1 jmcneill
107 1.34 jmcneill int
108 1.1 jmcneill fdtbus_get_size_cells(int phandle)
109 1.1 jmcneill {
110 1.4 jmcneill uint32_t size_cells;
111 1.1 jmcneill
112 1.13 jmcneill if (of_getprop_uint32(phandle, "#size-cells", &size_cells))
113 1.1 jmcneill size_cells = 0;
114 1.1 jmcneill
115 1.1 jmcneill return size_cells;
116 1.1 jmcneill }
117 1.1 jmcneill
118 1.1 jmcneill int
119 1.1 jmcneill fdtbus_get_phandle(int phandle, const char *prop)
120 1.1 jmcneill {
121 1.1 jmcneill u_int phandle_ref;
122 1.17 jmcneill const u_int *buf;
123 1.1 jmcneill int len;
124 1.1 jmcneill
125 1.17 jmcneill buf = fdt_getprop(fdtbus_get_data(),
126 1.17 jmcneill fdtbus_phandle2offset(phandle), prop, &len);
127 1.17 jmcneill if (buf == NULL || len < sizeof(phandle_ref))
128 1.1 jmcneill return -1;
129 1.1 jmcneill
130 1.17 jmcneill phandle_ref = be32dec(buf);
131 1.1 jmcneill
132 1.5 jmcneill return fdtbus_get_phandle_from_native(phandle_ref);
133 1.5 jmcneill }
134 1.5 jmcneill
135 1.5 jmcneill int
136 1.30 hkenken fdtbus_get_phandle_with_data(int phandle, const char *prop, const char *cells,
137 1.30 hkenken int index, struct fdt_phandle_data *data)
138 1.30 hkenken {
139 1.30 hkenken int len;
140 1.30 hkenken const int offset = 1;
141 1.30 hkenken
142 1.30 hkenken const u_int *p = fdtbus_get_prop(phandle, prop, &len);
143 1.30 hkenken if (p == NULL || len <= 0)
144 1.30 hkenken return EINVAL;
145 1.30 hkenken
146 1.30 hkenken for (int i = 0; len > 0; i++) {
147 1.30 hkenken u_int phandle_ref = be32toh(*p);
148 1.30 hkenken const u_int iparent = fdtbus_get_phandle_from_native(phandle_ref);
149 1.30 hkenken uint32_t cells_num;
150 1.30 hkenken of_getprop_uint32(iparent, cells, &cells_num);
151 1.30 hkenken
152 1.30 hkenken if (index == i) {
153 1.30 hkenken if (data != NULL) {
154 1.30 hkenken data->phandle = iparent;
155 1.30 hkenken data->count = cells_num;
156 1.30 hkenken data->values = p + offset;
157 1.30 hkenken }
158 1.30 hkenken goto done;
159 1.30 hkenken }
160 1.30 hkenken
161 1.30 hkenken const u_int reclen = offset + cells_num;
162 1.30 hkenken len -= reclen * sizeof(u_int);
163 1.30 hkenken p += reclen;
164 1.30 hkenken }
165 1.30 hkenken return EINVAL;
166 1.30 hkenken
167 1.30 hkenken done:
168 1.30 hkenken return 0;
169 1.30 hkenken }
170 1.30 hkenken
171 1.30 hkenken int
172 1.5 jmcneill fdtbus_get_phandle_from_native(int phandle)
173 1.5 jmcneill {
174 1.5 jmcneill const int off = fdt_node_offset_by_phandle(fdt_data, phandle);
175 1.1 jmcneill if (off < 0) {
176 1.1 jmcneill return -1;
177 1.1 jmcneill }
178 1.3 jmcneill return fdtbus_offset2phandle(off);
179 1.1 jmcneill }
180 1.1 jmcneill
181 1.6 jmcneill bool
182 1.6 jmcneill fdtbus_get_path(int phandle, char *buf, size_t buflen)
183 1.6 jmcneill {
184 1.6 jmcneill const int off = fdtbus_phandle2offset(phandle);
185 1.6 jmcneill if (off < 0) {
186 1.6 jmcneill return false;
187 1.6 jmcneill }
188 1.6 jmcneill if (fdt_get_path(fdt_data, off, buf, (int)buflen) != 0) {
189 1.6 jmcneill return false;
190 1.6 jmcneill }
191 1.6 jmcneill return true;
192 1.6 jmcneill }
193 1.6 jmcneill
194 1.34 jmcneill uint64_t
195 1.13 jmcneill fdtbus_get_cells(const uint8_t *buf, int cells)
196 1.13 jmcneill {
197 1.13 jmcneill switch (cells) {
198 1.13 jmcneill case 0: return 0;
199 1.13 jmcneill case 1: return be32dec(buf);
200 1.22 jakllsch case 2: return ((uint64_t)be32dec(buf)<<32)|be32dec(buf+4);
201 1.13 jmcneill default: panic("fdtbus_get_cells: bad cells val %d\n", cells);
202 1.13 jmcneill }
203 1.13 jmcneill }
204 1.13 jmcneill
205 1.13 jmcneill static uint64_t
206 1.13 jmcneill fdtbus_decode_range(int phandle, uint64_t paddr)
207 1.13 jmcneill {
208 1.13 jmcneill const int parent = OF_parent(phandle);
209 1.13 jmcneill if (parent == -1)
210 1.13 jmcneill return paddr;
211 1.20 skrll
212 1.20 skrll if (!fdtbus_decoderegprop)
213 1.20 skrll return paddr;
214 1.20 skrll
215 1.13 jmcneill const uint8_t *buf;
216 1.13 jmcneill int len;
217 1.13 jmcneill
218 1.13 jmcneill buf = fdt_getprop(fdtbus_get_data(),
219 1.13 jmcneill fdtbus_phandle2offset(phandle), "ranges", &len);
220 1.13 jmcneill if (buf == NULL)
221 1.13 jmcneill return paddr;
222 1.13 jmcneill
223 1.13 jmcneill if (len == 0) {
224 1.13 jmcneill /* pass through to parent */
225 1.13 jmcneill return fdtbus_decode_range(parent, paddr);
226 1.13 jmcneill }
227 1.13 jmcneill
228 1.13 jmcneill const int addr_cells = fdtbus_get_addr_cells(phandle);
229 1.13 jmcneill const int size_cells = fdtbus_get_size_cells(phandle);
230 1.31 jmcneill const int paddr_cells = fdtbus_get_addr_cells(parent);
231 1.13 jmcneill if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
232 1.13 jmcneill return paddr;
233 1.13 jmcneill
234 1.13 jmcneill while (len > 0) {
235 1.13 jmcneill uint64_t cba, pba, cl;
236 1.13 jmcneill cba = fdtbus_get_cells(buf, addr_cells);
237 1.13 jmcneill buf += addr_cells * 4;
238 1.13 jmcneill pba = fdtbus_get_cells(buf, paddr_cells);
239 1.13 jmcneill buf += paddr_cells * 4;
240 1.13 jmcneill cl = fdtbus_get_cells(buf, size_cells);
241 1.13 jmcneill buf += size_cells * 4;
242 1.13 jmcneill
243 1.31 jmcneill #ifdef FDTBUS_DEBUG
244 1.35 rin printf("%s: %s: cba=%#" PRIx64 ", pba=%#" PRIx64 ", cl=%#" PRIx64 "\n", __func__, fdt_get_name(fdtbus_get_data(), fdtbus_phandle2offset(phandle), NULL), cba, pba, cl);
245 1.31 jmcneill #endif
246 1.31 jmcneill
247 1.13 jmcneill if (paddr >= cba && paddr < cba + cl)
248 1.13 jmcneill return fdtbus_decode_range(parent, pba) + (paddr - cba);
249 1.13 jmcneill
250 1.13 jmcneill len -= (addr_cells + paddr_cells + size_cells) * 4;
251 1.13 jmcneill }
252 1.13 jmcneill
253 1.13 jmcneill /* No mapping found */
254 1.13 jmcneill return paddr;
255 1.13 jmcneill }
256 1.13 jmcneill
257 1.1 jmcneill int
258 1.18 jmcneill fdtbus_get_reg_byname(int phandle, const char *name, bus_addr_t *paddr,
259 1.18 jmcneill bus_size_t *psize)
260 1.18 jmcneill {
261 1.18 jmcneill u_int index;
262 1.29 jakllsch int error;
263 1.18 jmcneill
264 1.29 jakllsch error = fdtbus_get_index(phandle, "reg-names", name, &index);
265 1.29 jakllsch if (error != 0)
266 1.29 jakllsch return ENOENT;
267 1.18 jmcneill
268 1.29 jakllsch return fdtbus_get_reg(phandle, index, paddr, psize);
269 1.18 jmcneill }
270 1.18 jmcneill
271 1.18 jmcneill int
272 1.1 jmcneill fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
273 1.1 jmcneill {
274 1.11 jmcneill uint64_t addr, size;
275 1.11 jmcneill int error;
276 1.11 jmcneill
277 1.11 jmcneill error = fdtbus_get_reg64(phandle, index, &addr, &size);
278 1.11 jmcneill if (error)
279 1.11 jmcneill return error;
280 1.11 jmcneill
281 1.11 jmcneill if (sizeof(bus_addr_t) == 4 && (addr + size) > 0x100000000)
282 1.11 jmcneill return ERANGE;
283 1.11 jmcneill
284 1.11 jmcneill if (paddr)
285 1.11 jmcneill *paddr = (bus_addr_t)addr;
286 1.11 jmcneill if (psize)
287 1.11 jmcneill *psize = (bus_size_t)size;
288 1.11 jmcneill
289 1.11 jmcneill return 0;
290 1.11 jmcneill }
291 1.11 jmcneill
292 1.11 jmcneill int
293 1.11 jmcneill fdtbus_get_reg64(int phandle, u_int index, uint64_t *paddr, uint64_t *psize)
294 1.11 jmcneill {
295 1.11 jmcneill uint64_t addr, size;
296 1.7 jmcneill const uint8_t *buf;
297 1.1 jmcneill int len;
298 1.1 jmcneill
299 1.13 jmcneill const int addr_cells = fdtbus_get_addr_cells(OF_parent(phandle));
300 1.13 jmcneill const int size_cells = fdtbus_get_size_cells(OF_parent(phandle));
301 1.1 jmcneill if (addr_cells == -1 || size_cells == -1)
302 1.11 jmcneill return EINVAL;
303 1.1 jmcneill
304 1.7 jmcneill buf = fdt_getprop(fdtbus_get_data(),
305 1.7 jmcneill fdtbus_phandle2offset(phandle), "reg", &len);
306 1.7 jmcneill if (buf == NULL || len <= 0)
307 1.11 jmcneill return EINVAL;
308 1.7 jmcneill
309 1.1 jmcneill const u_int reglen = size_cells * 4 + addr_cells * 4;
310 1.1 jmcneill if (reglen == 0)
311 1.11 jmcneill return EINVAL;
312 1.1 jmcneill
313 1.7 jmcneill if (index >= len / reglen)
314 1.11 jmcneill return ENXIO;
315 1.1 jmcneill
316 1.13 jmcneill buf += index * reglen;
317 1.13 jmcneill addr = fdtbus_get_cells(buf, addr_cells);
318 1.13 jmcneill buf += addr_cells * 4;
319 1.13 jmcneill size = fdtbus_get_cells(buf, size_cells);
320 1.13 jmcneill
321 1.13 jmcneill if (paddr) {
322 1.13 jmcneill *paddr = fdtbus_decode_range(OF_parent(phandle), addr);
323 1.27 jmcneill #ifdef FDTBUS_DEBUG
324 1.13 jmcneill const char *name = fdt_get_name(fdtbus_get_data(),
325 1.13 jmcneill fdtbus_phandle2offset(phandle), NULL);
326 1.27 jmcneill printf("fdt: [%s] decoded addr #%u: %" PRIx64
327 1.23 christos " -> %" PRIx64 "\n", name, index, addr, *paddr);
328 1.27 jmcneill #endif
329 1.1 jmcneill }
330 1.1 jmcneill if (psize)
331 1.1 jmcneill *psize = size;
332 1.1 jmcneill
333 1.1 jmcneill return 0;
334 1.1 jmcneill }
335 1.8 jmcneill
336 1.10 jmcneill bool
337 1.10 jmcneill fdtbus_status_okay(int phandle)
338 1.10 jmcneill {
339 1.10 jmcneill const int off = fdtbus_phandle2offset(phandle);
340 1.10 jmcneill
341 1.10 jmcneill const char *prop = fdt_getprop(fdtbus_get_data(), off, "status", NULL);
342 1.10 jmcneill if (prop == NULL)
343 1.10 jmcneill return true;
344 1.10 jmcneill
345 1.10 jmcneill return strncmp(prop, "ok", 2) == 0;
346 1.10 jmcneill }
347 1.14 jmcneill
348 1.15 jmcneill const void *
349 1.15 jmcneill fdtbus_get_prop(int phandle, const char *prop, int *plen)
350 1.15 jmcneill {
351 1.15 jmcneill const int off = fdtbus_phandle2offset(phandle);
352 1.15 jmcneill
353 1.15 jmcneill return fdt_getprop(fdtbus_get_data(), off, prop, plen);
354 1.15 jmcneill }
355 1.15 jmcneill
356 1.14 jmcneill const char *
357 1.14 jmcneill fdtbus_get_string(int phandle, const char *prop)
358 1.14 jmcneill {
359 1.14 jmcneill const int off = fdtbus_phandle2offset(phandle);
360 1.14 jmcneill
361 1.19 jmcneill if (strcmp(prop, "name") == 0)
362 1.19 jmcneill return fdt_get_name(fdtbus_get_data(), off, NULL);
363 1.19 jmcneill else
364 1.19 jmcneill return fdt_getprop(fdtbus_get_data(), off, prop, NULL);
365 1.14 jmcneill }
366 1.16 jmcneill
367 1.16 jmcneill const char *
368 1.16 jmcneill fdtbus_get_string_index(int phandle, const char *prop, u_int index)
369 1.16 jmcneill {
370 1.39 thorpej const char *names;
371 1.39 thorpej int len;
372 1.16 jmcneill
373 1.16 jmcneill if ((len = OF_getproplen(phandle, prop)) < 0)
374 1.16 jmcneill return NULL;
375 1.16 jmcneill
376 1.16 jmcneill names = fdtbus_get_string(phandle, prop);
377 1.16 jmcneill
378 1.39 thorpej return strlist_string(names, len, index);
379 1.16 jmcneill }
380 1.29 jakllsch
381 1.29 jakllsch int
382 1.29 jakllsch fdtbus_get_index(int phandle, const char *prop, const char *name, u_int *idx)
383 1.29 jakllsch {
384 1.29 jakllsch const char *p;
385 1.39 thorpej int len, index;
386 1.29 jakllsch
387 1.29 jakllsch p = fdtbus_get_prop(phandle, prop, &len);
388 1.29 jakllsch if (p == NULL || len <= 0)
389 1.29 jakllsch return -1;
390 1.29 jakllsch
391 1.39 thorpej index = strlist_index(p, len, name);
392 1.39 thorpej if (index == -1)
393 1.39 thorpej return -1;
394 1.29 jakllsch
395 1.39 thorpej *idx = index;
396 1.39 thorpej return 0;
397 1.29 jakllsch }
398