blob: e80667ef72a3226cbc04d5e20677c5f392fc96db [file] [log] [blame]
Etienne Carriere02fd1262020-09-09 18:44:00 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020, Linaro Limited
4 *
5 * Tests scmi_agent uclass and the SCMI drivers implemented in other
6 * uclass devices probe when a SCMI server exposes resources.
7 *
Etienne Carriere09665cb2022-02-21 09:22:39 +01008 * Note in test.dts the protocol@10 node in scmi node. Protocol 0x10 is not
Yuepeng Xing71cc0212022-12-02 14:23:07 +08009 * implemented in U-Boot SCMI components but the implementation is expected
Etienne Carriere02fd1262020-09-09 18:44:00 +020010 * 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.
13 */
14
15#include <common.h>
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020016#include <clk.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +020017#include <dm.h>
Etienne Carriere8b9b6892020-09-09 18:44:07 +020018#include <reset.h>
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +090019#include <scmi_agent.h>
AKASHI Takahiro49f366b2023-10-11 19:07:06 +090020#include <scmi_agent-uclass.h>
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +090021#include <scmi_protocols.h>
AKASHI Takahirod40686d2023-11-14 11:14:28 +090022#include <vsprintf.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +020023#include <asm/scmi_test.h>
24#include <dm/device-internal.h>
25#include <dm/test.h>
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020026#include <linux/kconfig.h>
Etienne Carriereb8f15cd2021-03-08 22:38:07 +010027#include <power/regulator.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +020028#include <test/ut.h>
29
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020030static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts,
AKASHI Takahirodae00462023-10-11 19:07:03 +090031 struct sandbox_scmi_agent *agent,
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020032 struct udevice *dev)
33{
34 struct sandbox_scmi_devices *scmi_devices;
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020035
36 /* Device references to check context against test sequence */
37 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020038 ut_assertnonnull(scmi_devices);
Etienne Carriere09665cb2022-02-21 09:22:39 +010039 ut_asserteq(2, scmi_devices->clk_count);
Etienne Carrierec83a8432021-03-08 22:38:08 +010040 ut_asserteq(1, scmi_devices->reset_count);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +010041 ut_asserteq(2, scmi_devices->regul_count);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020042
43 /* State of the simulated SCMI server exposed */
Etienne Carrierebf1f1322022-02-21 09:22:41 +010044 ut_asserteq(3, agent->clk_count);
Etienne Carriere09665cb2022-02-21 09:22:39 +010045 ut_assertnonnull(agent->clk);
46 ut_asserteq(1, agent->reset_count);
47 ut_assertnonnull(agent->reset);
48 ut_asserteq(2, agent->voltd_count);
49 ut_assertnonnull(agent->voltd);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020050
51 return 0;
52}
53
54static int load_sandbox_scmi_test_devices(struct unit_test_state *uts,
AKASHI Takahirodae00462023-10-11 19:07:03 +090055 struct sandbox_scmi_agent **ctx,
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020056 struct udevice **dev)
57{
AKASHI Takahirodae00462023-10-11 19:07:03 +090058 struct udevice *agent_dev;
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020059
AKASHI Takahirodae00462023-10-11 19:07:03 +090060 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
61 &agent_dev));
62 ut_assertnonnull(agent_dev);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020063
AKASHI Takahirodae00462023-10-11 19:07:03 +090064 *ctx = sandbox_scmi_agent_ctx(agent_dev);
65 ut_assertnonnull(*ctx);
66
67 /* probe */
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020068 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_scmi",
69 dev));
70 ut_assertnonnull(*dev);
71
AKASHI Takahirodae00462023-10-11 19:07:03 +090072 return ut_assert_scmi_state_postprobe(uts, *ctx, *dev);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020073}
74
75static int release_sandbox_scmi_test_devices(struct unit_test_state *uts,
76 struct udevice *dev)
77{
AKASHI Takahirodae00462023-10-11 19:07:03 +090078 /* un-probe */
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020079 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
80
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020081 return 0;
82}
83
Etienne Carriere02fd1262020-09-09 18:44:00 +020084/*
85 * Test SCMI states when loading and releasing resources
86 * related to SCMI drivers.
87 */
88static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts)
89{
AKASHI Takahirodae00462023-10-11 19:07:03 +090090 struct sandbox_scmi_agent *ctx;
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020091 struct udevice *dev = NULL;
92 int ret;
Etienne Carriere02fd1262020-09-09 18:44:00 +020093
AKASHI Takahirodae00462023-10-11 19:07:03 +090094 ret = load_sandbox_scmi_test_devices(uts, &ctx, &dev);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020095 if (!ret)
96 ret = release_sandbox_scmi_test_devices(uts, dev);
Etienne Carriere02fd1262020-09-09 18:44:00 +020097
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020098 return ret;
Etienne Carriere02fd1262020-09-09 18:44:00 +020099}
Etienne Carriere02fd1262020-09-09 18:44:00 +0200100DM_TEST(dm_test_scmi_sandbox_agent, UT_TESTF_SCAN_FDT);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200101
AKASHI Takahiro49f366b2023-10-11 19:07:06 +0900102static int dm_test_scmi_base(struct unit_test_state *uts)
103{
104 struct udevice *agent_dev, *base;
105 struct scmi_agent_priv *priv;
106 u32 version, num_agents, num_protocols, impl_version;
107 u32 attributes, agent_id;
108 u8 *vendor, *agent_name, *protocols;
109 int ret;
110
111 /* preparation */
112 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
113 &agent_dev));
114 ut_assertnonnull(agent_dev);
115 ut_assertnonnull(priv = dev_get_uclass_plat(agent_dev));
116 ut_assertnonnull(base = scmi_get_protocol(agent_dev,
117 SCMI_PROTOCOL_ID_BASE));
118
119 /* version */
120 ret = scmi_base_protocol_version(base, &version);
121 ut_assertok(ret);
122 ut_asserteq(priv->version, version);
123
124 /* protocol attributes */
125 ret = scmi_base_protocol_attrs(base, &num_agents, &num_protocols);
126 ut_assertok(ret);
127 ut_asserteq(priv->num_agents, num_agents);
128 ut_asserteq(priv->num_protocols, num_protocols);
129
130 /* discover vendor */
131 ret = scmi_base_discover_vendor(base, &vendor);
132 ut_assertok(ret);
133 ut_asserteq_str(priv->vendor, vendor);
134 free(vendor);
135
136 /* message attributes */
137 ret = scmi_base_protocol_message_attrs(base,
138 SCMI_BASE_DISCOVER_SUB_VENDOR,
139 &attributes);
140 ut_assertok(ret);
141 ut_assertok(attributes);
142
143 /* discover sub vendor */
144 ret = scmi_base_discover_sub_vendor(base, &vendor);
145 ut_assertok(ret);
146 ut_asserteq_str(priv->sub_vendor, vendor);
147 free(vendor);
148
149 /* impl version */
150 ret = scmi_base_discover_impl_version(base, &impl_version);
151 ut_assertok(ret);
152 ut_asserteq(priv->impl_version, impl_version);
153
154 /* discover agent (my self) */
155 ret = scmi_base_discover_agent(base, 0xffffffff, &agent_id,
156 &agent_name);
157 ut_assertok(ret);
158 ut_asserteq(priv->agent_id, agent_id);
159 ut_asserteq_str(priv->agent_name, agent_name);
160 free(agent_name);
161
162 /* discover protocols */
163 ret = scmi_base_discover_list_protocols(base, &protocols);
164 ut_asserteq(num_protocols, ret);
165 ut_asserteq_mem(priv->protocols, protocols, sizeof(u8) * num_protocols);
166 free(protocols);
167
168 /*
169 * NOTE: Sandbox SCMI driver handles device-0 only. It supports setting
170 * access and protocol permissions, but doesn't allow unsetting them nor
171 * resetting the configurations.
172 */
173 /* set device permissions */
174 ret = scmi_base_set_device_permissions(base, agent_id, 0,
175 SCMI_BASE_SET_DEVICE_PERMISSIONS_ACCESS);
176 ut_assertok(ret); /* SCMI_SUCCESS */
177 ret = scmi_base_set_device_permissions(base, agent_id, 1,
178 SCMI_BASE_SET_DEVICE_PERMISSIONS_ACCESS);
179 ut_asserteq(-ENOENT, ret); /* SCMI_NOT_FOUND */
180 ret = scmi_base_set_device_permissions(base, agent_id, 0, 0);
181 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
182
183 /* set protocol permissions */
184 ret = scmi_base_set_protocol_permissions(base, agent_id, 0,
185 SCMI_PROTOCOL_ID_CLOCK,
186 SCMI_BASE_SET_PROTOCOL_PERMISSIONS_ACCESS);
187 ut_assertok(ret); /* SCMI_SUCCESS */
188 ret = scmi_base_set_protocol_permissions(base, agent_id, 1,
189 SCMI_PROTOCOL_ID_CLOCK,
190 SCMI_BASE_SET_PROTOCOL_PERMISSIONS_ACCESS);
191 ut_asserteq(-ENOENT, ret); /* SCMI_NOT_FOUND */
192 ret = scmi_base_set_protocol_permissions(base, agent_id, 0,
193 SCMI_PROTOCOL_ID_CLOCK, 0);
194 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
195
196 /* reset agent configuration */
197 ret = scmi_base_reset_agent_configuration(base, agent_id, 0);
198 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
199 ret = scmi_base_reset_agent_configuration(base, agent_id,
200 SCMI_BASE_RESET_ALL_ACCESS_PERMISSIONS);
201 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
202 ret = scmi_base_reset_agent_configuration(base, agent_id, 0);
203 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
204
205 return 0;
206}
207
208DM_TEST(dm_test_scmi_base, UT_TESTF_SCAN_FDT);
209
AKASHI Takahirod40686d2023-11-14 11:14:28 +0900210static int dm_test_scmi_cmd(struct unit_test_state *uts)
211{
212 struct udevice *agent_dev;
213 int num_proto = 0;
214 char cmd_out[30];
215
216 if (!CONFIG_IS_ENABLED(CMD_SCMI))
217 return -EAGAIN;
218
219 /* preparation */
220 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
221 &agent_dev));
222 ut_assertnonnull(agent_dev);
223
224 /*
225 * Estimate the number of provided protocols.
226 * This estimation is correct as far as a corresponding
227 * protocol support is added to sandbox fake serer.
228 */
229 if (CONFIG_IS_ENABLED(POWER_DOMAIN))
230 num_proto++;
231 if (CONFIG_IS_ENABLED(CLK_SCMI))
232 num_proto++;
233 if (CONFIG_IS_ENABLED(RESET_SCMI))
234 num_proto++;
235 if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
236 num_proto++;
237
238 /* scmi info */
239 ut_assertok(run_command("scmi info", 0));
240
241 ut_assert_nextline("SCMI device: scmi");
242 snprintf(cmd_out, 30, " protocol version: 0x%x",
243 SCMI_BASE_PROTOCOL_VERSION);
244 ut_assert_nextline(cmd_out);
245 ut_assert_nextline(" # of agents: 2");
246 ut_assert_nextline(" 0: platform");
247 ut_assert_nextline(" > 1: OSPM");
248 snprintf(cmd_out, 30, " # of protocols: %d", num_proto);
249 ut_assert_nextline(cmd_out);
250 if (CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN))
251 ut_assert_nextline(" Power domain management");
252 if (CONFIG_IS_ENABLED(CLK_SCMI))
253 ut_assert_nextline(" Clock management");
254 if (CONFIG_IS_ENABLED(RESET_SCMI))
255 ut_assert_nextline(" Reset domain management");
256 if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
257 ut_assert_nextline(" Voltage domain management");
258 ut_assert_nextline(" vendor: U-Boot");
259 ut_assert_nextline(" sub vendor: Sandbox");
260 ut_assert_nextline(" impl version: 0x1");
261
262 ut_assert_console_end();
263
264 /* scmi perm_dev */
265 ut_assertok(run_command("scmi perm_dev 1 0 1", 0));
266 ut_assert_console_end();
267
268 ut_assert(run_command("scmi perm_dev 1 0 0", 0));
269 ut_assert_nextline("Denying access to device:0 failed (-13)");
270 ut_assert_console_end();
271
272 /* scmi perm_proto */
273 ut_assertok(run_command("scmi perm_proto 1 0 14 1", 0));
274 ut_assert_console_end();
275
276 ut_assert(run_command("scmi perm_proto 1 0 14 0", 0));
277 ut_assert_nextline("Denying access to protocol:0x14 on device:0 failed (-13)");
278 ut_assert_console_end();
279
280 /* scmi reset */
281 ut_assert(run_command("scmi reset 1 1", 0));
282 ut_assert_nextline("Reset failed (-13)");
283 ut_assert_console_end();
284
285 return 0;
286}
287
288DM_TEST(dm_test_scmi_cmd, UT_TESTF_SCAN_FDT);
289
AKASHI Takahiro9e3474d2023-10-16 14:39:46 +0900290static int dm_test_scmi_power_domains(struct unit_test_state *uts)
291{
292 struct sandbox_scmi_agent *agent;
293 struct sandbox_scmi_devices *scmi_devices;
294 struct udevice *agent_dev, *pwd, *dev;
295 u32 version, count, attributes, pstate;
296 u64 stats_addr;
297 size_t stats_len;
298 u8 *name;
299 int ret;
300
AKASHI Takahiro08372eb2023-11-14 11:14:24 +0900301 if (!CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN))
302 return -EAGAIN;
303
AKASHI Takahiro9e3474d2023-10-16 14:39:46 +0900304 /* preparation */
305 ut_assertok(load_sandbox_scmi_test_devices(uts, &agent, &dev));
306 ut_assertnonnull(agent);
307 scmi_devices = sandbox_scmi_devices_ctx(dev);
308 ut_assertnonnull(scmi_devices);
309 ut_asserteq(2, scmi_devices->pwdom->id); /* in test.dts */
310
311 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
312 &agent_dev));
313 ut_assertnonnull(agent_dev);
314 pwd = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_POWER_DOMAIN);
315 ut_assertnonnull(pwd);
316
317 /*
318 * SCMI Power domain management protocol interfaces
319 */
320 /* version */
321 ret = scmi_generic_protocol_version(pwd, SCMI_PROTOCOL_ID_POWER_DOMAIN,
322 &version);
323 ut_assertok(ret);
324 ut_asserteq(agent->pwdom_version, version);
325
326 /* protocol attributes */
327 ret = scmi_pwd_protocol_attrs(pwd, &count, &stats_addr, &stats_len);
328 ut_assertok(ret);
329 ut_asserteq(agent->pwdom_count, count);
330 ut_asserteq(0, stats_len);
331
332 /* protocol message attributes */
333 ret = scmi_pwd_protocol_message_attrs(pwd, SCMI_PWD_STATE_SET,
334 &attributes);
335 ut_assertok(ret);
336 ret = scmi_pwd_protocol_message_attrs(pwd, SCMI_PWD_STATE_NOTIFY,
337 &attributes);
338 ut_asserteq(-ENOENT, ret); /* the protocol not supported */
339
340 /* power domain attributes */
341 ret = scmi_pwd_attrs(pwd, 0, &attributes, &name);
342 ut_assertok(ret);
343 ut_asserteq_str("power-domain--0", name);
344 free(name);
345
346 ret = scmi_pwd_attrs(pwd, 10, &attributes, &name);
347 ut_asserteq(-ENOENT, ret); /* domain-10 doesn't exist */
348
349 /* power domain state set/get */
350 ret = scmi_pwd_state_set(pwd, 0, 0, 0);
351 ut_assertok(ret);
352 ret = scmi_pwd_state_get(pwd, 0, &pstate);
353 ut_assertok(ret);
354 ut_asserteq(0, pstate); /* ON */
355
356 ret = scmi_pwd_state_set(pwd, 0, 0, SCMI_PWD_PSTATE_TYPE_LOST);
357 ut_assertok(ret);
358 ret = scmi_pwd_state_get(pwd, 0, &pstate);
359 ut_assertok(ret);
360 ut_asserteq(SCMI_PWD_PSTATE_TYPE_LOST, pstate); /* OFF */
361
362 ret = scmi_pwd_state_set(pwd, 0, 10, 0);
363 ut_asserteq(-ENOENT, ret);
364
365 /* power domain name get */
366 ret = scmi_pwd_name_get(pwd, 0, &name);
367 ut_assertok(ret);
368 ut_asserteq_str("power-domain--0-extended", name);
369 free(name);
370
371 ret = scmi_pwd_name_get(pwd, 10, &name);
372 ut_asserteq(-ENOENT, ret); /* domain-10 doesn't exist */
373
374 /*
375 * U-Boot driver model interfaces
376 */
377 /* power_domain_on */
378 ret = power_domain_on(scmi_devices->pwdom);
379 ut_assertok(ret);
380 ret = scmi_pwd_state_get(pwd, scmi_devices->pwdom->id, &pstate);
381 ut_assertok(ret);
382 ut_asserteq(0, pstate); /* ON */
383
384 /* power_domain_off */
385 ret = power_domain_off(scmi_devices->pwdom);
386 ut_assertok(ret);
387 ret = scmi_pwd_state_get(pwd, scmi_devices->pwdom->id, &pstate);
388 ut_assertok(ret);
389 ut_asserteq(SCMI_PWD_PSTATE_TYPE_LOST, pstate); /* OFF */
390
391 return release_sandbox_scmi_test_devices(uts, dev);
392}
393
394DM_TEST(dm_test_scmi_power_domains, UT_TESTF_SCAN_FDT);
395
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200396static int dm_test_scmi_clocks(struct unit_test_state *uts)
397{
Etienne Carriere09665cb2022-02-21 09:22:39 +0100398 struct sandbox_scmi_agent *agent;
AKASHI Takahirodae00462023-10-11 19:07:03 +0900399 struct sandbox_scmi_devices *scmi_devices;
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +0900400 struct udevice *agent_dev, *clock_dev, *dev;
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200401 int ret_dev;
402 int ret;
403
AKASHI Takahiro08372eb2023-11-14 11:14:24 +0900404 if (!CONFIG_IS_ENABLED(CLK_SCMI))
405 return -EAGAIN;
406
AKASHI Takahirodae00462023-10-11 19:07:03 +0900407 ret = load_sandbox_scmi_test_devices(uts, &agent, &dev);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200408 if (ret)
409 return ret;
410
411 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere09665cb2022-02-21 09:22:39 +0100412 ut_assertnonnull(scmi_devices);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200413
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +0900414 /* Sandbox SCMI clock protocol has its own channel */
415 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
416 &agent_dev));
417 ut_assertnonnull(agent_dev);
418 clock_dev = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_CLOCK);
419 ut_assertnonnull(clock_dev);
420 ut_asserteq(0x14, sandbox_scmi_channel_id(clock_dev));
421
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200422 /* Test SCMI clocks rate manipulation */
Etienne Carrierebf1f1322022-02-21 09:22:41 +0100423 ut_asserteq(333, agent->clk[0].rate);
424 ut_asserteq(200, agent->clk[1].rate);
425 ut_asserteq(1000, agent->clk[2].rate);
426
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200427 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
428 ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1]));
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200429
430 ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088);
431 ut_assert(!ret_dev || ret_dev == 1088);
432
Etienne Carrierebf1f1322022-02-21 09:22:41 +0100433 ut_asserteq(1088, agent->clk[0].rate);
434 ut_asserteq(200, agent->clk[1].rate);
435 ut_asserteq(1000, agent->clk[2].rate);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200436
437 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
438 ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1]));
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200439
440 /* restore original rate for further tests */
441 ret_dev = clk_set_rate(&scmi_devices->clk[1], 333);
442 ut_assert(!ret_dev || ret_dev == 333);
443
444 /* Test SCMI clocks gating manipulation */
Etienne Carriere09665cb2022-02-21 09:22:39 +0100445 ut_assert(!agent->clk[0].enabled);
446 ut_assert(!agent->clk[1].enabled);
447 ut_assert(!agent->clk[2].enabled);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200448
449 ut_asserteq(0, clk_enable(&scmi_devices->clk[1]));
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200450
Etienne Carrierebf1f1322022-02-21 09:22:41 +0100451 ut_assert(agent->clk[0].enabled);
452 ut_assert(!agent->clk[1].enabled);
Etienne Carriere09665cb2022-02-21 09:22:39 +0100453 ut_assert(!agent->clk[2].enabled);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200454
455 ut_assertok(clk_disable(&scmi_devices->clk[1]));
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200456
Etienne Carriere09665cb2022-02-21 09:22:39 +0100457 ut_assert(!agent->clk[0].enabled);
458 ut_assert(!agent->clk[1].enabled);
459 ut_assert(!agent->clk[2].enabled);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200460
461 return release_sandbox_scmi_test_devices(uts, dev);
462}
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200463DM_TEST(dm_test_scmi_clocks, UT_TESTF_SCAN_FDT);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200464
465static int dm_test_scmi_resets(struct unit_test_state *uts)
466{
Etienne Carriere09665cb2022-02-21 09:22:39 +0100467 struct sandbox_scmi_agent *agent;
AKASHI Takahirodae00462023-10-11 19:07:03 +0900468 struct sandbox_scmi_devices *scmi_devices;
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +0900469 struct udevice *agent_dev, *reset_dev, *dev = NULL;
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200470 int ret;
471
AKASHI Takahiro08372eb2023-11-14 11:14:24 +0900472 if (!CONFIG_IS_ENABLED(RESET_SCMI))
473 return -EAGAIN;
474
AKASHI Takahirodae00462023-10-11 19:07:03 +0900475 ret = load_sandbox_scmi_test_devices(uts, &agent, &dev);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200476 if (ret)
477 return ret;
478
479 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere09665cb2022-02-21 09:22:39 +0100480 ut_assertnonnull(scmi_devices);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200481
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +0900482 /* Sandbox SCMI reset protocol doesn't have its own channel */
483 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
484 &agent_dev));
485 ut_assertnonnull(agent_dev);
486 reset_dev = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_RESET_DOMAIN);
487 ut_assertnonnull(reset_dev);
488 ut_asserteq(0x0, sandbox_scmi_channel_id(reset_dev));
489
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200490 /* Test SCMI resect controller manipulation */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100491 ut_assert(!agent->reset[0].asserted);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200492
493 ut_assertok(reset_assert(&scmi_devices->reset[0]));
Marek Vasut1531c4e2023-03-10 04:33:13 +0100494 ut_assert(agent->reset[0].asserted);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200495
496 ut_assertok(reset_deassert(&scmi_devices->reset[0]));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100497 ut_assert(!agent->reset[0].asserted);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200498
499 return release_sandbox_scmi_test_devices(uts, dev);
500}
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200501DM_TEST(dm_test_scmi_resets, UT_TESTF_SCAN_FDT);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100502
503static int dm_test_scmi_voltage_domains(struct unit_test_state *uts)
504{
Etienne Carriere09665cb2022-02-21 09:22:39 +0100505 struct sandbox_scmi_agent *agent;
AKASHI Takahirodae00462023-10-11 19:07:03 +0900506 struct sandbox_scmi_devices *scmi_devices;
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100507 struct dm_regulator_uclass_plat *uc_pdata;
508 struct udevice *dev;
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100509 struct udevice *regul0_dev;
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100510
AKASHI Takahiro08372eb2023-11-14 11:14:24 +0900511 if (!CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
512 return -EAGAIN;
513
AKASHI Takahirodae00462023-10-11 19:07:03 +0900514 ut_assertok(load_sandbox_scmi_test_devices(uts, &agent, &dev));
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100515
516 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere09665cb2022-02-21 09:22:39 +0100517 ut_assertnonnull(scmi_devices);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100518
519 /* Set/Get an SCMI voltage domain level */
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100520 regul0_dev = scmi_devices->regul[0];
521 ut_assert(regul0_dev);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100522
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100523 uc_pdata = dev_get_uclass_plat(regul0_dev);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100524 ut_assert(uc_pdata);
525
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100526 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->min_uV));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100527 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->min_uV);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100528
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100529 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->min_uV);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100530
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100531 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->max_uV));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100532 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->max_uV);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100533
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100534 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->max_uV);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100535
536 /* Enable/disable SCMI voltage domains */
537 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
538 ut_assertok(regulator_set_enable(scmi_devices->regul[1], false));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100539 ut_assert(!agent->voltd[0].enabled);
540 ut_assert(!agent->voltd[1].enabled);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100541
542 ut_assertok(regulator_set_enable(scmi_devices->regul[0], true));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100543 ut_assert(agent->voltd[0].enabled);
544 ut_assert(!agent->voltd[1].enabled);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100545
546 ut_assertok(regulator_set_enable(scmi_devices->regul[1], true));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100547 ut_assert(agent->voltd[0].enabled);
548 ut_assert(agent->voltd[1].enabled);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100549
550 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100551 ut_assert(!agent->voltd[0].enabled);
552 ut_assert(agent->voltd[1].enabled);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100553
554 return release_sandbox_scmi_test_devices(uts, dev);
555}
556DM_TEST(dm_test_scmi_voltage_domains, UT_TESTF_SCAN_FDT);