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