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