1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <linux/libfdt.h>
13 #include <dm/uclass-internal.h>
15 struct sandbox_pci_emul_priv {
19 int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
20 struct udevice **containerp, struct udevice **emulp)
26 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev);
28 debug("%s: Could not find emulator for dev %x\n", __func__,
35 * See commit 4345998ae9df,
36 * "pci: sandbox: Support dynamically binding device driver"
38 ret = uclass_find_device_by_phandle(UCLASS_PCI_EMUL, dev,
39 "sandbox,emul", emulp);
40 if (ret && device_get_uclass_id(dev) != UCLASS_PCI_GENERIC)
43 return *emulp ? 0 : -ENODEV;
46 static int sandbox_pci_emul_post_probe(struct udevice *dev)
48 struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
51 sandbox_set_enable_pci_map(true);
56 static int sandbox_pci_emul_pre_remove(struct udevice *dev)
58 struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
61 sandbox_set_enable_pci_map(priv->dev_count > 0);
66 UCLASS_DRIVER(pci_emul) = {
67 .id = UCLASS_PCI_EMUL,
69 .post_probe = sandbox_pci_emul_post_probe,
70 .pre_remove = sandbox_pci_emul_pre_remove,
71 .priv_auto_alloc_size = sizeof(struct sandbox_pci_emul_priv),
75 * This uclass is a child of the pci bus. Its platdata is not defined here so
76 * is defined by its parent, UCLASS_PCI, which uses struct pci_child_platdata.
77 * See per_child_platdata_auto_alloc_size in UCLASS_DRIVER(pci).
79 UCLASS_DRIVER(pci_emul_parent) = {
80 .id = UCLASS_PCI_EMUL_PARENT,
81 .name = "pci_emul_parent",
82 .post_bind = dm_scan_fdt_dev,
85 static const struct udevice_id pci_emul_parent_ids[] = {
86 { .compatible = "sandbox,pci-emul-parent" },
90 U_BOOT_DRIVER(pci_emul_parent_drv) = {
91 .name = "pci_emul_parent_drv",
92 .id = UCLASS_PCI_EMUL_PARENT,
93 .of_match = pci_emul_parent_ids,