pci.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <asm/io.h>
  8. #include <asm/test.h>
  9. #include <dm/test.h>
  10. #include <test/ut.h>
  11. /* Test that sandbox PCI works correctly */
  12. static int dm_test_pci_base(struct unit_test_state *uts)
  13. {
  14. struct udevice *bus;
  15. ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
  16. return 0;
  17. }
  18. DM_TEST(dm_test_pci_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  19. /* Test that sandbox PCI bus numbering and device works correctly */
  20. static int dm_test_pci_busdev(struct unit_test_state *uts)
  21. {
  22. struct udevice *bus;
  23. struct udevice *swap;
  24. u16 vendor, device;
  25. /* Test bus#0 and its devices */
  26. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
  27. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
  28. vendor = 0;
  29. ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
  30. ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
  31. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
  32. device = 0;
  33. ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
  34. ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
  35. /* Test bus#1 and its devices */
  36. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
  37. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
  38. vendor = 0;
  39. ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
  40. ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
  41. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
  42. device = 0;
  43. ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
  44. ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
  45. return 0;
  46. }
  47. DM_TEST(dm_test_pci_busdev, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  48. /* Test that we can use the swapcase device correctly */
  49. static int dm_test_pci_swapcase(struct unit_test_state *uts)
  50. {
  51. struct udevice *swap;
  52. ulong io_addr, mem_addr;
  53. char *ptr;
  54. /* Check that asking for the device 0 automatically fires up PCI */
  55. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
  56. /* First test I/O */
  57. io_addr = dm_pci_read_bar32(swap, 0);
  58. outb(2, io_addr);
  59. ut_asserteq(2, inb(io_addr));
  60. /*
  61. * Now test memory mapping - note we must unmap and remap to cause
  62. * the swapcase emulation to see our data and response.
  63. */
  64. mem_addr = dm_pci_read_bar32(swap, 1);
  65. ptr = map_sysmem(mem_addr, 20);
  66. strcpy(ptr, "This is a TesT");
  67. unmap_sysmem(ptr);
  68. ptr = map_sysmem(mem_addr, 20);
  69. ut_asserteq_str("tHIS IS A tESt", ptr);
  70. unmap_sysmem(ptr);
  71. /* Check that asking for the device 1 automatically fires up PCI */
  72. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
  73. /* First test I/O */
  74. io_addr = dm_pci_read_bar32(swap, 0);
  75. outb(2, io_addr);
  76. ut_asserteq(2, inb(io_addr));
  77. /*
  78. * Now test memory mapping - note we must unmap and remap to cause
  79. * the swapcase emulation to see our data and response.
  80. */
  81. mem_addr = dm_pci_read_bar32(swap, 1);
  82. ptr = map_sysmem(mem_addr, 20);
  83. strcpy(ptr, "This is a TesT");
  84. unmap_sysmem(ptr);
  85. ptr = map_sysmem(mem_addr, 20);
  86. ut_asserteq_str("tHIS IS A tESt", ptr);
  87. unmap_sysmem(ptr);
  88. return 0;
  89. }
  90. DM_TEST(dm_test_pci_swapcase, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  91. /* Test that we can dynamically bind the device driver correctly */
  92. static int dm_test_pci_drvdata(struct unit_test_state *uts)
  93. {
  94. struct udevice *bus, *swap;
  95. /* Check that asking for the device automatically fires up PCI */
  96. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
  97. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
  98. ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
  99. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
  100. ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
  101. return 0;
  102. }
  103. DM_TEST(dm_test_pci_drvdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  104. /* Test that devices on PCI bus#2 can be accessed correctly */
  105. static int dm_test_pci_mixed(struct unit_test_state *uts)
  106. {
  107. /* PCI bus#2 has both statically and dynamic declared devices */
  108. struct udevice *bus, *swap;
  109. u16 vendor, device;
  110. ulong io_addr, mem_addr;
  111. char *ptr;
  112. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 2, &bus));
  113. /* Test the dynamic device */
  114. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x08, 0), &swap));
  115. vendor = 0;
  116. ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
  117. ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
  118. /* First test I/O */
  119. io_addr = dm_pci_read_bar32(swap, 0);
  120. outb(2, io_addr);
  121. ut_asserteq(2, inb(io_addr));
  122. /*
  123. * Now test memory mapping - note we must unmap and remap to cause
  124. * the swapcase emulation to see our data and response.
  125. */
  126. mem_addr = dm_pci_read_bar32(swap, 1);
  127. ptr = map_sysmem(mem_addr, 30);
  128. strcpy(ptr, "This is a TesT oN dYNAMIc");
  129. unmap_sysmem(ptr);
  130. ptr = map_sysmem(mem_addr, 30);
  131. ut_asserteq_str("tHIS IS A tESt On DynamiC", ptr);
  132. unmap_sysmem(ptr);
  133. /* Test the static device */
  134. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x1f, 0), &swap));
  135. device = 0;
  136. ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
  137. ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
  138. /* First test I/O */
  139. io_addr = dm_pci_read_bar32(swap, 0);
  140. outb(2, io_addr);
  141. ut_asserteq(2, inb(io_addr));
  142. /*
  143. * Now test memory mapping - note we must unmap and remap to cause
  144. * the swapcase emulation to see our data and response.
  145. */
  146. mem_addr = dm_pci_read_bar32(swap, 1);
  147. ptr = map_sysmem(mem_addr, 30);
  148. strcpy(ptr, "This is a TesT oN sTATIc");
  149. unmap_sysmem(ptr);
  150. ptr = map_sysmem(mem_addr, 30);
  151. ut_asserteq_str("tHIS IS A tESt On StatiC", ptr);
  152. unmap_sysmem(ptr);
  153. return 0;
  154. }
  155. DM_TEST(dm_test_pci_mixed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  156. /* Test looking up PCI capability and extended capability */
  157. static int dm_test_pci_cap(struct unit_test_state *uts)
  158. {
  159. struct udevice *bus, *swap;
  160. int cap;
  161. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
  162. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
  163. /* look up PCI_CAP_ID_EXP */
  164. cap = dm_pci_find_capability(swap, PCI_CAP_ID_EXP);
  165. ut_asserteq(PCI_CAP_ID_EXP_OFFSET, cap);
  166. /* look up PCI_CAP_ID_PCIX */
  167. cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX);
  168. ut_asserteq(0, cap);
  169. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
  170. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
  171. /* look up PCI_EXT_CAP_ID_DSN */
  172. cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_DSN);
  173. ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
  174. /* look up PCI_EXT_CAP_ID_SRIOV */
  175. cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV);
  176. ut_asserteq(0, cap);
  177. return 0;
  178. }
  179. DM_TEST(dm_test_pci_cap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);