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