blob: 38227583547341f080fa124818521d2dad08277d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass36d0d3b2015-03-05 12:25:28 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass36d0d3b2015-03-05 12:25:28 -07005 */
6
7#include <common.h>
8#include <dm.h>
9#include <fdtdec.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Simon Glass36d0d3b2015-03-05 12:25:28 -070011#include <pci.h>
12#include <dm/lists.h>
13
Bin Meng841f3212018-08-03 01:14:49 -070014struct sandbox_pci_emul_priv {
Simon Glass36d0d3b2015-03-05 12:25:28 -070015 int dev_count;
16};
17
18int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
Bin Meng43459982018-08-03 01:14:45 -070019 struct udevice **containerp, struct udevice **emulp)
Simon Glass36d0d3b2015-03-05 12:25:28 -070020{
21 struct udevice *dev;
22 int ret;
23
Bin Meng43459982018-08-03 01:14:45 -070024 *containerp = NULL;
Bin Mengb3f96b42018-08-03 01:14:43 -070025 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev);
Simon Glass36d0d3b2015-03-05 12:25:28 -070026 if (ret) {
27 debug("%s: Could not find emulator for dev %x\n", __func__,
28 find_devfn);
29 return ret;
30 }
Bin Meng43459982018-08-03 01:14:45 -070031 *containerp = dev;
Simon Glass36d0d3b2015-03-05 12:25:28 -070032
Bin Meng43459982018-08-03 01:14:45 -070033 if (device_get_uclass_id(dev) == UCLASS_PCI_GENERIC) {
34 ret = device_find_first_child(dev, emulp);
35 if (ret)
36 return ret;
37 } else {
38 *emulp = dev;
39 }
Simon Glass36d0d3b2015-03-05 12:25:28 -070040
41 return *emulp ? 0 : -ENODEV;
42}
43
44static int sandbox_pci_emul_post_probe(struct udevice *dev)
45{
Bin Meng841f3212018-08-03 01:14:49 -070046 struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
Simon Glass36d0d3b2015-03-05 12:25:28 -070047
48 priv->dev_count++;
49 sandbox_set_enable_pci_map(true);
50
51 return 0;
52}
53
54static int sandbox_pci_emul_pre_remove(struct udevice *dev)
55{
Bin Meng841f3212018-08-03 01:14:49 -070056 struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
Simon Glass36d0d3b2015-03-05 12:25:28 -070057
58 priv->dev_count--;
59 sandbox_set_enable_pci_map(priv->dev_count > 0);
60
61 return 0;
62}
63
64UCLASS_DRIVER(pci_emul) = {
65 .id = UCLASS_PCI_EMUL,
66 .name = "pci_emul",
67 .post_probe = sandbox_pci_emul_post_probe,
68 .pre_remove = sandbox_pci_emul_pre_remove,
Bin Meng841f3212018-08-03 01:14:49 -070069 .priv_auto_alloc_size = sizeof(struct sandbox_pci_emul_priv),
Simon Glass36d0d3b2015-03-05 12:25:28 -070070};