blob: facb5b06ffb559da65eedd86e6037b0f4cd93c23 [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
Etienne Carrierec0dd1772020-09-09 18:44:07 +020065 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 Carriere87d4f272020-09-09 18:44:05 +020075static 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 Takahiro8e545b32023-10-16 14:39:45 +090082 .pwdom = &priv->pwdom,
83 .pwdom_count = 1,
Etienne Carriere87d4f272020-09-09 18:44:05 +020084 .clk = priv->clk,
85 .clk_count = SCMI_TEST_DEVICES_CLK_COUNT,
Etienne Carrierec0dd1772020-09-09 18:44:07 +020086 .reset = priv->reset_ctl,
87 .reset_count = SCMI_TEST_DEVICES_RD_COUNT,
Etienne Carriere01242182021-03-08 22:38:07 +010088 .regul = priv->regulators,
89 .regul_count = SCMI_TEST_DEVICES_VOLTD_COUNT,
Etienne Carriere87d4f272020-09-09 18:44:05 +020090 };
91
AKASHI Takahiro8e545b32023-10-16 14:39:45 +090092 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 Carriere87d4f272020-09-09 18:44:05 +020098 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 Carrierec0dd1772020-09-09 18:44:07 +0200106 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 Carriere01242182021-03-08 22:38:07 +0100114 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 Carriere87d4f272020-09-09 18:44:05 +0200128 return 0;
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200129
Etienne Carriere01242182021-03-08 22:38:07 +0100130err_regul:
131 n = SCMI_TEST_DEVICES_RD_COUNT;
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200132err_reset:
133 for (; n > 0; n--)
134 reset_free(priv->devices.reset + n - 1);
135
136 return ret;
Etienne Carriere87d4f272020-09-09 18:44:05 +0200137}
138
139static const struct udevice_id sandbox_scmi_devices_ids[] = {
140 { .compatible = "sandbox,scmi-devices" },
141 { }
142};
143
144U_BOOT_DRIVER(sandbox_scmi_devices) = {
145 .name = "sandbox-scmi_devices",
146 .id = UCLASS_MISC,
147 .of_match = sandbox_scmi_devices_ids,
Etienne Carriere41d62e22022-02-21 09:22:39 +0100148 .priv_auto = sizeof(struct sandbox_scmi_device_priv),
Etienne Carrierec0dd1772020-09-09 18:44:07 +0200149 .remove = sandbox_scmi_devices_remove,
Etienne Carriere87d4f272020-09-09 18:44:05 +0200150 .probe = sandbox_scmi_devices_probe,
151};