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