2 * Copyright 2015 John Wehle <john@feith.com>
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
28 * Amlogic aml8726 clock control module driver.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/resource.h>
46 #include <machine/bus.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
51 #include <arm/amlogic/aml8726/aml8726_soc.h>
52 #include <arm/amlogic/aml8726/aml8726_ccm.h>
55 struct aml8726_ccm_softc {
57 struct aml8726_ccm_function *soc;
58 struct resource *res[1];
62 static struct resource_spec aml8726_ccm_spec[] = {
63 { SYS_RES_MEMORY, 0, RF_ACTIVE },
67 #define AML_CCM_LOCK(sc) mtx_lock(&(sc)->mtx)
68 #define AML_CCM_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
69 #define AML_CCM_LOCK_INIT(sc) \
70 mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \
72 #define AML_CCM_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx);
74 #define CSR_WRITE_4(sc, reg, val) bus_write_4((sc)->res[0], reg, (val))
75 #define CSR_READ_4(sc, reg) bus_read_4((sc)->res[0], reg)
78 aml8726_ccm_configure_gates(struct aml8726_ccm_softc *sc)
80 struct aml8726_ccm_function *f;
81 struct aml8726_ccm_gate *g;
88 node = ofw_bus_get_node(sc->dev);
90 len = OF_getprop_alloc(node, "functions",
94 device_printf(sc->dev, "missing functions attribute in FDT\n");
98 function_name = functions;
101 for (f = sc->soc; f->name != NULL; f++)
102 if (strncmp(f->name, function_name, len) == 0)
105 if (f->name == NULL) {
106 /* display message prior to queuing up next string */
107 device_printf(sc->dev,
108 "unknown function attribute %.*s in FDT\n",
112 /* queue up next string */
113 while (*function_name && len) {
128 * Enable the clock gates necessary for the function.
130 * In some cases a clock may be shared across functions
131 * (meaning don't disable a clock without ensuring that
132 * it's not required by someone else).
134 for (g = f->gates; g->bits != 0x00000000; g++) {
135 value = CSR_READ_4(sc, g->addr);
137 CSR_WRITE_4(sc, g->addr, value);
143 OF_prop_free(functions);
149 aml8726_ccm_probe(device_t dev)
152 if (!ofw_bus_status_okay(dev))
155 if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-ccm"))
158 device_set_desc(dev, "Amlogic aml8726 ccm");
160 return (BUS_PROBE_DEFAULT);
164 aml8726_ccm_attach(device_t dev)
166 struct aml8726_ccm_softc *sc = device_get_softc(dev);
170 switch (aml8726_soc_hw_rev) {
171 case AML_SOC_HW_REV_M3:
172 sc->soc = aml8726_m3_ccm;
174 case AML_SOC_HW_REV_M6:
175 sc->soc = aml8726_m6_ccm;
177 case AML_SOC_HW_REV_M8:
178 sc->soc = aml8726_m8_ccm;
180 case AML_SOC_HW_REV_M8B:
181 sc->soc = aml8726_m8b_ccm;
184 device_printf(dev, "unsupported SoC\n");
189 if (bus_alloc_resources(dev, aml8726_ccm_spec, sc->res)) {
190 device_printf(dev, "can not allocate resources for device\n");
194 AML_CCM_LOCK_INIT(sc);
196 return (aml8726_ccm_configure_gates(sc));
200 aml8726_ccm_detach(device_t dev)
202 struct aml8726_ccm_softc *sc = device_get_softc(dev);
204 AML_CCM_LOCK_DESTROY(sc);
206 bus_release_resources(dev, aml8726_ccm_spec, sc->res);
211 static device_method_t aml8726_ccm_methods[] = {
212 /* Device interface */
213 DEVMETHOD(device_probe, aml8726_ccm_probe),
214 DEVMETHOD(device_attach, aml8726_ccm_attach),
215 DEVMETHOD(device_detach, aml8726_ccm_detach),
220 static driver_t aml8726_ccm_driver = {
223 sizeof(struct aml8726_ccm_softc),
226 static devclass_t aml8726_ccm_devclass;
228 EARLY_DRIVER_MODULE(ccm, simplebus, aml8726_ccm_driver,
229 aml8726_ccm_devclass, 0, 0, BUS_PASS_CPU + BUS_PASS_ORDER_LATE);