]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/amlogic/aml8726/aml8726_ccm.c
MFV r329710: 8966 Source file zfs_acl.c, function zfs_aclset_common contains a use...
[FreeBSD/FreeBSD.git] / sys / arm / amlogic / aml8726 / aml8726_ccm.c
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/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50
51 #include <arm/amlogic/aml8726/aml8726_soc.h>
52 #include <arm/amlogic/aml8726/aml8726_ccm.h>
53
54
55 struct aml8726_ccm_softc {
56         device_t                        dev;
57         struct aml8726_ccm_function     *soc;
58         struct resource                 *res[1];
59         struct mtx                      mtx;
60 };
61
62 static struct resource_spec aml8726_ccm_spec[] = {
63         { SYS_RES_MEMORY, 0, RF_ACTIVE },
64         { -1, 0 }
65 };
66
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),        \
71     "ccm", MTX_DEF)
72 #define AML_CCM_LOCK_DESTROY(sc)        mtx_destroy(&(sc)->mtx);
73
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)
76
77 static int
78 aml8726_ccm_configure_gates(struct aml8726_ccm_softc *sc)
79 {
80         struct aml8726_ccm_function *f;
81         struct aml8726_ccm_gate *g;
82         char *function_name;
83         char *functions;
84         phandle_t node;
85         ssize_t len;
86         uint32_t value;
87
88         node = ofw_bus_get_node(sc->dev);
89
90         len = OF_getprop_alloc(node, "functions", sizeof(char),
91             (void **)&functions);
92
93         if (len < 0) {
94                 device_printf(sc->dev, "missing functions attribute in FDT\n");
95                 return (ENXIO);
96         }
97
98         function_name = functions;
99
100         while (len) {
101                 for (f = sc->soc; f->name != NULL; f++)
102                         if (strncmp(f->name, function_name, len) == 0)
103                                 break;
104
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",
109                             len, function_name);
110                 }
111
112                 /* queue up next string */
113                 while (*function_name && len) {
114                         function_name++;
115                         len--;
116                 }
117                 if (len) {
118                         function_name++;
119                         len--;
120                 }
121
122                 if (f->name == NULL)
123                         continue;
124
125                 AML_CCM_LOCK(sc);
126
127                 /*
128                  * Enable the clock gates necessary for the function.
129                  *
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).
133                  */
134                 for (g = f->gates; g->bits != 0x00000000; g++) {
135                         value = CSR_READ_4(sc, g->addr);
136                         value |= g->bits;
137                         CSR_WRITE_4(sc, g->addr, value);
138                 }
139
140                 AML_CCM_UNLOCK(sc);
141         }
142
143         OF_prop_free(functions);
144
145         return (0);
146 }
147
148 static int
149 aml8726_ccm_probe(device_t dev)
150 {
151
152         if (!ofw_bus_status_okay(dev))
153                 return (ENXIO);
154
155         if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-ccm"))
156                 return (ENXIO);
157
158         device_set_desc(dev, "Amlogic aml8726 ccm");
159
160         return (BUS_PROBE_DEFAULT);
161 }
162
163 static int
164 aml8726_ccm_attach(device_t dev)
165 {
166         struct aml8726_ccm_softc *sc = device_get_softc(dev);
167
168         sc->dev = dev;
169
170         switch (aml8726_soc_hw_rev) {
171         case AML_SOC_HW_REV_M3:
172                 sc->soc = aml8726_m3_ccm;
173                 break;
174         case AML_SOC_HW_REV_M6:
175                 sc->soc = aml8726_m6_ccm;
176                 break;
177         case AML_SOC_HW_REV_M8:
178                 sc->soc = aml8726_m8_ccm;
179                 break;
180         case AML_SOC_HW_REV_M8B:
181                 sc->soc = aml8726_m8b_ccm;
182                 break;
183         default:
184                 device_printf(dev, "unsupported SoC\n");
185                 return (ENXIO);
186                 /* NOTREACHED */
187         }
188
189         if (bus_alloc_resources(dev, aml8726_ccm_spec, sc->res)) {
190                 device_printf(dev, "can not allocate resources for device\n");
191                 return (ENXIO);
192         }
193
194         AML_CCM_LOCK_INIT(sc);
195
196         return (aml8726_ccm_configure_gates(sc));
197 }
198
199 static int
200 aml8726_ccm_detach(device_t dev)
201 {
202         struct aml8726_ccm_softc *sc = device_get_softc(dev);
203
204         AML_CCM_LOCK_DESTROY(sc);
205
206         bus_release_resources(dev, aml8726_ccm_spec, sc->res);
207
208         return (0);
209 }
210
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),
216
217         DEVMETHOD_END
218 };
219
220 static driver_t aml8726_ccm_driver = {
221         "ccm",
222         aml8726_ccm_methods,
223         sizeof(struct aml8726_ccm_softc),
224 };
225
226 static devclass_t aml8726_ccm_devclass;
227
228 EARLY_DRIVER_MODULE(ccm, simplebus, aml8726_ccm_driver,
229     aml8726_ccm_devclass, 0, 0,  BUS_PASS_CPU + BUS_PASS_ORDER_LATE);