2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021, Adrian Chadd <adrian@FreeBSD.org>
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 unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* Driver for Qualcomm IPQ4018 clock and reset device */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/sglist.h>
38 #include <sys/random.h>
39 #include <sys/stdatomic.h>
40 #include <sys/mutex.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/extres/hwreset/hwreset.h>
52 #include "clkdev_if.h"
53 #include "hwreset_if.h"
55 #include <dt-bindings/clock/qcom,gcc-ipq4019.h>
57 #include "qcom_gcc_ipq4018_var.h"
60 static int qcom_gcc_ipq4018_modevent(module_t, int, void *);
62 static int qcom_gcc_ipq4018_probe(device_t);
63 static int qcom_gcc_ipq4018_attach(device_t);
64 static int qcom_gcc_ipq4018_detach(device_t);
67 qcom_gcc_ipq4018_modevent(module_t mod, int type, void *unused)
87 qcom_gcc_ipq4018_probe(device_t dev)
89 if (! ofw_bus_status_okay(dev))
92 if (ofw_bus_is_compatible(dev, "qcom,gcc-ipq4019") == 0)
99 qcom_gcc_ipq4018_attach(device_t dev)
101 struct qcom_gcc_ipq4018_softc *sc;
103 sc = device_get_softc(dev);
105 /* Found a compatible device! */
109 sc->reg = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY,
110 &sc->reg_rid, 0x60000, RF_ACTIVE);
111 if (sc->reg == NULL) {
112 device_printf(dev, "Couldn't allocate memory resource!\n");
116 device_set_desc(dev, "Qualcomm IPQ4018 Clock/Reset Controller");
118 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
121 * Register as a reset provider.
123 hwreset_register_ofw_provider(dev);
126 * Setup and register as a clock provider.
128 qcom_gcc_ipq4018_clock_setup(sc);
134 qcom_gcc_ipq4018_detach(device_t dev)
136 struct qcom_gcc_ipq4018_softc *sc;
138 sc = device_get_softc(dev);
141 * TBD - deregistering reset/clock resources.
144 if (sc->reg != NULL) {
145 bus_release_resource(sc->dev, SYS_RES_MEMORY,
146 sc->reg_rid, sc->reg);
151 static device_method_t qcom_gcc_ipq4018_methods[] = {
152 /* Device methods. */
153 DEVMETHOD(device_probe, qcom_gcc_ipq4018_probe),
154 DEVMETHOD(device_attach, qcom_gcc_ipq4018_attach),
155 DEVMETHOD(device_detach, qcom_gcc_ipq4018_detach),
157 /* Reset interface */
158 DEVMETHOD(hwreset_assert, qcom_gcc_ipq4018_hwreset_assert),
159 DEVMETHOD(hwreset_is_asserted, qcom_gcc_ipq4018_hwreset_is_asserted),
161 /* Clock interface */
162 DEVMETHOD(clkdev_read_4, qcom_gcc_ipq4018_clock_read),
163 DEVMETHOD(clkdev_write_4, qcom_gcc_ipq4018_clock_write),
164 DEVMETHOD(clkdev_modify_4, qcom_gcc_ipq4018_clock_modify),
165 DEVMETHOD(clkdev_device_lock, qcom_gcc_ipq4018_clock_lock),
166 DEVMETHOD(clkdev_device_unlock, qcom_gcc_ipq4018_clock_unlock),
171 static driver_t qcom_gcc_ipq4018_driver = {
173 qcom_gcc_ipq4018_methods,
174 sizeof(struct qcom_gcc_ipq4018_softc)
176 static devclass_t qcom_gcc_ipq4018_devclass;
178 EARLY_DRIVER_MODULE(qcom_gcc_ipq4018, simplebus, qcom_gcc_ipq4018_driver,
179 qcom_gcc_ipq4018_devclass, qcom_gcc_ipq4018_modevent, 0,
180 BUS_PASS_CPU + BUS_PASS_ORDER_EARLY);
181 EARLY_DRIVER_MODULE(qcom_gcc_ipq4018, ofwbus, qcom_gcc_ipq4018_driver,
182 qcom_gcc_ipq4018_devclass, qcom_gcc_ipq4018_modevent, 0,
183 BUS_PASS_CPU + BUS_PASS_ORDER_EARLY);
184 MODULE_VERSION(qcom_gcc_ipq4018, 1);