blob: 69239a198f0cfd1efb79463211fb7e43ff213d83 [file] [log] [blame]
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020, Linaro Limited
4 */
5
Patrick Delaunay5dd84d42021-02-24 11:19:44 +01006#define LOG_CATEGORY UCLASS_MISC
7
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +02008#include <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <malloc.h>
Etienne Carriere8b9b6892020-09-09 18:44:07 +020012#include <reset.h>
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020013#include <asm/io.h>
14#include <asm/scmi_test.h>
15#include <dm/device_compat.h>
16
17/*
18 * Simulate to some extent a SCMI exchange.
19 * This drivers gets SCMI resources and offers API function to the
Etienne Carriere8b9b6892020-09-09 18:44:07 +020020 * SCMI test sequence manipulate the resources, currently clock
21 * and reset controllers.
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020022 */
23
24#define SCMI_TEST_DEVICES_CLK_COUNT 3
Etienne Carriere8b9b6892020-09-09 18:44:07 +020025#define SCMI_TEST_DEVICES_RD_COUNT 1
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020026
27/*
28 * struct sandbox_scmi_device_priv - Storage for device handles used by test
29 * @clk: Array of clock instances used by tests
Etienne Carriere8b9b6892020-09-09 18:44:07 +020030 * @reset_clt: Array of the reset controller instances used by tests
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020031 * @devices: Resources exposed by sandbox_scmi_devices_ctx()
32 */
33struct sandbox_scmi_device_priv {
34 struct clk clk[SCMI_TEST_DEVICES_CLK_COUNT];
Etienne Carriere8b9b6892020-09-09 18:44:07 +020035 struct reset_ctl reset_ctl[SCMI_TEST_DEVICES_RD_COUNT];
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020036 struct sandbox_scmi_devices devices;
37};
38
39struct sandbox_scmi_devices *sandbox_scmi_devices_ctx(struct udevice *dev)
40{
41 struct sandbox_scmi_device_priv *priv = dev_get_priv(dev);
42
43 if (priv)
44 return &priv->devices;
45
46 return NULL;
47}
48
Etienne Carriere8b9b6892020-09-09 18:44:07 +020049static int sandbox_scmi_devices_remove(struct udevice *dev)
50{
51 struct sandbox_scmi_devices *devices = sandbox_scmi_devices_ctx(dev);
52 int ret = 0;
53 size_t n;
54
Heinrich Schuchardt748cbaa2021-02-01 03:01:54 +010055 if (!devices)
56 return 0;
57
Etienne Carriere8b9b6892020-09-09 18:44:07 +020058 for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) {
59 int ret2 = reset_free(devices->reset + n);
60
61 if (ret2 && !ret)
62 ret = ret2;
63 }
64
65 return ret;
66}
67
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020068static int sandbox_scmi_devices_probe(struct udevice *dev)
69{
70 struct sandbox_scmi_device_priv *priv = dev_get_priv(dev);
71 int ret;
72 size_t n;
73
74 priv->devices = (struct sandbox_scmi_devices){
75 .clk = priv->clk,
76 .clk_count = SCMI_TEST_DEVICES_CLK_COUNT,
Etienne Carriere8b9b6892020-09-09 18:44:07 +020077 .reset = priv->reset_ctl,
78 .reset_count = SCMI_TEST_DEVICES_RD_COUNT,
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020079 };
80
81 for (n = 0; n < SCMI_TEST_DEVICES_CLK_COUNT; n++) {
82 ret = clk_get_by_index(dev, n, priv->devices.clk + n);
83 if (ret) {
84 dev_err(dev, "%s: Failed on clk %zu\n", __func__, n);
85 return ret;
86 }
87 }
88
Etienne Carriere8b9b6892020-09-09 18:44:07 +020089 for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) {
90 ret = reset_get_by_index(dev, n, priv->devices.reset + n);
91 if (ret) {
92 dev_err(dev, "%s: Failed on reset %zu\n", __func__, n);
93 goto err_reset;
94 }
95 }
96
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020097 return 0;
Etienne Carriere8b9b6892020-09-09 18:44:07 +020098
99err_reset:
100 for (; n > 0; n--)
101 reset_free(priv->devices.reset + n - 1);
102
103 return ret;
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200104}
105
106static const struct udevice_id sandbox_scmi_devices_ids[] = {
107 { .compatible = "sandbox,scmi-devices" },
108 { }
109};
110
111U_BOOT_DRIVER(sandbox_scmi_devices) = {
112 .name = "sandbox-scmi_devices",
113 .id = UCLASS_MISC,
114 .of_match = sandbox_scmi_devices_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700115 .priv_auto = sizeof(struct sandbox_scmi_device_priv),
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200116 .remove = sandbox_scmi_devices_remove,
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200117 .probe = sandbox_scmi_devices_probe,
118};