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