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