xref: /openbmc/u-boot/drivers/core/ofnode.c (revision 9450ab2b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2017 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <fdtdec.h>
10 #include <fdt_support.h>
11 #include <linux/libfdt.h>
12 #include <dm/of_access.h>
13 #include <dm/of_addr.h>
14 #include <dm/ofnode.h>
15 #include <linux/err.h>
16 #include <linux/ioport.h>
17 
ofnode_read_u32(ofnode node,const char * propname,u32 * outp)18 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
19 {
20 	assert(ofnode_valid(node));
21 	debug("%s: %s: ", __func__, propname);
22 
23 	if (ofnode_is_np(node)) {
24 		return of_read_u32(ofnode_to_np(node), propname, outp);
25 	} else {
26 		const fdt32_t *cell;
27 		int len;
28 
29 		cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
30 				   propname, &len);
31 		if (!cell || len < sizeof(int)) {
32 			debug("(not found)\n");
33 			return -EINVAL;
34 		}
35 		*outp = fdt32_to_cpu(cell[0]);
36 	}
37 	debug("%#x (%d)\n", *outp, *outp);
38 
39 	return 0;
40 }
41 
ofnode_read_u32_default(ofnode node,const char * propname,u32 def)42 int ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
43 {
44 	assert(ofnode_valid(node));
45 	ofnode_read_u32(node, propname, &def);
46 
47 	return def;
48 }
49 
ofnode_read_s32_default(ofnode node,const char * propname,s32 def)50 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
51 {
52 	assert(ofnode_valid(node));
53 	ofnode_read_u32(node, propname, (u32 *)&def);
54 
55 	return def;
56 }
57 
ofnode_read_u64(ofnode node,const char * propname,u64 * outp)58 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
59 {
60 	const fdt64_t *cell;
61 	int len;
62 
63 	assert(ofnode_valid(node));
64 	debug("%s: %s: ", __func__, propname);
65 
66 	if (ofnode_is_np(node))
67 		return of_read_u64(ofnode_to_np(node), propname, outp);
68 
69 	cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
70 			   &len);
71 	if (!cell || len < sizeof(*cell)) {
72 		debug("(not found)\n");
73 		return -EINVAL;
74 	}
75 	*outp = fdt64_to_cpu(cell[0]);
76 	debug("%#llx (%lld)\n", (unsigned long long)*outp,
77 	      (unsigned long long)*outp);
78 
79 	return 0;
80 }
81 
ofnode_read_u64_default(ofnode node,const char * propname,u64 def)82 int ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
83 {
84 	assert(ofnode_valid(node));
85 	ofnode_read_u64(node, propname, &def);
86 
87 	return def;
88 }
89 
ofnode_read_bool(ofnode node,const char * propname)90 bool ofnode_read_bool(ofnode node, const char *propname)
91 {
92 	const void *prop;
93 
94 	assert(ofnode_valid(node));
95 	debug("%s: %s: ", __func__, propname);
96 
97 	prop = ofnode_get_property(node, propname, NULL);
98 
99 	debug("%s\n", prop ? "true" : "false");
100 
101 	return prop ? true : false;
102 }
103 
ofnode_read_string(ofnode node,const char * propname)104 const char *ofnode_read_string(ofnode node, const char *propname)
105 {
106 	const char *str = NULL;
107 	int len = -1;
108 
109 	assert(ofnode_valid(node));
110 	debug("%s: %s: ", __func__, propname);
111 
112 	if (ofnode_is_np(node)) {
113 		struct property *prop = of_find_property(
114 				ofnode_to_np(node), propname, NULL);
115 
116 		if (prop) {
117 			str = prop->value;
118 			len = prop->length;
119 		}
120 	} else {
121 		str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
122 				  propname, &len);
123 	}
124 	if (!str) {
125 		debug("<not found>\n");
126 		return NULL;
127 	}
128 	if (strnlen(str, len) >= len) {
129 		debug("<invalid>\n");
130 		return NULL;
131 	}
132 	debug("%s\n", str);
133 
134 	return str;
135 }
136 
ofnode_find_subnode(ofnode node,const char * subnode_name)137 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
138 {
139 	ofnode subnode;
140 
141 	assert(ofnode_valid(node));
142 	debug("%s: %s: ", __func__, subnode_name);
143 
144 	if (ofnode_is_np(node)) {
145 		const struct device_node *np = ofnode_to_np(node);
146 
147 		for (np = np->child; np; np = np->sibling) {
148 			if (!strcmp(subnode_name, np->name))
149 				break;
150 		}
151 		subnode = np_to_ofnode(np);
152 	} else {
153 		int ooffset = fdt_subnode_offset(gd->fdt_blob,
154 				ofnode_to_offset(node), subnode_name);
155 		subnode = offset_to_ofnode(ooffset);
156 	}
157 	debug("%s\n", ofnode_valid(subnode) ?
158 	      ofnode_get_name(subnode) : "<none>");
159 
160 	return subnode;
161 }
162 
ofnode_read_u32_array(ofnode node,const char * propname,u32 * out_values,size_t sz)163 int ofnode_read_u32_array(ofnode node, const char *propname,
164 			  u32 *out_values, size_t sz)
165 {
166 	assert(ofnode_valid(node));
167 	debug("%s: %s: ", __func__, propname);
168 
169 	if (ofnode_is_np(node)) {
170 		return of_read_u32_array(ofnode_to_np(node), propname,
171 					 out_values, sz);
172 	} else {
173 		return fdtdec_get_int_array(gd->fdt_blob,
174 					    ofnode_to_offset(node), propname,
175 					    out_values, sz);
176 	}
177 }
178 
ofnode_first_subnode(ofnode node)179 ofnode ofnode_first_subnode(ofnode node)
180 {
181 	assert(ofnode_valid(node));
182 	if (ofnode_is_np(node))
183 		return np_to_ofnode(node.np->child);
184 
185 	return offset_to_ofnode(
186 		fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
187 }
188 
ofnode_next_subnode(ofnode node)189 ofnode ofnode_next_subnode(ofnode node)
190 {
191 	assert(ofnode_valid(node));
192 	if (ofnode_is_np(node))
193 		return np_to_ofnode(node.np->sibling);
194 
195 	return offset_to_ofnode(
196 		fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
197 }
198 
ofnode_get_parent(ofnode node)199 ofnode ofnode_get_parent(ofnode node)
200 {
201 	ofnode parent;
202 
203 	assert(ofnode_valid(node));
204 	if (ofnode_is_np(node))
205 		parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
206 	else
207 		parent.of_offset = fdt_parent_offset(gd->fdt_blob,
208 						     ofnode_to_offset(node));
209 
210 	return parent;
211 }
212 
ofnode_get_name(ofnode node)213 const char *ofnode_get_name(ofnode node)
214 {
215 	assert(ofnode_valid(node));
216 	if (ofnode_is_np(node))
217 		return strrchr(node.np->full_name, '/') + 1;
218 
219 	return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
220 }
221 
ofnode_get_by_phandle(uint phandle)222 ofnode ofnode_get_by_phandle(uint phandle)
223 {
224 	ofnode node;
225 
226 	if (of_live_active())
227 		node = np_to_ofnode(of_find_node_by_phandle(phandle));
228 	else
229 		node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
230 							    phandle);
231 
232 	return node;
233 }
234 
ofnode_read_size(ofnode node,const char * propname)235 int ofnode_read_size(ofnode node, const char *propname)
236 {
237 	int len;
238 
239 	if (ofnode_is_np(node)) {
240 		struct property *prop = of_find_property(
241 				ofnode_to_np(node), propname, NULL);
242 
243 		if (prop)
244 			return prop->length;
245 	} else {
246 		if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
247 				&len))
248 			return len;
249 	}
250 
251 	return -EINVAL;
252 }
253 
ofnode_get_addr_index(ofnode node,int index)254 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
255 {
256 	int na, ns;
257 	fdt_size_t size;
258 
259 	if (ofnode_is_np(node)) {
260 		const __be32 *prop_val;
261 		uint flags;
262 
263 		prop_val = of_get_address(ofnode_to_np(node), index,
264 					  (u64 *)&size, &flags);
265 		if (!prop_val)
266 			return FDT_ADDR_T_NONE;
267 
268 		ns = of_n_size_cells(ofnode_to_np(node));
269 
270 		if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
271 			return of_translate_address(ofnode_to_np(node), prop_val);
272 		} else {
273 			na = of_n_addr_cells(ofnode_to_np(node));
274 			return of_read_number(prop_val, na);
275 		}
276 	} else {
277 		na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
278 		ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
279 		return fdtdec_get_addr_size_fixed(gd->fdt_blob,
280 						  ofnode_to_offset(node), "reg",
281 						  index, na, ns, &size, true);
282 	}
283 
284 	return FDT_ADDR_T_NONE;
285 }
286 
ofnode_get_addr(ofnode node)287 fdt_addr_t ofnode_get_addr(ofnode node)
288 {
289 	return ofnode_get_addr_index(node, 0);
290 }
291 
ofnode_stringlist_search(ofnode node,const char * property,const char * string)292 int ofnode_stringlist_search(ofnode node, const char *property,
293 			     const char *string)
294 {
295 	if (ofnode_is_np(node)) {
296 		return of_property_match_string(ofnode_to_np(node),
297 						property, string);
298 	} else {
299 		int ret;
300 
301 		ret = fdt_stringlist_search(gd->fdt_blob,
302 					    ofnode_to_offset(node), property,
303 					    string);
304 		if (ret == -FDT_ERR_NOTFOUND)
305 			return -ENODATA;
306 		else if (ret < 0)
307 			return -EINVAL;
308 
309 		return ret;
310 	}
311 }
312 
ofnode_read_string_index(ofnode node,const char * property,int index,const char ** outp)313 int ofnode_read_string_index(ofnode node, const char *property, int index,
314 			     const char **outp)
315 {
316 	if (ofnode_is_np(node)) {
317 		return of_property_read_string_index(ofnode_to_np(node),
318 						     property, index, outp);
319 	} else {
320 		int len;
321 
322 		*outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
323 					   property, index, &len);
324 		if (len < 0)
325 			return -EINVAL;
326 		return 0;
327 	}
328 }
329 
ofnode_read_string_count(ofnode node,const char * property)330 int ofnode_read_string_count(ofnode node, const char *property)
331 {
332 	if (ofnode_is_np(node)) {
333 		return of_property_count_strings(ofnode_to_np(node), property);
334 	} else {
335 		return fdt_stringlist_count(gd->fdt_blob,
336 					    ofnode_to_offset(node), property);
337 	}
338 }
339 
ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args * in,struct ofnode_phandle_args * out)340 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
341 					    struct ofnode_phandle_args *out)
342 {
343 	assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
344 	out->node = offset_to_ofnode(in->node);
345 	out->args_count = in->args_count;
346 	memcpy(out->args, in->args, sizeof(out->args));
347 }
348 
ofnode_from_of_phandle_args(struct of_phandle_args * in,struct ofnode_phandle_args * out)349 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
350 					struct ofnode_phandle_args *out)
351 {
352 	assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
353 	out->node = np_to_ofnode(in->np);
354 	out->args_count = in->args_count;
355 	memcpy(out->args, in->args, sizeof(out->args));
356 }
357 
ofnode_parse_phandle_with_args(ofnode node,const char * list_name,const char * cells_name,int cell_count,int index,struct ofnode_phandle_args * out_args)358 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
359 				   const char *cells_name, int cell_count,
360 				   int index,
361 				   struct ofnode_phandle_args *out_args)
362 {
363 	if (ofnode_is_np(node)) {
364 		struct of_phandle_args args;
365 		int ret;
366 
367 		ret = of_parse_phandle_with_args(ofnode_to_np(node),
368 						 list_name, cells_name, index,
369 						 &args);
370 		if (ret)
371 			return ret;
372 		ofnode_from_of_phandle_args(&args, out_args);
373 	} else {
374 		struct fdtdec_phandle_args args;
375 		int ret;
376 
377 		ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
378 						     ofnode_to_offset(node),
379 						     list_name, cells_name,
380 						     cell_count, index, &args);
381 		if (ret)
382 			return ret;
383 		ofnode_from_fdtdec_phandle_args(&args, out_args);
384 	}
385 
386 	return 0;
387 }
388 
ofnode_count_phandle_with_args(ofnode node,const char * list_name,const char * cells_name)389 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
390 				   const char *cells_name)
391 {
392 	if (ofnode_is_np(node))
393 		return of_count_phandle_with_args(ofnode_to_np(node),
394 				list_name, cells_name);
395 	else
396 		return fdtdec_parse_phandle_with_args(gd->fdt_blob,
397 				ofnode_to_offset(node), list_name, cells_name,
398 				0, -1, NULL);
399 }
400 
ofnode_path(const char * path)401 ofnode ofnode_path(const char *path)
402 {
403 	if (of_live_active())
404 		return np_to_ofnode(of_find_node_by_path(path));
405 	else
406 		return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
407 }
408 
ofnode_get_chosen_prop(const char * name)409 const char *ofnode_get_chosen_prop(const char *name)
410 {
411 	ofnode chosen_node;
412 
413 	chosen_node = ofnode_path("/chosen");
414 
415 	return ofnode_read_string(chosen_node, name);
416 }
417 
ofnode_get_chosen_node(const char * name)418 ofnode ofnode_get_chosen_node(const char *name)
419 {
420 	const char *prop;
421 
422 	prop = ofnode_get_chosen_prop(name);
423 	if (!prop)
424 		return ofnode_null();
425 
426 	return ofnode_path(prop);
427 }
428 
decode_timing_property(ofnode node,const char * name,struct timing_entry * result)429 static int decode_timing_property(ofnode node, const char *name,
430 				  struct timing_entry *result)
431 {
432 	int length, ret = 0;
433 
434 	length = ofnode_read_size(node, name);
435 	if (length < 0) {
436 		debug("%s: could not find property %s\n",
437 		      ofnode_get_name(node), name);
438 		return length;
439 	}
440 
441 	if (length == sizeof(u32)) {
442 		result->typ = ofnode_read_u32_default(node, name, 0);
443 		result->min = result->typ;
444 		result->max = result->typ;
445 	} else {
446 		ret = ofnode_read_u32_array(node, name, &result->min, 3);
447 	}
448 
449 	return ret;
450 }
451 
ofnode_decode_display_timing(ofnode parent,int index,struct display_timing * dt)452 int ofnode_decode_display_timing(ofnode parent, int index,
453 				 struct display_timing *dt)
454 {
455 	int i;
456 	ofnode timings, node;
457 	u32 val = 0;
458 	int ret = 0;
459 
460 	timings = ofnode_find_subnode(parent, "display-timings");
461 	if (!ofnode_valid(timings))
462 		return -EINVAL;
463 
464 	i = 0;
465 	ofnode_for_each_subnode(node, timings) {
466 		if (i++ == index)
467 			break;
468 	}
469 
470 	if (!ofnode_valid(node))
471 		return -EINVAL;
472 
473 	memset(dt, 0, sizeof(*dt));
474 
475 	ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
476 	ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
477 	ret |= decode_timing_property(node, "hactive", &dt->hactive);
478 	ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
479 	ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
480 	ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
481 	ret |= decode_timing_property(node, "vactive", &dt->vactive);
482 	ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
483 	ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
484 
485 	dt->flags = 0;
486 	val = ofnode_read_u32_default(node, "vsync-active", -1);
487 	if (val != -1) {
488 		dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
489 				DISPLAY_FLAGS_VSYNC_LOW;
490 	}
491 	val = ofnode_read_u32_default(node, "hsync-active", -1);
492 	if (val != -1) {
493 		dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
494 				DISPLAY_FLAGS_HSYNC_LOW;
495 	}
496 	val = ofnode_read_u32_default(node, "de-active", -1);
497 	if (val != -1) {
498 		dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
499 				DISPLAY_FLAGS_DE_LOW;
500 	}
501 	val = ofnode_read_u32_default(node, "pixelclk-active", -1);
502 	if (val != -1) {
503 		dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
504 				DISPLAY_FLAGS_PIXDATA_NEGEDGE;
505 	}
506 
507 	if (ofnode_read_bool(node, "interlaced"))
508 		dt->flags |= DISPLAY_FLAGS_INTERLACED;
509 	if (ofnode_read_bool(node, "doublescan"))
510 		dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
511 	if (ofnode_read_bool(node, "doubleclk"))
512 		dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
513 
514 	return ret;
515 }
516 
ofnode_get_property(ofnode node,const char * propname,int * lenp)517 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
518 {
519 	if (ofnode_is_np(node))
520 		return of_get_property(ofnode_to_np(node), propname, lenp);
521 	else
522 		return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
523 				   propname, lenp);
524 }
525 
ofnode_is_available(ofnode node)526 bool ofnode_is_available(ofnode node)
527 {
528 	if (ofnode_is_np(node))
529 		return of_device_is_available(ofnode_to_np(node));
530 	else
531 		return fdtdec_get_is_enabled(gd->fdt_blob,
532 					     ofnode_to_offset(node));
533 }
534 
ofnode_get_addr_size(ofnode node,const char * property,fdt_size_t * sizep)535 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
536 				fdt_size_t *sizep)
537 {
538 	if (ofnode_is_np(node)) {
539 		int na, ns;
540 		int psize;
541 		const struct device_node *np = ofnode_to_np(node);
542 		const __be32 *prop = of_get_property(np, property, &psize);
543 
544 		if (!prop)
545 			return FDT_ADDR_T_NONE;
546 		na = of_n_addr_cells(np);
547 		ns = of_n_size_cells(np);
548 		*sizep = of_read_number(prop + na, ns);
549 
550 		if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0)
551 			return of_translate_address(np, prop);
552 		else
553 			return of_read_number(prop, na);
554 	} else {
555 		return fdtdec_get_addr_size(gd->fdt_blob,
556 					    ofnode_to_offset(node), property,
557 					    sizep);
558 	}
559 }
560 
ofnode_read_u8_array_ptr(ofnode node,const char * propname,size_t sz)561 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
562 					size_t sz)
563 {
564 	if (ofnode_is_np(node)) {
565 		const struct device_node *np = ofnode_to_np(node);
566 		int psize;
567 		const __be32 *prop = of_get_property(np, propname, &psize);
568 
569 		if (!prop || sz != psize)
570 			return NULL;
571 		return (uint8_t *)prop;
572 
573 	} else {
574 		return fdtdec_locate_byte_array(gd->fdt_blob,
575 				ofnode_to_offset(node), propname, sz);
576 	}
577 }
578 
ofnode_read_pci_addr(ofnode node,enum fdt_pci_space type,const char * propname,struct fdt_pci_addr * addr)579 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
580 			 const char *propname, struct fdt_pci_addr *addr)
581 {
582 	const fdt32_t *cell;
583 	int len;
584 	int ret = -ENOENT;
585 
586 	debug("%s: %s: ", __func__, propname);
587 
588 	/*
589 	 * If we follow the pci bus bindings strictly, we should check
590 	 * the value of the node's parent node's #address-cells and
591 	 * #size-cells. They need to be 3 and 2 accordingly. However,
592 	 * for simplicity we skip the check here.
593 	 */
594 	cell = ofnode_get_property(node, propname, &len);
595 	if (!cell)
596 		goto fail;
597 
598 	if ((len % FDT_PCI_REG_SIZE) == 0) {
599 		int num = len / FDT_PCI_REG_SIZE;
600 		int i;
601 
602 		for (i = 0; i < num; i++) {
603 			debug("pci address #%d: %08lx %08lx %08lx\n", i,
604 			      (ulong)fdt32_to_cpu(cell[0]),
605 			      (ulong)fdt32_to_cpu(cell[1]),
606 			      (ulong)fdt32_to_cpu(cell[2]));
607 			if ((fdt32_to_cpu(*cell) & type) == type) {
608 				addr->phys_hi = fdt32_to_cpu(cell[0]);
609 				addr->phys_mid = fdt32_to_cpu(cell[1]);
610 				addr->phys_lo = fdt32_to_cpu(cell[1]);
611 				break;
612 			}
613 
614 			cell += (FDT_PCI_ADDR_CELLS +
615 				 FDT_PCI_SIZE_CELLS);
616 		}
617 
618 		if (i == num) {
619 			ret = -ENXIO;
620 			goto fail;
621 		}
622 
623 		return 0;
624 	}
625 
626 	ret = -EINVAL;
627 
628 fail:
629 	debug("(not found)\n");
630 	return ret;
631 }
632 
ofnode_read_pci_vendev(ofnode node,u16 * vendor,u16 * device)633 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
634 {
635 	const char *list, *end;
636 	int len;
637 
638 	list = ofnode_get_property(node, "compatible", &len);
639 	if (!list)
640 		return -ENOENT;
641 
642 	end = list + len;
643 	while (list < end) {
644 		len = strlen(list);
645 		if (len >= strlen("pciVVVV,DDDD")) {
646 			char *s = strstr(list, "pci");
647 
648 			/*
649 			 * check if the string is something like pciVVVV,DDDD.RR
650 			 * or just pciVVVV,DDDD
651 			 */
652 			if (s && s[7] == ',' &&
653 			    (s[12] == '.' || s[12] == 0)) {
654 				s += 3;
655 				*vendor = simple_strtol(s, NULL, 16);
656 
657 				s += 5;
658 				*device = simple_strtol(s, NULL, 16);
659 
660 				return 0;
661 			}
662 		}
663 		list += (len + 1);
664 	}
665 
666 	return -ENOENT;
667 }
668 
ofnode_read_addr_cells(ofnode node)669 int ofnode_read_addr_cells(ofnode node)
670 {
671 	if (ofnode_is_np(node))
672 		return of_n_addr_cells(ofnode_to_np(node));
673 	else  /* NOTE: this call should walk up the parent stack */
674 		return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
675 }
676 
ofnode_read_size_cells(ofnode node)677 int ofnode_read_size_cells(ofnode node)
678 {
679 	if (ofnode_is_np(node))
680 		return of_n_size_cells(ofnode_to_np(node));
681 	else  /* NOTE: this call should walk up the parent stack */
682 		return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
683 }
684 
ofnode_read_simple_addr_cells(ofnode node)685 int ofnode_read_simple_addr_cells(ofnode node)
686 {
687 	if (ofnode_is_np(node))
688 		return of_simple_addr_cells(ofnode_to_np(node));
689 	else
690 		return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
691 }
692 
ofnode_read_simple_size_cells(ofnode node)693 int ofnode_read_simple_size_cells(ofnode node)
694 {
695 	if (ofnode_is_np(node))
696 		return of_simple_size_cells(ofnode_to_np(node));
697 	else
698 		return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
699 }
700 
ofnode_pre_reloc(ofnode node)701 bool ofnode_pre_reloc(ofnode node)
702 {
703 	if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
704 		return true;
705 	if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
706 		return true;
707 
708 #ifdef CONFIG_TPL_BUILD
709 	if (ofnode_read_bool(node, "u-boot,dm-tpl"))
710 		return true;
711 #elif defined(CONFIG_SPL_BUILD)
712 	if (ofnode_read_bool(node, "u-boot,dm-spl"))
713 		return true;
714 #else
715 	/*
716 	 * In regular builds individual spl and tpl handling both
717 	 * count as handled pre-relocation for later second init.
718 	 */
719 	if (ofnode_read_bool(node, "u-boot,dm-spl") ||
720 	    ofnode_read_bool(node, "u-boot,dm-tpl"))
721 		return true;
722 #endif
723 
724 	return false;
725 }
726 
ofnode_read_resource(ofnode node,uint index,struct resource * res)727 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
728 {
729 	if (ofnode_is_np(node)) {
730 		return of_address_to_resource(ofnode_to_np(node), index, res);
731 	} else {
732 		struct fdt_resource fres;
733 		int ret;
734 
735 		ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
736 				       "reg", index, &fres);
737 		if (ret < 0)
738 			return -EINVAL;
739 		memset(res, '\0', sizeof(*res));
740 		res->start = fres.start;
741 		res->end = fres.end;
742 
743 		return 0;
744 	}
745 }
746 
ofnode_read_resource_byname(ofnode node,const char * name,struct resource * res)747 int ofnode_read_resource_byname(ofnode node, const char *name,
748 				struct resource *res)
749 {
750 	int index;
751 
752 	index = ofnode_stringlist_search(node, "reg-names", name);
753 	if (index < 0)
754 		return index;
755 
756 	return ofnode_read_resource(node, index, res);
757 }
758 
ofnode_translate_address(ofnode node,const fdt32_t * in_addr)759 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
760 {
761 	if (ofnode_is_np(node))
762 		return of_translate_address(ofnode_to_np(node), in_addr);
763 	else
764 		return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
765 }
766 
ofnode_device_is_compatible(ofnode node,const char * compat)767 int ofnode_device_is_compatible(ofnode node, const char *compat)
768 {
769 	if (ofnode_is_np(node))
770 		return of_device_is_compatible(ofnode_to_np(node), compat,
771 					       NULL, NULL);
772 	else
773 		return !fdt_node_check_compatible(gd->fdt_blob,
774 						  ofnode_to_offset(node),
775 						  compat);
776 }
777 
ofnode_by_compatible(ofnode from,const char * compat)778 ofnode ofnode_by_compatible(ofnode from, const char *compat)
779 {
780 	if (of_live_active()) {
781 		return np_to_ofnode(of_find_compatible_node(
782 			(struct device_node *)ofnode_to_np(from), NULL,
783 			compat));
784 	} else {
785 		return offset_to_ofnode(fdt_node_offset_by_compatible(
786 				gd->fdt_blob, ofnode_to_offset(from), compat));
787 	}
788 }
789 
ofnode_by_prop_value(ofnode from,const char * propname,const void * propval,int proplen)790 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
791 			    const void *propval, int proplen)
792 {
793 	if (of_live_active()) {
794 		return np_to_ofnode(of_find_node_by_prop_value(
795 			(struct device_node *)ofnode_to_np(from), propname,
796 			propval, proplen));
797 	} else {
798 		return offset_to_ofnode(fdt_node_offset_by_prop_value(
799 				gd->fdt_blob, ofnode_to_offset(from),
800 				propname, propval, proplen));
801 	}
802 }
803 
ofnode_write_prop(ofnode node,const char * propname,int len,const void * value)804 int ofnode_write_prop(ofnode node, const char *propname, int len,
805 		      const void *value)
806 {
807 	const struct device_node *np = ofnode_to_np(node);
808 	struct property *pp;
809 	struct property *pp_last = NULL;
810 	struct property *new;
811 
812 	if (!of_live_active())
813 		return -ENOSYS;
814 
815 	if (!np)
816 		return -EINVAL;
817 
818 	for (pp = np->properties; pp; pp = pp->next) {
819 		if (strcmp(pp->name, propname) == 0) {
820 			/* Property exists -> change value */
821 			pp->value = (void *)value;
822 			pp->length = len;
823 			return 0;
824 		}
825 		pp_last = pp;
826 	}
827 
828 	if (!pp_last)
829 		return -ENOENT;
830 
831 	/* Property does not exist -> append new property */
832 	new = malloc(sizeof(struct property));
833 	if (!new)
834 		return -ENOMEM;
835 
836 	new->name = strdup(propname);
837 	if (!new->name) {
838 		free(new);
839 		return -ENOMEM;
840 	}
841 
842 	new->value = (void *)value;
843 	new->length = len;
844 	new->next = NULL;
845 
846 	pp_last->next = new;
847 
848 	return 0;
849 }
850 
ofnode_write_string(ofnode node,const char * propname,const char * value)851 int ofnode_write_string(ofnode node, const char *propname, const char *value)
852 {
853 	if (!of_live_active())
854 		return -ENOSYS;
855 
856 	assert(ofnode_valid(node));
857 
858 	debug("%s: %s = %s", __func__, propname, value);
859 
860 	return ofnode_write_prop(node, propname, strlen(value) + 1, value);
861 }
862 
ofnode_set_enabled(ofnode node,bool value)863 int ofnode_set_enabled(ofnode node, bool value)
864 {
865 	if (!of_live_active())
866 		return -ENOSYS;
867 
868 	assert(ofnode_valid(node));
869 
870 	if (value)
871 		return ofnode_write_string(node, "status", "okay");
872 	else
873 		return ofnode_write_string(node, "status", "disable");
874 }
875