From: Peng Fan <peng....@nxp.com>

Add timeout support for mbox_send.

Signed-off-by: Peng Fan <peng....@nxp.com>
Signed-off-by: Alice Guo <alice....@nxp.com>
Reviewed-by: Ye Li <ye...@nxp.com>
---
 drivers/mailbox/mailbox-uclass.c | 19 ++++++++++++++++++-
 include/mailbox.h                |  2 ++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/drivers/mailbox/mailbox-uclass.c b/drivers/mailbox/mailbox-uclass.c
index 4bf4987ce0..0ca8b3770f 100644
--- a/drivers/mailbox/mailbox-uclass.c
+++ b/drivers/mailbox/mailbox-uclass.c
@@ -117,10 +117,27 @@ int mbox_free(struct mbox_chan *chan)
 int mbox_send(struct mbox_chan *chan, const void *data)
 {
        struct mbox_ops *ops = mbox_dev_ops(chan->dev);
+       ulong start_time, timeout_us;
+       int ret;
 
        debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
 
-       return ops->send(chan, data);
+       start_time = timer_get_us();
+       timeout_us = chan->tx_timeout_us;
+       /*
+        * Account for partial us ticks, but if timeout_us is 0, ensure we
+        * still don't wait at all.
+        */
+       if (timeout_us)
+               timeout_us++;
+
+       for (;;) {
+               ret = ops->send(chan, data);
+               if (ret != -EBUSY)
+                       return ret;
+               if ((timer_get_us() - start_time) >= timeout_us)
+                       return -ETIMEDOUT;
+       }
 }
 
 int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
diff --git a/include/mailbox.h b/include/mailbox.h
index e70266fb61..3cc64f20b7 100644
--- a/include/mailbox.h
+++ b/include/mailbox.h
@@ -46,6 +46,7 @@ struct udevice;
  *
  * @dev: The device which implements the mailbox.
  * @id: The mailbox channel ID within the provider.
+ * @tx_timeout_us: The tx timeout in us.
  * @con_priv: Hook for controller driver to attach private data
  *
  * Currently, the mailbox API assumes that a single integer ID is enough to
@@ -61,6 +62,7 @@ struct mbox_chan {
        struct udevice *dev;
        /* Written by of_xlate.*/
        unsigned long id;
+       unsigned long tx_timeout_us;
        void *con_priv;
 };
 
-- 
2.34.1

Reply via email to