xref: /NextBSD/sys/arm/amlogic/aml8726/aml8726_ccm.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*-
2  * Copyright 2015 John Wehle <john@feith.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Amlogic aml8726 clock control module driver.
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/resource.h>
44 #include <sys/rman.h>
45 
46 #include <machine/bus.h>
47 
48 #include <dev/fdt/fdt_common.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 #include <arm/amlogic/aml8726/aml8726_soc.h>
53 #include <arm/amlogic/aml8726/aml8726_ccm.h>
54 
55 
56 struct aml8726_ccm_softc {
57 	device_t			dev;
58 	struct aml8726_ccm_function	*soc;
59 	struct resource			*res[1];
60 	struct mtx			mtx;
61 };
62 
63 static struct resource_spec aml8726_ccm_spec[] = {
64 	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
65 	{ -1, 0 }
66 };
67 
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),	\
72     "ccm", MTX_DEF)
73 #define	AML_CCM_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->mtx);
74 
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)
77 
78 static int
aml8726_ccm_configure_gates(struct aml8726_ccm_softc * sc)79 aml8726_ccm_configure_gates(struct aml8726_ccm_softc *sc)
80 {
81 	struct aml8726_ccm_function *f;
82 	struct aml8726_ccm_gate *g;
83 	char *function_name;
84 	char *functions;
85 	phandle_t node;
86 	ssize_t len;
87 	uint32_t value;
88 
89 	node = ofw_bus_get_node(sc->dev);
90 
91 	len = OF_getprop_alloc(node, "functions", sizeof(char),
92 	    (void **)&functions);
93 
94 	if (len < 0) {
95 		device_printf(sc->dev, "missing functions attribute in FDT\n");
96 		return (ENXIO);
97 	}
98 
99 	function_name = functions;
100 
101 	while (len) {
102 		for (f = sc->soc; f->name != NULL; f++)
103 			if (strncmp(f->name, function_name, len) == 0)
104 				break;
105 
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",
110 			    len, function_name);
111 		}
112 
113 		/* queue up next string */
114 		while (*function_name && len) {
115 			function_name++;
116 			len--;
117 		}
118 		if (len) {
119 			function_name++;
120 			len--;
121 		}
122 
123 		if (f->name == NULL)
124 			continue;
125 
126 		AML_CCM_LOCK(sc);
127 
128 		/*
129 		 * Enable the clock gates necessary for the function.
130 		 *
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).
134 		 */
135 		for (g = f->gates; g->bits != 0x00000000; g++) {
136 			value = CSR_READ_4(sc, g->addr);
137 			value |= g->bits;
138 			CSR_WRITE_4(sc, g->addr, value);
139 		}
140 
141 		AML_CCM_UNLOCK(sc);
142 	}
143 
144 	free(functions, M_OFWPROP);
145 
146 	return (0);
147 }
148 
149 static int
aml8726_ccm_probe(device_t dev)150 aml8726_ccm_probe(device_t dev)
151 {
152 
153 	if (!ofw_bus_status_okay(dev))
154 		return (ENXIO);
155 
156 	if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-ccm"))
157 		return (ENXIO);
158 
159 	device_set_desc(dev, "Amlogic aml8726 ccm");
160 
161 	return (BUS_PROBE_DEFAULT);
162 }
163 
164 static int
aml8726_ccm_attach(device_t dev)165 aml8726_ccm_attach(device_t dev)
166 {
167 	struct aml8726_ccm_softc *sc = device_get_softc(dev);
168 
169 	sc->dev = dev;
170 
171 	switch (aml8726_soc_hw_rev) {
172 	case AML_SOC_HW_REV_M3:
173 		sc->soc = aml8726_m3_ccm;
174 		break;
175 	case AML_SOC_HW_REV_M6:
176 		sc->soc = aml8726_m6_ccm;
177 		break;
178 	case AML_SOC_HW_REV_M8:
179 		sc->soc = aml8726_m8_ccm;
180 		break;
181 	case AML_SOC_HW_REV_M8B:
182 		sc->soc = aml8726_m8b_ccm;
183 		break;
184 	default:
185 		device_printf(dev, "unsupported SoC\n");
186 		return (ENXIO);
187 		/* NOTREACHED */
188 	}
189 
190 	if (bus_alloc_resources(dev, aml8726_ccm_spec, sc->res)) {
191 		device_printf(dev, "can not allocate resources for device\n");
192 		return (ENXIO);
193 	}
194 
195 	AML_CCM_LOCK_INIT(sc);
196 
197 	return (aml8726_ccm_configure_gates(sc));
198 }
199 
200 static int
aml8726_ccm_detach(device_t dev)201 aml8726_ccm_detach(device_t dev)
202 {
203 	struct aml8726_ccm_softc *sc = device_get_softc(dev);
204 
205 	AML_CCM_LOCK_DESTROY(sc);
206 
207 	bus_release_resources(dev, aml8726_ccm_spec, sc->res);
208 
209 	return (0);
210 }
211 
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),
217 
218 	DEVMETHOD_END
219 };
220 
221 static driver_t aml8726_ccm_driver = {
222 	"ccm",
223 	aml8726_ccm_methods,
224 	sizeof(struct aml8726_ccm_softc),
225 };
226 
227 static devclass_t aml8726_ccm_devclass;
228 
229 EARLY_DRIVER_MODULE(ccm, simplebus, aml8726_ccm_driver,
230     aml8726_ccm_devclass, 0, 0,  BUS_PASS_CPU + BUS_PASS_ORDER_LATE);
231