xref: /freebsd/sys/dev/clk/allwinner/ccu_de2.c (revision 1f469a9fc498c3d406ef7c4e347232678f49da0a)
1e37e8677SEmmanuel Vadot /*-
2e37e8677SEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
3e37e8677SEmmanuel Vadot  *
4e37e8677SEmmanuel Vadot  * Copyright (c) 2018 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 
41e37e8677SEmmanuel Vadot #ifdef __aarch64__
42e37e8677SEmmanuel Vadot #include "opt_soc.h"
43e37e8677SEmmanuel Vadot #endif
44e37e8677SEmmanuel Vadot 
45be82b3a0SEmmanuel Vadot #include <dev/clk/clk_div.h>
46be82b3a0SEmmanuel Vadot #include <dev/clk/clk_fixed.h>
47be82b3a0SEmmanuel Vadot #include <dev/clk/clk_mux.h>
48e37e8677SEmmanuel Vadot 
49*1f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
50e37e8677SEmmanuel Vadot 
51e37e8677SEmmanuel Vadot #include <dev/clk/allwinner/aw_ccung.h>
52e37e8677SEmmanuel Vadot 
53e37e8677SEmmanuel Vadot #include <dt-bindings/clock/sun8i-de2.h>
54e37e8677SEmmanuel Vadot #include <dt-bindings/reset/sun8i-de2.h>
55e37e8677SEmmanuel Vadot 
56e37e8677SEmmanuel Vadot enum CCU_DE2 {
57e37e8677SEmmanuel Vadot 	H3_CCU = 1,
58e37e8677SEmmanuel Vadot 	A64_CCU,
59e37e8677SEmmanuel Vadot };
60e37e8677SEmmanuel Vadot 
61e37e8677SEmmanuel Vadot /* Non exported clocks */
62e37e8677SEmmanuel Vadot #define	CLK_MIXER0_DIV	3
63e37e8677SEmmanuel Vadot #define	CLK_MIXER1_DIV	4
64e37e8677SEmmanuel Vadot #define	CLK_WB_DIV	5
65e37e8677SEmmanuel Vadot 
66e37e8677SEmmanuel Vadot static struct aw_ccung_reset h3_de2_ccu_resets[] = {
67e37e8677SEmmanuel Vadot 	CCU_RESET(RST_MIXER0, 0x08, 0)
68e37e8677SEmmanuel Vadot 	CCU_RESET(RST_WB, 0x08, 2)
69e37e8677SEmmanuel Vadot };
70e37e8677SEmmanuel Vadot 
71e37e8677SEmmanuel Vadot static struct aw_ccung_reset a64_de2_ccu_resets[] = {
72e37e8677SEmmanuel Vadot 	CCU_RESET(RST_MIXER0, 0x08, 0)
73e37e8677SEmmanuel Vadot 	CCU_RESET(RST_MIXER1, 0x08, 1)
74e37e8677SEmmanuel Vadot 	CCU_RESET(RST_WB, 0x08, 2)
75e37e8677SEmmanuel Vadot };
76e37e8677SEmmanuel Vadot 
77e37e8677SEmmanuel Vadot static struct aw_ccung_gate h3_de2_ccu_gates[] = {
78e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_BUS_MIXER0, "mixer0", "mixer0-div", 0x00, 0)
79e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_BUS_WB, "wb", "wb-div", 0x00, 2)
80e37e8677SEmmanuel Vadot 
81e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_MIXER0, "bus-mixer0", "bus-de", 0x04, 0)
82e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_WB, "bus-wb", "bus-de", 0x04, 2)
83e37e8677SEmmanuel Vadot };
84e37e8677SEmmanuel Vadot 
85e37e8677SEmmanuel Vadot static struct aw_ccung_gate a64_de2_ccu_gates[] = {
86e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_BUS_MIXER0, "mixer0", "mixer0-div", 0x00, 0)
87e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_BUS_MIXER1, "mixer1", "mixer1-div", 0x00, 1)
88e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_BUS_WB, "wb", "wb-div", 0x00, 2)
89e37e8677SEmmanuel Vadot 
90e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_MIXER0, "bus-mixer0", "bus-de", 0x04, 0)
91e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_MIXER1, "bus-mixer1", "bus-de", 0x04, 1)
92e37e8677SEmmanuel Vadot 	CCU_GATE(CLK_WB, "bus-wb", "bus-de", 0x04, 2)
93e37e8677SEmmanuel Vadot };
94e37e8677SEmmanuel Vadot 
95e37e8677SEmmanuel Vadot static const char *div_parents[] = {"de"};
96e37e8677SEmmanuel Vadot 
97e37e8677SEmmanuel Vadot NM_CLK(mixer0_div_clk,
98e37e8677SEmmanuel Vadot     CLK_MIXER0_DIV,			/* id */
99e37e8677SEmmanuel Vadot     "mixer0-div", div_parents,		/* names, parents */
100e37e8677SEmmanuel Vadot     0x0C,				/* offset */
101e37e8677SEmmanuel Vadot     0, 0, 1, AW_CLK_FACTOR_FIXED,	/* N factor (fake)*/
102e37e8677SEmmanuel Vadot     0, 4, 0, 0,				/* M flags */
103e37e8677SEmmanuel Vadot     0, 0,				/* mux */
104e37e8677SEmmanuel Vadot     0,					/* gate */
105e37e8677SEmmanuel Vadot     AW_CLK_SCALE_CHANGE);	/* flags */
106e37e8677SEmmanuel Vadot 
107e37e8677SEmmanuel Vadot NM_CLK(mixer1_div_clk,
108e37e8677SEmmanuel Vadot     CLK_MIXER1_DIV,			/* id */
109e37e8677SEmmanuel Vadot     "mixer1-div", div_parents,		/* names, parents */
110e37e8677SEmmanuel Vadot     0x0C,				/* offset */
111e37e8677SEmmanuel Vadot     0, 0, 1, AW_CLK_FACTOR_FIXED,	/* N factor (fake)*/
112e37e8677SEmmanuel Vadot     4, 4, 0, 0,				/* M flags */
113e37e8677SEmmanuel Vadot     0, 0,				/* mux */
114e37e8677SEmmanuel Vadot     0,					/* gate */
115e37e8677SEmmanuel Vadot     AW_CLK_SCALE_CHANGE);		/* flags */
116e37e8677SEmmanuel Vadot 
117e37e8677SEmmanuel Vadot NM_CLK(wb_div_clk,
118e37e8677SEmmanuel Vadot     CLK_WB_DIV,				/* id */
119e37e8677SEmmanuel Vadot     "wb-div", div_parents,		/* names, parents */
120e37e8677SEmmanuel Vadot     0x0C,				/* offset */
121e37e8677SEmmanuel Vadot     0, 0, 1, AW_CLK_FACTOR_FIXED,	/* N factor (fake)*/
122e37e8677SEmmanuel Vadot     8, 4, 0, 0,				/* M flags */
123e37e8677SEmmanuel Vadot     0, 0,				/* mux */
124e37e8677SEmmanuel Vadot     0,					/* gate */
125e37e8677SEmmanuel Vadot     AW_CLK_SCALE_CHANGE);		/* flags */
126e37e8677SEmmanuel Vadot 
127e37e8677SEmmanuel Vadot static struct aw_ccung_clk h3_de2_ccu_clks[] = {
128e37e8677SEmmanuel Vadot 	{ .type = AW_CLK_NM, .clk.nm = &mixer0_div_clk},
129e37e8677SEmmanuel Vadot 	{ .type = AW_CLK_NM, .clk.nm = &wb_div_clk},
130e37e8677SEmmanuel Vadot };
131e37e8677SEmmanuel Vadot 
132e37e8677SEmmanuel Vadot static struct aw_ccung_clk a64_de2_ccu_clks[] = {
133e37e8677SEmmanuel Vadot 	{ .type = AW_CLK_NM, .clk.nm = &mixer0_div_clk},
134e37e8677SEmmanuel Vadot 	{ .type = AW_CLK_NM, .clk.nm = &mixer1_div_clk},
135e37e8677SEmmanuel Vadot 	{ .type = AW_CLK_NM, .clk.nm = &wb_div_clk},
136e37e8677SEmmanuel Vadot };
137e37e8677SEmmanuel Vadot 
138e37e8677SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
139e37e8677SEmmanuel Vadot 	{"allwinner,sun8i-h3-de2-clk", H3_CCU},
140e37e8677SEmmanuel Vadot 	{"allwinner,sun50i-a64-de2-clk", A64_CCU},
141e37e8677SEmmanuel Vadot 	{NULL,             0}
142e37e8677SEmmanuel Vadot };
143e37e8677SEmmanuel Vadot 
144e37e8677SEmmanuel Vadot static int
ccu_de2_probe(device_t dev)145e37e8677SEmmanuel Vadot ccu_de2_probe(device_t dev)
146e37e8677SEmmanuel Vadot {
147e37e8677SEmmanuel Vadot 
148e37e8677SEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
149e37e8677SEmmanuel Vadot 		return (ENXIO);
150e37e8677SEmmanuel Vadot 
151e37e8677SEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
152e37e8677SEmmanuel Vadot 		return (ENXIO);
153e37e8677SEmmanuel Vadot 
154e37e8677SEmmanuel Vadot 	device_set_desc(dev, "Allwinner DE2 Clock Control Unit");
155e37e8677SEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
156e37e8677SEmmanuel Vadot }
157e37e8677SEmmanuel Vadot 
158e37e8677SEmmanuel Vadot static int
ccu_de2_attach(device_t dev)159e37e8677SEmmanuel Vadot ccu_de2_attach(device_t dev)
160e37e8677SEmmanuel Vadot {
161e37e8677SEmmanuel Vadot 	struct aw_ccung_softc *sc;
162e37e8677SEmmanuel Vadot 	phandle_t node;
163e37e8677SEmmanuel Vadot 	clk_t mod, bus;
164e37e8677SEmmanuel Vadot 	hwreset_t rst_de;
165e37e8677SEmmanuel Vadot 	enum CCU_DE2 type;
166e37e8677SEmmanuel Vadot 
167e37e8677SEmmanuel Vadot 	sc = device_get_softc(dev);
168e37e8677SEmmanuel Vadot 	node = ofw_bus_get_node(dev);
169e37e8677SEmmanuel Vadot 
170e37e8677SEmmanuel Vadot 	type = (enum CCU_DE2)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
171e37e8677SEmmanuel Vadot 
172e37e8677SEmmanuel Vadot 	switch (type) {
173e37e8677SEmmanuel Vadot 	case H3_CCU:
174e37e8677SEmmanuel Vadot 		sc->resets = h3_de2_ccu_resets;
175e37e8677SEmmanuel Vadot 		sc->nresets = nitems(h3_de2_ccu_resets);
176e37e8677SEmmanuel Vadot 		sc->gates = h3_de2_ccu_gates;
177e37e8677SEmmanuel Vadot 		sc->ngates = nitems(h3_de2_ccu_gates);
178e37e8677SEmmanuel Vadot 		sc->clks = h3_de2_ccu_clks;
179e37e8677SEmmanuel Vadot 		sc->nclks = nitems(h3_de2_ccu_clks);
180e37e8677SEmmanuel Vadot 		break;
181e37e8677SEmmanuel Vadot 	case A64_CCU:
182e37e8677SEmmanuel Vadot 		sc->resets = a64_de2_ccu_resets;
183e37e8677SEmmanuel Vadot 		sc->nresets = nitems(a64_de2_ccu_resets);
184e37e8677SEmmanuel Vadot 		sc->gates = a64_de2_ccu_gates;
185e37e8677SEmmanuel Vadot 		sc->ngates = nitems(a64_de2_ccu_gates);
186e37e8677SEmmanuel Vadot 		sc->clks = a64_de2_ccu_clks;
187e37e8677SEmmanuel Vadot 		sc->nclks = nitems(a64_de2_ccu_clks);
188e37e8677SEmmanuel Vadot 		break;
189e37e8677SEmmanuel Vadot 	}
190e37e8677SEmmanuel Vadot 
191e37e8677SEmmanuel Vadot 	if (hwreset_get_by_ofw_idx(dev, node, 0, &rst_de) != 0) {
192e37e8677SEmmanuel Vadot 		device_printf(dev, "Cannot get de reset\n");
193e37e8677SEmmanuel Vadot 		return (ENXIO);
194e37e8677SEmmanuel Vadot 	}
195e37e8677SEmmanuel Vadot 	if (hwreset_deassert(rst_de) != 0) {
196e37e8677SEmmanuel Vadot 		device_printf(dev, "Cannot de-assert de reset\n");
197e37e8677SEmmanuel Vadot 		return (ENXIO);
198e37e8677SEmmanuel Vadot 	}
199e37e8677SEmmanuel Vadot 
200e37e8677SEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, node, "mod", &mod) != 0) {
201e37e8677SEmmanuel Vadot 		device_printf(dev, "Cannot get mod clock\n");
202e37e8677SEmmanuel Vadot 		return (ENXIO);
203e37e8677SEmmanuel Vadot 	}
204e37e8677SEmmanuel Vadot 	if (clk_enable(mod) != 0) {
205e37e8677SEmmanuel Vadot 		device_printf(dev, "Cannot enable mod clock\n");
206e37e8677SEmmanuel Vadot 		return (ENXIO);
207e37e8677SEmmanuel Vadot 	}
208e37e8677SEmmanuel Vadot 
209e37e8677SEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, node, "bus", &bus) != 0) {
210e37e8677SEmmanuel Vadot 		device_printf(dev, "Cannot get bus clock\n");
211e37e8677SEmmanuel Vadot 		return (ENXIO);
212e37e8677SEmmanuel Vadot 	}
213e37e8677SEmmanuel Vadot 	if (clk_enable(bus) != 0) {
214e37e8677SEmmanuel Vadot 		device_printf(dev, "Cannot enable bus clock\n");
215e37e8677SEmmanuel Vadot 		return (ENXIO);
216e37e8677SEmmanuel Vadot 	}
217e37e8677SEmmanuel Vadot 
218e37e8677SEmmanuel Vadot 	return (aw_ccung_attach(dev));
219e37e8677SEmmanuel Vadot }
220e37e8677SEmmanuel Vadot 
221e37e8677SEmmanuel Vadot static device_method_t ccu_de2_methods[] = {
222e37e8677SEmmanuel Vadot 	/* Device interface */
223e37e8677SEmmanuel Vadot 	DEVMETHOD(device_probe,		ccu_de2_probe),
224e37e8677SEmmanuel Vadot 	DEVMETHOD(device_attach,	ccu_de2_attach),
225e37e8677SEmmanuel Vadot 
226e37e8677SEmmanuel Vadot 	DEVMETHOD_END
227e37e8677SEmmanuel Vadot };
228e37e8677SEmmanuel Vadot 
229e37e8677SEmmanuel Vadot DEFINE_CLASS_1(ccu_de2, ccu_de2_driver, ccu_de2_methods,
230e37e8677SEmmanuel Vadot   sizeof(struct aw_ccung_softc), aw_ccung_driver);
231e37e8677SEmmanuel Vadot 
232e37e8677SEmmanuel Vadot EARLY_DRIVER_MODULE(ccu_de2, simplebus, ccu_de2_driver, 0, 0,
233e37e8677SEmmanuel Vadot     BUS_PASS_RESOURCE + BUS_PASS_ORDER_LAST);
234