2020-09-09 16:44:01 +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:01 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <mailbox.h>
|
|
|
|
#include <scmi_agent.h>
|
|
|
|
#include <scmi_agent-uclass.h>
|
2021-02-24 10:19:45 +00:00
|
|
|
#include <dm/device_compat.h>
|
2020-09-09 16:44:01 +00:00
|
|
|
#include <dm/devres.h>
|
|
|
|
#include <linux/compat.h>
|
|
|
|
|
|
|
|
#include "smt.h"
|
|
|
|
|
|
|
|
#define TIMEOUT_US_10MS 10000
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct scmi_mbox_channel - Description of an SCMI mailbox transport
|
|
|
|
* @smt: Shared memory buffer
|
|
|
|
* @mbox: Mailbox channel description
|
|
|
|
* @timeout_us: Timeout in microseconds for the mailbox transfer
|
|
|
|
*/
|
|
|
|
struct scmi_mbox_channel {
|
|
|
|
struct scmi_smt smt;
|
|
|
|
struct mbox_chan mbox;
|
|
|
|
ulong timeout_us;
|
|
|
|
};
|
|
|
|
|
2022-05-31 16:09:22 +00:00
|
|
|
/**
|
|
|
|
* struct scmi_channel - Channel instance referenced in SCMI drivers
|
|
|
|
* @ref: Reference to local channel instance
|
|
|
|
**/
|
|
|
|
struct scmi_channel {
|
|
|
|
struct scmi_mbox_channel ref;
|
|
|
|
};
|
|
|
|
|
2022-05-31 16:09:19 +00:00
|
|
|
static int scmi_mbox_process_msg(struct udevice *dev,
|
|
|
|
struct scmi_channel *channel,
|
|
|
|
struct scmi_msg *msg)
|
2020-09-09 16:44:01 +00:00
|
|
|
{
|
2022-05-31 16:09:29 +00:00
|
|
|
struct scmi_mbox_channel *chan = &channel->ref;
|
2020-09-09 16:44:01 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = scmi_write_msg_to_smt(dev, &chan->smt, msg);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Give shm addr to mbox in case it is meaningful */
|
|
|
|
ret = mbox_send(&chan->mbox, chan->smt.buf);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Message send failed: %d\n", ret);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Receive the response */
|
|
|
|
ret = mbox_recv(&chan->mbox, chan->smt.buf, chan->timeout_us);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Response failed: %d, abort\n", ret);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
|
|
|
|
|
|
|
|
out:
|
|
|
|
scmi_clear_smt_channel(&chan->smt);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-05-31 16:09:22 +00:00
|
|
|
static int setup_channel(struct udevice *dev, struct scmi_mbox_channel *chan)
|
2020-09-09 16:44:01 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mbox_get_by_index(dev, 0, &chan->mbox);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Failed to find mailbox: %d\n", ret);
|
2021-11-08 07:56:08 +00:00
|
|
|
return ret;
|
2020-09-09 16:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
|
2022-05-31 16:09:22 +00:00
|
|
|
if (ret) {
|
2020-09-09 16:44:01 +00:00
|
|
|
dev_err(dev, "Failed to get shm resources: %d\n", ret);
|
2022-05-31 16:09:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2020-09-09 16:44:01 +00:00
|
|
|
|
2022-05-31 16:09:22 +00:00
|
|
|
chan->timeout_us = TIMEOUT_US_10MS;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int scmi_mbox_get_channel(struct udevice *dev,
|
|
|
|
struct scmi_channel **channel)
|
|
|
|
{
|
|
|
|
struct scmi_mbox_channel *base_chan = dev_get_plat(dev->parent);
|
|
|
|
struct scmi_mbox_channel *chan;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!dev_read_prop(dev, "shmem", NULL)) {
|
|
|
|
/* Uses agent base channel */
|
|
|
|
*channel = container_of(base_chan, struct scmi_channel, ref);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
chan = calloc(1, sizeof(*chan));
|
|
|
|
if (!chan)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Setup a dedicated channel for the protocol */
|
|
|
|
ret = setup_channel(dev, chan);
|
|
|
|
if (ret) {
|
|
|
|
free(chan);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*channel = (void *)chan;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int scmi_mbox_of_to_plat(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct scmi_mbox_channel *chan = dev_get_plat(dev);
|
|
|
|
|
|
|
|
return setup_channel(dev, chan);
|
2020-09-09 16:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct udevice_id scmi_mbox_ids[] = {
|
|
|
|
{ .compatible = "arm,scmi" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct scmi_agent_ops scmi_mbox_ops = {
|
2022-05-31 16:09:22 +00:00
|
|
|
.of_get_channel = scmi_mbox_get_channel,
|
2020-09-09 16:44:01 +00:00
|
|
|
.process_msg = scmi_mbox_process_msg,
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(scmi_mbox) = {
|
|
|
|
.name = "scmi-over-mailbox",
|
|
|
|
.id = UCLASS_SCMI_AGENT,
|
|
|
|
.of_match = scmi_mbox_ids,
|
2021-11-08 07:56:09 +00:00
|
|
|
.plat_auto = sizeof(struct scmi_mbox_channel),
|
|
|
|
.of_to_plat = scmi_mbox_of_to_plat,
|
2020-09-09 16:44:01 +00:00
|
|
|
.ops = &scmi_mbox_ops,
|
|
|
|
};
|