summaryrefslogtreecommitdiff
path: root/test/dm/mux-cmd.c
blob: 11c237b5da904acf8b02c537e6e3420ef228d7c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020 Texas Instruments Inc.
 * Pratyush Yadav <p.yadav@ti.com>
 */
#include <common.h>
#include <dm.h>
#include <mux.h>
#include <mux-internal.h>
#include <dt-bindings/mux/mux.h>
#include <asm/test.h>
#include <dm/test.h>
#include <test/ut.h>
#include <console.h>
#include <rand.h>

#define BUF_SIZE		256

/* Test 'mux list' */
static int dm_test_cmd_mux_list(struct unit_test_state *uts)
{
	char str[BUF_SIZE], *tok;
	struct udevice *dev;
	struct mux_chip *chip;
	struct mux_control *mux;
	int i;
	unsigned long val;

	sandbox_set_enable_memio(true);

	ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
					      &dev));
	chip = dev_get_uclass_priv(dev);
	ut_assertnonnull(chip);

	run_command("mux list", 0);
	ut_assert_nextline("a-mux-controller:");

	/*
	 * Check the table header to make sure we are not out of sync with the
	 * code in the command. If we are, catch it early.
	 */
	console_record_readline(str, BUF_SIZE);
	tok = strtok(str, " ");
	ut_asserteq_str("ID", tok);

	tok = strtok(NULL, " ");
	ut_asserteq_str("Selected", tok);

	tok = strtok(NULL, " ");
	ut_asserteq_str("Current", tok);
	tok = strtok(NULL, " ");
	ut_asserteq_str("State", tok);

	tok = strtok(NULL, " ");
	ut_asserteq_str("Idle", tok);
	tok = strtok(NULL, " ");
	ut_asserteq_str("State", tok);

	tok = strtok(NULL, " ");
	ut_asserteq_str("Num", tok);
	tok = strtok(NULL, " ");
	ut_asserteq_str("States", tok);

	for (i = 0; i < chip->controllers; i++) {
		mux = &chip->mux[i];

		console_record_readline(str, BUF_SIZE);

		/*
		 * Check if the ID printed matches with the ID of the chip we
		 * have.
		 */
		tok = strtok(str, " ");
		ut_assertok(strict_strtoul(tok, 10, &val));
		ut_asserteq(i, val);

		/* Check if mux selection state matches. */
		tok = strtok(NULL, " ");
		if (mux->in_use) {
			ut_asserteq_str("yes", tok);
		} else {
			ut_asserteq_str("no", tok);
		}

		/* Check if the current state matches. */
		tok = strtok(NULL, " ");
		if (mux->cached_state == MUX_IDLE_AS_IS) {
			ut_asserteq_str("unknown", tok);
		} else {
			ut_assertok(strict_strtoul(tok, 16, &val));
			ut_asserteq(mux->cached_state, val);
		}

		/* Check if the idle state matches */
		tok = strtok(NULL, " ");
		if (mux->idle_state == MUX_IDLE_AS_IS) {
			ut_asserteq_str("as-is", tok);
		} else {
			ut_assertok(strict_strtoul(tok, 16, &val));
			ut_asserteq(mux->idle_state, val);
		}

		/* Check if the number of states matches */
		tok = strtok(NULL, " ");
		ut_assertok(strict_strtoul(tok, 16, &val));
		ut_asserteq(mux->states, val);
	}

	return 0;
}
DM_TEST(dm_test_cmd_mux_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);

static int dm_test_cmd_mux_select(struct unit_test_state *uts)
{
	struct udevice *dev;
	struct mux_chip *chip;
	struct mux_control *mux;
	char cmd[BUF_SIZE];
	unsigned int i, state;

	sandbox_set_enable_memio(true);

	ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
					      &dev));
	chip = dev_get_uclass_priv(dev);
	ut_assertnonnull(chip);

	srand(get_ticks() + rand());
	for (i = 0; i < chip->controllers; i++) {
		mux = &chip->mux[i];

		state = rand() % mux->states;

		snprintf(cmd, BUF_SIZE, "mux select a-mux-controller %x %x", i,
			 state);
		run_command(cmd, 0);
		ut_asserteq(!!mux->in_use, true);
		ut_asserteq(state, mux->cached_state);

		ut_assertok(mux_control_deselect(mux));
	}

	return 0;
}
DM_TEST(dm_test_cmd_mux_select, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);

static int dm_test_cmd_mux_deselect(struct unit_test_state *uts)
{
	struct udevice *dev;
	struct mux_chip *chip;
	struct mux_control *mux;
	char cmd[BUF_SIZE];
	unsigned int i, state;

	sandbox_set_enable_memio(true);

	ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
					      &dev));
	chip = dev_get_uclass_priv(dev);
	ut_assertnonnull(chip);

	srand(get_ticks() + rand());
	for (i = 0; i < chip->controllers; i++) {
		mux = &chip->mux[i];

		state = rand() % mux->states;
		ut_assertok(mux_control_select(mux, state));

		snprintf(cmd, BUF_SIZE, "mux deselect a-mux-controller %d", i);
		run_command(cmd, 0);
		ut_asserteq(!!mux->in_use, false);
	}

	return 0;
}
DM_TEST(dm_test_cmd_mux_deselect, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);