Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Stephen Warren | 3cdf676 | 2016-06-17 09:43:56 -0600 | [diff] [blame] | 8 | #include <mailbox.h> |
| 9 | #include <mailbox-uclass.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 10 | #include <time.h> |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 11 | |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 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, |
Simon Glass | 91dbd53 | 2017-05-18 20:09:46 -0600 | [diff] [blame] | 18 | struct ofnode_phandle_args *args) |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 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 | { |
Simon Glass | 91dbd53 | 2017-05-18 20:09:46 -0600 | [diff] [blame] | 34 | struct ofnode_phandle_args args; |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 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 | |
Simon Glass | 91dbd53 | 2017-05-18 20:09:46 -0600 | [diff] [blame] | 41 | ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index, |
| 42 | &args); |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 43 | if (ret) { |
Simon Glass | 91dbd53 | 2017-05-18 20:09:46 -0600 | [diff] [blame] | 44 | debug("%s: dev_read_phandle_with_args failed: %d\n", __func__, |
| 45 | ret); |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 46 | return ret; |
| 47 | } |
| 48 | |
Simon Glass | 91dbd53 | 2017-05-18 20:09:46 -0600 | [diff] [blame] | 49 | ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox); |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 50 | if (ret) { |
| 51 | debug("%s: uclass_get_device_by_of_offset failed: %d\n", |
| 52 | __func__, ret); |
Ibai Erkiaga | 4cbe31c | 2019-09-27 11:36:55 +0100 | [diff] [blame] | 53 | |
| 54 | /* Test with parent node */ |
| 55 | ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, |
| 56 | ofnode_get_parent(args.node), |
| 57 | &dev_mbox); |
| 58 | if (ret) { |
| 59 | debug("%s: mbox node from parent failed: %d\n", |
| 60 | __func__, ret); |
| 61 | return ret; |
| 62 | }; |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 63 | } |
| 64 | ops = mbox_dev_ops(dev_mbox); |
| 65 | |
| 66 | chan->dev = dev_mbox; |
| 67 | if (ops->of_xlate) |
| 68 | ret = ops->of_xlate(chan, &args); |
| 69 | else |
| 70 | ret = mbox_of_xlate_default(chan, &args); |
| 71 | if (ret) { |
| 72 | debug("of_xlate() failed: %d\n", ret); |
| 73 | return ret; |
| 74 | } |
| 75 | |
Ibai Erkiaga | be67838 | 2019-09-27 11:36:54 +0100 | [diff] [blame] | 76 | if (ops->request) |
| 77 | ret = ops->request(chan); |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 78 | if (ret) { |
| 79 | debug("ops->request() failed: %d\n", ret); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int mbox_get_by_name(struct udevice *dev, const char *name, |
| 87 | struct mbox_chan *chan) |
| 88 | { |
| 89 | int index; |
| 90 | |
| 91 | debug("%s(dev=%p, name=%s, chan=%p)\n", __func__, dev, name, chan); |
| 92 | |
Simon Glass | 91dbd53 | 2017-05-18 20:09:46 -0600 | [diff] [blame] | 93 | index = dev_read_stringlist_search(dev, "mbox-names", name); |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 94 | if (index < 0) { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 95 | debug("fdt_stringlist_search() failed: %d\n", index); |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 96 | return index; |
| 97 | } |
| 98 | |
| 99 | return mbox_get_by_index(dev, index, chan); |
| 100 | } |
| 101 | |
| 102 | int mbox_free(struct mbox_chan *chan) |
| 103 | { |
| 104 | struct mbox_ops *ops = mbox_dev_ops(chan->dev); |
| 105 | |
| 106 | debug("%s(chan=%p)\n", __func__, chan); |
| 107 | |
Ibai Erkiaga | be67838 | 2019-09-27 11:36:54 +0100 | [diff] [blame] | 108 | if (ops->free) |
| 109 | return ops->free(chan); |
| 110 | |
| 111 | return 0; |
Stephen Warren | d010783 | 2016-05-13 15:50:29 -0600 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int mbox_send(struct mbox_chan *chan, const void *data) |
| 115 | { |
| 116 | struct mbox_ops *ops = mbox_dev_ops(chan->dev); |
| 117 | |
| 118 | debug("%s(chan=%p, data=%p)\n", __func__, chan, data); |
| 119 | |
| 120 | return ops->send(chan, data); |
| 121 | } |
| 122 | |
| 123 | int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us) |
| 124 | { |
| 125 | struct mbox_ops *ops = mbox_dev_ops(chan->dev); |
| 126 | ulong start_time; |
| 127 | int ret; |
| 128 | |
| 129 | debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data, |
| 130 | timeout_us); |
| 131 | |
| 132 | start_time = timer_get_us(); |
| 133 | /* |
| 134 | * Account for partial us ticks, but if timeout_us is 0, ensure we |
| 135 | * still don't wait at all. |
| 136 | */ |
| 137 | if (timeout_us) |
| 138 | timeout_us++; |
| 139 | |
| 140 | for (;;) { |
| 141 | ret = ops->recv(chan, data); |
| 142 | if (ret != -ENODATA) |
| 143 | return ret; |
| 144 | if ((timer_get_us() - start_time) >= timeout_us) |
| 145 | return -ETIMEDOUT; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | UCLASS_DRIVER(mailbox) = { |
| 150 | .id = UCLASS_MAILBOX, |
| 151 | .name = "mailbox", |
| 152 | }; |