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