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