Etienne Carriere | 358599e | 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 | 9f5e4aa | 2021-02-24 11:19:44 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SCMI_AGENT |
| 7 | |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <scmi_agent-uclass.h> |
| 12 | #include <scmi_protocols.h> |
Patrick Delaunay | 0689dc5 | 2021-02-24 11:19:45 +0100 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
| 15 | #include <linux/compat.h> |
| 16 | |
| 17 | /** |
| 18 | * struct error_code - Helper structure for SCMI error code conversion |
| 19 | * @scmi: SCMI error code |
| 20 | * @errno: Related standard error number |
| 21 | */ |
| 22 | struct error_code { |
| 23 | int scmi; |
| 24 | int errno; |
| 25 | }; |
| 26 | |
| 27 | static const struct error_code scmi_linux_errmap[] = { |
| 28 | { .scmi = SCMI_NOT_SUPPORTED, .errno = -EOPNOTSUPP, }, |
| 29 | { .scmi = SCMI_INVALID_PARAMETERS, .errno = -EINVAL, }, |
| 30 | { .scmi = SCMI_DENIED, .errno = -EACCES, }, |
| 31 | { .scmi = SCMI_NOT_FOUND, .errno = -ENOENT, }, |
| 32 | { .scmi = SCMI_OUT_OF_RANGE, .errno = -ERANGE, }, |
| 33 | { .scmi = SCMI_BUSY, .errno = -EBUSY, }, |
| 34 | { .scmi = SCMI_COMMS_ERROR, .errno = -ECOMM, }, |
| 35 | { .scmi = SCMI_GENERIC_ERROR, .errno = -EIO, }, |
| 36 | { .scmi = SCMI_HARDWARE_ERROR, .errno = -EREMOTEIO, }, |
| 37 | { .scmi = SCMI_PROTOCOL_ERROR, .errno = -EPROTO, }, |
| 38 | }; |
| 39 | |
| 40 | int scmi_to_linux_errno(s32 scmi_code) |
| 41 | { |
| 42 | int n; |
| 43 | |
| 44 | if (!scmi_code) |
| 45 | return 0; |
| 46 | |
| 47 | for (n = 0; n < ARRAY_SIZE(scmi_linux_errmap); n++) |
| 48 | if (scmi_code == scmi_linux_errmap[n].scmi) |
AKASHI Takahiro | b1d774b | 2023-06-13 10:30:45 +0900 | [diff] [blame] | 49 | return scmi_linux_errmap[n].errno; |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 50 | |
| 51 | return -EPROTO; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * SCMI agent devices binds devices of various uclasses depeding on |
| 56 | * the FDT description. scmi_bind_protocol() is a generic bind sequence |
| 57 | * called by the uclass at bind stage, that is uclass post_bind. |
| 58 | */ |
| 59 | static int scmi_bind_protocols(struct udevice *dev) |
| 60 | { |
| 61 | int ret = 0; |
| 62 | ofnode node; |
Patrick Delaunay | 55b0aff | 2022-11-25 12:56:29 +0100 | [diff] [blame] | 63 | const char *name; |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 64 | |
| 65 | dev_for_each_subnode(node, dev) { |
Etienne Carriere | 6038884 | 2020-09-09 18:44:04 +0200 | [diff] [blame] | 66 | struct driver *drv = NULL; |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 67 | u32 protocol_id; |
| 68 | |
Simon Glass | 8909066 | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 69 | if (!ofnode_is_enabled(node)) |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 70 | continue; |
| 71 | |
| 72 | if (ofnode_read_u32(node, "reg", &protocol_id)) |
| 73 | continue; |
| 74 | |
Patrick Delaunay | 55b0aff | 2022-11-25 12:56:29 +0100 | [diff] [blame] | 75 | name = ofnode_get_name(node); |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 76 | switch (protocol_id) { |
Etienne Carriere | 6038884 | 2020-09-09 18:44:04 +0200 | [diff] [blame] | 77 | case SCMI_PROTOCOL_ID_CLOCK: |
Jonas Karlman | 2229758 | 2023-04-17 19:07:18 +0000 | [diff] [blame] | 78 | if (CONFIG_IS_ENABLED(CLK_SCMI)) |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 79 | drv = DM_DRIVER_GET(scmi_clock); |
Etienne Carriere | 6038884 | 2020-09-09 18:44:04 +0200 | [diff] [blame] | 80 | break; |
Etienne Carriere | 34d76fe | 2020-09-09 18:44:06 +0200 | [diff] [blame] | 81 | case SCMI_PROTOCOL_ID_RESET_DOMAIN: |
| 82 | if (IS_ENABLED(CONFIG_RESET_SCMI)) |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 83 | drv = DM_DRIVER_GET(scmi_reset_domain); |
Etienne Carriere | 34d76fe | 2020-09-09 18:44:06 +0200 | [diff] [blame] | 84 | break; |
Etienne Carriere | 1f213ee | 2021-03-08 22:38:06 +0100 | [diff] [blame] | 85 | case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN: |
| 86 | if (IS_ENABLED(CONFIG_DM_REGULATOR_SCMI)) { |
| 87 | node = ofnode_find_subnode(node, "regulators"); |
| 88 | if (!ofnode_valid(node)) { |
| 89 | dev_err(dev, "no regulators node\n"); |
| 90 | return -ENXIO; |
| 91 | } |
| 92 | drv = DM_DRIVER_GET(scmi_voltage_domain); |
| 93 | } |
| 94 | break; |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 95 | default: |
Etienne Carriere | 6038884 | 2020-09-09 18:44:04 +0200 | [diff] [blame] | 96 | break; |
| 97 | } |
| 98 | |
| 99 | if (!drv) { |
| 100 | dev_dbg(dev, "Ignore unsupported SCMI protocol %#x\n", |
| 101 | protocol_id); |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 102 | continue; |
| 103 | } |
| 104 | |
Patrick Delaunay | 55b0aff | 2022-11-25 12:56:29 +0100 | [diff] [blame] | 105 | ret = device_bind(dev, drv, name, NULL, node, NULL); |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 106 | if (ret) |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
Etienne Carriere | 5a11df3 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 113 | static struct udevice *find_scmi_transport_device(struct udevice *dev) |
| 114 | { |
| 115 | struct udevice *parent = dev; |
| 116 | |
| 117 | do { |
| 118 | parent = dev_get_parent(parent); |
| 119 | } while (parent && device_get_uclass_id(parent) != UCLASS_SCMI_AGENT); |
| 120 | |
| 121 | if (!parent) |
| 122 | dev_err(dev, "Invalid SCMI device, agent not found\n"); |
| 123 | |
| 124 | return parent; |
| 125 | } |
| 126 | |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 127 | static const struct scmi_agent_ops *transport_dev_ops(struct udevice *dev) |
| 128 | { |
| 129 | return (const struct scmi_agent_ops *)dev->driver->ops; |
| 130 | } |
| 131 | |
Etienne Carriere | 8e96801 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 132 | int devm_scmi_of_get_channel(struct udevice *dev, struct scmi_channel **channel) |
| 133 | { |
| 134 | struct udevice *parent; |
| 135 | |
| 136 | parent = find_scmi_transport_device(dev); |
| 137 | if (!parent) |
| 138 | return -ENODEV; |
| 139 | |
| 140 | if (transport_dev_ops(parent)->of_get_channel) |
Patrick Delaunay | eebb967 | 2022-09-30 09:36:38 +0200 | [diff] [blame] | 141 | return transport_dev_ops(parent)->of_get_channel(parent, channel); |
Etienne Carriere | 8e96801 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 142 | |
| 143 | /* Drivers without a get_channel operator don't need a channel ref */ |
| 144 | *channel = NULL; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Etienne Carriere | 8bcb1b4 | 2022-05-31 18:09:18 +0200 | [diff] [blame] | 149 | int devm_scmi_process_msg(struct udevice *dev, struct scmi_channel *channel, |
| 150 | struct scmi_msg *msg) |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 151 | { |
Etienne Carriere | 6983710 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 152 | const struct scmi_agent_ops *ops; |
Etienne Carriere | 5a11df3 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 153 | struct udevice *parent; |
Etienne Carriere | 6983710 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 154 | |
Etienne Carriere | 5a11df3 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 155 | parent = find_scmi_transport_device(dev); |
| 156 | if (!parent) |
Etienne Carriere | 6983710 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 157 | return -ENODEV; |
Etienne Carriere | 6983710 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 158 | |
| 159 | ops = transport_dev_ops(parent); |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 160 | |
| 161 | if (ops->process_msg) |
Etienne Carriere | 8e96801 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 162 | return ops->process_msg(parent, channel, msg); |
Etienne Carriere | 358599e | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 163 | |
| 164 | return -EPROTONOSUPPORT; |
| 165 | } |
| 166 | |
| 167 | UCLASS_DRIVER(scmi_agent) = { |
| 168 | .id = UCLASS_SCMI_AGENT, |
| 169 | .name = "scmi_agent", |
| 170 | .post_bind = scmi_bind_protocols, |
| 171 | }; |