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 | |
Bin Meng | 841f321 | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 14 | struct sandbox_pci_emul_priv { |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 15 | int dev_count; |
| 16 | }; |
| 17 | |
| 18 | int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn, |
Bin Meng | 4345998 | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 19 | struct udevice **containerp, struct udevice **emulp) |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 20 | { |
| 21 | struct udevice *dev; |
| 22 | int ret; |
| 23 | |
Bin Meng | 4345998 | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 24 | *containerp = NULL; |
Bin Meng | b3f96b4 | 2018-08-03 01:14:43 -0700 | [diff] [blame] | 25 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev); |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 26 | if (ret) { |
| 27 | debug("%s: Could not find emulator for dev %x\n", __func__, |
| 28 | find_devfn); |
| 29 | return ret; |
| 30 | } |
Bin Meng | 4345998 | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 31 | *containerp = dev; |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 32 | |
Bin Meng | 4345998 | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 33 | 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 Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 40 | |
| 41 | return *emulp ? 0 : -ENODEV; |
| 42 | } |
| 43 | |
| 44 | static int sandbox_pci_emul_post_probe(struct udevice *dev) |
| 45 | { |
Bin Meng | 841f321 | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 46 | struct sandbox_pci_emul_priv *priv = dev->uclass->priv; |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 47 | |
| 48 | priv->dev_count++; |
| 49 | sandbox_set_enable_pci_map(true); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int sandbox_pci_emul_pre_remove(struct udevice *dev) |
| 55 | { |
Bin Meng | 841f321 | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 56 | struct sandbox_pci_emul_priv *priv = dev->uclass->priv; |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 57 | |
| 58 | priv->dev_count--; |
| 59 | sandbox_set_enable_pci_map(priv->dev_count > 0); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | UCLASS_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 Meng | 841f321 | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 69 | .priv_auto_alloc_size = sizeof(struct sandbox_pci_emul_priv), |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 70 | }; |