Etienne Carriere | 358599e | 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 | * |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 8 | * Note in test.dts the protocol@10 node in scmi node. Protocol 0x10 is not |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 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 | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 16 | #include <clk.h> |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 17 | #include <dm.h> |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 18 | #include <reset.h> |
Etienne Carriere | 358599e | 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 | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 22 | #include <linux/kconfig.h> |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 23 | #include <power/regulator.h> |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 24 | #include <test/ut.h> |
| 25 | |
Etienne Carriere | 87d4f27 | 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); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 31 | ut_assertnull(scmi_ctx->agent); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static 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 Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 41 | struct sandbox_scmi_agent *agent; |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 42 | |
| 43 | /* Device references to check context against test sequence */ |
| 44 | scmi_devices = sandbox_scmi_devices_ctx(dev); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 45 | ut_assertnonnull(scmi_devices); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 46 | ut_asserteq(2, scmi_devices->clk_count); |
Etienne Carriere | c3bba70 | 2021-03-08 22:38:08 +0100 | [diff] [blame] | 47 | ut_asserteq(1, scmi_devices->reset_count); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 48 | ut_asserteq(2, scmi_devices->regul_count); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 49 | |
| 50 | /* State of the simulated SCMI server exposed */ |
| 51 | scmi_ctx = sandbox_scmi_service_ctx(); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 52 | ut_assertnonnull(scmi_ctx); |
| 53 | agent = scmi_ctx->agent; |
| 54 | ut_assertnonnull(agent); |
Etienne Carriere | 10d3e5d | 2022-02-21 09:22:41 +0100 | [diff] [blame] | 55 | ut_asserteq(3, agent->clk_count); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 56 | 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 Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static 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 | |
| 81 | static 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 Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 90 | /* |
| 91 | * Test SCMI states when loading and releasing resources |
| 92 | * related to SCMI drivers. |
| 93 | */ |
| 94 | static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts) |
| 95 | { |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 96 | struct udevice *dev = NULL; |
| 97 | int ret; |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 98 | |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 99 | ret = load_sandbox_scmi_test_devices(uts, &dev); |
| 100 | if (!ret) |
| 101 | ret = release_sandbox_scmi_test_devices(uts, dev); |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 102 | |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 103 | return ret; |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 104 | } |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 105 | DM_TEST(dm_test_scmi_sandbox_agent, UT_TESTF_SCAN_FDT); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 106 | |
| 107 | static 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 Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 111 | struct sandbox_scmi_agent *agent; |
| 112 | struct udevice *dev; |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 113 | int ret_dev; |
| 114 | int ret; |
| 115 | |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 116 | ret = load_sandbox_scmi_test_devices(uts, &dev); |
| 117 | if (ret) |
| 118 | return ret; |
| 119 | |
| 120 | scmi_devices = sandbox_scmi_devices_ctx(dev); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 121 | ut_assertnonnull(scmi_devices); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 122 | scmi_ctx = sandbox_scmi_service_ctx(); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 123 | ut_assertnonnull(scmi_ctx); |
| 124 | agent = scmi_ctx->agent; |
| 125 | ut_assertnonnull(agent); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 126 | |
| 127 | /* Test SCMI clocks rate manipulation */ |
Etienne Carriere | 10d3e5d | 2022-02-21 09:22:41 +0100 | [diff] [blame] | 128 | 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 Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 132 | ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0])); |
| 133 | ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1])); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 134 | |
| 135 | ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088); |
| 136 | ut_assert(!ret_dev || ret_dev == 1088); |
| 137 | |
Etienne Carriere | 10d3e5d | 2022-02-21 09:22:41 +0100 | [diff] [blame] | 138 | ut_asserteq(1088, agent->clk[0].rate); |
| 139 | ut_asserteq(200, agent->clk[1].rate); |
| 140 | ut_asserteq(1000, agent->clk[2].rate); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 141 | |
| 142 | ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0])); |
| 143 | ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1])); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 144 | |
| 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 Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 150 | ut_assert(!agent->clk[0].enabled); |
| 151 | ut_assert(!agent->clk[1].enabled); |
| 152 | ut_assert(!agent->clk[2].enabled); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 153 | |
| 154 | ut_asserteq(0, clk_enable(&scmi_devices->clk[1])); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 155 | |
Etienne Carriere | 10d3e5d | 2022-02-21 09:22:41 +0100 | [diff] [blame] | 156 | ut_assert(agent->clk[0].enabled); |
| 157 | ut_assert(!agent->clk[1].enabled); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 158 | ut_assert(!agent->clk[2].enabled); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 159 | |
| 160 | ut_assertok(clk_disable(&scmi_devices->clk[1])); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 161 | |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 162 | ut_assert(!agent->clk[0].enabled); |
| 163 | ut_assert(!agent->clk[1].enabled); |
| 164 | ut_assert(!agent->clk[2].enabled); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 165 | |
| 166 | return release_sandbox_scmi_test_devices(uts, dev); |
| 167 | } |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 168 | DM_TEST(dm_test_scmi_clocks, UT_TESTF_SCAN_FDT); |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 169 | |
| 170 | static 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 Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 174 | struct sandbox_scmi_agent *agent; |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 175 | struct udevice *dev = NULL; |
| 176 | int ret; |
| 177 | |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 178 | ret = load_sandbox_scmi_test_devices(uts, &dev); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | |
| 182 | scmi_devices = sandbox_scmi_devices_ctx(dev); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 183 | ut_assertnonnull(scmi_devices); |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 184 | scmi_ctx = sandbox_scmi_service_ctx(); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 185 | ut_assertnonnull(scmi_ctx); |
| 186 | agent = scmi_ctx->agent; |
| 187 | ut_assertnonnull(agent); |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 188 | |
| 189 | /* Test SCMI resect controller manipulation */ |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 190 | ut_assert(!agent->reset[0].asserted) |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 191 | |
| 192 | ut_assertok(reset_assert(&scmi_devices->reset[0])); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 193 | ut_assert(agent->reset[0].asserted) |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 194 | |
| 195 | ut_assertok(reset_deassert(&scmi_devices->reset[0])); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 196 | ut_assert(!agent->reset[0].asserted); |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 197 | |
| 198 | return release_sandbox_scmi_test_devices(uts, dev); |
| 199 | } |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 200 | DM_TEST(dm_test_scmi_resets, UT_TESTF_SCAN_FDT); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 201 | |
| 202 | static 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 Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 206 | struct sandbox_scmi_agent *agent; |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 207 | struct dm_regulator_uclass_plat *uc_pdata; |
| 208 | struct udevice *dev; |
Etienne Carriere | 3dfd739 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 209 | struct udevice *regul0_dev; |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 210 | |
| 211 | ut_assertok(load_sandbox_scmi_test_devices(uts, &dev)); |
| 212 | |
| 213 | scmi_devices = sandbox_scmi_devices_ctx(dev); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 214 | ut_assertnonnull(scmi_devices); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 215 | scmi_ctx = sandbox_scmi_service_ctx(); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 216 | ut_assertnonnull(scmi_ctx); |
| 217 | agent = scmi_ctx->agent; |
| 218 | ut_assertnonnull(agent); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 219 | |
| 220 | /* Set/Get an SCMI voltage domain level */ |
Etienne Carriere | 3dfd739 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 221 | regul0_dev = scmi_devices->regul[0]; |
| 222 | ut_assert(regul0_dev); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 223 | |
Etienne Carriere | 3dfd739 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 224 | uc_pdata = dev_get_uclass_plat(regul0_dev); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 225 | ut_assert(uc_pdata); |
| 226 | |
Etienne Carriere | 3dfd739 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 227 | ut_assertok(regulator_set_value(regul0_dev, uc_pdata->min_uV)); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 228 | ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->min_uV); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 229 | |
Etienne Carriere | 3dfd739 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 230 | ut_assert(regulator_get_value(regul0_dev) == uc_pdata->min_uV); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 231 | |
Etienne Carriere | 3dfd739 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 232 | ut_assertok(regulator_set_value(regul0_dev, uc_pdata->max_uV)); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 233 | ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->max_uV); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 234 | |
Etienne Carriere | 3dfd739 | 2021-03-08 22:38:09 +0100 | [diff] [blame] | 235 | ut_assert(regulator_get_value(regul0_dev) == uc_pdata->max_uV); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 236 | |
| 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 Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 240 | ut_assert(!agent->voltd[0].enabled); |
| 241 | ut_assert(!agent->voltd[1].enabled); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 242 | |
| 243 | ut_assertok(regulator_set_enable(scmi_devices->regul[0], true)); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 244 | ut_assert(agent->voltd[0].enabled); |
| 245 | ut_assert(!agent->voltd[1].enabled); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 246 | |
| 247 | ut_assertok(regulator_set_enable(scmi_devices->regul[1], true)); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 248 | ut_assert(agent->voltd[0].enabled); |
| 249 | ut_assert(agent->voltd[1].enabled); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 250 | |
| 251 | ut_assertok(regulator_set_enable(scmi_devices->regul[0], false)); |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 252 | ut_assert(!agent->voltd[0].enabled); |
| 253 | ut_assert(agent->voltd[1].enabled); |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 254 | |
| 255 | return release_sandbox_scmi_test_devices(uts, dev); |
| 256 | } |
| 257 | DM_TEST(dm_test_scmi_voltage_domains, UT_TESTF_SCAN_FDT); |