1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
9 #include <mailbox-uclass.h>
11 static inline struct mbox_ops *mbox_dev_ops(struct udevice *dev)
13 return (struct mbox_ops *)dev->driver->ops;
16 static int mbox_of_xlate_default(struct mbox_chan *chan,
17 struct ofnode_phandle_args *args)
19 debug("%s(chan=%p)\n", __func__, chan);
21 if (args->args_count != 1) {
22 debug("Invaild args_count: %d\n", args->args_count);
26 chan->id = args->args[0];
31 int mbox_get_by_index(struct udevice *dev, int index, struct mbox_chan *chan)
33 struct ofnode_phandle_args args;
35 struct udevice *dev_mbox;
38 debug("%s(dev=%p, index=%d, chan=%p)\n", __func__, dev, index, chan);
40 ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index,
43 debug("%s: dev_read_phandle_with_args failed: %d\n", __func__,
48 ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox);
50 debug("%s: uclass_get_device_by_of_offset failed: %d\n",
54 ops = mbox_dev_ops(dev_mbox);
58 ret = ops->of_xlate(chan, &args);
60 ret = mbox_of_xlate_default(chan, &args);
62 debug("of_xlate() failed: %d\n", ret);
66 ret = ops->request(chan);
68 debug("ops->request() failed: %d\n", ret);
75 int mbox_get_by_name(struct udevice *dev, const char *name,
76 struct mbox_chan *chan)
80 debug("%s(dev=%p, name=%s, chan=%p)\n", __func__, dev, name, chan);
82 index = dev_read_stringlist_search(dev, "mbox-names", name);
84 debug("fdt_stringlist_search() failed: %d\n", index);
88 return mbox_get_by_index(dev, index, chan);
91 int mbox_free(struct mbox_chan *chan)
93 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
95 debug("%s(chan=%p)\n", __func__, chan);
97 return ops->free(chan);
100 int mbox_send(struct mbox_chan *chan, const void *data)
102 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
104 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
106 return ops->send(chan, data);
109 int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
111 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
115 debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data,
118 start_time = timer_get_us();
120 * Account for partial us ticks, but if timeout_us is 0, ensure we
121 * still don't wait at all.
127 ret = ops->recv(chan, data);
130 if ((timer_get_us() - start_time) >= timeout_us)
135 UCLASS_DRIVER(mailbox) = {
136 .id = UCLASS_MAILBOX,