xref: /freebsd/sys/dev/clk/starfive/jh7110_clk_sys.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
5  * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
6  * Copyright (c) 2022 Mitchell Horne <mhorne@FreeBSD.org>
7  * Copyright (c) 2024 Jari Sihvola <jsihv@gmx.com>
8  */
9 
10 /* Clocks for JH7110 SYS group. PLL driver must be attached before this. */
11 
12 #include <sys/param.h>
13 #include <sys/systm.h>
14 #include <sys/bus.h>
15 #include <sys/kernel.h>
16 #include <sys/module.h>
17 #include <sys/mutex.h>
18 #include <sys/resource.h>
19 #include <sys/rman.h>
20 
21 #include <machine/bus.h>
22 
23 #include <dev/fdt/simplebus.h>
24 #include <dev/ofw/ofw_bus.h>
25 #include <dev/ofw/ofw_bus_subr.h>
26 
27 #include <dev/clk/clk.h>
28 #include <dev/clk/starfive/jh7110_clk.h>
29 #include <dev/hwreset/hwreset.h>
30 
31 #include <dt-bindings/clock/starfive,jh7110-crg.h>
32 
33 #include "clkdev_if.h"
34 #include "hwreset_if.h"
35 
36 static struct ofw_compat_data compat_data[] = {
37 	{ "starfive,jh7110-syscrg",	1 },
38 	{ NULL,				0 }
39 };
40 
41 static struct resource_spec res_spec[] = {
42 	{ SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE },
43 	RESOURCE_SPEC_END
44 };
45 
46 /* parents for non-pll SYS clocks */
47 static const char *cpu_root_p[] = { "osc", "pll0_out" };
48 static const char *cpu_core_p[] = { "cpu_root" };
49 static const char *cpu_bus_p[] = { "cpu_core" };
50 static const char *perh_root_p[] = { "pll0_out", "pll2_out" };
51 static const char *bus_root_p[] = { "osc", "pll2_out" };
52 
53 static const char *apb_bus_p[] = { "stg_axiahb" };
54 static const char *apb0_p[] = { "apb_bus" };
55 static const char *u0_sys_iomux_apb_p[] = { "apb_bus" };
56 static const char *stg_axiahb_p[] = { "axi_cfg0" };
57 static const char *ahb0_p[] = { "stg_axiahb" };
58 static const char *axi_cfg0_p[] = { "bus_root" };
59 
60 static const char *u0_dw_uart_clk_apb_p[] = { "apb0" };
61 static const char *u0_dw_uart_clk_core_p[] = { "osc" };
62 static const char *u0_dw_sdio_clk_ahb_p[] = { "ahb0" };
63 static const char *u0_dw_sdio_clk_sdcard_p[] = { "axi_cfg0" };
64 static const char *u1_dw_uart_clk_apb_p[] = { "apb0" };
65 static const char *u1_dw_uart_clk_core_p[] = { "osc" };
66 static const char *u1_dw_sdio_clk_ahb_p[] = { "ahb0" };
67 static const char *u1_dw_sdio_clk_sdcard_p[] = { "axi_cfg0" };
68 static const char *u2_dw_uart_clk_apb_p[] = { "apb0" };
69 static const char *u2_dw_uart_clk_core_p[] = { "osc" };
70 static const char *u3_dw_uart_clk_apb_p[] = { "apb0" };
71 static const char *u3_dw_uart_clk_core_p[] = { "perh_root" };
72 
73 static const char *gmac_src_p[] = { "pll0_out" };
74 static const char *gmac_phy_p[] = { "gmac_src" };
75 static const char *gmac0_gtxclk_p[] = { "pll0_out" };
76 static const char *gmac0_ptp_p[] = { "gmac_src" };
77 static const char *gmac0_gtxc_p[] = { "gmac0_gtxclk" };
78 static const char *gmac1_gtxclk_p[] = { "pll0_out" };
79 static const char *gmac1_gtxc_p[] = { "gmac1_gtxclk" };
80 static const char *gmac1_rmii_rtx_p[] = { "gmac1_rmii_refin" };
81 static const char *gmac1_axi_p[] = { "stg_axiahb" };
82 static const char *gmac1_ahb_p[] = { "ahb0" };
83 static const char *gmac1_ptp_p[] = { "gmac_src" };
84 static const char *gmac1_tx_inv_p[] = { "gmac1_tx" };
85 static const char *gmac1_tx_p[] = { "gmac1_gtxclk", "gmac1_rmii_rtx" };
86 static const char *gmac1_rx_p[] = { "gmac1_rgmii_rxin", "gmac1_rmii_rtx" };
87 static const char *gmac1_rx_inv_p[] = { "gmac1_rx" };
88 
89 /* non-pll SYS clocks */
90 static const struct jh7110_clk_def sys_clks[] = {
91 	JH7110_MUX(JH7110_SYSCLK_CPU_ROOT, "cpu_root", cpu_root_p),
92 	JH7110_DIV(JH7110_SYSCLK_CPU_CORE, "cpu_core", cpu_core_p, 7),
93 	JH7110_DIV(JH7110_SYSCLK_CPU_BUS, "cpu_bus", cpu_bus_p, 2),
94 	JH7110_GATEDIV(JH7110_SYSCLK_PERH_ROOT, "perh_root", perh_root_p, 2),
95 	JH7110_MUX(JH7110_SYSCLK_BUS_ROOT, "bus_root", bus_root_p),
96 
97 	JH7110_GATE(JH7110_SYSCLK_APB0, "apb0", apb0_p),
98 	JH7110_GATE(JH7110_SYSCLK_IOMUX_APB, "u0_sys_iomux_apb",
99 	    u0_sys_iomux_apb_p),
100 	JH7110_GATE(JH7110_SYSCLK_UART0_APB, "u0_dw_uart_clk_apb",
101 	    u0_dw_uart_clk_apb_p),
102 	JH7110_GATE(JH7110_SYSCLK_UART0_CORE, "u0_dw_uart_clk_core",
103 	    u0_dw_uart_clk_core_p),
104 	JH7110_GATE(JH7110_SYSCLK_UART1_APB, "u1_dw_uart_clk_apb",
105 	    u1_dw_uart_clk_apb_p),
106 	JH7110_GATE(JH7110_SYSCLK_UART1_CORE, "u1_dw_uart_clk_core",
107 	    u1_dw_uart_clk_core_p),
108 	JH7110_GATE(JH7110_SYSCLK_UART2_APB, "u2_dw_uart_clk_apb",
109 	    u2_dw_uart_clk_apb_p),
110 	JH7110_GATE(JH7110_SYSCLK_UART2_CORE, "u2_dw_uart_clk_core",
111 	    u2_dw_uart_clk_core_p),
112 	JH7110_GATE(JH7110_SYSCLK_UART3_APB, "u3_dw_uart_clk_apb",
113 	    u3_dw_uart_clk_apb_p),
114 	JH7110_GATE(JH7110_SYSCLK_UART3_CORE, "u3_dw_uart_clk_core",
115 	    u3_dw_uart_clk_core_p),
116 
117 	JH7110_DIV(JH7110_SYSCLK_AXI_CFG0, "axi_cfg0", axi_cfg0_p, 3),
118 	JH7110_DIV(JH7110_SYSCLK_STG_AXIAHB, "stg_axiahb", stg_axiahb_p, 2),
119 	JH7110_GATE(JH7110_SYSCLK_AHB0, "ahb0", ahb0_p),
120 	JH7110_DIV(JH7110_SYSCLK_APB_BUS, "apb_bus", apb_bus_p, 8),
121 
122 	JH7110_GATE(JH7110_SYSCLK_SDIO0_AHB, "u0_dw_sdio_clk_ahb",
123 	    u0_dw_sdio_clk_ahb_p),
124 	JH7110_GATE(JH7110_SYSCLK_SDIO1_AHB, "u1_dw_sdio_clk_ahb",
125 	    u1_dw_sdio_clk_ahb_p),
126 	JH7110_GATEDIV(JH7110_SYSCLK_SDIO0_SDCARD, "u0_dw_sdio_clk_sdcard",
127 	    u0_dw_sdio_clk_sdcard_p, 15),
128 	JH7110_GATEDIV(JH7110_SYSCLK_SDIO1_SDCARD, "u1_dw_sdio_clk_sdcard",
129 	    u1_dw_sdio_clk_sdcard_p, 15),
130 
131 	JH7110_DIV(JH7110_SYSCLK_GMAC_SRC, "gmac_src", gmac_src_p, 7),
132 	JH7110_GATEDIV(JH7110_SYSCLK_GMAC0_GTXCLK, "gmac0_gtxclk",
133 	    gmac0_gtxclk_p, 15),
134 	JH7110_GATEDIV(JH7110_SYSCLK_GMAC0_PTP, "gmac0_ptp", gmac0_ptp_p, 31),
135 	JH7110_GATEDIV(JH7110_SYSCLK_GMAC_PHY, "gmac_phy", gmac_phy_p, 31),
136 	JH7110_GATE(JH7110_SYSCLK_GMAC0_GTXC, "gmac0_gtxc", gmac0_gtxc_p),
137 
138 	JH7110_MUX(JH7110_SYSCLK_GMAC1_RX, "gmac1_rx", gmac1_rx_p),
139 	JH7110_INV(JH7110_SYSCLK_GMAC1_RX_INV, "gmac1_rx_inv", gmac1_rx_inv_p),
140 	JH7110_GATE(JH7110_SYSCLK_GMAC1_AHB, "gmac1_ahb", gmac1_ahb_p),
141 	JH7110_DIV(JH7110_SYSCLK_GMAC1_GTXCLK, "gmac1_gtxclk",
142 	    gmac1_gtxclk_p, 15),
143 	JH7110_GATEMUX(JH7110_SYSCLK_GMAC1_TX, "gmac1_tx", gmac1_tx_p),
144 	JH7110_INV(JH7110_SYSCLK_GMAC1_TX_INV, "gmac1_tx_inv", gmac1_tx_inv_p),
145 	JH7110_GATEDIV(JH7110_SYSCLK_GMAC1_PTP, "gmac1_ptp", gmac1_ptp_p, 31),
146 	JH7110_GATE(JH7110_SYSCLK_GMAC1_AXI, "gmac1_axi", gmac1_axi_p),
147 	JH7110_GATE(JH7110_SYSCLK_GMAC1_GTXC, "gmac1_gtxc", gmac1_gtxc_p),
148 	JH7110_DIV(JH7110_SYSCLK_GMAC1_RMII_RTX, "gmac1_rmii_rtx",
149 	    gmac1_rmii_rtx_p, 30),
150 };
151 
152 static int
153 jh7110_clk_sys_probe(device_t dev)
154 {
155 	if (!ofw_bus_status_okay(dev))
156 		return (ENXIO);
157 
158 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
159 		return (ENXIO);
160 
161 	device_set_desc(dev, "StarFive JH7110 SYS clock generator");
162 
163 	return (BUS_PROBE_DEFAULT);
164 }
165 
166 static int
167 jh7110_clk_sys_attach(device_t dev)
168 {
169 	struct jh7110_clkgen_softc *sc;
170 	int i, error;
171 
172 	sc = device_get_softc(dev);
173 
174 	sc->reset_status_offset = SYSCRG_RESET_STATUS;
175 	sc->reset_selector_offset = SYSCRG_RESET_SELECTOR;
176 
177 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
178 
179 	/* Allocate memory groups */
180 	error = bus_alloc_resources(dev, res_spec, &sc->mem_res);
181 	if (error != 0) {
182 		device_printf(dev, "Couldn't allocate resources, error %d\n",
183 		    error);
184 		return (ENXIO);
185 	}
186 
187 	/* Create clock domain */
188 	sc->clkdom = clkdom_create(dev);
189 	if (sc->clkdom == NULL) {
190 		device_printf(dev, "Couldn't create clkdom\n");
191 		return (ENXIO);
192 	}
193 
194 	/* Register clocks */
195 	for (i = 0; i < nitems(sys_clks); i++) {
196 		error = jh7110_clk_register(sc->clkdom, &sys_clks[i]);
197 		if (error != 0) {
198 			device_printf(dev, "Couldn't register clock %s: %d\n",
199 			    sys_clks[i].clkdef.name, error);
200 			return (ENXIO);
201 		}
202 	}
203 
204 	if (clkdom_finit(sc->clkdom) != 0)
205 		panic("Cannot finalize clkdom initialization\n");
206 
207 	if (bootverbose)
208 		clkdom_dump(sc->clkdom);
209 
210 	hwreset_register_ofw_provider(dev);
211 
212 	return (0);
213 }
214 
215 static int
216 jh7110_clk_sys_detach(device_t dev)
217 {
218 	/* Detach not supported */
219 	return (EBUSY);
220 }
221 
222 static void
223 jh7110_clk_sys_device_lock(device_t dev)
224 {
225 	struct jh7110_clkgen_softc *sc;
226 
227 	sc = device_get_softc(dev);
228 	mtx_lock(&sc->mtx);
229 }
230 
231 static void
232 jh7110_clk_sys_device_unlock(device_t dev)
233 {
234 	struct jh7110_clkgen_softc *sc;
235 
236 	sc = device_get_softc(dev);
237 	mtx_unlock(&sc->mtx);
238 }
239 
240 static device_method_t jh7110_clk_sys_methods[] = {
241 	/* Device interface */
242 	DEVMETHOD(device_probe,		jh7110_clk_sys_probe),
243 	DEVMETHOD(device_attach,	jh7110_clk_sys_attach),
244 	DEVMETHOD(device_detach,	jh7110_clk_sys_detach),
245 
246 	/* clkdev interface */
247 	DEVMETHOD(clkdev_device_lock,	jh7110_clk_sys_device_lock),
248 	DEVMETHOD(clkdev_device_unlock,	jh7110_clk_sys_device_unlock),
249 
250 	/* Reset interface */
251 	DEVMETHOD(hwreset_assert,	jh7110_reset_assert),
252 	DEVMETHOD(hwreset_is_asserted,	jh7110_reset_is_asserted),
253 
254 	DEVMETHOD_END
255 };
256 
257 DEFINE_CLASS_0(jh7110_clk_sys, jh7110_clk_sys_driver, jh7110_clk_sys_methods,
258     sizeof(struct jh7110_clkgen_softc));
259 EARLY_DRIVER_MODULE(jh7110_clk_sys, simplebus, jh7110_clk_sys_driver, 0, 0,
260     BUS_PASS_BUS + BUS_PASS_ORDER_LATE);
261 MODULE_VERSION(jh7110_clk_sys, 1);
262