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