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