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