xref: /freebsd/sys/dev/firmware/arm/scmi_shmem.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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/cdefs.h>
32 __FBSDID("$FreeBSD$");
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 "mmio_sram_if.h"
46 
47 #include "scmi.h"
48 
49 struct shmem_softc {
50 	device_t		dev;
51 	device_t		parent;
52 	int			reg;
53 };
54 
55 static int
56 shmem_probe(device_t dev)
57 {
58 
59 	if (!ofw_bus_is_compatible(dev, "arm,scmi-shmem"))
60 		return (ENXIO);
61 
62 	if (!ofw_bus_status_okay(dev))
63 		return (ENXIO);
64 
65 	device_set_desc(dev, "ARM SCMI Shared Memory driver");
66 
67 	return (BUS_PROBE_DEFAULT);
68 }
69 
70 static int
71 shmem_attach(device_t dev)
72 {
73 	struct shmem_softc *sc;
74 	phandle_t node;
75 	int reg;
76 
77 	sc = device_get_softc(dev);
78 	sc->dev = dev;
79 	sc->parent = device_get_parent(dev);
80 
81 	node = ofw_bus_get_node(dev);
82 	if (node == -1)
83 		return (ENXIO);
84 
85 	OF_getencprop(node, "reg", &reg, sizeof(reg));
86 
87 	dprintf("%s: reg %x\n", __func__, reg);
88 
89 	sc->reg = reg;
90 
91 	OF_device_register_xref(OF_xref_from_node(node), dev);
92 
93 	return (0);
94 }
95 
96 static int
97 shmem_detach(device_t dev)
98 {
99 
100 	return (0);
101 }
102 
103 void
104 scmi_shmem_read(device_t dev, bus_size_t offset, void *buf, bus_size_t len)
105 {
106 	struct shmem_softc *sc;
107 	uint8_t *addr;
108 	int i;
109 
110 	sc = device_get_softc(dev);
111 
112 	addr = (uint8_t *)buf;
113 
114 	for (i = 0; i < len; i++)
115 		addr[i] = MMIO_SRAM_READ_1(sc->parent, sc->reg + offset + i);
116 }
117 
118 void
119 scmi_shmem_write(device_t dev, bus_size_t offset, const void *buf,
120     bus_size_t len)
121 {
122 	struct shmem_softc *sc;
123 	const uint8_t *addr;
124 	int i;
125 
126 	sc = device_get_softc(dev);
127 
128 	addr = (const uint8_t *)buf;
129 
130 	for (i = 0; i < len; i++)
131 		MMIO_SRAM_WRITE_1(sc->parent, sc->reg + offset + i, addr[i]);
132 }
133 
134 static device_method_t shmem_methods[] = {
135 	DEVMETHOD(device_probe,		shmem_probe),
136 	DEVMETHOD(device_attach,	shmem_attach),
137 	DEVMETHOD(device_detach,	shmem_detach),
138 	DEVMETHOD_END
139 };
140 
141 DEFINE_CLASS_1(shmem, shmem_driver, shmem_methods, sizeof(struct shmem_softc),
142     simplebus_driver);
143 
144 EARLY_DRIVER_MODULE(shmem, mmio_sram, shmem_driver, 0, 0,
145     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
146 MODULE_VERSION(scmi, 1);
147