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