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> |
| 9 | #include <dm/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 10 | #include <test/ut.h> |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 11 | |
| 12 | /* Test that sandbox PCI works correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 13 | static int dm_test_pci_base(struct unit_test_state *uts) |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 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 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame^] | 23 | /* Test that sandbox PCI bus numbering and device works correctly */ |
| 24 | static int dm_test_pci_busdev(struct unit_test_state *uts) |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 25 | { |
| 26 | struct udevice *bus; |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame^] | 27 | struct udevice *emul, *swap; |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 28 | |
| 29 | ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus)); |
| 30 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame^] | 31 | ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 0, &emul)); |
| 32 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap)); |
| 33 | ut_assert(device_active(swap)); |
| 34 | ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 1, &emul)); |
| 35 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap)); |
| 36 | ut_assert(device_active(swap)); |
| 37 | |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 38 | return 0; |
| 39 | } |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame^] | 40 | 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] | 41 | |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 42 | /* Test that we can use the swapcase device correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 43 | static int dm_test_pci_swapcase(struct unit_test_state *uts) |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 44 | { |
Bin Meng | 1445042 | 2018-08-03 01:14:38 -0700 | [diff] [blame] | 45 | struct udevice *swap; |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 46 | ulong io_addr, mem_addr; |
| 47 | char *ptr; |
| 48 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame^] | 49 | /* Check that asking for the device 0 automatically fires up PCI */ |
| 50 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap)); |
| 51 | |
| 52 | /* First test I/O */ |
| 53 | io_addr = dm_pci_read_bar32(swap, 0); |
| 54 | outb(2, io_addr); |
| 55 | ut_asserteq(2, inb(io_addr)); |
| 56 | |
| 57 | /* |
| 58 | * Now test memory mapping - note we must unmap and remap to cause |
| 59 | * the swapcase emulation to see our data and response. |
| 60 | */ |
| 61 | mem_addr = dm_pci_read_bar32(swap, 1); |
| 62 | ptr = map_sysmem(mem_addr, 20); |
| 63 | strcpy(ptr, "This is a TesT"); |
| 64 | unmap_sysmem(ptr); |
| 65 | |
| 66 | ptr = map_sysmem(mem_addr, 20); |
| 67 | ut_asserteq_str("tHIS IS A tESt", ptr); |
| 68 | unmap_sysmem(ptr); |
| 69 | |
| 70 | /* Check that asking for the device 1 automatically fires up PCI */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 71 | 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] | 72 | |
| 73 | /* First test I/O */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 74 | io_addr = dm_pci_read_bar32(swap, 0); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 75 | outb(2, io_addr); |
| 76 | ut_asserteq(2, inb(io_addr)); |
| 77 | |
| 78 | /* |
| 79 | * Now test memory mapping - note we must unmap and remap to cause |
| 80 | * the swapcase emulation to see our data and response. |
| 81 | */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 82 | mem_addr = dm_pci_read_bar32(swap, 1); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 83 | ptr = map_sysmem(mem_addr, 20); |
| 84 | strcpy(ptr, "This is a TesT"); |
| 85 | unmap_sysmem(ptr); |
| 86 | |
| 87 | ptr = map_sysmem(mem_addr, 20); |
| 88 | ut_asserteq_str("tHIS IS A tESt", ptr); |
| 89 | unmap_sysmem(ptr); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | DM_TEST(dm_test_pci_swapcase, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |