test: dm: pci: Test more than one PCI host controller
[oweals/u-boot.git] / test / dm / pci.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <asm/io.h>
9 #include <dm/test.h>
10 #include <test/ut.h>
11
12 /* Test that sandbox PCI works correctly */
13 static int dm_test_pci_base(struct unit_test_state *uts)
14 {
15         struct udevice *bus;
16
17         ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
18
19         return 0;
20 }
21 DM_TEST(dm_test_pci_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
22
23 /* Test that sandbox PCI bus numbering and device works correctly */
24 static int dm_test_pci_busdev(struct unit_test_state *uts)
25 {
26         struct udevice *bus;
27         struct udevice *emul, *swap;
28
29         /* Test bus#0 and its devices */
30         ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
31
32         ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 0, &emul));
33         ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
34         ut_assert(device_active(swap));
35         ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 1, &emul));
36         ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
37         ut_assert(device_active(swap));
38
39         /* Test bus#1 and its devices */
40         ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
41
42         ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 2, &emul));
43         ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
44         ut_assert(device_active(swap));
45         ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 3, &emul));
46         ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
47         ut_assert(device_active(swap));
48
49         return 0;
50 }
51 DM_TEST(dm_test_pci_busdev, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
52
53 /* Test that we can use the swapcase device correctly */
54 static int dm_test_pci_swapcase(struct unit_test_state *uts)
55 {
56         struct udevice *swap;
57         ulong io_addr, mem_addr;
58         char *ptr;
59
60         /* Check that asking for the device 0 automatically fires up PCI */
61         ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
62
63         /* First test I/O */
64         io_addr = dm_pci_read_bar32(swap, 0);
65         outb(2, io_addr);
66         ut_asserteq(2, inb(io_addr));
67
68         /*
69          * Now test memory mapping - note we must unmap and remap to cause
70          * the swapcase emulation to see our data and response.
71          */
72         mem_addr = dm_pci_read_bar32(swap, 1);
73         ptr = map_sysmem(mem_addr, 20);
74         strcpy(ptr, "This is a TesT");
75         unmap_sysmem(ptr);
76
77         ptr = map_sysmem(mem_addr, 20);
78         ut_asserteq_str("tHIS IS A tESt", ptr);
79         unmap_sysmem(ptr);
80
81         /* Check that asking for the device 1 automatically fires up PCI */
82         ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
83
84         /* First test I/O */
85         io_addr = dm_pci_read_bar32(swap, 0);
86         outb(2, io_addr);
87         ut_asserteq(2, inb(io_addr));
88
89         /*
90          * Now test memory mapping - note we must unmap and remap to cause
91          * the swapcase emulation to see our data and response.
92          */
93         mem_addr = dm_pci_read_bar32(swap, 1);
94         ptr = map_sysmem(mem_addr, 20);
95         strcpy(ptr, "This is a TesT");
96         unmap_sysmem(ptr);
97
98         ptr = map_sysmem(mem_addr, 20);
99         ut_asserteq_str("tHIS IS A tESt", ptr);
100         unmap_sysmem(ptr);
101
102         return 0;
103 }
104 DM_TEST(dm_test_pci_swapcase, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);