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/fdt/fdt_common.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <arm/amlogic/aml8726/aml8726_soc.h>
53 #include <arm/amlogic/aml8726/aml8726_ccm.h>
56 struct aml8726_ccm_softc {
58 struct aml8726_ccm_function *soc;
59 struct resource *res[1];
63 static struct resource_spec aml8726_ccm_spec[] = {
64 { SYS_RES_MEMORY, 0, RF_ACTIVE },
68 #define AML_CCM_LOCK(sc) mtx_lock(&(sc)->mtx)
69 #define AML_CCM_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
70 #define AML_CCM_LOCK_INIT(sc) \
71 mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \
73 #define AML_CCM_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx);
75 #define CSR_WRITE_4(sc, reg, val) bus_write_4((sc)->res[0], reg, (val))
76 #define CSR_READ_4(sc, reg) bus_read_4((sc)->res[0], reg)
79 aml8726_ccm_configure_gates(struct aml8726_ccm_softc *sc)
81 struct aml8726_ccm_function *f;
82 struct aml8726_ccm_gate *g;
89 node = ofw_bus_get_node(sc->dev);
91 len = OF_getprop_alloc(node, "functions", sizeof(char),
95 device_printf(sc->dev, "missing functions attribute in FDT\n");
99 function_name = functions;
102 for (f = sc->soc; f->name != NULL; f++)
103 if (strncmp(f->name, function_name, len) == 0)
106 if (f->name == NULL) {
107 /* display message prior to queuing up next string */
108 device_printf(sc->dev,
109 "unknown function attribute %.*s in FDT\n",
113 /* queue up next string */
114 while (*function_name && len) {
129 * Enable the clock gates necessary for the function.
131 * In some cases a clock may be shared across functions
132 * (meaning don't disable a clock without ensuring that
133 * it's not required by someone else).
135 for (g = f->gates; g->bits != 0x00000000; g++) {
136 value = CSR_READ_4(sc, g->addr);
138 CSR_WRITE_4(sc, g->addr, value);
144 free(functions, M_OFWPROP);
150 aml8726_ccm_probe(device_t dev)
153 if (!ofw_bus_status_okay(dev))
156 if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-ccm"))
159 device_set_desc(dev, "Amlogic aml8726 ccm");
161 return (BUS_PROBE_DEFAULT);
165 aml8726_ccm_attach(device_t dev)
167 struct aml8726_ccm_softc *sc = device_get_softc(dev);
171 switch (aml8726_soc_hw_rev) {
172 case AML_SOC_HW_REV_M3:
173 sc->soc = aml8726_m3_ccm;
175 case AML_SOC_HW_REV_M6:
176 sc->soc = aml8726_m6_ccm;
178 case AML_SOC_HW_REV_M8:
179 sc->soc = aml8726_m8_ccm;
181 case AML_SOC_HW_REV_M8B:
182 sc->soc = aml8726_m8b_ccm;
185 device_printf(dev, "unsupported SoC\n");
190 if (bus_alloc_resources(dev, aml8726_ccm_spec, sc->res)) {
191 device_printf(dev, "can not allocate resources for device\n");
195 AML_CCM_LOCK_INIT(sc);
197 return (aml8726_ccm_configure_gates(sc));
201 aml8726_ccm_detach(device_t dev)
203 struct aml8726_ccm_softc *sc = device_get_softc(dev);
205 AML_CCM_LOCK_DESTROY(sc);
207 bus_release_resources(dev, aml8726_ccm_spec, sc->res);
212 static device_method_t aml8726_ccm_methods[] = {
213 /* Device interface */
214 DEVMETHOD(device_probe, aml8726_ccm_probe),
215 DEVMETHOD(device_attach, aml8726_ccm_attach),
216 DEVMETHOD(device_detach, aml8726_ccm_detach),
221 static driver_t aml8726_ccm_driver = {
224 sizeof(struct aml8726_ccm_softc),
227 static devclass_t aml8726_ccm_devclass;
229 EARLY_DRIVER_MODULE(ccm, simplebus, aml8726_ccm_driver,
230 aml8726_ccm_devclass, 0, 0, BUS_PASS_CPU + BUS_PASS_ORDER_LATE);