Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +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 | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <scmi_agent.h> |
| 11 | #include <scmi_agent-uclass.h> |
| 12 | #include <dm/devres.h> |
Patrick Delaunay | 2f7d3a1 | 2021-02-24 11:19:43 +0100 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
| 15 | #include <linux/arm-smccc.h> |
| 16 | #include <linux/compat.h> |
| 17 | |
| 18 | #include "smt.h" |
| 19 | |
| 20 | #define SMCCC_RET_NOT_SUPPORTED ((unsigned long)-1) |
| 21 | |
| 22 | /** |
| 23 | * struct scmi_smccc_channel - Description of an SCMI SMCCC transport |
| 24 | * @func_id: SMCCC function ID used by the SCMI transport |
| 25 | * @smt: Shared memory buffer |
| 26 | */ |
| 27 | struct scmi_smccc_channel { |
| 28 | ulong func_id; |
| 29 | struct scmi_smt smt; |
| 30 | }; |
| 31 | |
Etienne Carriere | f28df1d | 2022-05-31 18:09:23 +0200 | [diff] [blame] | 32 | /** |
| 33 | * struct scmi_channel - Channel instance referenced in SCMI drivers |
| 34 | * @ref: Reference to local channel instance |
| 35 | **/ |
| 36 | struct scmi_channel { |
| 37 | struct scmi_smccc_channel ref; |
| 38 | }; |
| 39 | |
Etienne Carriere | 0544029 | 2022-05-31 18:09:19 +0200 | [diff] [blame] | 40 | static int scmi_smccc_process_msg(struct udevice *dev, |
| 41 | struct scmi_channel *channel, |
| 42 | struct scmi_msg *msg) |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 43 | { |
Etienne Carriere | 76b690a | 2022-05-31 18:09:29 +0200 | [diff] [blame] | 44 | struct scmi_smccc_channel *chan = &channel->ref; |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 45 | struct arm_smccc_res res; |
| 46 | int ret; |
| 47 | |
| 48 | ret = scmi_write_msg_to_smt(dev, &chan->smt, msg); |
| 49 | if (ret) |
| 50 | return ret; |
| 51 | |
| 52 | arm_smccc_smc(chan->func_id, 0, 0, 0, 0, 0, 0, 0, &res); |
| 53 | if (res.a0 == SMCCC_RET_NOT_SUPPORTED) |
| 54 | ret = -ENXIO; |
| 55 | else |
| 56 | ret = scmi_read_resp_from_smt(dev, &chan->smt, msg); |
| 57 | |
| 58 | scmi_clear_smt_channel(&chan->smt); |
| 59 | |
| 60 | return ret; |
| 61 | } |
| 62 | |
Etienne Carriere | f28df1d | 2022-05-31 18:09:23 +0200 | [diff] [blame] | 63 | static int setup_channel(struct udevice *dev, struct scmi_smccc_channel *chan) |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 64 | { |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 65 | u32 func_id; |
| 66 | int ret; |
| 67 | |
| 68 | if (dev_read_u32(dev, "arm,smc-id", &func_id)) { |
| 69 | dev_err(dev, "Missing property func-id\n"); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | chan->func_id = func_id; |
| 74 | |
| 75 | ret = scmi_dt_get_smt_buffer(dev, &chan->smt); |
Etienne Carriere | f694235 | 2021-11-08 08:56:11 +0100 | [diff] [blame] | 76 | if (ret) |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 77 | dev_err(dev, "Failed to get smt resources: %d\n", ret); |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 78 | |
Etienne Carriere | f694235 | 2021-11-08 08:56:11 +0100 | [diff] [blame] | 79 | return ret; |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Etienne Carriere | f28df1d | 2022-05-31 18:09:23 +0200 | [diff] [blame] | 82 | static int scmi_smccc_get_channel(struct udevice *dev, |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 83 | struct udevice *protocol, |
Etienne Carriere | f28df1d | 2022-05-31 18:09:23 +0200 | [diff] [blame] | 84 | struct scmi_channel **channel) |
| 85 | { |
Patrick Delaunay | 3d8b950 | 2022-09-30 09:36:38 +0200 | [diff] [blame] | 86 | struct scmi_smccc_channel *base_chan = dev_get_plat(dev); |
Etienne Carriere | f28df1d | 2022-05-31 18:09:23 +0200 | [diff] [blame] | 87 | struct scmi_smccc_channel *chan; |
| 88 | u32 func_id; |
| 89 | int ret; |
| 90 | |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 91 | if (dev_read_u32(protocol, "arm,smc-id", &func_id)) { |
Etienne Carriere | f28df1d | 2022-05-31 18:09:23 +0200 | [diff] [blame] | 92 | /* Uses agent base channel */ |
| 93 | *channel = container_of(base_chan, struct scmi_channel, ref); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /* Setup a dedicated channel */ |
| 99 | chan = calloc(1, sizeof(*chan)); |
| 100 | if (!chan) |
| 101 | return -ENOMEM; |
| 102 | |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 103 | ret = setup_channel(protocol, chan); |
Etienne Carriere | f28df1d | 2022-05-31 18:09:23 +0200 | [diff] [blame] | 104 | if (ret) { |
| 105 | free(chan); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | *channel = container_of(chan, struct scmi_channel, ref); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int scmi_smccc_of_to_plat(struct udevice *dev) |
| 115 | { |
| 116 | struct scmi_smccc_channel *chan = dev_get_plat(dev); |
| 117 | |
| 118 | return setup_channel(dev, chan); |
| 119 | } |
| 120 | |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 121 | static const struct udevice_id scmi_smccc_ids[] = { |
| 122 | { .compatible = "arm,scmi-smc" }, |
| 123 | { } |
| 124 | }; |
| 125 | |
| 126 | static const struct scmi_agent_ops scmi_smccc_ops = { |
Etienne Carriere | f28df1d | 2022-05-31 18:09:23 +0200 | [diff] [blame] | 127 | .of_get_channel = scmi_smccc_get_channel, |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 128 | .process_msg = scmi_smccc_process_msg, |
| 129 | }; |
| 130 | |
| 131 | U_BOOT_DRIVER(scmi_smccc) = { |
| 132 | .name = "scmi-over-smccc", |
| 133 | .id = UCLASS_SCMI_AGENT, |
| 134 | .of_match = scmi_smccc_ids, |
Etienne Carriere | 213d5cf | 2021-11-08 08:56:10 +0100 | [diff] [blame] | 135 | .plat_auto = sizeof(struct scmi_smccc_channel), |
| 136 | .of_to_plat = scmi_smccc_of_to_plat, |
Etienne Carriere | a26d0ec | 2020-09-09 18:44:02 +0200 | [diff] [blame] | 137 | .ops = &scmi_smccc_ops, |
| 138 | }; |