blob: 73a7e0a844aa07695ea09d8b877758dd1b4615a1 [file] [log] [blame]
Etienne Carriere1e359132020-09-09 18:44:02 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Linaro Limited.
4 */
5
Patrick Delaunay9f5e4aa2021-02-24 11:19:44 +01006#define LOG_CATEGORY UCLASS_SCMI_AGENT
7
Etienne Carriere1e359132020-09-09 18:44:02 +02008#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <scmi_agent.h>
12#include <scmi_agent-uclass.h>
13#include <dm/devres.h>
Patrick Delaunay1b9ad512021-02-24 11:19:43 +010014#include <dm/device_compat.h>
Etienne Carriere1e359132020-09-09 18:44:02 +020015#include <dm/device-internal.h>
16#include <linux/arm-smccc.h>
17#include <linux/compat.h>
18
19#include "smt.h"
20
21#define SMCCC_RET_NOT_SUPPORTED ((unsigned long)-1)
22
23/**
24 * struct scmi_smccc_channel - Description of an SCMI SMCCC transport
25 * @func_id: SMCCC function ID used by the SCMI transport
26 * @smt: Shared memory buffer
27 */
28struct scmi_smccc_channel {
29 ulong func_id;
30 struct scmi_smt smt;
31};
32
Etienne Carriere57b812f2022-05-31 18:09:23 +020033/**
34 * struct scmi_channel - Channel instance referenced in SCMI drivers
35 * @ref: Reference to local channel instance
36 **/
37struct scmi_channel {
38 struct scmi_smccc_channel ref;
39};
40
Etienne Carriere85dc5822022-05-31 18:09:19 +020041static int scmi_smccc_process_msg(struct udevice *dev,
42 struct scmi_channel *channel,
43 struct scmi_msg *msg)
Etienne Carriere1e359132020-09-09 18:44:02 +020044{
Etienne Carriere3de5aef2021-11-08 08:56:10 +010045 struct scmi_smccc_channel *chan = dev_get_plat(dev);
Etienne Carriere1e359132020-09-09 18:44:02 +020046 struct arm_smccc_res res;
47 int ret;
48
Etienne Carriere57b812f2022-05-31 18:09:23 +020049 /* Support SCMI drivers upgraded to of_get_channel operator */
50 if (channel)
51 chan = &channel->ref;
52
Etienne Carriere1e359132020-09-09 18:44:02 +020053 ret = scmi_write_msg_to_smt(dev, &chan->smt, msg);
54 if (ret)
55 return ret;
56
57 arm_smccc_smc(chan->func_id, 0, 0, 0, 0, 0, 0, 0, &res);
58 if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
59 ret = -ENXIO;
60 else
61 ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
62
63 scmi_clear_smt_channel(&chan->smt);
64
65 return ret;
66}
67
Etienne Carriere57b812f2022-05-31 18:09:23 +020068static int setup_channel(struct udevice *dev, struct scmi_smccc_channel *chan)
Etienne Carriere1e359132020-09-09 18:44:02 +020069{
Etienne Carriere1e359132020-09-09 18:44:02 +020070 u32 func_id;
71 int ret;
72
73 if (dev_read_u32(dev, "arm,smc-id", &func_id)) {
74 dev_err(dev, "Missing property func-id\n");
75 return -EINVAL;
76 }
77
78 chan->func_id = func_id;
79
80 ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
Etienne Carriere32190a92021-11-08 08:56:11 +010081 if (ret)
Etienne Carriere1e359132020-09-09 18:44:02 +020082 dev_err(dev, "Failed to get smt resources: %d\n", ret);
Etienne Carriere1e359132020-09-09 18:44:02 +020083
Etienne Carriere32190a92021-11-08 08:56:11 +010084 return ret;
Etienne Carriere1e359132020-09-09 18:44:02 +020085}
86
Etienne Carriere57b812f2022-05-31 18:09:23 +020087static int scmi_smccc_get_channel(struct udevice *dev,
88 struct scmi_channel **channel)
89{
90 struct scmi_smccc_channel *base_chan = dev_get_plat(dev->parent);
91 struct scmi_smccc_channel *chan;
92 u32 func_id;
93 int ret;
94
95 if (dev_read_u32(dev, "arm,smc-id", &func_id)) {
96 /* Uses agent base channel */
97 *channel = container_of(base_chan, struct scmi_channel, ref);
98
99 return 0;
100 }
101
102 /* Setup a dedicated channel */
103 chan = calloc(1, sizeof(*chan));
104 if (!chan)
105 return -ENOMEM;
106
107 ret = setup_channel(dev, chan);
108 if (ret) {
109 free(chan);
110 return ret;
111 }
112
113 *channel = container_of(chan, struct scmi_channel, ref);
114
115 return 0;
116}
117
118static int scmi_smccc_of_to_plat(struct udevice *dev)
119{
120 struct scmi_smccc_channel *chan = dev_get_plat(dev);
121
122 return setup_channel(dev, chan);
123}
124
Etienne Carriere1e359132020-09-09 18:44:02 +0200125static const struct udevice_id scmi_smccc_ids[] = {
126 { .compatible = "arm,scmi-smc" },
127 { }
128};
129
130static const struct scmi_agent_ops scmi_smccc_ops = {
Etienne Carriere57b812f2022-05-31 18:09:23 +0200131 .of_get_channel = scmi_smccc_get_channel,
Etienne Carriere1e359132020-09-09 18:44:02 +0200132 .process_msg = scmi_smccc_process_msg,
133};
134
135U_BOOT_DRIVER(scmi_smccc) = {
136 .name = "scmi-over-smccc",
137 .id = UCLASS_SCMI_AGENT,
138 .of_match = scmi_smccc_ids,
Etienne Carriere3de5aef2021-11-08 08:56:10 +0100139 .plat_auto = sizeof(struct scmi_smccc_channel),
140 .of_to_plat = scmi_smccc_of_to_plat,
Etienne Carriere1e359132020-09-09 18:44:02 +0200141 .ops = &scmi_smccc_ops,
142};