blob: 8db3ad32f85ee4683f368d3e81333ab61e97e4a2 [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>
20#include <scmi_protocols.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +020021#include <asm/scmi_test.h>
22#include <dm/device-internal.h>
23#include <dm/test.h>
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020024#include <linux/kconfig.h>
Etienne Carriereb8f15cd2021-03-08 22:38:07 +010025#include <power/regulator.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +020026#include <test/ut.h>
27
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020028static int ut_assert_scmi_state_preprobe(struct unit_test_state *uts)
29{
30 struct sandbox_scmi_service *scmi_ctx = sandbox_scmi_service_ctx();
31
32 ut_assertnonnull(scmi_ctx);
Etienne Carriere09665cb2022-02-21 09:22:39 +010033 ut_assertnull(scmi_ctx->agent);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020034
35 return 0;
36}
37
38static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts,
39 struct udevice *dev)
40{
41 struct sandbox_scmi_devices *scmi_devices;
42 struct sandbox_scmi_service *scmi_ctx;
Etienne Carriere09665cb2022-02-21 09:22:39 +010043 struct sandbox_scmi_agent *agent;
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020044
45 /* Device references to check context against test sequence */
46 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020047 ut_assertnonnull(scmi_devices);
Etienne Carriere09665cb2022-02-21 09:22:39 +010048 ut_asserteq(2, scmi_devices->clk_count);
Etienne Carrierec83a8432021-03-08 22:38:08 +010049 ut_asserteq(1, scmi_devices->reset_count);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +010050 ut_asserteq(2, scmi_devices->regul_count);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020051
52 /* State of the simulated SCMI server exposed */
53 scmi_ctx = sandbox_scmi_service_ctx();
Etienne Carriere09665cb2022-02-21 09:22:39 +010054 ut_assertnonnull(scmi_ctx);
55 agent = scmi_ctx->agent;
56 ut_assertnonnull(agent);
Etienne Carrierebf1f1322022-02-21 09:22:41 +010057 ut_asserteq(3, agent->clk_count);
Etienne Carriere09665cb2022-02-21 09:22:39 +010058 ut_assertnonnull(agent->clk);
59 ut_asserteq(1, agent->reset_count);
60 ut_assertnonnull(agent->reset);
61 ut_asserteq(2, agent->voltd_count);
62 ut_assertnonnull(agent->voltd);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020063
64 return 0;
65}
66
67static int load_sandbox_scmi_test_devices(struct unit_test_state *uts,
68 struct udevice **dev)
69{
70 int ret;
71
72 ret = ut_assert_scmi_state_preprobe(uts);
73 if (ret)
74 return ret;
75
76 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_scmi",
77 dev));
78 ut_assertnonnull(*dev);
79
80 return ut_assert_scmi_state_postprobe(uts, *dev);
81}
82
83static int release_sandbox_scmi_test_devices(struct unit_test_state *uts,
84 struct udevice *dev)
85{
86 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
87
88 /* Not sure test devices are fully removed, agent may not be visible */
89 return 0;
90}
91
Etienne Carriere02fd1262020-09-09 18:44:00 +020092/*
93 * Test SCMI states when loading and releasing resources
94 * related to SCMI drivers.
95 */
96static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts)
97{
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020098 struct udevice *dev = NULL;
99 int ret;
Etienne Carriere02fd1262020-09-09 18:44:00 +0200100
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200101 ret = load_sandbox_scmi_test_devices(uts, &dev);
102 if (!ret)
103 ret = release_sandbox_scmi_test_devices(uts, dev);
Etienne Carriere02fd1262020-09-09 18:44:00 +0200104
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200105 return ret;
Etienne Carriere02fd1262020-09-09 18:44:00 +0200106}
Etienne Carriere02fd1262020-09-09 18:44:00 +0200107DM_TEST(dm_test_scmi_sandbox_agent, UT_TESTF_SCAN_FDT);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200108
109static int dm_test_scmi_clocks(struct unit_test_state *uts)
110{
111 struct sandbox_scmi_devices *scmi_devices;
112 struct sandbox_scmi_service *scmi_ctx;
Etienne Carriere09665cb2022-02-21 09:22:39 +0100113 struct sandbox_scmi_agent *agent;
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +0900114 struct udevice *agent_dev, *clock_dev, *dev;
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200115 int ret_dev;
116 int ret;
117
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200118 ret = load_sandbox_scmi_test_devices(uts, &dev);
119 if (ret)
120 return ret;
121
122 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere09665cb2022-02-21 09:22:39 +0100123 ut_assertnonnull(scmi_devices);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200124 scmi_ctx = sandbox_scmi_service_ctx();
Etienne Carriere09665cb2022-02-21 09:22:39 +0100125 ut_assertnonnull(scmi_ctx);
126 agent = scmi_ctx->agent;
127 ut_assertnonnull(agent);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200128
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +0900129 /* Sandbox SCMI clock protocol has its own channel */
130 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
131 &agent_dev));
132 ut_assertnonnull(agent_dev);
133 clock_dev = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_CLOCK);
134 ut_assertnonnull(clock_dev);
135 ut_asserteq(0x14, sandbox_scmi_channel_id(clock_dev));
136
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200137 /* Test SCMI clocks rate manipulation */
Etienne Carrierebf1f1322022-02-21 09:22:41 +0100138 ut_asserteq(333, agent->clk[0].rate);
139 ut_asserteq(200, agent->clk[1].rate);
140 ut_asserteq(1000, agent->clk[2].rate);
141
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200142 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
143 ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1]));
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200144
145 ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088);
146 ut_assert(!ret_dev || ret_dev == 1088);
147
Etienne Carrierebf1f1322022-02-21 09:22:41 +0100148 ut_asserteq(1088, agent->clk[0].rate);
149 ut_asserteq(200, agent->clk[1].rate);
150 ut_asserteq(1000, agent->clk[2].rate);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200151
152 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
153 ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1]));
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200154
155 /* restore original rate for further tests */
156 ret_dev = clk_set_rate(&scmi_devices->clk[1], 333);
157 ut_assert(!ret_dev || ret_dev == 333);
158
159 /* Test SCMI clocks gating manipulation */
Etienne Carriere09665cb2022-02-21 09:22:39 +0100160 ut_assert(!agent->clk[0].enabled);
161 ut_assert(!agent->clk[1].enabled);
162 ut_assert(!agent->clk[2].enabled);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200163
164 ut_asserteq(0, clk_enable(&scmi_devices->clk[1]));
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200165
Etienne Carrierebf1f1322022-02-21 09:22:41 +0100166 ut_assert(agent->clk[0].enabled);
167 ut_assert(!agent->clk[1].enabled);
Etienne Carriere09665cb2022-02-21 09:22:39 +0100168 ut_assert(!agent->clk[2].enabled);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200169
170 ut_assertok(clk_disable(&scmi_devices->clk[1]));
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200171
Etienne Carriere09665cb2022-02-21 09:22:39 +0100172 ut_assert(!agent->clk[0].enabled);
173 ut_assert(!agent->clk[1].enabled);
174 ut_assert(!agent->clk[2].enabled);
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200175
176 return release_sandbox_scmi_test_devices(uts, dev);
177}
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200178DM_TEST(dm_test_scmi_clocks, UT_TESTF_SCAN_FDT);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200179
180static int dm_test_scmi_resets(struct unit_test_state *uts)
181{
182 struct sandbox_scmi_devices *scmi_devices;
183 struct sandbox_scmi_service *scmi_ctx;
Etienne Carriere09665cb2022-02-21 09:22:39 +0100184 struct sandbox_scmi_agent *agent;
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +0900185 struct udevice *agent_dev, *reset_dev, *dev = NULL;
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200186 int ret;
187
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200188 ret = load_sandbox_scmi_test_devices(uts, &dev);
189 if (ret)
190 return ret;
191
192 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere09665cb2022-02-21 09:22:39 +0100193 ut_assertnonnull(scmi_devices);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200194 scmi_ctx = sandbox_scmi_service_ctx();
Etienne Carriere09665cb2022-02-21 09:22:39 +0100195 ut_assertnonnull(scmi_ctx);
196 agent = scmi_ctx->agent;
197 ut_assertnonnull(agent);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200198
AKASHI Takahirocc4ecda2023-10-11 19:06:59 +0900199 /* Sandbox SCMI reset protocol doesn't have its own channel */
200 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
201 &agent_dev));
202 ut_assertnonnull(agent_dev);
203 reset_dev = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_RESET_DOMAIN);
204 ut_assertnonnull(reset_dev);
205 ut_asserteq(0x0, sandbox_scmi_channel_id(reset_dev));
206
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200207 /* Test SCMI resect controller manipulation */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100208 ut_assert(!agent->reset[0].asserted);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200209
210 ut_assertok(reset_assert(&scmi_devices->reset[0]));
Marek Vasut1531c4e2023-03-10 04:33:13 +0100211 ut_assert(agent->reset[0].asserted);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200212
213 ut_assertok(reset_deassert(&scmi_devices->reset[0]));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100214 ut_assert(!agent->reset[0].asserted);
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200215
216 return release_sandbox_scmi_test_devices(uts, dev);
217}
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200218DM_TEST(dm_test_scmi_resets, UT_TESTF_SCAN_FDT);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100219
220static int dm_test_scmi_voltage_domains(struct unit_test_state *uts)
221{
222 struct sandbox_scmi_devices *scmi_devices;
223 struct sandbox_scmi_service *scmi_ctx;
Etienne Carriere09665cb2022-02-21 09:22:39 +0100224 struct sandbox_scmi_agent *agent;
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100225 struct dm_regulator_uclass_plat *uc_pdata;
226 struct udevice *dev;
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100227 struct udevice *regul0_dev;
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100228
229 ut_assertok(load_sandbox_scmi_test_devices(uts, &dev));
230
231 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere09665cb2022-02-21 09:22:39 +0100232 ut_assertnonnull(scmi_devices);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100233 scmi_ctx = sandbox_scmi_service_ctx();
Etienne Carriere09665cb2022-02-21 09:22:39 +0100234 ut_assertnonnull(scmi_ctx);
235 agent = scmi_ctx->agent;
236 ut_assertnonnull(agent);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100237
238 /* Set/Get an SCMI voltage domain level */
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100239 regul0_dev = scmi_devices->regul[0];
240 ut_assert(regul0_dev);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100241
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100242 uc_pdata = dev_get_uclass_plat(regul0_dev);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100243 ut_assert(uc_pdata);
244
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100245 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->min_uV));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100246 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->min_uV);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100247
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100248 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->min_uV);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100249
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100250 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->max_uV));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100251 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->max_uV);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100252
Etienne Carriereeeaf2822021-03-08 22:38:09 +0100253 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->max_uV);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100254
255 /* Enable/disable SCMI voltage domains */
256 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
257 ut_assertok(regulator_set_enable(scmi_devices->regul[1], false));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100258 ut_assert(!agent->voltd[0].enabled);
259 ut_assert(!agent->voltd[1].enabled);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100260
261 ut_assertok(regulator_set_enable(scmi_devices->regul[0], true));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100262 ut_assert(agent->voltd[0].enabled);
263 ut_assert(!agent->voltd[1].enabled);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100264
265 ut_assertok(regulator_set_enable(scmi_devices->regul[1], true));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100266 ut_assert(agent->voltd[0].enabled);
267 ut_assert(agent->voltd[1].enabled);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100268
269 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
Etienne Carriere09665cb2022-02-21 09:22:39 +0100270 ut_assert(!agent->voltd[0].enabled);
271 ut_assert(agent->voltd[1].enabled);
Etienne Carriereb8f15cd2021-03-08 22:38:07 +0100272
273 return release_sandbox_scmi_test_devices(uts, dev);
274}
275DM_TEST(dm_test_scmi_voltage_domains, UT_TESTF_SCAN_FDT);