blob: 1fa1e9eef966b5e351586662c298a5dd7243689a [file] [log] [blame]
Etienne Carriere02fd1262020-09-09 18:44:00 +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_SCMI_AGENT
7
Etienne Carriere02fd1262020-09-09 18:44:00 +02008#include <common.h>
9#include <dm.h>
10#include <errno.h>
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090011#include <scmi_agent.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +020012#include <scmi_agent-uclass.h>
13#include <scmi_protocols.h>
Patrick Delaunayd4c9f682021-02-24 11:19:45 +010014#include <dm/device_compat.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +020015#include <dm/device-internal.h>
16#include <linux/compat.h>
17
18/**
19 * struct error_code - Helper structure for SCMI error code conversion
20 * @scmi: SCMI error code
21 * @errno: Related standard error number
22 */
23struct error_code {
24 int scmi;
25 int errno;
26};
27
28static const struct error_code scmi_linux_errmap[] = {
29 { .scmi = SCMI_NOT_SUPPORTED, .errno = -EOPNOTSUPP, },
30 { .scmi = SCMI_INVALID_PARAMETERS, .errno = -EINVAL, },
31 { .scmi = SCMI_DENIED, .errno = -EACCES, },
32 { .scmi = SCMI_NOT_FOUND, .errno = -ENOENT, },
33 { .scmi = SCMI_OUT_OF_RANGE, .errno = -ERANGE, },
34 { .scmi = SCMI_BUSY, .errno = -EBUSY, },
35 { .scmi = SCMI_COMMS_ERROR, .errno = -ECOMM, },
36 { .scmi = SCMI_GENERIC_ERROR, .errno = -EIO, },
37 { .scmi = SCMI_HARDWARE_ERROR, .errno = -EREMOTEIO, },
38 { .scmi = SCMI_PROTOCOL_ERROR, .errno = -EPROTO, },
39};
40
41int scmi_to_linux_errno(s32 scmi_code)
42{
43 int n;
44
45 if (!scmi_code)
46 return 0;
47
48 for (n = 0; n < ARRAY_SIZE(scmi_linux_errmap); n++)
49 if (scmi_code == scmi_linux_errmap[n].scmi)
AKASHI Takahiroc9371542023-06-13 10:30:45 +090050 return scmi_linux_errmap[n].errno;
Etienne Carriere02fd1262020-09-09 18:44:00 +020051
52 return -EPROTO;
53}
54
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090055static struct udevice *find_scmi_protocol_device(struct udevice *dev)
Etienne Carriere2ee64582022-05-31 18:09:20 +020056{
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090057 struct udevice *parent = NULL, *protocol;
Etienne Carriere2ee64582022-05-31 18:09:20 +020058
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090059 for (protocol = dev; protocol; protocol = parent) {
60 parent = dev_get_parent(protocol);
61 if (!parent ||
62 device_get_uclass_id(parent) == UCLASS_SCMI_AGENT)
63 break;
64 }
Etienne Carriere2ee64582022-05-31 18:09:20 +020065
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090066 if (!parent) {
Etienne Carriere2ee64582022-05-31 18:09:20 +020067 dev_err(dev, "Invalid SCMI device, agent not found\n");
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090068 return NULL;
69 }
Etienne Carriere2ee64582022-05-31 18:09:20 +020070
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090071 return protocol;
Etienne Carriere2ee64582022-05-31 18:09:20 +020072}
73
Etienne Carriere02fd1262020-09-09 18:44:00 +020074static const struct scmi_agent_ops *transport_dev_ops(struct udevice *dev)
75{
76 return (const struct scmi_agent_ops *)dev->driver->ops;
77}
78
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090079/**
80 * scmi_of_get_channel() - Get SCMI channel handle
81 *
82 * @dev: SCMI agent device
83 * @channel: Output reference to the SCMI channel upon success
84 *
85 * On return, @channel will be set.
86 * Return 0 on success and a negative errno on failure
87 */
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +090088static int scmi_of_get_channel(struct udevice *dev, struct udevice *protocol,
89 struct scmi_channel **channel)
Etienne Carriere187cea12022-05-31 18:09:21 +020090{
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090091 const struct scmi_agent_ops *ops;
Etienne Carriere187cea12022-05-31 18:09:21 +020092
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090093 ops = transport_dev_ops(dev);
94 if (ops->of_get_channel)
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +090095 return ops->of_get_channel(dev, protocol, channel);
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +090096 else
97 return -EPROTONOSUPPORT;
98}
99
100int devm_scmi_of_get_channel(struct udevice *dev)
101{
102 struct udevice *protocol;
103 struct scmi_agent_proto_priv *priv;
104 int ret;
105
106 protocol = find_scmi_protocol_device(dev);
107 if (!protocol)
Etienne Carriere187cea12022-05-31 18:09:21 +0200108 return -ENODEV;
109
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900110 priv = dev_get_parent_priv(protocol);
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +0900111 ret = scmi_of_get_channel(protocol->parent, protocol, &priv->channel);
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900112 if (ret == -EPROTONOSUPPORT) {
113 /* Drivers without a get_channel operator don't need a channel ref */
114 priv->channel = NULL;
Etienne Carriere187cea12022-05-31 18:09:21 +0200115
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900116 return 0;
117 }
Etienne Carriere187cea12022-05-31 18:09:21 +0200118
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900119 return ret;
Etienne Carriere187cea12022-05-31 18:09:21 +0200120}
121
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900122/**
123 * scmi_process_msg() - Send and process an SCMI message
124 *
125 * Send a message to an SCMI server.
126 * Caller sets scmi_msg::out_msg_sz to the output message buffer size.
127 *
128 * @dev: SCMI agent device
129 * @channel: Communication channel for the device
130 * @msg: Message structure reference
131 *
132 * On return, scmi_msg::out_msg_sz stores the response payload size.
133 * Return: 0 on success and a negative errno on failure
134 */
135static int scmi_process_msg(struct udevice *dev, struct scmi_channel *channel,
136 struct scmi_msg *msg)
Etienne Carriere02fd1262020-09-09 18:44:00 +0200137{
Etienne Carriere2f26c042022-02-21 09:22:40 +0100138 const struct scmi_agent_ops *ops;
Etienne Carriere2f26c042022-02-21 09:22:40 +0100139
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900140 ops = transport_dev_ops(dev);
141 if (ops->process_msg)
142 return ops->process_msg(dev, channel, msg);
143 else
144 return -EPROTONOSUPPORT;
145}
Etienne Carriere2f26c042022-02-21 09:22:40 +0100146
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900147int devm_scmi_process_msg(struct udevice *dev, struct scmi_msg *msg)
148{
149 struct udevice *protocol;
150 struct scmi_agent_proto_priv *priv;
Etienne Carriere02fd1262020-09-09 18:44:00 +0200151
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900152 protocol = find_scmi_protocol_device(dev);
153 if (!protocol)
154 return -ENODEV;
155
156 priv = dev_get_parent_priv(protocol);
Etienne Carriere02fd1262020-09-09 18:44:00 +0200157
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900158 return scmi_process_msg(protocol->parent, priv->channel, msg);
Etienne Carriere02fd1262020-09-09 18:44:00 +0200159}
160
AKASHI Takahiro6d379152023-10-11 19:06:57 +0900161/*
162 * SCMI agent devices binds devices of various uclasses depending on
163 * the FDT description. scmi_bind_protocol() is a generic bind sequence
164 * called by the uclass at bind stage, that is uclass post_bind.
165 */
166static int scmi_bind_protocols(struct udevice *dev)
167{
168 int ret = 0;
169 ofnode node;
170 const char *name;
171
172 dev_for_each_subnode(node, dev) {
173 struct driver *drv = NULL;
174 u32 protocol_id;
175
176 if (!ofnode_is_enabled(node))
177 continue;
178
179 if (ofnode_read_u32(node, "reg", &protocol_id))
180 continue;
181
182 name = ofnode_get_name(node);
183 switch (protocol_id) {
184 case SCMI_PROTOCOL_ID_CLOCK:
185 if (CONFIG_IS_ENABLED(CLK_SCMI))
186 drv = DM_DRIVER_GET(scmi_clock);
187 break;
188 case SCMI_PROTOCOL_ID_RESET_DOMAIN:
189 if (IS_ENABLED(CONFIG_RESET_SCMI))
190 drv = DM_DRIVER_GET(scmi_reset_domain);
191 break;
192 case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
193 if (IS_ENABLED(CONFIG_DM_REGULATOR_SCMI)) {
194 node = ofnode_find_subnode(node, "regulators");
195 if (!ofnode_valid(node)) {
196 dev_err(dev, "no regulators node\n");
197 return -ENXIO;
198 }
199 drv = DM_DRIVER_GET(scmi_voltage_domain);
200 }
201 break;
202 default:
203 break;
204 }
205
206 if (!drv) {
207 dev_dbg(dev, "Ignore unsupported SCMI protocol %#x\n",
208 protocol_id);
209 continue;
210 }
211
212 ret = device_bind(dev, drv, name, NULL, node, NULL);
213 if (ret)
214 break;
215 }
216
217 return ret;
218}
219
Etienne Carriere02fd1262020-09-09 18:44:00 +0200220UCLASS_DRIVER(scmi_agent) = {
221 .id = UCLASS_SCMI_AGENT,
222 .name = "scmi_agent",
223 .post_bind = scmi_bind_protocols,
AKASHI Takahiro7b3aa372023-10-11 19:06:54 +0900224 .per_child_auto = sizeof(struct scmi_agent_proto_priv),
Etienne Carriere02fd1262020-09-09 18:44:00 +0200225};