1e37e8677SEmmanuel Vadot /*-
2e37e8677SEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause
3e37e8677SEmmanuel Vadot *
4e37e8677SEmmanuel Vadot * Copyright (c) 2019 Emmanuel Vadot <manu@freebsd.org>
5e37e8677SEmmanuel Vadot *
6e37e8677SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
7e37e8677SEmmanuel Vadot * modification, are permitted provided that the following conditions
8e37e8677SEmmanuel Vadot * are met:
9e37e8677SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
10e37e8677SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
11e37e8677SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
12e37e8677SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
13e37e8677SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
14e37e8677SEmmanuel Vadot *
15e37e8677SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16e37e8677SEmmanuel Vadot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17e37e8677SEmmanuel Vadot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18e37e8677SEmmanuel Vadot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19e37e8677SEmmanuel Vadot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20e37e8677SEmmanuel Vadot * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21e37e8677SEmmanuel Vadot * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22e37e8677SEmmanuel Vadot * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23e37e8677SEmmanuel Vadot * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e37e8677SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e37e8677SEmmanuel Vadot * SUCH DAMAGE.
26e37e8677SEmmanuel Vadot */
27e37e8677SEmmanuel Vadot
28e37e8677SEmmanuel Vadot #include <sys/param.h>
29e37e8677SEmmanuel Vadot #include <sys/systm.h>
30e37e8677SEmmanuel Vadot #include <sys/bus.h>
31e37e8677SEmmanuel Vadot #include <sys/rman.h>
32e37e8677SEmmanuel Vadot #include <sys/kernel.h>
33e37e8677SEmmanuel Vadot #include <sys/module.h>
34e37e8677SEmmanuel Vadot #include <machine/bus.h>
35e37e8677SEmmanuel Vadot
36e37e8677SEmmanuel Vadot #include <dev/fdt/simplebus.h>
37e37e8677SEmmanuel Vadot
38e37e8677SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
39e37e8677SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
40e37e8677SEmmanuel Vadot
41*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_div.h>
42*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_fixed.h>
43*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_mux.h>
44e37e8677SEmmanuel Vadot
45e37e8677SEmmanuel Vadot #include <dev/clk/allwinner/aw_ccung.h>
46e37e8677SEmmanuel Vadot
47e37e8677SEmmanuel Vadot #include <dt-bindings/clock/sun50i-h6-r-ccu.h>
48e37e8677SEmmanuel Vadot #include <dt-bindings/reset/sun50i-h6-r-ccu.h>
49e37e8677SEmmanuel Vadot
50e37e8677SEmmanuel Vadot /* Non-exported clocks */
51e37e8677SEmmanuel Vadot #define CLK_R_AHB 1
52e37e8677SEmmanuel Vadot #define CLK_R_APB2 3
53e37e8677SEmmanuel Vadot
54e37e8677SEmmanuel Vadot static struct aw_ccung_reset ccu_sun50i_h6_r_resets[] = {
55e37e8677SEmmanuel Vadot CCU_RESET(RST_R_APB1_TIMER, 0x11c, 16)
56e37e8677SEmmanuel Vadot CCU_RESET(RST_R_APB1_TWD, 0x12c, 16)
57e37e8677SEmmanuel Vadot CCU_RESET(RST_R_APB1_PWM, 0x13c, 16)
58e37e8677SEmmanuel Vadot CCU_RESET(RST_R_APB2_UART, 0x18c, 16)
59e37e8677SEmmanuel Vadot CCU_RESET(RST_R_APB2_I2C, 0x19c, 16)
60e37e8677SEmmanuel Vadot CCU_RESET(RST_R_APB1_IR, 0x1cc, 16)
61e37e8677SEmmanuel Vadot CCU_RESET(RST_R_APB1_W1, 0x1ec, 16)
62e37e8677SEmmanuel Vadot };
63e37e8677SEmmanuel Vadot
64e37e8677SEmmanuel Vadot static struct aw_ccung_gate ccu_sun50i_h6_r_gates[] = {
65e37e8677SEmmanuel Vadot CCU_GATE(CLK_R_APB1_TIMER, "r_apb1-timer", "r_apb1", 0x11c, 0)
66e37e8677SEmmanuel Vadot CCU_GATE(CLK_R_APB1_TWD, "r_apb1-twd", "r_apb1", 0x12c, 0)
67e37e8677SEmmanuel Vadot CCU_GATE(CLK_R_APB1_PWM, "r_apb1-pwm", "r_apb1", 0x13c, 0)
68e37e8677SEmmanuel Vadot CCU_GATE(CLK_R_APB2_UART, "r_apb1-uart", "r_apb2", 0x18c, 0)
69e37e8677SEmmanuel Vadot CCU_GATE(CLK_R_APB2_I2C, "r_apb1-i2c", "r_apb2", 0x19c, 0)
70e37e8677SEmmanuel Vadot CCU_GATE(CLK_R_APB1_IR, "r_apb1-ir", "r_apb1", 0x1cc, 0)
71e37e8677SEmmanuel Vadot CCU_GATE(CLK_R_APB1_W1, "r_apb1-w1", "r_apb1", 0x1ec, 0)
72e37e8677SEmmanuel Vadot };
73e37e8677SEmmanuel Vadot
74e37e8677SEmmanuel Vadot static const char *ar100_parents[] = {"osc24M", "osc32k", "pll_periph0", "iosc"};
75e37e8677SEmmanuel Vadot PREDIV_CLK(ar100_clk, CLK_AR100, /* id */
76e37e8677SEmmanuel Vadot "ar100", ar100_parents, /* name, parents */
77e37e8677SEmmanuel Vadot 0x00, /* offset */
78e37e8677SEmmanuel Vadot 16, 2, /* mux */
79e37e8677SEmmanuel Vadot 4, 2, 0, AW_CLK_FACTOR_POWER_OF_TWO, /* div */
80e37e8677SEmmanuel Vadot 8, 5, 0, AW_CLK_FACTOR_HAS_COND, /* prediv */
81e37e8677SEmmanuel Vadot 16, 2, 2); /* prediv condition */
82e37e8677SEmmanuel Vadot
83e37e8677SEmmanuel Vadot static const char *r_ahb_parents[] = {"ar100"};
84e37e8677SEmmanuel Vadot FIXED_CLK(r_ahb_clk,
85e37e8677SEmmanuel Vadot CLK_R_AHB, /* id */
86e37e8677SEmmanuel Vadot "r_ahb", /* name */
87e37e8677SEmmanuel Vadot r_ahb_parents, /* parent */
88e37e8677SEmmanuel Vadot 0, /* freq */
89e37e8677SEmmanuel Vadot 1, /* mult */
90e37e8677SEmmanuel Vadot 1, /* div */
91e37e8677SEmmanuel Vadot 0); /* flags */
92e37e8677SEmmanuel Vadot
93e37e8677SEmmanuel Vadot static const char *r_apb1_parents[] = {"r_ahb"};
94e37e8677SEmmanuel Vadot DIV_CLK(r_apb1_clk,
95e37e8677SEmmanuel Vadot CLK_R_APB1, /* id */
96e37e8677SEmmanuel Vadot "r_apb1", r_apb1_parents, /* name, parents */
97e37e8677SEmmanuel Vadot 0x0c, /* offset */
98e37e8677SEmmanuel Vadot 0, 2, /* shift, width */
99e37e8677SEmmanuel Vadot 0, NULL); /* flags, div table */
100e37e8677SEmmanuel Vadot
101e37e8677SEmmanuel Vadot static const char *r_apb2_parents[] = {"osc24M", "osc32k", "pll_periph0", "iosc"};
102e37e8677SEmmanuel Vadot PREDIV_CLK(r_apb2_clk, CLK_R_APB2, /* id */
103e37e8677SEmmanuel Vadot "r_apb2", r_apb2_parents, /* name, parents */
104e37e8677SEmmanuel Vadot 0x10, /* offset */
105e37e8677SEmmanuel Vadot 16, 2, /* mux */
106e37e8677SEmmanuel Vadot 4, 2, 0, AW_CLK_FACTOR_POWER_OF_TWO, /* div */
107e37e8677SEmmanuel Vadot 8, 5, 0, AW_CLK_FACTOR_HAS_COND, /* prediv */
108e37e8677SEmmanuel Vadot 16, 2, 2); /* prediv condition */
109e37e8677SEmmanuel Vadot
110e37e8677SEmmanuel Vadot static struct aw_ccung_clk clks[] = {
111e37e8677SEmmanuel Vadot { .type = AW_CLK_PREDIV_MUX, .clk.prediv_mux = &ar100_clk},
112e37e8677SEmmanuel Vadot { .type = AW_CLK_FIXED, .clk.fixed = &r_ahb_clk},
113e37e8677SEmmanuel Vadot { .type = AW_CLK_DIV, .clk.div = &r_apb1_clk},
114e37e8677SEmmanuel Vadot { .type = AW_CLK_PREDIV_MUX, .clk.prediv_mux = &r_apb2_clk},
115e37e8677SEmmanuel Vadot };
116e37e8677SEmmanuel Vadot
117e37e8677SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
118e37e8677SEmmanuel Vadot { "allwinner,sun50i-h6-r-ccu", 1 },
119e37e8677SEmmanuel Vadot { NULL, 0},
120e37e8677SEmmanuel Vadot };
121e37e8677SEmmanuel Vadot
122e37e8677SEmmanuel Vadot static int
ccu_sun50i_h6_r_probe(device_t dev)123e37e8677SEmmanuel Vadot ccu_sun50i_h6_r_probe(device_t dev)
124e37e8677SEmmanuel Vadot {
125e37e8677SEmmanuel Vadot
126e37e8677SEmmanuel Vadot if (!ofw_bus_status_okay(dev))
127e37e8677SEmmanuel Vadot return (ENXIO);
128e37e8677SEmmanuel Vadot
129e37e8677SEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
130e37e8677SEmmanuel Vadot return (ENXIO);
131e37e8677SEmmanuel Vadot
132e37e8677SEmmanuel Vadot device_set_desc(dev, "Allwinner SUN50I_H6_R Clock Control Unit NG");
133e37e8677SEmmanuel Vadot return (BUS_PROBE_DEFAULT);
134e37e8677SEmmanuel Vadot }
135e37e8677SEmmanuel Vadot
136e37e8677SEmmanuel Vadot static int
ccu_sun50i_h6_r_attach(device_t dev)137e37e8677SEmmanuel Vadot ccu_sun50i_h6_r_attach(device_t dev)
138e37e8677SEmmanuel Vadot {
139e37e8677SEmmanuel Vadot struct aw_ccung_softc *sc;
140e37e8677SEmmanuel Vadot
141e37e8677SEmmanuel Vadot sc = device_get_softc(dev);
142e37e8677SEmmanuel Vadot
143e37e8677SEmmanuel Vadot sc->resets = ccu_sun50i_h6_r_resets;
144e37e8677SEmmanuel Vadot sc->nresets = nitems(ccu_sun50i_h6_r_resets);
145e37e8677SEmmanuel Vadot sc->gates = ccu_sun50i_h6_r_gates;
146e37e8677SEmmanuel Vadot sc->ngates = nitems(ccu_sun50i_h6_r_gates);
147e37e8677SEmmanuel Vadot sc->clks = clks;
148e37e8677SEmmanuel Vadot sc->nclks = nitems(clks);
149e37e8677SEmmanuel Vadot
150e37e8677SEmmanuel Vadot return (aw_ccung_attach(dev));
151e37e8677SEmmanuel Vadot }
152e37e8677SEmmanuel Vadot
153e37e8677SEmmanuel Vadot static device_method_t ccu_sun50i_h6_r_methods[] = {
154e37e8677SEmmanuel Vadot /* Device interface */
155e37e8677SEmmanuel Vadot DEVMETHOD(device_probe, ccu_sun50i_h6_r_probe),
156e37e8677SEmmanuel Vadot DEVMETHOD(device_attach, ccu_sun50i_h6_r_attach),
157e37e8677SEmmanuel Vadot
158e37e8677SEmmanuel Vadot DEVMETHOD_END
159e37e8677SEmmanuel Vadot };
160e37e8677SEmmanuel Vadot
161e37e8677SEmmanuel Vadot DEFINE_CLASS_1(ccu_sun50i_h6_r, ccu_sun50i_h6_r_driver, ccu_sun50i_h6_r_methods,
162e37e8677SEmmanuel Vadot sizeof(struct aw_ccung_softc), aw_ccung_driver);
163e37e8677SEmmanuel Vadot
164e37e8677SEmmanuel Vadot EARLY_DRIVER_MODULE(ccu_sun50i_h6_r, simplebus, ccu_sun50i_h6_r_driver, 0, 0,
165e37e8677SEmmanuel Vadot BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
166