blob: 972c6addde21b7338cb4183b9fcaf257fe85d77d [file] [log] [blame]
Etienne Carrierea26d0ec2020-09-09 18:44:02 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Linaro Limited.
4 */
5
Patrick Delaunay5dd84d42021-02-24 11:19:44 +01006#define LOG_CATEGORY UCLASS_SCMI_AGENT
7
Etienne Carrierea26d0ec2020-09-09 18:44:02 +02008#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <scmi_agent.h>
12#include <scmi_agent-uclass.h>
13#include <dm/devres.h>
Patrick Delaunay2f7d3a12021-02-24 11:19:43 +010014#include <dm/device_compat.h>
Etienne Carrierea26d0ec2020-09-09 18:44:02 +020015#include <dm/device-internal.h>
16#include <linux/arm-smccc.h>
17#include <linux/compat.h>
18
19#include "smt.h"
20
21#define SMCCC_RET_NOT_SUPPORTED ((unsigned long)-1)
22
23/**
24 * struct scmi_smccc_channel - Description of an SCMI SMCCC transport
25 * @func_id: SMCCC function ID used by the SCMI transport
26 * @smt: Shared memory buffer
27 */
28struct scmi_smccc_channel {
29 ulong func_id;
30 struct scmi_smt smt;
31};
32
Etienne Carrieref28df1d2022-05-31 18:09:23 +020033/**
34 * struct scmi_channel - Channel instance referenced in SCMI drivers
35 * @ref: Reference to local channel instance
36 **/
37struct scmi_channel {
38 struct scmi_smccc_channel ref;
39};
40
Etienne Carriere05440292022-05-31 18:09:19 +020041static int scmi_smccc_process_msg(struct udevice *dev,
42 struct scmi_channel *channel,
43 struct scmi_msg *msg)
Etienne Carrierea26d0ec2020-09-09 18:44:02 +020044{
Etienne Carriere76b690a2022-05-31 18:09:29 +020045 struct scmi_smccc_channel *chan = &channel->ref;
Etienne Carrierea26d0ec2020-09-09 18:44:02 +020046 struct arm_smccc_res res;
47 int ret;
48
49 ret = scmi_write_msg_to_smt(dev, &chan->smt, msg);
50 if (ret)
51 return ret;
52
53 arm_smccc_smc(chan->func_id, 0, 0, 0, 0, 0, 0, 0, &res);
54 if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
55 ret = -ENXIO;
56 else
57 ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
58
59 scmi_clear_smt_channel(&chan->smt);
60
61 return ret;
62}
63
Etienne Carrieref28df1d2022-05-31 18:09:23 +020064static int setup_channel(struct udevice *dev, struct scmi_smccc_channel *chan)
Etienne Carrierea26d0ec2020-09-09 18:44:02 +020065{
Etienne Carrierea26d0ec2020-09-09 18:44:02 +020066 u32 func_id;
67 int ret;
68
69 if (dev_read_u32(dev, "arm,smc-id", &func_id)) {
70 dev_err(dev, "Missing property func-id\n");
71 return -EINVAL;
72 }
73
74 chan->func_id = func_id;
75
76 ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
Etienne Carrieref6942352021-11-08 08:56:11 +010077 if (ret)
Etienne Carrierea26d0ec2020-09-09 18:44:02 +020078 dev_err(dev, "Failed to get smt resources: %d\n", ret);
Etienne Carrierea26d0ec2020-09-09 18:44:02 +020079
Etienne Carrieref6942352021-11-08 08:56:11 +010080 return ret;
Etienne Carrierea26d0ec2020-09-09 18:44:02 +020081}
82
Etienne Carrieref28df1d2022-05-31 18:09:23 +020083static int scmi_smccc_get_channel(struct udevice *dev,
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +090084 struct udevice *protocol,
Etienne Carrieref28df1d2022-05-31 18:09:23 +020085 struct scmi_channel **channel)
86{
Patrick Delaunay3d8b9502022-09-30 09:36:38 +020087 struct scmi_smccc_channel *base_chan = dev_get_plat(dev);
Etienne Carrieref28df1d2022-05-31 18:09:23 +020088 struct scmi_smccc_channel *chan;
89 u32 func_id;
90 int ret;
91
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +090092 if (dev_read_u32(protocol, "arm,smc-id", &func_id)) {
Etienne Carrieref28df1d2022-05-31 18:09:23 +020093 /* Uses agent base channel */
94 *channel = container_of(base_chan, struct scmi_channel, ref);
95
96 return 0;
97 }
98
99 /* Setup a dedicated channel */
100 chan = calloc(1, sizeof(*chan));
101 if (!chan)
102 return -ENOMEM;
103
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +0900104 ret = setup_channel(protocol, chan);
Etienne Carrieref28df1d2022-05-31 18:09:23 +0200105 if (ret) {
106 free(chan);
107 return ret;
108 }
109
110 *channel = container_of(chan, struct scmi_channel, ref);
111
112 return 0;
113}
114
115static int scmi_smccc_of_to_plat(struct udevice *dev)
116{
117 struct scmi_smccc_channel *chan = dev_get_plat(dev);
118
119 return setup_channel(dev, chan);
120}
121
Etienne Carrierea26d0ec2020-09-09 18:44:02 +0200122static const struct udevice_id scmi_smccc_ids[] = {
123 { .compatible = "arm,scmi-smc" },
124 { }
125};
126
127static const struct scmi_agent_ops scmi_smccc_ops = {
Etienne Carrieref28df1d2022-05-31 18:09:23 +0200128 .of_get_channel = scmi_smccc_get_channel,
Etienne Carrierea26d0ec2020-09-09 18:44:02 +0200129 .process_msg = scmi_smccc_process_msg,
130};
131
132U_BOOT_DRIVER(scmi_smccc) = {
133 .name = "scmi-over-smccc",
134 .id = UCLASS_SCMI_AGENT,
135 .of_match = scmi_smccc_ids,
Etienne Carriere213d5cf2021-11-08 08:56:10 +0100136 .plat_auto = sizeof(struct scmi_smccc_channel),
137 .of_to_plat = scmi_smccc_of_to_plat,
Etienne Carrierea26d0ec2020-09-09 18:44:02 +0200138 .ops = &scmi_smccc_ops,
139};