1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020, Linaro Limited
5 * Tests scmi_agent uclass and the SCMI drivers implemented in other
6 * uclass devices probe when a SCMI server exposes resources.
8 * Note in test.dts the protocol@10 node in scmi node. Protocol 0x10 is not
9 * implemented in U-Boot SCMI components but the implementation is exepected
10 * to not complain on unknown protocol IDs, as long as it is not used. Note
11 * in test.dts tests that SCMI drivers probing does not fail for such an
12 * unknown SCMI protocol ID.
19 #include <asm/scmi_test.h>
20 #include <dm/device-internal.h>
22 #include <linux/kconfig.h>
23 #include <power/regulator.h>
26 static int ut_assert_scmi_state_preprobe(struct unit_test_state *uts)
28 struct sandbox_scmi_service *scmi_ctx = sandbox_scmi_service_ctx();
30 ut_assertnonnull(scmi_ctx);
31 ut_assertnull(scmi_ctx->agent);
36 static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts,
39 struct sandbox_scmi_devices *scmi_devices;
40 struct sandbox_scmi_service *scmi_ctx;
41 struct sandbox_scmi_agent *agent;
43 /* Device references to check context against test sequence */
44 scmi_devices = sandbox_scmi_devices_ctx(dev);
45 ut_assertnonnull(scmi_devices);
46 ut_asserteq(2, scmi_devices->clk_count);
47 ut_asserteq(1, scmi_devices->reset_count);
48 ut_asserteq(2, scmi_devices->regul_count);
50 /* State of the simulated SCMI server exposed */
51 scmi_ctx = sandbox_scmi_service_ctx();
52 ut_assertnonnull(scmi_ctx);
53 agent = scmi_ctx->agent;
54 ut_assertnonnull(agent);
55 ut_asserteq(3, agent->clk_count);
56 ut_assertnonnull(agent->clk);
57 ut_asserteq(1, agent->reset_count);
58 ut_assertnonnull(agent->reset);
59 ut_asserteq(2, agent->voltd_count);
60 ut_assertnonnull(agent->voltd);
65 static int load_sandbox_scmi_test_devices(struct unit_test_state *uts,
70 ret = ut_assert_scmi_state_preprobe(uts);
74 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_scmi",
76 ut_assertnonnull(*dev);
78 return ut_assert_scmi_state_postprobe(uts, *dev);
81 static int release_sandbox_scmi_test_devices(struct unit_test_state *uts,
84 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
86 /* Not sure test devices are fully removed, agent may not be visible */
91 * Test SCMI states when loading and releasing resources
92 * related to SCMI drivers.
94 static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts)
96 struct udevice *dev = NULL;
99 ret = load_sandbox_scmi_test_devices(uts, &dev);
101 ret = release_sandbox_scmi_test_devices(uts, dev);
105 DM_TEST(dm_test_scmi_sandbox_agent, UT_TESTF_SCAN_FDT);
107 static int dm_test_scmi_clocks(struct unit_test_state *uts)
109 struct sandbox_scmi_devices *scmi_devices;
110 struct sandbox_scmi_service *scmi_ctx;
111 struct sandbox_scmi_agent *agent;
116 ret = load_sandbox_scmi_test_devices(uts, &dev);
120 scmi_devices = sandbox_scmi_devices_ctx(dev);
121 ut_assertnonnull(scmi_devices);
122 scmi_ctx = sandbox_scmi_service_ctx();
123 ut_assertnonnull(scmi_ctx);
124 agent = scmi_ctx->agent;
125 ut_assertnonnull(agent);
127 /* Test SCMI clocks rate manipulation */
128 ut_asserteq(333, agent->clk[0].rate);
129 ut_asserteq(200, agent->clk[1].rate);
130 ut_asserteq(1000, agent->clk[2].rate);
132 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
133 ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1]));
135 ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088);
136 ut_assert(!ret_dev || ret_dev == 1088);
138 ut_asserteq(1088, agent->clk[0].rate);
139 ut_asserteq(200, agent->clk[1].rate);
140 ut_asserteq(1000, agent->clk[2].rate);
142 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
143 ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1]));
145 /* restore original rate for further tests */
146 ret_dev = clk_set_rate(&scmi_devices->clk[1], 333);
147 ut_assert(!ret_dev || ret_dev == 333);
149 /* Test SCMI clocks gating manipulation */
150 ut_assert(!agent->clk[0].enabled);
151 ut_assert(!agent->clk[1].enabled);
152 ut_assert(!agent->clk[2].enabled);
154 ut_asserteq(0, clk_enable(&scmi_devices->clk[1]));
156 ut_assert(agent->clk[0].enabled);
157 ut_assert(!agent->clk[1].enabled);
158 ut_assert(!agent->clk[2].enabled);
160 ut_assertok(clk_disable(&scmi_devices->clk[1]));
162 ut_assert(!agent->clk[0].enabled);
163 ut_assert(!agent->clk[1].enabled);
164 ut_assert(!agent->clk[2].enabled);
166 return release_sandbox_scmi_test_devices(uts, dev);
168 DM_TEST(dm_test_scmi_clocks, UT_TESTF_SCAN_FDT);
170 static int dm_test_scmi_resets(struct unit_test_state *uts)
172 struct sandbox_scmi_devices *scmi_devices;
173 struct sandbox_scmi_service *scmi_ctx;
174 struct sandbox_scmi_agent *agent;
175 struct udevice *dev = NULL;
178 ret = load_sandbox_scmi_test_devices(uts, &dev);
182 scmi_devices = sandbox_scmi_devices_ctx(dev);
183 ut_assertnonnull(scmi_devices);
184 scmi_ctx = sandbox_scmi_service_ctx();
185 ut_assertnonnull(scmi_ctx);
186 agent = scmi_ctx->agent;
187 ut_assertnonnull(agent);
189 /* Test SCMI resect controller manipulation */
190 ut_assert(!agent->reset[0].asserted)
192 ut_assertok(reset_assert(&scmi_devices->reset[0]));
193 ut_assert(agent->reset[0].asserted)
195 ut_assertok(reset_deassert(&scmi_devices->reset[0]));
196 ut_assert(!agent->reset[0].asserted);
198 return release_sandbox_scmi_test_devices(uts, dev);
200 DM_TEST(dm_test_scmi_resets, UT_TESTF_SCAN_FDT);
202 static int dm_test_scmi_voltage_domains(struct unit_test_state *uts)
204 struct sandbox_scmi_devices *scmi_devices;
205 struct sandbox_scmi_service *scmi_ctx;
206 struct sandbox_scmi_agent *agent;
207 struct dm_regulator_uclass_plat *uc_pdata;
209 struct udevice *regul0_dev;
211 ut_assertok(load_sandbox_scmi_test_devices(uts, &dev));
213 scmi_devices = sandbox_scmi_devices_ctx(dev);
214 ut_assertnonnull(scmi_devices);
215 scmi_ctx = sandbox_scmi_service_ctx();
216 ut_assertnonnull(scmi_ctx);
217 agent = scmi_ctx->agent;
218 ut_assertnonnull(agent);
220 /* Set/Get an SCMI voltage domain level */
221 regul0_dev = scmi_devices->regul[0];
222 ut_assert(regul0_dev);
224 uc_pdata = dev_get_uclass_plat(regul0_dev);
227 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->min_uV));
228 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->min_uV);
230 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->min_uV);
232 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->max_uV));
233 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->max_uV);
235 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->max_uV);
237 /* Enable/disable SCMI voltage domains */
238 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
239 ut_assertok(regulator_set_enable(scmi_devices->regul[1], false));
240 ut_assert(!agent->voltd[0].enabled);
241 ut_assert(!agent->voltd[1].enabled);
243 ut_assertok(regulator_set_enable(scmi_devices->regul[0], true));
244 ut_assert(agent->voltd[0].enabled);
245 ut_assert(!agent->voltd[1].enabled);
247 ut_assertok(regulator_set_enable(scmi_devices->regul[1], true));
248 ut_assert(agent->voltd[0].enabled);
249 ut_assert(agent->voltd[1].enabled);
251 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
252 ut_assert(!agent->voltd[0].enabled);
253 ut_assert(agent->voltd[1].enabled);
255 return release_sandbox_scmi_test_devices(uts, dev);
257 DM_TEST(dm_test_scmi_voltage_domains, UT_TESTF_SCAN_FDT);