2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-05-13 21:50:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, NVIDIA CORPORATION.
|
|
|
|
*/
|
|
|
|
|
2021-04-27 09:02:19 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_MAILBOX
|
|
|
|
|
2016-05-13 21:50:29 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2016-06-17 15:43:56 +00:00
|
|
|
#include <mailbox.h>
|
|
|
|
#include <mailbox-uclass.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2019-11-14 19:57:30 +00:00
|
|
|
#include <time.h>
|
2016-05-13 21:50:29 +00:00
|
|
|
|
|
|
|
static inline struct mbox_ops *mbox_dev_ops(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return (struct mbox_ops *)dev->driver->ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mbox_of_xlate_default(struct mbox_chan *chan,
|
2017-05-19 02:09:46 +00:00
|
|
|
struct ofnode_phandle_args *args)
|
2016-05-13 21:50:29 +00:00
|
|
|
{
|
|
|
|
debug("%s(chan=%p)\n", __func__, chan);
|
|
|
|
|
|
|
|
if (args->args_count != 1) {
|
2021-12-01 19:26:53 +00:00
|
|
|
debug("Invalid args_count: %d\n", args->args_count);
|
2016-05-13 21:50:29 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
chan->id = args->args[0];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbox_get_by_index(struct udevice *dev, int index, struct mbox_chan *chan)
|
|
|
|
{
|
2017-05-19 02:09:46 +00:00
|
|
|
struct ofnode_phandle_args args;
|
2016-05-13 21:50:29 +00:00
|
|
|
int ret;
|
|
|
|
struct udevice *dev_mbox;
|
|
|
|
struct mbox_ops *ops;
|
|
|
|
|
|
|
|
debug("%s(dev=%p, index=%d, chan=%p)\n", __func__, dev, index, chan);
|
|
|
|
|
2017-05-19 02:09:46 +00:00
|
|
|
ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index,
|
|
|
|
&args);
|
2016-05-13 21:50:29 +00:00
|
|
|
if (ret) {
|
2017-05-19 02:09:46 +00:00
|
|
|
debug("%s: dev_read_phandle_with_args failed: %d\n", __func__,
|
|
|
|
ret);
|
2016-05-13 21:50:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:09:46 +00:00
|
|
|
ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox);
|
2016-05-13 21:50:29 +00:00
|
|
|
if (ret) {
|
|
|
|
debug("%s: uclass_get_device_by_of_offset failed: %d\n",
|
|
|
|
__func__, ret);
|
2019-09-27 10:36:55 +00:00
|
|
|
|
|
|
|
/* Test with parent node */
|
|
|
|
ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX,
|
|
|
|
ofnode_get_parent(args.node),
|
|
|
|
&dev_mbox);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: mbox node from parent failed: %d\n",
|
|
|
|
__func__, ret);
|
|
|
|
return ret;
|
|
|
|
};
|
2016-05-13 21:50:29 +00:00
|
|
|
}
|
|
|
|
ops = mbox_dev_ops(dev_mbox);
|
|
|
|
|
|
|
|
chan->dev = dev_mbox;
|
|
|
|
if (ops->of_xlate)
|
|
|
|
ret = ops->of_xlate(chan, &args);
|
|
|
|
else
|
|
|
|
ret = mbox_of_xlate_default(chan, &args);
|
|
|
|
if (ret) {
|
|
|
|
debug("of_xlate() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:36:54 +00:00
|
|
|
if (ops->request)
|
|
|
|
ret = ops->request(chan);
|
2016-05-13 21:50:29 +00:00
|
|
|
if (ret) {
|
|
|
|
debug("ops->request() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbox_get_by_name(struct udevice *dev, const char *name,
|
|
|
|
struct mbox_chan *chan)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
debug("%s(dev=%p, name=%s, chan=%p)\n", __func__, dev, name, chan);
|
|
|
|
|
2017-05-19 02:09:46 +00:00
|
|
|
index = dev_read_stringlist_search(dev, "mbox-names", name);
|
2016-05-13 21:50:29 +00:00
|
|
|
if (index < 0) {
|
2016-10-02 23:59:28 +00:00
|
|
|
debug("fdt_stringlist_search() failed: %d\n", index);
|
2016-05-13 21:50:29 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mbox_get_by_index(dev, index, chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbox_free(struct mbox_chan *chan)
|
|
|
|
{
|
|
|
|
struct mbox_ops *ops = mbox_dev_ops(chan->dev);
|
|
|
|
|
|
|
|
debug("%s(chan=%p)\n", __func__, chan);
|
|
|
|
|
2020-02-03 14:35:50 +00:00
|
|
|
if (ops->rfree)
|
|
|
|
return ops->rfree(chan);
|
2019-09-27 10:36:54 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-05-13 21:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int mbox_send(struct mbox_chan *chan, const void *data)
|
|
|
|
{
|
|
|
|
struct mbox_ops *ops = mbox_dev_ops(chan->dev);
|
|
|
|
|
|
|
|
debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
|
|
|
|
|
|
|
|
return ops->send(chan, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
|
|
|
|
{
|
|
|
|
struct mbox_ops *ops = mbox_dev_ops(chan->dev);
|
|
|
|
ulong start_time;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data,
|
|
|
|
timeout_us);
|
|
|
|
|
|
|
|
start_time = timer_get_us();
|
|
|
|
/*
|
|
|
|
* Account for partial us ticks, but if timeout_us is 0, ensure we
|
|
|
|
* still don't wait at all.
|
|
|
|
*/
|
|
|
|
if (timeout_us)
|
|
|
|
timeout_us++;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ret = ops->recv(chan, data);
|
|
|
|
if (ret != -ENODATA)
|
|
|
|
return ret;
|
|
|
|
if ((timer_get_us() - start_time) >= timeout_us)
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UCLASS_DRIVER(mailbox) = {
|
|
|
|
.id = UCLASS_MAILBOX,
|
|
|
|
.name = "mailbox",
|
|
|
|
};
|