Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <asm/io.h> |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 9 | #include <asm/test.h> |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 10 | #include <dm/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 11 | #include <test/ut.h> |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 12 | |
| 13 | /* Test that sandbox PCI works correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | static int dm_test_pci_base(struct unit_test_state *uts) |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 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 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 24 | /* Test that sandbox PCI bus numbering and device works correctly */ |
| 25 | static int dm_test_pci_busdev(struct unit_test_state *uts) |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 26 | { |
| 27 | struct udevice *bus; |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 28 | struct udevice *swap; |
| 29 | u16 vendor, device; |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 30 | |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 31 | /* Test bus#0 and its devices */ |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 32 | ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus)); |
| 33 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 34 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap)); |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 35 | vendor = 0; |
| 36 | ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor)); |
| 37 | ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor); |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 38 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap)); |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 39 | device = 0; |
| 40 | ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device)); |
| 41 | ut_asserteq(SANDBOX_PCI_DEVICE_ID, device); |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 42 | |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 43 | /* Test bus#1 and its devices */ |
| 44 | ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus)); |
| 45 | |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 46 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap)); |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 47 | vendor = 0; |
| 48 | ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor)); |
| 49 | ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor); |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 50 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap)); |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 51 | device = 0; |
| 52 | ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device)); |
| 53 | ut_asserteq(SANDBOX_PCI_DEVICE_ID, device); |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 54 | |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 55 | return 0; |
| 56 | } |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 57 | DM_TEST(dm_test_pci_busdev, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 58 | |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 59 | /* Test that we can use the swapcase device correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 60 | static int dm_test_pci_swapcase(struct unit_test_state *uts) |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 61 | { |
Bin Meng | 1445042 | 2018-08-03 01:14:38 -0700 | [diff] [blame] | 62 | struct udevice *swap; |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 63 | ulong io_addr, mem_addr; |
| 64 | char *ptr; |
| 65 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 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 */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 88 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap)); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 89 | |
| 90 | /* First test I/O */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 91 | io_addr = dm_pci_read_bar32(swap, 0); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 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 | */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 99 | mem_addr = dm_pci_read_bar32(swap, 1); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 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); |
Bin Meng | 4e08073 | 2018-08-03 01:14:48 -0700 | [diff] [blame] | 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); |
Bin Meng | 510dddb | 2018-08-03 01:14:50 -0700 | [diff] [blame] | 128 | |
| 129 | /* Test that devices on PCI bus#2 can be accessed correctly */ |
| 130 | static int dm_test_pci_mixed(struct unit_test_state *uts) |
| 131 | { |
| 132 | /* PCI bus#2 has both statically and dynamic declared devices */ |
| 133 | struct udevice *bus, *swap; |
| 134 | u16 vendor, device; |
| 135 | ulong io_addr, mem_addr; |
| 136 | char *ptr; |
| 137 | |
| 138 | ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 2, &bus)); |
| 139 | |
| 140 | /* Test the dynamic device */ |
| 141 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x08, 0), &swap)); |
| 142 | vendor = 0; |
| 143 | ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor)); |
| 144 | ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor); |
| 145 | |
| 146 | /* First test I/O */ |
| 147 | io_addr = dm_pci_read_bar32(swap, 0); |
| 148 | outb(2, io_addr); |
| 149 | ut_asserteq(2, inb(io_addr)); |
| 150 | |
| 151 | /* |
| 152 | * Now test memory mapping - note we must unmap and remap to cause |
| 153 | * the swapcase emulation to see our data and response. |
| 154 | */ |
| 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"); |
| 158 | unmap_sysmem(ptr); |
| 159 | |
| 160 | ptr = map_sysmem(mem_addr, 30); |
| 161 | ut_asserteq_str("tHIS IS A tESt On DynamiC", ptr); |
| 162 | unmap_sysmem(ptr); |
| 163 | |
| 164 | /* Test the static device */ |
| 165 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x1f, 0), &swap)); |
| 166 | device = 0; |
| 167 | ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device)); |
| 168 | ut_asserteq(SANDBOX_PCI_DEVICE_ID, device); |
| 169 | |
| 170 | /* First test I/O */ |
| 171 | io_addr = dm_pci_read_bar32(swap, 0); |
| 172 | outb(2, io_addr); |
| 173 | ut_asserteq(2, inb(io_addr)); |
| 174 | |
| 175 | /* |
| 176 | * Now test memory mapping - note we must unmap and remap to cause |
| 177 | * the swapcase emulation to see our data and response. |
| 178 | */ |
| 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"); |
| 182 | unmap_sysmem(ptr); |
| 183 | |
| 184 | ptr = map_sysmem(mem_addr, 30); |
| 185 | ut_asserteq_str("tHIS IS A tESt On StatiC", ptr); |
| 186 | unmap_sysmem(ptr); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | DM_TEST(dm_test_pci_mixed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Bin Meng | d74d312 | 2018-08-03 01:14:53 -0700 | [diff] [blame] | 191 | |
| 192 | /* Test looking up PCI capability and extended capability */ |
| 193 | static int dm_test_pci_cap(struct unit_test_state *uts) |
| 194 | { |
| 195 | struct udevice *bus, *swap; |
| 196 | int cap; |
| 197 | |
| 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)); |
| 200 | |
| 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); |
| 204 | |
| 205 | /* look up PCI_CAP_ID_PCIX */ |
| 206 | cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX); |
| 207 | ut_asserteq(0, cap); |
| 208 | |
| 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)); |
| 211 | |
| 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); |
| 215 | |
| 216 | /* look up PCI_EXT_CAP_ID_SRIOV */ |
| 217 | cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV); |
| 218 | ut_asserteq(0, cap); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | DM_TEST(dm_test_pci_cap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |