1 /* 2 * Copyright (c) 2016, NVIDIA CORPORATION. 3 * 4 * SPDX-License-Identifier: GPL-2.0 5 */ 6 7 #include <common.h> 8 #include <dm.h> 9 #include <mailbox.h> 10 #include <mailbox-uclass.h> 11 12 static inline struct mbox_ops *mbox_dev_ops(struct udevice *dev) 13 { 14 return (struct mbox_ops *)dev->driver->ops; 15 } 16 17 static int mbox_of_xlate_default(struct mbox_chan *chan, 18 struct ofnode_phandle_args *args) 19 { 20 debug("%s(chan=%p)\n", __func__, chan); 21 22 if (args->args_count != 1) { 23 debug("Invaild args_count: %d\n", args->args_count); 24 return -EINVAL; 25 } 26 27 chan->id = args->args[0]; 28 29 return 0; 30 } 31 32 int mbox_get_by_index(struct udevice *dev, int index, struct mbox_chan *chan) 33 { 34 struct ofnode_phandle_args args; 35 int ret; 36 struct udevice *dev_mbox; 37 struct mbox_ops *ops; 38 39 debug("%s(dev=%p, index=%d, chan=%p)\n", __func__, dev, index, chan); 40 41 ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index, 42 &args); 43 if (ret) { 44 debug("%s: dev_read_phandle_with_args failed: %d\n", __func__, 45 ret); 46 return ret; 47 } 48 49 ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox); 50 if (ret) { 51 debug("%s: uclass_get_device_by_of_offset failed: %d\n", 52 __func__, ret); 53 return ret; 54 } 55 ops = mbox_dev_ops(dev_mbox); 56 57 chan->dev = dev_mbox; 58 if (ops->of_xlate) 59 ret = ops->of_xlate(chan, &args); 60 else 61 ret = mbox_of_xlate_default(chan, &args); 62 if (ret) { 63 debug("of_xlate() failed: %d\n", ret); 64 return ret; 65 } 66 67 ret = ops->request(chan); 68 if (ret) { 69 debug("ops->request() failed: %d\n", ret); 70 return ret; 71 } 72 73 return 0; 74 } 75 76 int 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 83 index = dev_read_stringlist_search(dev, "mbox-names", name); 84 if (index < 0) { 85 debug("fdt_stringlist_search() failed: %d\n", index); 86 return index; 87 } 88 89 return mbox_get_by_index(dev, index, chan); 90 } 91 92 int 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 98 return ops->free(chan); 99 } 100 101 int mbox_send(struct mbox_chan *chan, const void *data) 102 { 103 struct mbox_ops *ops = mbox_dev_ops(chan->dev); 104 105 debug("%s(chan=%p, data=%p)\n", __func__, chan, data); 106 107 return ops->send(chan, data); 108 } 109 110 int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us) 111 { 112 struct mbox_ops *ops = mbox_dev_ops(chan->dev); 113 ulong start_time; 114 int ret; 115 116 debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data, 117 timeout_us); 118 119 start_time = timer_get_us(); 120 /* 121 * Account for partial us ticks, but if timeout_us is 0, ensure we 122 * still don't wait at all. 123 */ 124 if (timeout_us) 125 timeout_us++; 126 127 for (;;) { 128 ret = ops->recv(chan, data); 129 if (ret != -ENODATA) 130 return ret; 131 if ((timer_get_us() - start_time) >= timeout_us) 132 return -ETIMEDOUT; 133 } 134 } 135 136 UCLASS_DRIVER(mailbox) = { 137 .id = UCLASS_MAILBOX, 138 .name = "mailbox", 139 }; 140