blob: a0b8afb87a012fd52bdad5ade62ff5dd7c4534c6 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Simon Glass36d0d3b2015-03-05 12:25:28 -070012#include <pci.h>
13#include <dm/lists.h>
14
Bin Meng841f3212018-08-03 01:14:49 -070015struct sandbox_pci_emul_priv {
Simon Glass36d0d3b2015-03-05 12:25:28 -070016 int dev_count;
17};
18
Simon Glassc4e72c42020-01-27 08:49:37 -070019int sandbox_pci_get_emul(const struct udevice *bus, pci_dev_t find_devfn,
Bin Meng43459982018-08-03 01:14:45 -070020 struct udevice **containerp, struct udevice **emulp)
Simon Glass36d0d3b2015-03-05 12:25:28 -070021{
Simon Glass6498fda2019-09-21 14:32:41 -060022 struct pci_emul_uc_priv *upriv;
Simon Glass36d0d3b2015-03-05 12:25:28 -070023 struct udevice *dev;
24 int ret;
25
Bin Meng43459982018-08-03 01:14:45 -070026 *containerp = NULL;
Bin Mengb3f96b42018-08-03 01:14:43 -070027 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev);
Simon Glass36d0d3b2015-03-05 12:25:28 -070028 if (ret) {
29 debug("%s: Could not find emulator for dev %x\n", __func__,
30 find_devfn);
31 return ret;
32 }
Bin Meng43459982018-08-03 01:14:45 -070033 *containerp = dev;
Simon Glass36d0d3b2015-03-05 12:25:28 -070034
Simon Glassbe0d8fa2019-08-31 17:59:32 -060035 ret = uclass_get_device_by_phandle(UCLASS_PCI_EMUL, dev, "sandbox,emul",
36 emulp);
Simon Glass6498fda2019-09-21 14:32:41 -060037 if (!ret) {
38 upriv = dev_get_uclass_priv(*emulp);
Simon Glass36d0d3b2015-03-05 12:25:28 -070039
Simon Glass6498fda2019-09-21 14:32:41 -060040 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 */
46 *emulp = dev;
47 }
48
49 return 0;
50}
51
52int 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 Glass36d0d3b2015-03-05 12:25:28 -070061}
62
Simon Glass75d8f492019-09-25 08:56:42 -060063uint 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 Glass36d0d3b2015-03-05 12:25:28 -070083static int sandbox_pci_emul_post_probe(struct udevice *dev)
84{
Simon Glass0fd3d912020-12-22 19:30:28 -070085 struct sandbox_pci_emul_priv *priv = uclass_get_priv(dev->uclass);
Simon Glass36d0d3b2015-03-05 12:25:28 -070086
87 priv->dev_count++;
88 sandbox_set_enable_pci_map(true);
89
90 return 0;
91}
92
93static int sandbox_pci_emul_pre_remove(struct udevice *dev)
94{
Simon Glass0fd3d912020-12-22 19:30:28 -070095 struct sandbox_pci_emul_priv *priv = uclass_get_priv(dev->uclass);
Simon Glass36d0d3b2015-03-05 12:25:28 -070096
97 priv->dev_count--;
98 sandbox_set_enable_pci_map(priv->dev_count > 0);
99
100 return 0;
101}
102
103UCLASS_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 Glass41575d82020-12-03 16:55:17 -0700108 .priv_auto = sizeof(struct sandbox_pci_emul_priv),
109 .per_device_auto = sizeof(struct pci_emul_uc_priv),
Simon Glass36d0d3b2015-03-05 12:25:28 -0700110};
Simon Glass9b69ba42019-09-25 08:56:10 -0600111
112/*
Simon Glasscaa4daa2020-12-03 16:55:18 -0700113 * This uclass is a child of the pci bus. Its plat is not defined here so
Simon Glass8a8d24b2020-12-03 16:55:23 -0700114 * is defined by its parent, UCLASS_PCI, which uses struct pci_child_plat.
Simon Glasscaa4daa2020-12-03 16:55:18 -0700115 * See per_child_plat_auto in UCLASS_DRIVER(pci).
Simon Glass9b69ba42019-09-25 08:56:10 -0600116 */
117UCLASS_DRIVER(pci_emul_parent) = {
118 .id = UCLASS_PCI_EMUL_PARENT,
119 .name = "pci_emul_parent",
120 .post_bind = dm_scan_fdt_dev,
121};
122
123static const struct udevice_id pci_emul_parent_ids[] = {
124 { .compatible = "sandbox,pci-emul-parent" },
125 { }
126};
127
128U_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};