2 * Copyright (c) 2017 Kyle Evans <kevans@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This is a generic syscon driver, whose purpose is to provide access to
29 * various unrelated bits packed in a single register space. It is usually used
30 * as a fallback to more specific driver, but works well enough for simple
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
43 #include <machine/bus.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
48 #include "syscon_if.h"
50 #define SYSCON_LOCK(_sc) mtx_lock(&(_sc)->mtx)
51 #define SYSCON_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
52 #define SYSCON_LOCK_INIT(_sc) mtx_init(&(_sc)->mtx, \
53 device_get_nameunit((_sc)->dev), "syscon", MTX_DEF)
54 #define SYSCON_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx);
55 #define SYSCON_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED);
56 #define SYSCON_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_NOTOWNED);
60 struct resource *mem_res;
64 static struct ofw_compat_data compat_data[] = {
70 syscon_read_4(device_t dev, device_t consumer, bus_size_t offset)
72 struct syscon_softc *sc;
75 sc = device_get_softc(dev);
78 val = bus_read_4(sc->mem_res, offset);
84 syscon_write_4(device_t dev, device_t consumer, bus_size_t offset, uint32_t val)
86 struct syscon_softc *sc;
88 sc = device_get_softc(dev);
91 bus_write_4(sc->mem_res, offset, val);
96 syscon_modify_4(device_t dev, device_t consumer, bus_size_t offset,
97 uint32_t clear_bits, uint32_t set_bits)
99 struct syscon_softc *sc;
102 sc = device_get_softc(dev);
105 val = bus_read_4(sc->mem_res, offset);
108 bus_write_4(sc->mem_res, offset, val);
113 syscon_probe(device_t dev)
116 if (!ofw_bus_status_okay(dev))
118 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
121 device_set_desc(dev, "syscon");
122 return (BUS_PROBE_GENERIC);
126 syscon_attach(device_t dev)
128 struct syscon_softc *sc;
132 sc = device_get_softc(dev);
134 node = ofw_bus_get_node(sc->dev);
136 SYSCON_LOCK_INIT(sc);
139 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
141 if (sc->mem_res == NULL) {
142 device_printf(dev, "Cannot allocate memory resource\n");
146 OF_device_register_xref(OF_xref_from_node(node), dev);
152 syscon_detach(device_t dev)
154 struct syscon_softc *sc;
156 sc = device_get_softc(dev);
158 OF_device_register_xref(OF_xref_from_device(dev), NULL);
160 SYSCON_LOCK_DESTROY(sc);
161 if (sc->mem_res != NULL)
162 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
166 static device_method_t syscon_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_probe, syscon_probe),
169 DEVMETHOD(device_attach, syscon_attach),
170 DEVMETHOD(device_detach, syscon_detach),
172 /* Syscon interface */
173 DEVMETHOD(syscon_read_4, syscon_read_4),
174 DEVMETHOD(syscon_write_4, syscon_write_4),
175 DEVMETHOD(syscon_modify_4, syscon_modify_4),
180 DEFINE_CLASS_0(syscon, syscon_driver, syscon_methods,
181 sizeof(struct syscon_softc));
182 static devclass_t syscon_devclass;
183 EARLY_DRIVER_MODULE(syscon, simplebus, syscon_driver, syscon_devclass, 0, 0,
184 BUS_PASS_BUS + BUS_PASS_ORDER_LATE);
185 MODULE_VERSION(syscon, 1);