blob: 603e2bb40aff1a8aa63005e8df37dcd903244b49 [file] [log] [blame]
Etienne Carriere87d4f272020-09-09 18:44:05 +02001// SPDX-License-Identifier: GPL-2.0
2/*
Etienne Carriere01242182021-03-08 22:38:07 +01003 * Copyright (C) 2020-2021, Linaro Limited
Etienne Carriere87d4f272020-09-09 18:44:05 +02004 */
5
Patrick Delaunay9f5e4aa2021-02-24 11:19:44 +01006#define LOG_CATEGORY UCLASS_MISC
7
Etienne Carriere87d4f272020-09-09 18:44:05 +02008#include <common.h>
9#include <clk.h>
10#include <dm.h>
Etienne Carriere01242182021-03-08 22:38:07 +010011#include <log.h>
Etienne Carriere87d4f272020-09-09 18:44:05 +020012#include <malloc.h>
Etienne Carrierec0dd1772020-09-09 18:44:07 +020013#include <reset.h>
Etienne Carriere87d4f272020-09-09 18:44:05 +020014#include <asm/io.h>
15#include <asm/scmi_test.h>
16#include <dm/device_compat.h>
Etienne Carriere01242182021-03-08 22:38:07 +010017#include <power/regulator.h>
Etienne Carriere87d4f272020-09-09 18:44:05 +020018
19/*
20 * Simulate to some extent a SCMI exchange.
21 * This drivers gets SCMI resources and offers API function to the
Etienne Carrierec0dd1772020-09-09 18:44:07 +020022 * SCMI test sequence manipulate the resources, currently clock
23 * and reset controllers.
Etienne Carriere87d4f272020-09-09 18:44:05 +020024 */
25
Etienne Carriere41d62e22022-02-21 09:22:39 +010026#define SCMI_TEST_DEVICES_CLK_COUNT 2
Etienne Carrierec0dd1772020-09-09 18:44:07 +020027#define SCMI_TEST_DEVICES_RD_COUNT 1
Etienne Carriere01242182021-03-08 22:38:07 +010028#define SCMI_TEST_DEVICES_VOLTD_COUNT 2
Etienne Carriere87d4f272020-09-09 18:44:05 +020029
30/*
31 * struct sandbox_scmi_device_priv - Storage for device handles used by test
AKASHI Takahiro8e545b32023-10-16 14:39:45 +090032 * @pwdom: Power domain device
Etienne Carriere87d4f272020-09-09 18:44:05 +020033 * @clk: Array of clock instances used by tests
Etienne Carrierec0dd1772020-09-09 18:44:07 +020034 * @reset_clt: Array of the reset controller instances used by tests
Etienne Carriere01242182021-03-08 22:38:07 +010035 * @regulators: Array of regulator device references used by the tests
Etienne Carriere87d4f272020-09-09 18:44:05 +020036 * @devices: Resources exposed by sandbox_scmi_devices_ctx()
37 */
38struct sandbox_scmi_device_priv {
AKASHI Takahiro8e545b32023-10-16 14:39:45 +090039 struct power_domain pwdom;
Etienne Carriere87d4f272020-09-09 18:44:05 +020040 struct clk clk[SCMI_TEST_DEVICES_CLK_COUNT];
Etienne Carrierec0dd1772020-09-09 18:44:07 +020041 struct reset_ctl reset_ctl[SCMI_TEST_DEVICES_RD_COUNT];
Etienne Carriere01242182021-03-08 22:38:07 +010042 struct udevice *regulators[SCMI_TEST_DEVICES_VOLTD_COUNT];
Etienne Carriere87d4f272020-09-09 18:44:05 +020043 struct sandbox_scmi_devices devices;
44};
45
46struct 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 Carrierec0dd1772020-09-09 18:44:07 +020056static 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 Schuchardt6f5edba2021-02-01 03:01:54 +010062 if (!devices)
63 return 0;
64
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +090065 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 Carrierec0dd1772020-09-09 18:44:07 +020068
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +090069 if (ret2 && !ret)
70 ret = ret2;
71 }
Etienne Carrierec0dd1772020-09-09 18:44:07 +020072
73 return ret;
74}
75
Etienne Carriere87d4f272020-09-09 18:44:05 +020076static 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 Takahiro8e545b32023-10-16 14:39:45 +090083 .pwdom = &priv->pwdom,
84 .pwdom_count = 1,
Etienne Carriere87d4f272020-09-09 18:44:05 +020085 .clk = priv->clk,
86 .clk_count = SCMI_TEST_DEVICES_CLK_COUNT,
Etienne Carrierec0dd1772020-09-09 18:44:07 +020087 .reset = priv->reset_ctl,
88 .reset_count = SCMI_TEST_DEVICES_RD_COUNT,
Etienne Carriere01242182021-03-08 22:38:07 +010089 .regul = priv->regulators,
90 .regul_count = SCMI_TEST_DEVICES_VOLTD_COUNT,
Etienne Carriere87d4f272020-09-09 18:44:05 +020091 };
92
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +090093 if (CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN)) {
94 ret = power_domain_get_by_index(dev, priv->devices.pwdom, 0);
Etienne Carriere87d4f272020-09-09 18:44:05 +020095 if (ret) {
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +090096 dev_err(dev, "%s: Failed on power domain\n", __func__);
Etienne Carriere87d4f272020-09-09 18:44:05 +020097 return ret;
98 }
99 }
100
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +0900101 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 Carrierec0dd1772020-09-09 18:44:07 +0200109 }
110 }
111
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +0900112 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 Carriere01242182021-03-08 22:38:07 +0100123
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +0900124 if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI)) {
125 for (n = 0; n < SCMI_TEST_DEVICES_VOLTD_COUNT; n++) {
126 char name[32];
Etienne Carriere01242182021-03-08 22:38:07 +0100127
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +0900128 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 Carriere01242182021-03-08 22:38:07 +0100140 }
141 }
142
Etienne Carriere87d4f272020-09-09 18:44:05 +0200143 return 0;
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200144
Etienne Carriere01242182021-03-08 22:38:07 +0100145err_regul:
146 n = SCMI_TEST_DEVICES_RD_COUNT;
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200147err_reset:
AKASHI Takahiro9c07c0a2023-11-14 11:14:25 +0900148 if (CONFIG_IS_ENABLED(RESET_SCMI))
149 for (; n > 0; n--)
150 reset_free(priv->devices.reset + n - 1);
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200151
152 return ret;
Etienne Carriere87d4f272020-09-09 18:44:05 +0200153}
154
155static const struct udevice_id sandbox_scmi_devices_ids[] = {
156 { .compatible = "sandbox,scmi-devices" },
157 { }
158};
159
160U_BOOT_DRIVER(sandbox_scmi_devices) = {
161 .name = "sandbox-scmi_devices",
162 .id = UCLASS_MISC,
163 .of_match = sandbox_scmi_devices_ids,
Etienne Carriere41d62e22022-02-21 09:22:39 +0100164 .priv_auto = sizeof(struct sandbox_scmi_device_priv),
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200165 .remove = sandbox_scmi_devices_remove,
Etienne Carriere87d4f272020-09-09 18:44:05 +0200166 .probe = sandbox_scmi_devices_probe,
167};