xref: /openbmc/u-boot/drivers/core/device.c (revision 4349b55b)
1 /*
2  * Device manager
3  *
4  * Copyright (c) 2013 Google, Inc
5  *
6  * (C) Copyright 2012
7  * Pavel Herrmann <morpheus.ibis@gmail.com>
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #include <common.h>
13 #include <fdtdec.h>
14 #include <fdt_support.h>
15 #include <malloc.h>
16 #include <dm/device.h>
17 #include <dm/device-internal.h>
18 #include <dm/lists.h>
19 #include <dm/pinctrl.h>
20 #include <dm/platdata.h>
21 #include <dm/uclass.h>
22 #include <dm/uclass-internal.h>
23 #include <dm/util.h>
24 #include <linux/err.h>
25 #include <linux/list.h>
26 
27 DECLARE_GLOBAL_DATA_PTR;
28 
29 int device_bind(struct udevice *parent, const struct driver *drv,
30 		const char *name, void *platdata, int of_offset,
31 		struct udevice **devp)
32 {
33 	struct udevice *dev;
34 	struct uclass *uc;
35 	int size, ret = 0;
36 
37 	if (devp)
38 		*devp = NULL;
39 	if (!name)
40 		return -EINVAL;
41 
42 	ret = uclass_get(drv->id, &uc);
43 	if (ret) {
44 		debug("Missing uclass for driver %s\n", drv->name);
45 		return ret;
46 	}
47 
48 	dev = calloc(1, sizeof(struct udevice));
49 	if (!dev)
50 		return -ENOMEM;
51 
52 	INIT_LIST_HEAD(&dev->sibling_node);
53 	INIT_LIST_HEAD(&dev->child_head);
54 	INIT_LIST_HEAD(&dev->uclass_node);
55 #ifdef CONFIG_DEVRES
56 	INIT_LIST_HEAD(&dev->devres_head);
57 #endif
58 	dev->platdata = platdata;
59 	dev->name = name;
60 	dev->of_offset = of_offset;
61 	dev->parent = parent;
62 	dev->driver = drv;
63 	dev->uclass = uc;
64 
65 	dev->seq = -1;
66 	dev->req_seq = -1;
67 	if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
68 		/*
69 		 * Some devices, such as a SPI bus, I2C bus and serial ports
70 		 * are numbered using aliases.
71 		 *
72 		 * This is just a 'requested' sequence, and will be
73 		 * resolved (and ->seq updated) when the device is probed.
74 		 */
75 		if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
76 			if (uc->uc_drv->name && of_offset != -1) {
77 				fdtdec_get_alias_seq(gd->fdt_blob,
78 						uc->uc_drv->name, of_offset,
79 						&dev->req_seq);
80 			}
81 		}
82 	}
83 
84 	if (!dev->platdata && drv->platdata_auto_alloc_size) {
85 		dev->flags |= DM_FLAG_ALLOC_PDATA;
86 		dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
87 		if (!dev->platdata) {
88 			ret = -ENOMEM;
89 			goto fail_alloc1;
90 		}
91 	}
92 
93 	size = uc->uc_drv->per_device_platdata_auto_alloc_size;
94 	if (size) {
95 		dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
96 		dev->uclass_platdata = calloc(1, size);
97 		if (!dev->uclass_platdata) {
98 			ret = -ENOMEM;
99 			goto fail_alloc2;
100 		}
101 	}
102 
103 	if (parent) {
104 		size = parent->driver->per_child_platdata_auto_alloc_size;
105 		if (!size) {
106 			size = parent->uclass->uc_drv->
107 					per_child_platdata_auto_alloc_size;
108 		}
109 		if (size) {
110 			dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
111 			dev->parent_platdata = calloc(1, size);
112 			if (!dev->parent_platdata) {
113 				ret = -ENOMEM;
114 				goto fail_alloc3;
115 			}
116 		}
117 	}
118 
119 	/* put dev into parent's successor list */
120 	if (parent)
121 		list_add_tail(&dev->sibling_node, &parent->child_head);
122 
123 	ret = uclass_bind_device(dev);
124 	if (ret)
125 		goto fail_uclass_bind;
126 
127 	/* if we fail to bind we remove device from successors and free it */
128 	if (drv->bind) {
129 		ret = drv->bind(dev);
130 		if (ret)
131 			goto fail_bind;
132 	}
133 	if (parent && parent->driver->child_post_bind) {
134 		ret = parent->driver->child_post_bind(dev);
135 		if (ret)
136 			goto fail_child_post_bind;
137 	}
138 	if (uc->uc_drv->post_bind) {
139 		ret = uc->uc_drv->post_bind(dev);
140 		if (ret)
141 			goto fail_uclass_post_bind;
142 	}
143 
144 	if (parent)
145 		dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
146 	if (devp)
147 		*devp = dev;
148 
149 	dev->flags |= DM_FLAG_BOUND;
150 
151 	return 0;
152 
153 fail_uclass_post_bind:
154 	/* There is no child unbind() method, so no clean-up required */
155 fail_child_post_bind:
156 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
157 		if (drv->unbind && drv->unbind(dev)) {
158 			dm_warn("unbind() method failed on dev '%s' on error path\n",
159 				dev->name);
160 		}
161 	}
162 
163 fail_bind:
164 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
165 		if (uclass_unbind_device(dev)) {
166 			dm_warn("Failed to unbind dev '%s' on error path\n",
167 				dev->name);
168 		}
169 	}
170 fail_uclass_bind:
171 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
172 		list_del(&dev->sibling_node);
173 		if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
174 			free(dev->parent_platdata);
175 			dev->parent_platdata = NULL;
176 		}
177 	}
178 fail_alloc3:
179 	if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
180 		free(dev->uclass_platdata);
181 		dev->uclass_platdata = NULL;
182 	}
183 fail_alloc2:
184 	if (dev->flags & DM_FLAG_ALLOC_PDATA) {
185 		free(dev->platdata);
186 		dev->platdata = NULL;
187 	}
188 fail_alloc1:
189 	devres_release_all(dev);
190 
191 	free(dev);
192 
193 	return ret;
194 }
195 
196 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
197 			const struct driver_info *info, struct udevice **devp)
198 {
199 	struct driver *drv;
200 
201 	drv = lists_driver_lookup_name(info->name);
202 	if (!drv)
203 		return -ENOENT;
204 	if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
205 		return -EPERM;
206 
207 	return device_bind(parent, drv, info->name, (void *)info->platdata,
208 			   -1, devp);
209 }
210 
211 static void *alloc_priv(int size, uint flags)
212 {
213 	void *priv;
214 
215 	if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
216 		priv = memalign(ARCH_DMA_MINALIGN, size);
217 		if (priv)
218 			memset(priv, '\0', size);
219 	} else {
220 		priv = calloc(1, size);
221 	}
222 
223 	return priv;
224 }
225 
226 int device_probe(struct udevice *dev)
227 {
228 	const struct driver *drv;
229 	int size = 0;
230 	int ret;
231 	int seq;
232 
233 	if (!dev)
234 		return -EINVAL;
235 
236 	if (dev->flags & DM_FLAG_ACTIVATED)
237 		return 0;
238 
239 	drv = dev->driver;
240 	assert(drv);
241 
242 	/* Allocate private data if requested and not reentered */
243 	if (drv->priv_auto_alloc_size && !dev->priv) {
244 		dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
245 		if (!dev->priv) {
246 			ret = -ENOMEM;
247 			goto fail;
248 		}
249 	}
250 	/* Allocate private data if requested and not reentered */
251 	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
252 	if (size && !dev->uclass_priv) {
253 		dev->uclass_priv = calloc(1, size);
254 		if (!dev->uclass_priv) {
255 			ret = -ENOMEM;
256 			goto fail;
257 		}
258 	}
259 
260 	/* Ensure all parents are probed */
261 	if (dev->parent) {
262 		size = dev->parent->driver->per_child_auto_alloc_size;
263 		if (!size) {
264 			size = dev->parent->uclass->uc_drv->
265 					per_child_auto_alloc_size;
266 		}
267 		if (size && !dev->parent_priv) {
268 			dev->parent_priv = alloc_priv(size, drv->flags);
269 			if (!dev->parent_priv) {
270 				ret = -ENOMEM;
271 				goto fail;
272 			}
273 		}
274 
275 		ret = device_probe(dev->parent);
276 		if (ret)
277 			goto fail;
278 
279 		/*
280 		 * The device might have already been probed during
281 		 * the call to device_probe() on its parent device
282 		 * (e.g. PCI bridge devices). Test the flags again
283 		 * so that we don't mess up the device.
284 		 */
285 		if (dev->flags & DM_FLAG_ACTIVATED)
286 			return 0;
287 	}
288 
289 	seq = uclass_resolve_seq(dev);
290 	if (seq < 0) {
291 		ret = seq;
292 		goto fail;
293 	}
294 	dev->seq = seq;
295 
296 	dev->flags |= DM_FLAG_ACTIVATED;
297 
298 	/*
299 	 * Process pinctrl for everything except the root device, and
300 	 * continue regardless of the result of pinctrl. Don't process pinctrl
301 	 * settings for pinctrl devices since the device may not yet be
302 	 * probed.
303 	 */
304 	if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
305 		pinctrl_select_state(dev, "default");
306 
307 	ret = uclass_pre_probe_device(dev);
308 	if (ret)
309 		goto fail;
310 
311 	if (dev->parent && dev->parent->driver->child_pre_probe) {
312 		ret = dev->parent->driver->child_pre_probe(dev);
313 		if (ret)
314 			goto fail;
315 	}
316 
317 	if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
318 		ret = drv->ofdata_to_platdata(dev);
319 		if (ret)
320 			goto fail;
321 	}
322 
323 	if (drv->probe) {
324 		ret = drv->probe(dev);
325 		if (ret) {
326 			dev->flags &= ~DM_FLAG_ACTIVATED;
327 			goto fail;
328 		}
329 	}
330 
331 	ret = uclass_post_probe_device(dev);
332 	if (ret)
333 		goto fail_uclass;
334 
335 	if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
336 		pinctrl_select_state(dev, "default");
337 
338 	return 0;
339 fail_uclass:
340 	if (device_remove(dev)) {
341 		dm_warn("%s: Device '%s' failed to remove on error path\n",
342 			__func__, dev->name);
343 	}
344 fail:
345 	dev->flags &= ~DM_FLAG_ACTIVATED;
346 
347 	dev->seq = -1;
348 	device_free(dev);
349 
350 	return ret;
351 }
352 
353 void *dev_get_platdata(struct udevice *dev)
354 {
355 	if (!dev) {
356 		dm_warn("%s: null device\n", __func__);
357 		return NULL;
358 	}
359 
360 	return dev->platdata;
361 }
362 
363 void *dev_get_parent_platdata(struct udevice *dev)
364 {
365 	if (!dev) {
366 		dm_warn("%s: null device\n", __func__);
367 		return NULL;
368 	}
369 
370 	return dev->parent_platdata;
371 }
372 
373 void *dev_get_uclass_platdata(struct udevice *dev)
374 {
375 	if (!dev) {
376 		dm_warn("%s: null device\n", __func__);
377 		return NULL;
378 	}
379 
380 	return dev->uclass_platdata;
381 }
382 
383 void *dev_get_priv(struct udevice *dev)
384 {
385 	if (!dev) {
386 		dm_warn("%s: null device\n", __func__);
387 		return NULL;
388 	}
389 
390 	return dev->priv;
391 }
392 
393 void *dev_get_uclass_priv(struct udevice *dev)
394 {
395 	if (!dev) {
396 		dm_warn("%s: null device\n", __func__);
397 		return NULL;
398 	}
399 
400 	return dev->uclass_priv;
401 }
402 
403 void *dev_get_parent_priv(struct udevice *dev)
404 {
405 	if (!dev) {
406 		dm_warn("%s: null device\n", __func__);
407 		return NULL;
408 	}
409 
410 	return dev->parent_priv;
411 }
412 
413 static int device_get_device_tail(struct udevice *dev, int ret,
414 				  struct udevice **devp)
415 {
416 	if (ret)
417 		return ret;
418 
419 	ret = device_probe(dev);
420 	if (ret)
421 		return ret;
422 
423 	*devp = dev;
424 
425 	return 0;
426 }
427 
428 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
429 {
430 	struct udevice *dev;
431 
432 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
433 		if (!index--)
434 			return device_get_device_tail(dev, 0, devp);
435 	}
436 
437 	return -ENODEV;
438 }
439 
440 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
441 			     bool find_req_seq, struct udevice **devp)
442 {
443 	struct udevice *dev;
444 
445 	*devp = NULL;
446 	if (seq_or_req_seq == -1)
447 		return -ENODEV;
448 
449 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
450 		if ((find_req_seq ? dev->req_seq : dev->seq) ==
451 				seq_or_req_seq) {
452 			*devp = dev;
453 			return 0;
454 		}
455 	}
456 
457 	return -ENODEV;
458 }
459 
460 int device_get_child_by_seq(struct udevice *parent, int seq,
461 			    struct udevice **devp)
462 {
463 	struct udevice *dev;
464 	int ret;
465 
466 	*devp = NULL;
467 	ret = device_find_child_by_seq(parent, seq, false, &dev);
468 	if (ret == -ENODEV) {
469 		/*
470 		 * We didn't find it in probed devices. See if there is one
471 		 * that will request this seq if probed.
472 		 */
473 		ret = device_find_child_by_seq(parent, seq, true, &dev);
474 	}
475 	return device_get_device_tail(dev, ret, devp);
476 }
477 
478 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
479 				   struct udevice **devp)
480 {
481 	struct udevice *dev;
482 
483 	*devp = NULL;
484 
485 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
486 		if (dev->of_offset == of_offset) {
487 			*devp = dev;
488 			return 0;
489 		}
490 	}
491 
492 	return -ENODEV;
493 }
494 
495 int device_get_child_by_of_offset(struct udevice *parent, int node,
496 				  struct udevice **devp)
497 {
498 	struct udevice *dev;
499 	int ret;
500 
501 	*devp = NULL;
502 	ret = device_find_child_by_of_offset(parent, node, &dev);
503 	return device_get_device_tail(dev, ret, devp);
504 }
505 
506 static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
507 							int of_offset)
508 {
509 	struct udevice *dev, *found;
510 
511 	if (parent->of_offset == of_offset)
512 		return parent;
513 
514 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
515 		found = _device_find_global_by_of_offset(dev, of_offset);
516 		if (found)
517 			return found;
518 	}
519 
520 	return NULL;
521 }
522 
523 int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
524 {
525 	struct udevice *dev;
526 
527 	dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
528 	return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
529 }
530 
531 int device_find_first_child(struct udevice *parent, struct udevice **devp)
532 {
533 	if (list_empty(&parent->child_head)) {
534 		*devp = NULL;
535 	} else {
536 		*devp = list_first_entry(&parent->child_head, struct udevice,
537 					 sibling_node);
538 	}
539 
540 	return 0;
541 }
542 
543 int device_find_next_child(struct udevice **devp)
544 {
545 	struct udevice *dev = *devp;
546 	struct udevice *parent = dev->parent;
547 
548 	if (list_is_last(&dev->sibling_node, &parent->child_head)) {
549 		*devp = NULL;
550 	} else {
551 		*devp = list_entry(dev->sibling_node.next, struct udevice,
552 				   sibling_node);
553 	}
554 
555 	return 0;
556 }
557 
558 struct udevice *dev_get_parent(struct udevice *child)
559 {
560 	return child->parent;
561 }
562 
563 ulong dev_get_driver_data(struct udevice *dev)
564 {
565 	return dev->driver_data;
566 }
567 
568 const void *dev_get_driver_ops(struct udevice *dev)
569 {
570 	if (!dev || !dev->driver->ops)
571 		return NULL;
572 
573 	return dev->driver->ops;
574 }
575 
576 enum uclass_id device_get_uclass_id(struct udevice *dev)
577 {
578 	return dev->uclass->uc_drv->id;
579 }
580 
581 const char *dev_get_uclass_name(struct udevice *dev)
582 {
583 	if (!dev)
584 		return NULL;
585 
586 	return dev->uclass->uc_drv->name;
587 }
588 
589 fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
590 {
591 #if CONFIG_IS_ENABLED(OF_CONTROL)
592 	fdt_addr_t addr;
593 
594 	if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
595 		const fdt32_t *reg;
596 		int len = 0;
597 		int na, ns;
598 
599 		na = fdt_address_cells(gd->fdt_blob, dev->parent->of_offset);
600 		if (na < 1) {
601 			debug("bad #address-cells\n");
602 			return FDT_ADDR_T_NONE;
603 		}
604 
605 		ns = fdt_size_cells(gd->fdt_blob, dev->parent->of_offset);
606 		if (ns < 0) {
607 			debug("bad #size-cells\n");
608 			return FDT_ADDR_T_NONE;
609 		}
610 
611 		reg = fdt_getprop(gd->fdt_blob, dev->of_offset, "reg", &len);
612 		if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
613 			debug("Req index out of range\n");
614 			return FDT_ADDR_T_NONE;
615 		}
616 
617 		reg += index * (na + ns);
618 
619 		/*
620 		 * Use the full-fledged translate function for complex
621 		 * bus setups.
622 		 */
623 		addr = fdt_translate_address((void *)gd->fdt_blob,
624 					     dev->of_offset, reg);
625 	} else {
626 		/*
627 		 * Use the "simple" translate function for less complex
628 		 * bus setups.
629 		 */
630 		addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
631 							dev->parent->of_offset,
632 							dev->of_offset, "reg",
633 							index, NULL);
634 		if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
635 			if (device_get_uclass_id(dev->parent) ==
636 			    UCLASS_SIMPLE_BUS)
637 				addr = simple_bus_translate(dev->parent, addr);
638 		}
639 	}
640 
641 	/*
642 	 * Some platforms need a special address translation. Those
643 	 * platforms (e.g. mvebu in SPL) can configure a translation
644 	 * offset in the DM by calling dm_set_translation_offset() that
645 	 * will get added to all addresses returned by dev_get_addr().
646 	 */
647 	addr += dm_get_translation_offset();
648 
649 	return addr;
650 #else
651 	return FDT_ADDR_T_NONE;
652 #endif
653 }
654 
655 fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name)
656 {
657 #if CONFIG_IS_ENABLED(OF_CONTROL)
658 	int index;
659 
660 	index = fdt_find_string(gd->fdt_blob, dev->of_offset, "reg-names",
661 				name);
662 	if (index < 0)
663 		return index;
664 
665 	return dev_get_addr_index(dev, index);
666 #else
667 	return FDT_ADDR_T_NONE;
668 #endif
669 }
670 
671 fdt_addr_t dev_get_addr(struct udevice *dev)
672 {
673 	return dev_get_addr_index(dev, 0);
674 }
675 
676 void *dev_get_addr_ptr(struct udevice *dev)
677 {
678 	return (void *)(uintptr_t)dev_get_addr_index(dev, 0);
679 }
680 
681 bool device_has_children(struct udevice *dev)
682 {
683 	return !list_empty(&dev->child_head);
684 }
685 
686 bool device_has_active_children(struct udevice *dev)
687 {
688 	struct udevice *child;
689 
690 	for (device_find_first_child(dev, &child);
691 	     child;
692 	     device_find_next_child(&child)) {
693 		if (device_active(child))
694 			return true;
695 	}
696 
697 	return false;
698 }
699 
700 bool device_is_last_sibling(struct udevice *dev)
701 {
702 	struct udevice *parent = dev->parent;
703 
704 	if (!parent)
705 		return false;
706 	return list_is_last(&dev->sibling_node, &parent->child_head);
707 }
708 
709 void device_set_name_alloced(struct udevice *dev)
710 {
711 	dev->flags |= DM_NAME_ALLOCED;
712 }
713 
714 int device_set_name(struct udevice *dev, const char *name)
715 {
716 	name = strdup(name);
717 	if (!name)
718 		return -ENOMEM;
719 	dev->name = name;
720 	device_set_name_alloced(dev);
721 
722 	return 0;
723 }
724 
725 bool of_device_is_compatible(struct udevice *dev, const char *compat)
726 {
727 	const void *fdt = gd->fdt_blob;
728 
729 	return !fdt_node_check_compatible(fdt, dev->of_offset, compat);
730 }
731 
732 bool of_machine_is_compatible(const char *compat)
733 {
734 	const void *fdt = gd->fdt_blob;
735 
736 	return !fdt_node_check_compatible(fdt, 0, compat);
737 }
738