]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
36d0d3b4 SG |
2 | /* |
3 | * Copyright (c) 2014 Google, Inc | |
4 | * Written by Simon Glass <[email protected]> | |
36d0d3b4 SG |
5 | */ |
6 | ||
36d0d3b4 SG |
7 | #include <dm.h> |
8 | #include <fdtdec.h> | |
f7ae49fc | 9 | #include <log.h> |
b08c8c48 | 10 | #include <linux/libfdt.h> |
36d0d3b4 SG |
11 | #include <pci.h> |
12 | #include <dm/lists.h> | |
13 | ||
841f3216 | 14 | struct sandbox_pci_emul_priv { |
36d0d3b4 SG |
15 | int dev_count; |
16 | }; | |
17 | ||
c4e72c4a | 18 | int sandbox_pci_get_emul(const struct udevice *bus, pci_dev_t find_devfn, |
4345998a | 19 | struct udevice **containerp, struct udevice **emulp) |
36d0d3b4 | 20 | { |
6498fda1 | 21 | struct pci_emul_uc_priv *upriv; |
36d0d3b4 SG |
22 | struct udevice *dev; |
23 | int ret; | |
24 | ||
4345998a | 25 | *containerp = NULL; |
b3f96b4f | 26 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev); |
36d0d3b4 SG |
27 | if (ret) { |
28 | debug("%s: Could not find emulator for dev %x\n", __func__, | |
29 | find_devfn); | |
30 | return ret; | |
31 | } | |
4345998a | 32 | *containerp = dev; |
36d0d3b4 | 33 | |
be0d8fa4 SG |
34 | ret = uclass_get_device_by_phandle(UCLASS_PCI_EMUL, dev, "sandbox,emul", |
35 | emulp); | |
6498fda1 SG |
36 | if (!ret) { |
37 | upriv = dev_get_uclass_priv(*emulp); | |
38 | ||
39 | upriv->client = dev; | |
40 | } else if (device_get_uclass_id(dev) != UCLASS_PCI_GENERIC) { | |
41 | /* | |
42 | * See commit 4345998ae9df, | |
43 | * "pci: sandbox: Support dynamically binding device driver" | |
44 | */ | |
4345998a | 45 | *emulp = dev; |
6498fda1 SG |
46 | } |
47 | ||
48 | return 0; | |
49 | } | |
36d0d3b4 | 50 | |
6498fda1 SG |
51 | int sandbox_pci_get_client(struct udevice *emul, struct udevice **devp) |
52 | { | |
53 | struct pci_emul_uc_priv *upriv = dev_get_uclass_priv(emul); | |
54 | ||
55 | if (!upriv->client) | |
56 | return -ENOENT; | |
57 | *devp = upriv->client; | |
58 | ||
59 | return 0; | |
36d0d3b4 SG |
60 | } |
61 | ||
75d8f494 SG |
62 | uint sandbox_pci_read_bar(u32 barval, int type, uint size) |
63 | { | |
64 | u32 result; | |
65 | ||
66 | result = barval; | |
67 | if (result == 0xffffffff) { | |
68 | if (type == PCI_BASE_ADDRESS_SPACE_IO) { | |
69 | result = (~(size - 1) & | |
70 | PCI_BASE_ADDRESS_IO_MASK) | | |
71 | PCI_BASE_ADDRESS_SPACE_IO; | |
72 | } else { | |
73 | result = (~(size - 1) & | |
74 | PCI_BASE_ADDRESS_MEM_MASK) | | |
75 | PCI_BASE_ADDRESS_MEM_TYPE_32; | |
76 | } | |
77 | } | |
78 | ||
79 | return result; | |
80 | } | |
81 | ||
36d0d3b4 SG |
82 | static int sandbox_pci_emul_post_probe(struct udevice *dev) |
83 | { | |
0fd3d911 | 84 | struct sandbox_pci_emul_priv *priv = uclass_get_priv(dev->uclass); |
36d0d3b4 SG |
85 | |
86 | priv->dev_count++; | |
87 | sandbox_set_enable_pci_map(true); | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
92 | static int sandbox_pci_emul_pre_remove(struct udevice *dev) | |
93 | { | |
0fd3d911 | 94 | struct sandbox_pci_emul_priv *priv = uclass_get_priv(dev->uclass); |
36d0d3b4 SG |
95 | |
96 | priv->dev_count--; | |
97 | sandbox_set_enable_pci_map(priv->dev_count > 0); | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | UCLASS_DRIVER(pci_emul) = { | |
103 | .id = UCLASS_PCI_EMUL, | |
104 | .name = "pci_emul", | |
105 | .post_probe = sandbox_pci_emul_post_probe, | |
106 | .pre_remove = sandbox_pci_emul_pre_remove, | |
41575d8e SG |
107 | .priv_auto = sizeof(struct sandbox_pci_emul_priv), |
108 | .per_device_auto = sizeof(struct pci_emul_uc_priv), | |
36d0d3b4 | 109 | }; |
9b69ba4a SG |
110 | |
111 | /* | |
caa4daa2 | 112 | * This uclass is a child of the pci bus. Its plat is not defined here so |
8a8d24bd | 113 | * is defined by its parent, UCLASS_PCI, which uses struct pci_child_plat. |
caa4daa2 | 114 | * See per_child_plat_auto in UCLASS_DRIVER(pci). |
9b69ba4a SG |
115 | */ |
116 | UCLASS_DRIVER(pci_emul_parent) = { | |
117 | .id = UCLASS_PCI_EMUL_PARENT, | |
118 | .name = "pci_emul_parent", | |
119 | .post_bind = dm_scan_fdt_dev, | |
120 | }; | |
121 | ||
122 | static const struct udevice_id pci_emul_parent_ids[] = { | |
123 | { .compatible = "sandbox,pci-emul-parent" }, | |
124 | { } | |
125 | }; | |
126 | ||
127 | U_BOOT_DRIVER(pci_emul_parent_drv) = { | |
128 | .name = "pci_emul_parent_drv", | |
129 | .id = UCLASS_PCI_EMUL_PARENT, | |
130 | .of_match = pci_emul_parent_ids, | |
131 | }; |