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