2 * Copyright (c) 2017 Oleksandr Tymoshenko <gonzo@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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Ben Gray.
16 * 4. The name of the company nor the name of the author may be used to
17 * endorse or promote products derived from this software without specific
18 * prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY BEN GRAY ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL BEN GRAY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * SCM - System Control Module
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/resource.h>
46 #include <sys/mutex.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include "versatile_scm.h"
57 struct versatile_scm_softc {
59 struct resource * sc_mem_res;
62 static struct versatile_scm_softc *versatile_scm_sc;
64 #define versatile_scm_read_4(sc, reg) \
65 bus_read_4((sc)->sc_mem_res, (reg))
66 #define versatile_scm_write_4(sc, reg, val) \
67 bus_write_4((sc)->sc_mem_res, (reg), (val))
70 versatile_scm_probe(device_t dev)
72 if (!ofw_bus_status_okay(dev))
75 if (!ofw_bus_is_compatible(dev, "syscon"))
78 if (versatile_scm_sc) {
82 device_set_desc(dev, "Versatile Control Module");
83 return (BUS_PROBE_DEFAULT);
87 versatile_scm_attach(device_t dev)
89 struct versatile_scm_softc *sc;
92 sc = device_get_softc(dev);
96 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
98 if (sc->sc_mem_res == NULL) {
99 device_printf(dev, "could not allocate memory resources\n");
103 versatile_scm_sc = sc;
109 versatile_scm_reg_read_4(uint32_t reg, uint32_t *val)
111 if (!versatile_scm_sc)
114 *val = versatile_scm_read_4(versatile_scm_sc, reg);
119 versatile_scm_reg_write_4(uint32_t reg, uint32_t val)
121 if (!versatile_scm_sc)
124 versatile_scm_write_4(versatile_scm_sc, reg, val);
128 static device_method_t versatile_scm_methods[] = {
129 DEVMETHOD(device_probe, versatile_scm_probe),
130 DEVMETHOD(device_attach, versatile_scm_attach),
135 static driver_t versatile_scm_driver = {
137 versatile_scm_methods,
138 sizeof(struct versatile_scm_softc),
141 static devclass_t versatile_scm_devclass;
143 EARLY_DRIVER_MODULE(versatile_scm, simplebus, versatile_scm_driver, versatile_scm_devclass, 0, 0,
144 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);