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> |
Simon Glass | 9b69ba4 | 2019-09-25 08:56:10 -0600 | [diff] [blame^] | 13 | #include <dm/uclass-internal.h> |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 14 | |
Bin Meng | 841f321 | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 15 | struct sandbox_pci_emul_priv { |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 16 | int dev_count; |
| 17 | }; |
| 18 | |
| 19 | 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] | 20 | struct udevice **containerp, struct udevice **emulp) |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 21 | { |
| 22 | struct udevice *dev; |
| 23 | int ret; |
| 24 | |
Bin Meng | 4345998 | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 25 | *containerp = NULL; |
Bin Meng | b3f96b4 | 2018-08-03 01:14:43 -0700 | [diff] [blame] | 26 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev); |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 27 | if (ret) { |
| 28 | debug("%s: Could not find emulator for dev %x\n", __func__, |
| 29 | find_devfn); |
| 30 | return ret; |
| 31 | } |
Bin Meng | 4345998 | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 32 | *containerp = dev; |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 33 | |
Simon Glass | 9b69ba4 | 2019-09-25 08:56:10 -0600 | [diff] [blame^] | 34 | /* |
| 35 | * See commit 4345998ae9df, |
| 36 | * "pci: sandbox: Support dynamically binding device driver" |
| 37 | */ |
| 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) |
Bin Meng | 4345998 | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 41 | *emulp = dev; |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 42 | |
| 43 | return *emulp ? 0 : -ENODEV; |
| 44 | } |
| 45 | |
| 46 | static int sandbox_pci_emul_post_probe(struct udevice *dev) |
| 47 | { |
Bin Meng | 841f321 | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 48 | struct sandbox_pci_emul_priv *priv = dev->uclass->priv; |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 49 | |
| 50 | priv->dev_count++; |
| 51 | sandbox_set_enable_pci_map(true); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int sandbox_pci_emul_pre_remove(struct udevice *dev) |
| 57 | { |
Bin Meng | 841f321 | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 58 | struct sandbox_pci_emul_priv *priv = dev->uclass->priv; |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 59 | |
| 60 | priv->dev_count--; |
| 61 | sandbox_set_enable_pci_map(priv->dev_count > 0); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | UCLASS_DRIVER(pci_emul) = { |
| 67 | .id = UCLASS_PCI_EMUL, |
| 68 | .name = "pci_emul", |
| 69 | .post_probe = sandbox_pci_emul_post_probe, |
| 70 | .pre_remove = sandbox_pci_emul_pre_remove, |
Bin Meng | 841f321 | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 71 | .priv_auto_alloc_size = sizeof(struct sandbox_pci_emul_priv), |
Simon Glass | 36d0d3b | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 72 | }; |
Simon Glass | 9b69ba4 | 2019-09-25 08:56:10 -0600 | [diff] [blame^] | 73 | |
| 74 | /* |
| 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). |
| 78 | */ |
| 79 | UCLASS_DRIVER(pci_emul_parent) = { |
| 80 | .id = UCLASS_PCI_EMUL_PARENT, |
| 81 | .name = "pci_emul_parent", |
| 82 | .post_bind = dm_scan_fdt_dev, |
| 83 | }; |
| 84 | |
| 85 | static const struct udevice_id pci_emul_parent_ids[] = { |
| 86 | { .compatible = "sandbox,pci-emul-parent" }, |
| 87 | { } |
| 88 | }; |
| 89 | |
| 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, |
| 94 | }; |