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