xref: /rk3399_rockchip-uboot/drivers/core/device.c (revision 1f25ada2a8eccef6ea3d9844a40d05d7ce7027d4)
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 <asm/io.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 
30 DECLARE_GLOBAL_DATA_PTR;
31 
32 static int device_bind_common(struct udevice *parent, const struct driver *drv,
33 			      const char *name, void *platdata,
34 			      ulong driver_data, ofnode node,
35 			      uint of_platdata_size, struct udevice **devp)
36 {
37 	struct udevice *dev;
38 	struct uclass *uc;
39 	int size, ret = 0;
40 
41 	if (devp)
42 		*devp = NULL;
43 	if (!name)
44 		return -EINVAL;
45 
46 	ret = uclass_get(drv->id, &uc);
47 	if (ret) {
48 		debug("Missing uclass for driver %s\n", drv->name);
49 		return ret;
50 	}
51 
52 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
53 		if (!strcmp(name, dev->name)){
54 			debug("%s do not bind dev already in list %s\n",
55 			        __func__, name);
56 			dev->node = node;
57 			return 0;
58 		}
59 	}
60 
61 	dev = calloc(1, sizeof(struct udevice));
62 	if (!dev)
63 		return -ENOMEM;
64 
65 	INIT_LIST_HEAD(&dev->sibling_node);
66 	INIT_LIST_HEAD(&dev->child_head);
67 	INIT_LIST_HEAD(&dev->uclass_node);
68 #ifdef CONFIG_DEVRES
69 	INIT_LIST_HEAD(&dev->devres_head);
70 #endif
71 	dev->platdata = platdata;
72 	dev->driver_data = driver_data;
73 	dev->name = name;
74 	dev->node = node;
75 	dev->parent = parent;
76 	dev->driver = drv;
77 	dev->uclass = uc;
78 
79 	dev->seq = -1;
80 	dev->req_seq = -1;
81 	if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
82 		/*
83 		 * Some devices, such as a SPI bus, I2C bus and serial ports
84 		 * are numbered using aliases.
85 		 *
86 		 * This is just a 'requested' sequence, and will be
87 		 * resolved (and ->seq updated) when the device is probed.
88 		 */
89 		if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
90 			if (uc->uc_drv->name && ofnode_valid(node)) {
91 				dev_read_alias_seq(dev, &dev->req_seq);
92 			}
93 		}
94 	}
95 
96 	if (drv->platdata_auto_alloc_size) {
97 		bool alloc = !platdata;
98 
99 		if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
100 			if (of_platdata_size) {
101 				dev->flags |= DM_FLAG_OF_PLATDATA;
102 				if (of_platdata_size <
103 						drv->platdata_auto_alloc_size)
104 					alloc = true;
105 			}
106 		}
107 		if (alloc) {
108 			dev->flags |= DM_FLAG_ALLOC_PDATA;
109 			dev->platdata = calloc(1,
110 					       drv->platdata_auto_alloc_size);
111 			if (!dev->platdata) {
112 				ret = -ENOMEM;
113 				goto fail_alloc1;
114 			}
115 			if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
116 				memcpy(dev->platdata, platdata,
117 				       of_platdata_size);
118 			}
119 		}
120 	}
121 
122 	size = uc->uc_drv->per_device_platdata_auto_alloc_size;
123 	if (size) {
124 		dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
125 		dev->uclass_platdata = calloc(1, size);
126 		if (!dev->uclass_platdata) {
127 			ret = -ENOMEM;
128 			goto fail_alloc2;
129 		}
130 	}
131 
132 	if (parent) {
133 		size = parent->driver->per_child_platdata_auto_alloc_size;
134 		if (!size) {
135 			size = parent->uclass->uc_drv->
136 					per_child_platdata_auto_alloc_size;
137 		}
138 		if (size) {
139 			dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
140 			dev->parent_platdata = calloc(1, size);
141 			if (!dev->parent_platdata) {
142 				ret = -ENOMEM;
143 				goto fail_alloc3;
144 			}
145 		}
146 	}
147 
148 	/* put dev into parent's successor list */
149 	if (parent)
150 		list_add_tail(&dev->sibling_node, &parent->child_head);
151 
152 	ret = uclass_bind_device(dev);
153 	if (ret)
154 		goto fail_uclass_bind;
155 
156 	/* if we fail to bind we remove device from successors and free it */
157 	if (drv->bind) {
158 		ret = drv->bind(dev);
159 		if (ret)
160 			goto fail_bind;
161 	}
162 	if (parent && parent->driver->child_post_bind) {
163 		ret = parent->driver->child_post_bind(dev);
164 		if (ret)
165 			goto fail_child_post_bind;
166 	}
167 	if (uc->uc_drv->post_bind) {
168 		ret = uc->uc_drv->post_bind(dev);
169 		if (ret)
170 			goto fail_uclass_post_bind;
171 	}
172 
173 	if (parent)
174 		pr_debug("Bound device %s to %s\n", dev->name, parent->name);
175 	if (devp)
176 		*devp = dev;
177 
178 	dev->flags |= DM_FLAG_BOUND;
179 
180 	return 0;
181 
182 fail_uclass_post_bind:
183 	/* There is no child unbind() method, so no clean-up required */
184 fail_child_post_bind:
185 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
186 		if (drv->unbind && drv->unbind(dev)) {
187 			dm_warn("unbind() method failed on dev '%s' on error path\n",
188 				dev->name);
189 		}
190 	}
191 
192 fail_bind:
193 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
194 		if (uclass_unbind_device(dev)) {
195 			dm_warn("Failed to unbind dev '%s' on error path\n",
196 				dev->name);
197 		}
198 	}
199 fail_uclass_bind:
200 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
201 		list_del(&dev->sibling_node);
202 		if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
203 			free(dev->parent_platdata);
204 			dev->parent_platdata = NULL;
205 		}
206 	}
207 fail_alloc3:
208 	if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
209 		free(dev->uclass_platdata);
210 		dev->uclass_platdata = NULL;
211 	}
212 fail_alloc2:
213 	if (dev->flags & DM_FLAG_ALLOC_PDATA) {
214 		free(dev->platdata);
215 		dev->platdata = NULL;
216 	}
217 fail_alloc1:
218 	devres_release_all(dev);
219 
220 	free(dev);
221 
222 	return ret;
223 }
224 
225 int device_bind_with_driver_data(struct udevice *parent,
226 				 const struct driver *drv, const char *name,
227 				 ulong driver_data, ofnode node,
228 				 struct udevice **devp)
229 {
230 	return device_bind_common(parent, drv, name, NULL, driver_data, node,
231 				  0, devp);
232 }
233 
234 int device_bind(struct udevice *parent, const struct driver *drv,
235 		const char *name, void *platdata, int of_offset,
236 		struct udevice **devp)
237 {
238 	return device_bind_common(parent, drv, name, platdata, 0,
239 				  offset_to_ofnode(of_offset), 0, 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 	const struct driver *drv;
309 	int size = 0;
310 	int ret;
311 	int seq;
312 
313 	if (!dev)
314 		return -EINVAL;
315 
316 	if (dev->flags & DM_FLAG_ACTIVATED)
317 		return 0;
318 
319 	drv = dev->driver;
320 	assert(drv);
321 
322 	/* Allocate private data if requested and not reentered */
323 	if (drv->priv_auto_alloc_size && !dev->priv) {
324 		dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
325 		if (!dev->priv) {
326 			ret = -ENOMEM;
327 			goto fail;
328 		}
329 	}
330 	/* Allocate private data if requested and not reentered */
331 	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
332 	if (size && !dev->uclass_priv) {
333 		dev->uclass_priv = calloc(1, size);
334 		if (!dev->uclass_priv) {
335 			ret = -ENOMEM;
336 			goto fail;
337 		}
338 	}
339 
340 	/* Ensure all parents are probed */
341 	if (dev->parent) {
342 		size = dev->parent->driver->per_child_auto_alloc_size;
343 		if (!size) {
344 			size = dev->parent->uclass->uc_drv->
345 					per_child_auto_alloc_size;
346 		}
347 		if (size && !dev->parent_priv) {
348 			dev->parent_priv = alloc_priv(size, drv->flags);
349 			if (!dev->parent_priv) {
350 				ret = -ENOMEM;
351 				goto fail;
352 			}
353 		}
354 
355 		ret = device_probe(dev->parent);
356 		if (ret)
357 			goto fail;
358 
359 		/*
360 		 * The device might have already been probed during
361 		 * the call to device_probe() on its parent device
362 		 * (e.g. PCI bridge devices). Test the flags again
363 		 * so that we don't mess up the device.
364 		 */
365 		if (dev->flags & DM_FLAG_ACTIVATED)
366 			return 0;
367 	}
368 
369 	seq = uclass_resolve_seq(dev);
370 	if (seq < 0) {
371 		ret = seq;
372 		goto fail;
373 	}
374 	dev->seq = seq;
375 
376 	dev->flags |= DM_FLAG_ACTIVATED;
377 
378 	/*
379 	 * Process pinctrl for everything except the root device, and
380 	 * continue regardless of the result of pinctrl. Don't process pinctrl
381 	 * settings for pinctrl devices since the device may not yet be
382 	 * probed.
383 	 */
384 	if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
385 		pinctrl_select_state(dev, "default");
386 
387 	ret = uclass_pre_probe_device(dev);
388 	if (ret)
389 		goto fail;
390 
391 	if (dev->parent && dev->parent->driver->child_pre_probe) {
392 		ret = dev->parent->driver->child_pre_probe(dev);
393 		if (ret)
394 			goto fail;
395 	}
396 
397 	if (drv->ofdata_to_platdata && dev_has_of_node(dev)) {
398 		ret = drv->ofdata_to_platdata(dev);
399 		if (ret)
400 			goto fail;
401 	}
402 
403 	if (drv->probe) {
404 		ret = drv->probe(dev);
405 		if (ret) {
406 			dev->flags &= ~DM_FLAG_ACTIVATED;
407 			goto fail;
408 		}
409 	}
410 
411 	ret = uclass_post_probe_device(dev);
412 	if (ret)
413 		goto fail_uclass;
414 
415 	if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
416 		pinctrl_select_state(dev, "default");
417 
418 	return 0;
419 fail_uclass:
420 	if (device_remove(dev, DM_REMOVE_NORMAL)) {
421 		dm_warn("%s: Device '%s' failed to remove on error path\n",
422 			__func__, dev->name);
423 	}
424 fail:
425 	dev->flags &= ~DM_FLAG_ACTIVATED;
426 
427 	dev->seq = -1;
428 	device_free(dev);
429 
430 	return ret;
431 }
432 
433 void *dev_get_platdata(struct udevice *dev)
434 {
435 	if (!dev) {
436 		dm_warn("%s: null device\n", __func__);
437 		return NULL;
438 	}
439 
440 	return dev->platdata;
441 }
442 
443 void *dev_get_parent_platdata(struct udevice *dev)
444 {
445 	if (!dev) {
446 		dm_warn("%s: null device\n", __func__);
447 		return NULL;
448 	}
449 
450 	return dev->parent_platdata;
451 }
452 
453 void *dev_get_uclass_platdata(struct udevice *dev)
454 {
455 	if (!dev) {
456 		dm_warn("%s: null device\n", __func__);
457 		return NULL;
458 	}
459 
460 	return dev->uclass_platdata;
461 }
462 
463 void *dev_get_priv(struct udevice *dev)
464 {
465 	if (!dev) {
466 		dm_warn("%s: null device\n", __func__);
467 		return NULL;
468 	}
469 
470 	return dev->priv;
471 }
472 
473 void *dev_get_uclass_priv(struct udevice *dev)
474 {
475 	if (!dev) {
476 		dm_warn("%s: null device\n", __func__);
477 		return NULL;
478 	}
479 
480 	return dev->uclass_priv;
481 }
482 
483 void *dev_get_parent_priv(struct udevice *dev)
484 {
485 	if (!dev) {
486 		dm_warn("%s: null device\n", __func__);
487 		return NULL;
488 	}
489 
490 	return dev->parent_priv;
491 }
492 
493 static int device_get_device_tail(struct udevice *dev, int ret,
494 				  struct udevice **devp)
495 {
496 	if (ret)
497 		return ret;
498 
499 	ret = device_probe(dev);
500 	if (ret)
501 		return ret;
502 
503 	*devp = dev;
504 
505 	return 0;
506 }
507 
508 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
509 {
510 	struct udevice *dev;
511 
512 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
513 		if (!index--)
514 			return device_get_device_tail(dev, 0, devp);
515 	}
516 
517 	return -ENODEV;
518 }
519 
520 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
521 			     bool find_req_seq, struct udevice **devp)
522 {
523 	struct udevice *dev;
524 
525 	*devp = NULL;
526 	if (seq_or_req_seq == -1)
527 		return -ENODEV;
528 
529 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
530 		if ((find_req_seq ? dev->req_seq : dev->seq) ==
531 				seq_or_req_seq) {
532 			*devp = dev;
533 			return 0;
534 		}
535 	}
536 
537 	return -ENODEV;
538 }
539 
540 int device_get_child_by_seq(struct udevice *parent, int seq,
541 			    struct udevice **devp)
542 {
543 	struct udevice *dev;
544 	int ret;
545 
546 	*devp = NULL;
547 	ret = device_find_child_by_seq(parent, seq, false, &dev);
548 	if (ret == -ENODEV) {
549 		/*
550 		 * We didn't find it in probed devices. See if there is one
551 		 * that will request this seq if probed.
552 		 */
553 		ret = device_find_child_by_seq(parent, seq, true, &dev);
554 	}
555 	return device_get_device_tail(dev, ret, devp);
556 }
557 
558 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
559 				   struct udevice **devp)
560 {
561 	struct udevice *dev;
562 
563 	*devp = NULL;
564 
565 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
566 		if (dev_of_offset(dev) == of_offset) {
567 			*devp = dev;
568 			return 0;
569 		}
570 	}
571 
572 	return -ENODEV;
573 }
574 
575 int device_get_child_by_of_offset(struct udevice *parent, int node,
576 				  struct udevice **devp)
577 {
578 	struct udevice *dev;
579 	int ret;
580 
581 	*devp = NULL;
582 	ret = device_find_child_by_of_offset(parent, node, &dev);
583 	return device_get_device_tail(dev, ret, devp);
584 }
585 
586 static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
587 							int of_offset)
588 {
589 	struct udevice *dev, *found;
590 
591 	if (dev_of_offset(parent) == of_offset)
592 		return parent;
593 
594 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
595 		found = _device_find_global_by_of_offset(dev, of_offset);
596 		if (found)
597 			return found;
598 	}
599 
600 	return NULL;
601 }
602 
603 int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
604 {
605 	struct udevice *dev;
606 
607 	dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
608 	return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
609 }
610 
611 int device_find_first_child(struct udevice *parent, struct udevice **devp)
612 {
613 	if (list_empty(&parent->child_head)) {
614 		*devp = NULL;
615 	} else {
616 		*devp = list_first_entry(&parent->child_head, struct udevice,
617 					 sibling_node);
618 	}
619 
620 	return 0;
621 }
622 
623 int device_find_next_child(struct udevice **devp)
624 {
625 	struct udevice *dev = *devp;
626 	struct udevice *parent = dev->parent;
627 
628 	if (list_is_last(&dev->sibling_node, &parent->child_head)) {
629 		*devp = NULL;
630 	} else {
631 		*devp = list_entry(dev->sibling_node.next, struct udevice,
632 				   sibling_node);
633 	}
634 
635 	return 0;
636 }
637 
638 struct udevice *dev_get_parent(struct udevice *child)
639 {
640 	return child->parent;
641 }
642 
643 ulong dev_get_driver_data(struct udevice *dev)
644 {
645 	return dev->driver_data;
646 }
647 
648 const void *dev_get_driver_ops(struct udevice *dev)
649 {
650 	if (!dev || !dev->driver->ops)
651 		return NULL;
652 
653 	return dev->driver->ops;
654 }
655 
656 enum uclass_id device_get_uclass_id(struct udevice *dev)
657 {
658 	return dev->uclass->uc_drv->id;
659 }
660 
661 const char *dev_get_uclass_name(struct udevice *dev)
662 {
663 	if (!dev)
664 		return NULL;
665 
666 	return dev->uclass->uc_drv->name;
667 }
668 
669 bool device_has_children(struct udevice *dev)
670 {
671 	return !list_empty(&dev->child_head);
672 }
673 
674 bool device_has_active_children(struct udevice *dev)
675 {
676 	struct udevice *child;
677 
678 	for (device_find_first_child(dev, &child);
679 	     child;
680 	     device_find_next_child(&child)) {
681 		if (device_active(child))
682 			return true;
683 	}
684 
685 	return false;
686 }
687 
688 bool device_is_last_sibling(struct udevice *dev)
689 {
690 	struct udevice *parent = dev->parent;
691 
692 	if (!parent)
693 		return false;
694 	return list_is_last(&dev->sibling_node, &parent->child_head);
695 }
696 
697 void device_set_name_alloced(struct udevice *dev)
698 {
699 	dev->flags |= DM_FLAG_NAME_ALLOCED;
700 }
701 
702 int device_set_name(struct udevice *dev, const char *name)
703 {
704 	name = strdup(name);
705 	if (!name)
706 		return -ENOMEM;
707 	dev->name = name;
708 	device_set_name_alloced(dev);
709 
710 	return 0;
711 }
712 
713 bool device_is_compatible(struct udevice *dev, const char *compat)
714 {
715 	const void *fdt = gd->fdt_blob;
716 	ofnode node = dev_ofnode(dev);
717 
718 	if (ofnode_is_np(node))
719 		return of_device_is_compatible(ofnode_to_np(node), compat, NULL, NULL);
720 	else
721 		return !fdt_node_check_compatible(fdt, ofnode_to_offset(node), compat);
722 }
723 
724 bool of_machine_is_compatible(const char *compat)
725 {
726 	const void *fdt = gd->fdt_blob;
727 
728 	return !fdt_node_check_compatible(fdt, 0, compat);
729 }
730