1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <linux/libfdt.h>
15 struct sandbox_pci_emul_priv {
19 int sandbox_pci_get_emul(const struct udevice *bus, pci_dev_t find_devfn,
20 struct udevice **containerp, struct udevice **emulp)
22 struct pci_emul_uc_priv *upriv;
27 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev);
29 debug("%s: Could not find emulator for dev %x\n", __func__,
35 ret = uclass_get_device_by_phandle(UCLASS_PCI_EMUL, dev, "sandbox,emul",
38 upriv = dev_get_uclass_priv(*emulp);
41 } else if (device_get_uclass_id(dev) != UCLASS_PCI_GENERIC) {
43 * See commit 4345998ae9df,
44 * "pci: sandbox: Support dynamically binding device driver"
52 int sandbox_pci_get_client(struct udevice *emul, struct udevice **devp)
54 struct pci_emul_uc_priv *upriv = dev_get_uclass_priv(emul);
58 *devp = upriv->client;
63 uint sandbox_pci_read_bar(u32 barval, int type, uint size)
68 if (result == 0xffffffff) {
69 if (type == PCI_BASE_ADDRESS_SPACE_IO) {
70 result = (~(size - 1) &
71 PCI_BASE_ADDRESS_IO_MASK) |
72 PCI_BASE_ADDRESS_SPACE_IO;
74 result = (~(size - 1) &
75 PCI_BASE_ADDRESS_MEM_MASK) |
76 PCI_BASE_ADDRESS_MEM_TYPE_32;
83 static int sandbox_pci_emul_post_probe(struct udevice *dev)
85 struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
88 sandbox_set_enable_pci_map(true);
93 static int sandbox_pci_emul_pre_remove(struct udevice *dev)
95 struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
98 sandbox_set_enable_pci_map(priv->dev_count > 0);
103 UCLASS_DRIVER(pci_emul) = {
104 .id = UCLASS_PCI_EMUL,
106 .post_probe = sandbox_pci_emul_post_probe,
107 .pre_remove = sandbox_pci_emul_pre_remove,
108 .priv_auto_alloc_size = sizeof(struct sandbox_pci_emul_priv),
109 .per_device_auto_alloc_size = sizeof(struct pci_emul_uc_priv),
113 * This uclass is a child of the pci bus. Its platdata is not defined here so
114 * is defined by its parent, UCLASS_PCI, which uses struct pci_child_platdata.
115 * See per_child_platdata_auto_alloc_size in UCLASS_DRIVER(pci).
117 UCLASS_DRIVER(pci_emul_parent) = {
118 .id = UCLASS_PCI_EMUL_PARENT,
119 .name = "pci_emul_parent",
120 .post_bind = dm_scan_fdt_dev,
123 static const struct udevice_id pci_emul_parent_ids[] = {
124 { .compatible = "sandbox,pci-emul-parent" },
128 U_BOOT_DRIVER(pci_emul_parent_drv) = {
129 .name = "pci_emul_parent_drv",
130 .id = UCLASS_PCI_EMUL_PARENT,
131 .of_match = pci_emul_parent_ids,