test: dm: pci: Add tests for mixed static and dynamic devices on the same bus

In the Sandbox test configuration, PCI bus#0 only has static devices
while bus#1 only has dynamic devices. Create a bus#2 that has both
types of devices and test such.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 44d24f9..7035646 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -16,6 +16,7 @@
 		mmc1 = "/mmc1";
 		pci0 = &pci0;
 		pci1 = &pci1;
+		pci2 = &pci2;
 		remoteproc1 = &rproc_1;
 		remoteproc2 = &rproc_2;
 		rtc0 = &rtc_0;
@@ -330,6 +331,23 @@
 				    0x0c 0x00 0x1234 0x5678>;
 	};
 
+	pci2: pci-controller2 {
+		compatible = "sandbox,pci";
+		device_type = "pci";
+		#address-cells = <3>;
+		#size-cells = <2>;
+		ranges = <0x02000000 0 0x50000000 0x50000000 0 0x2000
+				0x01000000 0 0x60000000 0x60000000 0 0x2000>;
+		sandbox,dev-info = <0x08 0x00 0x1234 0x5678>;
+		pci@1f,0 {
+			compatible = "pci-generic";
+			reg = <0xf800 0 0 0 0>;
+			emul@1f,0 {
+				compatible = "sandbox,swap-case";
+			};
+		};
+	};
+
 	probing {
 		compatible = "simple-bus";
 		test1 {
diff --git a/test/dm/pci.c b/test/dm/pci.c
index 53d2ca1..e03f6ba 100644
--- a/test/dm/pci.c
+++ b/test/dm/pci.c
@@ -125,3 +125,66 @@
 	return 0;
 }
 DM_TEST(dm_test_pci_drvdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test that devices on PCI bus#2 can be accessed correctly */
+static int dm_test_pci_mixed(struct unit_test_state *uts)
+{
+	/* PCI bus#2 has both statically and dynamic declared devices */
+	struct udevice *bus, *swap;
+	u16 vendor, device;
+	ulong io_addr, mem_addr;
+	char *ptr;
+
+	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 2, &bus));
+
+	/* Test the dynamic device */
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x08, 0), &swap));
+	vendor = 0;
+	ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
+	ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
+
+	/* First test I/O */
+	io_addr = dm_pci_read_bar32(swap, 0);
+	outb(2, io_addr);
+	ut_asserteq(2, inb(io_addr));
+
+	/*
+	 * Now test memory mapping - note we must unmap and remap to cause
+	 * the swapcase emulation to see our data and response.
+	 */
+	mem_addr = dm_pci_read_bar32(swap, 1);
+	ptr = map_sysmem(mem_addr, 30);
+	strcpy(ptr, "This is a TesT oN dYNAMIc");
+	unmap_sysmem(ptr);
+
+	ptr = map_sysmem(mem_addr, 30);
+	ut_asserteq_str("tHIS IS A tESt On DynamiC", ptr);
+	unmap_sysmem(ptr);
+
+	/* Test the static device */
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x1f, 0), &swap));
+	device = 0;
+	ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
+	ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
+
+	/* First test I/O */
+	io_addr = dm_pci_read_bar32(swap, 0);
+	outb(2, io_addr);
+	ut_asserteq(2, inb(io_addr));
+
+	/*
+	 * Now test memory mapping - note we must unmap and remap to cause
+	 * the swapcase emulation to see our data and response.
+	 */
+	mem_addr = dm_pci_read_bar32(swap, 1);
+	ptr = map_sysmem(mem_addr, 30);
+	strcpy(ptr, "This is a TesT oN sTATIc");
+	unmap_sysmem(ptr);
+
+	ptr = map_sysmem(mem_addr, 30);
+	ut_asserteq_str("tHIS IS A tESt On StatiC", ptr);
+	unmap_sysmem(ptr);
+
+	return 0;
+}
+DM_TEST(dm_test_pci_mixed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);