2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017,2018 Emmanuel Vadot <manu@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Allwinner Clock Control Unit
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <machine/bus.h>
47 #include <dev/fdt/simplebus.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/extres/clk/clk.h>
53 #include <dev/extres/clk/clk_gate.h>
55 #include <dev/extres/hwreset/hwreset.h>
57 #include <arm/allwinner/clkng/aw_ccung.h>
58 #include <arm/allwinner/clkng/aw_clk.h>
64 #include "clkdev_if.h"
65 #include "hwreset_if.h"
67 static struct resource_spec aw_ccung_spec[] = {
68 { SYS_RES_MEMORY, 0, RF_ACTIVE },
72 #define CCU_READ4(sc, reg) bus_read_4((sc)->res, (reg))
73 #define CCU_WRITE4(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
76 aw_ccung_write_4(device_t dev, bus_addr_t addr, uint32_t val)
78 struct aw_ccung_softc *sc;
80 sc = device_get_softc(dev);
81 CCU_WRITE4(sc, addr, val);
86 aw_ccung_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
88 struct aw_ccung_softc *sc;
90 sc = device_get_softc(dev);
92 *val = CCU_READ4(sc, addr);
97 aw_ccung_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
99 struct aw_ccung_softc *sc;
102 sc = device_get_softc(dev);
104 reg = CCU_READ4(sc, addr);
107 CCU_WRITE4(sc, addr, reg);
113 aw_ccung_reset_assert(device_t dev, intptr_t id, bool reset)
115 struct aw_ccung_softc *sc;
118 sc = device_get_softc(dev);
120 if (id >= sc->nresets || sc->resets[id].offset == 0)
124 val = CCU_READ4(sc, sc->resets[id].offset);
126 val &= ~(1 << sc->resets[id].shift);
128 val |= 1 << sc->resets[id].shift;
129 CCU_WRITE4(sc, sc->resets[id].offset, val);
130 mtx_unlock(&sc->mtx);
136 aw_ccung_reset_is_asserted(device_t dev, intptr_t id, bool *reset)
138 struct aw_ccung_softc *sc;
141 sc = device_get_softc(dev);
143 if (id >= sc->nresets || sc->resets[id].offset == 0)
147 val = CCU_READ4(sc, sc->resets[id].offset);
148 *reset = (val & (1 << sc->resets[id].shift)) != 0 ? false : true;
149 mtx_unlock(&sc->mtx);
155 aw_ccung_device_lock(device_t dev)
157 struct aw_ccung_softc *sc;
159 sc = device_get_softc(dev);
164 aw_ccung_device_unlock(device_t dev)
166 struct aw_ccung_softc *sc;
168 sc = device_get_softc(dev);
169 mtx_unlock(&sc->mtx);
173 aw_ccung_register_gates(struct aw_ccung_softc *sc)
175 struct clk_gate_def def;
178 for (i = 0; i < sc->ngates; i++) {
179 if (sc->gates[i].name == NULL)
181 memset(&def, 0, sizeof(def));
183 def.clkdef.name = sc->gates[i].name;
184 def.clkdef.parent_names = &sc->gates[i].parent_name;
185 def.clkdef.parent_cnt = 1;
186 def.offset = sc->gates[i].offset;
187 def.shift = sc->gates[i].shift;
191 clknode_gate_register(sc->clkdom, &def);
198 aw_ccung_init_clocks(struct aw_ccung_softc *sc)
200 struct clknode *clknode;
203 for (i = 0; i < sc->n_clk_init; i++) {
204 clknode = clknode_find_by_name(sc->clk_init[i].name);
205 if (clknode == NULL) {
206 device_printf(sc->dev, "Cannot find clock %s\n",
207 sc->clk_init[i].name);
211 if (sc->clk_init[i].parent_name != NULL) {
213 device_printf(sc->dev, "Setting %s as parent for %s\n",
214 sc->clk_init[i].parent_name,
215 sc->clk_init[i].name);
216 error = clknode_set_parent_by_name(clknode,
217 sc->clk_init[i].parent_name);
219 device_printf(sc->dev,
220 "Cannot set parent to %s for %s\n",
221 sc->clk_init[i].parent_name,
222 sc->clk_init[i].name);
226 if (sc->clk_init[i].default_freq != 0) {
227 error = clknode_set_freq(clknode,
228 sc->clk_init[i].default_freq, 0 , 0);
230 device_printf(sc->dev,
231 "Cannot set frequency for %s to %ju\n",
232 sc->clk_init[i].name,
233 sc->clk_init[i].default_freq);
237 if (sc->clk_init[i].enable) {
238 error = clknode_enable(clknode);
240 device_printf(sc->dev,
241 "Cannot enable %s\n",
242 sc->clk_init[i].name);
250 aw_ccung_attach(device_t dev)
252 struct aw_ccung_softc *sc;
255 sc = device_get_softc(dev);
258 if (bus_alloc_resources(dev, aw_ccung_spec, &sc->res) != 0) {
259 device_printf(dev, "cannot allocate resources for device\n");
263 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
265 sc->clkdom = clkdom_create(dev);
266 if (sc->clkdom == NULL)
267 panic("Cannot create clkdom\n");
269 for (i = 0; i < sc->nclks; i++) {
270 switch (sc->clks[i].type) {
271 case AW_CLK_UNDEFINED:
274 clknode_mux_register(sc->clkdom, sc->clks[i].clk.mux);
277 clknode_div_register(sc->clkdom, sc->clks[i].clk.div);
280 clknode_fixed_register(sc->clkdom,
281 sc->clks[i].clk.fixed);
284 aw_clk_nkmp_register(sc->clkdom, sc->clks[i].clk.nkmp);
287 aw_clk_nm_register(sc->clkdom, sc->clks[i].clk.nm);
289 case AW_CLK_PREDIV_MUX:
290 aw_clk_prediv_mux_register(sc->clkdom,
291 sc->clks[i].clk.prediv_mux);
297 aw_ccung_register_gates(sc);
298 if (clkdom_finit(sc->clkdom) != 0)
299 panic("cannot finalize clkdom initialization\n");
301 clkdom_xlock(sc->clkdom);
302 aw_ccung_init_clocks(sc);
303 clkdom_unlock(sc->clkdom);
306 clkdom_dump(sc->clkdom);
308 /* If we have resets, register our self as a reset provider */
310 hwreset_register_ofw_provider(dev);
315 static device_method_t aw_ccung_methods[] = {
316 /* clkdev interface */
317 DEVMETHOD(clkdev_write_4, aw_ccung_write_4),
318 DEVMETHOD(clkdev_read_4, aw_ccung_read_4),
319 DEVMETHOD(clkdev_modify_4, aw_ccung_modify_4),
320 DEVMETHOD(clkdev_device_lock, aw_ccung_device_lock),
321 DEVMETHOD(clkdev_device_unlock, aw_ccung_device_unlock),
323 /* Reset interface */
324 DEVMETHOD(hwreset_assert, aw_ccung_reset_assert),
325 DEVMETHOD(hwreset_is_asserted, aw_ccung_reset_is_asserted),
330 DEFINE_CLASS_0(aw_ccung, aw_ccung_driver, aw_ccung_methods,
331 sizeof(struct aw_ccung_softc));