blob: 3655594c26d60e3c991b287746e0927c647a199e [file] [log] [blame]
Etienne Carriere358599e2020-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 Carriere41d62e22022-02-21 09:22:39 +01008 * Note in test.dts the protocol@10 node in scmi node. Protocol 0x10 is not
Yuepeng Xing7943ae22022-12-02 14:23:07 +08009 * implemented in U-Boot SCMI components but the implementation is expected
Etienne Carriere358599e2020-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
Etienne Carriere87d4f272020-09-09 18:44:05 +020015#include <clk.h>
Etienne Carriere358599e2020-09-09 18:44:00 +020016#include <dm.h>
Etienne Carrierec0dd1772020-09-09 18:44:07 +020017#include <reset.h>
AKASHI Takahiroa89d9f42023-10-11 19:06:59 +090018#include <scmi_agent.h>
AKASHI Takahiro93ecae92023-10-11 19:07:06 +090019#include <scmi_agent-uclass.h>
AKASHI Takahiroa89d9f42023-10-11 19:06:59 +090020#include <scmi_protocols.h>
Raymond Maocb73fe92024-05-16 14:11:52 -070021#include <stdio.h>
Etienne Carriere358599e2020-09-09 18:44:00 +020022#include <asm/scmi_test.h>
23#include <dm/device-internal.h>
24#include <dm/test.h>
Etienne Carriere01242182021-03-08 22:38:07 +010025#include <power/regulator.h>
Etienne Carriere358599e2020-09-09 18:44:00 +020026#include <test/ut.h>
27
Etienne Carriere87d4f272020-09-09 18:44:05 +020028static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts,
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090029 struct sandbox_scmi_agent *agent,
Etienne Carriere87d4f272020-09-09 18:44:05 +020030 struct udevice *dev)
31{
32 struct sandbox_scmi_devices *scmi_devices;
Etienne Carriere87d4f272020-09-09 18:44:05 +020033
34 /* Device references to check context against test sequence */
35 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere87d4f272020-09-09 18:44:05 +020036 ut_assertnonnull(scmi_devices);
Etienne Carriere41d62e22022-02-21 09:22:39 +010037 ut_asserteq(2, scmi_devices->clk_count);
Etienne Carrierec3bba702021-03-08 22:38:08 +010038 ut_asserteq(1, scmi_devices->reset_count);
Etienne Carriere01242182021-03-08 22:38:07 +010039 ut_asserteq(2, scmi_devices->regul_count);
Etienne Carriere87d4f272020-09-09 18:44:05 +020040
41 /* State of the simulated SCMI server exposed */
Etienne Carriere10d3e5d2022-02-21 09:22:41 +010042 ut_asserteq(3, agent->clk_count);
Etienne Carriere41d62e22022-02-21 09:22:39 +010043 ut_assertnonnull(agent->clk);
44 ut_asserteq(1, agent->reset_count);
45 ut_assertnonnull(agent->reset);
46 ut_asserteq(2, agent->voltd_count);
47 ut_assertnonnull(agent->voltd);
Etienne Carriere87d4f272020-09-09 18:44:05 +020048
49 return 0;
50}
51
52static int load_sandbox_scmi_test_devices(struct unit_test_state *uts,
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090053 struct sandbox_scmi_agent **ctx,
Etienne Carriere87d4f272020-09-09 18:44:05 +020054 struct udevice **dev)
55{
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090056 struct udevice *agent_dev;
Etienne Carriere87d4f272020-09-09 18:44:05 +020057
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090058 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
59 &agent_dev));
60 ut_assertnonnull(agent_dev);
Etienne Carriere87d4f272020-09-09 18:44:05 +020061
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090062 *ctx = sandbox_scmi_agent_ctx(agent_dev);
63 ut_assertnonnull(*ctx);
64
65 /* probe */
Etienne Carriere87d4f272020-09-09 18:44:05 +020066 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_scmi",
67 dev));
68 ut_assertnonnull(*dev);
69
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090070 return ut_assert_scmi_state_postprobe(uts, *ctx, *dev);
Etienne Carriere87d4f272020-09-09 18:44:05 +020071}
72
73static int release_sandbox_scmi_test_devices(struct unit_test_state *uts,
74 struct udevice *dev)
75{
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090076 /* un-probe */
Etienne Carriere87d4f272020-09-09 18:44:05 +020077 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
78
Etienne Carriere87d4f272020-09-09 18:44:05 +020079 return 0;
80}
81
Etienne Carriere358599e2020-09-09 18:44:00 +020082/*
83 * Test SCMI states when loading and releasing resources
84 * related to SCMI drivers.
85 */
86static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts)
87{
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090088 struct sandbox_scmi_agent *ctx;
Etienne Carriere87d4f272020-09-09 18:44:05 +020089 struct udevice *dev = NULL;
90 int ret;
Etienne Carriere358599e2020-09-09 18:44:00 +020091
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +090092 ret = load_sandbox_scmi_test_devices(uts, &ctx, &dev);
Etienne Carriere87d4f272020-09-09 18:44:05 +020093 if (!ret)
94 ret = release_sandbox_scmi_test_devices(uts, dev);
Etienne Carriere358599e2020-09-09 18:44:00 +020095
Etienne Carriere87d4f272020-09-09 18:44:05 +020096 return ret;
Etienne Carriere358599e2020-09-09 18:44:00 +020097}
Simon Glass725c4382024-08-22 07:57:48 -060098DM_TEST(dm_test_scmi_sandbox_agent, UTF_SCAN_FDT);
Etienne Carriere87d4f272020-09-09 18:44:05 +020099
AKASHI Takahiro93ecae92023-10-11 19:07:06 +0900100static int dm_test_scmi_base(struct unit_test_state *uts)
101{
102 struct udevice *agent_dev, *base;
103 struct scmi_agent_priv *priv;
104 u32 version, num_agents, num_protocols, impl_version;
105 u32 attributes, agent_id;
106 u8 *vendor, *agent_name, *protocols;
107 int ret;
108
109 /* preparation */
110 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
111 &agent_dev));
112 ut_assertnonnull(agent_dev);
113 ut_assertnonnull(priv = dev_get_uclass_plat(agent_dev));
114 ut_assertnonnull(base = scmi_get_protocol(agent_dev,
115 SCMI_PROTOCOL_ID_BASE));
116
117 /* version */
118 ret = scmi_base_protocol_version(base, &version);
119 ut_assertok(ret);
120 ut_asserteq(priv->version, version);
121
122 /* protocol attributes */
123 ret = scmi_base_protocol_attrs(base, &num_agents, &num_protocols);
124 ut_assertok(ret);
125 ut_asserteq(priv->num_agents, num_agents);
126 ut_asserteq(priv->num_protocols, num_protocols);
127
128 /* discover vendor */
129 ret = scmi_base_discover_vendor(base, &vendor);
130 ut_assertok(ret);
131 ut_asserteq_str(priv->vendor, vendor);
132 free(vendor);
133
134 /* message attributes */
135 ret = scmi_base_protocol_message_attrs(base,
136 SCMI_BASE_DISCOVER_SUB_VENDOR,
137 &attributes);
138 ut_assertok(ret);
139 ut_assertok(attributes);
140
141 /* discover sub vendor */
142 ret = scmi_base_discover_sub_vendor(base, &vendor);
143 ut_assertok(ret);
144 ut_asserteq_str(priv->sub_vendor, vendor);
145 free(vendor);
146
147 /* impl version */
148 ret = scmi_base_discover_impl_version(base, &impl_version);
149 ut_assertok(ret);
150 ut_asserteq(priv->impl_version, impl_version);
151
152 /* discover agent (my self) */
153 ret = scmi_base_discover_agent(base, 0xffffffff, &agent_id,
154 &agent_name);
155 ut_assertok(ret);
156 ut_asserteq(priv->agent_id, agent_id);
157 ut_asserteq_str(priv->agent_name, agent_name);
158 free(agent_name);
159
160 /* discover protocols */
161 ret = scmi_base_discover_list_protocols(base, &protocols);
162 ut_asserteq(num_protocols, ret);
163 ut_asserteq_mem(priv->protocols, protocols, sizeof(u8) * num_protocols);
164 free(protocols);
165
166 /*
167 * NOTE: Sandbox SCMI driver handles device-0 only. It supports setting
168 * access and protocol permissions, but doesn't allow unsetting them nor
169 * resetting the configurations.
170 */
171 /* set device permissions */
172 ret = scmi_base_set_device_permissions(base, agent_id, 0,
173 SCMI_BASE_SET_DEVICE_PERMISSIONS_ACCESS);
174 ut_assertok(ret); /* SCMI_SUCCESS */
175 ret = scmi_base_set_device_permissions(base, agent_id, 1,
176 SCMI_BASE_SET_DEVICE_PERMISSIONS_ACCESS);
177 ut_asserteq(-ENOENT, ret); /* SCMI_NOT_FOUND */
178 ret = scmi_base_set_device_permissions(base, agent_id, 0, 0);
179 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
180
181 /* set protocol permissions */
182 ret = scmi_base_set_protocol_permissions(base, agent_id, 0,
183 SCMI_PROTOCOL_ID_CLOCK,
184 SCMI_BASE_SET_PROTOCOL_PERMISSIONS_ACCESS);
185 ut_assertok(ret); /* SCMI_SUCCESS */
186 ret = scmi_base_set_protocol_permissions(base, agent_id, 1,
187 SCMI_PROTOCOL_ID_CLOCK,
188 SCMI_BASE_SET_PROTOCOL_PERMISSIONS_ACCESS);
189 ut_asserteq(-ENOENT, ret); /* SCMI_NOT_FOUND */
190 ret = scmi_base_set_protocol_permissions(base, agent_id, 0,
191 SCMI_PROTOCOL_ID_CLOCK, 0);
192 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
193
194 /* reset agent configuration */
195 ret = scmi_base_reset_agent_configuration(base, agent_id, 0);
196 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
197 ret = scmi_base_reset_agent_configuration(base, agent_id,
198 SCMI_BASE_RESET_ALL_ACCESS_PERMISSIONS);
199 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
200 ret = scmi_base_reset_agent_configuration(base, agent_id, 0);
201 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
202
203 return 0;
204}
Simon Glass725c4382024-08-22 07:57:48 -0600205DM_TEST(dm_test_scmi_base, UTF_SCAN_FDT);
AKASHI Takahiro93ecae92023-10-11 19:07:06 +0900206
AKASHI Takahiro3d6fe582023-11-14 11:14:28 +0900207static int dm_test_scmi_cmd(struct unit_test_state *uts)
208{
209 struct udevice *agent_dev;
210 int num_proto = 0;
211 char cmd_out[30];
212
213 if (!CONFIG_IS_ENABLED(CMD_SCMI))
214 return -EAGAIN;
215
216 /* preparation */
217 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
218 &agent_dev));
219 ut_assertnonnull(agent_dev);
220
221 /*
222 * Estimate the number of provided protocols.
223 * This estimation is correct as far as a corresponding
224 * protocol support is added to sandbox fake serer.
225 */
226 if (CONFIG_IS_ENABLED(POWER_DOMAIN))
227 num_proto++;
228 if (CONFIG_IS_ENABLED(CLK_SCMI))
229 num_proto++;
230 if (CONFIG_IS_ENABLED(RESET_SCMI))
231 num_proto++;
232 if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
233 num_proto++;
234
235 /* scmi info */
236 ut_assertok(run_command("scmi info", 0));
237
238 ut_assert_nextline("SCMI device: scmi");
239 snprintf(cmd_out, 30, " protocol version: 0x%x",
240 SCMI_BASE_PROTOCOL_VERSION);
241 ut_assert_nextline(cmd_out);
242 ut_assert_nextline(" # of agents: 2");
243 ut_assert_nextline(" 0: platform");
244 ut_assert_nextline(" > 1: OSPM");
245 snprintf(cmd_out, 30, " # of protocols: %d", num_proto);
246 ut_assert_nextline(cmd_out);
247 if (CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN))
248 ut_assert_nextline(" Power domain management");
249 if (CONFIG_IS_ENABLED(CLK_SCMI))
250 ut_assert_nextline(" Clock management");
251 if (CONFIG_IS_ENABLED(RESET_SCMI))
252 ut_assert_nextline(" Reset domain management");
253 if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
254 ut_assert_nextline(" Voltage domain management");
255 ut_assert_nextline(" vendor: U-Boot");
256 ut_assert_nextline(" sub vendor: Sandbox");
257 ut_assert_nextline(" impl version: 0x1");
258
259 ut_assert_console_end();
260
261 /* scmi perm_dev */
262 ut_assertok(run_command("scmi perm_dev 1 0 1", 0));
263 ut_assert_console_end();
264
265 ut_assert(run_command("scmi perm_dev 1 0 0", 0));
266 ut_assert_nextline("Denying access to device:0 failed (-13)");
267 ut_assert_console_end();
268
269 /* scmi perm_proto */
270 ut_assertok(run_command("scmi perm_proto 1 0 14 1", 0));
271 ut_assert_console_end();
272
273 ut_assert(run_command("scmi perm_proto 1 0 14 0", 0));
274 ut_assert_nextline("Denying access to protocol:0x14 on device:0 failed (-13)");
275 ut_assert_console_end();
276
277 /* scmi reset */
278 ut_assert(run_command("scmi reset 1 1", 0));
279 ut_assert_nextline("Reset failed (-13)");
280 ut_assert_console_end();
281
282 return 0;
283}
Simon Glass675fde82024-08-22 07:57:59 -0600284DM_TEST(dm_test_scmi_cmd, UTF_SCAN_FDT | UTF_CONSOLE);
AKASHI Takahiro3d6fe582023-11-14 11:14:28 +0900285
AKASHI Takahiro2ba0f822023-10-16 14:39:46 +0900286static int dm_test_scmi_power_domains(struct unit_test_state *uts)
287{
288 struct sandbox_scmi_agent *agent;
289 struct sandbox_scmi_devices *scmi_devices;
290 struct udevice *agent_dev, *pwd, *dev;
291 u32 version, count, attributes, pstate;
292 u64 stats_addr;
293 size_t stats_len;
294 u8 *name;
295 int ret;
296
AKASHI Takahiro63cd0dc2023-11-14 11:14:24 +0900297 if (!CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN))
298 return -EAGAIN;
299
AKASHI Takahiro2ba0f822023-10-16 14:39:46 +0900300 /* preparation */
301 ut_assertok(load_sandbox_scmi_test_devices(uts, &agent, &dev));
302 ut_assertnonnull(agent);
303 scmi_devices = sandbox_scmi_devices_ctx(dev);
304 ut_assertnonnull(scmi_devices);
305 ut_asserteq(2, scmi_devices->pwdom->id); /* in test.dts */
306
307 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
308 &agent_dev));
309 ut_assertnonnull(agent_dev);
310 pwd = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_POWER_DOMAIN);
311 ut_assertnonnull(pwd);
312
313 /*
314 * SCMI Power domain management protocol interfaces
315 */
316 /* version */
317 ret = scmi_generic_protocol_version(pwd, SCMI_PROTOCOL_ID_POWER_DOMAIN,
318 &version);
319 ut_assertok(ret);
320 ut_asserteq(agent->pwdom_version, version);
321
322 /* protocol attributes */
323 ret = scmi_pwd_protocol_attrs(pwd, &count, &stats_addr, &stats_len);
324 ut_assertok(ret);
325 ut_asserteq(agent->pwdom_count, count);
326 ut_asserteq(0, stats_len);
327
328 /* protocol message attributes */
329 ret = scmi_pwd_protocol_message_attrs(pwd, SCMI_PWD_STATE_SET,
330 &attributes);
331 ut_assertok(ret);
332 ret = scmi_pwd_protocol_message_attrs(pwd, SCMI_PWD_STATE_NOTIFY,
333 &attributes);
334 ut_asserteq(-ENOENT, ret); /* the protocol not supported */
335
336 /* power domain attributes */
337 ret = scmi_pwd_attrs(pwd, 0, &attributes, &name);
338 ut_assertok(ret);
339 ut_asserteq_str("power-domain--0", name);
340 free(name);
341
342 ret = scmi_pwd_attrs(pwd, 10, &attributes, &name);
343 ut_asserteq(-ENOENT, ret); /* domain-10 doesn't exist */
344
345 /* power domain state set/get */
346 ret = scmi_pwd_state_set(pwd, 0, 0, 0);
347 ut_assertok(ret);
348 ret = scmi_pwd_state_get(pwd, 0, &pstate);
349 ut_assertok(ret);
350 ut_asserteq(0, pstate); /* ON */
351
352 ret = scmi_pwd_state_set(pwd, 0, 0, SCMI_PWD_PSTATE_TYPE_LOST);
353 ut_assertok(ret);
354 ret = scmi_pwd_state_get(pwd, 0, &pstate);
355 ut_assertok(ret);
356 ut_asserteq(SCMI_PWD_PSTATE_TYPE_LOST, pstate); /* OFF */
357
358 ret = scmi_pwd_state_set(pwd, 0, 10, 0);
359 ut_asserteq(-ENOENT, ret);
360
361 /* power domain name get */
362 ret = scmi_pwd_name_get(pwd, 0, &name);
363 ut_assertok(ret);
364 ut_asserteq_str("power-domain--0-extended", name);
365 free(name);
366
367 ret = scmi_pwd_name_get(pwd, 10, &name);
368 ut_asserteq(-ENOENT, ret); /* domain-10 doesn't exist */
369
370 /*
371 * U-Boot driver model interfaces
372 */
373 /* power_domain_on */
374 ret = power_domain_on(scmi_devices->pwdom);
375 ut_assertok(ret);
376 ret = scmi_pwd_state_get(pwd, scmi_devices->pwdom->id, &pstate);
377 ut_assertok(ret);
378 ut_asserteq(0, pstate); /* ON */
379
380 /* power_domain_off */
381 ret = power_domain_off(scmi_devices->pwdom);
382 ut_assertok(ret);
383 ret = scmi_pwd_state_get(pwd, scmi_devices->pwdom->id, &pstate);
384 ut_assertok(ret);
385 ut_asserteq(SCMI_PWD_PSTATE_TYPE_LOST, pstate); /* OFF */
386
387 return release_sandbox_scmi_test_devices(uts, dev);
388}
Simon Glass725c4382024-08-22 07:57:48 -0600389DM_TEST(dm_test_scmi_power_domains, UTF_SCAN_FDT);
AKASHI Takahiro2ba0f822023-10-16 14:39:46 +0900390
Etienne Carriere87d4f272020-09-09 18:44:05 +0200391static int dm_test_scmi_clocks(struct unit_test_state *uts)
392{
Etienne Carriere41d62e22022-02-21 09:22:39 +0100393 struct sandbox_scmi_agent *agent;
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +0900394 struct sandbox_scmi_devices *scmi_devices;
AKASHI Takahiroa89d9f42023-10-11 19:06:59 +0900395 struct udevice *agent_dev, *clock_dev, *dev;
Etienne Carriere87d4f272020-09-09 18:44:05 +0200396 int ret_dev;
397 int ret;
398
AKASHI Takahiro63cd0dc2023-11-14 11:14:24 +0900399 if (!CONFIG_IS_ENABLED(CLK_SCMI))
400 return -EAGAIN;
401
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +0900402 ret = load_sandbox_scmi_test_devices(uts, &agent, &dev);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200403 if (ret)
404 return ret;
405
406 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere41d62e22022-02-21 09:22:39 +0100407 ut_assertnonnull(scmi_devices);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200408
AKASHI Takahiroa89d9f42023-10-11 19:06:59 +0900409 /* Sandbox SCMI clock protocol has its own channel */
410 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
411 &agent_dev));
412 ut_assertnonnull(agent_dev);
413 clock_dev = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_CLOCK);
414 ut_assertnonnull(clock_dev);
415 ut_asserteq(0x14, sandbox_scmi_channel_id(clock_dev));
416
Etienne Carriere87d4f272020-09-09 18:44:05 +0200417 /* Test SCMI clocks rate manipulation */
Etienne Carriere10d3e5d2022-02-21 09:22:41 +0100418 ut_asserteq(333, agent->clk[0].rate);
419 ut_asserteq(200, agent->clk[1].rate);
420 ut_asserteq(1000, agent->clk[2].rate);
421
Etienne Carriere87d4f272020-09-09 18:44:05 +0200422 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
423 ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1]));
Etienne Carriere87d4f272020-09-09 18:44:05 +0200424
425 ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088);
426 ut_assert(!ret_dev || ret_dev == 1088);
427
Etienne Carriere10d3e5d2022-02-21 09:22:41 +0100428 ut_asserteq(1088, agent->clk[0].rate);
429 ut_asserteq(200, agent->clk[1].rate);
430 ut_asserteq(1000, agent->clk[2].rate);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200431
432 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
433 ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1]));
Etienne Carriere87d4f272020-09-09 18:44:05 +0200434
435 /* restore original rate for further tests */
436 ret_dev = clk_set_rate(&scmi_devices->clk[1], 333);
437 ut_assert(!ret_dev || ret_dev == 333);
438
439 /* Test SCMI clocks gating manipulation */
Etienne Carriere41d62e22022-02-21 09:22:39 +0100440 ut_assert(!agent->clk[0].enabled);
441 ut_assert(!agent->clk[1].enabled);
442 ut_assert(!agent->clk[2].enabled);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200443
444 ut_asserteq(0, clk_enable(&scmi_devices->clk[1]));
Etienne Carriere87d4f272020-09-09 18:44:05 +0200445
Etienne Carriere10d3e5d2022-02-21 09:22:41 +0100446 ut_assert(agent->clk[0].enabled);
447 ut_assert(!agent->clk[1].enabled);
Etienne Carriere41d62e22022-02-21 09:22:39 +0100448 ut_assert(!agent->clk[2].enabled);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200449
450 ut_assertok(clk_disable(&scmi_devices->clk[1]));
Etienne Carriere87d4f272020-09-09 18:44:05 +0200451
Etienne Carriere41d62e22022-02-21 09:22:39 +0100452 ut_assert(!agent->clk[0].enabled);
453 ut_assert(!agent->clk[1].enabled);
454 ut_assert(!agent->clk[2].enabled);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200455
456 return release_sandbox_scmi_test_devices(uts, dev);
457}
Simon Glass725c4382024-08-22 07:57:48 -0600458DM_TEST(dm_test_scmi_clocks, UTF_SCAN_FDT);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200459
460static int dm_test_scmi_resets(struct unit_test_state *uts)
461{
Etienne Carriere41d62e22022-02-21 09:22:39 +0100462 struct sandbox_scmi_agent *agent;
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +0900463 struct sandbox_scmi_devices *scmi_devices;
AKASHI Takahiroa89d9f42023-10-11 19:06:59 +0900464 struct udevice *agent_dev, *reset_dev, *dev = NULL;
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200465 int ret;
466
AKASHI Takahiro63cd0dc2023-11-14 11:14:24 +0900467 if (!CONFIG_IS_ENABLED(RESET_SCMI))
468 return -EAGAIN;
469
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +0900470 ret = load_sandbox_scmi_test_devices(uts, &agent, &dev);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200471 if (ret)
472 return ret;
473
474 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere41d62e22022-02-21 09:22:39 +0100475 ut_assertnonnull(scmi_devices);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200476
AKASHI Takahiroa89d9f42023-10-11 19:06:59 +0900477 /* Sandbox SCMI reset protocol doesn't have its own channel */
478 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
479 &agent_dev));
480 ut_assertnonnull(agent_dev);
481 reset_dev = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_RESET_DOMAIN);
482 ut_assertnonnull(reset_dev);
483 ut_asserteq(0x0, sandbox_scmi_channel_id(reset_dev));
484
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200485 /* Test SCMI resect controller manipulation */
Marek Vasutfa847bb2023-03-10 04:33:13 +0100486 ut_assert(!agent->reset[0].asserted);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200487
488 ut_assertok(reset_assert(&scmi_devices->reset[0]));
Marek Vasutfa847bb2023-03-10 04:33:13 +0100489 ut_assert(agent->reset[0].asserted);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200490
491 ut_assertok(reset_deassert(&scmi_devices->reset[0]));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100492 ut_assert(!agent->reset[0].asserted);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200493
494 return release_sandbox_scmi_test_devices(uts, dev);
495}
Simon Glass725c4382024-08-22 07:57:48 -0600496DM_TEST(dm_test_scmi_resets, UTF_SCAN_FDT);
Etienne Carriere01242182021-03-08 22:38:07 +0100497
498static int dm_test_scmi_voltage_domains(struct unit_test_state *uts)
499{
Etienne Carriere41d62e22022-02-21 09:22:39 +0100500 struct sandbox_scmi_agent *agent;
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +0900501 struct sandbox_scmi_devices *scmi_devices;
Etienne Carriere01242182021-03-08 22:38:07 +0100502 struct dm_regulator_uclass_plat *uc_pdata;
503 struct udevice *dev;
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100504 struct udevice *regul0_dev;
Etienne Carriere01242182021-03-08 22:38:07 +0100505
AKASHI Takahiro63cd0dc2023-11-14 11:14:24 +0900506 if (!CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
507 return -EAGAIN;
508
AKASHI Takahirobddcd3a2023-10-11 19:07:03 +0900509 ut_assertok(load_sandbox_scmi_test_devices(uts, &agent, &dev));
Etienne Carriere01242182021-03-08 22:38:07 +0100510
511 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere41d62e22022-02-21 09:22:39 +0100512 ut_assertnonnull(scmi_devices);
Etienne Carriere01242182021-03-08 22:38:07 +0100513
514 /* Set/Get an SCMI voltage domain level */
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100515 regul0_dev = scmi_devices->regul[0];
516 ut_assert(regul0_dev);
Etienne Carriere01242182021-03-08 22:38:07 +0100517
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100518 uc_pdata = dev_get_uclass_plat(regul0_dev);
Etienne Carriere01242182021-03-08 22:38:07 +0100519 ut_assert(uc_pdata);
520
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100521 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->min_uV));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100522 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->min_uV);
Etienne Carriere01242182021-03-08 22:38:07 +0100523
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100524 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->min_uV);
Etienne Carriere01242182021-03-08 22:38:07 +0100525
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100526 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->max_uV));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100527 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->max_uV);
Etienne Carriere01242182021-03-08 22:38:07 +0100528
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100529 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->max_uV);
Etienne Carriere01242182021-03-08 22:38:07 +0100530
531 /* Enable/disable SCMI voltage domains */
532 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
533 ut_assertok(regulator_set_enable(scmi_devices->regul[1], false));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100534 ut_assert(!agent->voltd[0].enabled);
535 ut_assert(!agent->voltd[1].enabled);
Etienne Carriere01242182021-03-08 22:38:07 +0100536
537 ut_assertok(regulator_set_enable(scmi_devices->regul[0], true));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100538 ut_assert(agent->voltd[0].enabled);
539 ut_assert(!agent->voltd[1].enabled);
Etienne Carriere01242182021-03-08 22:38:07 +0100540
541 ut_assertok(regulator_set_enable(scmi_devices->regul[1], true));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100542 ut_assert(agent->voltd[0].enabled);
543 ut_assert(agent->voltd[1].enabled);
Etienne Carriere01242182021-03-08 22:38:07 +0100544
545 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100546 ut_assert(!agent->voltd[0].enabled);
547 ut_assert(agent->voltd[1].enabled);
Etienne Carriere01242182021-03-08 22:38:07 +0100548
549 return release_sandbox_scmi_test_devices(uts, dev);
550}
Simon Glass725c4382024-08-22 07:57:48 -0600551DM_TEST(dm_test_scmi_voltage_domains, UTF_SCAN_FDT);