1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Ruslan Bukin <br@bsdpad.com>
5 * Copyright (c) 2023 Arm Ltd
6 *
7 * This work was supported by Innovate UK project 105694, "Digital Security
8 * by Design (DSbD) Technology Platform Prototype".
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/bus.h>
37#include <sys/cpu.h>
38#include <sys/kernel.h>
39#include <sys/module.h>
40
41#include <dev/fdt/simplebus.h>
42#include <dev/fdt/fdt_common.h>
43#include <dev/ofw/ofw_bus_subr.h>
44
45#include "dev/mailbox/arm/arm_doorbell.h"
46
47#include "scmi.h"
48#include "scmi_protocols.h"
49#include "scmi_shmem.h"
50
51#define SCMI_MBOX_POLL_INTERVAL_MS	3
52
53struct scmi_mailbox_softc {
54	struct scmi_softc	base;
55	device_t		a2p_dev;
56	struct arm_doorbell	*db;
57};
58
59static int	scmi_mailbox_transport_init(device_t);
60static void	scmi_mailbox_transport_cleanup(device_t);
61static int	scmi_mailbox_xfer_msg(device_t, struct scmi_msg *);
62static int	scmi_mailbox_poll_msg(device_t, struct scmi_msg *,
63    unsigned int);
64static int	scmi_mailbox_collect_reply(device_t, struct scmi_msg *);
65static void	scmi_mailbox_tx_complete(device_t, void *);
66
67static int	scmi_mailbox_probe(device_t);
68
69static void
70scmi_mailbox_a2p_callback(void *arg)
71{
72	struct scmi_mailbox_softc *sc;
73	uint32_t msg_header;
74	int ret;
75
76	sc = arg;
77
78	ret = scmi_shmem_read_msg_header(sc->a2p_dev, &msg_header);
79	if (ret == 0)
80		scmi_rx_irq_callback(sc->base.dev, sc->a2p_dev, msg_header);
81}
82
83static int
84scmi_mailbox_transport_init(device_t dev)
85{
86	struct scmi_mailbox_softc *sc;
87	phandle_t node;
88
89	sc = device_get_softc(dev);
90
91	node = ofw_bus_get_node(dev);
92	if (node == -1)
93		return (ENXIO);
94	/*
95	 * TODO
96	 * - Support P2A shmem + IRQ/doorbell
97	 * - Support other mailbox devices
98	 */
99	sc->a2p_dev = scmi_shmem_get(dev, node, SCMI_CHAN_A2P);
100	if (sc->a2p_dev == NULL) {
101		device_printf(dev, "A2P shmem dev not found.\n");
102		return (ENXIO);
103	}
104
105	/* TODO: Fix ofw_get...mbox doorbell names NOT required in Linux DT */
106	sc->db = arm_doorbell_ofw_get(dev, "tx");
107	if (sc->db == NULL) {
108		device_printf(dev, "Doorbell device not found.\n");
109		return (ENXIO);
110	}
111
112	sc->base.trs_desc.reply_timo_ms = 30;
113
114	arm_doorbell_set_handler(sc->db, scmi_mailbox_a2p_callback, sc);
115
116	return (0);
117}
118
119static void
120scmi_mailbox_transport_cleanup(device_t dev)
121{
122	struct scmi_mailbox_softc *sc;
123
124	sc = device_get_softc(dev);
125
126	arm_doorbell_set_handler(sc->db, NULL, NULL);
127}
128
129static int
130scmi_mailbox_xfer_msg(device_t dev, struct scmi_msg *msg)
131{
132	struct scmi_mailbox_softc *sc;
133	int ret;
134
135	sc = device_get_softc(dev);
136
137	ret = scmi_shmem_prepare_msg(sc->a2p_dev, (uint8_t *)&msg->hdr,
138	    msg->tx_len, msg->polling);
139	if (ret != 0)
140		return (ret);
141
142	/* Interrupt SCP firmware. */
143	arm_doorbell_set(sc->db);
144
145	return (0);
146}
147
148static int
149scmi_mailbox_poll_msg(device_t dev, struct scmi_msg *msg, unsigned int tmo_ms)
150{
151	struct scmi_mailbox_softc *sc;
152	unsigned int tmo_loops = tmo_ms / SCMI_MBOX_POLL_INTERVAL_MS;
153
154	sc = device_get_softc(dev);
155
156	do {
157		if (scmi_shmem_poll_msg(sc->a2p_dev, &msg->hdr))
158			break;
159		DELAY(SCMI_MBOX_POLL_INTERVAL_MS * 1000);
160	} while (tmo_loops--);
161
162	return (tmo_loops ? 0 : 1);
163}
164
165static int
166scmi_mailbox_collect_reply(device_t dev, struct scmi_msg *msg)
167{
168	struct scmi_mailbox_softc *sc;
169	int ret;
170
171	sc = device_get_softc(dev);
172
173	ret = scmi_shmem_read_msg_payload(sc->a2p_dev,
174	    msg->payld, msg->rx_len - SCMI_MSG_HDR_SIZE);
175
176	return (ret);
177}
178
179static void
180scmi_mailbox_tx_complete(device_t dev, void *chan)
181{
182	struct scmi_mailbox_softc *sc;
183
184	sc = device_get_softc(dev);
185	scmi_shmem_tx_complete(sc->a2p_dev);
186}
187
188static void
189scmi_mailbox_clear_channel(device_t dev, void *chan)
190{
191	/* Only P2A channel can be cleared forcibly by agent */
192	scmi_shmem_clear_channel(chan);
193}
194
195static int
196scmi_mailbox_probe(device_t dev)
197{
198
199	if (!ofw_bus_is_compatible(dev, "arm,scmi"))
200		return (ENXIO);
201
202	if (!ofw_bus_status_okay(dev))
203		return (ENXIO);
204
205	device_set_desc(dev, "ARM SCMI Mailbox Transport driver");
206
207	return (BUS_PROBE_DEFAULT);
208}
209
210static device_method_t scmi_mailbox_methods[] = {
211	DEVMETHOD(device_probe,		scmi_mailbox_probe),
212
213	/* SCMI interface */
214	DEVMETHOD(scmi_transport_init,		scmi_mailbox_transport_init),
215	DEVMETHOD(scmi_transport_cleanup,	scmi_mailbox_transport_cleanup),
216	DEVMETHOD(scmi_xfer_msg,		scmi_mailbox_xfer_msg),
217	DEVMETHOD(scmi_poll_msg,		scmi_mailbox_poll_msg),
218	DEVMETHOD(scmi_collect_reply,		scmi_mailbox_collect_reply),
219	DEVMETHOD(scmi_tx_complete,		scmi_mailbox_tx_complete),
220	DEVMETHOD(scmi_clear_channel,		scmi_mailbox_clear_channel),
221
222	DEVMETHOD_END
223};
224
225DEFINE_CLASS_1(scmi_mailbox, scmi_mailbox_driver, scmi_mailbox_methods,
226    sizeof(struct scmi_mailbox_softc), scmi_driver);
227
228DRIVER_MODULE(scmi_mailbox, simplebus, scmi_mailbox_driver, 0, 0);
229MODULE_VERSION(scmi_mailbox, 1);
230