xref: /freebsd/sys/dev/sram/mmio_sram.c (revision 18250ec6c089c0c50cbd9fd87d78e03ff89916df)
154b96380SRuslan Bukin /*-
254b96380SRuslan Bukin  * SPDX-License-Identifier: BSD-2-Clause
354b96380SRuslan Bukin  *
454b96380SRuslan Bukin  * Copyright (c) 2022 Ruslan Bukin <br@bsdpad.com>
554b96380SRuslan Bukin  *
654b96380SRuslan Bukin  * This work was supported by Innovate UK project 105694, "Digital Security
754b96380SRuslan Bukin  * by Design (DSbD) Technology Platform Prototype".
854b96380SRuslan Bukin  *
954b96380SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
1054b96380SRuslan Bukin  * modification, are permitted provided that the following conditions
1154b96380SRuslan Bukin  * are met:
1254b96380SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
1354b96380SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
1454b96380SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
1554b96380SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
1654b96380SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
1754b96380SRuslan Bukin  *
1854b96380SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1954b96380SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2054b96380SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2154b96380SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2254b96380SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2354b96380SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2454b96380SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2554b96380SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2654b96380SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2754b96380SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2854b96380SRuslan Bukin  * SUCH DAMAGE.
2954b96380SRuslan Bukin  */
3054b96380SRuslan Bukin 
3154b96380SRuslan Bukin #include <sys/param.h>
3254b96380SRuslan Bukin #include <sys/systm.h>
3354b96380SRuslan Bukin #include <sys/bus.h>
3454b96380SRuslan Bukin #include <sys/rman.h>
3554b96380SRuslan Bukin #include <sys/kernel.h>
3654b96380SRuslan Bukin #include <sys/module.h>
3754b96380SRuslan Bukin 
3854b96380SRuslan Bukin #include <machine/bus.h>
3954b96380SRuslan Bukin 
4054b96380SRuslan Bukin #include <dev/fdt/simplebus.h>
4154b96380SRuslan Bukin #include <dev/fdt/fdt_common.h>
4254b96380SRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
4354b96380SRuslan Bukin 
4454b96380SRuslan Bukin #include "mmio_sram_if.h"
4554b96380SRuslan Bukin 
4654b96380SRuslan Bukin #define	dprintf(fmt, ...)
4754b96380SRuslan Bukin 
4854b96380SRuslan Bukin static struct resource_spec mmio_sram_spec[] = {
4954b96380SRuslan Bukin 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
5054b96380SRuslan Bukin 	{ -1, 0 }
5154b96380SRuslan Bukin };
5254b96380SRuslan Bukin 
5354b96380SRuslan Bukin struct mmio_sram_softc {
5454b96380SRuslan Bukin 	struct simplebus_softc	simplebus_sc;
5554b96380SRuslan Bukin 	struct resource		*res[1];
5654b96380SRuslan Bukin 	device_t		dev;
5754b96380SRuslan Bukin };
5854b96380SRuslan Bukin 
5954b96380SRuslan Bukin static int
mmio_sram_probe(device_t dev)6054b96380SRuslan Bukin mmio_sram_probe(device_t dev)
6154b96380SRuslan Bukin {
6254b96380SRuslan Bukin 
6354b96380SRuslan Bukin 	if (!ofw_bus_is_compatible(dev, "mmio-sram"))
6454b96380SRuslan Bukin 		return (ENXIO);
6554b96380SRuslan Bukin 
6654b96380SRuslan Bukin 	if (!ofw_bus_status_okay(dev))
6754b96380SRuslan Bukin 		return (ENXIO);
6854b96380SRuslan Bukin 
6954b96380SRuslan Bukin 	device_set_desc(dev, "MMIO SRAM");
7054b96380SRuslan Bukin 
7154b96380SRuslan Bukin 	return (BUS_PROBE_DEFAULT);
7254b96380SRuslan Bukin }
7354b96380SRuslan Bukin 
7454b96380SRuslan Bukin static int
mmio_sram_attach(device_t dev)7554b96380SRuslan Bukin mmio_sram_attach(device_t dev)
7654b96380SRuslan Bukin {
7754b96380SRuslan Bukin 	struct mmio_sram_softc *sc;
7854b96380SRuslan Bukin 	phandle_t node;
7954b96380SRuslan Bukin 
8054b96380SRuslan Bukin 	sc = device_get_softc(dev);
8154b96380SRuslan Bukin 	sc->dev = dev;
8254b96380SRuslan Bukin 
8354b96380SRuslan Bukin 	if (bus_alloc_resources(dev, mmio_sram_spec, sc->res) != 0) {
8454b96380SRuslan Bukin 		device_printf(dev, "Can't allocate resources for device.\n");
8554b96380SRuslan Bukin 		return (ENXIO);
8654b96380SRuslan Bukin 	}
8754b96380SRuslan Bukin 
8854b96380SRuslan Bukin 	node = ofw_bus_get_node(dev);
8954b96380SRuslan Bukin 	if (node == -1)
9054b96380SRuslan Bukin 		return (ENXIO);
9154b96380SRuslan Bukin 
9254b96380SRuslan Bukin 	simplebus_init(dev, node);
9354b96380SRuslan Bukin 
9454b96380SRuslan Bukin 	/*
9554b96380SRuslan Bukin 	 * Allow devices to identify.
9654b96380SRuslan Bukin 	 */
97723da5d9SJohn Baldwin 	bus_identify_children(dev);
9854b96380SRuslan Bukin 
9954b96380SRuslan Bukin 	/*
10054b96380SRuslan Bukin 	 * Now walk the OFW tree and attach top-level devices.
10154b96380SRuslan Bukin 	 */
10254b96380SRuslan Bukin 	for (node = OF_child(node); node > 0; node = OF_peer(node))
10354b96380SRuslan Bukin 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
10454b96380SRuslan Bukin 
105*18250ec6SJohn Baldwin 	bus_attach_children(dev);
106*18250ec6SJohn Baldwin 	return (0);
10754b96380SRuslan Bukin }
10854b96380SRuslan Bukin 
10954b96380SRuslan Bukin static int
mmio_sram_detach(device_t dev)11054b96380SRuslan Bukin mmio_sram_detach(device_t dev)
11154b96380SRuslan Bukin {
11254b96380SRuslan Bukin 	struct mmio_sram_softc *sc;
11354b96380SRuslan Bukin 
11454b96380SRuslan Bukin 	sc = device_get_softc(dev);
11554b96380SRuslan Bukin 
11654b96380SRuslan Bukin 	bus_release_resources(dev, mmio_sram_spec, sc->res);
11754b96380SRuslan Bukin 
11854b96380SRuslan Bukin 	return (0);
11954b96380SRuslan Bukin }
12054b96380SRuslan Bukin 
12154b96380SRuslan Bukin static uint8_t
mmio_sram_read_1(device_t dev,bus_size_t offset)12254b96380SRuslan Bukin mmio_sram_read_1(device_t dev, bus_size_t offset)
12354b96380SRuslan Bukin {
12454b96380SRuslan Bukin 	struct mmio_sram_softc *sc;
12554b96380SRuslan Bukin 
12654b96380SRuslan Bukin 	sc = device_get_softc(dev);
12754b96380SRuslan Bukin 
12854b96380SRuslan Bukin 	dprintf("%s: reading from %lx\n", __func__, offset);
12954b96380SRuslan Bukin 
13054b96380SRuslan Bukin 	return (bus_read_1(sc->res[0], offset));
13154b96380SRuslan Bukin }
13254b96380SRuslan Bukin 
13354b96380SRuslan Bukin static void
mmio_sram_write_1(device_t dev,bus_size_t offset,uint8_t val)13454b96380SRuslan Bukin mmio_sram_write_1(device_t dev, bus_size_t offset, uint8_t val)
13554b96380SRuslan Bukin {
13654b96380SRuslan Bukin 	struct mmio_sram_softc *sc;
13754b96380SRuslan Bukin 
13854b96380SRuslan Bukin 	sc = device_get_softc(dev);
13954b96380SRuslan Bukin 
14054b96380SRuslan Bukin 	dprintf("%s: writing to %lx val %x\n", __func__, offset, val);
14154b96380SRuslan Bukin 
14254b96380SRuslan Bukin 	bus_write_1(sc->res[0], offset, val);
14354b96380SRuslan Bukin }
14454b96380SRuslan Bukin 
14554b96380SRuslan Bukin static device_method_t mmio_sram_methods[] = {
14654b96380SRuslan Bukin 	/* Device Interface */
14754b96380SRuslan Bukin 	DEVMETHOD(device_probe,		mmio_sram_probe),
14854b96380SRuslan Bukin 	DEVMETHOD(device_attach,	mmio_sram_attach),
14954b96380SRuslan Bukin 	DEVMETHOD(device_detach,	mmio_sram_detach),
15054b96380SRuslan Bukin 
15154b96380SRuslan Bukin 	/* MMIO interface */
15254b96380SRuslan Bukin 	DEVMETHOD(mmio_sram_read_1,	mmio_sram_read_1),
15354b96380SRuslan Bukin 	DEVMETHOD(mmio_sram_write_1,	mmio_sram_write_1),
15454b96380SRuslan Bukin 	DEVMETHOD_END
15554b96380SRuslan Bukin };
15654b96380SRuslan Bukin 
15754b96380SRuslan Bukin DEFINE_CLASS_1(mmio_sram, mmio_sram_driver, mmio_sram_methods,
15854b96380SRuslan Bukin     sizeof(struct mmio_sram_softc), simplebus_driver);
15954b96380SRuslan Bukin 
16054b96380SRuslan Bukin EARLY_DRIVER_MODULE(mmio_sram, simplebus, mmio_sram_driver, 0, 0,
16154b96380SRuslan Bukin     BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
16254b96380SRuslan Bukin MODULE_VERSION(mmio_sram, 1);
163