blob: 3ca9a1567c925b82c653b91f61ad6c941432e3e9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd9e90bb2015-03-05 12:25:28 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassd9e90bb2015-03-05 12:25:28 -07005 */
6
7#include <common.h>
8#include <dm.h>
9#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Simon Glassd9e90bb2015-03-05 12:25:28 -070012#include <pci.h>
13#include <dm/lists.h>
14
Bin Meng11c41ab2018-08-03 01:14:49 -070015struct sandbox_pci_emul_priv {
Simon Glassd9e90bb2015-03-05 12:25:28 -070016 int dev_count;
17};
18
Simon Glass2a311e82020-01-27 08:49:37 -070019int sandbox_pci_get_emul(const struct udevice *bus, pci_dev_t find_devfn,
Bin Meng156bc6f2018-08-03 01:14:45 -070020 struct udevice **containerp, struct udevice **emulp)
Simon Glassd9e90bb2015-03-05 12:25:28 -070021{
Simon Glassa51fd072019-09-21 14:32:41 -060022 struct pci_emul_uc_priv *upriv;
Simon Glassd9e90bb2015-03-05 12:25:28 -070023 struct udevice *dev;
24 int ret;
25
Bin Meng156bc6f2018-08-03 01:14:45 -070026 *containerp = NULL;
Bin Meng2fc987e2018-08-03 01:14:43 -070027 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev);
Simon Glassd9e90bb2015-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 Meng156bc6f2018-08-03 01:14:45 -070033 *containerp = dev;
Simon Glassd9e90bb2015-03-05 12:25:28 -070034
Simon Glass1bc0f4f2019-08-31 17:59:32 -060035 ret = uclass_get_device_by_phandle(UCLASS_PCI_EMUL, dev, "sandbox,emul",
36 emulp);
Simon Glassa51fd072019-09-21 14:32:41 -060037 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 Meng156bc6f2018-08-03 01:14:45 -070046 *emulp = dev;
Simon Glassa51fd072019-09-21 14:32:41 -060047 }
48
49 return 0;
50}
Simon Glassd9e90bb2015-03-05 12:25:28 -070051
Simon Glassa51fd072019-09-21 14:32:41 -060052int 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 Glassd9e90bb2015-03-05 12:25:28 -070061}
62
Simon Glass72231f72019-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 Glassd9e90bb2015-03-05 12:25:28 -070083static int sandbox_pci_emul_post_probe(struct udevice *dev)
84{
Bin Meng11c41ab2018-08-03 01:14:49 -070085 struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
Simon Glassd9e90bb2015-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{
Bin Meng11c41ab2018-08-03 01:14:49 -070095 struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
Simon Glassd9e90bb2015-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 Glass8a2b47f2020-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 Glassd9e90bb2015-03-05 12:25:28 -0700110};
Simon Glassb98ba4c2019-09-25 08:56:10 -0600111
112/*
113 * This uclass is a child of the pci bus. Its platdata is not defined here so
114 * is defined by its parent, UCLASS_PCI, which uses struct pci_child_platdata.
Simon Glass8a2b47f2020-12-03 16:55:17 -0700115 * See per_child_platdata_auto in UCLASS_DRIVER(pci).
Simon Glassb98ba4c2019-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};