1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
10 #include <mailbox-uclass.h>
14 static inline struct mbox_ops *mbox_dev_ops(struct udevice *dev)
16 return (struct mbox_ops *)dev->driver->ops;
19 static int mbox_of_xlate_default(struct mbox_chan *chan,
20 struct ofnode_phandle_args *args)
22 debug("%s(chan=%p)\n", __func__, chan);
24 if (args->args_count != 1) {
25 debug("Invaild args_count: %d\n", args->args_count);
29 chan->id = args->args[0];
34 int mbox_get_by_index(struct udevice *dev, int index, struct mbox_chan *chan)
36 struct ofnode_phandle_args args;
38 struct udevice *dev_mbox;
41 debug("%s(dev=%p, index=%d, chan=%p)\n", __func__, dev, index, chan);
43 ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index,
46 debug("%s: dev_read_phandle_with_args failed: %d\n", __func__,
51 ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox);
53 debug("%s: uclass_get_device_by_of_offset failed: %d\n",
56 /* Test with parent node */
57 ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX,
58 ofnode_get_parent(args.node),
61 debug("%s: mbox node from parent failed: %d\n",
66 ops = mbox_dev_ops(dev_mbox);
70 ret = ops->of_xlate(chan, &args);
72 ret = mbox_of_xlate_default(chan, &args);
74 debug("of_xlate() failed: %d\n", ret);
79 ret = ops->request(chan);
81 debug("ops->request() failed: %d\n", ret);
88 int mbox_get_by_name(struct udevice *dev, const char *name,
89 struct mbox_chan *chan)
93 debug("%s(dev=%p, name=%s, chan=%p)\n", __func__, dev, name, chan);
95 index = dev_read_stringlist_search(dev, "mbox-names", name);
97 debug("fdt_stringlist_search() failed: %d\n", index);
101 return mbox_get_by_index(dev, index, chan);
104 int mbox_free(struct mbox_chan *chan)
106 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
108 debug("%s(chan=%p)\n", __func__, chan);
111 return ops->rfree(chan);
116 int mbox_send(struct mbox_chan *chan, const void *data)
118 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
120 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
122 return ops->send(chan, data);
125 int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
127 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
131 debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data,
134 start_time = timer_get_us();
136 * Account for partial us ticks, but if timeout_us is 0, ensure we
137 * still don't wait at all.
143 ret = ops->recv(chan, data);
146 if ((timer_get_us() - start_time) >= timeout_us)
151 UCLASS_DRIVER(mailbox) = {
152 .id = UCLASS_MAILBOX,