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