blob: 1a6fafbf531bb843088b76f3c1315b029dd13f82 [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
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
9#include <malloc.h>
Etienne Carriere8b9b6892020-09-09 18:44:07 +020010#include <reset.h>
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020011#include <asm/io.h>
12#include <asm/scmi_test.h>
13#include <dm/device_compat.h>
14
15/*
16 * Simulate to some extent a SCMI exchange.
17 * This drivers gets SCMI resources and offers API function to the
Etienne Carriere8b9b6892020-09-09 18:44:07 +020018 * SCMI test sequence manipulate the resources, currently clock
19 * and reset controllers.
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020020 */
21
22#define SCMI_TEST_DEVICES_CLK_COUNT 3
Etienne Carriere8b9b6892020-09-09 18:44:07 +020023#define SCMI_TEST_DEVICES_RD_COUNT 1
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020024
25/*
26 * struct sandbox_scmi_device_priv - Storage for device handles used by test
27 * @clk: Array of clock instances used by tests
Etienne Carriere8b9b6892020-09-09 18:44:07 +020028 * @reset_clt: Array of the reset controller instances used by tests
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020029 * @devices: Resources exposed by sandbox_scmi_devices_ctx()
30 */
31struct sandbox_scmi_device_priv {
32 struct clk clk[SCMI_TEST_DEVICES_CLK_COUNT];
Etienne Carriere8b9b6892020-09-09 18:44:07 +020033 struct reset_ctl reset_ctl[SCMI_TEST_DEVICES_RD_COUNT];
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020034 struct sandbox_scmi_devices devices;
35};
36
37struct sandbox_scmi_devices *sandbox_scmi_devices_ctx(struct udevice *dev)
38{
39 struct sandbox_scmi_device_priv *priv = dev_get_priv(dev);
40
41 if (priv)
42 return &priv->devices;
43
44 return NULL;
45}
46
Etienne Carriere8b9b6892020-09-09 18:44:07 +020047static int sandbox_scmi_devices_remove(struct udevice *dev)
48{
49 struct sandbox_scmi_devices *devices = sandbox_scmi_devices_ctx(dev);
50 int ret = 0;
51 size_t n;
52
Heinrich Schuchardt748cbaa2021-02-01 03:01:54 +010053 if (!devices)
54 return 0;
55
Etienne Carriere8b9b6892020-09-09 18:44:07 +020056 for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) {
57 int ret2 = reset_free(devices->reset + n);
58
59 if (ret2 && !ret)
60 ret = ret2;
61 }
62
63 return ret;
64}
65
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020066static int sandbox_scmi_devices_probe(struct udevice *dev)
67{
68 struct sandbox_scmi_device_priv *priv = dev_get_priv(dev);
69 int ret;
70 size_t n;
71
72 priv->devices = (struct sandbox_scmi_devices){
73 .clk = priv->clk,
74 .clk_count = SCMI_TEST_DEVICES_CLK_COUNT,
Etienne Carriere8b9b6892020-09-09 18:44:07 +020075 .reset = priv->reset_ctl,
76 .reset_count = SCMI_TEST_DEVICES_RD_COUNT,
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020077 };
78
79 for (n = 0; n < SCMI_TEST_DEVICES_CLK_COUNT; n++) {
80 ret = clk_get_by_index(dev, n, priv->devices.clk + n);
81 if (ret) {
82 dev_err(dev, "%s: Failed on clk %zu\n", __func__, n);
83 return ret;
84 }
85 }
86
Etienne Carriere8b9b6892020-09-09 18:44:07 +020087 for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) {
88 ret = reset_get_by_index(dev, n, priv->devices.reset + n);
89 if (ret) {
90 dev_err(dev, "%s: Failed on reset %zu\n", __func__, n);
91 goto err_reset;
92 }
93 }
94
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +020095 return 0;
Etienne Carriere8b9b6892020-09-09 18:44:07 +020096
97err_reset:
98 for (; n > 0; n--)
99 reset_free(priv->devices.reset + n - 1);
100
101 return ret;
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200102}
103
104static const struct udevice_id sandbox_scmi_devices_ids[] = {
105 { .compatible = "sandbox,scmi-devices" },
106 { }
107};
108
109U_BOOT_DRIVER(sandbox_scmi_devices) = {
110 .name = "sandbox-scmi_devices",
111 .id = UCLASS_MISC,
112 .of_match = sandbox_scmi_devices_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700113 .priv_auto = sizeof(struct sandbox_scmi_device_priv),
Etienne Carriere8b9b6892020-09-09 18:44:07 +0200114 .remove = sandbox_scmi_devices_remove,
Etienne Carriere2d94c08fa2020-09-09 18:44:05 +0200115 .probe = sandbox_scmi_devices_probe,
116};