1*54b96380SRuslan Bukin /*-
2*54b96380SRuslan Bukin * SPDX-License-Identifier: BSD-2-Clause
3*54b96380SRuslan Bukin *
4*54b96380SRuslan Bukin * Copyright (c) 2022 Ruslan Bukin <br@bsdpad.com>
5*54b96380SRuslan Bukin *
6*54b96380SRuslan Bukin * This work was supported by Innovate UK project 105694, "Digital Security
7*54b96380SRuslan Bukin * by Design (DSbD) Technology Platform Prototype".
8*54b96380SRuslan Bukin *
9*54b96380SRuslan Bukin * Redistribution and use in source and binary forms, with or without
10*54b96380SRuslan Bukin * modification, are permitted provided that the following conditions
11*54b96380SRuslan Bukin * are met:
12*54b96380SRuslan Bukin * 1. Redistributions of source code must retain the above copyright
13*54b96380SRuslan Bukin * notice, this list of conditions and the following disclaimer.
14*54b96380SRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright
15*54b96380SRuslan Bukin * notice, this list of conditions and the following disclaimer in the
16*54b96380SRuslan Bukin * documentation and/or other materials provided with the distribution.
17*54b96380SRuslan Bukin *
18*54b96380SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*54b96380SRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*54b96380SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*54b96380SRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*54b96380SRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*54b96380SRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*54b96380SRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*54b96380SRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*54b96380SRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*54b96380SRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*54b96380SRuslan Bukin * SUCH DAMAGE.
29*54b96380SRuslan Bukin */
30*54b96380SRuslan Bukin
31*54b96380SRuslan Bukin #include <sys/param.h>
32*54b96380SRuslan Bukin #include <sys/systm.h>
33*54b96380SRuslan Bukin #include <sys/bus.h>
34*54b96380SRuslan Bukin #include <sys/rman.h>
35*54b96380SRuslan Bukin #include <sys/kernel.h>
36*54b96380SRuslan Bukin #include <sys/module.h>
37*54b96380SRuslan Bukin
38*54b96380SRuslan Bukin #include <machine/bus.h>
39*54b96380SRuslan Bukin
40*54b96380SRuslan Bukin #include <dev/fdt/simplebus.h>
41*54b96380SRuslan Bukin #include <dev/fdt/fdt_common.h>
42*54b96380SRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
43*54b96380SRuslan Bukin
44*54b96380SRuslan Bukin #include "arm_doorbell.h"
45*54b96380SRuslan Bukin
46*54b96380SRuslan Bukin #define MHU_CHAN_RX_LP 0x000 /* Low priority channel */
47*54b96380SRuslan Bukin #define MHU_CHAN_RX_HP 0x020 /* High priority channel */
48*54b96380SRuslan Bukin #define MHU_CHAN_RX_SEC 0x200 /* Secure channel */
49*54b96380SRuslan Bukin #define MHU_INTR_STAT 0x00
50*54b96380SRuslan Bukin #define MHU_INTR_SET 0x08
51*54b96380SRuslan Bukin #define MHU_INTR_CLEAR 0x10
52*54b96380SRuslan Bukin
53*54b96380SRuslan Bukin #define MHU_TX_REG_OFFSET 0x100
54*54b96380SRuslan Bukin
55*54b96380SRuslan Bukin #define DOORBELL_N_CHANNELS 3
56*54b96380SRuslan Bukin #define DOORBELL_N_DOORBELLS (DOORBELL_N_CHANNELS * 32)
57*54b96380SRuslan Bukin
58*54b96380SRuslan Bukin struct arm_doorbell dbells[DOORBELL_N_DOORBELLS];
59*54b96380SRuslan Bukin
60*54b96380SRuslan Bukin static struct resource_spec arm_doorbell_spec[] = {
61*54b96380SRuslan Bukin { SYS_RES_MEMORY, 0, RF_ACTIVE },
62*54b96380SRuslan Bukin { SYS_RES_IRQ, 0, RF_ACTIVE },
63*54b96380SRuslan Bukin { SYS_RES_IRQ, 1, RF_ACTIVE },
64*54b96380SRuslan Bukin { -1, 0 }
65*54b96380SRuslan Bukin };
66*54b96380SRuslan Bukin
67*54b96380SRuslan Bukin struct arm_doorbell_softc {
68*54b96380SRuslan Bukin struct resource *res[3];
69*54b96380SRuslan Bukin void *lp_intr_cookie;
70*54b96380SRuslan Bukin void *hp_intr_cookie;
71*54b96380SRuslan Bukin device_t dev;
72*54b96380SRuslan Bukin };
73*54b96380SRuslan Bukin
74*54b96380SRuslan Bukin static void
arm_doorbell_lp_intr(void * arg)75*54b96380SRuslan Bukin arm_doorbell_lp_intr(void *arg)
76*54b96380SRuslan Bukin {
77*54b96380SRuslan Bukin struct arm_doorbell_softc *sc;
78*54b96380SRuslan Bukin struct arm_doorbell *db;
79*54b96380SRuslan Bukin uint32_t reg;
80*54b96380SRuslan Bukin int i;
81*54b96380SRuslan Bukin
82*54b96380SRuslan Bukin sc = arg;
83*54b96380SRuslan Bukin
84*54b96380SRuslan Bukin reg = bus_read_4(sc->res[0], MHU_CHAN_RX_LP + MHU_INTR_STAT);
85*54b96380SRuslan Bukin for (i = 0; i < 32; i++) {
86*54b96380SRuslan Bukin if (reg & (1 << i)) {
87*54b96380SRuslan Bukin db = &dbells[i];
88*54b96380SRuslan Bukin bus_write_4(sc->res[0], MHU_CHAN_RX_LP + MHU_INTR_CLEAR,
89*54b96380SRuslan Bukin (1 << i));
90*54b96380SRuslan Bukin if (db->func != NULL)
91*54b96380SRuslan Bukin db->func(db->arg);
92*54b96380SRuslan Bukin }
93*54b96380SRuslan Bukin }
94*54b96380SRuslan Bukin }
95*54b96380SRuslan Bukin
96*54b96380SRuslan Bukin static void
arm_doorbell_hp_intr(void * arg)97*54b96380SRuslan Bukin arm_doorbell_hp_intr(void *arg)
98*54b96380SRuslan Bukin {
99*54b96380SRuslan Bukin struct arm_doorbell_softc *sc;
100*54b96380SRuslan Bukin struct arm_doorbell *db;
101*54b96380SRuslan Bukin uint32_t reg;
102*54b96380SRuslan Bukin int i;
103*54b96380SRuslan Bukin
104*54b96380SRuslan Bukin sc = arg;
105*54b96380SRuslan Bukin
106*54b96380SRuslan Bukin reg = bus_read_4(sc->res[0], MHU_CHAN_RX_HP + MHU_INTR_STAT);
107*54b96380SRuslan Bukin for (i = 0; i < 32; i++) {
108*54b96380SRuslan Bukin if (reg & (1 << i)) {
109*54b96380SRuslan Bukin db = &dbells[i];
110*54b96380SRuslan Bukin bus_write_4(sc->res[0], MHU_CHAN_RX_HP + MHU_INTR_CLEAR,
111*54b96380SRuslan Bukin (1 << i));
112*54b96380SRuslan Bukin if (db->func != NULL)
113*54b96380SRuslan Bukin db->func(db->arg);
114*54b96380SRuslan Bukin }
115*54b96380SRuslan Bukin }
116*54b96380SRuslan Bukin }
117*54b96380SRuslan Bukin
118*54b96380SRuslan Bukin static int
arm_doorbell_probe(device_t dev)119*54b96380SRuslan Bukin arm_doorbell_probe(device_t dev)
120*54b96380SRuslan Bukin {
121*54b96380SRuslan Bukin
122*54b96380SRuslan Bukin if (!ofw_bus_is_compatible(dev, "arm,mhu-doorbell"))
123*54b96380SRuslan Bukin return (ENXIO);
124*54b96380SRuslan Bukin
125*54b96380SRuslan Bukin if (!ofw_bus_status_okay(dev))
126*54b96380SRuslan Bukin return (ENXIO);
127*54b96380SRuslan Bukin
128*54b96380SRuslan Bukin device_set_desc(dev, "ARM MHU Doorbell");
129*54b96380SRuslan Bukin
130*54b96380SRuslan Bukin return (BUS_PROBE_DEFAULT);
131*54b96380SRuslan Bukin }
132*54b96380SRuslan Bukin
133*54b96380SRuslan Bukin static int
arm_doorbell_attach(device_t dev)134*54b96380SRuslan Bukin arm_doorbell_attach(device_t dev)
135*54b96380SRuslan Bukin {
136*54b96380SRuslan Bukin struct arm_doorbell_softc *sc;
137*54b96380SRuslan Bukin phandle_t node;
138*54b96380SRuslan Bukin int error;
139*54b96380SRuslan Bukin
140*54b96380SRuslan Bukin sc = device_get_softc(dev);
141*54b96380SRuslan Bukin sc->dev = dev;
142*54b96380SRuslan Bukin
143*54b96380SRuslan Bukin node = ofw_bus_get_node(dev);
144*54b96380SRuslan Bukin if (node == -1)
145*54b96380SRuslan Bukin return (ENXIO);
146*54b96380SRuslan Bukin
147*54b96380SRuslan Bukin if (bus_alloc_resources(dev, arm_doorbell_spec, sc->res) != 0) {
148*54b96380SRuslan Bukin device_printf(dev, "Can't allocate resources for device.\n");
149*54b96380SRuslan Bukin return (ENXIO);
150*54b96380SRuslan Bukin }
151*54b96380SRuslan Bukin
152*54b96380SRuslan Bukin /* Setup interrupt handlers. */
153*54b96380SRuslan Bukin error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_MISC | INTR_MPSAFE,
154*54b96380SRuslan Bukin NULL, arm_doorbell_lp_intr, sc, &sc->lp_intr_cookie);
155*54b96380SRuslan Bukin if (error != 0) {
156*54b96380SRuslan Bukin device_printf(dev, "Can't setup LP interrupt handler.\n");
157*54b96380SRuslan Bukin bus_release_resources(dev, arm_doorbell_spec, sc->res);
158*54b96380SRuslan Bukin return (ENXIO);
159*54b96380SRuslan Bukin }
160*54b96380SRuslan Bukin
161*54b96380SRuslan Bukin error = bus_setup_intr(dev, sc->res[2], INTR_TYPE_MISC | INTR_MPSAFE,
162*54b96380SRuslan Bukin NULL, arm_doorbell_hp_intr, sc, &sc->hp_intr_cookie);
163*54b96380SRuslan Bukin if (error != 0) {
164*54b96380SRuslan Bukin device_printf(dev, "Can't setup HP interrupt handler.\n");
165*54b96380SRuslan Bukin bus_release_resources(dev, arm_doorbell_spec, sc->res);
166*54b96380SRuslan Bukin return (ENXIO);
167*54b96380SRuslan Bukin }
168*54b96380SRuslan Bukin
169*54b96380SRuslan Bukin OF_device_register_xref(OF_xref_from_node(node), dev);
170*54b96380SRuslan Bukin
171*54b96380SRuslan Bukin return (0);
172*54b96380SRuslan Bukin }
173*54b96380SRuslan Bukin
174*54b96380SRuslan Bukin static int
arm_doorbell_detach(device_t dev)175*54b96380SRuslan Bukin arm_doorbell_detach(device_t dev)
176*54b96380SRuslan Bukin {
177*54b96380SRuslan Bukin
178*54b96380SRuslan Bukin return (EBUSY);
179*54b96380SRuslan Bukin }
180*54b96380SRuslan Bukin
181*54b96380SRuslan Bukin struct arm_doorbell *
arm_doorbell_ofw_get(device_t dev,const char * name)182*54b96380SRuslan Bukin arm_doorbell_ofw_get(device_t dev, const char *name)
183*54b96380SRuslan Bukin {
184*54b96380SRuslan Bukin phandle_t node, parent;
185*54b96380SRuslan Bukin struct arm_doorbell *db;
186*54b96380SRuslan Bukin device_t db_dev;
187*54b96380SRuslan Bukin pcell_t *cells;
188*54b96380SRuslan Bukin int nmboxes;
189*54b96380SRuslan Bukin int ncells;
190*54b96380SRuslan Bukin int idx;
191*54b96380SRuslan Bukin int db_id;
192*54b96380SRuslan Bukin int error;
193*54b96380SRuslan Bukin int chan;
194*54b96380SRuslan Bukin
195*54b96380SRuslan Bukin node = ofw_bus_get_node(dev);
196*54b96380SRuslan Bukin
197*54b96380SRuslan Bukin error = ofw_bus_parse_xref_list_get_length(node, "mboxes",
198*54b96380SRuslan Bukin "#mbox-cells", &nmboxes);
199*54b96380SRuslan Bukin if (error) {
200*54b96380SRuslan Bukin device_printf(dev, "%s can't get mboxes list.\n", __func__);
201*54b96380SRuslan Bukin return (NULL);
202*54b96380SRuslan Bukin }
203*54b96380SRuslan Bukin
204*54b96380SRuslan Bukin if (nmboxes == 0) {
205*54b96380SRuslan Bukin device_printf(dev, "%s mbox list is empty.\n", __func__);
206*54b96380SRuslan Bukin return (NULL);
207*54b96380SRuslan Bukin }
208*54b96380SRuslan Bukin
209*54b96380SRuslan Bukin error = ofw_bus_find_string_index(node, "mbox-names", name, &idx);
210*54b96380SRuslan Bukin if (error != 0) {
211*54b96380SRuslan Bukin device_printf(dev, "%s can't find string index.\n",
212*54b96380SRuslan Bukin __func__);
213*54b96380SRuslan Bukin return (NULL);
214*54b96380SRuslan Bukin }
215*54b96380SRuslan Bukin
216*54b96380SRuslan Bukin error = ofw_bus_parse_xref_list_alloc(node, "mboxes", "#mbox-cells",
217*54b96380SRuslan Bukin idx, &parent, &ncells, &cells);
218*54b96380SRuslan Bukin if (error != 0) {
219*54b96380SRuslan Bukin device_printf(dev, "%s can't get mbox device xref\n",
220*54b96380SRuslan Bukin __func__);
221*54b96380SRuslan Bukin return (NULL);
222*54b96380SRuslan Bukin }
223*54b96380SRuslan Bukin
224*54b96380SRuslan Bukin if (ncells != 2) {
225*54b96380SRuslan Bukin device_printf(dev, "Unexpected data size.\n");
226*54b96380SRuslan Bukin OF_prop_free(cells);
227*54b96380SRuslan Bukin return (NULL);
228*54b96380SRuslan Bukin }
229*54b96380SRuslan Bukin
230*54b96380SRuslan Bukin db_dev = OF_device_from_xref(parent);
231*54b96380SRuslan Bukin if (db_dev == NULL) {
232*54b96380SRuslan Bukin device_printf(dev, "%s: Can't get arm_doorbell device\n",
233*54b96380SRuslan Bukin __func__);
234*54b96380SRuslan Bukin OF_prop_free(cells);
235*54b96380SRuslan Bukin return (NULL);
236*54b96380SRuslan Bukin }
237*54b96380SRuslan Bukin
238*54b96380SRuslan Bukin chan = cells[0];
239*54b96380SRuslan Bukin if (chan >= DOORBELL_N_CHANNELS) {
240*54b96380SRuslan Bukin device_printf(dev, "Unexpected channel number.\n");
241*54b96380SRuslan Bukin OF_prop_free(cells);
242*54b96380SRuslan Bukin return (NULL);
243*54b96380SRuslan Bukin }
244*54b96380SRuslan Bukin
245*54b96380SRuslan Bukin db_id = cells[1];
246*54b96380SRuslan Bukin if (db_id >= 32) {
247*54b96380SRuslan Bukin device_printf(dev, "Unexpected channel bit.\n");
248*54b96380SRuslan Bukin OF_prop_free(cells);
249*54b96380SRuslan Bukin return (NULL);
250*54b96380SRuslan Bukin }
251*54b96380SRuslan Bukin
252*54b96380SRuslan Bukin db = &dbells[chan * db_id];
253*54b96380SRuslan Bukin db->dev = dev;
254*54b96380SRuslan Bukin db->db_dev = db_dev;
255*54b96380SRuslan Bukin db->chan = chan;
256*54b96380SRuslan Bukin db->db = db_id;
257*54b96380SRuslan Bukin
258*54b96380SRuslan Bukin OF_prop_free(cells);
259*54b96380SRuslan Bukin
260*54b96380SRuslan Bukin return (db);
261*54b96380SRuslan Bukin }
262*54b96380SRuslan Bukin
263*54b96380SRuslan Bukin void
arm_doorbell_set(struct arm_doorbell * db)264*54b96380SRuslan Bukin arm_doorbell_set(struct arm_doorbell *db)
265*54b96380SRuslan Bukin {
266*54b96380SRuslan Bukin struct arm_doorbell_softc *sc;
267*54b96380SRuslan Bukin uint32_t offset;
268*54b96380SRuslan Bukin
269*54b96380SRuslan Bukin sc = device_get_softc(db->db_dev);
270*54b96380SRuslan Bukin
271*54b96380SRuslan Bukin switch (db->chan) {
272*54b96380SRuslan Bukin case 0:
273*54b96380SRuslan Bukin offset = MHU_CHAN_RX_LP;
274*54b96380SRuslan Bukin break;
275*54b96380SRuslan Bukin case 1:
276*54b96380SRuslan Bukin offset = MHU_CHAN_RX_HP;
277*54b96380SRuslan Bukin break;
278*54b96380SRuslan Bukin case 2:
279*54b96380SRuslan Bukin offset = MHU_CHAN_RX_SEC;
280*54b96380SRuslan Bukin break;
281*54b96380SRuslan Bukin default:
282*54b96380SRuslan Bukin panic("not reached");
283*54b96380SRuslan Bukin };
284*54b96380SRuslan Bukin
285*54b96380SRuslan Bukin offset |= MHU_TX_REG_OFFSET;
286*54b96380SRuslan Bukin
287*54b96380SRuslan Bukin bus_write_4(sc->res[0], offset + MHU_INTR_SET, (1 << db->db));
288*54b96380SRuslan Bukin }
289*54b96380SRuslan Bukin
290*54b96380SRuslan Bukin int
arm_doorbell_get(struct arm_doorbell * db)291*54b96380SRuslan Bukin arm_doorbell_get(struct arm_doorbell *db)
292*54b96380SRuslan Bukin {
293*54b96380SRuslan Bukin struct arm_doorbell_softc *sc;
294*54b96380SRuslan Bukin uint32_t offset;
295*54b96380SRuslan Bukin uint32_t reg;
296*54b96380SRuslan Bukin
297*54b96380SRuslan Bukin sc = device_get_softc(db->db_dev);
298*54b96380SRuslan Bukin
299*54b96380SRuslan Bukin switch (db->chan) {
300*54b96380SRuslan Bukin case 0:
301*54b96380SRuslan Bukin offset = MHU_CHAN_RX_LP;
302*54b96380SRuslan Bukin break;
303*54b96380SRuslan Bukin case 1:
304*54b96380SRuslan Bukin offset = MHU_CHAN_RX_HP;
305*54b96380SRuslan Bukin break;
306*54b96380SRuslan Bukin case 2:
307*54b96380SRuslan Bukin offset = MHU_CHAN_RX_SEC;
308*54b96380SRuslan Bukin break;
309*54b96380SRuslan Bukin default:
310*54b96380SRuslan Bukin panic("not reached");
311*54b96380SRuslan Bukin };
312*54b96380SRuslan Bukin
313*54b96380SRuslan Bukin reg = bus_read_4(sc->res[0], offset + MHU_INTR_STAT);
314*54b96380SRuslan Bukin if (reg & (1 << db->db)) {
315*54b96380SRuslan Bukin bus_write_4(sc->res[0], offset + MHU_INTR_CLEAR,
316*54b96380SRuslan Bukin (1 << db->db));
317*54b96380SRuslan Bukin return (1);
318*54b96380SRuslan Bukin }
319*54b96380SRuslan Bukin
320*54b96380SRuslan Bukin return (0);
321*54b96380SRuslan Bukin }
322*54b96380SRuslan Bukin
323*54b96380SRuslan Bukin void
arm_doorbell_set_handler(struct arm_doorbell * db,void (* func)(void *),void * arg)324*54b96380SRuslan Bukin arm_doorbell_set_handler(struct arm_doorbell *db, void (*func)(void *),
325*54b96380SRuslan Bukin void *arg)
326*54b96380SRuslan Bukin {
327*54b96380SRuslan Bukin
328*54b96380SRuslan Bukin db->func = func;
329*54b96380SRuslan Bukin db->arg = arg;
330*54b96380SRuslan Bukin }
331*54b96380SRuslan Bukin
332*54b96380SRuslan Bukin static device_method_t arm_doorbell_methods[] = {
333*54b96380SRuslan Bukin DEVMETHOD(device_probe, arm_doorbell_probe),
334*54b96380SRuslan Bukin DEVMETHOD(device_attach, arm_doorbell_attach),
335*54b96380SRuslan Bukin DEVMETHOD(device_detach, arm_doorbell_detach),
336*54b96380SRuslan Bukin DEVMETHOD_END
337*54b96380SRuslan Bukin };
338*54b96380SRuslan Bukin
339*54b96380SRuslan Bukin DEFINE_CLASS_1(arm_doorbell, arm_doorbell_driver, arm_doorbell_methods,
340*54b96380SRuslan Bukin sizeof(struct arm_doorbell_softc), simplebus_driver);
341*54b96380SRuslan Bukin
342*54b96380SRuslan Bukin EARLY_DRIVER_MODULE(arm_doorbell, simplebus, arm_doorbell_driver, 0, 0,
343*54b96380SRuslan Bukin BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
344*54b96380SRuslan Bukin MODULE_VERSION(arm_doorbell, 1);
345