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