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