mux-cmd.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Texas Instruments Inc.
  4. * Pratyush Yadav <p.yadav@ti.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <mux.h>
  9. #include <mux-internal.h>
  10. #include <dt-bindings/mux/mux.h>
  11. #include <asm/test.h>
  12. #include <dm/test.h>
  13. #include <test/ut.h>
  14. #include <console.h>
  15. #include <rand.h>
  16. #define BUF_SIZE 256
  17. /* Test 'mux list' */
  18. static int dm_test_cmd_mux_list(struct unit_test_state *uts)
  19. {
  20. char str[BUF_SIZE], *tok;
  21. struct udevice *dev;
  22. struct mux_chip *chip;
  23. struct mux_control *mux;
  24. int i;
  25. unsigned long val;
  26. sandbox_set_enable_memio(true);
  27. ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
  28. &dev));
  29. chip = dev_get_uclass_priv(dev);
  30. ut_assertnonnull(chip);
  31. run_command("mux list", 0);
  32. ut_assert_nextline("a-mux-controller:");
  33. /*
  34. * Check the table header to make sure we are not out of sync with the
  35. * code in the command. If we are, catch it early.
  36. */
  37. console_record_readline(str, BUF_SIZE);
  38. tok = strtok(str, " ");
  39. ut_asserteq_str("ID", tok);
  40. tok = strtok(NULL, " ");
  41. ut_asserteq_str("Selected", tok);
  42. tok = strtok(NULL, " ");
  43. ut_asserteq_str("Current", tok);
  44. tok = strtok(NULL, " ");
  45. ut_asserteq_str("State", tok);
  46. tok = strtok(NULL, " ");
  47. ut_asserteq_str("Idle", tok);
  48. tok = strtok(NULL, " ");
  49. ut_asserteq_str("State", tok);
  50. tok = strtok(NULL, " ");
  51. ut_asserteq_str("Num", tok);
  52. tok = strtok(NULL, " ");
  53. ut_asserteq_str("States", tok);
  54. for (i = 0; i < chip->controllers; i++) {
  55. mux = &chip->mux[i];
  56. console_record_readline(str, BUF_SIZE);
  57. /*
  58. * Check if the ID printed matches with the ID of the chip we
  59. * have.
  60. */
  61. tok = strtok(str, " ");
  62. ut_assertok(strict_strtoul(tok, 10, &val));
  63. ut_asserteq(i, val);
  64. /* Check if mux selection state matches. */
  65. tok = strtok(NULL, " ");
  66. if (mux->in_use) {
  67. ut_asserteq_str("yes", tok);
  68. } else {
  69. ut_asserteq_str("no", tok);
  70. }
  71. /* Check if the current state matches. */
  72. tok = strtok(NULL, " ");
  73. if (mux->cached_state == MUX_IDLE_AS_IS) {
  74. ut_asserteq_str("unknown", tok);
  75. } else {
  76. ut_assertok(strict_strtoul(tok, 16, &val));
  77. ut_asserteq(mux->cached_state, val);
  78. }
  79. /* Check if the idle state matches */
  80. tok = strtok(NULL, " ");
  81. if (mux->idle_state == MUX_IDLE_AS_IS) {
  82. ut_asserteq_str("as-is", tok);
  83. } else {
  84. ut_assertok(strict_strtoul(tok, 16, &val));
  85. ut_asserteq(mux->idle_state, val);
  86. }
  87. /* Check if the number of states matches */
  88. tok = strtok(NULL, " ");
  89. ut_assertok(strict_strtoul(tok, 16, &val));
  90. ut_asserteq(mux->states, val);
  91. }
  92. return 0;
  93. }
  94. DM_TEST(dm_test_cmd_mux_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  95. static int dm_test_cmd_mux_select(struct unit_test_state *uts)
  96. {
  97. struct udevice *dev;
  98. struct mux_chip *chip;
  99. struct mux_control *mux;
  100. char cmd[BUF_SIZE];
  101. unsigned int i, state;
  102. sandbox_set_enable_memio(true);
  103. ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
  104. &dev));
  105. chip = dev_get_uclass_priv(dev);
  106. ut_assertnonnull(chip);
  107. srand(get_ticks() + rand());
  108. for (i = 0; i < chip->controllers; i++) {
  109. mux = &chip->mux[i];
  110. state = rand() % mux->states;
  111. snprintf(cmd, BUF_SIZE, "mux select a-mux-controller %x %x", i,
  112. state);
  113. run_command(cmd, 0);
  114. ut_asserteq(!!mux->in_use, true);
  115. ut_asserteq(state, mux->cached_state);
  116. ut_assertok(mux_control_deselect(mux));
  117. }
  118. return 0;
  119. }
  120. DM_TEST(dm_test_cmd_mux_select, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  121. static int dm_test_cmd_mux_deselect(struct unit_test_state *uts)
  122. {
  123. struct udevice *dev;
  124. struct mux_chip *chip;
  125. struct mux_control *mux;
  126. char cmd[BUF_SIZE];
  127. unsigned int i, state;
  128. sandbox_set_enable_memio(true);
  129. ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
  130. &dev));
  131. chip = dev_get_uclass_priv(dev);
  132. ut_assertnonnull(chip);
  133. srand(get_ticks() + rand());
  134. for (i = 0; i < chip->controllers; i++) {
  135. mux = &chip->mux[i];
  136. state = rand() % mux->states;
  137. ut_assertok(mux_control_select(mux, state));
  138. snprintf(cmd, BUF_SIZE, "mux deselect a-mux-controller %d", i);
  139. run_command(cmd, 0);
  140. ut_asserteq(!!mux->in_use, false);
  141. }
  142. return 0;
  143. }
  144. DM_TEST(dm_test_cmd_mux_deselect, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);