blob: e63b67c5ee8ccc1751c4819468483dac067e8699 [file] [log] [blame]
Etienne Carriere2b412082020-09-09 18:44:01 +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 Carriere2b412082020-09-09 18:44:01 +02008#include <common.h>
9#include <dm.h>
Etienne Carriere2b412082020-09-09 18:44:01 +020010#include <errno.h>
11#include <mailbox.h>
12#include <scmi_agent.h>
13#include <scmi_agent-uclass.h>
Patrick Delaunayd4c9f682021-02-24 11:19:45 +010014#include <dm/device_compat.h>
Etienne Carriere2b412082020-09-09 18:44:01 +020015#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 */
28struct scmi_mbox_channel {
29 struct scmi_smt smt;
30 struct mbox_chan mbox;
31 ulong timeout_us;
32};
33
Etienne Carriere274c0c22022-05-31 18:09:22 +020034/**
35 * struct scmi_channel - Channel instance referenced in SCMI drivers
36 * @ref: Reference to local channel instance
37 **/
38struct scmi_channel {
39 struct scmi_mbox_channel ref;
40};
41
Etienne Carriere05440292022-05-31 18:09:19 +020042static int scmi_mbox_process_msg(struct udevice *dev,
43 struct scmi_channel *channel,
44 struct scmi_msg *msg)
Etienne Carriere2b412082020-09-09 18:44:01 +020045{
Etienne Carriere3119e272021-11-08 08:56:09 +010046 struct scmi_mbox_channel *chan = dev_get_plat(dev);
Etienne Carriere2b412082020-09-09 18:44:01 +020047 int ret;
48
Etienne Carriere274c0c22022-05-31 18:09:22 +020049 /* Support SCMI drivers upgraded to of_get_channel operator */
50 if (channel)
51 chan = &channel->ref;
52
Etienne Carriere2b412082020-09-09 18:44:01 +020053 ret = scmi_write_msg_to_smt(dev, &chan->smt, msg);
54 if (ret)
55 return ret;
56
57 /* Give shm addr to mbox in case it is meaningful */
58 ret = mbox_send(&chan->mbox, chan->smt.buf);
59 if (ret) {
60 dev_err(dev, "Message send failed: %d\n", ret);
61 goto out;
62 }
63
64 /* Receive the response */
65 ret = mbox_recv(&chan->mbox, chan->smt.buf, chan->timeout_us);
66 if (ret) {
67 dev_err(dev, "Response failed: %d, abort\n", ret);
68 goto out;
69 }
70
71 ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
72
73out:
74 scmi_clear_smt_channel(&chan->smt);
75
76 return ret;
77}
78
Etienne Carriere274c0c22022-05-31 18:09:22 +020079static int setup_channel(struct udevice *dev, struct scmi_mbox_channel *chan)
Etienne Carriere2b412082020-09-09 18:44:01 +020080{
Etienne Carriere2b412082020-09-09 18:44:01 +020081 int ret;
82
Etienne Carriere2b412082020-09-09 18:44:01 +020083 ret = mbox_get_by_index(dev, 0, &chan->mbox);
84 if (ret) {
85 dev_err(dev, "Failed to find mailbox: %d\n", ret);
Etienne Carriereadbf0fc2021-11-08 08:56:08 +010086 return ret;
Etienne Carriere2b412082020-09-09 18:44:01 +020087 }
88
89 ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
Etienne Carriere274c0c22022-05-31 18:09:22 +020090 if (ret) {
Etienne Carriere2b412082020-09-09 18:44:01 +020091 dev_err(dev, "Failed to get shm resources: %d\n", ret);
Etienne Carriere274c0c22022-05-31 18:09:22 +020092 return ret;
93 }
Etienne Carriere2b412082020-09-09 18:44:01 +020094
Etienne Carriere274c0c22022-05-31 18:09:22 +020095 chan->timeout_us = TIMEOUT_US_10MS;
96
97 return 0;
98}
99
100static int scmi_mbox_get_channel(struct udevice *dev,
101 struct scmi_channel **channel)
102{
103 struct scmi_mbox_channel *base_chan = dev_get_plat(dev->parent);
104 struct scmi_mbox_channel *chan;
105 int ret;
106
107 if (!dev_read_prop(dev, "shmem", NULL)) {
108 /* Uses agent base channel */
109 *channel = container_of(base_chan, struct scmi_channel, ref);
110
111 return 0;
112 }
113
114 chan = calloc(1, sizeof(*chan));
115 if (!chan)
116 return -ENOMEM;
117
118 /* Setup a dedicated channel for the protocol */
119 ret = setup_channel(dev, chan);
120 if (ret) {
121 free(chan);
122 return ret;
123 }
124
125 *channel = (void *)chan;
126
127 return 0;
128}
129
130int scmi_mbox_of_to_plat(struct udevice *dev)
131{
132 struct scmi_mbox_channel *chan = dev_get_plat(dev);
133
134 return setup_channel(dev, chan);
Etienne Carriere2b412082020-09-09 18:44:01 +0200135}
136
137static const struct udevice_id scmi_mbox_ids[] = {
138 { .compatible = "arm,scmi" },
139 { }
140};
141
142static const struct scmi_agent_ops scmi_mbox_ops = {
Etienne Carriere274c0c22022-05-31 18:09:22 +0200143 .of_get_channel = scmi_mbox_get_channel,
Etienne Carriere2b412082020-09-09 18:44:01 +0200144 .process_msg = scmi_mbox_process_msg,
145};
146
147U_BOOT_DRIVER(scmi_mbox) = {
148 .name = "scmi-over-mailbox",
149 .id = UCLASS_SCMI_AGENT,
150 .of_match = scmi_mbox_ids,
Etienne Carriere3119e272021-11-08 08:56:09 +0100151 .plat_auto = sizeof(struct scmi_mbox_channel),
152 .of_to_plat = scmi_mbox_of_to_plat,
Etienne Carriere2b412082020-09-09 18:44:01 +0200153 .ops = &scmi_mbox_ops,
154};