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