]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/aw_ccu.c
Extract eventfilter declarations to sys/_eventfilter.h
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / aw_ccu.c
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * 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  * $FreeBSD$
27  */
28
29 /*
30  * Allwinner oscillator clock
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <machine/bus.h>
45
46 #include <dev/fdt/simplebus.h>
47
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50
51 #include <dev/extres/clk/clk.h>
52
53 #include "clkdev_if.h"
54
55 #define CCU_BASE        0x01c20000
56 #define CCU_SIZE        0x400
57
58 struct aw_ccu_softc {
59         struct simplebus_softc  sc;
60         bus_space_tag_t         bst;
61         bus_space_handle_t      bsh;
62         struct mtx              mtx;
63         int                     flags;
64 };
65
66 static struct ofw_compat_data compat_data[] = {
67         { "allwinner,sun7i-a20",        1 },
68         { "allwinner,sun6i-a31",        1 },
69         { "allwinner,sun6i-a31s",       1 },
70         { NULL, 0 }
71 };
72
73 static int
74 aw_ccu_check_addr(struct aw_ccu_softc *sc, bus_addr_t addr,
75     bus_space_handle_t *pbsh, bus_size_t *poff)
76 {
77         if (addr >= CCU_BASE && addr < (CCU_BASE + CCU_SIZE)) {
78                 *poff = addr - CCU_BASE;
79                 *pbsh = sc->bsh;
80                 return (0);
81         }
82         return (EINVAL);
83 }
84
85 static int
86 aw_ccu_write_4(device_t dev, bus_addr_t addr, uint32_t val)
87 {
88         struct aw_ccu_softc *sc;
89         bus_space_handle_t bsh;
90         bus_size_t reg;
91
92         sc = device_get_softc(dev);
93
94         if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
95                 return (EINVAL);
96
97         mtx_assert(&sc->mtx, MA_OWNED);
98         bus_space_write_4(sc->bst, bsh, reg, val);
99
100         return (0);
101 }
102
103 static int
104 aw_ccu_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
105 {
106         struct aw_ccu_softc *sc;
107         bus_space_handle_t bsh;
108         bus_size_t reg;
109
110         sc = device_get_softc(dev);
111
112         if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
113                 return (EINVAL);
114
115         mtx_assert(&sc->mtx, MA_OWNED);
116         *val = bus_space_read_4(sc->bst, bsh, reg);
117
118         return (0);
119 }
120
121 static int
122 aw_ccu_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
123 {
124         struct aw_ccu_softc *sc;
125         bus_space_handle_t bsh;
126         bus_size_t reg;
127         uint32_t val;
128
129         sc = device_get_softc(dev);
130
131         if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
132                 return (EINVAL);
133
134         mtx_assert(&sc->mtx, MA_OWNED);
135         val = bus_space_read_4(sc->bst, bsh, reg);
136         val &= ~clr;
137         val |= set;
138         bus_space_write_4(sc->bst, bsh, reg, val);
139
140         return (0);
141 }
142
143 static void
144 aw_ccu_device_lock(device_t dev)
145 {
146         struct aw_ccu_softc *sc;
147
148         sc = device_get_softc(dev);
149         mtx_lock(&sc->mtx);
150 }
151
152 static void
153 aw_ccu_device_unlock(device_t dev)
154 {
155         struct aw_ccu_softc *sc;
156
157         sc = device_get_softc(dev);
158         mtx_unlock(&sc->mtx);
159 }
160
161 static const struct ofw_compat_data *
162 aw_ccu_search_compatible(void) 
163 {
164         const struct ofw_compat_data *compat;
165         phandle_t root;
166
167         root = OF_finddevice("/");
168         for (compat = compat_data; compat->ocd_str != NULL; compat++)
169                 if (ofw_bus_node_is_compatible(root, compat->ocd_str))
170                         break;
171
172         return (compat);
173 }
174
175 static int
176 aw_ccu_probe(device_t dev)
177 {
178         const char *name;
179
180         name = ofw_bus_get_name(dev);
181
182         if (name == NULL || strcmp(name, "clocks") != 0)
183                 return (ENXIO);
184
185         if (aw_ccu_search_compatible()->ocd_data == 0)
186                 return (ENXIO);
187
188         device_set_desc(dev, "Allwinner Clock Control Unit");
189         return (BUS_PROBE_SPECIFIC);
190 }
191
192 static int
193 aw_ccu_attach(device_t dev)
194 {
195         struct aw_ccu_softc *sc;
196         phandle_t node, child;
197         device_t cdev;
198         int error;
199
200         sc = device_get_softc(dev);
201         node = ofw_bus_get_node(dev);
202
203         simplebus_init(dev, node);
204
205         sc->flags = aw_ccu_search_compatible()->ocd_data;
206
207         /*
208          * Map registers. The DT doesn't have a "reg" property
209          * for the /clocks node and child nodes have conflicting "reg"
210          * properties.
211          */
212         sc->bst = bus_get_bus_tag(dev);
213         error = bus_space_map(sc->bst, CCU_BASE, CCU_SIZE, 0,
214             &sc->bsh);
215         if (error != 0) {
216                 device_printf(dev, "couldn't map CCU: %d\n", error);
217                 return (error);
218         }
219
220         mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
221
222         /* Attach child devices */
223         for (child = OF_child(node); child > 0; child = OF_peer(child)) {
224                 cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
225                 if (cdev != NULL)
226                         device_probe_and_attach(cdev);
227         }
228
229         return (bus_generic_attach(dev));
230 }
231
232 static device_method_t aw_ccu_methods[] = {
233         /* Device interface */
234         DEVMETHOD(device_probe,         aw_ccu_probe),
235         DEVMETHOD(device_attach,        aw_ccu_attach),
236
237         /* clkdev interface */
238         DEVMETHOD(clkdev_write_4,       aw_ccu_write_4),
239         DEVMETHOD(clkdev_read_4,        aw_ccu_read_4),
240         DEVMETHOD(clkdev_modify_4,      aw_ccu_modify_4),
241         DEVMETHOD(clkdev_device_lock,   aw_ccu_device_lock),
242         DEVMETHOD(clkdev_device_unlock, aw_ccu_device_unlock),
243
244         DEVMETHOD_END
245 };
246
247 DEFINE_CLASS_1(aw_ccu, aw_ccu_driver, aw_ccu_methods,
248     sizeof(struct aw_ccu_softc), simplebus_driver);
249
250 static devclass_t aw_ccu_devclass;
251
252 EARLY_DRIVER_MODULE(aw_ccu, simplebus, aw_ccu_driver, aw_ccu_devclass,
253     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
254
255 MODULE_VERSION(aw_ccu, 1);