2 * Copyright (c) 2016 Stanislav Galabov.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
39 #include <sys/malloc.h>
41 #include <machine/fdt.h>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
47 #include <mips/mediatek/mtk_sysctl.h>
49 #include <dev/fdt/fdt_common.h>
51 struct mtk_sysctl_softc {
53 struct resource *mem_res;
58 static struct mtk_sysctl_softc *mtk_sysctl_sc = NULL;
60 static struct ofw_compat_data compat_data[] = {
61 { "ralink,rt2880-sysc", 1 },
62 { "ralink,rt3050-sysc", 1 },
63 { "ralink,rt3352-sysc", 1 },
64 { "ralink,rt3883-sysc", 1 },
65 { "ralink,rt5350-sysc", 1 },
66 { "ralink,mt7620a-sysc", 1 },
67 { "mtk,mt7621-sysc", 1 },
73 #define MTK_SYSCTL_LOCK(sc) mtx_lock_spin(&(sc)->mtx)
74 #define MTK_SYSCTL_UNLOCK(sc) mtx_unlock_spin(&(sc)->mtx)
75 #define MTK_SYSCTL_LOCK_INIT(sc) \
76 mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \
77 "mtk_sysctl", MTX_SPIN)
78 #define MTK_SYSCTL_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx)
81 mtk_sysctl_probe(device_t dev)
84 if (!ofw_bus_status_okay(dev))
87 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
90 device_set_desc(dev, "MTK System Controller");
92 return (BUS_PROBE_DEFAULT);
95 static int mtk_sysctl_detach(device_t);
98 mtk_sysctl_attach(device_t dev)
100 struct mtk_sysctl_softc *sc = device_get_softc(dev);
102 if (device_get_unit(dev) != 0 || mtk_sysctl_sc != NULL) {
103 device_printf(dev, "Only one sysctl module supported\n");
109 /* Map control/status registers. */
111 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
112 &sc->mem_rid, RF_ACTIVE);
114 if (sc->mem_res == NULL) {
115 device_printf(dev, "couldn't map memory\n");
116 mtk_sysctl_detach(dev);
122 MTK_SYSCTL_LOCK_INIT(sc);
128 mtk_sysctl_detach(device_t dev)
130 struct mtk_sysctl_softc *sc = device_get_softc(dev);
133 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
136 MTK_SYSCTL_LOCK_DESTROY(sc);
142 mtk_sysctl_get(uint32_t reg)
146 MTK_SYSCTL_LOCK(mtk_sysctl_sc);
147 val = bus_read_4(mtk_sysctl_sc->mem_res, reg);
148 MTK_SYSCTL_UNLOCK(mtk_sysctl_sc);
154 mtk_sysctl_set(uint32_t reg, uint32_t val)
157 MTK_SYSCTL_LOCK(mtk_sysctl_sc);
158 bus_write_4(mtk_sysctl_sc->mem_res, reg, val);
159 MTK_SYSCTL_UNLOCK(mtk_sysctl_sc);
163 mtk_sysctl_clr_set(uint32_t reg, uint32_t clr, uint32_t set)
167 MTK_SYSCTL_LOCK(mtk_sysctl_sc);
168 val = bus_read_4(mtk_sysctl_sc->mem_res, reg);
171 bus_write_4(mtk_sysctl_sc->mem_res, reg, val);
172 MTK_SYSCTL_UNLOCK(mtk_sysctl_sc);
175 static device_method_t mtk_sysctl_methods[] = {
176 DEVMETHOD(device_probe, mtk_sysctl_probe),
177 DEVMETHOD(device_attach, mtk_sysctl_attach),
178 DEVMETHOD(device_detach, mtk_sysctl_detach),
183 static driver_t mtk_sysctl_driver = {
186 sizeof(struct mtk_sysctl_softc),
188 static devclass_t mtk_sysctl_devclass;
190 EARLY_DRIVER_MODULE(mtk_sysctl, simplebus, mtk_sysctl_driver,
191 mtk_sysctl_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_EARLY);