xref: /freebsd/sys/dev/qcom_gcc/qcom_gcc_ipq4018.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021, Adrian Chadd <adrian@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
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.
15  *
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.
26  */
27 
28 /* Driver for Qualcomm IPQ4018 clock and reset device */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/sglist.h>
36 #include <sys/random.h>
37 #include <sys/stdatomic.h>
38 #include <sys/mutex.h>
39 
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/bus.h>
43 
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include <dev/extres/hwreset/hwreset.h>
49 
50 #include "clkdev_if.h"
51 #include "hwreset_if.h"
52 
53 #include <dt-bindings/clock/qcom,gcc-ipq4019.h>
54 
55 #include "qcom_gcc_ipq4018_var.h"
56 
57 
58 static int	qcom_gcc_ipq4018_modevent(module_t, int, void *);
59 
60 static int	qcom_gcc_ipq4018_probe(device_t);
61 static int	qcom_gcc_ipq4018_attach(device_t);
62 static int	qcom_gcc_ipq4018_detach(device_t);
63 
64 static int
65 qcom_gcc_ipq4018_modevent(module_t mod, int type, void *unused)
66 {
67 	int error;
68 
69 	switch (type) {
70 	case MOD_LOAD:
71 	case MOD_QUIESCE:
72 	case MOD_UNLOAD:
73 	case MOD_SHUTDOWN:
74 		error = 0;
75 		break;
76 	default:
77 		error = EOPNOTSUPP;
78 		break;
79 	}
80 
81 	return (error);
82 }
83 
84 static int
85 qcom_gcc_ipq4018_probe(device_t dev)
86 {
87 	if (! ofw_bus_status_okay(dev))
88 		return (ENXIO);
89 
90 	if (ofw_bus_is_compatible(dev, "qcom,gcc-ipq4019") == 0)
91 		return (ENXIO);
92 
93 	return (0);
94 }
95 
96 static int
97 qcom_gcc_ipq4018_attach(device_t dev)
98 {
99 	struct qcom_gcc_ipq4018_softc *sc;
100 
101 	sc = device_get_softc(dev);
102 
103 	/* Found a compatible device! */
104 	sc->dev = dev;
105 
106 	sc->reg_rid = 0;
107 	sc->reg = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY,
108 	    &sc->reg_rid, 0x60000, RF_ACTIVE);
109 	if (sc->reg == NULL) {
110 		device_printf(dev, "Couldn't allocate memory resource!\n");
111 		return (ENXIO);
112 	}
113 
114 	device_set_desc(dev, "Qualcomm IPQ4018 Clock/Reset Controller");
115 
116 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
117 
118 	/*
119 	 * Register as a reset provider.
120 	 */
121 	hwreset_register_ofw_provider(dev);
122 
123 	/*
124 	 * Setup and register as a clock provider.
125 	 */
126 	qcom_gcc_ipq4018_clock_setup(sc);
127 
128 	return (0);
129 }
130 
131 static int
132 qcom_gcc_ipq4018_detach(device_t dev)
133 {
134 	struct qcom_gcc_ipq4018_softc *sc;
135 
136 	sc = device_get_softc(dev);
137 
138 	/*
139 	 * TBD - deregistering reset/clock resources.
140 	 */
141 
142 	if (sc->reg != NULL) {
143 		bus_release_resource(sc->dev, SYS_RES_MEMORY,
144 		    sc->reg_rid, sc->reg);
145 	}
146 	return (0);
147 }
148 
149 static device_method_t qcom_gcc_ipq4018_methods[] = {
150 	/* Device methods. */
151 	DEVMETHOD(device_probe,		qcom_gcc_ipq4018_probe),
152 	DEVMETHOD(device_attach,	qcom_gcc_ipq4018_attach),
153 	DEVMETHOD(device_detach,	qcom_gcc_ipq4018_detach),
154 
155 	/* Reset interface */
156 	DEVMETHOD(hwreset_assert,	qcom_gcc_ipq4018_hwreset_assert),
157 	DEVMETHOD(hwreset_is_asserted,	qcom_gcc_ipq4018_hwreset_is_asserted),
158 
159 	/* Clock interface */
160 	DEVMETHOD(clkdev_read_4,	qcom_gcc_ipq4018_clock_read),
161 	DEVMETHOD(clkdev_write_4,	qcom_gcc_ipq4018_clock_write),
162 	DEVMETHOD(clkdev_modify_4,	qcom_gcc_ipq4018_clock_modify),
163 	DEVMETHOD(clkdev_device_lock,	qcom_gcc_ipq4018_clock_lock),
164 	DEVMETHOD(clkdev_device_unlock,	qcom_gcc_ipq4018_clock_unlock),
165 
166 	DEVMETHOD_END
167 };
168 
169 static driver_t qcom_gcc_ipq4018_driver = {
170 	"qcom_gcc",
171 	qcom_gcc_ipq4018_methods,
172 	sizeof(struct qcom_gcc_ipq4018_softc)
173 };
174 
175 EARLY_DRIVER_MODULE(qcom_gcc_ipq4018, simplebus, qcom_gcc_ipq4018_driver,
176     qcom_gcc_ipq4018_modevent, NULL, BUS_PASS_CPU + BUS_PASS_ORDER_EARLY);
177 EARLY_DRIVER_MODULE(qcom_gcc_ipq4018, ofwbus, qcom_gcc_ipq4018_driver,
178     qcom_gcc_ipq4018_modevent, NULL, BUS_PASS_CPU + BUS_PASS_ORDER_EARLY);
179 MODULE_VERSION(qcom_gcc_ipq4018, 1);
180