Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Linaro Limited. |
| 4 | */ |
| 5 | |
Patrick Delaunay | 5dd84d4 | 2021-02-24 11:19:44 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SCMI_AGENT |
| 7 | |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 8 | #include <dm.h> |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <mailbox.h> |
| 11 | #include <scmi_agent.h> |
| 12 | #include <scmi_agent-uclass.h> |
Patrick Delaunay | d4c9f68 | 2021-02-24 11:19:45 +0100 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 14 | #include <dm/devres.h> |
| 15 | #include <linux/compat.h> |
| 16 | |
| 17 | #include "smt.h" |
| 18 | |
| 19 | #define TIMEOUT_US_10MS 10000 |
| 20 | |
| 21 | /** |
| 22 | * struct scmi_mbox_channel - Description of an SCMI mailbox transport |
| 23 | * @smt: Shared memory buffer |
| 24 | * @mbox: Mailbox channel description |
| 25 | * @timeout_us: Timeout in microseconds for the mailbox transfer |
| 26 | */ |
| 27 | struct scmi_mbox_channel { |
| 28 | struct scmi_smt smt; |
| 29 | struct mbox_chan mbox; |
| 30 | ulong timeout_us; |
| 31 | }; |
| 32 | |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 33 | /** |
| 34 | * struct scmi_channel - Channel instance referenced in SCMI drivers |
| 35 | * @ref: Reference to local channel instance |
| 36 | **/ |
| 37 | struct scmi_channel { |
| 38 | struct scmi_mbox_channel ref; |
| 39 | }; |
| 40 | |
Etienne Carriere | 0544029 | 2022-05-31 18:09:19 +0200 | [diff] [blame] | 41 | static int scmi_mbox_process_msg(struct udevice *dev, |
| 42 | struct scmi_channel *channel, |
| 43 | struct scmi_msg *msg) |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 44 | { |
Etienne Carriere | 76b690a | 2022-05-31 18:09:29 +0200 | [diff] [blame] | 45 | struct scmi_mbox_channel *chan = &channel->ref; |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 46 | int ret; |
| 47 | |
| 48 | ret = scmi_write_msg_to_smt(dev, &chan->smt, msg); |
| 49 | if (ret) |
| 50 | return ret; |
| 51 | |
| 52 | /* Give shm addr to mbox in case it is meaningful */ |
| 53 | ret = mbox_send(&chan->mbox, chan->smt.buf); |
| 54 | if (ret) { |
| 55 | dev_err(dev, "Message send failed: %d\n", ret); |
| 56 | goto out; |
| 57 | } |
| 58 | |
| 59 | /* Receive the response */ |
| 60 | ret = mbox_recv(&chan->mbox, chan->smt.buf, chan->timeout_us); |
| 61 | if (ret) { |
| 62 | dev_err(dev, "Response failed: %d, abort\n", ret); |
| 63 | goto out; |
| 64 | } |
| 65 | |
| 66 | ret = scmi_read_resp_from_smt(dev, &chan->smt, msg); |
| 67 | |
| 68 | out: |
| 69 | scmi_clear_smt_channel(&chan->smt); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 74 | static int setup_channel(struct udevice *dev, struct scmi_mbox_channel *chan) |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 75 | { |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 76 | int ret; |
| 77 | |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 78 | ret = mbox_get_by_index(dev, 0, &chan->mbox); |
| 79 | if (ret) { |
| 80 | dev_err(dev, "Failed to find mailbox: %d\n", ret); |
Etienne Carriere | adbf0fc | 2021-11-08 08:56:08 +0100 | [diff] [blame] | 81 | return ret; |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | ret = scmi_dt_get_smt_buffer(dev, &chan->smt); |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 85 | if (ret) { |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 86 | dev_err(dev, "Failed to get shm resources: %d\n", ret); |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 87 | return ret; |
| 88 | } |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 89 | |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 90 | chan->timeout_us = TIMEOUT_US_10MS; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int scmi_mbox_get_channel(struct udevice *dev, |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 96 | struct udevice *protocol, |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 97 | struct scmi_channel **channel) |
| 98 | { |
Patrick Delaunay | 3d8b950 | 2022-09-30 09:36:38 +0200 | [diff] [blame] | 99 | struct scmi_mbox_channel *base_chan = dev_get_plat(dev); |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 100 | struct scmi_mbox_channel *chan; |
| 101 | int ret; |
| 102 | |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 103 | if (!dev_read_prop(protocol, "shmem", NULL)) { |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 104 | /* Uses agent base channel */ |
| 105 | *channel = container_of(base_chan, struct scmi_channel, ref); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | chan = calloc(1, sizeof(*chan)); |
| 111 | if (!chan) |
| 112 | return -ENOMEM; |
| 113 | |
| 114 | /* Setup a dedicated channel for the protocol */ |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 115 | ret = setup_channel(protocol, chan); |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 116 | if (ret) { |
| 117 | free(chan); |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | *channel = (void *)chan; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int scmi_mbox_of_to_plat(struct udevice *dev) |
| 127 | { |
| 128 | struct scmi_mbox_channel *chan = dev_get_plat(dev); |
| 129 | |
| 130 | return setup_channel(dev, chan); |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static const struct udevice_id scmi_mbox_ids[] = { |
| 134 | { .compatible = "arm,scmi" }, |
| 135 | { } |
| 136 | }; |
| 137 | |
| 138 | static const struct scmi_agent_ops scmi_mbox_ops = { |
Etienne Carriere | 274c0c2 | 2022-05-31 18:09:22 +0200 | [diff] [blame] | 139 | .of_get_channel = scmi_mbox_get_channel, |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 140 | .process_msg = scmi_mbox_process_msg, |
| 141 | }; |
| 142 | |
| 143 | U_BOOT_DRIVER(scmi_mbox) = { |
| 144 | .name = "scmi-over-mailbox", |
| 145 | .id = UCLASS_SCMI_AGENT, |
| 146 | .of_match = scmi_mbox_ids, |
Etienne Carriere | 3119e27 | 2021-11-08 08:56:09 +0100 | [diff] [blame] | 147 | .plat_auto = sizeof(struct scmi_mbox_channel), |
| 148 | .of_to_plat = scmi_mbox_of_to_plat, |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 149 | .ops = &scmi_mbox_ops, |
| 150 | }; |