xref: /rk3399_rockchip-uboot/drivers/core/root.c (revision 5aeedebc33849efbbee2d422a4c7da8194e1b670)
16494d708SSimon Glass /*
26494d708SSimon Glass  * Copyright (c) 2013 Google, Inc
36494d708SSimon Glass  *
46494d708SSimon Glass  * (C) Copyright 2012
56494d708SSimon Glass  * Pavel Herrmann <morpheus.ibis@gmail.com>
66494d708SSimon Glass  *
76494d708SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
86494d708SSimon Glass  */
96494d708SSimon Glass 
106494d708SSimon Glass #include <common.h>
116494d708SSimon Glass #include <errno.h>
1294f7afdfSSimon Glass #include <fdtdec.h>
136494d708SSimon Glass #include <malloc.h>
146a6d8fbeSSimon Glass #include <libfdt.h>
156494d708SSimon Glass #include <dm/device.h>
166494d708SSimon Glass #include <dm/device-internal.h>
176494d708SSimon Glass #include <dm/lists.h>
186494d708SSimon Glass #include <dm/platdata.h>
19fd536d81SJeroen Hofstee #include <dm/root.h>
206494d708SSimon Glass #include <dm/uclass.h>
216494d708SSimon Glass #include <dm/util.h>
226494d708SSimon Glass #include <linux/list.h>
236494d708SSimon Glass 
246494d708SSimon Glass DECLARE_GLOBAL_DATA_PTR;
256494d708SSimon Glass 
2666eaea6cSStefan Roese struct root_priv {
2766eaea6cSStefan Roese 	fdt_addr_t translation_offset;	/* optional translation offset */
2866eaea6cSStefan Roese };
2966eaea6cSStefan Roese 
306494d708SSimon Glass static const struct driver_info root_info = {
316494d708SSimon Glass 	.name		= "root_driver",
326494d708SSimon Glass };
336494d708SSimon Glass 
3454c5d08aSHeiko Schocher struct udevice *dm_root(void)
356494d708SSimon Glass {
366494d708SSimon Glass 	if (!gd->dm_root) {
376494d708SSimon Glass 		dm_warn("Virtual root driver does not exist!\n");
386494d708SSimon Glass 		return NULL;
396494d708SSimon Glass 	}
406494d708SSimon Glass 
416494d708SSimon Glass 	return gd->dm_root;
426494d708SSimon Glass }
436494d708SSimon Glass 
4466eaea6cSStefan Roese fdt_addr_t dm_get_translation_offset(void)
4566eaea6cSStefan Roese {
4666eaea6cSStefan Roese 	struct udevice *root = dm_root();
4766eaea6cSStefan Roese 	struct root_priv *priv = dev_get_priv(root);
4866eaea6cSStefan Roese 
4966eaea6cSStefan Roese 	return priv->translation_offset;
5066eaea6cSStefan Roese }
5166eaea6cSStefan Roese 
5266eaea6cSStefan Roese void dm_set_translation_offset(fdt_addr_t offs)
5366eaea6cSStefan Roese {
5466eaea6cSStefan Roese 	struct udevice *root = dm_root();
5566eaea6cSStefan Roese 	struct root_priv *priv = dev_get_priv(root);
5666eaea6cSStefan Roese 
5766eaea6cSStefan Roese 	priv->translation_offset = offs;
5866eaea6cSStefan Roese }
5966eaea6cSStefan Roese 
60484fdf5bSMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC)
61484fdf5bSMichal Simek void fix_drivers(void)
62484fdf5bSMichal Simek {
63484fdf5bSMichal Simek 	struct driver *drv =
64484fdf5bSMichal Simek 		ll_entry_start(struct driver, driver);
65484fdf5bSMichal Simek 	const int n_ents = ll_entry_count(struct driver, driver);
66484fdf5bSMichal Simek 	struct driver *entry;
67484fdf5bSMichal Simek 
68484fdf5bSMichal Simek 	for (entry = drv; entry != drv + n_ents; entry++) {
69484fdf5bSMichal Simek 		if (entry->of_match)
70484fdf5bSMichal Simek 			entry->of_match = (const struct udevice_id *)
71484fdf5bSMichal Simek 				((u32)entry->of_match + gd->reloc_off);
72484fdf5bSMichal Simek 		if (entry->bind)
73484fdf5bSMichal Simek 			entry->bind += gd->reloc_off;
74484fdf5bSMichal Simek 		if (entry->probe)
75484fdf5bSMichal Simek 			entry->probe += gd->reloc_off;
76484fdf5bSMichal Simek 		if (entry->remove)
77484fdf5bSMichal Simek 			entry->remove += gd->reloc_off;
78484fdf5bSMichal Simek 		if (entry->unbind)
79484fdf5bSMichal Simek 			entry->unbind += gd->reloc_off;
80484fdf5bSMichal Simek 		if (entry->ofdata_to_platdata)
81484fdf5bSMichal Simek 			entry->ofdata_to_platdata += gd->reloc_off;
8231e1029aSMichal Simek 		if (entry->child_post_bind)
8331e1029aSMichal Simek 			entry->child_post_bind += gd->reloc_off;
84484fdf5bSMichal Simek 		if (entry->child_pre_probe)
85484fdf5bSMichal Simek 			entry->child_pre_probe += gd->reloc_off;
86484fdf5bSMichal Simek 		if (entry->child_post_remove)
87484fdf5bSMichal Simek 			entry->child_post_remove += gd->reloc_off;
88484fdf5bSMichal Simek 		/* OPS are fixed in every uclass post_probe function */
89484fdf5bSMichal Simek 		if (entry->ops)
90484fdf5bSMichal Simek 			entry->ops += gd->reloc_off;
91484fdf5bSMichal Simek 	}
92484fdf5bSMichal Simek }
93484fdf5bSMichal Simek 
94484fdf5bSMichal Simek void fix_uclass(void)
95484fdf5bSMichal Simek {
96484fdf5bSMichal Simek 	struct uclass_driver *uclass =
97484fdf5bSMichal Simek 		ll_entry_start(struct uclass_driver, uclass);
98484fdf5bSMichal Simek 	const int n_ents = ll_entry_count(struct uclass_driver, uclass);
99484fdf5bSMichal Simek 	struct uclass_driver *entry;
100484fdf5bSMichal Simek 
101484fdf5bSMichal Simek 	for (entry = uclass; entry != uclass + n_ents; entry++) {
102484fdf5bSMichal Simek 		if (entry->post_bind)
103484fdf5bSMichal Simek 			entry->post_bind += gd->reloc_off;
104484fdf5bSMichal Simek 		if (entry->pre_unbind)
105484fdf5bSMichal Simek 			entry->pre_unbind += gd->reloc_off;
10631e1029aSMichal Simek 		if (entry->pre_probe)
10731e1029aSMichal Simek 			entry->pre_probe += gd->reloc_off;
108484fdf5bSMichal Simek 		if (entry->post_probe)
109484fdf5bSMichal Simek 			entry->post_probe += gd->reloc_off;
110484fdf5bSMichal Simek 		if (entry->pre_remove)
111484fdf5bSMichal Simek 			entry->pre_remove += gd->reloc_off;
11231e1029aSMichal Simek 		if (entry->child_post_bind)
11331e1029aSMichal Simek 			entry->child_post_bind += gd->reloc_off;
11431e1029aSMichal Simek 		if (entry->child_pre_probe)
11531e1029aSMichal Simek 			entry->child_pre_probe += gd->reloc_off;
116484fdf5bSMichal Simek 		if (entry->init)
117484fdf5bSMichal Simek 			entry->init += gd->reloc_off;
118484fdf5bSMichal Simek 		if (entry->destroy)
119484fdf5bSMichal Simek 			entry->destroy += gd->reloc_off;
120484fdf5bSMichal Simek 		/* FIXME maybe also need to fix these ops */
121484fdf5bSMichal Simek 		if (entry->ops)
122484fdf5bSMichal Simek 			entry->ops += gd->reloc_off;
123484fdf5bSMichal Simek 	}
124484fdf5bSMichal Simek }
125*5aeedebcSAngelo Dureghello 
126*5aeedebcSAngelo Dureghello void fix_devices(void)
127*5aeedebcSAngelo Dureghello {
128*5aeedebcSAngelo Dureghello 	struct driver_info *dev =
129*5aeedebcSAngelo Dureghello 		ll_entry_start(struct driver_info, driver_info);
130*5aeedebcSAngelo Dureghello 	const int n_ents = ll_entry_count(struct driver_info, driver_info);
131*5aeedebcSAngelo Dureghello 	struct driver_info *entry;
132*5aeedebcSAngelo Dureghello 
133*5aeedebcSAngelo Dureghello 	for (entry = dev; entry != dev + n_ents; entry++) {
134*5aeedebcSAngelo Dureghello 		if (entry->platdata)
135*5aeedebcSAngelo Dureghello 			entry->platdata += gd->reloc_off;
136*5aeedebcSAngelo Dureghello 	}
137*5aeedebcSAngelo Dureghello }
138*5aeedebcSAngelo Dureghello 
139484fdf5bSMichal Simek #endif
140484fdf5bSMichal Simek 
1416494d708SSimon Glass int dm_init(void)
1426494d708SSimon Glass {
1436494d708SSimon Glass 	int ret;
1446494d708SSimon Glass 
1456494d708SSimon Glass 	if (gd->dm_root) {
1466494d708SSimon Glass 		dm_warn("Virtual root driver already exists!\n");
1476494d708SSimon Glass 		return -EINVAL;
1486494d708SSimon Glass 	}
14989876a55SSimon Glass 	INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
1506494d708SSimon Glass 
151484fdf5bSMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC)
152484fdf5bSMichal Simek 	fix_drivers();
153484fdf5bSMichal Simek 	fix_uclass();
154*5aeedebcSAngelo Dureghello 	fix_devices();
155484fdf5bSMichal Simek #endif
156484fdf5bSMichal Simek 
15700606d7eSSimon Glass 	ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
1586494d708SSimon Glass 	if (ret)
1596494d708SSimon Glass 		return ret;
1600f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
1612f3b95dbSSimon Glass 	DM_ROOT_NON_CONST->of_offset = 0;
1622f3b95dbSSimon Glass #endif
1637497812dSSimon Glass 	ret = device_probe(DM_ROOT_NON_CONST);
1647497812dSSimon Glass 	if (ret)
1657497812dSSimon Glass 		return ret;
1666494d708SSimon Glass 
1676494d708SSimon Glass 	return 0;
1686494d708SSimon Glass }
1696494d708SSimon Glass 
1709adbd7a1SSimon Glass int dm_uninit(void)
1719adbd7a1SSimon Glass {
1729adbd7a1SSimon Glass 	device_remove(dm_root());
1739adbd7a1SSimon Glass 	device_unbind(dm_root());
1749adbd7a1SSimon Glass 
1759adbd7a1SSimon Glass 	return 0;
1769adbd7a1SSimon Glass }
1779adbd7a1SSimon Glass 
17800606d7eSSimon Glass int dm_scan_platdata(bool pre_reloc_only)
1796494d708SSimon Glass {
1806494d708SSimon Glass 	int ret;
1816494d708SSimon Glass 
18200606d7eSSimon Glass 	ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
1836494d708SSimon Glass 	if (ret == -ENOENT) {
1846494d708SSimon Glass 		dm_warn("Some drivers were not found\n");
1856494d708SSimon Glass 		ret = 0;
1866494d708SSimon Glass 	}
1876494d708SSimon Glass 
188cbf86d71SMasahiro Yamada 	return ret;
1896494d708SSimon Glass }
1906494d708SSimon Glass 
1910f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
1921ca7e206SSimon Glass int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
1931ca7e206SSimon Glass 		     bool pre_reloc_only)
1946494d708SSimon Glass {
1956494d708SSimon Glass 	int ret = 0, err;
1966494d708SSimon Glass 
1971ca7e206SSimon Glass 	for (offset = fdt_first_subnode(blob, offset);
1981ca7e206SSimon Glass 	     offset > 0;
1991ca7e206SSimon Glass 	     offset = fdt_next_subnode(blob, offset)) {
20000606d7eSSimon Glass 		if (pre_reloc_only &&
20100606d7eSSimon Glass 		    !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
20200606d7eSSimon Glass 			continue;
20394f7afdfSSimon Glass 		if (!fdtdec_get_is_enabled(blob, offset)) {
20494f7afdfSSimon Glass 			dm_dbg("   - ignoring disabled device\n");
20594f7afdfSSimon Glass 			continue;
20694f7afdfSSimon Glass 		}
2071f359e36SSimon Glass 		err = lists_bind_fdt(parent, blob, offset, NULL);
208bc7b2f43SSimon Glass 		if (err && !ret) {
2096494d708SSimon Glass 			ret = err;
210bc7b2f43SSimon Glass 			debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
211bc7b2f43SSimon Glass 			      ret);
212bc7b2f43SSimon Glass 		}
2136494d708SSimon Glass 	}
2146494d708SSimon Glass 
2156494d708SSimon Glass 	if (ret)
2166494d708SSimon Glass 		dm_warn("Some drivers failed to bind\n");
2176494d708SSimon Glass 
2186494d708SSimon Glass 	return ret;
2196494d708SSimon Glass }
2201ca7e206SSimon Glass 
2211ca7e206SSimon Glass int dm_scan_fdt(const void *blob, bool pre_reloc_only)
2221ca7e206SSimon Glass {
2231ca7e206SSimon Glass 	return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
2241ca7e206SSimon Glass }
2256494d708SSimon Glass #endif
2266494d708SSimon Glass 
227bb58503dSSimon Glass __weak int dm_scan_other(bool pre_reloc_only)
228bb58503dSSimon Glass {
229bb58503dSSimon Glass 	return 0;
230bb58503dSSimon Glass }
231bb58503dSSimon Glass 
232ab7cd627SSimon Glass int dm_init_and_scan(bool pre_reloc_only)
233ab7cd627SSimon Glass {
234ab7cd627SSimon Glass 	int ret;
235ab7cd627SSimon Glass 
236ab7cd627SSimon Glass 	ret = dm_init();
237ab7cd627SSimon Glass 	if (ret) {
238ab7cd627SSimon Glass 		debug("dm_init() failed: %d\n", ret);
239ab7cd627SSimon Glass 		return ret;
240ab7cd627SSimon Glass 	}
241ab7cd627SSimon Glass 	ret = dm_scan_platdata(pre_reloc_only);
242ab7cd627SSimon Glass 	if (ret) {
243ab7cd627SSimon Glass 		debug("dm_scan_platdata() failed: %d\n", ret);
244ab7cd627SSimon Glass 		return ret;
245ab7cd627SSimon Glass 	}
246b2b0d3e7SSimon Glass 
2470f925822SMasahiro Yamada 	if (CONFIG_IS_ENABLED(OF_CONTROL)) {
248ab7cd627SSimon Glass 		ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
249ab7cd627SSimon Glass 		if (ret) {
250ab7cd627SSimon Glass 			debug("dm_scan_fdt() failed: %d\n", ret);
251ab7cd627SSimon Glass 			return ret;
252ab7cd627SSimon Glass 		}
253b2b0d3e7SSimon Glass 	}
254b2b0d3e7SSimon Glass 
255bb58503dSSimon Glass 	ret = dm_scan_other(pre_reloc_only);
256bb58503dSSimon Glass 	if (ret)
257bb58503dSSimon Glass 		return ret;
258ab7cd627SSimon Glass 
259ab7cd627SSimon Glass 	return 0;
260ab7cd627SSimon Glass }
261ab7cd627SSimon Glass 
2626494d708SSimon Glass /* This is the root driver - all drivers are children of this */
2636494d708SSimon Glass U_BOOT_DRIVER(root_driver) = {
2646494d708SSimon Glass 	.name	= "root_driver",
2656494d708SSimon Glass 	.id	= UCLASS_ROOT,
26666eaea6cSStefan Roese 	.priv_auto_alloc_size = sizeof(struct root_priv),
2676494d708SSimon Glass };
2686494d708SSimon Glass 
2696494d708SSimon Glass /* This is the root uclass */
2706494d708SSimon Glass UCLASS_DRIVER(root) = {
2716494d708SSimon Glass 	.name	= "root",
2726494d708SSimon Glass 	.id	= UCLASS_ROOT,
2736494d708SSimon Glass };
274