2 * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
4 * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 /* Based on sys/arm/ti/ti_sysc.c */
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/resource.h>
38 #include <sys/mutex.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
43 #include <dev/fdt/simplebus.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include "syscon_if.h"
50 #include <dev/extres/syscon/syscon.h>
51 #include "clkdev_if.h"
53 #include <arm/ti/ti_cpuid.h>
56 #define DPRINTF(dev, msg...) device_printf(dev, msg)
58 #define DPRINTF(dev, msg...)
61 MALLOC_DECLARE(M_SYSCON);
63 struct ti_scm_syscon_softc {
64 struct simplebus_softc sc_simplebus;
66 struct syscon * syscon;
67 struct resource * res[1];
69 bus_space_handle_t bsh;
73 static struct resource_spec ti_scm_syscon_res_spec[] = {
74 { SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE },
79 static struct ofw_compat_data compat_data[] = {
84 /* --- dev/extres/syscon syscon_method_t interface --- */
86 ti_scm_syscon_write_4(struct syscon *syscon, bus_size_t offset, uint32_t val)
88 struct ti_scm_syscon_softc *sc;
90 sc = device_get_softc(syscon->pdev);
91 DPRINTF(sc->dev, "offset=%lx write %x\n", offset, val);
93 bus_space_write_4(sc->bst, sc->bsh, offset, val);
99 ti_scm_syscon_read_4(struct syscon *syscon, bus_size_t offset)
101 struct ti_scm_syscon_softc *sc;
104 sc = device_get_softc(syscon->pdev);
107 val = bus_space_read_4(sc->bst, sc->bsh, offset);
108 mtx_unlock(&sc->mtx);
109 DPRINTF(sc->dev, "offset=%lx Read %x\n", offset, val);
113 ti_scm_syscon_modify_4(struct syscon *syscon, bus_size_t offset, uint32_t clr, uint32_t set)
115 struct ti_scm_syscon_softc *sc;
118 sc = device_get_softc(syscon->pdev);
121 reg = bus_space_read_4(sc->bst, sc->bsh, offset);
124 bus_space_write_4(sc->bst, sc->bsh, offset, reg);
125 mtx_unlock(&sc->mtx);
126 DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", offset, reg, clr, set);
131 static syscon_method_t ti_scm_syscon_reg_methods[] = {
132 SYSCONMETHOD(syscon_read_4, ti_scm_syscon_read_4),
133 SYSCONMETHOD(syscon_write_4, ti_scm_syscon_write_4),
134 SYSCONMETHOD(syscon_modify_4, ti_scm_syscon_modify_4),
139 DEFINE_CLASS_1(ti_scm_syscon_reg, ti_scm_syscon_reg_class, ti_scm_syscon_reg_methods,
142 /* device interface */
144 ti_scm_syscon_probe(device_t dev)
146 if (!ofw_bus_status_okay(dev))
149 if (!ti_soc_is_supported())
152 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
155 device_set_desc(dev, "TI OMAP Control Module Syscon");
156 return(BUS_PROBE_DEFAULT);
160 ti_scm_syscon_attach(device_t dev)
162 struct ti_scm_syscon_softc *sc;
163 phandle_t node, child;
165 sc = device_get_softc(dev);
168 if (bus_alloc_resources(dev, ti_scm_syscon_res_spec, sc->res)) {
169 device_printf(sc->dev, "Cant allocate resources\n");
174 sc->bst = rman_get_bustag(sc->res[0]);
175 sc->bsh = rman_get_bushandle(sc->res[0]);
177 mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
178 node = ofw_bus_get_node(sc->dev);
180 /* dev/extres/syscon interface */
181 sc->syscon = syscon_create_ofw_node(dev, &ti_scm_syscon_reg_class, node);
182 if (sc->syscon == NULL) {
183 device_printf(dev, "Failed to create/register syscon\n");
187 simplebus_init(sc->dev, node);
189 bus_generic_probe(sc->dev);
190 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
191 simplebus_add_device(sc->dev, child, 0, NULL, -1, NULL);
194 return (bus_generic_attach(sc->dev));
197 /* syscon interface */
199 ti_scm_syscon_get_handle(device_t dev, struct syscon **syscon)
201 struct ti_scm_syscon_softc *sc;
203 sc = device_get_softc(dev);
204 *syscon = sc->syscon;
210 /* clkdev interface */
212 ti_scm_syscon_clk_write_4(device_t dev, bus_addr_t addr, uint32_t val)
214 struct ti_scm_syscon_softc *sc;
216 sc = device_get_softc(dev);
217 DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
218 bus_space_write_4(sc->bst, sc->bsh, addr, val);
223 ti_scm_syscon_clk_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
225 struct ti_scm_syscon_softc *sc;
227 sc = device_get_softc(dev);
229 *val = bus_space_read_4(sc->bst, sc->bsh, addr);
230 DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, *val);
235 ti_scm_syscon_clk_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
237 struct ti_scm_syscon_softc *sc;
240 sc = device_get_softc(dev);
242 reg = bus_space_read_4(sc->bst, sc->bsh, addr);
245 bus_space_write_4(sc->bst, sc->bsh, addr, reg);
246 DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", addr, reg, clr, set);
252 ti_scm_syscon_clk_device_lock(device_t dev)
254 struct ti_scm_syscon_softc *sc;
256 sc = device_get_softc(dev);
261 ti_scm_syscon_clk_device_unlock(device_t dev)
263 struct ti_scm_syscon_softc *sc;
264 sc = device_get_softc(dev);
265 mtx_unlock(&sc->mtx);
268 static device_method_t ti_scm_syscon_methods[] = {
269 DEVMETHOD(device_probe, ti_scm_syscon_probe),
270 DEVMETHOD(device_attach, ti_scm_syscon_attach),
272 /* syscon interface */
273 DEVMETHOD(syscon_get_handle, ti_scm_syscon_get_handle),
275 /* clkdev interface */
276 DEVMETHOD(clkdev_write_4, ti_scm_syscon_clk_write_4),
277 DEVMETHOD(clkdev_read_4, ti_scm_syscon_clk_read_4),
278 DEVMETHOD(clkdev_modify_4, ti_scm_syscon_clk_modify_4),
279 DEVMETHOD(clkdev_device_lock, ti_scm_syscon_clk_device_lock),
280 DEVMETHOD(clkdev_device_unlock, ti_scm_syscon_clk_device_unlock),
285 DEFINE_CLASS_1(ti_scm_syscon, ti_scm_syscon_driver, ti_scm_syscon_methods,
286 sizeof(struct ti_scm_syscon_softc), simplebus_driver);
288 EARLY_DRIVER_MODULE(ti_scm_syscon, simplebus, ti_scm_syscon_driver, 0, 0,
289 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
290 MODULE_VERSION(ti_scm_syscon, 1);
291 MODULE_DEPEND(ti_scm_syscon, ti_scm, 1, 1, 1);