Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +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 | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 11 | #include <scmi_agent.h> |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 12 | #include <scmi_agent-uclass.h> |
| 13 | #include <scmi_protocols.h> |
Patrick Delaunay | d4c9f68 | 2021-02-24 11:19:45 +0100 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
| 16 | #include <linux/compat.h> |
| 17 | |
| 18 | /** |
| 19 | * struct error_code - Helper structure for SCMI error code conversion |
| 20 | * @scmi: SCMI error code |
| 21 | * @errno: Related standard error number |
| 22 | */ |
| 23 | struct error_code { |
| 24 | int scmi; |
| 25 | int errno; |
| 26 | }; |
| 27 | |
| 28 | static const struct error_code scmi_linux_errmap[] = { |
| 29 | { .scmi = SCMI_NOT_SUPPORTED, .errno = -EOPNOTSUPP, }, |
| 30 | { .scmi = SCMI_INVALID_PARAMETERS, .errno = -EINVAL, }, |
| 31 | { .scmi = SCMI_DENIED, .errno = -EACCES, }, |
| 32 | { .scmi = SCMI_NOT_FOUND, .errno = -ENOENT, }, |
| 33 | { .scmi = SCMI_OUT_OF_RANGE, .errno = -ERANGE, }, |
| 34 | { .scmi = SCMI_BUSY, .errno = -EBUSY, }, |
| 35 | { .scmi = SCMI_COMMS_ERROR, .errno = -ECOMM, }, |
| 36 | { .scmi = SCMI_GENERIC_ERROR, .errno = -EIO, }, |
| 37 | { .scmi = SCMI_HARDWARE_ERROR, .errno = -EREMOTEIO, }, |
| 38 | { .scmi = SCMI_PROTOCOL_ERROR, .errno = -EPROTO, }, |
| 39 | }; |
| 40 | |
| 41 | int scmi_to_linux_errno(s32 scmi_code) |
| 42 | { |
| 43 | int n; |
| 44 | |
| 45 | if (!scmi_code) |
| 46 | return 0; |
| 47 | |
| 48 | for (n = 0; n < ARRAY_SIZE(scmi_linux_errmap); n++) |
| 49 | if (scmi_code == scmi_linux_errmap[n].scmi) |
AKASHI Takahiro | c937154 | 2023-06-13 10:30:45 +0900 | [diff] [blame] | 50 | return scmi_linux_errmap[n].errno; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 51 | |
| 52 | return -EPROTO; |
| 53 | } |
| 54 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 55 | static struct udevice *find_scmi_protocol_device(struct udevice *dev) |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 56 | { |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 57 | struct udevice *parent = NULL, *protocol; |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 58 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 59 | for (protocol = dev; protocol; protocol = parent) { |
| 60 | parent = dev_get_parent(protocol); |
| 61 | if (!parent || |
| 62 | device_get_uclass_id(parent) == UCLASS_SCMI_AGENT) |
| 63 | break; |
| 64 | } |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 65 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 66 | if (!parent) { |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 67 | dev_err(dev, "Invalid SCMI device, agent not found\n"); |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 70 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 71 | return protocol; |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 74 | static const struct scmi_agent_ops *transport_dev_ops(struct udevice *dev) |
| 75 | { |
| 76 | return (const struct scmi_agent_ops *)dev->driver->ops; |
| 77 | } |
| 78 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 79 | /** |
| 80 | * scmi_of_get_channel() - Get SCMI channel handle |
| 81 | * |
| 82 | * @dev: SCMI agent device |
| 83 | * @channel: Output reference to the SCMI channel upon success |
| 84 | * |
| 85 | * On return, @channel will be set. |
| 86 | * Return 0 on success and a negative errno on failure |
| 87 | */ |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 88 | static int scmi_of_get_channel(struct udevice *dev, struct udevice *protocol, |
| 89 | struct scmi_channel **channel) |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 90 | { |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 91 | const struct scmi_agent_ops *ops; |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 92 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 93 | ops = transport_dev_ops(dev); |
| 94 | if (ops->of_get_channel) |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 95 | return ops->of_get_channel(dev, protocol, channel); |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 96 | else |
| 97 | return -EPROTONOSUPPORT; |
| 98 | } |
| 99 | |
| 100 | int devm_scmi_of_get_channel(struct udevice *dev) |
| 101 | { |
| 102 | struct udevice *protocol; |
| 103 | struct scmi_agent_proto_priv *priv; |
| 104 | int ret; |
| 105 | |
| 106 | protocol = find_scmi_protocol_device(dev); |
| 107 | if (!protocol) |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 108 | return -ENODEV; |
| 109 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 110 | priv = dev_get_parent_priv(protocol); |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 111 | ret = scmi_of_get_channel(protocol->parent, protocol, &priv->channel); |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 112 | if (ret == -EPROTONOSUPPORT) { |
| 113 | /* Drivers without a get_channel operator don't need a channel ref */ |
| 114 | priv->channel = NULL; |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 115 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 116 | return 0; |
| 117 | } |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 118 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 119 | return ret; |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 120 | } |
| 121 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 122 | /** |
| 123 | * scmi_process_msg() - Send and process an SCMI message |
| 124 | * |
| 125 | * Send a message to an SCMI server. |
| 126 | * Caller sets scmi_msg::out_msg_sz to the output message buffer size. |
| 127 | * |
| 128 | * @dev: SCMI agent device |
| 129 | * @channel: Communication channel for the device |
| 130 | * @msg: Message structure reference |
| 131 | * |
| 132 | * On return, scmi_msg::out_msg_sz stores the response payload size. |
| 133 | * Return: 0 on success and a negative errno on failure |
| 134 | */ |
| 135 | static int scmi_process_msg(struct udevice *dev, struct scmi_channel *channel, |
| 136 | struct scmi_msg *msg) |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 137 | { |
Etienne Carriere | 2f26c04 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 138 | const struct scmi_agent_ops *ops; |
Etienne Carriere | 2f26c04 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 139 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 140 | ops = transport_dev_ops(dev); |
| 141 | if (ops->process_msg) |
| 142 | return ops->process_msg(dev, channel, msg); |
| 143 | else |
| 144 | return -EPROTONOSUPPORT; |
| 145 | } |
Etienne Carriere | 2f26c04 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 146 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 147 | int devm_scmi_process_msg(struct udevice *dev, struct scmi_msg *msg) |
| 148 | { |
| 149 | struct udevice *protocol; |
| 150 | struct scmi_agent_proto_priv *priv; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 151 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 152 | protocol = find_scmi_protocol_device(dev); |
| 153 | if (!protocol) |
| 154 | return -ENODEV; |
| 155 | |
| 156 | priv = dev_get_parent_priv(protocol); |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 157 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 158 | return scmi_process_msg(protocol->parent, priv->channel, msg); |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 159 | } |
| 160 | |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame^] | 161 | /* |
| 162 | * SCMI agent devices binds devices of various uclasses depending on |
| 163 | * the FDT description. scmi_bind_protocol() is a generic bind sequence |
| 164 | * called by the uclass at bind stage, that is uclass post_bind. |
| 165 | */ |
| 166 | static int scmi_bind_protocols(struct udevice *dev) |
| 167 | { |
| 168 | int ret = 0; |
| 169 | ofnode node; |
| 170 | const char *name; |
| 171 | |
| 172 | dev_for_each_subnode(node, dev) { |
| 173 | struct driver *drv = NULL; |
| 174 | u32 protocol_id; |
| 175 | |
| 176 | if (!ofnode_is_enabled(node)) |
| 177 | continue; |
| 178 | |
| 179 | if (ofnode_read_u32(node, "reg", &protocol_id)) |
| 180 | continue; |
| 181 | |
| 182 | name = ofnode_get_name(node); |
| 183 | switch (protocol_id) { |
| 184 | case SCMI_PROTOCOL_ID_CLOCK: |
| 185 | if (CONFIG_IS_ENABLED(CLK_SCMI)) |
| 186 | drv = DM_DRIVER_GET(scmi_clock); |
| 187 | break; |
| 188 | case SCMI_PROTOCOL_ID_RESET_DOMAIN: |
| 189 | if (IS_ENABLED(CONFIG_RESET_SCMI)) |
| 190 | drv = DM_DRIVER_GET(scmi_reset_domain); |
| 191 | break; |
| 192 | case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN: |
| 193 | if (IS_ENABLED(CONFIG_DM_REGULATOR_SCMI)) { |
| 194 | node = ofnode_find_subnode(node, "regulators"); |
| 195 | if (!ofnode_valid(node)) { |
| 196 | dev_err(dev, "no regulators node\n"); |
| 197 | return -ENXIO; |
| 198 | } |
| 199 | drv = DM_DRIVER_GET(scmi_voltage_domain); |
| 200 | } |
| 201 | break; |
| 202 | default: |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | if (!drv) { |
| 207 | dev_dbg(dev, "Ignore unsupported SCMI protocol %#x\n", |
| 208 | protocol_id); |
| 209 | continue; |
| 210 | } |
| 211 | |
| 212 | ret = device_bind(dev, drv, name, NULL, node, NULL); |
| 213 | if (ret) |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 220 | UCLASS_DRIVER(scmi_agent) = { |
| 221 | .id = UCLASS_SCMI_AGENT, |
| 222 | .name = "scmi_agent", |
| 223 | .post_bind = scmi_bind_protocols, |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 224 | .per_child_auto = sizeof(struct scmi_agent_proto_priv), |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 225 | }; |