1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
13 /* Test that sandbox PCI works correctly */
14 static int dm_test_pci_base(struct unit_test_state *uts)
18 ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
22 DM_TEST(dm_test_pci_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
24 /* Test that sandbox PCI bus numbering and device works correctly */
25 static int dm_test_pci_busdev(struct unit_test_state *uts)
31 /* Test bus#0 and its devices */
32 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
34 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
36 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
37 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
38 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
40 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
41 ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
43 /* Test bus#1 and its devices */
44 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
46 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
48 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
49 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
50 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
52 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
53 ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
57 DM_TEST(dm_test_pci_busdev, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
59 /* Test that we can use the swapcase device correctly */
60 static int dm_test_pci_swapcase(struct unit_test_state *uts)
63 ulong io_addr, mem_addr;
66 /* Check that asking for the device 0 automatically fires up PCI */
67 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
70 io_addr = dm_pci_read_bar32(swap, 0);
72 ut_asserteq(2, inb(io_addr));
75 * Now test memory mapping - note we must unmap and remap to cause
76 * the swapcase emulation to see our data and response.
78 mem_addr = dm_pci_read_bar32(swap, 1);
79 ptr = map_sysmem(mem_addr, 20);
80 strcpy(ptr, "This is a TesT");
83 ptr = map_sysmem(mem_addr, 20);
84 ut_asserteq_str("tHIS IS A tESt", ptr);
87 /* Check that asking for the device 1 automatically fires up PCI */
88 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
91 io_addr = dm_pci_read_bar32(swap, 0);
93 ut_asserteq(2, inb(io_addr));
96 * Now test memory mapping - note we must unmap and remap to cause
97 * the swapcase emulation to see our data and response.
99 mem_addr = dm_pci_read_bar32(swap, 1);
100 ptr = map_sysmem(mem_addr, 20);
101 strcpy(ptr, "This is a TesT");
104 ptr = map_sysmem(mem_addr, 20);
105 ut_asserteq_str("tHIS IS A tESt", ptr);
110 DM_TEST(dm_test_pci_swapcase, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
112 /* Test that we can dynamically bind the device driver correctly */
113 static int dm_test_pci_drvdata(struct unit_test_state *uts)
115 struct udevice *bus, *swap;
117 /* Check that asking for the device automatically fires up PCI */
118 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
120 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
121 ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
122 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
123 ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
127 DM_TEST(dm_test_pci_drvdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
129 /* Test that devices on PCI bus#2 can be accessed correctly */
130 static int dm_test_pci_mixed(struct unit_test_state *uts)
132 /* PCI bus#2 has both statically and dynamic declared devices */
133 struct udevice *bus, *swap;
135 ulong io_addr, mem_addr;
138 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 2, &bus));
140 /* Test the dynamic device */
141 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x08, 0), &swap));
143 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
144 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
147 io_addr = dm_pci_read_bar32(swap, 0);
149 ut_asserteq(2, inb(io_addr));
152 * Now test memory mapping - note we must unmap and remap to cause
153 * the swapcase emulation to see our data and response.
155 mem_addr = dm_pci_read_bar32(swap, 1);
156 ptr = map_sysmem(mem_addr, 30);
157 strcpy(ptr, "This is a TesT oN dYNAMIc");
160 ptr = map_sysmem(mem_addr, 30);
161 ut_asserteq_str("tHIS IS A tESt On DynamiC", ptr);
164 /* Test the static device */
165 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x1f, 0), &swap));
167 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
168 ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
171 io_addr = dm_pci_read_bar32(swap, 0);
173 ut_asserteq(2, inb(io_addr));
176 * Now test memory mapping - note we must unmap and remap to cause
177 * the swapcase emulation to see our data and response.
179 mem_addr = dm_pci_read_bar32(swap, 1);
180 ptr = map_sysmem(mem_addr, 30);
181 strcpy(ptr, "This is a TesT oN sTATIc");
184 ptr = map_sysmem(mem_addr, 30);
185 ut_asserteq_str("tHIS IS A tESt On StatiC", ptr);
190 DM_TEST(dm_test_pci_mixed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
192 /* Test looking up PCI capability and extended capability */
193 static int dm_test_pci_cap(struct unit_test_state *uts)
195 struct udevice *bus, *swap;
198 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
199 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
201 /* look up PCI_CAP_ID_EXP */
202 cap = dm_pci_find_capability(swap, PCI_CAP_ID_EXP);
203 ut_asserteq(PCI_CAP_ID_EXP_OFFSET, cap);
205 /* look up PCI_CAP_ID_PCIX */
206 cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX);
209 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
210 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
212 /* look up PCI_EXT_CAP_ID_DSN */
213 cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_DSN);
214 ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
216 /* look up PCI_EXT_CAP_ID_SRIOV */
217 cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV);
222 DM_TEST(dm_test_pci_cap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);