xref: /freebsd/sys/dev/firmware/arm/scmi_shmem.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Ruslan Bukin <br@bsdpad.com>
5  *
6  * This work was supported by Innovate UK project 105694, "Digital Security
7  * by Design (DSbD) Technology Platform Prototype".
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 
38 #include <dev/fdt/simplebus.h>
39 #include <dev/fdt/fdt_common.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 
42 #include "mmio_sram_if.h"
43 
44 #include "scmi.h"
45 
46 struct shmem_softc {
47 	device_t		dev;
48 	device_t		parent;
49 	int			reg;
50 };
51 
52 static int
53 shmem_probe(device_t dev)
54 {
55 
56 	if (!ofw_bus_is_compatible(dev, "arm,scmi-shmem"))
57 		return (ENXIO);
58 
59 	if (!ofw_bus_status_okay(dev))
60 		return (ENXIO);
61 
62 	device_set_desc(dev, "ARM SCMI Shared Memory driver");
63 
64 	return (BUS_PROBE_DEFAULT);
65 }
66 
67 static int
68 shmem_attach(device_t dev)
69 {
70 	struct shmem_softc *sc;
71 	phandle_t node;
72 	int reg;
73 
74 	sc = device_get_softc(dev);
75 	sc->dev = dev;
76 	sc->parent = device_get_parent(dev);
77 
78 	node = ofw_bus_get_node(dev);
79 	if (node == -1)
80 		return (ENXIO);
81 
82 	OF_getencprop(node, "reg", &reg, sizeof(reg));
83 
84 	dprintf("%s: reg %x\n", __func__, reg);
85 
86 	sc->reg = reg;
87 
88 	OF_device_register_xref(OF_xref_from_node(node), dev);
89 
90 	return (0);
91 }
92 
93 static int
94 shmem_detach(device_t dev)
95 {
96 
97 	return (0);
98 }
99 
100 void
101 scmi_shmem_read(device_t dev, bus_size_t offset, void *buf, bus_size_t len)
102 {
103 	struct shmem_softc *sc;
104 	uint8_t *addr;
105 	int i;
106 
107 	sc = device_get_softc(dev);
108 
109 	addr = (uint8_t *)buf;
110 
111 	for (i = 0; i < len; i++)
112 		addr[i] = MMIO_SRAM_READ_1(sc->parent, sc->reg + offset + i);
113 }
114 
115 void
116 scmi_shmem_write(device_t dev, bus_size_t offset, const void *buf,
117     bus_size_t len)
118 {
119 	struct shmem_softc *sc;
120 	const uint8_t *addr;
121 	int i;
122 
123 	sc = device_get_softc(dev);
124 
125 	addr = (const uint8_t *)buf;
126 
127 	for (i = 0; i < len; i++)
128 		MMIO_SRAM_WRITE_1(sc->parent, sc->reg + offset + i, addr[i]);
129 }
130 
131 static device_method_t shmem_methods[] = {
132 	DEVMETHOD(device_probe,		shmem_probe),
133 	DEVMETHOD(device_attach,	shmem_attach),
134 	DEVMETHOD(device_detach,	shmem_detach),
135 	DEVMETHOD_END
136 };
137 
138 DEFINE_CLASS_1(shmem, shmem_driver, shmem_methods, sizeof(struct shmem_softc),
139     simplebus_driver);
140 
141 EARLY_DRIVER_MODULE(shmem, mmio_sram, shmem_driver, 0, 0,
142     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
143 MODULE_VERSION(scmi, 1);
144