ofw_subr.c revision 1.37 1 /* $NetBSD: ofw_subr.c,v 1.37 2020/06/25 11:31:45 jdolecek Exp $ */
2
3 /*
4 * Copyright 1998
5 * Digital Equipment Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce
14 * and retain this copyright notice and list of conditions as
15 * they appear in the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 * Digital Equipment Corporation. Neither the "Digital Equipment
19 * Corporation" name nor any trademark or logo of Digital Equipment
20 * Corporation may be used to endorse or promote products derived
21 * from this software without the prior written permission of
22 * Digital Equipment Corporation.
23 *
24 * 3) This software is provided "AS-IS" and any express or implied
25 * warranties, including but not limited to, any implied warranties
26 * of merchantability, fitness for a particular purpose, or
27 * non-infringement are disclaimed. In no event shall DIGITAL be
28 * liable for any damages whatsoever, and in particular, DIGITAL
29 * shall not be liable for special, indirect, consequential, or
30 * incidental damages or damages for lost profits, loss of
31 * revenue or loss of use, whether such damages arise in contract,
32 * negligence, tort, under statute, in equity, at law or otherwise,
33 * even if advised of the possibility of such damage.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.37 2020/06/25 11:31:45 jdolecek Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <dev/ofw/openfirm.h>
43
44 #define OFW_MAX_STACK_BUF_SIZE 256
45 #define OFW_PATH_BUF_SIZE 512
46
47 /*
48 * int of_decode_int(p)
49 *
50 * This routine converts OFW encoded-int datums
51 * into the integer format of the host machine.
52 *
53 * It is primarily used to convert integer properties
54 * returned by the OF_getprop routine.
55 *
56 * Arguments:
57 * p pointer to unsigned char array which is an
58 * OFW-encoded integer.
59 *
60 * Return Value:
61 * Decoded integer value of argument p.
62 *
63 * Side Effects:
64 * None.
65 */
66 int
67 of_decode_int(const unsigned char *p)
68 {
69 unsigned int i = *p++ << 8;
70 i = (i + *p++) << 8;
71 i = (i + *p++) << 8;
72 return (i + *p);
73 }
74
75 /*
76 * int of_compatible(phandle, strings)
77 *
78 * This routine checks an OFW node's "compatible" entry to see if
79 * it matches any of the provided strings.
80 *
81 * It should be used when determining whether a driver can drive
82 * a particular device.
83 *
84 * Arguments:
85 * phandle OFW phandle of device to be checked for
86 * compatibility.
87 * strings Array of containing expected "compatibility"
88 * property values, presence of any of which
89 * indicates compatibility.
90 *
91 * Return Value:
92 * -1 if none of the strings are found in phandle's "compatibility"
93 * property, or the reverse index of the matching string in the
94 * phandle's "compatibility" property.
95 *
96 * Side Effects:
97 * None.
98 */
99 int
100 of_compatible(int phandle, const char * const *strings)
101 {
102
103 int len, olen, nstr, cstr, rv;
104 char *buf;
105 const char *sp, *nsp;
106
107 len = OF_getproplen(phandle, "compatible");
108 if (len <= 0)
109 return (-1);
110
111 buf = malloc(len, M_TEMP, M_WAITOK);
112
113 /* 'compatible' size should not change. */
114 if (OF_getprop(phandle, "compatible", buf, len) != len) {
115 rv = -1;
116 goto out;
117 }
118
119 /* count 'compatible' strings */
120 sp = buf;
121 nstr = 0;
122 olen = len;
123 while (len && (nsp = memchr(sp, 0, len)) != NULL) {
124 nsp++; /* skip over NUL char */
125 len -= (nsp - sp);
126 sp = nsp;
127 nstr++;
128 }
129 len = olen;
130
131 sp = buf;
132 rv = nstr;
133 while (len && (nsp = memchr(sp, 0, len)) != NULL) {
134 rv--;
135 /* look for a match among the strings provided */
136 for (cstr = 0; strings[cstr] != NULL; cstr++)
137 if (strcmp(sp, strings[cstr]) == 0)
138 goto out;
139
140 nsp++; /* skip over NUL char */
141 len -= (nsp - sp);
142 sp = nsp;
143 }
144 rv = -1;
145
146 out:
147 free(buf, M_TEMP);
148 return (rv);
149 }
150
151 /*
152 * int of_match_compatible(phandle, strings)
153 *
154 * This routine checks an OFW node's "compatible" entry to see if
155 * it matches any of the provided strings.
156 *
157 * It should be used when determining whether a driver can drive
158 * a particular device.
159 *
160 * Arguments:
161 * phandle OFW phandle of device to be checked for
162 * compatibility.
163 * strings Array of containing expected "compatibility"
164 * property values, presence of any of which
165 * indicates compatibility.
166 *
167 * Return Value:
168 * 0 if none of the strings are found in phandle's "compatibility"
169 * property, or a positive number based on the reverse index of the
170 * matching string in the phandle's "compatibility" property, plus 1.
171 *
172 * Side Effects:
173 * None.
174 */
175 int
176 of_match_compatible(int phandle, const char * const *strings)
177 {
178 return of_compatible(phandle, strings) + 1;
179 }
180
181 /*
182 * int of_match_compat_data(phandle, compat_data)
183 *
184 * This routine searches an array of compat_data structures for a
185 * matching "compatible" entry matching the supplied OFW node.
186 *
187 * It should be used when determining whether a driver can drive
188 * a particular device.
189 *
190 * Arguments:
191 * phandle OFW phandle of device to be checked for
192 * compatibility.
193 * compat_data Array of possible compat entry strings and
194 * associated metadata. The last entry in the
195 * list should have a "compat" of NULL to terminate
196 * the list.
197 *
198 * Return Value:
199 * 0 if none of the strings are found in phandle's "compatibility"
200 * property, or a positive number based on the reverse index of the
201 * matching string in the phandle's "compatibility" property, plus 1.
202 *
203 * Side Effects:
204 * None.
205 */
206 int
207 of_match_compat_data(int phandle, const struct of_compat_data *compat_data)
208 {
209 for (; compat_data->compat != NULL; compat_data++) {
210 const char *compat[] = { compat_data->compat, NULL };
211 const int match = of_match_compatible(phandle, compat);
212 if (match)
213 return match;
214 }
215 return 0;
216 }
217
218 /*
219 * const struct of_compat_data *of_search_compatible(phandle, compat_data)
220 *
221 * This routine searches an array of compat_data structures for a
222 * matching "compatible" entry matching the supplied OFW node.
223 *
224 * Arguments:
225 * phandle OFW phandle of device to be checked for
226 * compatibility.
227 * compat_data Array of possible compat entry strings and
228 * associated metadata. The last entry in the
229 * list should have a "compat" of NULL to terminate
230 * the list.
231 *
232 * Return Value:
233 * The first matching compat_data entry in the array. If no matches
234 * are found, the terminating ("compat" of NULL) record is returned.
235 *
236 * Side Effects:
237 * None.
238 */
239 const struct of_compat_data *
240 of_search_compatible(int phandle, const struct of_compat_data *compat_data)
241 {
242 for (; compat_data->compat != NULL; compat_data++) {
243 const char *compat[] = { compat_data->compat, NULL };
244 if (of_match_compatible(phandle, compat))
245 break;
246 }
247 return compat_data;
248 }
249
250 /*
251 * int of_packagename(phandle, buf, bufsize)
252 *
253 * This routine places the last component of an OFW node's name
254 * into a user-provided buffer.
255 *
256 * It can be used during autoconfiguration to make printing of
257 * device names more informative.
258 *
259 * Arguments:
260 * phandle OFW phandle of device whose name name is
261 * desired.
262 * buf Buffer to contain device name, provided by
263 * caller. (For now, must be at least 4
264 * bytes long.)
265 * bufsize Length of buffer referenced by 'buf', in
266 * bytes.
267 *
268 * Return Value:
269 * -1 if the device path name could not be obtained or would
270 * not fit in the allocated temporary buffer, or zero otherwise
271 * (meaning that the leaf node name was successfully extracted).
272 *
273 * Side Effects:
274 * If the leaf node name was successfully extracted, 'buf' is
275 * filled in with at most 'bufsize' bytes of the leaf node
276 * name. If the leaf node was not successfully extracted, a
277 * somewhat meaningful string is placed in the buffer. In
278 * either case, the contents of 'buf' will be NUL-terminated.
279 */
280 int
281 of_packagename(int phandle, char *buf, int bufsize)
282 {
283 char *pbuf;
284 const char *lastslash;
285 int l, rv;
286
287 pbuf = malloc(OFW_PATH_BUF_SIZE, M_TEMP, M_WAITOK);
288 l = OF_package_to_path(phandle, pbuf, OFW_PATH_BUF_SIZE);
289
290 /* check that we could get the name, and that it's not too long. */
291 if (l < 0 ||
292 (l == OFW_PATH_BUF_SIZE && pbuf[OFW_PATH_BUF_SIZE - 1] != '\0')) {
293 if (bufsize >= 25)
294 snprintf(buf, bufsize, "??? (phandle 0x%x)", phandle);
295 else if (bufsize >= 4)
296 strlcpy(buf, "???", bufsize);
297 else
298 panic("of_packagename: bufsize = %d is silly",
299 bufsize);
300 rv = -1;
301 } else {
302 pbuf[l] = '\0';
303 lastslash = strrchr(pbuf, '/');
304 strlcpy(buf, (lastslash == NULL) ? pbuf : (lastslash + 1),
305 bufsize);
306 rv = 0;
307 }
308
309 free(pbuf, M_TEMP);
310 return (rv);
311 }
312
313 /*
314 * Find the first child of a given node that matches name. Does not recurse.
315 */
316 int
317 of_find_firstchild_byname(int node, const char *name)
318 {
319 char namex[32];
320 int nn;
321
322 for (nn = OF_child(node); nn; nn = OF_peer(nn)) {
323 memset(namex, 0, sizeof(namex));
324 if (OF_getprop(nn, "name", namex, sizeof(namex)) == -1)
325 continue;
326 if (strcmp(name, namex) == 0)
327 return nn;
328 }
329 return -1;
330 }
331
332 /*
333 * Find a give node by name. Recurses, and seems to walk upwards too.
334 */
335
336 int
337 of_getnode_byname(int start, const char *target)
338 {
339 int node, next;
340 char name[64];
341
342 if (start == 0)
343 start = OF_peer(0);
344
345 for (node = start; node; node = next) {
346 memset(name, 0, sizeof name);
347 OF_getprop(node, "name", name, sizeof name - 1);
348 if (strcmp(name, target) == 0)
349 break;
350
351 if ((next = OF_child(node)) != 0)
352 continue;
353
354 while (node) {
355 if ((next = OF_peer(node)) != 0)
356 break;
357 node = OF_parent(node);
358 }
359 }
360
361 /* XXX is this correct? */
362 return node;
363 }
364
365 /*
366 * Create a uint32_t integer property from an OFW node property.
367 */
368
369 boolean_t
370 of_to_uint32_prop(prop_dictionary_t dict, int node, const char *ofname,
371 const char *propname)
372 {
373 uint32_t prop;
374
375 if (OF_getprop(node, ofname, &prop, sizeof(prop)) != sizeof(prop))
376 return FALSE;
377
378 return(prop_dictionary_set_uint32(dict, propname, prop));
379 }
380
381 /*
382 * Create a data property from an OFW node property. Max size of 256bytes.
383 */
384
385 boolean_t
386 of_to_dataprop(prop_dictionary_t dict, int node, const char *ofname,
387 const char *propname)
388 {
389 int len;
390 uint8_t prop[256];
391
392 len = OF_getprop(node, ofname, prop, 256);
393 if (len < 1)
394 return FALSE;
395
396 return prop_dictionary_set_data(dict, propname, prop, len);
397 }
398
399 /*
400 * look at output-device, see if there's a Sun-typical video mode specifier as
401 * in screen:r1024x768x60 attached. If found copy it into *buffer, otherwise
402 * return NULL
403 */
404
405 char *
406 of_get_mode_string(char *buffer, int len)
407 {
408 int options;
409 char *pos, output_device[256];
410
411 /*
412 * finally, let's see if there's a video mode specified in
413 * output-device and pass it on so there's at least some way
414 * to program video modes
415 */
416 options = OF_finddevice("/options");
417 if ((options == 0) || (options == -1))
418 return NULL;
419 if (OF_getprop(options, "output-device", output_device, 256) == 0)
420 return NULL;
421
422 /* find the mode string if there is one */
423 pos = strstr(output_device, ":r");
424 if (pos == NULL)
425 return NULL;
426 strncpy(buffer, pos + 2, len);
427 return buffer;
428 }
429
430 /*
431 * Iterate over the subtree of a i2c controller node.
432 * Add all sub-devices into an array as part of the controller's
433 * device properties.
434 * This is used by the i2c bus attach code to do direct configuration.
435 */
436 void
437 of_enter_i2c_devs(prop_dictionary_t props, int ofnode, size_t cell_size,
438 int addr_shift)
439 {
440 int node, len;
441 char name[32];
442 uint64_t reg64;
443 uint32_t reg32;
444 uint64_t addr;
445 prop_array_t array = NULL;
446 prop_dictionary_t dev;
447
448 for (node = OF_child(ofnode); node; node = OF_peer(node)) {
449 if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
450 continue;
451 len = OF_getproplen(node, "reg");
452 addr = 0;
453 if (cell_size == 8 && len >= sizeof(reg64)) {
454 if (OF_getprop(node, "reg", ®64, sizeof(reg64))
455 < sizeof(reg64))
456 continue;
457 addr = be64toh(reg64);
458 /*
459 * The i2c bus number (0 or 1) is encoded in bit 33
460 * of the register, but we encode it in bit 8 of
461 * i2c_addr_t.
462 */
463 if (addr & 0x100000000)
464 addr = (addr & 0xff) | 0x100;
465 } else if (cell_size == 4 && len >= sizeof(reg32)) {
466 if (OF_getprop(node, "reg", ®32, sizeof(reg32))
467 < sizeof(reg32))
468 continue;
469 addr = be32toh(reg32);
470 } else {
471 continue;
472 }
473 addr >>= addr_shift;
474 if (addr == 0) continue;
475
476 if (array == NULL)
477 array = prop_array_create();
478
479 dev = prop_dictionary_create();
480 prop_dictionary_set_string(dev, "name", name);
481 prop_dictionary_set_uint32(dev, "addr", addr);
482 prop_dictionary_set_uint64(dev, "cookie", node);
483 of_to_dataprop(dev, node, "compatible", "compatible");
484 prop_array_add(array, dev);
485 prop_object_release(dev);
486 }
487
488 if (array != NULL) {
489 prop_dictionary_set(props, "i2c-child-devices", array);
490 prop_object_release(array);
491 }
492 }
493
494 void
495 of_enter_spi_devs(prop_dictionary_t props, int ofnode, size_t cell_size)
496 {
497 int node, len;
498 char name[32];
499 uint64_t reg64;
500 uint32_t reg32;
501 uint32_t slave;
502 u_int32_t maxfreq;
503 prop_array_t array = NULL;
504 prop_dictionary_t dev;
505 int mode;
506
507 for (node = OF_child(ofnode); node; node = OF_peer(node)) {
508 if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
509 continue;
510 len = OF_getproplen(node, "reg");
511 slave = 0;
512 if (cell_size == 8 && len >= sizeof(reg64)) {
513 if (OF_getprop(node, "reg", ®64, sizeof(reg64))
514 < sizeof(reg64))
515 continue;
516 slave = be64toh(reg64);
517 } else if (cell_size == 4 && len >= sizeof(reg32)) {
518 if (OF_getprop(node, "reg", ®32, sizeof(reg32))
519 < sizeof(reg32))
520 continue;
521 slave = be32toh(reg32);
522 } else {
523 continue;
524 }
525 if (of_getprop_uint32(node, "spi-max-frequency", &maxfreq)) {
526 maxfreq = 0;
527 }
528 mode = ((int)of_hasprop(node, "cpol") << 1) | (int)of_hasprop(node, "cpha");
529
530 if (array == NULL)
531 array = prop_array_create();
532
533 dev = prop_dictionary_create();
534 prop_dictionary_set_string(dev, "name", name);
535 prop_dictionary_set_uint32(dev, "slave", slave);
536 prop_dictionary_set_uint32(dev, "mode", mode);
537 if (maxfreq > 0)
538 prop_dictionary_set_uint32(dev, "spi-max-frequency", maxfreq);
539 prop_dictionary_set_uint64(dev, "cookie", node);
540 of_to_dataprop(dev, node, "compatible", "compatible");
541 prop_array_add(array, dev);
542 prop_object_release(dev);
543 }
544
545 if (array != NULL) {
546 prop_dictionary_set(props, "spi-child-devices", array);
547 prop_object_release(array);
548 }
549 }
550
551
552 /*
553 * Returns true if the specified property is present.
554 */
555 bool
556 of_hasprop(int node, const char *prop)
557 {
558 return OF_getproplen(node, prop) >= 0;
559 }
560
561 /*
562 * Get the value of a uint32 property, compensating for host byte order.
563 * Returns 0 on success, non-zero on failure.
564 */
565 int
566 of_getprop_uint32(int node, const char *prop, uint32_t *val)
567 {
568 uint32_t v;
569 int len;
570
571 len = OF_getprop(node, prop, &v, sizeof(v));
572 if (len != sizeof(v))
573 return -1;
574
575 *val = be32toh(v);
576 return 0;
577 }
578
579 /*
580 * Get the value of a uint64 property, compensating for host byte order.
581 * Returns 0 on success, non-zero on failure.
582 */
583 int
584 of_getprop_uint64(int node, const char *prop, uint64_t *val)
585 {
586 uint64_t v;
587 int len;
588
589 len = OF_getprop(node, prop, &v, sizeof(v));
590 if (len != sizeof(v))
591 return -1;
592
593 *val = be64toh(v);
594 return 0;
595 }
596