Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 1 | // 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 | * |
| 8 | * Note in test.dts the protocol@10 node in agent 1. 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. |
| 13 | */ |
| 14 | |
| 15 | #include <common.h> |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 16 | #include <clk.h> |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 17 | #include <dm.h> |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 18 | #include <reset.h> |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 19 | #include <asm/scmi_test.h> |
| 20 | #include <dm/device-internal.h> |
| 21 | #include <dm/test.h> |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 22 | #include <linux/kconfig.h> |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 23 | #include <power/regulator.h> |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 24 | #include <test/ut.h> |
| 25 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 26 | static 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); |
| 31 | if (scmi_ctx->agent_count) |
| 32 | ut_asserteq(2, scmi_ctx->agent_count); |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts, |
| 38 | struct udevice *dev) |
| 39 | { |
| 40 | struct sandbox_scmi_devices *scmi_devices; |
| 41 | struct sandbox_scmi_service *scmi_ctx; |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 42 | struct sandbox_scmi_agent *agent0; |
| 43 | struct sandbox_scmi_agent *agent1; |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 44 | |
| 45 | /* Device references to check context against test sequence */ |
| 46 | scmi_devices = sandbox_scmi_devices_ctx(dev); |
| 47 | |
| 48 | ut_assertnonnull(scmi_devices); |
Etienne Carriere | c83a843 | 2021-03-08 22:38:08 +0100 | [diff] [blame] | 49 | ut_asserteq(3, scmi_devices->clk_count); |
| 50 | ut_asserteq(1, scmi_devices->reset_count); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 51 | ut_asserteq(2, scmi_devices->regul_count); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 52 | |
| 53 | /* State of the simulated SCMI server exposed */ |
| 54 | scmi_ctx = sandbox_scmi_service_ctx(); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 55 | agent0 = scmi_ctx->agent[0]; |
| 56 | agent1 = scmi_ctx->agent[1]; |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 57 | |
| 58 | ut_asserteq(2, scmi_ctx->agent_count); |
| 59 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 60 | ut_assertnonnull(agent0); |
| 61 | ut_asserteq(2, agent0->clk_count); |
| 62 | ut_assertnonnull(agent0->clk); |
| 63 | ut_asserteq(1, agent0->reset_count); |
| 64 | ut_assertnonnull(agent0->reset); |
| 65 | ut_asserteq(2, agent0->voltd_count); |
| 66 | ut_assertnonnull(agent0->voltd); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 67 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 68 | ut_assertnonnull(agent1); |
| 69 | ut_assertnonnull(agent1->clk); |
| 70 | ut_asserteq(1, agent1->clk_count); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int load_sandbox_scmi_test_devices(struct unit_test_state *uts, |
| 76 | struct udevice **dev) |
| 77 | { |
| 78 | int ret; |
| 79 | |
| 80 | ret = ut_assert_scmi_state_preprobe(uts); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | |
| 84 | ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_scmi", |
| 85 | dev)); |
| 86 | ut_assertnonnull(*dev); |
| 87 | |
| 88 | return ut_assert_scmi_state_postprobe(uts, *dev); |
| 89 | } |
| 90 | |
| 91 | static int release_sandbox_scmi_test_devices(struct unit_test_state *uts, |
| 92 | struct udevice *dev) |
| 93 | { |
| 94 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
| 95 | |
| 96 | /* Not sure test devices are fully removed, agent may not be visible */ |
| 97 | return 0; |
| 98 | } |
| 99 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 100 | /* |
| 101 | * Test SCMI states when loading and releasing resources |
| 102 | * related to SCMI drivers. |
| 103 | */ |
| 104 | static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts) |
| 105 | { |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 106 | struct udevice *dev = NULL; |
| 107 | int ret; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 108 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 109 | ret = load_sandbox_scmi_test_devices(uts, &dev); |
| 110 | if (!ret) |
| 111 | ret = release_sandbox_scmi_test_devices(uts, dev); |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 112 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 113 | return ret; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 114 | } |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 115 | DM_TEST(dm_test_scmi_sandbox_agent, UT_TESTF_SCAN_FDT); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 116 | |
| 117 | static int dm_test_scmi_clocks(struct unit_test_state *uts) |
| 118 | { |
| 119 | struct sandbox_scmi_devices *scmi_devices; |
| 120 | struct sandbox_scmi_service *scmi_ctx; |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 121 | struct sandbox_scmi_agent *agent0; |
| 122 | struct sandbox_scmi_agent *agent1; |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 123 | struct udevice *dev = NULL; |
| 124 | int ret_dev; |
| 125 | int ret; |
| 126 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 127 | ret = load_sandbox_scmi_test_devices(uts, &dev); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | |
| 131 | scmi_devices = sandbox_scmi_devices_ctx(dev); |
| 132 | scmi_ctx = sandbox_scmi_service_ctx(); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 133 | agent0 = scmi_ctx->agent[0]; |
| 134 | agent1 = scmi_ctx->agent[1]; |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 135 | |
| 136 | /* Test SCMI clocks rate manipulation */ |
| 137 | ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0])); |
| 138 | ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1])); |
| 139 | ut_asserteq(44, clk_get_rate(&scmi_devices->clk[2])); |
| 140 | |
| 141 | ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088); |
| 142 | ut_assert(!ret_dev || ret_dev == 1088); |
| 143 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 144 | ut_asserteq(1000, agent0->clk[0].rate); |
| 145 | ut_asserteq(1088, agent0->clk[1].rate); |
| 146 | ut_asserteq(44, agent1->clk[0].rate); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 147 | |
| 148 | ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0])); |
| 149 | ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1])); |
| 150 | ut_asserteq(44, clk_get_rate(&scmi_devices->clk[2])); |
| 151 | |
| 152 | /* restore original rate for further tests */ |
| 153 | ret_dev = clk_set_rate(&scmi_devices->clk[1], 333); |
| 154 | ut_assert(!ret_dev || ret_dev == 333); |
| 155 | |
| 156 | /* Test SCMI clocks gating manipulation */ |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 157 | ut_assert(!agent0->clk[0].enabled); |
| 158 | ut_assert(!agent0->clk[1].enabled); |
| 159 | ut_assert(!agent1->clk[0].enabled); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 160 | |
| 161 | ut_asserteq(0, clk_enable(&scmi_devices->clk[1])); |
| 162 | ut_asserteq(0, clk_enable(&scmi_devices->clk[2])); |
| 163 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 164 | ut_assert(!agent0->clk[0].enabled); |
| 165 | ut_assert(agent0->clk[1].enabled); |
| 166 | ut_assert(agent1->clk[0].enabled); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 167 | |
| 168 | ut_assertok(clk_disable(&scmi_devices->clk[1])); |
| 169 | ut_assertok(clk_disable(&scmi_devices->clk[2])); |
| 170 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 171 | ut_assert(!agent0->clk[0].enabled); |
| 172 | ut_assert(!agent0->clk[1].enabled); |
| 173 | ut_assert(!agent1->clk[0].enabled); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 174 | |
| 175 | return release_sandbox_scmi_test_devices(uts, dev); |
| 176 | } |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 177 | DM_TEST(dm_test_scmi_clocks, UT_TESTF_SCAN_FDT); |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 178 | |
| 179 | static int dm_test_scmi_resets(struct unit_test_state *uts) |
| 180 | { |
| 181 | struct sandbox_scmi_devices *scmi_devices; |
| 182 | struct sandbox_scmi_service *scmi_ctx; |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 183 | struct sandbox_scmi_agent *agent0; |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 184 | struct udevice *dev = NULL; |
| 185 | int ret; |
| 186 | |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 187 | ret = load_sandbox_scmi_test_devices(uts, &dev); |
| 188 | if (ret) |
| 189 | return ret; |
| 190 | |
| 191 | scmi_devices = sandbox_scmi_devices_ctx(dev); |
| 192 | scmi_ctx = sandbox_scmi_service_ctx(); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 193 | agent0 = scmi_ctx->agent[0]; |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 194 | |
| 195 | /* Test SCMI resect controller manipulation */ |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 196 | ut_assert(!agent0->reset[0].asserted) |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 197 | |
| 198 | ut_assertok(reset_assert(&scmi_devices->reset[0])); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 199 | ut_assert(agent0->reset[0].asserted) |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 200 | |
| 201 | ut_assertok(reset_deassert(&scmi_devices->reset[0])); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 202 | ut_assert(!agent0->reset[0].asserted); |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 203 | |
| 204 | return release_sandbox_scmi_test_devices(uts, dev); |
| 205 | } |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 206 | DM_TEST(dm_test_scmi_resets, UT_TESTF_SCAN_FDT); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 207 | |
| 208 | static int dm_test_scmi_voltage_domains(struct unit_test_state *uts) |
| 209 | { |
| 210 | struct sandbox_scmi_devices *scmi_devices; |
| 211 | struct sandbox_scmi_service *scmi_ctx; |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 212 | struct sandbox_scmi_agent *agent0; |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 213 | struct dm_regulator_uclass_plat *uc_pdata; |
| 214 | struct udevice *dev; |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 215 | struct udevice *regul0_dev; |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 216 | |
| 217 | ut_assertok(load_sandbox_scmi_test_devices(uts, &dev)); |
| 218 | |
| 219 | scmi_devices = sandbox_scmi_devices_ctx(dev); |
| 220 | scmi_ctx = sandbox_scmi_service_ctx(); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 221 | agent0 = scmi_ctx->agent[0]; |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 222 | |
| 223 | /* Set/Get an SCMI voltage domain level */ |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 224 | regul0_dev = scmi_devices->regul[0]; |
| 225 | ut_assert(regul0_dev); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 226 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 227 | uc_pdata = dev_get_uclass_plat(regul0_dev); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 228 | ut_assert(uc_pdata); |
| 229 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 230 | ut_assertok(regulator_set_value(regul0_dev, uc_pdata->min_uV)); |
| 231 | ut_asserteq(agent0->voltd[0].voltage_uv, uc_pdata->min_uV); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 232 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 233 | ut_assert(regulator_get_value(regul0_dev) == uc_pdata->min_uV); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 234 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 235 | ut_assertok(regulator_set_value(regul0_dev, uc_pdata->max_uV)); |
| 236 | ut_asserteq(agent0->voltd[0].voltage_uv, uc_pdata->max_uV); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 237 | |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 238 | ut_assert(regulator_get_value(regul0_dev) == uc_pdata->max_uV); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 239 | |
| 240 | /* Enable/disable SCMI voltage domains */ |
| 241 | ut_assertok(regulator_set_enable(scmi_devices->regul[0], false)); |
| 242 | ut_assertok(regulator_set_enable(scmi_devices->regul[1], false)); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 243 | ut_assert(!agent0->voltd[0].enabled); |
| 244 | ut_assert(!agent0->voltd[1].enabled); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 245 | |
| 246 | ut_assertok(regulator_set_enable(scmi_devices->regul[0], true)); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 247 | ut_assert(agent0->voltd[0].enabled); |
| 248 | ut_assert(!agent0->voltd[1].enabled); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 249 | |
| 250 | ut_assertok(regulator_set_enable(scmi_devices->regul[1], true)); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 251 | ut_assert(agent0->voltd[0].enabled); |
| 252 | ut_assert(agent0->voltd[1].enabled); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 253 | |
| 254 | ut_assertok(regulator_set_enable(scmi_devices->regul[0], false)); |
Etienne Carriere | eeaf282 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 255 | ut_assert(!agent0->voltd[0].enabled); |
| 256 | ut_assert(agent0->voltd[1].enabled); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 257 | |
| 258 | return release_sandbox_scmi_test_devices(uts, dev); |
| 259 | } |
| 260 | DM_TEST(dm_test_scmi_voltage_domains, UT_TESTF_SCAN_FDT); |