test-main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <malloc.h>
  10. #include <dm/test.h>
  11. #include <dm/root.h>
  12. #include <dm/uclass-internal.h>
  13. #include <dm/ut.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. struct dm_test_state global_test_state;
  16. /* Get ready for testing */
  17. static int dm_test_init(struct dm_test_state *dms)
  18. {
  19. memset(dms, '\0', sizeof(*dms));
  20. gd->dm_root = NULL;
  21. memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
  22. ut_assertok(dm_init());
  23. dms->root = dm_root();
  24. return 0;
  25. }
  26. /* Ensure all the test devices are probed */
  27. static int do_autoprobe(struct dm_test_state *dms)
  28. {
  29. struct udevice *dev;
  30. int ret;
  31. /* Scanning the uclass is enough to probe all the devices */
  32. for (ret = uclass_first_device(UCLASS_TEST, &dev);
  33. dev;
  34. ret = uclass_next_device(&dev))
  35. ;
  36. return ret;
  37. }
  38. static int dm_test_destroy(struct dm_test_state *dms)
  39. {
  40. int id;
  41. for (id = 0; id < UCLASS_COUNT; id++) {
  42. struct uclass *uc;
  43. /*
  44. * If the uclass doesn't exist we don't want to create it. So
  45. * check that here before we call uclass_find_device()/
  46. */
  47. uc = uclass_find(id);
  48. if (!uc)
  49. continue;
  50. ut_assertok(uclass_destroy(uc));
  51. }
  52. return 0;
  53. }
  54. int dm_test_main(void)
  55. {
  56. struct dm_test *tests = ll_entry_start(struct dm_test, dm_test);
  57. const int n_ents = ll_entry_count(struct dm_test, dm_test);
  58. struct dm_test_state *dms = &global_test_state;
  59. struct dm_test *test;
  60. /*
  61. * If we have no device tree, or it only has a root node, then these
  62. * tests clearly aren't going to work...
  63. */
  64. if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
  65. puts("Please run with test device tree:\n"
  66. " dtc -I dts -O dtb test/dm/test.dts -o test/dm/test.dtb\n"
  67. " ./u-boot -d test/dm/test.dtb\n");
  68. ut_assert(gd->fdt_blob);
  69. }
  70. printf("Running %d driver model tests\n", n_ents);
  71. for (test = tests; test < tests + n_ents; test++) {
  72. printf("Test: %s\n", test->name);
  73. ut_assertok(dm_test_init(dms));
  74. dms->start = mallinfo();
  75. if (test->flags & DM_TESTF_SCAN_PDATA)
  76. ut_assertok(dm_scan_platdata(false));
  77. if (test->flags & DM_TESTF_PROBE_TEST)
  78. ut_assertok(do_autoprobe(dms));
  79. if (test->flags & DM_TESTF_SCAN_FDT)
  80. ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
  81. if (test->func(dms))
  82. break;
  83. ut_assertok(dm_test_destroy(dms));
  84. }
  85. printf("Failures: %d\n", dms->fail_count);
  86. return 0;
  87. }