]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/aw_ccu.c
arm: allwinner: clk: Fix nm_recalc
[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/module.h>
42 #include <machine/bus.h>
43
44 #include <dev/fdt/simplebus.h>
45
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48
49 #include <dev/extres/clk/clk.h>
50
51 #include "clkdev_if.h"
52
53 #define CCU_BASE        0x01c20000
54 #define CCU_SIZE        0x400
55
56 #define PRCM_BASE       0x01f01400
57 #define PRCM_SIZE       0x200
58
59 #define SYSCTRL_BASE    0x01c00000
60 #define SYSCTRL_SIZE    0x34
61
62 struct aw_ccu_softc {
63         struct simplebus_softc  sc;
64         bus_space_tag_t         bst;
65         bus_space_handle_t      ccu_bsh;
66         bus_space_handle_t      prcm_bsh;
67         bus_space_handle_t      sysctrl_bsh;
68         struct mtx              mtx;
69         int                     flags;
70 };
71
72 #define CLOCK_CCU       (1 << 0)
73 #define CLOCK_PRCM      (1 << 1)
74 #define CLOCK_SYSCTRL   (1 << 2)
75
76 static struct ofw_compat_data compat_data[] = {
77         { "allwinner,sun4i-a10",        CLOCK_CCU },
78         { "allwinner,sun5i-a13",        CLOCK_CCU },
79         { "allwinner,sun7i-a20",        CLOCK_CCU },
80         { "allwinner,sun6i-a31",        CLOCK_CCU },
81         { "allwinner,sun6i-a31s",       CLOCK_CCU },
82         { "allwinner,sun50i-a64",       CLOCK_CCU },
83         { "allwinner,sun50i-h5",        CLOCK_CCU },
84         { "allwinner,sun8i-a33",        CLOCK_CCU },
85         { "allwinner,sun8i-a83t",       CLOCK_CCU|CLOCK_PRCM|CLOCK_SYSCTRL },
86         { "allwinner,sun8i-h2-plus",    CLOCK_CCU|CLOCK_PRCM },
87         { "allwinner,sun8i-h3",         CLOCK_CCU|CLOCK_PRCM },
88         { NULL, 0 }
89 };
90
91 static int
92 aw_ccu_check_addr(struct aw_ccu_softc *sc, bus_addr_t addr,
93     bus_space_handle_t *pbsh, bus_size_t *poff)
94 {
95         if (addr >= CCU_BASE && addr < (CCU_BASE + CCU_SIZE) &&
96             (sc->flags & CLOCK_CCU) != 0) {
97                 *poff = addr - CCU_BASE;
98                 *pbsh = sc->ccu_bsh;
99                 return (0);
100         }
101         if (addr >= PRCM_BASE && addr < (PRCM_BASE + PRCM_SIZE) &&
102             (sc->flags & CLOCK_PRCM) != 0) {
103                 *poff = addr - PRCM_BASE;
104                 *pbsh = sc->prcm_bsh;
105                 return (0);
106         }
107         if (addr >= SYSCTRL_BASE && addr < (SYSCTRL_BASE + SYSCTRL_SIZE) &&
108             (sc->flags & CLOCK_SYSCTRL) != 0) {
109                 *poff = addr - SYSCTRL_BASE;
110                 *pbsh = sc->sysctrl_bsh;
111                 return (0);
112         }
113         return (EINVAL);
114 }
115
116 static int
117 aw_ccu_write_4(device_t dev, bus_addr_t addr, uint32_t val)
118 {
119         struct aw_ccu_softc *sc;
120         bus_space_handle_t bsh;
121         bus_size_t reg;
122
123         sc = device_get_softc(dev);
124
125         if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
126                 return (EINVAL);
127
128         mtx_assert(&sc->mtx, MA_OWNED);
129         bus_space_write_4(sc->bst, bsh, reg, val);
130
131         return (0);
132 }
133
134 static int
135 aw_ccu_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
136 {
137         struct aw_ccu_softc *sc;
138         bus_space_handle_t bsh;
139         bus_size_t reg;
140
141         sc = device_get_softc(dev);
142
143         if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
144                 return (EINVAL);
145
146         mtx_assert(&sc->mtx, MA_OWNED);
147         *val = bus_space_read_4(sc->bst, bsh, reg);
148
149         return (0);
150 }
151
152 static int
153 aw_ccu_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
154 {
155         struct aw_ccu_softc *sc;
156         bus_space_handle_t bsh;
157         bus_size_t reg;
158         uint32_t val;
159
160         sc = device_get_softc(dev);
161
162         if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
163                 return (EINVAL);
164
165         mtx_assert(&sc->mtx, MA_OWNED);
166         val = bus_space_read_4(sc->bst, bsh, reg);
167         val &= ~clr;
168         val |= set;
169         bus_space_write_4(sc->bst, bsh, reg, val);
170
171         return (0);
172 }
173
174 static void
175 aw_ccu_device_lock(device_t dev)
176 {
177         struct aw_ccu_softc *sc;
178
179         sc = device_get_softc(dev);
180         mtx_lock(&sc->mtx);
181 }
182
183 static void
184 aw_ccu_device_unlock(device_t dev)
185 {
186         struct aw_ccu_softc *sc;
187
188         sc = device_get_softc(dev);
189         mtx_unlock(&sc->mtx);
190 }
191
192 static const struct ofw_compat_data *
193 aw_ccu_search_compatible(void) 
194 {
195         const struct ofw_compat_data *compat;
196         phandle_t root;
197
198         root = OF_finddevice("/");
199         for (compat = compat_data; compat->ocd_str != NULL; compat++)
200                 if (ofw_bus_node_is_compatible(root, compat->ocd_str))
201                         break;
202
203         return (compat);
204 }
205
206 static int
207 aw_ccu_probe(device_t dev)
208 {
209         const char *name;
210
211         name = ofw_bus_get_name(dev);
212
213         if (name == NULL || strcmp(name, "clocks") != 0)
214                 return (ENXIO);
215
216         if (aw_ccu_search_compatible()->ocd_data == 0)
217                 return (ENXIO);
218
219         device_set_desc(dev, "Allwinner Clock Control Unit");
220         return (BUS_PROBE_SPECIFIC);
221 }
222
223 static int
224 aw_ccu_attach(device_t dev)
225 {
226         struct aw_ccu_softc *sc;
227         phandle_t node, child;
228         device_t cdev;
229         int error;
230
231         sc = device_get_softc(dev);
232         node = ofw_bus_get_node(dev);
233
234         simplebus_init(dev, node);
235
236         sc->flags = aw_ccu_search_compatible()->ocd_data;
237
238         /*
239          * Map registers. The DT doesn't have a "reg" property
240          * for the /clocks node and child nodes have conflicting "reg"
241          * properties.
242          */
243         sc->bst = bus_get_bus_tag(dev);
244         if (sc->flags & CLOCK_CCU) {
245                 error = bus_space_map(sc->bst, CCU_BASE, CCU_SIZE, 0,
246                     &sc->ccu_bsh);
247                 if (error != 0) {
248                         device_printf(dev, "couldn't map CCU: %d\n", error);
249                         return (error);
250                 }
251         }
252         if (sc->flags & CLOCK_PRCM) {
253                 error = bus_space_map(sc->bst, PRCM_BASE, PRCM_SIZE, 0,
254                     &sc->prcm_bsh);
255                 if (error != 0) {
256                         device_printf(dev, "couldn't map PRCM: %d\n", error);
257                         return (error);
258                 }
259         }
260         if (sc->flags & CLOCK_SYSCTRL) {
261                 error = bus_space_map(sc->bst, SYSCTRL_BASE, SYSCTRL_SIZE, 0,
262                     &sc->sysctrl_bsh);
263                 if (error != 0) {
264                         device_printf(dev, "couldn't map SYSCTRL: %d\n", error);
265                         return (error);
266                 }
267         }
268
269         mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
270
271         /* Attach child devices */
272         for (child = OF_child(node); child > 0; child = OF_peer(child)) {
273                 cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
274                 if (cdev != NULL)
275                         device_probe_and_attach(cdev);
276         }
277
278         return (bus_generic_attach(dev));
279 }
280
281 static device_method_t aw_ccu_methods[] = {
282         /* Device interface */
283         DEVMETHOD(device_probe,         aw_ccu_probe),
284         DEVMETHOD(device_attach,        aw_ccu_attach),
285
286         /* clkdev interface */
287         DEVMETHOD(clkdev_write_4,       aw_ccu_write_4),
288         DEVMETHOD(clkdev_read_4,        aw_ccu_read_4),
289         DEVMETHOD(clkdev_modify_4,      aw_ccu_modify_4),
290         DEVMETHOD(clkdev_device_lock,   aw_ccu_device_lock),
291         DEVMETHOD(clkdev_device_unlock, aw_ccu_device_unlock),
292
293         DEVMETHOD_END
294 };
295
296 DEFINE_CLASS_1(aw_ccu, aw_ccu_driver, aw_ccu_methods,
297     sizeof(struct aw_ccu_softc), simplebus_driver);
298
299 static devclass_t aw_ccu_devclass;
300
301 EARLY_DRIVER_MODULE(aw_ccu, simplebus, aw_ccu_driver, aw_ccu_devclass,
302     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
303
304 MODULE_VERSION(aw_ccu, 1);