Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 10 | #include <linux/libfdt.h> |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 11 | #include <pci.h> |
| 12 | #include <dm/lists.h> |
| 13 | |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 14 | struct sandbox_pci_priv { |
| 15 | int dev_count; |
| 16 | }; |
| 17 | |
| 18 | int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn, |
| 19 | struct udevice **emulp) |
| 20 | { |
| 21 | struct udevice *dev; |
| 22 | int ret; |
| 23 | |
| 24 | ret = pci_bus_find_devfn(bus, find_devfn, &dev); |
| 25 | if (ret) { |
| 26 | debug("%s: Could not find emulator for dev %x\n", __func__, |
| 27 | find_devfn); |
| 28 | return ret; |
| 29 | } |
| 30 | |
| 31 | ret = device_find_first_child(dev, emulp); |
| 32 | if (ret) |
| 33 | return ret; |
| 34 | |
| 35 | return *emulp ? 0 : -ENODEV; |
| 36 | } |
| 37 | |
| 38 | static int sandbox_pci_emul_post_probe(struct udevice *dev) |
| 39 | { |
| 40 | struct sandbox_pci_priv *priv = dev->uclass->priv; |
| 41 | |
| 42 | priv->dev_count++; |
| 43 | sandbox_set_enable_pci_map(true); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static int sandbox_pci_emul_pre_remove(struct udevice *dev) |
| 49 | { |
| 50 | struct sandbox_pci_priv *priv = dev->uclass->priv; |
| 51 | |
| 52 | priv->dev_count--; |
| 53 | sandbox_set_enable_pci_map(priv->dev_count > 0); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | UCLASS_DRIVER(pci_emul) = { |
| 59 | .id = UCLASS_PCI_EMUL, |
| 60 | .name = "pci_emul", |
| 61 | .post_probe = sandbox_pci_emul_post_probe, |
| 62 | .pre_remove = sandbox_pci_emul_pre_remove, |
| 63 | .priv_auto_alloc_size = sizeof(struct sandbox_pci_priv), |
| 64 | }; |