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 442f11cd91SSimon Glass void dm_fixup_for_gd_move(struct global_data *new_gd) 452f11cd91SSimon Glass { 462f11cd91SSimon Glass /* The sentinel node has moved, so update things that point to it */ 47b0d9512aSLokesh Vutla if (gd->dm_root) { 482f11cd91SSimon Glass new_gd->uclass_root.next->prev = &new_gd->uclass_root; 492f11cd91SSimon Glass new_gd->uclass_root.prev->next = &new_gd->uclass_root; 502f11cd91SSimon Glass } 51b0d9512aSLokesh Vutla } 522f11cd91SSimon Glass 5366eaea6cSStefan Roese fdt_addr_t dm_get_translation_offset(void) 5466eaea6cSStefan Roese { 5566eaea6cSStefan Roese struct udevice *root = dm_root(); 5666eaea6cSStefan Roese struct root_priv *priv = dev_get_priv(root); 5766eaea6cSStefan Roese 5866eaea6cSStefan Roese return priv->translation_offset; 5966eaea6cSStefan Roese } 6066eaea6cSStefan Roese 6166eaea6cSStefan Roese void dm_set_translation_offset(fdt_addr_t offs) 6266eaea6cSStefan Roese { 6366eaea6cSStefan Roese struct udevice *root = dm_root(); 6466eaea6cSStefan Roese struct root_priv *priv = dev_get_priv(root); 6566eaea6cSStefan Roese 6666eaea6cSStefan Roese priv->translation_offset = offs; 6766eaea6cSStefan Roese } 6866eaea6cSStefan Roese 69484fdf5bSMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC) 70484fdf5bSMichal Simek void fix_drivers(void) 71484fdf5bSMichal Simek { 72484fdf5bSMichal Simek struct driver *drv = 73484fdf5bSMichal Simek ll_entry_start(struct driver, driver); 74484fdf5bSMichal Simek const int n_ents = ll_entry_count(struct driver, driver); 75484fdf5bSMichal Simek struct driver *entry; 76484fdf5bSMichal Simek 77484fdf5bSMichal Simek for (entry = drv; entry != drv + n_ents; entry++) { 78484fdf5bSMichal Simek if (entry->of_match) 79484fdf5bSMichal Simek entry->of_match = (const struct udevice_id *) 80484fdf5bSMichal Simek ((u32)entry->of_match + gd->reloc_off); 81484fdf5bSMichal Simek if (entry->bind) 82484fdf5bSMichal Simek entry->bind += gd->reloc_off; 83484fdf5bSMichal Simek if (entry->probe) 84484fdf5bSMichal Simek entry->probe += gd->reloc_off; 85484fdf5bSMichal Simek if (entry->remove) 86484fdf5bSMichal Simek entry->remove += gd->reloc_off; 87484fdf5bSMichal Simek if (entry->unbind) 88484fdf5bSMichal Simek entry->unbind += gd->reloc_off; 89484fdf5bSMichal Simek if (entry->ofdata_to_platdata) 90484fdf5bSMichal Simek entry->ofdata_to_platdata += gd->reloc_off; 9131e1029aSMichal Simek if (entry->child_post_bind) 9231e1029aSMichal Simek entry->child_post_bind += gd->reloc_off; 93484fdf5bSMichal Simek if (entry->child_pre_probe) 94484fdf5bSMichal Simek entry->child_pre_probe += gd->reloc_off; 95484fdf5bSMichal Simek if (entry->child_post_remove) 96484fdf5bSMichal Simek entry->child_post_remove += gd->reloc_off; 97484fdf5bSMichal Simek /* OPS are fixed in every uclass post_probe function */ 98484fdf5bSMichal Simek if (entry->ops) 99484fdf5bSMichal Simek entry->ops += gd->reloc_off; 100484fdf5bSMichal Simek } 101484fdf5bSMichal Simek } 102484fdf5bSMichal Simek 103484fdf5bSMichal Simek void fix_uclass(void) 104484fdf5bSMichal Simek { 105484fdf5bSMichal Simek struct uclass_driver *uclass = 106484fdf5bSMichal Simek ll_entry_start(struct uclass_driver, uclass); 107484fdf5bSMichal Simek const int n_ents = ll_entry_count(struct uclass_driver, uclass); 108484fdf5bSMichal Simek struct uclass_driver *entry; 109484fdf5bSMichal Simek 110484fdf5bSMichal Simek for (entry = uclass; entry != uclass + n_ents; entry++) { 111484fdf5bSMichal Simek if (entry->post_bind) 112484fdf5bSMichal Simek entry->post_bind += gd->reloc_off; 113484fdf5bSMichal Simek if (entry->pre_unbind) 114484fdf5bSMichal Simek entry->pre_unbind += gd->reloc_off; 11531e1029aSMichal Simek if (entry->pre_probe) 11631e1029aSMichal Simek entry->pre_probe += gd->reloc_off; 117484fdf5bSMichal Simek if (entry->post_probe) 118484fdf5bSMichal Simek entry->post_probe += gd->reloc_off; 119484fdf5bSMichal Simek if (entry->pre_remove) 120484fdf5bSMichal Simek entry->pre_remove += gd->reloc_off; 12131e1029aSMichal Simek if (entry->child_post_bind) 12231e1029aSMichal Simek entry->child_post_bind += gd->reloc_off; 12331e1029aSMichal Simek if (entry->child_pre_probe) 12431e1029aSMichal Simek entry->child_pre_probe += gd->reloc_off; 125484fdf5bSMichal Simek if (entry->init) 126484fdf5bSMichal Simek entry->init += gd->reloc_off; 127484fdf5bSMichal Simek if (entry->destroy) 128484fdf5bSMichal Simek entry->destroy += gd->reloc_off; 129484fdf5bSMichal Simek /* FIXME maybe also need to fix these ops */ 130484fdf5bSMichal Simek if (entry->ops) 131484fdf5bSMichal Simek entry->ops += gd->reloc_off; 132484fdf5bSMichal Simek } 133484fdf5bSMichal Simek } 1345aeedebcSAngelo Dureghello 1355aeedebcSAngelo Dureghello void fix_devices(void) 1365aeedebcSAngelo Dureghello { 1375aeedebcSAngelo Dureghello struct driver_info *dev = 1385aeedebcSAngelo Dureghello ll_entry_start(struct driver_info, driver_info); 1395aeedebcSAngelo Dureghello const int n_ents = ll_entry_count(struct driver_info, driver_info); 1405aeedebcSAngelo Dureghello struct driver_info *entry; 1415aeedebcSAngelo Dureghello 1425aeedebcSAngelo Dureghello for (entry = dev; entry != dev + n_ents; entry++) { 1435aeedebcSAngelo Dureghello if (entry->platdata) 1445aeedebcSAngelo Dureghello entry->platdata += gd->reloc_off; 1455aeedebcSAngelo Dureghello } 1465aeedebcSAngelo Dureghello } 1475aeedebcSAngelo Dureghello 148484fdf5bSMichal Simek #endif 149484fdf5bSMichal Simek 1506494d708SSimon Glass int dm_init(void) 1516494d708SSimon Glass { 1526494d708SSimon Glass int ret; 1536494d708SSimon Glass 1546494d708SSimon Glass if (gd->dm_root) { 1556494d708SSimon Glass dm_warn("Virtual root driver already exists!\n"); 1566494d708SSimon Glass return -EINVAL; 1576494d708SSimon Glass } 15889876a55SSimon Glass INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST); 1596494d708SSimon Glass 160484fdf5bSMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC) 161484fdf5bSMichal Simek fix_drivers(); 162484fdf5bSMichal Simek fix_uclass(); 1635aeedebcSAngelo Dureghello fix_devices(); 164484fdf5bSMichal Simek #endif 165484fdf5bSMichal Simek 16600606d7eSSimon Glass ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST); 1676494d708SSimon Glass if (ret) 1686494d708SSimon Glass return ret; 1690f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL) 1704984de2bSSimon Glass DM_ROOT_NON_CONST->node = offset_to_ofnode(0); 1712f3b95dbSSimon Glass #endif 1727497812dSSimon Glass ret = device_probe(DM_ROOT_NON_CONST); 1737497812dSSimon Glass if (ret) 1747497812dSSimon Glass return ret; 1756494d708SSimon Glass 1766494d708SSimon Glass return 0; 1776494d708SSimon Glass } 1786494d708SSimon Glass 1799adbd7a1SSimon Glass int dm_uninit(void) 1809adbd7a1SSimon Glass { 181706865afSStefan Roese device_remove(dm_root(), DM_REMOVE_NORMAL); 1829adbd7a1SSimon Glass device_unbind(dm_root()); 1839adbd7a1SSimon Glass 1849adbd7a1SSimon Glass return 0; 1859adbd7a1SSimon Glass } 1869adbd7a1SSimon Glass 187bc85aa40SStefan Roese #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) 188bc85aa40SStefan Roese int dm_remove_devices_flags(uint flags) 189bc85aa40SStefan Roese { 190bc85aa40SStefan Roese device_remove(dm_root(), flags); 191bc85aa40SStefan Roese 192bc85aa40SStefan Roese return 0; 193bc85aa40SStefan Roese } 194bc85aa40SStefan Roese #endif 195bc85aa40SStefan Roese 19600606d7eSSimon Glass int dm_scan_platdata(bool pre_reloc_only) 1976494d708SSimon Glass { 1986494d708SSimon Glass int ret; 1996494d708SSimon Glass 20000606d7eSSimon Glass ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only); 2016494d708SSimon Glass if (ret == -ENOENT) { 2026494d708SSimon Glass dm_warn("Some drivers were not found\n"); 2036494d708SSimon Glass ret = 0; 2046494d708SSimon Glass } 2056494d708SSimon Glass 206cbf86d71SMasahiro Yamada return ret; 2076494d708SSimon Glass } 2086494d708SSimon Glass 20929629eb8SSimon Glass #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) 210a771a04fSSimon Glass /** 211a771a04fSSimon Glass * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node 212a771a04fSSimon Glass * 213a771a04fSSimon Glass * This scans the subnodes of a device tree node and and creates a driver 214a771a04fSSimon Glass * for each one. 215a771a04fSSimon Glass * 216a771a04fSSimon Glass * @parent: Parent device for the devices that will be created 217a771a04fSSimon Glass * @blob: Pointer to device tree blob 218a771a04fSSimon Glass * @offset: Offset of node to scan 219a771a04fSSimon Glass * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC 220a771a04fSSimon Glass * flag. If false bind all drivers. 221a771a04fSSimon Glass * @return 0 if OK, -ve on error 222a771a04fSSimon Glass */ 223a771a04fSSimon Glass static int dm_scan_fdt_node(struct udevice *parent, const void *blob, 224a771a04fSSimon Glass int offset, bool pre_reloc_only) 2256494d708SSimon Glass { 2266494d708SSimon Glass int ret = 0, err; 2276494d708SSimon Glass 2281ca7e206SSimon Glass for (offset = fdt_first_subnode(blob, offset); 2291ca7e206SSimon Glass offset > 0; 2301ca7e206SSimon Glass offset = fdt_next_subnode(blob, offset)) { 23100606d7eSSimon Glass if (pre_reloc_only && 23227326c7eSHeiko Stübner !dm_fdt_pre_reloc(blob, offset)) 23300606d7eSSimon Glass continue; 23494f7afdfSSimon Glass if (!fdtdec_get_is_enabled(blob, offset)) { 23594f7afdfSSimon Glass dm_dbg(" - ignoring disabled device\n"); 23694f7afdfSSimon Glass continue; 23794f7afdfSSimon Glass } 238*f5b5719cSSimon Glass err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL); 239bc7b2f43SSimon Glass if (err && !ret) { 2406494d708SSimon Glass ret = err; 241bc7b2f43SSimon Glass debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL), 242bc7b2f43SSimon Glass ret); 243bc7b2f43SSimon Glass } 2446494d708SSimon Glass } 2456494d708SSimon Glass 2466494d708SSimon Glass if (ret) 2476494d708SSimon Glass dm_warn("Some drivers failed to bind\n"); 2486494d708SSimon Glass 2496494d708SSimon Glass return ret; 2506494d708SSimon Glass } 2511ca7e206SSimon Glass 252cc7f66f7SSimon Glass int dm_scan_fdt_dev(struct udevice *dev) 253cc7f66f7SSimon Glass { 254e160f7d4SSimon Glass if (dev_of_offset(dev) == -1) 255cc7f66f7SSimon Glass return 0; 256cc7f66f7SSimon Glass 257e160f7d4SSimon Glass return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev), 258cc7f66f7SSimon Glass gd->flags & GD_FLG_RELOC ? false : true); 259cc7f66f7SSimon Glass } 260cc7f66f7SSimon Glass 2611ca7e206SSimon Glass int dm_scan_fdt(const void *blob, bool pre_reloc_only) 2621ca7e206SSimon Glass { 2631ca7e206SSimon Glass return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only); 2641ca7e206SSimon Glass } 2656494d708SSimon Glass #endif 2666494d708SSimon Glass 267bb58503dSSimon Glass __weak int dm_scan_other(bool pre_reloc_only) 268bb58503dSSimon Glass { 269bb58503dSSimon Glass return 0; 270bb58503dSSimon Glass } 271bb58503dSSimon Glass 272ab7cd627SSimon Glass int dm_init_and_scan(bool pre_reloc_only) 273ab7cd627SSimon Glass { 274ab7cd627SSimon Glass int ret; 275ab7cd627SSimon Glass 276ab7cd627SSimon Glass ret = dm_init(); 277ab7cd627SSimon Glass if (ret) { 278ab7cd627SSimon Glass debug("dm_init() failed: %d\n", ret); 279ab7cd627SSimon Glass return ret; 280ab7cd627SSimon Glass } 281ab7cd627SSimon Glass ret = dm_scan_platdata(pre_reloc_only); 282ab7cd627SSimon Glass if (ret) { 283ab7cd627SSimon Glass debug("dm_scan_platdata() failed: %d\n", ret); 284ab7cd627SSimon Glass return ret; 285ab7cd627SSimon Glass } 286b2b0d3e7SSimon Glass 28729629eb8SSimon Glass if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { 288ab7cd627SSimon Glass ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only); 289ab7cd627SSimon Glass if (ret) { 290ab7cd627SSimon Glass debug("dm_scan_fdt() failed: %d\n", ret); 291ab7cd627SSimon Glass return ret; 292ab7cd627SSimon Glass } 293b2b0d3e7SSimon Glass } 294b2b0d3e7SSimon Glass 295bb58503dSSimon Glass ret = dm_scan_other(pre_reloc_only); 296bb58503dSSimon Glass if (ret) 297bb58503dSSimon Glass return ret; 298ab7cd627SSimon Glass 299ab7cd627SSimon Glass return 0; 300ab7cd627SSimon Glass } 301ab7cd627SSimon Glass 3026494d708SSimon Glass /* This is the root driver - all drivers are children of this */ 3036494d708SSimon Glass U_BOOT_DRIVER(root_driver) = { 3046494d708SSimon Glass .name = "root_driver", 3056494d708SSimon Glass .id = UCLASS_ROOT, 30666eaea6cSStefan Roese .priv_auto_alloc_size = sizeof(struct root_priv), 3076494d708SSimon Glass }; 3086494d708SSimon Glass 3096494d708SSimon Glass /* This is the root uclass */ 3106494d708SSimon Glass UCLASS_DRIVER(root) = { 3116494d708SSimon Glass .name = "root", 3126494d708SSimon Glass .id = UCLASS_ROOT, 3136494d708SSimon Glass }; 314