xref: /rk3399_rockchip-uboot/drivers/core/root.c (revision 386ae599c0eec847f4b4f44007d397a926dd4d6b)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * (C) Copyright 2012
5  * Pavel Herrmann <morpheus.ibis@gmail.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <errno.h>
12 #include <fdtdec.h>
13 #include <malloc.h>
14 #include <linux/libfdt.h>
15 #include <dm/device.h>
16 #include <dm/device-internal.h>
17 #include <dm/lists.h>
18 #include <dm/of.h>
19 #include <dm/of_access.h>
20 #include <dm/platdata.h>
21 #include <dm/read.h>
22 #include <dm/root.h>
23 #include <dm/uclass.h>
24 #include <dm/util.h>
25 #include <linux/list.h>
26 
27 DECLARE_GLOBAL_DATA_PTR;
28 
29 struct root_priv {
30 	fdt_addr_t translation_offset;	/* optional translation offset */
31 };
32 
33 static const struct driver_info root_info = {
34 	.name		= "root_driver",
35 };
36 
37 struct udevice *dm_root(void)
38 {
39 	if (!gd->dm_root) {
40 		dm_warn("Virtual root driver does not exist!\n");
41 		return NULL;
42 	}
43 
44 	return gd->dm_root;
45 }
46 
47 void dm_fixup_for_gd_move(struct global_data *new_gd)
48 {
49 	/* The sentinel node has moved, so update things that point to it */
50 	if (gd->dm_root) {
51 		new_gd->uclass_root.next->prev = &new_gd->uclass_root;
52 		new_gd->uclass_root.prev->next = &new_gd->uclass_root;
53 	}
54 }
55 
56 fdt_addr_t dm_get_translation_offset(void)
57 {
58 	struct udevice *root = dm_root();
59 	struct root_priv *priv = dev_get_priv(root);
60 
61 	return priv->translation_offset;
62 }
63 
64 void dm_set_translation_offset(fdt_addr_t offs)
65 {
66 	struct udevice *root = dm_root();
67 	struct root_priv *priv = dev_get_priv(root);
68 
69 	priv->translation_offset = offs;
70 }
71 
72 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
73 void fix_drivers(void)
74 {
75 	struct driver *drv =
76 		ll_entry_start(struct driver, driver);
77 	const int n_ents = ll_entry_count(struct driver, driver);
78 	struct driver *entry;
79 
80 	for (entry = drv; entry != drv + n_ents; entry++) {
81 		if (entry->of_match)
82 			entry->of_match = (const struct udevice_id *)
83 				((u32)entry->of_match + gd->reloc_off);
84 		if (entry->bind)
85 			entry->bind += gd->reloc_off;
86 		if (entry->probe)
87 			entry->probe += gd->reloc_off;
88 		if (entry->remove)
89 			entry->remove += gd->reloc_off;
90 		if (entry->unbind)
91 			entry->unbind += gd->reloc_off;
92 		if (entry->ofdata_to_platdata)
93 			entry->ofdata_to_platdata += gd->reloc_off;
94 		if (entry->child_post_bind)
95 			entry->child_post_bind += gd->reloc_off;
96 		if (entry->child_pre_probe)
97 			entry->child_pre_probe += gd->reloc_off;
98 		if (entry->child_post_remove)
99 			entry->child_post_remove += gd->reloc_off;
100 		/* OPS are fixed in every uclass post_probe function */
101 		if (entry->ops)
102 			entry->ops += gd->reloc_off;
103 	}
104 }
105 
106 void fix_uclass(void)
107 {
108 	struct uclass_driver *uclass =
109 		ll_entry_start(struct uclass_driver, uclass);
110 	const int n_ents = ll_entry_count(struct uclass_driver, uclass);
111 	struct uclass_driver *entry;
112 
113 	for (entry = uclass; entry != uclass + n_ents; entry++) {
114 		if (entry->post_bind)
115 			entry->post_bind += gd->reloc_off;
116 		if (entry->pre_unbind)
117 			entry->pre_unbind += gd->reloc_off;
118 		if (entry->pre_probe)
119 			entry->pre_probe += gd->reloc_off;
120 		if (entry->post_probe)
121 			entry->post_probe += gd->reloc_off;
122 		if (entry->pre_remove)
123 			entry->pre_remove += gd->reloc_off;
124 		if (entry->child_post_bind)
125 			entry->child_post_bind += gd->reloc_off;
126 		if (entry->child_pre_probe)
127 			entry->child_pre_probe += gd->reloc_off;
128 		if (entry->init)
129 			entry->init += gd->reloc_off;
130 		if (entry->destroy)
131 			entry->destroy += gd->reloc_off;
132 		/* FIXME maybe also need to fix these ops */
133 		if (entry->ops)
134 			entry->ops += gd->reloc_off;
135 	}
136 }
137 
138 void fix_devices(void)
139 {
140 	struct driver_info *dev =
141 		ll_entry_start(struct driver_info, driver_info);
142 	const int n_ents = ll_entry_count(struct driver_info, driver_info);
143 	struct driver_info *entry;
144 
145 	for (entry = dev; entry != dev + n_ents; entry++) {
146 		if (entry->platdata)
147 			entry->platdata += gd->reloc_off;
148 	}
149 }
150 
151 #endif
152 
153 int dm_init(bool of_live)
154 {
155 	int ret;
156 
157 	if (gd->dm_root) {
158 		dm_warn("Virtual root driver already exists!\n");
159 		return -EINVAL;
160 	}
161 	INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
162 
163 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
164 	fix_drivers();
165 	fix_uclass();
166 	fix_devices();
167 #endif
168 
169 	ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
170 	if (ret)
171 		return ret;
172 #if CONFIG_IS_ENABLED(OF_CONTROL)
173 # if CONFIG_IS_ENABLED(OF_LIVE)
174 	if (of_live)
175 		DM_ROOT_NON_CONST->node = np_to_ofnode(gd->of_root);
176 	else
177 #endif
178 		DM_ROOT_NON_CONST->node = offset_to_ofnode(0);
179 #endif
180 	ret = device_probe(DM_ROOT_NON_CONST);
181 	if (ret)
182 		return ret;
183 
184 	return 0;
185 }
186 
187 int dm_uninit(void)
188 {
189 	device_remove(dm_root(), DM_REMOVE_NORMAL);
190 	device_unbind(dm_root());
191 
192 	return 0;
193 }
194 
195 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
196 int dm_remove_devices_flags(uint flags)
197 {
198 	device_remove(dm_root(), flags);
199 
200 	return 0;
201 }
202 #endif
203 
204 int dm_scan_platdata(bool pre_reloc_only)
205 {
206 	int ret;
207 
208 	ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
209 	if (ret == -ENOENT) {
210 		dm_warn("Some drivers were not found\n");
211 		ret = 0;
212 	}
213 
214 	return ret;
215 }
216 
217 #if CONFIG_IS_ENABLED(OF_LIVE)
218 static int dm_scan_fdt_live(struct udevice *parent,
219 			    const struct device_node *node_parent,
220 			    bool pre_reloc_only)
221 {
222 	struct device_node *np;
223 	int ret = 0, err;
224 
225 	for (np = node_parent->child; np; np = np->sibling) {
226 		if (pre_reloc_only &&
227 #ifdef CONFIG_USING_KERNEL_DTB
228 		    (!of_find_property(np, "u-boot,dm-pre-reloc", NULL) &&
229 		     !of_find_property(np, "u-boot,dm-spl", NULL)))
230 #else
231 		     !of_find_property(np, "u-boot,dm-pre-reloc", NULL))
232 #endif
233 			continue;
234 		if (!of_device_is_available(np)) {
235 			pr_debug("   - ignoring disabled device\n");
236 			continue;
237 		}
238 		err = lists_bind_fdt(parent, np_to_ofnode(np), NULL);
239 		if (err && !ret) {
240 			ret = err;
241 			debug("%s: ret=%d\n", np->name, ret);
242 		}
243 
244 		/* There is no compatible in "/firmware", bind it by default. */
245 		if (!pre_reloc_only && !strcmp(np->name, "firmware"))
246 			ret = device_bind_driver_to_node(gd->dm_root,
247 				"firmware", np->name, np_to_ofnode(np), NULL);
248 	}
249 
250 	if (ret)
251 		dm_warn("Some drivers failed to bind\n");
252 
253 	return ret;
254 }
255 #endif /* CONFIG_IS_ENABLED(OF_LIVE) */
256 
257 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
258 /**
259  * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
260  *
261  * This scans the subnodes of a device tree node and and creates a driver
262  * for each one.
263  *
264  * @parent: Parent device for the devices that will be created
265  * @blob: Pointer to device tree blob
266  * @offset: Offset of node to scan
267  * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
268  * flag. If false bind all drivers.
269  * @return 0 if OK, -ve on error
270  */
271 static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
272 			    int offset, bool pre_reloc_only)
273 {
274 	int ret = 0, err;
275 
276 	for (offset = fdt_first_subnode(blob, offset);
277 	     offset > 0;
278 	     offset = fdt_next_subnode(blob, offset)) {
279 		if (pre_reloc_only &&
280 		    !dm_fdt_pre_reloc(blob, offset))
281 			continue;
282 		if (!fdtdec_get_is_enabled(blob, offset)) {
283 			pr_debug("   - ignoring disabled device\n");
284 			continue;
285 		}
286 		err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL);
287 		if (err && !ret) {
288 			ret = err;
289 			debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
290 			      ret);
291 		}
292 
293 #if CONFIG_IS_ENABLED(SCMI_FIRMWARE)
294 		const char *name;
295 
296 		/* There is no compatible in "/firmware", bind it by default. */
297 		name = fdt_get_name(blob, offset, NULL);
298 		if (name && !strcmp(name, "firmware"))
299 			ret = device_bind_driver_to_node(parent, "firmware",
300 					name, offset_to_ofnode(offset), NULL);
301 #endif
302 	}
303 
304 	if (ret)
305 		dm_warn("Some drivers failed to bind\n");
306 
307 	return ret;
308 }
309 
310 int dm_scan_fdt_dev(struct udevice *dev)
311 {
312 	if (!dev_of_valid(dev))
313 		return 0;
314 
315 #if CONFIG_IS_ENABLED(OF_LIVE)
316 	if (of_live_active())
317 		return dm_scan_fdt_live(dev, dev_np(dev),
318 				gd->flags & GD_FLG_RELOC ? false : true);
319 	else
320 #endif
321 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
322 				gd->flags & GD_FLG_RELOC ? false : true);
323 }
324 
325 int dm_scan_fdt(const void *blob, bool pre_reloc_only)
326 {
327 #if CONFIG_IS_ENABLED(OF_LIVE)
328 	if (of_live_active())
329 		return dm_scan_fdt_live(gd->dm_root, gd->of_root,
330 					pre_reloc_only);
331 	else
332 #endif
333 	return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
334 }
335 #else
336 static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
337 			    int offset, bool pre_reloc_only)
338 {
339 	return 0;
340 }
341 #endif
342 
343 int dm_extended_scan_fdt(const void *blob, bool pre_reloc_only)
344 {
345 	int node, ret;
346 
347 	ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
348 	if (ret) {
349 		debug("dm_scan_fdt() failed: %d\n", ret);
350 		return ret;
351 	}
352 
353 	/* bind fixed-clock */
354 	node = ofnode_to_offset(ofnode_path("/clocks"));
355 	/* if no DT "clocks" node, no need to go further */
356 	if (node < 0)
357 		return ret;
358 
359 	ret = dm_scan_fdt_node(gd->dm_root, gd->fdt_blob, node,
360 			       pre_reloc_only);
361 	if (ret)
362 		debug("dm_scan_fdt_node() failed: %d\n", ret);
363 
364 	return ret;
365 }
366 
367 __weak int dm_scan_other(bool pre_reloc_only)
368 {
369 	return 0;
370 }
371 
372 int dm_init_and_scan(bool pre_reloc_only)
373 {
374 	int ret;
375 
376 	ret = dm_init(IS_ENABLED(CONFIG_OF_LIVE));
377 	if (ret) {
378 		debug("dm_init() failed: %d\n", ret);
379 		return ret;
380 	}
381 	ret = dm_scan_platdata(pre_reloc_only);
382 	if (ret) {
383 		debug("dm_scan_platdata() failed: %d\n", ret);
384 		return ret;
385 	}
386 
387 	if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
388 		ret = dm_extended_scan_fdt(gd->fdt_blob, pre_reloc_only);
389 		if (ret) {
390 			debug("dm_extended_scan_dt() failed: %d\n", ret);
391 			return ret;
392 		}
393 	}
394 
395 	ret = dm_scan_other(pre_reloc_only);
396 	if (ret)
397 		return ret;
398 
399 	return 0;
400 }
401 
402 /* This is the root driver - all drivers are children of this */
403 U_BOOT_DRIVER(root_driver) = {
404 	.name	= "root_driver",
405 	.id	= UCLASS_ROOT,
406 	.priv_auto_alloc_size = sizeof(struct root_priv),
407 };
408 
409 /* This is the root uclass */
410 UCLASS_DRIVER(root) = {
411 	.name	= "root",
412 	.id	= UCLASS_ROOT,
413 };
414