2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * Allwinner oscillator clock
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <machine/bus.h>
44 #include <dev/fdt/simplebus.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/extres/clk/clk.h>
51 #include "clkdev_if.h"
53 #define CCU_BASE 0x01c20000
54 #define CCU_SIZE 0x400
56 #define PRCM_BASE 0x01f01400
57 #define PRCM_SIZE 0x200
59 #define SYSCTRL_BASE 0x01c00000
60 #define SYSCTRL_SIZE 0x34
63 struct simplebus_softc sc;
65 bus_space_handle_t ccu_bsh;
66 bus_space_handle_t prcm_bsh;
67 bus_space_handle_t sysctrl_bsh;
72 #define CLOCK_CCU (1 << 0)
73 #define CLOCK_PRCM (1 << 1)
74 #define CLOCK_SYSCTRL (1 << 2)
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,sun8i-a33", CLOCK_CCU },
84 { "allwinner,sun8i-a83t", CLOCK_CCU|CLOCK_PRCM|CLOCK_SYSCTRL },
85 { "allwinner,sun8i-h3", CLOCK_CCU|CLOCK_PRCM },
90 aw_ccu_check_addr(struct aw_ccu_softc *sc, bus_addr_t addr,
91 bus_space_handle_t *pbsh, bus_size_t *poff)
93 if (addr >= CCU_BASE && addr < (CCU_BASE + CCU_SIZE) &&
94 (sc->flags & CLOCK_CCU) != 0) {
95 *poff = addr - CCU_BASE;
99 if (addr >= PRCM_BASE && addr < (PRCM_BASE + PRCM_SIZE) &&
100 (sc->flags & CLOCK_PRCM) != 0) {
101 *poff = addr - PRCM_BASE;
102 *pbsh = sc->prcm_bsh;
105 if (addr >= SYSCTRL_BASE && addr < (SYSCTRL_BASE + SYSCTRL_SIZE) &&
106 (sc->flags & CLOCK_SYSCTRL) != 0) {
107 *poff = addr - SYSCTRL_BASE;
108 *pbsh = sc->sysctrl_bsh;
115 aw_ccu_write_4(device_t dev, bus_addr_t addr, uint32_t val)
117 struct aw_ccu_softc *sc;
118 bus_space_handle_t bsh;
121 sc = device_get_softc(dev);
123 if (aw_ccu_check_addr(sc, addr, &bsh, ®) != 0)
126 mtx_assert(&sc->mtx, MA_OWNED);
127 bus_space_write_4(sc->bst, bsh, reg, val);
133 aw_ccu_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
135 struct aw_ccu_softc *sc;
136 bus_space_handle_t bsh;
139 sc = device_get_softc(dev);
141 if (aw_ccu_check_addr(sc, addr, &bsh, ®) != 0)
144 mtx_assert(&sc->mtx, MA_OWNED);
145 *val = bus_space_read_4(sc->bst, bsh, reg);
151 aw_ccu_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
153 struct aw_ccu_softc *sc;
154 bus_space_handle_t bsh;
158 sc = device_get_softc(dev);
160 if (aw_ccu_check_addr(sc, addr, &bsh, ®) != 0)
163 mtx_assert(&sc->mtx, MA_OWNED);
164 val = bus_space_read_4(sc->bst, bsh, reg);
167 bus_space_write_4(sc->bst, bsh, reg, val);
173 aw_ccu_device_lock(device_t dev)
175 struct aw_ccu_softc *sc;
177 sc = device_get_softc(dev);
182 aw_ccu_device_unlock(device_t dev)
184 struct aw_ccu_softc *sc;
186 sc = device_get_softc(dev);
187 mtx_unlock(&sc->mtx);
190 static const struct ofw_compat_data *
191 aw_ccu_search_compatible(void)
193 const struct ofw_compat_data *compat;
196 root = OF_finddevice("/");
197 for (compat = compat_data; compat->ocd_str != NULL; compat++)
198 if (ofw_bus_node_is_compatible(root, compat->ocd_str))
205 aw_ccu_probe(device_t dev)
209 name = ofw_bus_get_name(dev);
211 if (name == NULL || strcmp(name, "clocks") != 0)
214 if (aw_ccu_search_compatible()->ocd_data == 0)
217 device_set_desc(dev, "Allwinner Clock Control Unit");
218 return (BUS_PROBE_SPECIFIC);
222 aw_ccu_attach(device_t dev)
224 struct aw_ccu_softc *sc;
225 phandle_t node, child;
229 sc = device_get_softc(dev);
230 node = ofw_bus_get_node(dev);
232 simplebus_init(dev, node);
234 sc->flags = aw_ccu_search_compatible()->ocd_data;
237 * Map registers. The DT doesn't have a "reg" property
238 * for the /clocks node and child nodes have conflicting "reg"
241 sc->bst = bus_get_bus_tag(dev);
242 if (sc->flags & CLOCK_CCU) {
243 error = bus_space_map(sc->bst, CCU_BASE, CCU_SIZE, 0,
246 device_printf(dev, "couldn't map CCU: %d\n", error);
250 if (sc->flags & CLOCK_PRCM) {
251 error = bus_space_map(sc->bst, PRCM_BASE, PRCM_SIZE, 0,
254 device_printf(dev, "couldn't map PRCM: %d\n", error);
258 if (sc->flags & CLOCK_SYSCTRL) {
259 error = bus_space_map(sc->bst, SYSCTRL_BASE, SYSCTRL_SIZE, 0,
262 device_printf(dev, "couldn't map SYSCTRL: %d\n", error);
267 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
269 /* Attach child devices */
270 for (child = OF_child(node); child > 0; child = OF_peer(child)) {
271 cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
273 device_probe_and_attach(cdev);
276 return (bus_generic_attach(dev));
279 static device_method_t aw_ccu_methods[] = {
280 /* Device interface */
281 DEVMETHOD(device_probe, aw_ccu_probe),
282 DEVMETHOD(device_attach, aw_ccu_attach),
284 /* clkdev interface */
285 DEVMETHOD(clkdev_write_4, aw_ccu_write_4),
286 DEVMETHOD(clkdev_read_4, aw_ccu_read_4),
287 DEVMETHOD(clkdev_modify_4, aw_ccu_modify_4),
288 DEVMETHOD(clkdev_device_lock, aw_ccu_device_lock),
289 DEVMETHOD(clkdev_device_unlock, aw_ccu_device_unlock),
294 DEFINE_CLASS_1(aw_ccu, aw_ccu_driver, aw_ccu_methods,
295 sizeof(struct aw_ccu_softc), simplebus_driver);
297 static devclass_t aw_ccu_devclass;
299 EARLY_DRIVER_MODULE(aw_ccu, simplebus, aw_ccu_driver, aw_ccu_devclass,
300 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
302 MODULE_VERSION(aw_ccu, 1);