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