aboutsummaryrefslogtreecommitdiff
path: root/test/dm/mux-mmio.c
blob: 3a871a19c7e277d0fdd23801e9a41743fb6bff69 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2017-2018 Texas Instruments Incorporated - https://www.ti.com/
 * Jean-Jacques Hiblot <jjhiblot@ti.com>
 */

#include <dm.h>
#include <mux.h>
#include <regmap.h>
#include <syscon.h>
#include <asm/test.h>
#include <dm/test.h>
#include <dm/device-internal.h>
#include <test/ut.h>

static int dm_test_mux_mmio_select(struct unit_test_state *uts)
{
	struct udevice *dev, *dev_b;
	struct regmap *map;
	struct mux_control *ctl0_a, *ctl0_b;
	struct mux_control *ctl1;
	struct mux_control *ctl_err;
	u32 val;
	int i;

	sandbox_set_enable_memio(true);

	ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
					      &dev));
	ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "b-test",
					      &dev_b));
	map = syscon_regmap_lookup_by_phandle(dev, "mux-syscon");
	ut_assertok_ptr(map);
	ut_assert(map);

	ut_assertok(mux_control_get(dev, "mux0", &ctl0_a));
	ut_assertok(mux_control_get(dev, "mux1", &ctl1));
	ut_asserteq(-ERANGE, mux_control_get(dev, "mux3", &ctl_err));
	ut_asserteq(-ENODATA, mux_control_get(dev, "dummy", &ctl_err));
	ut_assertok(mux_control_get(dev_b, "mux0", &ctl0_b));

	for (i = 0; i < mux_control_states(ctl0_a); i++) {
		/* Select a new state and verify the value in the regmap. */
		ut_assertok(mux_control_select(ctl0_a, i));
		ut_assertok(regmap_read(map, 0, &val));
		ut_asserteq(i, (val & 0x30) >> 4);
		/*
		 * Deselect the mux and verify that the value in the regmap
		 * reflects the idle state (fixed to MUX_IDLE_AS_IS).
		 */
		ut_assertok(mux_control_deselect(ctl0_a));
		ut_assertok(regmap_read(map, 0, &val));
		ut_asserteq(i, (val & 0x30) >> 4);
	}

	for (i = 0; i < mux_control_states(ctl1); i++) {
		/* Select a new state and verify the value in the regmap. */
		ut_assertok(mux_control_select(ctl1, i));
		ut_assertok(regmap_read(map, 0xc, &val));
		ut_asserteq(i, (val & 0x1E) >> 1);
		/*
		 * Deselect the mux and verify that the value in the regmap
		 * reflects the idle state (fixed to 2).
		 */
		ut_assertok(mux_control_deselect(ctl1));
		ut_assertok(regmap_read(map, 0xc, &val));
		ut_asserteq(2, (val & 0x1E) >> 1);
	}

	/* Try unbalanced selection/deselection. */
	ut_assertok(mux_control_select(ctl0_a, 0));
	ut_asserteq(-EBUSY, mux_control_select(ctl0_a, 1));
	ut_asserteq(-EBUSY, mux_control_select(ctl0_a, 0));
	ut_assertok(mux_control_deselect(ctl0_a));

	/* Try concurrent selection. */
	ut_assertok(mux_control_select(ctl0_a, 0));
	ut_assert(mux_control_select(ctl0_b, 0));
	ut_assertok(mux_control_deselect(ctl0_a));
	ut_assertok(mux_control_select(ctl0_b, 0));
	ut_assert(mux_control_select(ctl0_a, 0));
	ut_assertok(mux_control_deselect(ctl0_b));
	ut_assertok(mux_control_select(ctl0_a, 0));
	ut_assertok(mux_control_deselect(ctl0_a));

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

/* Test that managed API for mux work correctly */
static int dm_test_devm_mux_mmio(struct unit_test_state *uts)
{
	struct udevice *dev, *dev_b;
	struct mux_control *ctl0_a, *ctl0_b;
	struct mux_control *ctl1;
	struct mux_control *ctl_err;

	sandbox_set_enable_memio(true);

	ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
					      &dev));
	ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "b-test",
					      &dev_b));

	ctl0_a = devm_mux_control_get(dev, "mux0");
	ut_assertok_ptr(ctl0_a);
	ut_assert(ctl0_a);
	ctl1 = devm_mux_control_get(dev, "mux1");
	ut_assertok_ptr(ctl1);
	ut_assert(ctl1);
	ctl_err = devm_mux_control_get(dev, "mux3");
	ut_asserteq(-ERANGE, PTR_ERR(ctl_err));
	ctl_err = devm_mux_control_get(dev, "dummy");
	ut_asserteq(-ENODATA, PTR_ERR(ctl_err));

	ctl0_b = devm_mux_control_get(dev_b, "mux0");
	ut_assertok_ptr(ctl0_b);
	ut_assert(ctl0_b);

	/* Try concurrent selection. */
	ut_assertok(mux_control_select(ctl0_a, 0));
	ut_assert(mux_control_select(ctl0_b, 0));
	ut_assertok(mux_control_deselect(ctl0_a));
	ut_assertok(mux_control_select(ctl0_b, 0));
	ut_assert(mux_control_select(ctl0_a, 0));
	ut_assertok(mux_control_deselect(ctl0_b));

	/* Remove one device and check that the mux is released. */
	ut_assertok(mux_control_select(ctl0_a, 0));
	ut_assert(mux_control_select(ctl0_b, 0));
	device_remove(dev, DM_REMOVE_NORMAL);
	ut_assertok(mux_control_select(ctl0_b, 0));

	device_remove(dev_b, DM_REMOVE_NORMAL);
	return 0;
}
DM_TEST(dm_test_devm_mux_mmio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);