blob: 795f207304a840a3b8a5e00d77d20d3a739a165d [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
Etienne Carriere358599e2020-09-09 18:44:00 +02009 * 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.
13 */
14
15#include <common.h>
Etienne Carriere87d4f272020-09-09 18:44:05 +020016#include <clk.h>
Etienne Carriere358599e2020-09-09 18:44:00 +020017#include <dm.h>
Etienne Carrierec0dd1772020-09-09 18:44:07 +020018#include <reset.h>
Etienne Carriere358599e2020-09-09 18:44:00 +020019#include <asm/scmi_test.h>
20#include <dm/device-internal.h>
21#include <dm/test.h>
Etienne Carriere87d4f272020-09-09 18:44:05 +020022#include <linux/kconfig.h>
Etienne Carriere01242182021-03-08 22:38:07 +010023#include <power/regulator.h>
Etienne Carriere358599e2020-09-09 18:44:00 +020024#include <test/ut.h>
25
Etienne Carriere87d4f272020-09-09 18:44:05 +020026static int ut_assert_scmi_state_preprobe(struct unit_test_state *uts)
27{
28 struct sandbox_scmi_service *scmi_ctx = sandbox_scmi_service_ctx();
29
30 ut_assertnonnull(scmi_ctx);
Etienne Carriere41d62e22022-02-21 09:22:39 +010031 ut_assertnull(scmi_ctx->agent);
Etienne Carriere87d4f272020-09-09 18:44:05 +020032
33 return 0;
34}
35
36static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts,
37 struct udevice *dev)
38{
39 struct sandbox_scmi_devices *scmi_devices;
40 struct sandbox_scmi_service *scmi_ctx;
Etienne Carriere41d62e22022-02-21 09:22:39 +010041 struct sandbox_scmi_agent *agent;
Etienne Carriere87d4f272020-09-09 18:44:05 +020042
43 /* Device references to check context against test sequence */
44 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere87d4f272020-09-09 18:44:05 +020045 ut_assertnonnull(scmi_devices);
Etienne Carriere41d62e22022-02-21 09:22:39 +010046 ut_asserteq(2, scmi_devices->clk_count);
Etienne Carrierec3bba702021-03-08 22:38:08 +010047 ut_asserteq(1, scmi_devices->reset_count);
Etienne Carriere01242182021-03-08 22:38:07 +010048 ut_asserteq(2, scmi_devices->regul_count);
Etienne Carriere87d4f272020-09-09 18:44:05 +020049
50 /* State of the simulated SCMI server exposed */
51 scmi_ctx = sandbox_scmi_service_ctx();
Etienne Carriere41d62e22022-02-21 09:22:39 +010052 ut_assertnonnull(scmi_ctx);
53 agent = scmi_ctx->agent;
54 ut_assertnonnull(agent);
Etienne Carriere10d3e5d2022-02-21 09:22:41 +010055 ut_asserteq(3, agent->clk_count);
Etienne Carriere41d62e22022-02-21 09:22:39 +010056 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);
Etienne Carriere87d4f272020-09-09 18:44:05 +020061
62 return 0;
63}
64
65static int load_sandbox_scmi_test_devices(struct unit_test_state *uts,
66 struct udevice **dev)
67{
68 int ret;
69
70 ret = ut_assert_scmi_state_preprobe(uts);
71 if (ret)
72 return ret;
73
74 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_scmi",
75 dev));
76 ut_assertnonnull(*dev);
77
78 return ut_assert_scmi_state_postprobe(uts, *dev);
79}
80
81static int release_sandbox_scmi_test_devices(struct unit_test_state *uts,
82 struct udevice *dev)
83{
84 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
85
86 /* Not sure test devices are fully removed, agent may not be visible */
87 return 0;
88}
89
Etienne Carriere358599e2020-09-09 18:44:00 +020090/*
91 * Test SCMI states when loading and releasing resources
92 * related to SCMI drivers.
93 */
94static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts)
95{
Etienne Carriere87d4f272020-09-09 18:44:05 +020096 struct udevice *dev = NULL;
97 int ret;
Etienne Carriere358599e2020-09-09 18:44:00 +020098
Etienne Carriere87d4f272020-09-09 18:44:05 +020099 ret = load_sandbox_scmi_test_devices(uts, &dev);
100 if (!ret)
101 ret = release_sandbox_scmi_test_devices(uts, dev);
Etienne Carriere358599e2020-09-09 18:44:00 +0200102
Etienne Carriere87d4f272020-09-09 18:44:05 +0200103 return ret;
Etienne Carriere358599e2020-09-09 18:44:00 +0200104}
Etienne Carriere358599e2020-09-09 18:44:00 +0200105DM_TEST(dm_test_scmi_sandbox_agent, UT_TESTF_SCAN_FDT);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200106
107static int dm_test_scmi_clocks(struct unit_test_state *uts)
108{
109 struct sandbox_scmi_devices *scmi_devices;
110 struct sandbox_scmi_service *scmi_ctx;
Etienne Carriere41d62e22022-02-21 09:22:39 +0100111 struct sandbox_scmi_agent *agent;
112 struct udevice *dev;
Etienne Carriere87d4f272020-09-09 18:44:05 +0200113 int ret_dev;
114 int ret;
115
Etienne Carriere87d4f272020-09-09 18:44:05 +0200116 ret = load_sandbox_scmi_test_devices(uts, &dev);
117 if (ret)
118 return ret;
119
120 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere41d62e22022-02-21 09:22:39 +0100121 ut_assertnonnull(scmi_devices);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200122 scmi_ctx = sandbox_scmi_service_ctx();
Etienne Carriere41d62e22022-02-21 09:22:39 +0100123 ut_assertnonnull(scmi_ctx);
124 agent = scmi_ctx->agent;
125 ut_assertnonnull(agent);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200126
127 /* Test SCMI clocks rate manipulation */
Etienne Carriere10d3e5d2022-02-21 09:22:41 +0100128 ut_asserteq(333, agent->clk[0].rate);
129 ut_asserteq(200, agent->clk[1].rate);
130 ut_asserteq(1000, agent->clk[2].rate);
131
Etienne Carriere87d4f272020-09-09 18:44:05 +0200132 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
133 ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1]));
Etienne Carriere87d4f272020-09-09 18:44:05 +0200134
135 ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088);
136 ut_assert(!ret_dev || ret_dev == 1088);
137
Etienne Carriere10d3e5d2022-02-21 09:22:41 +0100138 ut_asserteq(1088, agent->clk[0].rate);
139 ut_asserteq(200, agent->clk[1].rate);
140 ut_asserteq(1000, agent->clk[2].rate);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200141
142 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
143 ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1]));
Etienne Carriere87d4f272020-09-09 18:44:05 +0200144
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);
148
149 /* Test SCMI clocks gating manipulation */
Etienne Carriere41d62e22022-02-21 09:22:39 +0100150 ut_assert(!agent->clk[0].enabled);
151 ut_assert(!agent->clk[1].enabled);
152 ut_assert(!agent->clk[2].enabled);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200153
154 ut_asserteq(0, clk_enable(&scmi_devices->clk[1]));
Etienne Carriere87d4f272020-09-09 18:44:05 +0200155
Etienne Carriere10d3e5d2022-02-21 09:22:41 +0100156 ut_assert(agent->clk[0].enabled);
157 ut_assert(!agent->clk[1].enabled);
Etienne Carriere41d62e22022-02-21 09:22:39 +0100158 ut_assert(!agent->clk[2].enabled);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200159
160 ut_assertok(clk_disable(&scmi_devices->clk[1]));
Etienne Carriere87d4f272020-09-09 18:44:05 +0200161
Etienne Carriere41d62e22022-02-21 09:22:39 +0100162 ut_assert(!agent->clk[0].enabled);
163 ut_assert(!agent->clk[1].enabled);
164 ut_assert(!agent->clk[2].enabled);
Etienne Carriere87d4f272020-09-09 18:44:05 +0200165
166 return release_sandbox_scmi_test_devices(uts, dev);
167}
Etienne Carriere87d4f272020-09-09 18:44:05 +0200168DM_TEST(dm_test_scmi_clocks, UT_TESTF_SCAN_FDT);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200169
170static int dm_test_scmi_resets(struct unit_test_state *uts)
171{
172 struct sandbox_scmi_devices *scmi_devices;
173 struct sandbox_scmi_service *scmi_ctx;
Etienne Carriere41d62e22022-02-21 09:22:39 +0100174 struct sandbox_scmi_agent *agent;
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200175 struct udevice *dev = NULL;
176 int ret;
177
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200178 ret = load_sandbox_scmi_test_devices(uts, &dev);
179 if (ret)
180 return ret;
181
182 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere41d62e22022-02-21 09:22:39 +0100183 ut_assertnonnull(scmi_devices);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200184 scmi_ctx = sandbox_scmi_service_ctx();
Etienne Carriere41d62e22022-02-21 09:22:39 +0100185 ut_assertnonnull(scmi_ctx);
186 agent = scmi_ctx->agent;
187 ut_assertnonnull(agent);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200188
189 /* Test SCMI resect controller manipulation */
Etienne Carriere41d62e22022-02-21 09:22:39 +0100190 ut_assert(!agent->reset[0].asserted)
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200191
192 ut_assertok(reset_assert(&scmi_devices->reset[0]));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100193 ut_assert(agent->reset[0].asserted)
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200194
195 ut_assertok(reset_deassert(&scmi_devices->reset[0]));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100196 ut_assert(!agent->reset[0].asserted);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200197
198 return release_sandbox_scmi_test_devices(uts, dev);
199}
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200200DM_TEST(dm_test_scmi_resets, UT_TESTF_SCAN_FDT);
Etienne Carriere01242182021-03-08 22:38:07 +0100201
202static int dm_test_scmi_voltage_domains(struct unit_test_state *uts)
203{
204 struct sandbox_scmi_devices *scmi_devices;
205 struct sandbox_scmi_service *scmi_ctx;
Etienne Carriere41d62e22022-02-21 09:22:39 +0100206 struct sandbox_scmi_agent *agent;
Etienne Carriere01242182021-03-08 22:38:07 +0100207 struct dm_regulator_uclass_plat *uc_pdata;
208 struct udevice *dev;
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100209 struct udevice *regul0_dev;
Etienne Carriere01242182021-03-08 22:38:07 +0100210
211 ut_assertok(load_sandbox_scmi_test_devices(uts, &dev));
212
213 scmi_devices = sandbox_scmi_devices_ctx(dev);
Etienne Carriere41d62e22022-02-21 09:22:39 +0100214 ut_assertnonnull(scmi_devices);
Etienne Carriere01242182021-03-08 22:38:07 +0100215 scmi_ctx = sandbox_scmi_service_ctx();
Etienne Carriere41d62e22022-02-21 09:22:39 +0100216 ut_assertnonnull(scmi_ctx);
217 agent = scmi_ctx->agent;
218 ut_assertnonnull(agent);
Etienne Carriere01242182021-03-08 22:38:07 +0100219
220 /* Set/Get an SCMI voltage domain level */
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100221 regul0_dev = scmi_devices->regul[0];
222 ut_assert(regul0_dev);
Etienne Carriere01242182021-03-08 22:38:07 +0100223
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100224 uc_pdata = dev_get_uclass_plat(regul0_dev);
Etienne Carriere01242182021-03-08 22:38:07 +0100225 ut_assert(uc_pdata);
226
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100227 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->min_uV));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100228 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->min_uV);
Etienne Carriere01242182021-03-08 22:38:07 +0100229
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100230 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->min_uV);
Etienne Carriere01242182021-03-08 22:38:07 +0100231
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100232 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->max_uV));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100233 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->max_uV);
Etienne Carriere01242182021-03-08 22:38:07 +0100234
Etienne Carriere3dfd7392021-03-08 22:38:09 +0100235 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->max_uV);
Etienne Carriere01242182021-03-08 22:38:07 +0100236
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));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100240 ut_assert(!agent->voltd[0].enabled);
241 ut_assert(!agent->voltd[1].enabled);
Etienne Carriere01242182021-03-08 22:38:07 +0100242
243 ut_assertok(regulator_set_enable(scmi_devices->regul[0], true));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100244 ut_assert(agent->voltd[0].enabled);
245 ut_assert(!agent->voltd[1].enabled);
Etienne Carriere01242182021-03-08 22:38:07 +0100246
247 ut_assertok(regulator_set_enable(scmi_devices->regul[1], true));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100248 ut_assert(agent->voltd[0].enabled);
249 ut_assert(agent->voltd[1].enabled);
Etienne Carriere01242182021-03-08 22:38:07 +0100250
251 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
Etienne Carriere41d62e22022-02-21 09:22:39 +0100252 ut_assert(!agent->voltd[0].enabled);
253 ut_assert(agent->voltd[1].enabled);
Etienne Carriere01242182021-03-08 22:38:07 +0100254
255 return release_sandbox_scmi_test_devices(uts, dev);
256}
257DM_TEST(dm_test_scmi_voltage_domains, UT_TESTF_SCAN_FDT);