xref: /openbmc/u-boot/drivers/core/uclass.c (revision 95963679)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  *
5  * (C) Copyright 2012
6  * Pavel Herrmann <morpheus.ibis@gmail.com>
7  */
8 
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <malloc.h>
13 #include <dm/device.h>
14 #include <dm/device-internal.h>
15 #include <dm/lists.h>
16 #include <dm/uclass.h>
17 #include <dm/uclass-internal.h>
18 #include <dm/util.h>
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
22 struct uclass *uclass_find(enum uclass_id key)
23 {
24 	struct uclass *uc;
25 
26 	if (!gd->dm_root)
27 		return NULL;
28 	/*
29 	 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
30 	 * node to the start of the list, or creating a linear array mapping
31 	 * id to node.
32 	 */
33 	list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
34 		if (uc->uc_drv->id == key)
35 			return uc;
36 	}
37 
38 	return NULL;
39 }
40 
41 /**
42  * uclass_add() - Create new uclass in list
43  * @id: Id number to create
44  * @ucp: Returns pointer to uclass, or NULL on error
45  * @return 0 on success, -ve on error
46  *
47  * The new uclass is added to the list. There must be only one uclass for
48  * each id.
49  */
50 static int uclass_add(enum uclass_id id, struct uclass **ucp)
51 {
52 	struct uclass_driver *uc_drv;
53 	struct uclass *uc;
54 	int ret;
55 
56 	*ucp = NULL;
57 	uc_drv = lists_uclass_lookup(id);
58 	if (!uc_drv) {
59 		debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
60 		      id);
61 		/*
62 		 * Use a strange error to make this case easier to find. When
63 		 * a uclass is not available it can prevent driver model from
64 		 * starting up and this failure is otherwise hard to debug.
65 		 */
66 		return -EPFNOSUPPORT;
67 	}
68 	uc = calloc(1, sizeof(*uc));
69 	if (!uc)
70 		return -ENOMEM;
71 	if (uc_drv->priv_auto_alloc_size) {
72 		uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
73 		if (!uc->priv) {
74 			ret = -ENOMEM;
75 			goto fail_mem;
76 		}
77 	}
78 	uc->uc_drv = uc_drv;
79 	INIT_LIST_HEAD(&uc->sibling_node);
80 	INIT_LIST_HEAD(&uc->dev_head);
81 	list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
82 
83 	if (uc_drv->init) {
84 		ret = uc_drv->init(uc);
85 		if (ret)
86 			goto fail;
87 	}
88 
89 	*ucp = uc;
90 
91 	return 0;
92 fail:
93 	if (uc_drv->priv_auto_alloc_size) {
94 		free(uc->priv);
95 		uc->priv = NULL;
96 	}
97 	list_del(&uc->sibling_node);
98 fail_mem:
99 	free(uc);
100 
101 	return ret;
102 }
103 
104 int uclass_destroy(struct uclass *uc)
105 {
106 	struct uclass_driver *uc_drv;
107 	struct udevice *dev;
108 	int ret;
109 
110 	/*
111 	 * We cannot use list_for_each_entry_safe() here. If a device in this
112 	 * uclass has a child device also in this uclass, it will be also be
113 	 * unbound (by the recursion in the call to device_unbind() below).
114 	 * We can loop until the list is empty.
115 	 */
116 	while (!list_empty(&uc->dev_head)) {
117 		dev = list_first_entry(&uc->dev_head, struct udevice,
118 				       uclass_node);
119 		ret = device_remove(dev, DM_REMOVE_NORMAL);
120 		if (ret)
121 			return ret;
122 		ret = device_unbind(dev);
123 		if (ret)
124 			return ret;
125 	}
126 
127 	uc_drv = uc->uc_drv;
128 	if (uc_drv->destroy)
129 		uc_drv->destroy(uc);
130 	list_del(&uc->sibling_node);
131 	if (uc_drv->priv_auto_alloc_size)
132 		free(uc->priv);
133 	free(uc);
134 
135 	return 0;
136 }
137 
138 int uclass_get(enum uclass_id id, struct uclass **ucp)
139 {
140 	struct uclass *uc;
141 
142 	*ucp = NULL;
143 	uc = uclass_find(id);
144 	if (!uc)
145 		return uclass_add(id, ucp);
146 	*ucp = uc;
147 
148 	return 0;
149 }
150 
151 const char *uclass_get_name(enum uclass_id id)
152 {
153 	struct uclass *uc;
154 
155 	if (uclass_get(id, &uc))
156 		return NULL;
157 	return uc->uc_drv->name;
158 }
159 
160 enum uclass_id uclass_get_by_name(const char *name)
161 {
162 	int i;
163 
164 	for (i = 0; i < UCLASS_COUNT; i++) {
165 		struct uclass_driver *uc_drv = lists_uclass_lookup(i);
166 
167 		if (uc_drv && !strcmp(uc_drv->name, name))
168 			return i;
169 	}
170 
171 	return UCLASS_INVALID;
172 }
173 
174 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
175 {
176 	struct uclass *uc;
177 	struct udevice *dev;
178 	int ret;
179 
180 	*devp = NULL;
181 	ret = uclass_get(id, &uc);
182 	if (ret)
183 		return ret;
184 	if (list_empty(&uc->dev_head))
185 		return -ENODEV;
186 
187 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
188 		if (!index--) {
189 			*devp = dev;
190 			return 0;
191 		}
192 	}
193 
194 	return -ENODEV;
195 }
196 
197 int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
198 {
199 	struct uclass *uc;
200 	int ret;
201 
202 	*devp = NULL;
203 	ret = uclass_get(id, &uc);
204 	if (ret)
205 		return ret;
206 	if (list_empty(&uc->dev_head))
207 		return 0;
208 
209 	*devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
210 
211 	return 0;
212 }
213 
214 int uclass_find_next_device(struct udevice **devp)
215 {
216 	struct udevice *dev = *devp;
217 
218 	*devp = NULL;
219 	if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
220 		return 0;
221 
222 	*devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
223 
224 	return 0;
225 }
226 
227 int uclass_find_device_by_name(enum uclass_id id, const char *name,
228 			       struct udevice **devp)
229 {
230 	struct uclass *uc;
231 	struct udevice *dev;
232 	int ret;
233 
234 	*devp = NULL;
235 	if (!name)
236 		return -EINVAL;
237 	ret = uclass_get(id, &uc);
238 	if (ret)
239 		return ret;
240 
241 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
242 		if (!strncmp(dev->name, name, strlen(name))) {
243 			*devp = dev;
244 			return 0;
245 		}
246 	}
247 
248 	return -ENODEV;
249 }
250 
251 int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
252 			      bool find_req_seq, struct udevice **devp)
253 {
254 	struct uclass *uc;
255 	struct udevice *dev;
256 	int ret;
257 
258 	*devp = NULL;
259 	debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq);
260 	if (seq_or_req_seq == -1)
261 		return -ENODEV;
262 	ret = uclass_get(id, &uc);
263 	if (ret)
264 		return ret;
265 
266 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
267 		debug("   - %d %d '%s'\n", dev->req_seq, dev->seq, dev->name);
268 		if ((find_req_seq ? dev->req_seq : dev->seq) ==
269 				seq_or_req_seq) {
270 			*devp = dev;
271 			debug("   - found\n");
272 			return 0;
273 		}
274 	}
275 	debug("   - not found\n");
276 
277 	return -ENODEV;
278 }
279 
280 int uclass_find_device_by_of_offset(enum uclass_id id, int node,
281 				    struct udevice **devp)
282 {
283 	struct uclass *uc;
284 	struct udevice *dev;
285 	int ret;
286 
287 	*devp = NULL;
288 	if (node < 0)
289 		return -ENODEV;
290 	ret = uclass_get(id, &uc);
291 	if (ret)
292 		return ret;
293 
294 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
295 		if (dev_of_offset(dev) == node) {
296 			*devp = dev;
297 			return 0;
298 		}
299 	}
300 
301 	return -ENODEV;
302 }
303 
304 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
305 				 struct udevice **devp)
306 {
307 	struct uclass *uc;
308 	struct udevice *dev;
309 	int ret;
310 
311 	log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
312 	*devp = NULL;
313 	if (!ofnode_valid(node))
314 		return -ENODEV;
315 	ret = uclass_get(id, &uc);
316 	if (ret)
317 		return ret;
318 
319 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
320 		log(LOGC_DM, LOGL_DEBUG_CONTENT, "      - checking %s\n",
321 		    dev->name);
322 		if (ofnode_equal(dev_ofnode(dev), node)) {
323 			*devp = dev;
324 			goto done;
325 		}
326 	}
327 	ret = -ENODEV;
328 
329 done:
330 	log(LOGC_DM, LOGL_DEBUG, "   - result for %s: %s (ret=%d)\n",
331 	    ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
332 	return ret;
333 }
334 
335 #if CONFIG_IS_ENABLED(OF_CONTROL)
336 static int uclass_find_device_by_phandle(enum uclass_id id,
337 					 struct udevice *parent,
338 					 const char *name,
339 					 struct udevice **devp)
340 {
341 	struct udevice *dev;
342 	struct uclass *uc;
343 	int find_phandle;
344 	int ret;
345 
346 	*devp = NULL;
347 	find_phandle = dev_read_u32_default(parent, name, -1);
348 	if (find_phandle <= 0)
349 		return -ENOENT;
350 	ret = uclass_get(id, &uc);
351 	if (ret)
352 		return ret;
353 
354 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
355 		uint phandle;
356 
357 		phandle = dev_read_phandle(dev);
358 
359 		if (phandle == find_phandle) {
360 			*devp = dev;
361 			return 0;
362 		}
363 	}
364 
365 	return -ENODEV;
366 }
367 #endif
368 
369 int uclass_get_device_by_driver(enum uclass_id id,
370 				const struct driver *find_drv,
371 				struct udevice **devp)
372 {
373 	struct udevice *dev;
374 	struct uclass *uc;
375 	int ret;
376 
377 	ret = uclass_get(id, &uc);
378 	if (ret)
379 		return ret;
380 
381 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
382 		if (dev->driver == find_drv)
383 			return uclass_get_device_tail(dev, 0, devp);
384 	}
385 
386 	return -ENODEV;
387 }
388 
389 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
390 {
391 	if (ret)
392 		return ret;
393 
394 	assert(dev);
395 	ret = device_probe(dev);
396 	if (ret)
397 		return ret;
398 
399 	*devp = dev;
400 
401 	return 0;
402 }
403 
404 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
405 {
406 	struct udevice *dev;
407 	int ret;
408 
409 	*devp = NULL;
410 	ret = uclass_find_device(id, index, &dev);
411 	return uclass_get_device_tail(dev, ret, devp);
412 }
413 
414 int uclass_get_device_by_name(enum uclass_id id, const char *name,
415 			      struct udevice **devp)
416 {
417 	struct udevice *dev;
418 	int ret;
419 
420 	*devp = NULL;
421 	ret = uclass_find_device_by_name(id, name, &dev);
422 	return uclass_get_device_tail(dev, ret, devp);
423 }
424 
425 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
426 {
427 	struct udevice *dev;
428 	int ret;
429 
430 	*devp = NULL;
431 	ret = uclass_find_device_by_seq(id, seq, false, &dev);
432 	if (ret == -ENODEV) {
433 		/*
434 		 * We didn't find it in probed devices. See if there is one
435 		 * that will request this seq if probed.
436 		 */
437 		ret = uclass_find_device_by_seq(id, seq, true, &dev);
438 	}
439 	return uclass_get_device_tail(dev, ret, devp);
440 }
441 
442 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
443 				   struct udevice **devp)
444 {
445 	struct udevice *dev;
446 	int ret;
447 
448 	*devp = NULL;
449 	ret = uclass_find_device_by_of_offset(id, node, &dev);
450 	return uclass_get_device_tail(dev, ret, devp);
451 }
452 
453 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
454 				struct udevice **devp)
455 {
456 	struct udevice *dev;
457 	int ret;
458 
459 	log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
460 	*devp = NULL;
461 	ret = uclass_find_device_by_ofnode(id, node, &dev);
462 	log(LOGC_DM, LOGL_DEBUG, "   - result for %s: %s (ret=%d)\n",
463 	    ofnode_get_name(node), dev ? dev->name : "(none)", ret);
464 
465 	return uclass_get_device_tail(dev, ret, devp);
466 }
467 
468 #if CONFIG_IS_ENABLED(OF_CONTROL)
469 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
470 				    struct udevice **devp)
471 {
472 	struct udevice *dev;
473 	struct uclass *uc;
474 	int ret;
475 
476 	*devp = NULL;
477 	ret = uclass_get(id, &uc);
478 	if (ret)
479 		return ret;
480 
481 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
482 		uint phandle;
483 
484 		phandle = dev_read_phandle(dev);
485 
486 		if (phandle == phandle_id) {
487 			*devp = dev;
488 			return uclass_get_device_tail(dev, ret, devp);
489 		}
490 	}
491 
492 	return -ENODEV;
493 }
494 
495 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
496 				 const char *name, struct udevice **devp)
497 {
498 	struct udevice *dev;
499 	int ret;
500 
501 	*devp = NULL;
502 	ret = uclass_find_device_by_phandle(id, parent, name, &dev);
503 	return uclass_get_device_tail(dev, ret, devp);
504 }
505 #endif
506 
507 int uclass_first_device(enum uclass_id id, struct udevice **devp)
508 {
509 	struct udevice *dev;
510 	int ret;
511 
512 	*devp = NULL;
513 	ret = uclass_find_first_device(id, &dev);
514 	if (!dev)
515 		return 0;
516 	return uclass_get_device_tail(dev, ret, devp);
517 }
518 
519 int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
520 {
521 	int ret;
522 
523 	ret = uclass_first_device(id, devp);
524 	if (ret)
525 		return ret;
526 	else if (!*devp)
527 		return -ENODEV;
528 
529 	return 0;
530 }
531 
532 int uclass_next_device(struct udevice **devp)
533 {
534 	struct udevice *dev = *devp;
535 	int ret;
536 
537 	*devp = NULL;
538 	ret = uclass_find_next_device(&dev);
539 	if (!dev)
540 		return 0;
541 	return uclass_get_device_tail(dev, ret, devp);
542 }
543 
544 int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
545 {
546 	int ret;
547 
548 	*devp = NULL;
549 	ret = uclass_find_first_device(id, devp);
550 	if (ret)
551 		return ret;
552 	if (!*devp)
553 		return 0;
554 
555 	return device_probe(*devp);
556 }
557 
558 int uclass_next_device_check(struct udevice **devp)
559 {
560 	int ret;
561 
562 	ret = uclass_find_next_device(devp);
563 	if (ret)
564 		return ret;
565 	if (!*devp)
566 		return 0;
567 
568 	return device_probe(*devp);
569 }
570 
571 int uclass_bind_device(struct udevice *dev)
572 {
573 	struct uclass *uc;
574 	int ret;
575 
576 	uc = dev->uclass;
577 	list_add_tail(&dev->uclass_node, &uc->dev_head);
578 
579 	if (dev->parent) {
580 		struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
581 
582 		if (uc_drv->child_post_bind) {
583 			ret = uc_drv->child_post_bind(dev);
584 			if (ret)
585 				goto err;
586 		}
587 	}
588 
589 	return 0;
590 err:
591 	/* There is no need to undo the parent's post_bind call */
592 	list_del(&dev->uclass_node);
593 
594 	return ret;
595 }
596 
597 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
598 int uclass_unbind_device(struct udevice *dev)
599 {
600 	struct uclass *uc;
601 	int ret;
602 
603 	uc = dev->uclass;
604 	if (uc->uc_drv->pre_unbind) {
605 		ret = uc->uc_drv->pre_unbind(dev);
606 		if (ret)
607 			return ret;
608 	}
609 
610 	list_del(&dev->uclass_node);
611 	return 0;
612 }
613 #endif
614 
615 int uclass_resolve_seq(struct udevice *dev)
616 {
617 	struct udevice *dup;
618 	int seq;
619 	int ret;
620 
621 	assert(dev->seq == -1);
622 	ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
623 					false, &dup);
624 	if (!ret) {
625 		dm_warn("Device '%s': seq %d is in use by '%s'\n",
626 			dev->name, dev->req_seq, dup->name);
627 	} else if (ret == -ENODEV) {
628 		/* Our requested sequence number is available */
629 		if (dev->req_seq != -1)
630 			return dev->req_seq;
631 	} else {
632 		return ret;
633 	}
634 
635 	for (seq = 0; seq < DM_MAX_SEQ; seq++) {
636 		ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
637 						false, &dup);
638 		if (ret == -ENODEV)
639 			break;
640 		if (ret)
641 			return ret;
642 	}
643 	return seq;
644 }
645 
646 int uclass_pre_probe_device(struct udevice *dev)
647 {
648 	struct uclass_driver *uc_drv;
649 	int ret;
650 
651 	uc_drv = dev->uclass->uc_drv;
652 	if (uc_drv->pre_probe) {
653 		ret = uc_drv->pre_probe(dev);
654 		if (ret)
655 			return ret;
656 	}
657 
658 	if (!dev->parent)
659 		return 0;
660 	uc_drv = dev->parent->uclass->uc_drv;
661 	if (uc_drv->child_pre_probe)
662 		return uc_drv->child_pre_probe(dev);
663 
664 	return 0;
665 }
666 
667 int uclass_post_probe_device(struct udevice *dev)
668 {
669 	struct uclass_driver *uc_drv = dev->uclass->uc_drv;
670 
671 	if (uc_drv->post_probe)
672 		return uc_drv->post_probe(dev);
673 
674 	return 0;
675 }
676 
677 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
678 int uclass_pre_remove_device(struct udevice *dev)
679 {
680 	struct uclass *uc;
681 	int ret;
682 
683 	uc = dev->uclass;
684 	if (uc->uc_drv->pre_remove) {
685 		ret = uc->uc_drv->pre_remove(dev);
686 		if (ret)
687 			return ret;
688 	}
689 
690 	return 0;
691 }
692 #endif
693