1*1afcdfc6SEtienne Carriere // SPDX-License-Identifier: GPL-2.0+
2*1afcdfc6SEtienne Carriere /*
3*1afcdfc6SEtienne Carriere * Copyright (C) 2020, Linaro Limited
4*1afcdfc6SEtienne Carriere */
5*1afcdfc6SEtienne Carriere
6*1afcdfc6SEtienne Carriere #include <common.h>
7*1afcdfc6SEtienne Carriere #include <dm.h>
8*1afcdfc6SEtienne Carriere #include <malloc.h>
9*1afcdfc6SEtienne Carriere #include <scmi_agent.h>
10*1afcdfc6SEtienne Carriere #include <scmi_agent-uclass.h>
11*1afcdfc6SEtienne Carriere #include <scmi_protocols.h>
12*1afcdfc6SEtienne Carriere #include <asm/io.h>
13*1afcdfc6SEtienne Carriere #include <asm/scmi_test.h>
14*1afcdfc6SEtienne Carriere #include <dm/device_compat.h>
15*1afcdfc6SEtienne Carriere
16*1afcdfc6SEtienne Carriere /*
17*1afcdfc6SEtienne Carriere * The sandbox SCMI agent driver simulates to some extend a SCMI message
18*1afcdfc6SEtienne Carriere * processing. It simulates few of the SCMI services for some of the
19*1afcdfc6SEtienne Carriere * SCMI protocols embedded in U-Boot. Currently none.
20*1afcdfc6SEtienne Carriere *
21*1afcdfc6SEtienne Carriere * This driver simulates 2 SCMI agents for test purpose.
22*1afcdfc6SEtienne Carriere *
23*1afcdfc6SEtienne Carriere * This Driver exports sandbox_scmi_service_ct() for the test sequence to
24*1afcdfc6SEtienne Carriere * get the state of the simulated services (clock state, rate, ...) and
25*1afcdfc6SEtienne Carriere * check back-end device state reflects the request send through the
26*1afcdfc6SEtienne Carriere * various uclass devices, currently nothing.
27*1afcdfc6SEtienne Carriere */
28*1afcdfc6SEtienne Carriere
29*1afcdfc6SEtienne Carriere #define SANDBOX_SCMI_AGENT_COUNT 2
30*1afcdfc6SEtienne Carriere
31*1afcdfc6SEtienne Carriere /* The list saves to simulted end devices references for test purpose */
32*1afcdfc6SEtienne Carriere struct sandbox_scmi_agent *sandbox_scmi_agent_list[SANDBOX_SCMI_AGENT_COUNT];
33*1afcdfc6SEtienne Carriere
34*1afcdfc6SEtienne Carriere static struct sandbox_scmi_service sandbox_scmi_service_state = {
35*1afcdfc6SEtienne Carriere .agent = sandbox_scmi_agent_list,
36*1afcdfc6SEtienne Carriere .agent_count = SANDBOX_SCMI_AGENT_COUNT,
37*1afcdfc6SEtienne Carriere };
38*1afcdfc6SEtienne Carriere
sandbox_scmi_service_ctx(void)39*1afcdfc6SEtienne Carriere struct sandbox_scmi_service *sandbox_scmi_service_ctx(void)
40*1afcdfc6SEtienne Carriere {
41*1afcdfc6SEtienne Carriere return &sandbox_scmi_service_state;
42*1afcdfc6SEtienne Carriere }
43*1afcdfc6SEtienne Carriere
debug_print_agent_state(struct udevice * dev,char * str)44*1afcdfc6SEtienne Carriere static void debug_print_agent_state(struct udevice *dev, char *str)
45*1afcdfc6SEtienne Carriere {
46*1afcdfc6SEtienne Carriere struct sandbox_scmi_agent *agent = dev_get_priv(dev);
47*1afcdfc6SEtienne Carriere
48*1afcdfc6SEtienne Carriere dev_dbg(dev, "Dump sandbox_scmi_agent %u: %s\n", agent->idx, str);
49*1afcdfc6SEtienne Carriere };
50*1afcdfc6SEtienne Carriere
sandbox_scmi_test_process_msg(struct udevice * dev,struct scmi_msg * msg)51*1afcdfc6SEtienne Carriere static int sandbox_scmi_test_process_msg(struct udevice *dev,
52*1afcdfc6SEtienne Carriere struct scmi_msg *msg)
53*1afcdfc6SEtienne Carriere {
54*1afcdfc6SEtienne Carriere switch (msg->protocol_id) {
55*1afcdfc6SEtienne Carriere case SCMI_PROTOCOL_ID_BASE:
56*1afcdfc6SEtienne Carriere case SCMI_PROTOCOL_ID_POWER_DOMAIN:
57*1afcdfc6SEtienne Carriere case SCMI_PROTOCOL_ID_SYSTEM:
58*1afcdfc6SEtienne Carriere case SCMI_PROTOCOL_ID_PERF:
59*1afcdfc6SEtienne Carriere case SCMI_PROTOCOL_ID_CLOCK:
60*1afcdfc6SEtienne Carriere case SCMI_PROTOCOL_ID_SENSOR:
61*1afcdfc6SEtienne Carriere case SCMI_PROTOCOL_ID_RESET_DOMAIN:
62*1afcdfc6SEtienne Carriere *(u32 *)msg->out_msg = SCMI_NOT_SUPPORTED;
63*1afcdfc6SEtienne Carriere return 0;
64*1afcdfc6SEtienne Carriere default:
65*1afcdfc6SEtienne Carriere break;
66*1afcdfc6SEtienne Carriere }
67*1afcdfc6SEtienne Carriere
68*1afcdfc6SEtienne Carriere dev_err(dev, "%s(%s): Unhandled protocol_id %#x/message_id %#x\n",
69*1afcdfc6SEtienne Carriere __func__, dev->name, msg->protocol_id, msg->message_id);
70*1afcdfc6SEtienne Carriere
71*1afcdfc6SEtienne Carriere if (msg->out_msg_sz < sizeof(u32))
72*1afcdfc6SEtienne Carriere return -EINVAL;
73*1afcdfc6SEtienne Carriere
74*1afcdfc6SEtienne Carriere /* Intentionnaly report unhandled IDs through the SCMI return code */
75*1afcdfc6SEtienne Carriere *(u32 *)msg->out_msg = SCMI_PROTOCOL_ERROR;
76*1afcdfc6SEtienne Carriere return 0;
77*1afcdfc6SEtienne Carriere }
78*1afcdfc6SEtienne Carriere
sandbox_scmi_test_remove(struct udevice * dev)79*1afcdfc6SEtienne Carriere static int sandbox_scmi_test_remove(struct udevice *dev)
80*1afcdfc6SEtienne Carriere {
81*1afcdfc6SEtienne Carriere struct sandbox_scmi_agent *agent = dev_get_priv(dev);
82*1afcdfc6SEtienne Carriere
83*1afcdfc6SEtienne Carriere debug_print_agent_state(dev, "removed");
84*1afcdfc6SEtienne Carriere
85*1afcdfc6SEtienne Carriere /* We only need to dereference the agent in the context */
86*1afcdfc6SEtienne Carriere sandbox_scmi_service_ctx()->agent[agent->idx] = NULL;
87*1afcdfc6SEtienne Carriere
88*1afcdfc6SEtienne Carriere return 0;
89*1afcdfc6SEtienne Carriere }
90*1afcdfc6SEtienne Carriere
sandbox_scmi_test_probe(struct udevice * dev)91*1afcdfc6SEtienne Carriere static int sandbox_scmi_test_probe(struct udevice *dev)
92*1afcdfc6SEtienne Carriere {
93*1afcdfc6SEtienne Carriere static const char basename[] = "sandbox-scmi-agent@";
94*1afcdfc6SEtienne Carriere struct sandbox_scmi_agent *agent = dev_get_priv(dev);
95*1afcdfc6SEtienne Carriere const size_t basename_size = sizeof(basename) - 1;
96*1afcdfc6SEtienne Carriere
97*1afcdfc6SEtienne Carriere if (strncmp(basename, dev->name, basename_size))
98*1afcdfc6SEtienne Carriere return -ENOENT;
99*1afcdfc6SEtienne Carriere
100*1afcdfc6SEtienne Carriere switch (dev->name[basename_size]) {
101*1afcdfc6SEtienne Carriere case '0':
102*1afcdfc6SEtienne Carriere *agent = (struct sandbox_scmi_agent){
103*1afcdfc6SEtienne Carriere .idx = 0,
104*1afcdfc6SEtienne Carriere };
105*1afcdfc6SEtienne Carriere break;
106*1afcdfc6SEtienne Carriere case '1':
107*1afcdfc6SEtienne Carriere *agent = (struct sandbox_scmi_agent){
108*1afcdfc6SEtienne Carriere .idx = 1,
109*1afcdfc6SEtienne Carriere };
110*1afcdfc6SEtienne Carriere break;
111*1afcdfc6SEtienne Carriere default:
112*1afcdfc6SEtienne Carriere dev_err(dev, "%s(): Unexpected agent ID %s\n",
113*1afcdfc6SEtienne Carriere __func__, dev->name + basename_size);
114*1afcdfc6SEtienne Carriere return -ENOENT;
115*1afcdfc6SEtienne Carriere }
116*1afcdfc6SEtienne Carriere
117*1afcdfc6SEtienne Carriere debug_print_agent_state(dev, "probed");
118*1afcdfc6SEtienne Carriere
119*1afcdfc6SEtienne Carriere /* Save reference for tests purpose */
120*1afcdfc6SEtienne Carriere sandbox_scmi_service_ctx()->agent[agent->idx] = agent;
121*1afcdfc6SEtienne Carriere
122*1afcdfc6SEtienne Carriere return 0;
123*1afcdfc6SEtienne Carriere };
124*1afcdfc6SEtienne Carriere
125*1afcdfc6SEtienne Carriere static const struct udevice_id sandbox_scmi_test_ids[] = {
126*1afcdfc6SEtienne Carriere { .compatible = "sandbox,scmi-agent" },
127*1afcdfc6SEtienne Carriere { }
128*1afcdfc6SEtienne Carriere };
129*1afcdfc6SEtienne Carriere
130*1afcdfc6SEtienne Carriere struct scmi_agent_ops sandbox_scmi_test_ops = {
131*1afcdfc6SEtienne Carriere .process_msg = sandbox_scmi_test_process_msg,
132*1afcdfc6SEtienne Carriere };
133*1afcdfc6SEtienne Carriere
134*1afcdfc6SEtienne Carriere U_BOOT_DRIVER(sandbox_scmi_agent) = {
135*1afcdfc6SEtienne Carriere .name = "sandbox-scmi_agent",
136*1afcdfc6SEtienne Carriere .id = UCLASS_SCMI_AGENT,
137*1afcdfc6SEtienne Carriere .of_match = sandbox_scmi_test_ids,
138*1afcdfc6SEtienne Carriere .priv_auto_alloc_size = sizeof(struct sandbox_scmi_agent),
139*1afcdfc6SEtienne Carriere .probe = sandbox_scmi_test_probe,
140*1afcdfc6SEtienne Carriere .remove = sandbox_scmi_test_remove,
141*1afcdfc6SEtienne Carriere .ops = &sandbox_scmi_test_ops,
142*1afcdfc6SEtienne Carriere };
143