2 * Copyright (c) 2016, NVIDIA CORPORATION.
4 * SPDX-License-Identifier: GPL-2.0
10 #include <mailbox-uclass.h>
12 static inline struct mbox_ops *mbox_dev_ops(struct udevice *dev)
14 return (struct mbox_ops *)dev->driver->ops;
17 static int mbox_of_xlate_default(struct mbox_chan *chan,
18 struct ofnode_phandle_args *args)
20 debug("%s(chan=%p)\n", __func__, chan);
22 if (args->args_count != 1) {
23 debug("Invaild args_count: %d\n", args->args_count);
27 chan->id = args->args[0];
32 int mbox_get_by_index(struct udevice *dev, int index, struct mbox_chan *chan)
34 struct ofnode_phandle_args args;
36 struct udevice *dev_mbox;
39 debug("%s(dev=%p, index=%d, chan=%p)\n", __func__, dev, index, chan);
41 ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index,
44 debug("%s: dev_read_phandle_with_args failed: %d\n", __func__,
49 ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox);
51 debug("%s: uclass_get_device_by_of_offset failed: %d\n",
55 ops = mbox_dev_ops(dev_mbox);
59 ret = ops->of_xlate(chan, &args);
61 ret = mbox_of_xlate_default(chan, &args);
63 debug("of_xlate() failed: %d\n", ret);
67 ret = ops->request(chan);
69 debug("ops->request() failed: %d\n", ret);
76 int mbox_get_by_name(struct udevice *dev, const char *name,
77 struct mbox_chan *chan)
81 debug("%s(dev=%p, name=%s, chan=%p)\n", __func__, dev, name, chan);
83 index = dev_read_stringlist_search(dev, "mbox-names", name);
85 debug("fdt_stringlist_search() failed: %d\n", index);
89 return mbox_get_by_index(dev, index, chan);
92 int mbox_free(struct mbox_chan *chan)
94 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
96 debug("%s(chan=%p)\n", __func__, chan);
98 return ops->free(chan);
101 int mbox_send(struct mbox_chan *chan, const void *data)
103 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
105 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
107 return ops->send(chan, data);
110 int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
112 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
116 debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data,
119 start_time = timer_get_us();
121 * Account for partial us ticks, but if timeout_us is 0, ensure we
122 * still don't wait at all.
128 ret = ops->recv(chan, data);
131 if ((timer_get_us() - start_time) >= timeout_us)
136 UCLASS_DRIVER(mailbox) = {
137 .id = UCLASS_MAILBOX,