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