Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d9e90bb | 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 | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 12 | #include <pci.h> |
| 13 | #include <dm/lists.h> |
| 14 | |
Bin Meng | 11c41ab | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 15 | struct sandbox_pci_emul_priv { |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 16 | int dev_count; |
| 17 | }; |
| 18 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 19 | int sandbox_pci_get_emul(const struct udevice *bus, pci_dev_t find_devfn, |
Bin Meng | 156bc6f | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 20 | struct udevice **containerp, struct udevice **emulp) |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 21 | { |
Simon Glass | a51fd07 | 2019-09-21 14:32:41 -0600 | [diff] [blame] | 22 | struct pci_emul_uc_priv *upriv; |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 23 | struct udevice *dev; |
| 24 | int ret; |
| 25 | |
Bin Meng | 156bc6f | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 26 | *containerp = NULL; |
Bin Meng | 2fc987e | 2018-08-03 01:14:43 -0700 | [diff] [blame] | 27 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev); |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 28 | if (ret) { |
| 29 | debug("%s: Could not find emulator for dev %x\n", __func__, |
| 30 | find_devfn); |
| 31 | return ret; |
| 32 | } |
Bin Meng | 156bc6f | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 33 | *containerp = dev; |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 34 | |
Simon Glass | 1bc0f4f | 2019-08-31 17:59:32 -0600 | [diff] [blame] | 35 | ret = uclass_get_device_by_phandle(UCLASS_PCI_EMUL, dev, "sandbox,emul", |
| 36 | emulp); |
Simon Glass | a51fd07 | 2019-09-21 14:32:41 -0600 | [diff] [blame] | 37 | if (!ret) { |
| 38 | upriv = dev_get_uclass_priv(*emulp); |
| 39 | |
| 40 | upriv->client = dev; |
| 41 | } else if (device_get_uclass_id(dev) != UCLASS_PCI_GENERIC) { |
| 42 | /* |
| 43 | * See commit 4345998ae9df, |
| 44 | * "pci: sandbox: Support dynamically binding device driver" |
| 45 | */ |
Bin Meng | 156bc6f | 2018-08-03 01:14:45 -0700 | [diff] [blame] | 46 | *emulp = dev; |
Simon Glass | a51fd07 | 2019-09-21 14:32:41 -0600 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 51 | |
Simon Glass | a51fd07 | 2019-09-21 14:32:41 -0600 | [diff] [blame] | 52 | int sandbox_pci_get_client(struct udevice *emul, struct udevice **devp) |
| 53 | { |
| 54 | struct pci_emul_uc_priv *upriv = dev_get_uclass_priv(emul); |
| 55 | |
| 56 | if (!upriv->client) |
| 57 | return -ENOENT; |
| 58 | *devp = upriv->client; |
| 59 | |
| 60 | return 0; |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Simon Glass | 72231f7 | 2019-09-25 08:56:42 -0600 | [diff] [blame] | 63 | uint sandbox_pci_read_bar(u32 barval, int type, uint size) |
| 64 | { |
| 65 | u32 result; |
| 66 | |
| 67 | result = barval; |
| 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; |
| 73 | } else { |
| 74 | result = (~(size - 1) & |
| 75 | PCI_BASE_ADDRESS_MEM_MASK) | |
| 76 | PCI_BASE_ADDRESS_MEM_TYPE_32; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return result; |
| 81 | } |
| 82 | |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 83 | static int sandbox_pci_emul_post_probe(struct udevice *dev) |
| 84 | { |
Bin Meng | 11c41ab | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 85 | struct sandbox_pci_emul_priv *priv = dev->uclass->priv; |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 86 | |
| 87 | priv->dev_count++; |
| 88 | sandbox_set_enable_pci_map(true); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int sandbox_pci_emul_pre_remove(struct udevice *dev) |
| 94 | { |
Bin Meng | 11c41ab | 2018-08-03 01:14:49 -0700 | [diff] [blame] | 95 | struct sandbox_pci_emul_priv *priv = dev->uclass->priv; |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 96 | |
| 97 | priv->dev_count--; |
| 98 | sandbox_set_enable_pci_map(priv->dev_count > 0); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | UCLASS_DRIVER(pci_emul) = { |
| 104 | .id = UCLASS_PCI_EMUL, |
| 105 | .name = "pci_emul", |
| 106 | .post_probe = sandbox_pci_emul_post_probe, |
| 107 | .pre_remove = sandbox_pci_emul_pre_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 108 | .priv_auto = sizeof(struct sandbox_pci_emul_priv), |
| 109 | .per_device_auto = sizeof(struct pci_emul_uc_priv), |
Simon Glass | d9e90bb | 2015-03-05 12:25:28 -0700 | [diff] [blame] | 110 | }; |
Simon Glass | b98ba4c | 2019-09-25 08:56:10 -0600 | [diff] [blame] | 111 | |
| 112 | /* |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 113 | * This uclass is a child of the pci bus. Its plat is not defined here so |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame^] | 114 | * is defined by its parent, UCLASS_PCI, which uses struct pci_child_plat. |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 115 | * See per_child_plat_auto in UCLASS_DRIVER(pci). |
Simon Glass | b98ba4c | 2019-09-25 08:56:10 -0600 | [diff] [blame] | 116 | */ |
| 117 | UCLASS_DRIVER(pci_emul_parent) = { |
| 118 | .id = UCLASS_PCI_EMUL_PARENT, |
| 119 | .name = "pci_emul_parent", |
| 120 | .post_bind = dm_scan_fdt_dev, |
| 121 | }; |
| 122 | |
| 123 | static const struct udevice_id pci_emul_parent_ids[] = { |
| 124 | { .compatible = "sandbox,pci-emul-parent" }, |
| 125 | { } |
| 126 | }; |
| 127 | |
| 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, |
| 132 | }; |