Merge branch 'master' of git://git.denx.de/u-boot-sh
[platform/kernel/u-boot.git] / drivers / core / device.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Device manager
4  *
5  * Copyright (c) 2013 Google, Inc
6  *
7  * (C) Copyright 2012
8  * Pavel Herrmann <morpheus.ibis@gmail.com>
9  */
10
11 #include <common.h>
12 #include <asm/io.h>
13 #include <clk.h>
14 #include <fdtdec.h>
15 #include <fdt_support.h>
16 #include <malloc.h>
17 #include <dm/device.h>
18 #include <dm/device-internal.h>
19 #include <dm/lists.h>
20 #include <dm/of_access.h>
21 #include <dm/pinctrl.h>
22 #include <dm/platdata.h>
23 #include <dm/read.h>
24 #include <dm/uclass.h>
25 #include <dm/uclass-internal.h>
26 #include <dm/util.h>
27 #include <linux/err.h>
28 #include <linux/list.h>
29 #include <power-domain.h>
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 static int device_bind_common(struct udevice *parent, const struct driver *drv,
34                               const char *name, void *platdata,
35                               ulong driver_data, ofnode node,
36                               uint of_platdata_size, struct udevice **devp)
37 {
38         struct udevice *dev;
39         struct uclass *uc;
40         int size, ret = 0;
41
42         if (devp)
43                 *devp = NULL;
44         if (!name)
45                 return -EINVAL;
46
47         ret = uclass_get(drv->id, &uc);
48         if (ret) {
49                 debug("Missing uclass for driver %s\n", drv->name);
50                 return ret;
51         }
52
53         dev = calloc(1, sizeof(struct udevice));
54         if (!dev)
55                 return -ENOMEM;
56
57         INIT_LIST_HEAD(&dev->sibling_node);
58         INIT_LIST_HEAD(&dev->child_head);
59         INIT_LIST_HEAD(&dev->uclass_node);
60 #ifdef CONFIG_DEVRES
61         INIT_LIST_HEAD(&dev->devres_head);
62 #endif
63         dev->platdata = platdata;
64         dev->driver_data = driver_data;
65         dev->name = name;
66         dev->node = node;
67         dev->parent = parent;
68         dev->driver = drv;
69         dev->uclass = uc;
70
71         dev->seq = -1;
72         dev->req_seq = -1;
73         if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
74                 /*
75                  * Some devices, such as a SPI bus, I2C bus and serial ports
76                  * are numbered using aliases.
77                  *
78                  * This is just a 'requested' sequence, and will be
79                  * resolved (and ->seq updated) when the device is probed.
80                  */
81                 if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
82                         if (uc->uc_drv->name && ofnode_valid(node)) {
83                                 dev_read_alias_seq(dev, &dev->req_seq);
84                         }
85                 }
86         }
87
88         if (drv->platdata_auto_alloc_size) {
89                 bool alloc = !platdata;
90
91                 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
92                         if (of_platdata_size) {
93                                 dev->flags |= DM_FLAG_OF_PLATDATA;
94                                 if (of_platdata_size <
95                                                 drv->platdata_auto_alloc_size)
96                                         alloc = true;
97                         }
98                 }
99                 if (alloc) {
100                         dev->flags |= DM_FLAG_ALLOC_PDATA;
101                         dev->platdata = calloc(1,
102                                                drv->platdata_auto_alloc_size);
103                         if (!dev->platdata) {
104                                 ret = -ENOMEM;
105                                 goto fail_alloc1;
106                         }
107                         if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
108                                 memcpy(dev->platdata, platdata,
109                                        of_platdata_size);
110                         }
111                 }
112         }
113
114         size = uc->uc_drv->per_device_platdata_auto_alloc_size;
115         if (size) {
116                 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
117                 dev->uclass_platdata = calloc(1, size);
118                 if (!dev->uclass_platdata) {
119                         ret = -ENOMEM;
120                         goto fail_alloc2;
121                 }
122         }
123
124         if (parent) {
125                 size = parent->driver->per_child_platdata_auto_alloc_size;
126                 if (!size) {
127                         size = parent->uclass->uc_drv->
128                                         per_child_platdata_auto_alloc_size;
129                 }
130                 if (size) {
131                         dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
132                         dev->parent_platdata = calloc(1, size);
133                         if (!dev->parent_platdata) {
134                                 ret = -ENOMEM;
135                                 goto fail_alloc3;
136                         }
137                 }
138         }
139
140         /* put dev into parent's successor list */
141         if (parent)
142                 list_add_tail(&dev->sibling_node, &parent->child_head);
143
144         ret = uclass_bind_device(dev);
145         if (ret)
146                 goto fail_uclass_bind;
147
148         /* if we fail to bind we remove device from successors and free it */
149         if (drv->bind) {
150                 ret = drv->bind(dev);
151                 if (ret)
152                         goto fail_bind;
153         }
154         if (parent && parent->driver->child_post_bind) {
155                 ret = parent->driver->child_post_bind(dev);
156                 if (ret)
157                         goto fail_child_post_bind;
158         }
159         if (uc->uc_drv->post_bind) {
160                 ret = uc->uc_drv->post_bind(dev);
161                 if (ret)
162                         goto fail_uclass_post_bind;
163         }
164
165         if (parent)
166                 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
167         if (devp)
168                 *devp = dev;
169
170         dev->flags |= DM_FLAG_BOUND;
171
172         return 0;
173
174 fail_uclass_post_bind:
175         /* There is no child unbind() method, so no clean-up required */
176 fail_child_post_bind:
177         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
178                 if (drv->unbind && drv->unbind(dev)) {
179                         dm_warn("unbind() method failed on dev '%s' on error path\n",
180                                 dev->name);
181                 }
182         }
183
184 fail_bind:
185         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
186                 if (uclass_unbind_device(dev)) {
187                         dm_warn("Failed to unbind dev '%s' on error path\n",
188                                 dev->name);
189                 }
190         }
191 fail_uclass_bind:
192         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
193                 list_del(&dev->sibling_node);
194                 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
195                         free(dev->parent_platdata);
196                         dev->parent_platdata = NULL;
197                 }
198         }
199 fail_alloc3:
200         if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
201                 free(dev->uclass_platdata);
202                 dev->uclass_platdata = NULL;
203         }
204 fail_alloc2:
205         if (dev->flags & DM_FLAG_ALLOC_PDATA) {
206                 free(dev->platdata);
207                 dev->platdata = NULL;
208         }
209 fail_alloc1:
210         devres_release_all(dev);
211
212         free(dev);
213
214         return ret;
215 }
216
217 int device_bind_with_driver_data(struct udevice *parent,
218                                  const struct driver *drv, const char *name,
219                                  ulong driver_data, ofnode node,
220                                  struct udevice **devp)
221 {
222         return device_bind_common(parent, drv, name, NULL, driver_data, node,
223                                   0, devp);
224 }
225
226 int device_bind(struct udevice *parent, const struct driver *drv,
227                 const char *name, void *platdata, int of_offset,
228                 struct udevice **devp)
229 {
230         return device_bind_common(parent, drv, name, platdata, 0,
231                                   offset_to_ofnode(of_offset), 0, devp);
232 }
233
234 int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
235                        const char *name, void *platdata, ofnode node,
236                        struct udevice **devp)
237 {
238         return device_bind_common(parent, drv, name, platdata, 0, node, 0,
239                                   devp);
240 }
241
242 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
243                         const struct driver_info *info, struct udevice **devp)
244 {
245         struct driver *drv;
246         uint platdata_size = 0;
247
248         drv = lists_driver_lookup_name(info->name);
249         if (!drv)
250                 return -ENOENT;
251         if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
252                 return -EPERM;
253
254 #if CONFIG_IS_ENABLED(OF_PLATDATA)
255         platdata_size = info->platdata_size;
256 #endif
257         return device_bind_common(parent, drv, info->name,
258                         (void *)info->platdata, 0, ofnode_null(), platdata_size,
259                         devp);
260 }
261
262 static void *alloc_priv(int size, uint flags)
263 {
264         void *priv;
265
266         if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
267                 size = ROUND(size, ARCH_DMA_MINALIGN);
268                 priv = memalign(ARCH_DMA_MINALIGN, size);
269                 if (priv) {
270                         memset(priv, '\0', size);
271
272                         /*
273                          * Ensure that the zero bytes are flushed to memory.
274                          * This prevents problems if the driver uses this as
275                          * both an input and an output buffer:
276                          *
277                          * 1. Zeroes written to buffer (here) and sit in the
278                          *      cache
279                          * 2. Driver issues a read command to DMA
280                          * 3. CPU runs out of cache space and evicts some cache
281                          *      data in the buffer, writing zeroes to RAM from
282                          *      the memset() above
283                          * 4. DMA completes
284                          * 5. Buffer now has some DMA data and some zeroes
285                          * 6. Data being read is now incorrect
286                          *
287                          * To prevent this, ensure that the cache is clean
288                          * within this range at the start. The driver can then
289                          * use normal flush-after-write, invalidate-before-read
290                          * procedures.
291                          *
292                          * TODO(sjg@chromium.org): Drop this microblaze
293                          * exception.
294                          */
295 #ifndef CONFIG_MICROBLAZE
296                         flush_dcache_range((ulong)priv, (ulong)priv + size);
297 #endif
298                 }
299         } else {
300                 priv = calloc(1, size);
301         }
302
303         return priv;
304 }
305
306 int device_probe(struct udevice *dev)
307 {
308         struct power_domain pd;
309         const struct driver *drv;
310         int size = 0;
311         int ret;
312         int seq;
313
314         if (!dev)
315                 return -EINVAL;
316
317         if (dev->flags & DM_FLAG_ACTIVATED)
318                 return 0;
319
320         drv = dev->driver;
321         assert(drv);
322
323         /* Allocate private data if requested and not reentered */
324         if (drv->priv_auto_alloc_size && !dev->priv) {
325                 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
326                 if (!dev->priv) {
327                         ret = -ENOMEM;
328                         goto fail;
329                 }
330         }
331         /* Allocate private data if requested and not reentered */
332         size = dev->uclass->uc_drv->per_device_auto_alloc_size;
333         if (size && !dev->uclass_priv) {
334                 dev->uclass_priv = calloc(1, size);
335                 if (!dev->uclass_priv) {
336                         ret = -ENOMEM;
337                         goto fail;
338                 }
339         }
340
341         /* Ensure all parents are probed */
342         if (dev->parent) {
343                 size = dev->parent->driver->per_child_auto_alloc_size;
344                 if (!size) {
345                         size = dev->parent->uclass->uc_drv->
346                                         per_child_auto_alloc_size;
347                 }
348                 if (size && !dev->parent_priv) {
349                         dev->parent_priv = alloc_priv(size, drv->flags);
350                         if (!dev->parent_priv) {
351                                 ret = -ENOMEM;
352                                 goto fail;
353                         }
354                 }
355
356                 ret = device_probe(dev->parent);
357                 if (ret)
358                         goto fail;
359
360                 /*
361                  * The device might have already been probed during
362                  * the call to device_probe() on its parent device
363                  * (e.g. PCI bridge devices). Test the flags again
364                  * so that we don't mess up the device.
365                  */
366                 if (dev->flags & DM_FLAG_ACTIVATED)
367                         return 0;
368         }
369
370         seq = uclass_resolve_seq(dev);
371         if (seq < 0) {
372                 ret = seq;
373                 goto fail;
374         }
375         dev->seq = seq;
376
377         dev->flags |= DM_FLAG_ACTIVATED;
378
379         /*
380          * Process pinctrl for everything except the root device, and
381          * continue regardless of the result of pinctrl. Don't process pinctrl
382          * settings for pinctrl devices since the device may not yet be
383          * probed.
384          */
385         if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
386                 pinctrl_select_state(dev, "default");
387
388         if (dev->parent && device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) {
389                 if (!power_domain_get(dev, &pd))
390                         power_domain_on(&pd);
391         }
392
393         ret = uclass_pre_probe_device(dev);
394         if (ret)
395                 goto fail;
396
397         if (dev->parent && dev->parent->driver->child_pre_probe) {
398                 ret = dev->parent->driver->child_pre_probe(dev);
399                 if (ret)
400                         goto fail;
401         }
402
403         if (drv->ofdata_to_platdata && dev_has_of_node(dev)) {
404                 ret = drv->ofdata_to_platdata(dev);
405                 if (ret)
406                         goto fail;
407         }
408
409         /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */
410         ret = clk_set_defaults(dev);
411         if (ret)
412                 goto fail;
413
414         if (drv->probe) {
415                 ret = drv->probe(dev);
416                 if (ret) {
417                         dev->flags &= ~DM_FLAG_ACTIVATED;
418                         goto fail;
419                 }
420         }
421
422         ret = uclass_post_probe_device(dev);
423         if (ret)
424                 goto fail_uclass;
425
426         if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
427                 pinctrl_select_state(dev, "default");
428
429         return 0;
430 fail_uclass:
431         if (device_remove(dev, DM_REMOVE_NORMAL)) {
432                 dm_warn("%s: Device '%s' failed to remove on error path\n",
433                         __func__, dev->name);
434         }
435 fail:
436         dev->flags &= ~DM_FLAG_ACTIVATED;
437
438         dev->seq = -1;
439         device_free(dev);
440
441         return ret;
442 }
443
444 void *dev_get_platdata(struct udevice *dev)
445 {
446         if (!dev) {
447                 dm_warn("%s: null device\n", __func__);
448                 return NULL;
449         }
450
451         return dev->platdata;
452 }
453
454 void *dev_get_parent_platdata(struct udevice *dev)
455 {
456         if (!dev) {
457                 dm_warn("%s: null device\n", __func__);
458                 return NULL;
459         }
460
461         return dev->parent_platdata;
462 }
463
464 void *dev_get_uclass_platdata(struct udevice *dev)
465 {
466         if (!dev) {
467                 dm_warn("%s: null device\n", __func__);
468                 return NULL;
469         }
470
471         return dev->uclass_platdata;
472 }
473
474 void *dev_get_priv(struct udevice *dev)
475 {
476         if (!dev) {
477                 dm_warn("%s: null device\n", __func__);
478                 return NULL;
479         }
480
481         return dev->priv;
482 }
483
484 void *dev_get_uclass_priv(struct udevice *dev)
485 {
486         if (!dev) {
487                 dm_warn("%s: null device\n", __func__);
488                 return NULL;
489         }
490
491         return dev->uclass_priv;
492 }
493
494 void *dev_get_parent_priv(struct udevice *dev)
495 {
496         if (!dev) {
497                 dm_warn("%s: null device\n", __func__);
498                 return NULL;
499         }
500
501         return dev->parent_priv;
502 }
503
504 static int device_get_device_tail(struct udevice *dev, int ret,
505                                   struct udevice **devp)
506 {
507         if (ret)
508                 return ret;
509
510         ret = device_probe(dev);
511         if (ret)
512                 return ret;
513
514         *devp = dev;
515
516         return 0;
517 }
518
519 /**
520  * device_find_by_ofnode() - Return device associated with given ofnode
521  *
522  * The returned device is *not* activated.
523  *
524  * @node: The ofnode for which a associated device should be looked up
525  * @devp: Pointer to structure to hold the found device
526  * Return: 0 if OK, -ve on error
527  */
528 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
529 {
530         struct uclass *uc;
531         struct udevice *dev;
532         int ret;
533
534         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
535                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
536                                                    &dev);
537                 if (!ret || dev) {
538                         *devp = dev;
539                         return 0;
540                 }
541         }
542
543         return -ENODEV;
544 }
545
546 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
547 {
548         struct udevice *dev;
549
550         list_for_each_entry(dev, &parent->child_head, sibling_node) {
551                 if (!index--)
552                         return device_get_device_tail(dev, 0, devp);
553         }
554
555         return -ENODEV;
556 }
557
558 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
559                              bool find_req_seq, struct udevice **devp)
560 {
561         struct udevice *dev;
562
563         *devp = NULL;
564         if (seq_or_req_seq == -1)
565                 return -ENODEV;
566
567         list_for_each_entry(dev, &parent->child_head, sibling_node) {
568                 if ((find_req_seq ? dev->req_seq : dev->seq) ==
569                                 seq_or_req_seq) {
570                         *devp = dev;
571                         return 0;
572                 }
573         }
574
575         return -ENODEV;
576 }
577
578 int device_get_child_by_seq(struct udevice *parent, int seq,
579                             struct udevice **devp)
580 {
581         struct udevice *dev;
582         int ret;
583
584         *devp = NULL;
585         ret = device_find_child_by_seq(parent, seq, false, &dev);
586         if (ret == -ENODEV) {
587                 /*
588                  * We didn't find it in probed devices. See if there is one
589                  * that will request this seq if probed.
590                  */
591                 ret = device_find_child_by_seq(parent, seq, true, &dev);
592         }
593         return device_get_device_tail(dev, ret, devp);
594 }
595
596 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
597                                    struct udevice **devp)
598 {
599         struct udevice *dev;
600
601         *devp = NULL;
602
603         list_for_each_entry(dev, &parent->child_head, sibling_node) {
604                 if (dev_of_offset(dev) == of_offset) {
605                         *devp = dev;
606                         return 0;
607                 }
608         }
609
610         return -ENODEV;
611 }
612
613 int device_get_child_by_of_offset(struct udevice *parent, int node,
614                                   struct udevice **devp)
615 {
616         struct udevice *dev;
617         int ret;
618
619         *devp = NULL;
620         ret = device_find_child_by_of_offset(parent, node, &dev);
621         return device_get_device_tail(dev, ret, devp);
622 }
623
624 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
625                                                      ofnode ofnode)
626 {
627         struct udevice *dev, *found;
628
629         if (ofnode_equal(dev_ofnode(parent), ofnode))
630                 return parent;
631
632         list_for_each_entry(dev, &parent->child_head, sibling_node) {
633                 found = _device_find_global_by_ofnode(dev, ofnode);
634                 if (found)
635                         return found;
636         }
637
638         return NULL;
639 }
640
641 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
642 {
643         *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
644
645         return *devp ? 0 : -ENOENT;
646 }
647
648 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
649 {
650         struct udevice *dev;
651
652         dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
653         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
654 }
655
656 int device_find_first_child(struct udevice *parent, struct udevice **devp)
657 {
658         if (list_empty(&parent->child_head)) {
659                 *devp = NULL;
660         } else {
661                 *devp = list_first_entry(&parent->child_head, struct udevice,
662                                          sibling_node);
663         }
664
665         return 0;
666 }
667
668 int device_find_next_child(struct udevice **devp)
669 {
670         struct udevice *dev = *devp;
671         struct udevice *parent = dev->parent;
672
673         if (list_is_last(&dev->sibling_node, &parent->child_head)) {
674                 *devp = NULL;
675         } else {
676                 *devp = list_entry(dev->sibling_node.next, struct udevice,
677                                    sibling_node);
678         }
679
680         return 0;
681 }
682
683 struct udevice *dev_get_parent(struct udevice *child)
684 {
685         return child->parent;
686 }
687
688 ulong dev_get_driver_data(struct udevice *dev)
689 {
690         return dev->driver_data;
691 }
692
693 const void *dev_get_driver_ops(struct udevice *dev)
694 {
695         if (!dev || !dev->driver->ops)
696                 return NULL;
697
698         return dev->driver->ops;
699 }
700
701 enum uclass_id device_get_uclass_id(struct udevice *dev)
702 {
703         return dev->uclass->uc_drv->id;
704 }
705
706 const char *dev_get_uclass_name(struct udevice *dev)
707 {
708         if (!dev)
709                 return NULL;
710
711         return dev->uclass->uc_drv->name;
712 }
713
714 bool device_has_children(struct udevice *dev)
715 {
716         return !list_empty(&dev->child_head);
717 }
718
719 bool device_has_active_children(struct udevice *dev)
720 {
721         struct udevice *child;
722
723         for (device_find_first_child(dev, &child);
724              child;
725              device_find_next_child(&child)) {
726                 if (device_active(child))
727                         return true;
728         }
729
730         return false;
731 }
732
733 bool device_is_last_sibling(struct udevice *dev)
734 {
735         struct udevice *parent = dev->parent;
736
737         if (!parent)
738                 return false;
739         return list_is_last(&dev->sibling_node, &parent->child_head);
740 }
741
742 void device_set_name_alloced(struct udevice *dev)
743 {
744         dev->flags |= DM_FLAG_NAME_ALLOCED;
745 }
746
747 int device_set_name(struct udevice *dev, const char *name)
748 {
749         name = strdup(name);
750         if (!name)
751                 return -ENOMEM;
752         dev->name = name;
753         device_set_name_alloced(dev);
754
755         return 0;
756 }
757
758 bool device_is_compatible(struct udevice *dev, const char *compat)
759 {
760         return ofnode_device_is_compatible(dev_ofnode(dev), compat);
761 }
762
763 bool of_machine_is_compatible(const char *compat)
764 {
765         const void *fdt = gd->fdt_blob;
766
767         return !fdt_node_check_compatible(fdt, 0, compat);
768 }
769
770 int dev_disable_by_path(const char *path)
771 {
772         struct uclass *uc;
773         ofnode node = ofnode_path(path);
774         struct udevice *dev;
775         int ret = 1;
776
777         if (!of_live_active())
778                 return -ENOSYS;
779
780         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
781                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
782                 if (!ret)
783                         break;
784         }
785
786         if (ret)
787                 return ret;
788
789         ret = device_remove(dev, DM_REMOVE_NORMAL);
790         if (ret)
791                 return ret;
792
793         ret = device_unbind(dev);
794         if (ret)
795                 return ret;
796
797         return ofnode_set_enabled(node, false);
798 }
799
800 int dev_enable_by_path(const char *path)
801 {
802         ofnode node = ofnode_path(path);
803         ofnode pnode = ofnode_get_parent(node);
804         struct udevice *parent;
805         int ret = 1;
806
807         if (!of_live_active())
808                 return -ENOSYS;
809
810         ret = device_find_by_ofnode(pnode, &parent);
811         if (ret)
812                 return ret;
813
814         ret = ofnode_set_enabled(node, true);
815         if (ret)
816                 return ret;
817
818         return lists_bind_fdt(parent, node, NULL);
819 }