test: dm: pci: Test driver binding with driver data provided
[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 <asm/test.h>
10 #include <dm/test.h>
11 #include <test/ut.h>
12
13 /* Test that sandbox PCI works correctly */
14 static int dm_test_pci_base(struct unit_test_state *uts)
15 {
16         struct udevice *bus;
17
18         ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
19
20         return 0;
21 }
22 DM_TEST(dm_test_pci_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
23
24 /* Test that sandbox PCI bus numbering and device works correctly */
25 static int dm_test_pci_busdev(struct unit_test_state *uts)
26 {
27         struct udevice *bus;
28         struct udevice *swap;
29         u16 vendor, device;
30
31         /* Test bus#0 and its devices */
32         ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
33
34         ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
35         vendor = 0;
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));
39         device = 0;
40         ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
41         ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
42
43         /* Test bus#1 and its devices */
44         ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
45
46         ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
47         vendor = 0;
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));
51         device = 0;
52         ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
53         ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
54
55         return 0;
56 }
57 DM_TEST(dm_test_pci_busdev, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
58
59 /* Test that we can use the swapcase device correctly */
60 static int dm_test_pci_swapcase(struct unit_test_state *uts)
61 {
62         struct udevice *swap;
63         ulong io_addr, mem_addr;
64         char *ptr;
65
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));
68
69         /* First test I/O */
70         io_addr = dm_pci_read_bar32(swap, 0);
71         outb(2, io_addr);
72         ut_asserteq(2, inb(io_addr));
73
74         /*
75          * Now test memory mapping - note we must unmap and remap to cause
76          * the swapcase emulation to see our data and response.
77          */
78         mem_addr = dm_pci_read_bar32(swap, 1);
79         ptr = map_sysmem(mem_addr, 20);
80         strcpy(ptr, "This is a TesT");
81         unmap_sysmem(ptr);
82
83         ptr = map_sysmem(mem_addr, 20);
84         ut_asserteq_str("tHIS IS A tESt", ptr);
85         unmap_sysmem(ptr);
86
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));
89
90         /* First test I/O */
91         io_addr = dm_pci_read_bar32(swap, 0);
92         outb(2, io_addr);
93         ut_asserteq(2, inb(io_addr));
94
95         /*
96          * Now test memory mapping - note we must unmap and remap to cause
97          * the swapcase emulation to see our data and response.
98          */
99         mem_addr = dm_pci_read_bar32(swap, 1);
100         ptr = map_sysmem(mem_addr, 20);
101         strcpy(ptr, "This is a TesT");
102         unmap_sysmem(ptr);
103
104         ptr = map_sysmem(mem_addr, 20);
105         ut_asserteq_str("tHIS IS A tESt", ptr);
106         unmap_sysmem(ptr);
107
108         return 0;
109 }
110 DM_TEST(dm_test_pci_swapcase, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
111
112 /* Test that we can dynamically bind the device driver correctly */
113 static int dm_test_pci_drvdata(struct unit_test_state *uts)
114 {
115         struct udevice *bus, *swap;
116
117         /* Check that asking for the device automatically fires up PCI */
118         ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
119
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);
124
125         return 0;
126 }
127 DM_TEST(dm_test_pci_drvdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);