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 | |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 65 | for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) { |
| 66 | int ret2 = reset_free(devices->reset + n); |
| 67 | |
| 68 | if (ret2 && !ret) |
| 69 | ret = ret2; |
| 70 | } |
| 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
Etienne Carriere | 87d4f27 | 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 | 8e545b3 | 2023-10-16 14:39:45 +0900 | [diff] [blame] | 82 | .pwdom = &priv->pwdom, |
| 83 | .pwdom_count = 1, |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 84 | .clk = priv->clk, |
| 85 | .clk_count = SCMI_TEST_DEVICES_CLK_COUNT, |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 86 | .reset = priv->reset_ctl, |
| 87 | .reset_count = SCMI_TEST_DEVICES_RD_COUNT, |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 88 | .regul = priv->regulators, |
| 89 | .regul_count = SCMI_TEST_DEVICES_VOLTD_COUNT, |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
AKASHI Takahiro | 8e545b3 | 2023-10-16 14:39:45 +0900 | [diff] [blame] | 92 | ret = power_domain_get_by_index(dev, priv->devices.pwdom, 0); |
| 93 | if (ret) { |
| 94 | dev_err(dev, "%s: Failed on power domain\n", __func__); |
| 95 | return ret; |
| 96 | } |
| 97 | |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 98 | for (n = 0; n < SCMI_TEST_DEVICES_CLK_COUNT; n++) { |
| 99 | ret = clk_get_by_index(dev, n, priv->devices.clk + n); |
| 100 | if (ret) { |
| 101 | dev_err(dev, "%s: Failed on clk %zu\n", __func__, n); |
| 102 | return ret; |
| 103 | } |
| 104 | } |
| 105 | |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 106 | for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) { |
| 107 | ret = reset_get_by_index(dev, n, priv->devices.reset + n); |
| 108 | if (ret) { |
| 109 | dev_err(dev, "%s: Failed on reset %zu\n", __func__, n); |
| 110 | goto err_reset; |
| 111 | } |
| 112 | } |
| 113 | |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 114 | for (n = 0; n < SCMI_TEST_DEVICES_VOLTD_COUNT; n++) { |
| 115 | char name[32]; |
| 116 | |
| 117 | ret = snprintf(name, sizeof(name), "regul%zu-supply", n); |
| 118 | assert(ret >= 0 && ret < sizeof(name)); |
| 119 | |
| 120 | ret = device_get_supply_regulator(dev, name, |
| 121 | priv->devices.regul + n); |
| 122 | if (ret) { |
| 123 | dev_err(dev, "%s: Failed on voltd %zu\n", __func__, n); |
| 124 | goto err_regul; |
| 125 | } |
| 126 | } |
| 127 | |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 128 | return 0; |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 129 | |
Etienne Carriere | 0124218 | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 130 | err_regul: |
| 131 | n = SCMI_TEST_DEVICES_RD_COUNT; |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 132 | err_reset: |
| 133 | for (; n > 0; n--) |
| 134 | reset_free(priv->devices.reset + n - 1); |
| 135 | |
| 136 | return ret; |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static const struct udevice_id sandbox_scmi_devices_ids[] = { |
| 140 | { .compatible = "sandbox,scmi-devices" }, |
| 141 | { } |
| 142 | }; |
| 143 | |
| 144 | U_BOOT_DRIVER(sandbox_scmi_devices) = { |
| 145 | .name = "sandbox-scmi_devices", |
| 146 | .id = UCLASS_MISC, |
| 147 | .of_match = sandbox_scmi_devices_ids, |
Etienne Carriere | 41d62e2 | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 148 | .priv_auto = sizeof(struct sandbox_scmi_device_priv), |
Etienne Carriere | c0dd177 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 149 | .remove = sandbox_scmi_devices_remove, |
Etienne Carriere | 87d4f27 | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 150 | .probe = sandbox_scmi_devices_probe, |
| 151 | }; |