ofw_subr.c revision 1.56 1 1.56 thorpej /* $NetBSD: ofw_subr.c,v 1.56 2021/02/04 20:19:09 thorpej Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright 1998
5 1.1 cgd * Digital Equipment Corporation. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This software is furnished under license and may be used and
8 1.1 cgd * copied only in accordance with the following terms and conditions.
9 1.1 cgd * Subject to these conditions, you may download, copy, install,
10 1.1 cgd * use, modify and distribute this software in source and/or binary
11 1.1 cgd * form. No title or ownership is transferred hereby.
12 1.1 cgd *
13 1.1 cgd * 1) Any source code used, modified or distributed must reproduce
14 1.1 cgd * and retain this copyright notice and list of conditions as
15 1.1 cgd * they appear in the source file.
16 1.1 cgd *
17 1.1 cgd * 2) No right is granted to use any trade name, trademark, or logo of
18 1.1 cgd * Digital Equipment Corporation. Neither the "Digital Equipment
19 1.1 cgd * Corporation" name nor any trademark or logo of Digital Equipment
20 1.1 cgd * Corporation may be used to endorse or promote products derived
21 1.1 cgd * from this software without the prior written permission of
22 1.1 cgd * Digital Equipment Corporation.
23 1.1 cgd *
24 1.1 cgd * 3) This software is provided "AS-IS" and any express or implied
25 1.1 cgd * warranties, including but not limited to, any implied warranties
26 1.1 cgd * of merchantability, fitness for a particular purpose, or
27 1.1 cgd * non-infringement are disclaimed. In no event shall DIGITAL be
28 1.1 cgd * liable for any damages whatsoever, and in particular, DIGITAL
29 1.1 cgd * shall not be liable for special, indirect, consequential, or
30 1.1 cgd * incidental damages or damages for lost profits, loss of
31 1.1 cgd * revenue or loss of use, whether such damages arise in contract,
32 1.1 cgd * negligence, tort, under statute, in equity, at law or otherwise,
33 1.1 cgd * even if advised of the possibility of such damage.
34 1.1 cgd */
35 1.7 lukem
36 1.7 lukem #include <sys/cdefs.h>
37 1.56 thorpej __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.56 2021/02/04 20:19:09 thorpej Exp $");
38 1.1 cgd
39 1.2 cgd #include <sys/param.h>
40 1.42 thorpej #include <sys/device.h>
41 1.46 thorpej #include <sys/kmem.h>
42 1.2 cgd #include <sys/systm.h>
43 1.2 cgd #include <dev/ofw/openfirm.h>
44 1.2 cgd
45 1.3 cgd #define OFW_MAX_STACK_BUF_SIZE 256
46 1.3 cgd #define OFW_PATH_BUF_SIZE 512
47 1.3 cgd
48 1.1 cgd /*
49 1.3 cgd * int of_decode_int(p)
50 1.3 cgd *
51 1.3 cgd * This routine converts OFW encoded-int datums
52 1.3 cgd * into the integer format of the host machine.
53 1.1 cgd *
54 1.3 cgd * It is primarily used to convert integer properties
55 1.3 cgd * returned by the OF_getprop routine.
56 1.2 cgd *
57 1.2 cgd * Arguments:
58 1.2 cgd * p pointer to unsigned char array which is an
59 1.2 cgd * OFW-encoded integer.
60 1.2 cgd *
61 1.2 cgd * Return Value:
62 1.2 cgd * Decoded integer value of argument p.
63 1.3 cgd *
64 1.3 cgd * Side Effects:
65 1.3 cgd * None.
66 1.1 cgd */
67 1.1 cgd int
68 1.14 dsl of_decode_int(const unsigned char *p)
69 1.1 cgd {
70 1.1 cgd unsigned int i = *p++ << 8;
71 1.1 cgd i = (i + *p++) << 8;
72 1.1 cgd i = (i + *p++) << 8;
73 1.1 cgd return (i + *p);
74 1.2 cgd }
75 1.2 cgd
76 1.2 cgd /*
77 1.3 cgd * int of_compatible(phandle, strings)
78 1.3 cgd *
79 1.2 cgd * This routine checks an OFW node's "compatible" entry to see if
80 1.2 cgd * it matches any of the provided strings.
81 1.2 cgd *
82 1.54 thorpej * of_compatible_match() is the preferred way to perform driver
83 1.52 thorpej * compatibility match. However, this routine that deals with
84 1.52 thorpej * only strings is useful in some situations and is provided for
85 1.52 thorpej * convenience.
86 1.2 cgd *
87 1.2 cgd * Arguments:
88 1.2 cgd * phandle OFW phandle of device to be checked for
89 1.2 cgd * compatibility.
90 1.2 cgd * strings Array of containing expected "compatibility"
91 1.2 cgd * property values, presence of any of which
92 1.2 cgd * indicates compatibility.
93 1.2 cgd *
94 1.2 cgd * Return Value:
95 1.52 thorpej * 0 if none of the strings are found in phandle's "compatibility"
96 1.24 jmcneill * property, or the reverse index of the matching string in the
97 1.52 thorpej * phandle's "compatibility" property plus 1.
98 1.3 cgd *
99 1.3 cgd * Side Effects:
100 1.3 cgd * None.
101 1.2 cgd */
102 1.2 cgd int
103 1.12 garbled of_compatible(int phandle, const char * const *strings)
104 1.2 cgd {
105 1.47 thorpej char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
106 1.47 thorpej const char *cp;
107 1.52 thorpej int proplen, match = 0;
108 1.24 jmcneill
109 1.47 thorpej proplen = OF_getproplen(phandle, "compatible");
110 1.47 thorpej if (proplen <= 0) {
111 1.52 thorpej return 0;
112 1.38 rin }
113 1.2 cgd
114 1.47 thorpej prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
115 1.47 thorpej
116 1.47 thorpej if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
117 1.2 cgd goto out;
118 1.2 cgd }
119 1.2 cgd
120 1.47 thorpej for (; (cp = *strings) != NULL; strings++) {
121 1.47 thorpej if ((match = strlist_match(prop, proplen, cp)) != 0) {
122 1.47 thorpej break;
123 1.47 thorpej }
124 1.47 thorpej }
125 1.47 thorpej
126 1.47 thorpej out:
127 1.47 thorpej kmem_tmpbuf_free(prop, proplen, propbuf);
128 1.52 thorpej return match;
129 1.24 jmcneill }
130 1.10 perry
131 1.24 jmcneill /*
132 1.54 thorpej * int of_compatible_match(phandle, compat_data)
133 1.30 jmcneill *
134 1.51 thorpej * This routine searches an array of device_compatible_entry structures
135 1.51 thorpej * for a matching "compatible" entry matching the supplied OFW node,
136 1.51 thorpej * and returns a weighted match value corresponding to which string
137 1.51 thorpej * from the "compatible" property was matched, which more weight given
138 1.51 thorpej * to the first string than the last.
139 1.30 jmcneill *
140 1.30 jmcneill * It should be used when determining whether a driver can drive
141 1.30 jmcneill * a particular device.
142 1.30 jmcneill *
143 1.30 jmcneill * Arguments:
144 1.30 jmcneill * phandle OFW phandle of device to be checked for
145 1.30 jmcneill * compatibility.
146 1.30 jmcneill * compat_data Array of possible compat entry strings and
147 1.30 jmcneill * associated metadata. The last entry in the
148 1.30 jmcneill * list should have a "compat" of NULL to terminate
149 1.30 jmcneill * the list.
150 1.30 jmcneill *
151 1.30 jmcneill * Return Value:
152 1.30 jmcneill * 0 if none of the strings are found in phandle's "compatibility"
153 1.30 jmcneill * property, or a positive number based on the reverse index of the
154 1.30 jmcneill * matching string in the phandle's "compatibility" property, plus 1.
155 1.30 jmcneill *
156 1.30 jmcneill * Side Effects:
157 1.30 jmcneill * None.
158 1.30 jmcneill */
159 1.30 jmcneill int
160 1.54 thorpej of_compatible_match(int phandle,
161 1.42 thorpej const struct device_compatible_entry *compat_data)
162 1.30 jmcneill {
163 1.46 thorpej char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
164 1.46 thorpej int proplen, match = 0;
165 1.46 thorpej
166 1.46 thorpej proplen = OF_getproplen(phandle, "compatible");
167 1.46 thorpej if (proplen <= 0) {
168 1.46 thorpej return 0;
169 1.46 thorpej }
170 1.46 thorpej
171 1.46 thorpej prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
172 1.46 thorpej
173 1.46 thorpej if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
174 1.46 thorpej goto out;
175 1.30 jmcneill }
176 1.46 thorpej
177 1.46 thorpej match = device_compatible_match_strlist(prop, proplen, compat_data);
178 1.46 thorpej
179 1.46 thorpej out:
180 1.46 thorpej kmem_tmpbuf_free(prop, proplen, propbuf);
181 1.46 thorpej return match;
182 1.30 jmcneill }
183 1.30 jmcneill
184 1.30 jmcneill /*
185 1.54 thorpej * const struct device_compatible_entry *of_compatible_lookup(phandle,
186 1.43 jmcneill * compat_data)
187 1.29 jmcneill *
188 1.51 thorpej * This routine searches an array of device_compatible_entry structures
189 1.51 thorpej * for a "compatible" entry matching the supplied OFW node.
190 1.29 jmcneill *
191 1.29 jmcneill * Arguments:
192 1.29 jmcneill * phandle OFW phandle of device to be checked for
193 1.29 jmcneill * compatibility.
194 1.29 jmcneill * compat_data Array of possible compat entry strings and
195 1.29 jmcneill * associated metadata. The last entry in the
196 1.29 jmcneill * list should have a "compat" of NULL to terminate
197 1.29 jmcneill * the list.
198 1.29 jmcneill *
199 1.29 jmcneill * Return Value:
200 1.29 jmcneill * The first matching compat_data entry in the array. If no matches
201 1.46 thorpej * are found, NULL is returned.
202 1.29 jmcneill *
203 1.29 jmcneill * Side Effects:
204 1.29 jmcneill * None.
205 1.29 jmcneill */
206 1.42 thorpej const struct device_compatible_entry *
207 1.54 thorpej of_compatible_lookup(int phandle,
208 1.42 thorpej const struct device_compatible_entry *compat_data)
209 1.29 jmcneill {
210 1.46 thorpej char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
211 1.46 thorpej const struct device_compatible_entry *match = NULL;
212 1.46 thorpej int proplen;
213 1.46 thorpej
214 1.46 thorpej proplen = OF_getproplen(phandle, "compatible");
215 1.46 thorpej if (proplen <= 0) {
216 1.46 thorpej return 0;
217 1.29 jmcneill }
218 1.46 thorpej
219 1.46 thorpej prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
220 1.46 thorpej
221 1.46 thorpej if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
222 1.46 thorpej goto out;
223 1.46 thorpej }
224 1.46 thorpej
225 1.46 thorpej match = device_compatible_lookup_strlist(prop, proplen, compat_data);
226 1.46 thorpej
227 1.46 thorpej out:
228 1.46 thorpej kmem_tmpbuf_free(prop, proplen, propbuf);
229 1.46 thorpej return match;
230 1.29 jmcneill }
231 1.29 jmcneill
232 1.29 jmcneill /*
233 1.4 cgd * int of_packagename(phandle, buf, bufsize)
234 1.3 cgd *
235 1.3 cgd * This routine places the last component of an OFW node's name
236 1.3 cgd * into a user-provided buffer.
237 1.3 cgd *
238 1.3 cgd * It can be used during autoconfiguration to make printing of
239 1.3 cgd * device names more informative.
240 1.3 cgd *
241 1.3 cgd * Arguments:
242 1.3 cgd * phandle OFW phandle of device whose name name is
243 1.3 cgd * desired.
244 1.3 cgd * buf Buffer to contain device name, provided by
245 1.3 cgd * caller. (For now, must be at least 4
246 1.3 cgd * bytes long.)
247 1.3 cgd * bufsize Length of buffer referenced by 'buf', in
248 1.3 cgd * bytes.
249 1.3 cgd *
250 1.3 cgd * Return Value:
251 1.3 cgd * -1 if the device path name could not be obtained or would
252 1.3 cgd * not fit in the allocated temporary buffer, or zero otherwise
253 1.6 soren * (meaning that the leaf node name was successfully extracted).
254 1.3 cgd *
255 1.3 cgd * Side Effects:
256 1.3 cgd * If the leaf node name was successfully extracted, 'buf' is
257 1.3 cgd * filled in with at most 'bufsize' bytes of the leaf node
258 1.3 cgd * name. If the leaf node was not successfully extracted, a
259 1.3 cgd * somewhat meaningful string is placed in the buffer. In
260 1.3 cgd * either case, the contents of 'buf' will be NUL-terminated.
261 1.3 cgd */
262 1.3 cgd int
263 1.12 garbled of_packagename(int phandle, char *buf, int bufsize)
264 1.3 cgd {
265 1.3 cgd char *pbuf;
266 1.3 cgd const char *lastslash;
267 1.3 cgd int l, rv;
268 1.3 cgd
269 1.48 thorpej pbuf = kmem_alloc(OFW_PATH_BUF_SIZE, KM_SLEEP);
270 1.3 cgd l = OF_package_to_path(phandle, pbuf, OFW_PATH_BUF_SIZE);
271 1.3 cgd
272 1.3 cgd /* check that we could get the name, and that it's not too long. */
273 1.3 cgd if (l < 0 ||
274 1.3 cgd (l == OFW_PATH_BUF_SIZE && pbuf[OFW_PATH_BUF_SIZE - 1] != '\0')) {
275 1.3 cgd if (bufsize >= 25)
276 1.9 itojun snprintf(buf, bufsize, "??? (phandle 0x%x)", phandle);
277 1.3 cgd else if (bufsize >= 4)
278 1.9 itojun strlcpy(buf, "???", bufsize);
279 1.3 cgd else
280 1.4 cgd panic("of_packagename: bufsize = %d is silly",
281 1.4 cgd bufsize);
282 1.3 cgd rv = -1;
283 1.3 cgd } else {
284 1.5 mycroft pbuf[l] = '\0';
285 1.3 cgd lastslash = strrchr(pbuf, '/');
286 1.9 itojun strlcpy(buf, (lastslash == NULL) ? pbuf : (lastslash + 1),
287 1.3 cgd bufsize);
288 1.3 cgd rv = 0;
289 1.3 cgd }
290 1.3 cgd
291 1.48 thorpej kmem_free(pbuf, OFW_PATH_BUF_SIZE);
292 1.3 cgd return (rv);
293 1.1 cgd }
294 1.12 garbled
295 1.12 garbled /*
296 1.13 garbled * Find the first child of a given node that matches name. Does not recurse.
297 1.12 garbled */
298 1.12 garbled int
299 1.12 garbled of_find_firstchild_byname(int node, const char *name)
300 1.12 garbled {
301 1.12 garbled char namex[32];
302 1.12 garbled int nn;
303 1.12 garbled
304 1.12 garbled for (nn = OF_child(node); nn; nn = OF_peer(nn)) {
305 1.12 garbled memset(namex, 0, sizeof(namex));
306 1.12 garbled if (OF_getprop(nn, "name", namex, sizeof(namex)) == -1)
307 1.12 garbled continue;
308 1.12 garbled if (strcmp(name, namex) == 0)
309 1.12 garbled return nn;
310 1.12 garbled }
311 1.12 garbled return -1;
312 1.12 garbled }
313 1.13 garbled
314 1.13 garbled /*
315 1.40 jmcneill * Find a child node that is compatible with str. Recurses, starting at node.
316 1.40 jmcneill */
317 1.40 jmcneill int
318 1.40 jmcneill of_find_bycompat(int node, const char *str)
319 1.40 jmcneill {
320 1.40 jmcneill const char * compatible[] = { str, NULL };
321 1.40 jmcneill int child, ret;
322 1.40 jmcneill
323 1.40 jmcneill for (child = OF_child(node); child; child = OF_peer(child)) {
324 1.53 thorpej if (of_compatible(child, compatible))
325 1.40 jmcneill return child;
326 1.40 jmcneill ret = of_find_bycompat(child, str);
327 1.40 jmcneill if (ret != -1)
328 1.40 jmcneill return ret;
329 1.40 jmcneill }
330 1.40 jmcneill
331 1.40 jmcneill return -1;
332 1.40 jmcneill }
333 1.40 jmcneill
334 1.40 jmcneill /*
335 1.13 garbled * Find a give node by name. Recurses, and seems to walk upwards too.
336 1.13 garbled */
337 1.13 garbled
338 1.13 garbled int
339 1.13 garbled of_getnode_byname(int start, const char *target)
340 1.13 garbled {
341 1.13 garbled int node, next;
342 1.13 garbled char name[64];
343 1.13 garbled
344 1.13 garbled if (start == 0)
345 1.13 garbled start = OF_peer(0);
346 1.13 garbled
347 1.13 garbled for (node = start; node; node = next) {
348 1.13 garbled memset(name, 0, sizeof name);
349 1.13 garbled OF_getprop(node, "name", name, sizeof name - 1);
350 1.13 garbled if (strcmp(name, target) == 0)
351 1.13 garbled break;
352 1.13 garbled
353 1.13 garbled if ((next = OF_child(node)) != 0)
354 1.13 garbled continue;
355 1.13 garbled
356 1.13 garbled while (node) {
357 1.13 garbled if ((next = OF_peer(node)) != 0)
358 1.13 garbled break;
359 1.13 garbled node = OF_parent(node);
360 1.13 garbled }
361 1.13 garbled }
362 1.13 garbled
363 1.13 garbled /* XXX is this correct? */
364 1.13 garbled return node;
365 1.13 garbled }
366 1.13 garbled
367 1.13 garbled /*
368 1.13 garbled * Create a uint32_t integer property from an OFW node property.
369 1.13 garbled */
370 1.13 garbled
371 1.50 mrg bool
372 1.13 garbled of_to_uint32_prop(prop_dictionary_t dict, int node, const char *ofname,
373 1.13 garbled const char *propname)
374 1.13 garbled {
375 1.13 garbled uint32_t prop;
376 1.13 garbled
377 1.13 garbled if (OF_getprop(node, ofname, &prop, sizeof(prop)) != sizeof(prop))
378 1.13 garbled return FALSE;
379 1.13 garbled
380 1.13 garbled return(prop_dictionary_set_uint32(dict, propname, prop));
381 1.13 garbled }
382 1.13 garbled
383 1.13 garbled /*
384 1.13 garbled * Create a data property from an OFW node property. Max size of 256bytes.
385 1.13 garbled */
386 1.13 garbled
387 1.50 mrg bool
388 1.13 garbled of_to_dataprop(prop_dictionary_t dict, int node, const char *ofname,
389 1.13 garbled const char *propname)
390 1.13 garbled {
391 1.13 garbled int len;
392 1.13 garbled uint8_t prop[256];
393 1.13 garbled
394 1.13 garbled len = OF_getprop(node, ofname, prop, 256);
395 1.13 garbled if (len < 1)
396 1.13 garbled return FALSE;
397 1.13 garbled
398 1.36 thorpej return prop_dictionary_set_data(dict, propname, prop, len);
399 1.13 garbled }
400 1.15 macallan
401 1.15 macallan /*
402 1.15 macallan * look at output-device, see if there's a Sun-typical video mode specifier as
403 1.15 macallan * in screen:r1024x768x60 attached. If found copy it into *buffer, otherwise
404 1.15 macallan * return NULL
405 1.15 macallan */
406 1.15 macallan
407 1.15 macallan char *
408 1.15 macallan of_get_mode_string(char *buffer, int len)
409 1.15 macallan {
410 1.15 macallan int options;
411 1.15 macallan char *pos, output_device[256];
412 1.15 macallan
413 1.15 macallan /*
414 1.15 macallan * finally, let's see if there's a video mode specified in
415 1.15 macallan * output-device and pass it on so there's at least some way
416 1.15 macallan * to program video modes
417 1.15 macallan */
418 1.15 macallan options = OF_finddevice("/options");
419 1.15 macallan if ((options == 0) || (options == -1))
420 1.15 macallan return NULL;
421 1.15 macallan if (OF_getprop(options, "output-device", output_device, 256) == 0)
422 1.15 macallan return NULL;
423 1.15 macallan
424 1.15 macallan /* find the mode string if there is one */
425 1.15 macallan pos = strstr(output_device, ":r");
426 1.15 macallan if (pos == NULL)
427 1.15 macallan return NULL;
428 1.15 macallan strncpy(buffer, pos + 2, len);
429 1.15 macallan return buffer;
430 1.15 macallan }
431 1.17 martin
432 1.17 martin /*
433 1.28 jmcneill * Returns true if the specified property is present.
434 1.27 jmcneill */
435 1.27 jmcneill bool
436 1.28 jmcneill of_hasprop(int node, const char *prop)
437 1.27 jmcneill {
438 1.27 jmcneill return OF_getproplen(node, prop) >= 0;
439 1.27 jmcneill }
440 1.27 jmcneill
441 1.27 jmcneill /*
442 1.27 jmcneill * Get the value of a uint32 property, compensating for host byte order.
443 1.27 jmcneill * Returns 0 on success, non-zero on failure.
444 1.27 jmcneill */
445 1.27 jmcneill int
446 1.27 jmcneill of_getprop_uint32(int node, const char *prop, uint32_t *val)
447 1.27 jmcneill {
448 1.27 jmcneill uint32_t v;
449 1.27 jmcneill int len;
450 1.27 jmcneill
451 1.27 jmcneill len = OF_getprop(node, prop, &v, sizeof(v));
452 1.27 jmcneill if (len != sizeof(v))
453 1.27 jmcneill return -1;
454 1.27 jmcneill
455 1.27 jmcneill *val = be32toh(v);
456 1.27 jmcneill return 0;
457 1.27 jmcneill }
458 1.32 jmcneill
459 1.41 ryo int
460 1.41 ryo of_getprop_uint32_array(int node, const char *prop, uint32_t *array, int n)
461 1.41 ryo {
462 1.41 ryo uint32_t *v = array;
463 1.41 ryo int len;
464 1.41 ryo
465 1.41 ryo len = OF_getprop(node, prop, array, n * sizeof(*v));
466 1.41 ryo if (len < (int)(n * sizeof(*v)))
467 1.41 ryo return -1;
468 1.41 ryo
469 1.41 ryo for (; n > 0; n--) {
470 1.41 ryo BE32TOH(*v);
471 1.41 ryo v++;
472 1.41 ryo }
473 1.41 ryo
474 1.41 ryo return 0;
475 1.41 ryo }
476 1.32 jmcneill /*
477 1.32 jmcneill * Get the value of a uint64 property, compensating for host byte order.
478 1.32 jmcneill * Returns 0 on success, non-zero on failure.
479 1.32 jmcneill */
480 1.32 jmcneill int
481 1.32 jmcneill of_getprop_uint64(int node, const char *prop, uint64_t *val)
482 1.32 jmcneill {
483 1.32 jmcneill uint64_t v;
484 1.32 jmcneill int len;
485 1.32 jmcneill
486 1.32 jmcneill len = OF_getprop(node, prop, &v, sizeof(v));
487 1.32 jmcneill if (len != sizeof(v))
488 1.32 jmcneill return -1;
489 1.32 jmcneill
490 1.32 jmcneill *val = be64toh(v);
491 1.32 jmcneill return 0;
492 1.32 jmcneill }
493