blob: 809f26b2025864d4102bb6fb4c13b8acde29cc9e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warrend0107832016-05-13 15:50:29 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warrend0107832016-05-13 15:50:29 -06004 */
5
6#include <common.h>
7#include <dm.h>
Stephen Warren3cdf6762016-06-17 09:43:56 -06008#include <mailbox.h>
9#include <mailbox-uclass.h>
Stephen Warrend0107832016-05-13 15:50:29 -060010
Stephen Warrend0107832016-05-13 15:50:29 -060011static inline struct mbox_ops *mbox_dev_ops(struct udevice *dev)
12{
13 return (struct mbox_ops *)dev->driver->ops;
14}
15
16static int mbox_of_xlate_default(struct mbox_chan *chan,
Simon Glass91dbd532017-05-18 20:09:46 -060017 struct ofnode_phandle_args *args)
Stephen Warrend0107832016-05-13 15:50:29 -060018{
19 debug("%s(chan=%p)\n", __func__, chan);
20
21 if (args->args_count != 1) {
22 debug("Invaild args_count: %d\n", args->args_count);
23 return -EINVAL;
24 }
25
26 chan->id = args->args[0];
27
28 return 0;
29}
30
31int mbox_get_by_index(struct udevice *dev, int index, struct mbox_chan *chan)
32{
Simon Glass91dbd532017-05-18 20:09:46 -060033 struct ofnode_phandle_args args;
Stephen Warrend0107832016-05-13 15:50:29 -060034 int ret;
35 struct udevice *dev_mbox;
36 struct mbox_ops *ops;
37
38 debug("%s(dev=%p, index=%d, chan=%p)\n", __func__, dev, index, chan);
39
Simon Glass91dbd532017-05-18 20:09:46 -060040 ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index,
41 &args);
Stephen Warrend0107832016-05-13 15:50:29 -060042 if (ret) {
Simon Glass91dbd532017-05-18 20:09:46 -060043 debug("%s: dev_read_phandle_with_args failed: %d\n", __func__,
44 ret);
Stephen Warrend0107832016-05-13 15:50:29 -060045 return ret;
46 }
47
Simon Glass91dbd532017-05-18 20:09:46 -060048 ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox);
Stephen Warrend0107832016-05-13 15:50:29 -060049 if (ret) {
50 debug("%s: uclass_get_device_by_of_offset failed: %d\n",
51 __func__, ret);
52 return ret;
53 }
54 ops = mbox_dev_ops(dev_mbox);
55
56 chan->dev = dev_mbox;
57 if (ops->of_xlate)
58 ret = ops->of_xlate(chan, &args);
59 else
60 ret = mbox_of_xlate_default(chan, &args);
61 if (ret) {
62 debug("of_xlate() failed: %d\n", ret);
63 return ret;
64 }
65
Ibai Erkiagabe678382019-09-27 11:36:54 +010066 if (ops->request)
67 ret = ops->request(chan);
Stephen Warrend0107832016-05-13 15:50:29 -060068 if (ret) {
69 debug("ops->request() failed: %d\n", ret);
70 return ret;
71 }
72
73 return 0;
74}
75
76int mbox_get_by_name(struct udevice *dev, const char *name,
77 struct mbox_chan *chan)
78{
79 int index;
80
81 debug("%s(dev=%p, name=%s, chan=%p)\n", __func__, dev, name, chan);
82
Simon Glass91dbd532017-05-18 20:09:46 -060083 index = dev_read_stringlist_search(dev, "mbox-names", name);
Stephen Warrend0107832016-05-13 15:50:29 -060084 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -060085 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warrend0107832016-05-13 15:50:29 -060086 return index;
87 }
88
89 return mbox_get_by_index(dev, index, chan);
90}
91
92int mbox_free(struct mbox_chan *chan)
93{
94 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
95
96 debug("%s(chan=%p)\n", __func__, chan);
97
Ibai Erkiagabe678382019-09-27 11:36:54 +010098 if (ops->free)
99 return ops->free(chan);
100
101 return 0;
Stephen Warrend0107832016-05-13 15:50:29 -0600102}
103
104int mbox_send(struct mbox_chan *chan, const void *data)
105{
106 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
107
108 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
109
110 return ops->send(chan, data);
111}
112
113int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
114{
115 struct mbox_ops *ops = mbox_dev_ops(chan->dev);
116 ulong start_time;
117 int ret;
118
119 debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data,
120 timeout_us);
121
122 start_time = timer_get_us();
123 /*
124 * Account for partial us ticks, but if timeout_us is 0, ensure we
125 * still don't wait at all.
126 */
127 if (timeout_us)
128 timeout_us++;
129
130 for (;;) {
131 ret = ops->recv(chan, data);
132 if (ret != -ENODATA)
133 return ret;
134 if ((timer_get_us() - start_time) >= timeout_us)
135 return -ETIMEDOUT;
136 }
137}
138
139UCLASS_DRIVER(mailbox) = {
140 .id = UCLASS_MAILBOX,
141 .name = "mailbox",
142};