xref: /openbmc/u-boot/test/dm/pci.c (revision dd4808f9)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2d3b7ff14SSimon Glass /*
3d3b7ff14SSimon Glass  * Copyright (C) 2015 Google, Inc
4d3b7ff14SSimon Glass  */
5d3b7ff14SSimon Glass 
6d3b7ff14SSimon Glass #include <common.h>
7d3b7ff14SSimon Glass #include <dm.h>
8d3b7ff14SSimon Glass #include <asm/io.h>
9d3b7ff14SSimon Glass #include <dm/test.h>
10e721b882SJoe Hershberger #include <test/ut.h>
11d3b7ff14SSimon Glass 
12d3b7ff14SSimon Glass /* Test that sandbox PCI works correctly */
13e721b882SJoe Hershberger static int dm_test_pci_base(struct unit_test_state *uts)
14d3b7ff14SSimon Glass {
15d3b7ff14SSimon Glass 	struct udevice *bus;
16d3b7ff14SSimon Glass 
17d3b7ff14SSimon Glass 	ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
18d3b7ff14SSimon Glass 
19d3b7ff14SSimon Glass 	return 0;
20d3b7ff14SSimon Glass }
21d3b7ff14SSimon Glass DM_TEST(dm_test_pci_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
22d3b7ff14SSimon Glass 
232bb02e4fSSimon Glass /* Test that sandbox PCI bus numbering works correctly */
242bb02e4fSSimon Glass static int dm_test_pci_busnum(struct unit_test_state *uts)
252bb02e4fSSimon Glass {
262bb02e4fSSimon Glass 	struct udevice *bus;
272bb02e4fSSimon Glass 
282bb02e4fSSimon Glass 	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
292bb02e4fSSimon Glass 
302bb02e4fSSimon Glass 	return 0;
312bb02e4fSSimon Glass }
322bb02e4fSSimon Glass DM_TEST(dm_test_pci_busnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
332bb02e4fSSimon Glass 
34d3b7ff14SSimon Glass /* Test that we can use the swapcase device correctly */
35e721b882SJoe Hershberger static int dm_test_pci_swapcase(struct unit_test_state *uts)
36d3b7ff14SSimon Glass {
37*dd4808f9SBin Meng 	struct udevice *swap;
38d3b7ff14SSimon Glass 	ulong io_addr, mem_addr;
39d3b7ff14SSimon Glass 	char *ptr;
40d3b7ff14SSimon Glass 
41d3b7ff14SSimon Glass 	/* Check that asking for the device automatically fires up PCI */
42c0322412SSimon Glass 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
43d3b7ff14SSimon Glass 
44d3b7ff14SSimon Glass 	/* First test I/O */
45c0322412SSimon Glass 	io_addr = dm_pci_read_bar32(swap, 0);
46d3b7ff14SSimon Glass 	outb(2, io_addr);
47d3b7ff14SSimon Glass 	ut_asserteq(2, inb(io_addr));
48d3b7ff14SSimon Glass 
49d3b7ff14SSimon Glass 	/*
50d3b7ff14SSimon Glass 	 * Now test memory mapping - note we must unmap and remap to cause
51d3b7ff14SSimon Glass 	 * the swapcase emulation to see our data and response.
52d3b7ff14SSimon Glass 	 */
53c0322412SSimon Glass 	mem_addr = dm_pci_read_bar32(swap, 1);
54d3b7ff14SSimon Glass 	ptr = map_sysmem(mem_addr, 20);
55d3b7ff14SSimon Glass 	strcpy(ptr, "This is a TesT");
56d3b7ff14SSimon Glass 	unmap_sysmem(ptr);
57d3b7ff14SSimon Glass 
58d3b7ff14SSimon Glass 	ptr = map_sysmem(mem_addr, 20);
59d3b7ff14SSimon Glass 	ut_asserteq_str("tHIS IS A tESt", ptr);
60d3b7ff14SSimon Glass 	unmap_sysmem(ptr);
61d3b7ff14SSimon Glass 
62d3b7ff14SSimon Glass 	return 0;
63d3b7ff14SSimon Glass }
64d3b7ff14SSimon Glass DM_TEST(dm_test_pci_swapcase, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
65