Coverage Report

Created: 2026-03-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/u-boot/test/dm/mux-cmd.c
Line
Count
Source
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 <dm.h>
7
#include <mux.h>
8
#include <mux-internal.h>
9
#include <dt-bindings/mux/mux.h>
10
#include <asm/test.h>
11
#include <dm/test.h>
12
#include <test/ut.h>
13
#include <console.h>
14
#include <rand.h>
15
#include <time.h>
16
17
0
#define BUF_SIZE    256
18
19
/* Test 'mux list' */
20
static int dm_test_cmd_mux_list(struct unit_test_state *uts)
21
0
{
22
0
  char str[BUF_SIZE], *tok;
23
0
  struct udevice *dev;
24
0
  struct mux_chip *chip;
25
0
  struct mux_control *mux;
26
0
  int i;
27
0
  unsigned long val;
28
29
0
  ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
30
0
                &dev));
31
0
  chip = dev_get_uclass_priv(dev);
32
0
  ut_assertnonnull(chip);
33
34
0
  run_command("mux list", 0);
35
0
  ut_assert_nextline("a-mux-controller:");
36
37
  /*
38
   * Check the table header to make sure we are not out of sync with the
39
   * code in the command. If we are, catch it early.
40
   */
41
0
  console_record_readline(str, BUF_SIZE);
42
0
  tok = strtok(str, " ");
43
0
  ut_asserteq_str("ID", tok);
44
45
0
  tok = strtok(NULL, " ");
46
0
  ut_asserteq_str("Selected", tok);
47
48
0
  tok = strtok(NULL, " ");
49
0
  ut_asserteq_str("Current", tok);
50
0
  tok = strtok(NULL, " ");
51
0
  ut_asserteq_str("State", tok);
52
53
0
  tok = strtok(NULL, " ");
54
0
  ut_asserteq_str("Idle", tok);
55
0
  tok = strtok(NULL, " ");
56
0
  ut_asserteq_str("State", tok);
57
58
0
  tok = strtok(NULL, " ");
59
0
  ut_asserteq_str("Num", tok);
60
0
  tok = strtok(NULL, " ");
61
0
  ut_asserteq_str("States", tok);
62
63
0
  for (i = 0; i < chip->controllers; i++) {
64
0
    mux = &chip->mux[i];
65
66
0
    console_record_readline(str, BUF_SIZE);
67
68
    /*
69
     * Check if the ID printed matches with the ID of the chip we
70
     * have.
71
     */
72
0
    tok = strtok(str, " ");
73
0
    ut_assertok(strict_strtoul(tok, 10, &val));
74
0
    ut_asserteq(i, val);
75
76
    /* Check if mux selection state matches. */
77
0
    tok = strtok(NULL, " ");
78
0
    if (mux->in_use) {
79
0
      ut_asserteq_str("yes", tok);
80
0
    } else {
81
0
      ut_asserteq_str("no", tok);
82
0
    }
83
84
    /* Check if the current state matches. */
85
0
    tok = strtok(NULL, " ");
86
0
    if (mux->cached_state == MUX_IDLE_AS_IS) {
87
0
      ut_asserteq_str("unknown", tok);
88
0
    } else {
89
0
      ut_assertok(strict_strtoul(tok, 16, &val));
90
0
      ut_asserteq(mux->cached_state, val);
91
0
    }
92
93
    /* Check if the idle state matches */
94
0
    tok = strtok(NULL, " ");
95
0
    if (mux->idle_state == MUX_IDLE_AS_IS) {
96
0
      ut_asserteq_str("as-is", tok);
97
0
    } else {
98
0
      ut_assertok(strict_strtoul(tok, 16, &val));
99
0
      ut_asserteq(mux->idle_state, val);
100
0
    }
101
102
    /* Check if the number of states matches */
103
0
    tok = strtok(NULL, " ");
104
0
    ut_assertok(strict_strtoul(tok, 16, &val));
105
0
    ut_asserteq(mux->states, val);
106
0
  }
107
108
0
  return 0;
109
0
}
110
DM_TEST(dm_test_cmd_mux_list, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
111
112
static int dm_test_cmd_mux_select(struct unit_test_state *uts)
113
0
{
114
0
  struct udevice *dev;
115
0
  struct mux_chip *chip;
116
0
  struct mux_control *mux;
117
0
  char cmd[BUF_SIZE];
118
0
  unsigned int i, state;
119
120
0
  ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
121
0
                &dev));
122
0
  chip = dev_get_uclass_priv(dev);
123
0
  ut_assertnonnull(chip);
124
125
0
  srand(get_ticks() + rand());
126
0
  for (i = 0; i < chip->controllers; i++) {
127
0
    mux = &chip->mux[i];
128
129
0
    state = rand() % mux->states;
130
131
0
    snprintf(cmd, BUF_SIZE, "mux select a-mux-controller %x %x", i,
132
0
       state);
133
0
    run_command(cmd, 0);
134
0
    ut_asserteq(!!mux->in_use, true);
135
0
    ut_asserteq(state, mux->cached_state);
136
137
0
    ut_assertok(mux_control_deselect(mux));
138
0
  }
139
140
0
  return 0;
141
0
}
142
DM_TEST(dm_test_cmd_mux_select, UTF_SCAN_PDATA | UTF_SCAN_FDT);
143
144
static int dm_test_cmd_mux_deselect(struct unit_test_state *uts)
145
0
{
146
0
  struct udevice *dev;
147
0
  struct mux_chip *chip;
148
0
  struct mux_control *mux;
149
0
  char cmd[BUF_SIZE];
150
0
  unsigned int i, state;
151
152
0
  ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
153
0
                &dev));
154
0
  chip = dev_get_uclass_priv(dev);
155
0
  ut_assertnonnull(chip);
156
157
0
  srand(get_ticks() + rand());
158
0
  for (i = 0; i < chip->controllers; i++) {
159
0
    mux = &chip->mux[i];
160
161
0
    state = rand() % mux->states;
162
0
    ut_assertok(mux_control_select(mux, state));
163
164
0
    snprintf(cmd, BUF_SIZE, "mux deselect a-mux-controller %d", i);
165
0
    run_command(cmd, 0);
166
0
    ut_asserteq(!!mux->in_use, false);
167
0
  }
168
169
0
  return 0;
170
0
}
171
DM_TEST(dm_test_cmd_mux_deselect, UTF_SCAN_PDATA | UTF_SCAN_FDT);