2020-09-09 16:44:00 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Linaro Limited.
|
|
|
|
*/
|
|
|
|
|
2021-02-24 10:19:44 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_SCMI_AGENT
|
|
|
|
|
2020-09-09 16:44:00 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2023-10-11 10:06:54 +00:00
|
|
|
#include <scmi_agent.h>
|
2020-09-09 16:44:00 +00:00
|
|
|
#include <scmi_agent-uclass.h>
|
|
|
|
#include <scmi_protocols.h>
|
2021-02-24 10:19:45 +00:00
|
|
|
#include <dm/device_compat.h>
|
2020-09-09 16:44:00 +00:00
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <linux/compat.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct error_code - Helper structure for SCMI error code conversion
|
|
|
|
* @scmi: SCMI error code
|
|
|
|
* @errno: Related standard error number
|
|
|
|
*/
|
|
|
|
struct error_code {
|
|
|
|
int scmi;
|
|
|
|
int errno;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct error_code scmi_linux_errmap[] = {
|
|
|
|
{ .scmi = SCMI_NOT_SUPPORTED, .errno = -EOPNOTSUPP, },
|
|
|
|
{ .scmi = SCMI_INVALID_PARAMETERS, .errno = -EINVAL, },
|
|
|
|
{ .scmi = SCMI_DENIED, .errno = -EACCES, },
|
|
|
|
{ .scmi = SCMI_NOT_FOUND, .errno = -ENOENT, },
|
|
|
|
{ .scmi = SCMI_OUT_OF_RANGE, .errno = -ERANGE, },
|
|
|
|
{ .scmi = SCMI_BUSY, .errno = -EBUSY, },
|
|
|
|
{ .scmi = SCMI_COMMS_ERROR, .errno = -ECOMM, },
|
|
|
|
{ .scmi = SCMI_GENERIC_ERROR, .errno = -EIO, },
|
|
|
|
{ .scmi = SCMI_HARDWARE_ERROR, .errno = -EREMOTEIO, },
|
|
|
|
{ .scmi = SCMI_PROTOCOL_ERROR, .errno = -EPROTO, },
|
|
|
|
};
|
|
|
|
|
2023-10-11 10:06:58 +00:00
|
|
|
struct udevice *scmi_get_protocol(struct udevice *dev,
|
|
|
|
enum scmi_std_protocol id)
|
|
|
|
{
|
|
|
|
struct scmi_agent_priv *priv;
|
|
|
|
struct udevice *proto;
|
|
|
|
|
|
|
|
priv = dev_get_uclass_plat(dev);
|
|
|
|
if (!priv) {
|
|
|
|
dev_err(dev, "No priv data found\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case SCMI_PROTOCOL_ID_CLOCK:
|
|
|
|
proto = priv->clock_dev;
|
|
|
|
break;
|
|
|
|
case SCMI_PROTOCOL_ID_RESET_DOMAIN:
|
|
|
|
proto = priv->resetdom_dev;
|
|
|
|
break;
|
|
|
|
case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
|
|
|
|
proto = priv->voltagedom_dev;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(dev, "Protocol not supported\n");
|
|
|
|
proto = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (proto && device_probe(proto))
|
|
|
|
dev_err(dev, "Probe failed\n");
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scmi_add_protocol - add protocol to agent
|
|
|
|
* @dev: SCMI agent device
|
|
|
|
* @proto_id: SCMI protocol ID
|
|
|
|
* @proto: SCMI protocol device
|
|
|
|
*
|
|
|
|
* Associate the protocol instance, @proto, to the agent, @dev,
|
|
|
|
* for later use.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, error code on failure
|
|
|
|
*/
|
|
|
|
static int scmi_add_protocol(struct udevice *dev,
|
|
|
|
enum scmi_std_protocol proto_id,
|
|
|
|
struct udevice *proto)
|
|
|
|
{
|
|
|
|
struct scmi_agent_priv *priv;
|
|
|
|
|
|
|
|
priv = dev_get_uclass_plat(dev);
|
|
|
|
if (!priv) {
|
|
|
|
dev_err(dev, "No priv data found\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (proto_id) {
|
|
|
|
case SCMI_PROTOCOL_ID_CLOCK:
|
|
|
|
priv->clock_dev = proto;
|
|
|
|
break;
|
|
|
|
case SCMI_PROTOCOL_ID_RESET_DOMAIN:
|
|
|
|
priv->resetdom_dev = proto;
|
|
|
|
break;
|
|
|
|
case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
|
|
|
|
priv->voltagedom_dev = proto;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(dev, "Protocol not supported\n");
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-09 16:44:00 +00:00
|
|
|
int scmi_to_linux_errno(s32 scmi_code)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!scmi_code)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (n = 0; n < ARRAY_SIZE(scmi_linux_errmap); n++)
|
|
|
|
if (scmi_code == scmi_linux_errmap[n].scmi)
|
2023-06-13 01:30:45 +00:00
|
|
|
return scmi_linux_errmap[n].errno;
|
2020-09-09 16:44:00 +00:00
|
|
|
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
static struct udevice *find_scmi_protocol_device(struct udevice *dev)
|
2022-05-31 16:09:20 +00:00
|
|
|
{
|
2023-10-11 10:06:54 +00:00
|
|
|
struct udevice *parent = NULL, *protocol;
|
2022-05-31 16:09:20 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
for (protocol = dev; protocol; protocol = parent) {
|
|
|
|
parent = dev_get_parent(protocol);
|
|
|
|
if (!parent ||
|
|
|
|
device_get_uclass_id(parent) == UCLASS_SCMI_AGENT)
|
|
|
|
break;
|
|
|
|
}
|
2022-05-31 16:09:20 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
if (!parent) {
|
2022-05-31 16:09:20 +00:00
|
|
|
dev_err(dev, "Invalid SCMI device, agent not found\n");
|
2023-10-11 10:06:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-05-31 16:09:20 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
return protocol;
|
2022-05-31 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 16:44:00 +00:00
|
|
|
static const struct scmi_agent_ops *transport_dev_ops(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return (const struct scmi_agent_ops *)dev->driver->ops;
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
/**
|
|
|
|
* scmi_of_get_channel() - Get SCMI channel handle
|
|
|
|
*
|
|
|
|
* @dev: SCMI agent device
|
|
|
|
* @channel: Output reference to the SCMI channel upon success
|
|
|
|
*
|
|
|
|
* On return, @channel will be set.
|
|
|
|
* Return 0 on success and a negative errno on failure
|
|
|
|
*/
|
2023-10-11 10:06:55 +00:00
|
|
|
static int scmi_of_get_channel(struct udevice *dev, struct udevice *protocol,
|
|
|
|
struct scmi_channel **channel)
|
2022-05-31 16:09:21 +00:00
|
|
|
{
|
2023-10-11 10:06:54 +00:00
|
|
|
const struct scmi_agent_ops *ops;
|
2022-05-31 16:09:21 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
ops = transport_dev_ops(dev);
|
|
|
|
if (ops->of_get_channel)
|
2023-10-11 10:06:55 +00:00
|
|
|
return ops->of_get_channel(dev, protocol, channel);
|
2023-10-11 10:06:54 +00:00
|
|
|
else
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int devm_scmi_of_get_channel(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct udevice *protocol;
|
|
|
|
struct scmi_agent_proto_priv *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
protocol = find_scmi_protocol_device(dev);
|
|
|
|
if (!protocol)
|
2022-05-31 16:09:21 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
priv = dev_get_parent_priv(protocol);
|
2023-10-11 10:06:55 +00:00
|
|
|
ret = scmi_of_get_channel(protocol->parent, protocol, &priv->channel);
|
2023-10-11 10:06:54 +00:00
|
|
|
if (ret == -EPROTONOSUPPORT) {
|
|
|
|
/* Drivers without a get_channel operator don't need a channel ref */
|
|
|
|
priv->channel = NULL;
|
2022-05-31 16:09:21 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-05-31 16:09:21 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
return ret;
|
2022-05-31 16:09:21 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
/**
|
|
|
|
* scmi_process_msg() - Send and process an SCMI message
|
|
|
|
*
|
|
|
|
* Send a message to an SCMI server.
|
|
|
|
* Caller sets scmi_msg::out_msg_sz to the output message buffer size.
|
|
|
|
*
|
|
|
|
* @dev: SCMI agent device
|
|
|
|
* @channel: Communication channel for the device
|
|
|
|
* @msg: Message structure reference
|
|
|
|
*
|
|
|
|
* On return, scmi_msg::out_msg_sz stores the response payload size.
|
|
|
|
* Return: 0 on success and a negative errno on failure
|
|
|
|
*/
|
|
|
|
static int scmi_process_msg(struct udevice *dev, struct scmi_channel *channel,
|
|
|
|
struct scmi_msg *msg)
|
2020-09-09 16:44:00 +00:00
|
|
|
{
|
2022-02-21 08:22:40 +00:00
|
|
|
const struct scmi_agent_ops *ops;
|
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
ops = transport_dev_ops(dev);
|
|
|
|
if (ops->process_msg)
|
|
|
|
return ops->process_msg(dev, channel, msg);
|
|
|
|
else
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
2022-02-21 08:22:40 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
int devm_scmi_process_msg(struct udevice *dev, struct scmi_msg *msg)
|
|
|
|
{
|
|
|
|
struct udevice *protocol;
|
|
|
|
struct scmi_agent_proto_priv *priv;
|
2020-09-09 16:44:00 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
protocol = find_scmi_protocol_device(dev);
|
|
|
|
if (!protocol)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
priv = dev_get_parent_priv(protocol);
|
2020-09-09 16:44:00 +00:00
|
|
|
|
2023-10-11 10:06:54 +00:00
|
|
|
return scmi_process_msg(protocol->parent, priv->channel, msg);
|
2020-09-09 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 10:06:57 +00:00
|
|
|
/*
|
|
|
|
* SCMI agent devices binds devices of various uclasses depending on
|
|
|
|
* the FDT description. scmi_bind_protocol() is a generic bind sequence
|
|
|
|
* called by the uclass at bind stage, that is uclass post_bind.
|
|
|
|
*/
|
|
|
|
static int scmi_bind_protocols(struct udevice *dev)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
ofnode node;
|
|
|
|
const char *name;
|
2023-10-11 10:06:58 +00:00
|
|
|
struct driver *drv;
|
|
|
|
struct udevice *proto;
|
2023-10-11 10:06:57 +00:00
|
|
|
|
|
|
|
dev_for_each_subnode(node, dev) {
|
|
|
|
u32 protocol_id;
|
|
|
|
|
|
|
|
if (!ofnode_is_enabled(node))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ofnode_read_u32(node, "reg", &protocol_id))
|
|
|
|
continue;
|
|
|
|
|
2023-10-11 10:06:58 +00:00
|
|
|
drv = NULL;
|
2023-10-11 10:06:57 +00:00
|
|
|
name = ofnode_get_name(node);
|
|
|
|
switch (protocol_id) {
|
|
|
|
case SCMI_PROTOCOL_ID_CLOCK:
|
|
|
|
if (CONFIG_IS_ENABLED(CLK_SCMI))
|
|
|
|
drv = DM_DRIVER_GET(scmi_clock);
|
|
|
|
break;
|
|
|
|
case SCMI_PROTOCOL_ID_RESET_DOMAIN:
|
|
|
|
if (IS_ENABLED(CONFIG_RESET_SCMI))
|
|
|
|
drv = DM_DRIVER_GET(scmi_reset_domain);
|
|
|
|
break;
|
|
|
|
case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
|
|
|
|
if (IS_ENABLED(CONFIG_DM_REGULATOR_SCMI)) {
|
|
|
|
node = ofnode_find_subnode(node, "regulators");
|
|
|
|
if (!ofnode_valid(node)) {
|
|
|
|
dev_err(dev, "no regulators node\n");
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
drv = DM_DRIVER_GET(scmi_voltage_domain);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!drv) {
|
|
|
|
dev_dbg(dev, "Ignore unsupported SCMI protocol %#x\n",
|
|
|
|
protocol_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:06:58 +00:00
|
|
|
ret = device_bind(dev, drv, name, NULL, node, &proto);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to bind %s protocol\n", drv->name);
|
2023-10-11 10:06:57 +00:00
|
|
|
break;
|
2023-10-11 10:06:58 +00:00
|
|
|
}
|
|
|
|
ret = scmi_add_protocol(dev, protocol_id, proto);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to add protocol: %s, ret: %d\n",
|
|
|
|
proto->name, ret);
|
|
|
|
break;
|
|
|
|
}
|
2023-10-11 10:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-09 16:44:00 +00:00
|
|
|
UCLASS_DRIVER(scmi_agent) = {
|
|
|
|
.id = UCLASS_SCMI_AGENT,
|
|
|
|
.name = "scmi_agent",
|
|
|
|
.post_bind = scmi_bind_protocols,
|
2023-10-11 10:06:58 +00:00
|
|
|
.per_device_plat_auto = sizeof(struct scmi_agent_priv),
|
2023-10-11 10:06:54 +00:00
|
|
|
.per_child_auto = sizeof(struct scmi_agent_proto_priv),
|
2020-09-09 16:44:00 +00:00
|
|
|
};
|