1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
6 #define LOG_CATEGORY UCLASS_MAILBOX
12 #include <mailbox-uclass.h>
16 static inline struct mbox_ops *mbox_dev_ops(struct udevice *dev)
18 return (struct mbox_ops *)dev->driver->ops;
21 static int mbox_of_xlate_default(struct mbox_chan *chan,
22 struct ofnode_phandle_args *args)
24 debug("%s(chan=%p)\n", __func__, chan);
26 if (args->args_count != 1) {
27 debug("Invalid args_count: %d\n", args->args_count);
31 chan->id = args->args[0];
36 int mbox_get_by_index(struct udevice *dev, int index, struct mbox_chan *chan)
38 struct ofnode_phandle_args args;
40 struct udevice *dev_mbox;
43 debug("%s(dev=%p, index=%d, chan=%p)\n", __func__, dev, index, chan);
45 ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index,
48 debug("%s: dev_read_phandle_with_args failed: %d\n", __func__,
53 ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox);
55 debug("%s: uclass_get_device_by_of_offset failed: %d\n",
58 /* Test with parent node */
59 ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX,
60 ofnode_get_parent(args.node),
63 debug("%s: mbox node from parent failed: %d\n",
68 ops = mbox_dev_ops(dev_mbox);
72 ret = ops->of_xlate(chan, &args);
74 ret = mbox_of_xlate_default(chan, &args);
76 debug("of_xlate() failed: %d\n", ret);
81 ret = ops->request(chan);
83 debug("ops->request() failed: %d\n", ret);
90 int mbox_get_by_name(struct udevice *dev, const char *name,
91 struct mbox_chan *chan)
95 debug("%s(dev=%p, name=%s, chan=%p)\n", __func__, dev, name, chan);
97 index = dev_read_stringlist_search(dev, "mbox-names", name);
99 debug("fdt_stringlist_search() failed: %d\n", index);
103 return mbox_get_by_index(dev, index, chan);
106 int mbox_free(struct mbox_chan *chan)
108 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
110 debug("%s(chan=%p)\n", __func__, chan);
113 return ops->rfree(chan);
118 int mbox_send(struct mbox_chan *chan, const void *data)
120 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
122 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
124 return ops->send(chan, data);
127 int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
129 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
133 debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data,
136 start_time = timer_get_us();
138 * Account for partial us ticks, but if timeout_us is 0, ensure we
139 * still don't wait at all.
145 ret = ops->recv(chan, data);
148 if ((timer_get_us() - start_time) >= timeout_us)
153 UCLASS_DRIVER(mailbox) = {
154 .id = UCLASS_MAILBOX,