ofw_subr.c revision 1.46 1 1.46 thorpej /* $NetBSD: ofw_subr.c,v 1.46 2021/01/24 17:44:16 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.46 thorpej __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.46 2021/01/24 17:44:16 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 <sys/malloc.h>
44 1.2 cgd #include <dev/ofw/openfirm.h>
45 1.2 cgd
46 1.3 cgd #define OFW_MAX_STACK_BUF_SIZE 256
47 1.3 cgd #define OFW_PATH_BUF_SIZE 512
48 1.3 cgd
49 1.1 cgd /*
50 1.3 cgd * int of_decode_int(p)
51 1.3 cgd *
52 1.3 cgd * This routine converts OFW encoded-int datums
53 1.3 cgd * into the integer format of the host machine.
54 1.1 cgd *
55 1.3 cgd * It is primarily used to convert integer properties
56 1.3 cgd * returned by the OF_getprop routine.
57 1.2 cgd *
58 1.2 cgd * Arguments:
59 1.2 cgd * p pointer to unsigned char array which is an
60 1.2 cgd * OFW-encoded integer.
61 1.2 cgd *
62 1.2 cgd * Return Value:
63 1.2 cgd * Decoded integer value of argument p.
64 1.3 cgd *
65 1.3 cgd * Side Effects:
66 1.3 cgd * None.
67 1.1 cgd */
68 1.1 cgd int
69 1.14 dsl of_decode_int(const unsigned char *p)
70 1.1 cgd {
71 1.1 cgd unsigned int i = *p++ << 8;
72 1.1 cgd i = (i + *p++) << 8;
73 1.1 cgd i = (i + *p++) << 8;
74 1.1 cgd return (i + *p);
75 1.2 cgd }
76 1.2 cgd
77 1.2 cgd /*
78 1.3 cgd * int of_compatible(phandle, strings)
79 1.3 cgd *
80 1.2 cgd * This routine checks an OFW node's "compatible" entry to see if
81 1.2 cgd * it matches any of the provided strings.
82 1.2 cgd *
83 1.2 cgd * It should be used when determining whether a driver can drive
84 1.24 jmcneill * a particular device.
85 1.2 cgd *
86 1.2 cgd * Arguments:
87 1.2 cgd * phandle OFW phandle of device to be checked for
88 1.2 cgd * compatibility.
89 1.2 cgd * strings Array of containing expected "compatibility"
90 1.2 cgd * property values, presence of any of which
91 1.2 cgd * indicates compatibility.
92 1.2 cgd *
93 1.2 cgd * Return Value:
94 1.8 wiz * -1 if none of the strings are found in phandle's "compatibility"
95 1.24 jmcneill * property, or the reverse index of the matching string in the
96 1.24 jmcneill * phandle's "compatibility" property.
97 1.3 cgd *
98 1.3 cgd * Side Effects:
99 1.3 cgd * None.
100 1.2 cgd */
101 1.2 cgd int
102 1.12 garbled of_compatible(int phandle, const char * const *strings)
103 1.2 cgd {
104 1.24 jmcneill
105 1.38 rin int len, olen, allocated, nstr, cstr, rv;
106 1.38 rin char *buf, sbuf[OFW_MAX_STACK_BUF_SIZE];
107 1.2 cgd const char *sp, *nsp;
108 1.2 cgd
109 1.2 cgd len = OF_getproplen(phandle, "compatible");
110 1.2 cgd if (len <= 0)
111 1.2 cgd return (-1);
112 1.2 cgd
113 1.38 rin if (len > sizeof(sbuf)) {
114 1.38 rin buf = malloc(len, M_TEMP, M_WAITOK);
115 1.38 rin allocated = 1;
116 1.38 rin } else {
117 1.38 rin buf = sbuf;
118 1.38 rin allocated = 0;
119 1.38 rin }
120 1.2 cgd
121 1.2 cgd /* 'compatible' size should not change. */
122 1.2 cgd if (OF_getprop(phandle, "compatible", buf, len) != len) {
123 1.2 cgd rv = -1;
124 1.2 cgd goto out;
125 1.2 cgd }
126 1.2 cgd
127 1.24 jmcneill /* count 'compatible' strings */
128 1.24 jmcneill sp = buf;
129 1.24 jmcneill nstr = 0;
130 1.24 jmcneill olen = len;
131 1.24 jmcneill while (len && (nsp = memchr(sp, 0, len)) != NULL) {
132 1.24 jmcneill nsp++; /* skip over NUL char */
133 1.24 jmcneill len -= (nsp - sp);
134 1.24 jmcneill sp = nsp;
135 1.24 jmcneill nstr++;
136 1.24 jmcneill }
137 1.24 jmcneill len = olen;
138 1.24 jmcneill
139 1.2 cgd sp = buf;
140 1.24 jmcneill rv = nstr;
141 1.2 cgd while (len && (nsp = memchr(sp, 0, len)) != NULL) {
142 1.24 jmcneill rv--;
143 1.2 cgd /* look for a match among the strings provided */
144 1.24 jmcneill for (cstr = 0; strings[cstr] != NULL; cstr++)
145 1.24 jmcneill if (strcmp(sp, strings[cstr]) == 0)
146 1.2 cgd goto out;
147 1.2 cgd
148 1.2 cgd nsp++; /* skip over NUL char */
149 1.2 cgd len -= (nsp - sp);
150 1.2 cgd sp = nsp;
151 1.2 cgd }
152 1.2 cgd rv = -1;
153 1.2 cgd
154 1.2 cgd out:
155 1.38 rin if (allocated)
156 1.38 rin free(buf, M_TEMP);
157 1.2 cgd return (rv);
158 1.24 jmcneill }
159 1.10 perry
160 1.24 jmcneill /*
161 1.24 jmcneill * int of_match_compatible(phandle, strings)
162 1.24 jmcneill *
163 1.24 jmcneill * This routine checks an OFW node's "compatible" entry to see if
164 1.24 jmcneill * it matches any of the provided strings.
165 1.24 jmcneill *
166 1.24 jmcneill * It should be used when determining whether a driver can drive
167 1.24 jmcneill * a particular device.
168 1.24 jmcneill *
169 1.24 jmcneill * Arguments:
170 1.24 jmcneill * phandle OFW phandle of device to be checked for
171 1.24 jmcneill * compatibility.
172 1.24 jmcneill * strings Array of containing expected "compatibility"
173 1.24 jmcneill * property values, presence of any of which
174 1.24 jmcneill * indicates compatibility.
175 1.24 jmcneill *
176 1.24 jmcneill * Return Value:
177 1.24 jmcneill * 0 if none of the strings are found in phandle's "compatibility"
178 1.24 jmcneill * property, or a positive number based on the reverse index of the
179 1.24 jmcneill * matching string in the phandle's "compatibility" property, plus 1.
180 1.24 jmcneill *
181 1.24 jmcneill * Side Effects:
182 1.24 jmcneill * None.
183 1.24 jmcneill */
184 1.24 jmcneill int
185 1.24 jmcneill of_match_compatible(int phandle, const char * const *strings)
186 1.24 jmcneill {
187 1.24 jmcneill return of_compatible(phandle, strings) + 1;
188 1.3 cgd }
189 1.3 cgd
190 1.3 cgd /*
191 1.30 jmcneill * int of_match_compat_data(phandle, compat_data)
192 1.30 jmcneill *
193 1.30 jmcneill * This routine searches an array of compat_data structures for a
194 1.30 jmcneill * matching "compatible" entry matching the supplied OFW node.
195 1.30 jmcneill *
196 1.30 jmcneill * It should be used when determining whether a driver can drive
197 1.30 jmcneill * a particular device.
198 1.30 jmcneill *
199 1.30 jmcneill * Arguments:
200 1.30 jmcneill * phandle OFW phandle of device to be checked for
201 1.30 jmcneill * compatibility.
202 1.30 jmcneill * compat_data Array of possible compat entry strings and
203 1.30 jmcneill * associated metadata. The last entry in the
204 1.30 jmcneill * list should have a "compat" of NULL to terminate
205 1.30 jmcneill * the list.
206 1.30 jmcneill *
207 1.30 jmcneill * Return Value:
208 1.30 jmcneill * 0 if none of the strings are found in phandle's "compatibility"
209 1.30 jmcneill * property, or a positive number based on the reverse index of the
210 1.30 jmcneill * matching string in the phandle's "compatibility" property, plus 1.
211 1.30 jmcneill *
212 1.30 jmcneill * Side Effects:
213 1.30 jmcneill * None.
214 1.30 jmcneill */
215 1.30 jmcneill int
216 1.42 thorpej of_match_compat_data(int phandle,
217 1.42 thorpej const struct device_compatible_entry *compat_data)
218 1.30 jmcneill {
219 1.46 thorpej char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
220 1.46 thorpej int proplen, match = 0;
221 1.46 thorpej
222 1.46 thorpej proplen = OF_getproplen(phandle, "compatible");
223 1.46 thorpej if (proplen <= 0) {
224 1.46 thorpej return 0;
225 1.46 thorpej }
226 1.46 thorpej
227 1.46 thorpej prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
228 1.46 thorpej
229 1.46 thorpej if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
230 1.46 thorpej goto out;
231 1.30 jmcneill }
232 1.46 thorpej
233 1.46 thorpej match = device_compatible_match_strlist(prop, proplen, compat_data);
234 1.46 thorpej
235 1.46 thorpej out:
236 1.46 thorpej kmem_tmpbuf_free(prop, proplen, propbuf);
237 1.46 thorpej return match;
238 1.30 jmcneill }
239 1.30 jmcneill
240 1.30 jmcneill /*
241 1.43 jmcneill * const struct device_compatible_entry *of_search_compatible(phandle,
242 1.43 jmcneill * compat_data)
243 1.29 jmcneill *
244 1.29 jmcneill * This routine searches an array of compat_data structures for a
245 1.29 jmcneill * matching "compatible" entry matching the supplied OFW node.
246 1.29 jmcneill *
247 1.29 jmcneill * Arguments:
248 1.29 jmcneill * phandle OFW phandle of device to be checked for
249 1.29 jmcneill * compatibility.
250 1.29 jmcneill * compat_data Array of possible compat entry strings and
251 1.29 jmcneill * associated metadata. The last entry in the
252 1.29 jmcneill * list should have a "compat" of NULL to terminate
253 1.29 jmcneill * the list.
254 1.29 jmcneill *
255 1.29 jmcneill * Return Value:
256 1.29 jmcneill * The first matching compat_data entry in the array. If no matches
257 1.46 thorpej * are found, NULL is returned.
258 1.29 jmcneill *
259 1.29 jmcneill * Side Effects:
260 1.29 jmcneill * None.
261 1.29 jmcneill */
262 1.42 thorpej const struct device_compatible_entry *
263 1.42 thorpej of_search_compatible(int phandle,
264 1.42 thorpej const struct device_compatible_entry *compat_data)
265 1.29 jmcneill {
266 1.46 thorpej char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
267 1.46 thorpej const struct device_compatible_entry *match = NULL;
268 1.46 thorpej int proplen;
269 1.46 thorpej
270 1.46 thorpej proplen = OF_getproplen(phandle, "compatible");
271 1.46 thorpej if (proplen <= 0) {
272 1.46 thorpej return 0;
273 1.29 jmcneill }
274 1.46 thorpej
275 1.46 thorpej prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
276 1.46 thorpej
277 1.46 thorpej if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
278 1.46 thorpej goto out;
279 1.46 thorpej }
280 1.46 thorpej
281 1.46 thorpej match = device_compatible_lookup_strlist(prop, proplen, compat_data);
282 1.46 thorpej
283 1.46 thorpej out:
284 1.46 thorpej kmem_tmpbuf_free(prop, proplen, propbuf);
285 1.46 thorpej return match;
286 1.29 jmcneill }
287 1.29 jmcneill
288 1.29 jmcneill /*
289 1.4 cgd * int of_packagename(phandle, buf, bufsize)
290 1.3 cgd *
291 1.3 cgd * This routine places the last component of an OFW node's name
292 1.3 cgd * into a user-provided buffer.
293 1.3 cgd *
294 1.3 cgd * It can be used during autoconfiguration to make printing of
295 1.3 cgd * device names more informative.
296 1.3 cgd *
297 1.3 cgd * Arguments:
298 1.3 cgd * phandle OFW phandle of device whose name name is
299 1.3 cgd * desired.
300 1.3 cgd * buf Buffer to contain device name, provided by
301 1.3 cgd * caller. (For now, must be at least 4
302 1.3 cgd * bytes long.)
303 1.3 cgd * bufsize Length of buffer referenced by 'buf', in
304 1.3 cgd * bytes.
305 1.3 cgd *
306 1.3 cgd * Return Value:
307 1.3 cgd * -1 if the device path name could not be obtained or would
308 1.3 cgd * not fit in the allocated temporary buffer, or zero otherwise
309 1.6 soren * (meaning that the leaf node name was successfully extracted).
310 1.3 cgd *
311 1.3 cgd * Side Effects:
312 1.3 cgd * If the leaf node name was successfully extracted, 'buf' is
313 1.3 cgd * filled in with at most 'bufsize' bytes of the leaf node
314 1.3 cgd * name. If the leaf node was not successfully extracted, a
315 1.3 cgd * somewhat meaningful string is placed in the buffer. In
316 1.3 cgd * either case, the contents of 'buf' will be NUL-terminated.
317 1.3 cgd */
318 1.3 cgd int
319 1.12 garbled of_packagename(int phandle, char *buf, int bufsize)
320 1.3 cgd {
321 1.3 cgd char *pbuf;
322 1.3 cgd const char *lastslash;
323 1.3 cgd int l, rv;
324 1.3 cgd
325 1.3 cgd pbuf = malloc(OFW_PATH_BUF_SIZE, M_TEMP, M_WAITOK);
326 1.3 cgd l = OF_package_to_path(phandle, pbuf, OFW_PATH_BUF_SIZE);
327 1.3 cgd
328 1.3 cgd /* check that we could get the name, and that it's not too long. */
329 1.3 cgd if (l < 0 ||
330 1.3 cgd (l == OFW_PATH_BUF_SIZE && pbuf[OFW_PATH_BUF_SIZE - 1] != '\0')) {
331 1.3 cgd if (bufsize >= 25)
332 1.9 itojun snprintf(buf, bufsize, "??? (phandle 0x%x)", phandle);
333 1.3 cgd else if (bufsize >= 4)
334 1.9 itojun strlcpy(buf, "???", bufsize);
335 1.3 cgd else
336 1.4 cgd panic("of_packagename: bufsize = %d is silly",
337 1.4 cgd bufsize);
338 1.3 cgd rv = -1;
339 1.3 cgd } else {
340 1.5 mycroft pbuf[l] = '\0';
341 1.3 cgd lastslash = strrchr(pbuf, '/');
342 1.9 itojun strlcpy(buf, (lastslash == NULL) ? pbuf : (lastslash + 1),
343 1.3 cgd bufsize);
344 1.3 cgd rv = 0;
345 1.3 cgd }
346 1.3 cgd
347 1.3 cgd free(pbuf, M_TEMP);
348 1.3 cgd return (rv);
349 1.1 cgd }
350 1.12 garbled
351 1.12 garbled /*
352 1.13 garbled * Find the first child of a given node that matches name. Does not recurse.
353 1.12 garbled */
354 1.12 garbled int
355 1.12 garbled of_find_firstchild_byname(int node, const char *name)
356 1.12 garbled {
357 1.12 garbled char namex[32];
358 1.12 garbled int nn;
359 1.12 garbled
360 1.12 garbled for (nn = OF_child(node); nn; nn = OF_peer(nn)) {
361 1.12 garbled memset(namex, 0, sizeof(namex));
362 1.12 garbled if (OF_getprop(nn, "name", namex, sizeof(namex)) == -1)
363 1.12 garbled continue;
364 1.12 garbled if (strcmp(name, namex) == 0)
365 1.12 garbled return nn;
366 1.12 garbled }
367 1.12 garbled return -1;
368 1.12 garbled }
369 1.13 garbled
370 1.13 garbled /*
371 1.40 jmcneill * Find a child node that is compatible with str. Recurses, starting at node.
372 1.40 jmcneill */
373 1.40 jmcneill int
374 1.40 jmcneill of_find_bycompat(int node, const char *str)
375 1.40 jmcneill {
376 1.40 jmcneill const char * compatible[] = { str, NULL };
377 1.40 jmcneill int child, ret;
378 1.40 jmcneill
379 1.40 jmcneill for (child = OF_child(node); child; child = OF_peer(child)) {
380 1.40 jmcneill if (of_match_compatible(child, compatible) != 0)
381 1.40 jmcneill return child;
382 1.40 jmcneill ret = of_find_bycompat(child, str);
383 1.40 jmcneill if (ret != -1)
384 1.40 jmcneill return ret;
385 1.40 jmcneill }
386 1.40 jmcneill
387 1.40 jmcneill return -1;
388 1.40 jmcneill }
389 1.40 jmcneill
390 1.40 jmcneill /*
391 1.13 garbled * Find a give node by name. Recurses, and seems to walk upwards too.
392 1.13 garbled */
393 1.13 garbled
394 1.13 garbled int
395 1.13 garbled of_getnode_byname(int start, const char *target)
396 1.13 garbled {
397 1.13 garbled int node, next;
398 1.13 garbled char name[64];
399 1.13 garbled
400 1.13 garbled if (start == 0)
401 1.13 garbled start = OF_peer(0);
402 1.13 garbled
403 1.13 garbled for (node = start; node; node = next) {
404 1.13 garbled memset(name, 0, sizeof name);
405 1.13 garbled OF_getprop(node, "name", name, sizeof name - 1);
406 1.13 garbled if (strcmp(name, target) == 0)
407 1.13 garbled break;
408 1.13 garbled
409 1.13 garbled if ((next = OF_child(node)) != 0)
410 1.13 garbled continue;
411 1.13 garbled
412 1.13 garbled while (node) {
413 1.13 garbled if ((next = OF_peer(node)) != 0)
414 1.13 garbled break;
415 1.13 garbled node = OF_parent(node);
416 1.13 garbled }
417 1.13 garbled }
418 1.13 garbled
419 1.13 garbled /* XXX is this correct? */
420 1.13 garbled return node;
421 1.13 garbled }
422 1.13 garbled
423 1.13 garbled /*
424 1.13 garbled * Create a uint32_t integer property from an OFW node property.
425 1.13 garbled */
426 1.13 garbled
427 1.13 garbled boolean_t
428 1.13 garbled of_to_uint32_prop(prop_dictionary_t dict, int node, const char *ofname,
429 1.13 garbled const char *propname)
430 1.13 garbled {
431 1.13 garbled uint32_t prop;
432 1.13 garbled
433 1.13 garbled if (OF_getprop(node, ofname, &prop, sizeof(prop)) != sizeof(prop))
434 1.13 garbled return FALSE;
435 1.13 garbled
436 1.13 garbled return(prop_dictionary_set_uint32(dict, propname, prop));
437 1.13 garbled }
438 1.13 garbled
439 1.13 garbled /*
440 1.13 garbled * Create a data property from an OFW node property. Max size of 256bytes.
441 1.13 garbled */
442 1.13 garbled
443 1.13 garbled boolean_t
444 1.13 garbled of_to_dataprop(prop_dictionary_t dict, int node, const char *ofname,
445 1.13 garbled const char *propname)
446 1.13 garbled {
447 1.13 garbled int len;
448 1.13 garbled uint8_t prop[256];
449 1.13 garbled
450 1.13 garbled len = OF_getprop(node, ofname, prop, 256);
451 1.13 garbled if (len < 1)
452 1.13 garbled return FALSE;
453 1.13 garbled
454 1.36 thorpej return prop_dictionary_set_data(dict, propname, prop, len);
455 1.13 garbled }
456 1.15 macallan
457 1.15 macallan /*
458 1.15 macallan * look at output-device, see if there's a Sun-typical video mode specifier as
459 1.15 macallan * in screen:r1024x768x60 attached. If found copy it into *buffer, otherwise
460 1.15 macallan * return NULL
461 1.15 macallan */
462 1.15 macallan
463 1.15 macallan char *
464 1.15 macallan of_get_mode_string(char *buffer, int len)
465 1.15 macallan {
466 1.15 macallan int options;
467 1.15 macallan char *pos, output_device[256];
468 1.15 macallan
469 1.15 macallan /*
470 1.15 macallan * finally, let's see if there's a video mode specified in
471 1.15 macallan * output-device and pass it on so there's at least some way
472 1.15 macallan * to program video modes
473 1.15 macallan */
474 1.15 macallan options = OF_finddevice("/options");
475 1.15 macallan if ((options == 0) || (options == -1))
476 1.15 macallan return NULL;
477 1.15 macallan if (OF_getprop(options, "output-device", output_device, 256) == 0)
478 1.15 macallan return NULL;
479 1.15 macallan
480 1.15 macallan /* find the mode string if there is one */
481 1.15 macallan pos = strstr(output_device, ":r");
482 1.15 macallan if (pos == NULL)
483 1.15 macallan return NULL;
484 1.15 macallan strncpy(buffer, pos + 2, len);
485 1.15 macallan return buffer;
486 1.15 macallan }
487 1.17 martin
488 1.17 martin /*
489 1.17 martin * Iterate over the subtree of a i2c controller node.
490 1.17 martin * Add all sub-devices into an array as part of the controller's
491 1.17 martin * device properties.
492 1.17 martin * This is used by the i2c bus attach code to do direct configuration.
493 1.17 martin */
494 1.17 martin void
495 1.26 jmcneill of_enter_i2c_devs(prop_dictionary_t props, int ofnode, size_t cell_size,
496 1.26 jmcneill int addr_shift)
497 1.17 martin {
498 1.17 martin int node, len;
499 1.31 thorpej char name[32];
500 1.18 martin uint64_t reg64;
501 1.18 martin uint32_t reg32;
502 1.18 martin uint64_t addr;
503 1.19 jdc prop_array_t array = NULL;
504 1.17 martin prop_dictionary_t dev;
505 1.17 martin
506 1.17 martin for (node = OF_child(ofnode); node; node = OF_peer(node)) {
507 1.17 martin if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
508 1.17 martin continue;
509 1.17 martin len = OF_getproplen(node, "reg");
510 1.18 martin addr = 0;
511 1.18 martin if (cell_size == 8 && len >= sizeof(reg64)) {
512 1.18 martin if (OF_getprop(node, "reg", ®64, sizeof(reg64))
513 1.18 martin < sizeof(reg64))
514 1.17 martin continue;
515 1.25 jmcneill addr = be64toh(reg64);
516 1.20 jdc /*
517 1.20 jdc * The i2c bus number (0 or 1) is encoded in bit 33
518 1.20 jdc * of the register, but we encode it in bit 8 of
519 1.20 jdc * i2c_addr_t.
520 1.20 jdc */
521 1.20 jdc if (addr & 0x100000000)
522 1.20 jdc addr = (addr & 0xff) | 0x100;
523 1.18 martin } else if (cell_size == 4 && len >= sizeof(reg32)) {
524 1.18 martin if (OF_getprop(node, "reg", ®32, sizeof(reg32))
525 1.18 martin < sizeof(reg32))
526 1.17 martin continue;
527 1.25 jmcneill addr = be32toh(reg32);
528 1.17 martin } else {
529 1.18 martin continue;
530 1.17 martin }
531 1.26 jmcneill addr >>= addr_shift;
532 1.18 martin if (addr == 0) continue;
533 1.17 martin
534 1.19 jdc if (array == NULL)
535 1.19 jdc array = prop_array_create();
536 1.19 jdc
537 1.17 martin dev = prop_dictionary_create();
538 1.35 thorpej prop_dictionary_set_string(dev, "name", name);
539 1.18 martin prop_dictionary_set_uint32(dev, "addr", addr);
540 1.17 martin prop_dictionary_set_uint64(dev, "cookie", node);
541 1.17 martin of_to_dataprop(dev, node, "compatible", "compatible");
542 1.17 martin prop_array_add(array, dev);
543 1.17 martin prop_object_release(dev);
544 1.17 martin }
545 1.17 martin
546 1.19 jdc if (array != NULL) {
547 1.19 jdc prop_dictionary_set(props, "i2c-child-devices", array);
548 1.19 jdc prop_object_release(array);
549 1.19 jdc }
550 1.17 martin }
551 1.27 jmcneill
552 1.34 tnn void
553 1.34 tnn of_enter_spi_devs(prop_dictionary_t props, int ofnode, size_t cell_size)
554 1.34 tnn {
555 1.34 tnn int node, len;
556 1.34 tnn char name[32];
557 1.34 tnn uint64_t reg64;
558 1.34 tnn uint32_t reg32;
559 1.34 tnn uint32_t slave;
560 1.34 tnn u_int32_t maxfreq;
561 1.34 tnn prop_array_t array = NULL;
562 1.34 tnn prop_dictionary_t dev;
563 1.34 tnn int mode;
564 1.34 tnn
565 1.34 tnn for (node = OF_child(ofnode); node; node = OF_peer(node)) {
566 1.34 tnn if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
567 1.34 tnn continue;
568 1.34 tnn len = OF_getproplen(node, "reg");
569 1.34 tnn slave = 0;
570 1.34 tnn if (cell_size == 8 && len >= sizeof(reg64)) {
571 1.34 tnn if (OF_getprop(node, "reg", ®64, sizeof(reg64))
572 1.34 tnn < sizeof(reg64))
573 1.34 tnn continue;
574 1.34 tnn slave = be64toh(reg64);
575 1.34 tnn } else if (cell_size == 4 && len >= sizeof(reg32)) {
576 1.34 tnn if (OF_getprop(node, "reg", ®32, sizeof(reg32))
577 1.34 tnn < sizeof(reg32))
578 1.34 tnn continue;
579 1.34 tnn slave = be32toh(reg32);
580 1.34 tnn } else {
581 1.34 tnn continue;
582 1.34 tnn }
583 1.34 tnn if (of_getprop_uint32(node, "spi-max-frequency", &maxfreq)) {
584 1.34 tnn maxfreq = 0;
585 1.34 tnn }
586 1.34 tnn mode = ((int)of_hasprop(node, "cpol") << 1) | (int)of_hasprop(node, "cpha");
587 1.34 tnn
588 1.34 tnn if (array == NULL)
589 1.34 tnn array = prop_array_create();
590 1.34 tnn
591 1.34 tnn dev = prop_dictionary_create();
592 1.35 thorpej prop_dictionary_set_string(dev, "name", name);
593 1.34 tnn prop_dictionary_set_uint32(dev, "slave", slave);
594 1.34 tnn prop_dictionary_set_uint32(dev, "mode", mode);
595 1.34 tnn if (maxfreq > 0)
596 1.34 tnn prop_dictionary_set_uint32(dev, "spi-max-frequency", maxfreq);
597 1.34 tnn prop_dictionary_set_uint64(dev, "cookie", node);
598 1.34 tnn of_to_dataprop(dev, node, "compatible", "compatible");
599 1.34 tnn prop_array_add(array, dev);
600 1.34 tnn prop_object_release(dev);
601 1.34 tnn }
602 1.34 tnn
603 1.34 tnn if (array != NULL) {
604 1.34 tnn prop_dictionary_set(props, "spi-child-devices", array);
605 1.34 tnn prop_object_release(array);
606 1.34 tnn }
607 1.34 tnn }
608 1.34 tnn
609 1.34 tnn
610 1.27 jmcneill /*
611 1.28 jmcneill * Returns true if the specified property is present.
612 1.27 jmcneill */
613 1.27 jmcneill bool
614 1.28 jmcneill of_hasprop(int node, const char *prop)
615 1.27 jmcneill {
616 1.27 jmcneill return OF_getproplen(node, prop) >= 0;
617 1.27 jmcneill }
618 1.27 jmcneill
619 1.27 jmcneill /*
620 1.27 jmcneill * Get the value of a uint32 property, compensating for host byte order.
621 1.27 jmcneill * Returns 0 on success, non-zero on failure.
622 1.27 jmcneill */
623 1.27 jmcneill int
624 1.27 jmcneill of_getprop_uint32(int node, const char *prop, uint32_t *val)
625 1.27 jmcneill {
626 1.27 jmcneill uint32_t v;
627 1.27 jmcneill int len;
628 1.27 jmcneill
629 1.27 jmcneill len = OF_getprop(node, prop, &v, sizeof(v));
630 1.27 jmcneill if (len != sizeof(v))
631 1.27 jmcneill return -1;
632 1.27 jmcneill
633 1.27 jmcneill *val = be32toh(v);
634 1.27 jmcneill return 0;
635 1.27 jmcneill }
636 1.32 jmcneill
637 1.41 ryo int
638 1.41 ryo of_getprop_uint32_array(int node, const char *prop, uint32_t *array, int n)
639 1.41 ryo {
640 1.41 ryo uint32_t *v = array;
641 1.41 ryo int len;
642 1.41 ryo
643 1.41 ryo len = OF_getprop(node, prop, array, n * sizeof(*v));
644 1.41 ryo if (len < (int)(n * sizeof(*v)))
645 1.41 ryo return -1;
646 1.41 ryo
647 1.41 ryo for (; n > 0; n--) {
648 1.41 ryo BE32TOH(*v);
649 1.41 ryo v++;
650 1.41 ryo }
651 1.41 ryo
652 1.41 ryo return 0;
653 1.41 ryo }
654 1.32 jmcneill /*
655 1.32 jmcneill * Get the value of a uint64 property, compensating for host byte order.
656 1.32 jmcneill * Returns 0 on success, non-zero on failure.
657 1.32 jmcneill */
658 1.32 jmcneill int
659 1.32 jmcneill of_getprop_uint64(int node, const char *prop, uint64_t *val)
660 1.32 jmcneill {
661 1.32 jmcneill uint64_t v;
662 1.32 jmcneill int len;
663 1.32 jmcneill
664 1.32 jmcneill len = OF_getprop(node, prop, &v, sizeof(v));
665 1.32 jmcneill if (len != sizeof(v))
666 1.32 jmcneill return -1;
667 1.32 jmcneill
668 1.32 jmcneill *val = be64toh(v);
669 1.32 jmcneill return 0;
670 1.32 jmcneill }
671