dm: define LOG_CATEGORY for all uclass
[platform/kernel/u-boot.git] / drivers / core / root.c
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 #define LOG_CATEGORY UCLASS_ROOT
10
11 #include <common.h>
12 #include <errno.h>
13 #include <fdtdec.h>
14 #include <log.h>
15 #include <malloc.h>
16 #include <asm-generic/sections.h>
17 #include <asm/global_data.h>
18 #include <linux/libfdt.h>
19 #include <dm/acpi.h>
20 #include <dm/device.h>
21 #include <dm/device-internal.h>
22 #include <dm/lists.h>
23 #include <dm/of.h>
24 #include <dm/of_access.h>
25 #include <dm/platdata.h>
26 #include <dm/read.h>
27 #include <dm/root.h>
28 #include <dm/uclass.h>
29 #include <dm/util.h>
30 #include <linux/list.h>
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 static struct driver_info root_info = {
35         .name           = "root_driver",
36 };
37
38 struct udevice *dm_root(void)
39 {
40         if (!gd->dm_root) {
41                 dm_warn("Virtual root driver does not exist!\n");
42                 return NULL;
43         }
44
45         return gd->dm_root;
46 }
47
48 void dm_fixup_for_gd_move(struct global_data *new_gd)
49 {
50         /* The sentinel node has moved, so update things that point to it */
51         if (gd->dm_root) {
52                 new_gd->uclass_root->next->prev = new_gd->uclass_root;
53                 new_gd->uclass_root->prev->next = new_gd->uclass_root;
54         }
55 }
56
57 void fix_drivers(void)
58 {
59         struct driver *drv =
60                 ll_entry_start(struct driver, driver);
61         const int n_ents = ll_entry_count(struct driver, driver);
62         struct driver *entry;
63
64         for (entry = drv; entry != drv + n_ents; entry++) {
65                 if (entry->of_match)
66                         entry->of_match = (const struct udevice_id *)
67                                 ((ulong)entry->of_match + gd->reloc_off);
68                 if (entry->bind)
69                         entry->bind += gd->reloc_off;
70                 if (entry->probe)
71                         entry->probe += gd->reloc_off;
72                 if (entry->remove)
73                         entry->remove += gd->reloc_off;
74                 if (entry->unbind)
75                         entry->unbind += gd->reloc_off;
76                 if (entry->of_to_plat)
77                         entry->of_to_plat += gd->reloc_off;
78                 if (entry->child_post_bind)
79                         entry->child_post_bind += gd->reloc_off;
80                 if (entry->child_pre_probe)
81                         entry->child_pre_probe += gd->reloc_off;
82                 if (entry->child_post_remove)
83                         entry->child_post_remove += gd->reloc_off;
84                 /* OPS are fixed in every uclass post_probe function */
85                 if (entry->ops)
86                         entry->ops += gd->reloc_off;
87         }
88 }
89
90 void fix_uclass(void)
91 {
92         struct uclass_driver *uclass =
93                 ll_entry_start(struct uclass_driver, uclass_driver);
94         const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
95         struct uclass_driver *entry;
96
97         for (entry = uclass; entry != uclass + n_ents; entry++) {
98                 if (entry->post_bind)
99                         entry->post_bind += gd->reloc_off;
100                 if (entry->pre_unbind)
101                         entry->pre_unbind += gd->reloc_off;
102                 if (entry->pre_probe)
103                         entry->pre_probe += gd->reloc_off;
104                 if (entry->post_probe)
105                         entry->post_probe += gd->reloc_off;
106                 if (entry->pre_remove)
107                         entry->pre_remove += gd->reloc_off;
108                 if (entry->child_post_bind)
109                         entry->child_post_bind += gd->reloc_off;
110                 if (entry->child_pre_probe)
111                         entry->child_pre_probe += gd->reloc_off;
112                 if (entry->init)
113                         entry->init += gd->reloc_off;
114                 if (entry->destroy)
115                         entry->destroy += gd->reloc_off;
116                 /* FIXME maybe also need to fix these ops */
117                 if (entry->ops)
118                         entry->ops += gd->reloc_off;
119         }
120 }
121
122 void fix_devices(void)
123 {
124         struct driver_info *dev =
125                 ll_entry_start(struct driver_info, driver_info);
126         const int n_ents = ll_entry_count(struct driver_info, driver_info);
127         struct driver_info *entry;
128
129         for (entry = dev; entry != dev + n_ents; entry++) {
130                 if (entry->plat)
131                         entry->plat += gd->reloc_off;
132         }
133 }
134
135 static int dm_setup_inst(void)
136 {
137         DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
138
139         if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
140                 struct udevice_rt *urt;
141                 void *base;
142                 int n_ents;
143                 uint size;
144
145                 /* Allocate the udevice_rt table */
146                 n_ents = ll_entry_count(struct udevice, udevice);
147                 urt = calloc(n_ents, sizeof(struct udevice_rt));
148                 if (!urt)
149                         return log_msg_ret("urt", -ENOMEM);
150                 gd_set_dm_udevice_rt(urt);
151
152                 /* Now allocate space for the priv/plat data, and copy it in */
153                 size = __priv_data_end - __priv_data_start;
154
155                 base = calloc(1, size);
156                 if (!base)
157                         return log_msg_ret("priv", -ENOMEM);
158                 memcpy(base, __priv_data_start, size);
159                 gd_set_dm_priv_base(base);
160         }
161
162         return 0;
163 }
164
165 int dm_init(bool of_live)
166 {
167         int ret;
168
169         if (gd->dm_root) {
170                 dm_warn("Virtual root driver already exists!\n");
171                 return -EINVAL;
172         }
173         if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
174                 gd->uclass_root = &uclass_head;
175         } else {
176                 gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST;
177                 INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST);
178         }
179
180         if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
181                 fix_drivers();
182                 fix_uclass();
183                 fix_devices();
184         }
185
186         if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
187                 ret = dm_setup_inst();
188                 if (ret) {
189                         log_debug("dm_setup_inst() failed: %d\n", ret);
190                         return ret;
191                 }
192         } else {
193                 ret = device_bind_by_name(NULL, false, &root_info,
194                                           &DM_ROOT_NON_CONST);
195                 if (ret)
196                         return ret;
197                 if (CONFIG_IS_ENABLED(OF_CONTROL))
198                         dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root());
199                 ret = device_probe(DM_ROOT_NON_CONST);
200                 if (ret)
201                         return ret;
202         }
203
204         return 0;
205 }
206
207 int dm_uninit(void)
208 {
209         /* Remove non-vital devices first */
210         device_remove(dm_root(), DM_REMOVE_NON_VITAL);
211         device_remove(dm_root(), DM_REMOVE_NORMAL);
212         device_unbind(dm_root());
213         gd->dm_root = NULL;
214
215         return 0;
216 }
217
218 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
219 int dm_remove_devices_flags(uint flags)
220 {
221         device_remove(dm_root(), flags);
222
223         return 0;
224 }
225 #endif
226
227 int dm_scan_plat(bool pre_reloc_only)
228 {
229         int ret;
230
231         if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
232                 struct driver_rt *dyn;
233                 int n_ents;
234
235                 n_ents = ll_entry_count(struct driver_info, driver_info);
236                 dyn = calloc(n_ents, sizeof(struct driver_rt));
237                 if (!dyn)
238                         return -ENOMEM;
239                 gd_set_dm_driver_rt(dyn);
240         }
241
242         ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
243         if (ret == -ENOENT) {
244                 dm_warn("Some drivers were not found\n");
245                 ret = 0;
246         }
247
248         return ret;
249 }
250
251 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
252 /**
253  * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
254  *
255  * This scans the subnodes of a device tree node and and creates a driver
256  * for each one.
257  *
258  * @parent: Parent device for the devices that will be created
259  * @node: Node to scan
260  * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
261  * flag. If false bind all drivers.
262  * @return 0 if OK, -ve on error
263  */
264 static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
265                             bool pre_reloc_only)
266 {
267         int ret = 0, err = 0;
268         ofnode node;
269
270         if (!ofnode_valid(parent_node))
271                 return 0;
272
273         for (node = ofnode_first_subnode(parent_node);
274              ofnode_valid(node);
275              node = ofnode_next_subnode(node)) {
276                 const char *node_name = ofnode_get_name(node);
277
278                 if (!ofnode_is_enabled(node)) {
279                         pr_debug("   - ignoring disabled device\n");
280                         continue;
281                 }
282                 err = lists_bind_fdt(parent, node, NULL, pre_reloc_only);
283                 if (err && !ret) {
284                         ret = err;
285                         debug("%s: ret=%d\n", node_name, ret);
286                 }
287         }
288
289         if (ret)
290                 dm_warn("Some drivers failed to bind\n");
291
292         return ret;
293 }
294
295 int dm_scan_fdt_dev(struct udevice *dev)
296 {
297         return dm_scan_fdt_node(dev, dev_ofnode(dev),
298                                 gd->flags & GD_FLG_RELOC ? false : true);
299 }
300
301 int dm_scan_fdt(bool pre_reloc_only)
302 {
303         return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
304 }
305
306 static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
307 {
308         ofnode node;
309
310         node = ofnode_path(path);
311
312         return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
313 }
314
315 int dm_extended_scan(bool pre_reloc_only)
316 {
317         int ret, i;
318         const char * const nodes[] = {
319                 "/chosen",
320                 "/clocks",
321                 "/firmware"
322         };
323
324         ret = dm_scan_fdt(pre_reloc_only);
325         if (ret) {
326                 debug("dm_scan_fdt() failed: %d\n", ret);
327                 return ret;
328         }
329
330         /* Some nodes aren't devices themselves but may contain some */
331         for (i = 0; i < ARRAY_SIZE(nodes); i++) {
332                 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
333                 if (ret) {
334                         debug("dm_scan_fdt() scan for %s failed: %d\n",
335                               nodes[i], ret);
336                         return ret;
337                 }
338         }
339
340         return ret;
341 }
342 #endif
343
344 __weak int dm_scan_other(bool pre_reloc_only)
345 {
346         return 0;
347 }
348
349 #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
350 void *dm_priv_to_rw(void *priv)
351 {
352         long offset = priv - (void *)__priv_data_start;
353
354         return gd_dm_priv_base() + offset;
355 }
356 #endif
357
358 /**
359  * dm_scan() - Scan tables to bind devices
360  *
361  * Runs through the driver_info tables and binds the devices it finds. Then runs
362  * through the devicetree nodes. Finally calls dm_scan_other() to add any
363  * special devices
364  *
365  * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
366  * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
367  */
368 static int dm_scan(bool pre_reloc_only)
369 {
370         int ret;
371
372         ret = dm_scan_plat(pre_reloc_only);
373         if (ret) {
374                 debug("dm_scan_plat() failed: %d\n", ret);
375                 return ret;
376         }
377
378         if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
379                 ret = dm_extended_scan(pre_reloc_only);
380                 if (ret) {
381                         debug("dm_extended_scan() failed: %d\n", ret);
382                         return ret;
383                 }
384         }
385
386         ret = dm_scan_other(pre_reloc_only);
387         if (ret)
388                 return ret;
389
390         return 0;
391 }
392
393 int dm_init_and_scan(bool pre_reloc_only)
394 {
395         int ret;
396
397         ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
398         if (ret) {
399                 debug("dm_init() failed: %d\n", ret);
400                 return ret;
401         }
402         if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
403                 ret = dm_scan(pre_reloc_only);
404                 if (ret) {
405                         log_debug("dm_scan() failed: %d\n", ret);
406                         return ret;
407                 }
408         }
409
410         return 0;
411 }
412
413 #ifdef CONFIG_ACPIGEN
414 static int root_acpi_get_name(const struct udevice *dev, char *out_name)
415 {
416         return acpi_copy_name(out_name, "\\_SB");
417 }
418
419 struct acpi_ops root_acpi_ops = {
420         .get_name       = root_acpi_get_name,
421 };
422 #endif
423
424 /* This is the root driver - all drivers are children of this */
425 U_BOOT_DRIVER(root_driver) = {
426         .name   = "root_driver",
427         .id     = UCLASS_ROOT,
428         ACPI_OPS_PTR(&root_acpi_ops)
429 };
430
431 /* This is the root uclass */
432 UCLASS_DRIVER(root) = {
433         .name   = "root",
434         .id     = UCLASS_ROOT,
435 };