Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 3 | * Copyright (C) 2020-2021, Linaro Limited |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 9f5e4aa | 2021-02-24 11:19:44 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_MISC |
| 7 | |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <clk.h> |
| 10 | #include <dm.h> |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 11 | #include <log.h> |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 12 | #include <malloc.h> |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 13 | #include <reset.h> |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <asm/scmi_test.h> |
| 16 | #include <dm/device_compat.h> |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 17 | #include <power/regulator.h> |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Simulate to some extent a SCMI exchange. |
| 21 | * This drivers gets SCMI resources and offers API function to the |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 22 | * SCMI test sequence manipulate the resources, currently clock |
| 23 | * and reset controllers. |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 26 | #define SCMI_TEST_DEVICES_CLK_COUNT 2 |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 27 | #define SCMI_TEST_DEVICES_RD_COUNT 1 |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 28 | #define SCMI_TEST_DEVICES_VOLTD_COUNT 2 |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * struct sandbox_scmi_device_priv - Storage for device handles used by test |
AKASHI Takahiro | 8e545b3 | 2023-10-16 14:39:45 +0900 | [diff] [blame] | 32 | * @pwdom: Power domain device |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 33 | * @clk: Array of clock instances used by tests |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 34 | * @reset_clt: Array of the reset controller instances used by tests |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 35 | * @regulators: Array of regulator device references used by the tests |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 36 | * @devices: Resources exposed by sandbox_scmi_devices_ctx() |
| 37 | */ |
| 38 | struct sandbox_scmi_device_priv { |
AKASHI Takahiro | 8e545b3 | 2023-10-16 14:39:45 +0900 | [diff] [blame] | 39 | struct power_domain pwdom; |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 40 | struct clk clk[SCMI_TEST_DEVICES_CLK_COUNT]; |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 41 | struct reset_ctl reset_ctl[SCMI_TEST_DEVICES_RD_COUNT]; |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 42 | struct udevice *regulators[SCMI_TEST_DEVICES_VOLTD_COUNT]; |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 43 | struct sandbox_scmi_devices devices; |
| 44 | }; |
| 45 | |
| 46 | struct sandbox_scmi_devices *sandbox_scmi_devices_ctx(struct udevice *dev) |
| 47 | { |
| 48 | struct sandbox_scmi_device_priv *priv = dev_get_priv(dev); |
| 49 | |
| 50 | if (priv) |
| 51 | return &priv->devices; |
| 52 | |
| 53 | return NULL; |
| 54 | } |
| 55 | |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 56 | static int sandbox_scmi_devices_remove(struct udevice *dev) |
| 57 | { |
| 58 | struct sandbox_scmi_devices *devices = sandbox_scmi_devices_ctx(dev); |
| 59 | int ret = 0; |
| 60 | size_t n; |
| 61 | |
Heinrich Schuchardt | 6f5edba | 2021-02-01 03:01:54 +0100 | [diff] [blame] | 62 | if (!devices) |
| 63 | return 0; |
| 64 | |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 65 | if (CONFIG_IS_ENABLED(RESET_SCMI)) |
| 66 | for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) { |
| 67 | int ret2 = reset_free(devices->reset + n); |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 68 | |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 69 | if (ret2 && !ret) |
| 70 | ret = ret2; |
| 71 | } |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 76 | static int sandbox_scmi_devices_probe(struct udevice *dev) |
| 77 | { |
| 78 | struct sandbox_scmi_device_priv *priv = dev_get_priv(dev); |
| 79 | int ret; |
| 80 | size_t n; |
| 81 | |
| 82 | priv->devices = (struct sandbox_scmi_devices){ |
AKASHI Takahiro | 8e545b3 | 2023-10-16 14:39:45 +0900 | [diff] [blame] | 83 | .pwdom = &priv->pwdom, |
| 84 | .pwdom_count = 1, |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 85 | .clk = priv->clk, |
| 86 | .clk_count = SCMI_TEST_DEVICES_CLK_COUNT, |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 87 | .reset = priv->reset_ctl, |
| 88 | .reset_count = SCMI_TEST_DEVICES_RD_COUNT, |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 89 | .regul = priv->regulators, |
| 90 | .regul_count = SCMI_TEST_DEVICES_VOLTD_COUNT, |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 93 | if (CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN)) { |
| 94 | ret = power_domain_get_by_index(dev, priv->devices.pwdom, 0); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 95 | if (ret) { |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 96 | dev_err(dev, "%s: Failed on power domain\n", __func__); |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 97 | return ret; |
| 98 | } |
| 99 | } |
| 100 | |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 101 | if (CONFIG_IS_ENABLED(CLK_SCMI)) { |
| 102 | for (n = 0; n < SCMI_TEST_DEVICES_CLK_COUNT; n++) { |
| 103 | ret = clk_get_by_index(dev, n, priv->devices.clk + n); |
| 104 | if (ret) { |
| 105 | dev_err(dev, "%s: Failed on clk %zu\n", |
| 106 | __func__, n); |
| 107 | return ret; |
| 108 | } |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 112 | if (CONFIG_IS_ENABLED(RESET_SCMI)) { |
| 113 | for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) { |
| 114 | ret = reset_get_by_index(dev, n, |
| 115 | priv->devices.reset + n); |
| 116 | if (ret) { |
| 117 | dev_err(dev, "%s: Failed on reset %zu\n", |
| 118 | __func__, n); |
| 119 | goto err_reset; |
| 120 | } |
| 121 | } |
| 122 | } |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 123 | |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 124 | if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI)) { |
| 125 | for (n = 0; n < SCMI_TEST_DEVICES_VOLTD_COUNT; n++) { |
| 126 | char name[32]; |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 127 | |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 128 | ret = snprintf(name, sizeof(name), "regul%zu-supply", |
| 129 | n); |
| 130 | assert(ret >= 0 && ret < sizeof(name)); |
| 131 | |
| 132 | ret = device_get_supply_regulator(dev, name, |
| 133 | priv->devices.regul |
| 134 | + n); |
| 135 | if (ret) { |
| 136 | dev_err(dev, "%s: Failed on voltd %zu\n", |
| 137 | __func__, n); |
| 138 | goto err_regul; |
| 139 | } |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 143 | return 0; |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 144 | |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 145 | err_regul: |
| 146 | n = SCMI_TEST_DEVICES_RD_COUNT; |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 147 | err_reset: |
AKASHI Takahiro | 9c07c0a | 2023-11-14 11:14:25 +0900 | [diff] [blame] | 148 | if (CONFIG_IS_ENABLED(RESET_SCMI)) |
| 149 | for (; n > 0; n--) |
| 150 | reset_free(priv->devices.reset + n - 1); |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 151 | |
| 152 | return ret; |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static const struct udevice_id sandbox_scmi_devices_ids[] = { |
| 156 | { .compatible = "sandbox,scmi-devices" }, |
| 157 | { } |
| 158 | }; |
| 159 | |
| 160 | U_BOOT_DRIVER(sandbox_scmi_devices) = { |
| 161 | .name = "sandbox-scmi_devices", |
| 162 | .id = UCLASS_MISC, |
| 163 | .of_match = sandbox_scmi_devices_ids, |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 164 | .priv_auto = sizeof(struct sandbox_scmi_device_priv), |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 165 | .remove = sandbox_scmi_devices_remove, |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 166 | .probe = sandbox_scmi_devices_probe, |
| 167 | }; |