xref: /netbsd-src/sys/arch/riscv/sifive/fu540_prci.c (revision 644d8f85eb5b192f82c18bd555b19090fa48928d)
1*644d8f85Sjmcneill /* $NetBSD: fu540_prci.c,v 1.1 2022/11/25 12:35:44 jmcneill Exp $ */
2*644d8f85Sjmcneill 
3*644d8f85Sjmcneill /*-
4*644d8f85Sjmcneill  * Copyright (c) 2022 Jared McNeill <jmcneill@invisible.ca>
5*644d8f85Sjmcneill  * All rights reserved.
6*644d8f85Sjmcneill  *
7*644d8f85Sjmcneill  * Redistribution and use in source and binary forms, with or without
8*644d8f85Sjmcneill  * modification, are permitted provided that the following conditions
9*644d8f85Sjmcneill  * are met:
10*644d8f85Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11*644d8f85Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12*644d8f85Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13*644d8f85Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14*644d8f85Sjmcneill  *    documentation and/or other materials provided with the distribution.
15*644d8f85Sjmcneill  *
16*644d8f85Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*644d8f85Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*644d8f85Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*644d8f85Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*644d8f85Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*644d8f85Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*644d8f85Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*644d8f85Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*644d8f85Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*644d8f85Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*644d8f85Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
27*644d8f85Sjmcneill  */
28*644d8f85Sjmcneill 
29*644d8f85Sjmcneill #include <sys/cdefs.h>
30*644d8f85Sjmcneill 
31*644d8f85Sjmcneill __KERNEL_RCSID(0, "$NetBSD: fu540_prci.c,v 1.1 2022/11/25 12:35:44 jmcneill Exp $");
32*644d8f85Sjmcneill 
33*644d8f85Sjmcneill #include <sys/param.h>
34*644d8f85Sjmcneill #include <sys/bus.h>
35*644d8f85Sjmcneill #include <sys/device.h>
36*644d8f85Sjmcneill #include <sys/intr.h>
37*644d8f85Sjmcneill #include <sys/systm.h>
38*644d8f85Sjmcneill #include <sys/time.h>
39*644d8f85Sjmcneill #include <sys/kmem.h>
40*644d8f85Sjmcneill 
41*644d8f85Sjmcneill #include <dev/clk/clk_backend.h>
42*644d8f85Sjmcneill 
43*644d8f85Sjmcneill #include <dev/fdt/fdtvar.h>
44*644d8f85Sjmcneill 
45*644d8f85Sjmcneill #define	COREPLLCFG0	0x04
46*644d8f85Sjmcneill #define	DDRPLLCFG0	0x0c
47*644d8f85Sjmcneill #define	DDRPLLCFG1	0x10
48*644d8f85Sjmcneill #define	GEMGXLPLLCFG0	0x1c
49*644d8f85Sjmcneill #define	GEMGXLPLLCFG1	0x20
50*644d8f85Sjmcneill #define	 PLL0_DIVQ	__BITS(17,15)
51*644d8f85Sjmcneill #define	 PLL0_DIVF	__BITS(14,6)
52*644d8f85Sjmcneill #define	 PLL0_DIVR	__BITS(5,0)
53*644d8f85Sjmcneill #define	 PLL1_CKE	__BIT(24)
54*644d8f85Sjmcneill #define	CORECLKSEL	0x24
55*644d8f85Sjmcneill 
56*644d8f85Sjmcneill enum fu540_clkid {
57*644d8f85Sjmcneill 	clkid_corepll,
58*644d8f85Sjmcneill 	clkid_ddrpll,
59*644d8f85Sjmcneill 	clkid_gemgxlpll,
60*644d8f85Sjmcneill 	clkid_tlclk,
61*644d8f85Sjmcneill 	num_clkid
62*644d8f85Sjmcneill };
63*644d8f85Sjmcneill CTASSERT(num_clkid == 4);
64*644d8f85Sjmcneill 
65*644d8f85Sjmcneill static int fu540_prci_match(device_t, cfdata_t, void *);
66*644d8f85Sjmcneill static void fu540_prci_attach(device_t, device_t, void *);
67*644d8f85Sjmcneill 
68*644d8f85Sjmcneill static u_int fu540_prci_clk_get_rate(void *, struct clk *);
69*644d8f85Sjmcneill 
70*644d8f85Sjmcneill static const struct device_compatible_entry compat_data[] = {
71*644d8f85Sjmcneill 	{ .compat = "sifive,fu540-c000-prci" },
72*644d8f85Sjmcneill 	DEVICE_COMPAT_EOL
73*644d8f85Sjmcneill };
74*644d8f85Sjmcneill 
75*644d8f85Sjmcneill struct fu540_prci_softc {
76*644d8f85Sjmcneill 	device_t		sc_dev;
77*644d8f85Sjmcneill 	bus_space_tag_t		sc_bst;
78*644d8f85Sjmcneill 	bus_space_handle_t	sc_bsh;
79*644d8f85Sjmcneill 	struct clk_domain	sc_clkdom;
80*644d8f85Sjmcneill 	struct clk		sc_clk[num_clkid];
81*644d8f85Sjmcneill 
82*644d8f85Sjmcneill 	u_int			sc_hfclk;
83*644d8f85Sjmcneill 	u_int			sc_rtcclk;
84*644d8f85Sjmcneill };
85*644d8f85Sjmcneill 
86*644d8f85Sjmcneill #define	RD4(sc, reg)		\
87*644d8f85Sjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
88*644d8f85Sjmcneill #define	WR4(sc, reg, val)	\
89*644d8f85Sjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
90*644d8f85Sjmcneill 
91*644d8f85Sjmcneill CFATTACH_DECL_NEW(fu540_prci, sizeof(struct fu540_prci_softc),
92*644d8f85Sjmcneill 	fu540_prci_match, fu540_prci_attach, NULL, NULL);
93*644d8f85Sjmcneill 
94*644d8f85Sjmcneill static struct clk *
fu540_prci_clk_get(void * priv,const char * name)95*644d8f85Sjmcneill fu540_prci_clk_get(void *priv, const char *name)
96*644d8f85Sjmcneill {
97*644d8f85Sjmcneill 	struct fu540_prci_softc * const sc = priv;
98*644d8f85Sjmcneill 	u_int clkid;
99*644d8f85Sjmcneill 
100*644d8f85Sjmcneill 	for (clkid = 0; clkid < num_clkid; clkid++) {
101*644d8f85Sjmcneill 		if (strcmp(name, sc->sc_clk[clkid].name) == 0) {
102*644d8f85Sjmcneill 			return &sc->sc_clk[clkid];
103*644d8f85Sjmcneill 		}
104*644d8f85Sjmcneill 	}
105*644d8f85Sjmcneill 
106*644d8f85Sjmcneill 	return NULL;
107*644d8f85Sjmcneill }
108*644d8f85Sjmcneill 
109*644d8f85Sjmcneill static void
fu540_prci_clk_put(void * priv,struct clk * clk)110*644d8f85Sjmcneill fu540_prci_clk_put(void *priv, struct clk *clk)
111*644d8f85Sjmcneill {
112*644d8f85Sjmcneill }
113*644d8f85Sjmcneill 
114*644d8f85Sjmcneill static u_int
fu540_prci_clk_get_rate_pll(struct fu540_prci_softc * sc,u_int reg)115*644d8f85Sjmcneill fu540_prci_clk_get_rate_pll(struct fu540_prci_softc *sc, u_int reg)
116*644d8f85Sjmcneill {
117*644d8f85Sjmcneill 	uint32_t val;
118*644d8f85Sjmcneill 	u_int rate, divr, divf, divq;
119*644d8f85Sjmcneill 
120*644d8f85Sjmcneill 	val = RD4(sc, reg);
121*644d8f85Sjmcneill 	divr = __SHIFTOUT(val, PLL0_DIVR) + 1;
122*644d8f85Sjmcneill 	divf = __SHIFTOUT(val, PLL0_DIVF) + 1;
123*644d8f85Sjmcneill 	divq = __SHIFTOUT(val, PLL0_DIVQ);
124*644d8f85Sjmcneill 	rate = sc->sc_hfclk * divr * divf;
125*644d8f85Sjmcneill 	rate <<= divq;
126*644d8f85Sjmcneill 
127*644d8f85Sjmcneill 	return rate;
128*644d8f85Sjmcneill }
129*644d8f85Sjmcneill 
130*644d8f85Sjmcneill static u_int
fu540_prci_clk_get_rate(void * priv,struct clk * clk)131*644d8f85Sjmcneill fu540_prci_clk_get_rate(void *priv, struct clk *clk)
132*644d8f85Sjmcneill {
133*644d8f85Sjmcneill 	struct fu540_prci_softc * const sc = priv;
134*644d8f85Sjmcneill 	u_int rate;
135*644d8f85Sjmcneill 
136*644d8f85Sjmcneill 	if (clk == &sc->sc_clk[clkid_corepll] ||
137*644d8f85Sjmcneill 	    clk == &sc->sc_clk[clkid_tlclk]) {
138*644d8f85Sjmcneill 		rate = fu540_prci_clk_get_rate_pll(sc, COREPLLCFG0);
139*644d8f85Sjmcneill 		if (clk == &sc->sc_clk[clkid_tlclk]) {
140*644d8f85Sjmcneill 			rate /= 2;
141*644d8f85Sjmcneill 		}
142*644d8f85Sjmcneill 		return rate;
143*644d8f85Sjmcneill 	} else if (clk == &sc->sc_clk[clkid_ddrpll]) {
144*644d8f85Sjmcneill 		return fu540_prci_clk_get_rate_pll(sc, DDRPLLCFG0);
145*644d8f85Sjmcneill 	} else if (clk == &sc->sc_clk[clkid_gemgxlpll]) {
146*644d8f85Sjmcneill 		return fu540_prci_clk_get_rate_pll(sc, GEMGXLPLLCFG0);
147*644d8f85Sjmcneill 	} else {
148*644d8f85Sjmcneill 		/* Not implemented. */
149*644d8f85Sjmcneill 		return 0;
150*644d8f85Sjmcneill 	}
151*644d8f85Sjmcneill }
152*644d8f85Sjmcneill 
153*644d8f85Sjmcneill static int
fu540_prci_clk_enable(void * priv,struct clk * clk)154*644d8f85Sjmcneill fu540_prci_clk_enable(void *priv, struct clk *clk)
155*644d8f85Sjmcneill {
156*644d8f85Sjmcneill 	struct fu540_prci_softc * const sc = priv;
157*644d8f85Sjmcneill 	uint32_t val;
158*644d8f85Sjmcneill 
159*644d8f85Sjmcneill 	if (clk == &sc->sc_clk[clkid_corepll] ||
160*644d8f85Sjmcneill 	    clk == &sc->sc_clk[clkid_tlclk]) {
161*644d8f85Sjmcneill 		/* Always enabled. */
162*644d8f85Sjmcneill 		return 0;
163*644d8f85Sjmcneill 	} else if (clk == &sc->sc_clk[clkid_ddrpll]) {
164*644d8f85Sjmcneill 		val = RD4(sc, DDRPLLCFG1);
165*644d8f85Sjmcneill 		WR4(sc, DDRPLLCFG1, val | PLL1_CKE);
166*644d8f85Sjmcneill 		return 0;
167*644d8f85Sjmcneill 	} else if (clk == &sc->sc_clk[clkid_gemgxlpll]) {
168*644d8f85Sjmcneill 		val = RD4(sc, GEMGXLPLLCFG1);
169*644d8f85Sjmcneill 		WR4(sc, GEMGXLPLLCFG1, val | PLL1_CKE);
170*644d8f85Sjmcneill 		return 0;
171*644d8f85Sjmcneill 	} else {
172*644d8f85Sjmcneill 		/* Not implemented. */
173*644d8f85Sjmcneill 		return ENXIO;
174*644d8f85Sjmcneill 	}
175*644d8f85Sjmcneill }
176*644d8f85Sjmcneill 
177*644d8f85Sjmcneill static int
fu540_prci_clk_disable(void * priv,struct clk * clk)178*644d8f85Sjmcneill fu540_prci_clk_disable(void *priv, struct clk *clk)
179*644d8f85Sjmcneill {
180*644d8f85Sjmcneill 	return ENXIO;
181*644d8f85Sjmcneill }
182*644d8f85Sjmcneill 
183*644d8f85Sjmcneill static const struct clk_funcs fu540_prci_clk_funcs = {
184*644d8f85Sjmcneill 	.get = fu540_prci_clk_get,
185*644d8f85Sjmcneill 	.put = fu540_prci_clk_put,
186*644d8f85Sjmcneill 	.get_rate = fu540_prci_clk_get_rate,
187*644d8f85Sjmcneill 	.enable = fu540_prci_clk_enable,
188*644d8f85Sjmcneill 	.disable = fu540_prci_clk_disable,
189*644d8f85Sjmcneill };
190*644d8f85Sjmcneill 
191*644d8f85Sjmcneill static struct clk *
fu540_prci_fdt_decode(device_t dev,int cc_phandle,const void * data,size_t len)192*644d8f85Sjmcneill fu540_prci_fdt_decode(device_t dev, int cc_phandle, const void *data, size_t len)
193*644d8f85Sjmcneill {
194*644d8f85Sjmcneill 	struct fu540_prci_softc * const sc = device_private(dev);
195*644d8f85Sjmcneill 	u_int clkid;
196*644d8f85Sjmcneill 
197*644d8f85Sjmcneill 	if (len != 4) {
198*644d8f85Sjmcneill 		return NULL;
199*644d8f85Sjmcneill 	}
200*644d8f85Sjmcneill 
201*644d8f85Sjmcneill 	clkid = be32dec(data);
202*644d8f85Sjmcneill 	if (clkid >= num_clkid) {
203*644d8f85Sjmcneill 		return NULL;
204*644d8f85Sjmcneill 	}
205*644d8f85Sjmcneill 
206*644d8f85Sjmcneill 	return &sc->sc_clk[clkid];
207*644d8f85Sjmcneill }
208*644d8f85Sjmcneill 
209*644d8f85Sjmcneill static const struct fdtbus_clock_controller_func fu540_prci_fdt_funcs = {
210*644d8f85Sjmcneill 	.decode = fu540_prci_fdt_decode
211*644d8f85Sjmcneill };
212*644d8f85Sjmcneill 
213*644d8f85Sjmcneill static int
fu540_prci_match(device_t parent,cfdata_t cf,void * aux)214*644d8f85Sjmcneill fu540_prci_match(device_t parent, cfdata_t cf, void *aux)
215*644d8f85Sjmcneill {
216*644d8f85Sjmcneill 	struct fdt_attach_args * const faa = aux;
217*644d8f85Sjmcneill 
218*644d8f85Sjmcneill 	return of_compatible_match(faa->faa_phandle, compat_data);
219*644d8f85Sjmcneill }
220*644d8f85Sjmcneill 
221*644d8f85Sjmcneill static void
fu540_prci_attach(device_t parent,device_t self,void * aux)222*644d8f85Sjmcneill fu540_prci_attach(device_t parent, device_t self, void *aux)
223*644d8f85Sjmcneill {
224*644d8f85Sjmcneill 	struct fu540_prci_softc * const sc = device_private(self);
225*644d8f85Sjmcneill 	struct fdt_attach_args * const faa = aux;
226*644d8f85Sjmcneill 	const int phandle = faa->faa_phandle;
227*644d8f85Sjmcneill 	const char *clkname;
228*644d8f85Sjmcneill 	struct clk *clk;
229*644d8f85Sjmcneill 	bus_addr_t addr;
230*644d8f85Sjmcneill 	bus_size_t size;
231*644d8f85Sjmcneill 	u_int clkid;
232*644d8f85Sjmcneill 
233*644d8f85Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
234*644d8f85Sjmcneill 		aprint_error(": couldn't get registers\n");
235*644d8f85Sjmcneill 		return;
236*644d8f85Sjmcneill 	}
237*644d8f85Sjmcneill 
238*644d8f85Sjmcneill 	sc->sc_dev = self;
239*644d8f85Sjmcneill 	sc->sc_bst = faa->faa_bst;
240*644d8f85Sjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
241*644d8f85Sjmcneill 		aprint_error(": couldn't map registers\n");
242*644d8f85Sjmcneill 		return;
243*644d8f85Sjmcneill 	}
244*644d8f85Sjmcneill 
245*644d8f85Sjmcneill 	clk = fdtbus_clock_get(phandle, "hfclk");
246*644d8f85Sjmcneill 	if (clk == NULL) {
247*644d8f85Sjmcneill 		aprint_error(": couldn't get hfclk\n");
248*644d8f85Sjmcneill 		return;
249*644d8f85Sjmcneill 	}
250*644d8f85Sjmcneill 	sc->sc_hfclk = clk_get_rate(clk);
251*644d8f85Sjmcneill 
252*644d8f85Sjmcneill 	clk = fdtbus_clock_get(phandle, "rtcclk");
253*644d8f85Sjmcneill 	if (clk == NULL) {
254*644d8f85Sjmcneill 		aprint_error(": couldn't get rtcclk\n");
255*644d8f85Sjmcneill 		return;
256*644d8f85Sjmcneill 	}
257*644d8f85Sjmcneill 	sc->sc_rtcclk = clk_get_rate(clk);
258*644d8f85Sjmcneill 
259*644d8f85Sjmcneill 	sc->sc_clkdom.name = device_xname(self);
260*644d8f85Sjmcneill 	sc->sc_clkdom.funcs = &fu540_prci_clk_funcs;
261*644d8f85Sjmcneill 	sc->sc_clkdom.priv = sc;
262*644d8f85Sjmcneill 	for (clkid = 0; clkid < num_clkid; clkid++) {
263*644d8f85Sjmcneill 		clkname = fdtbus_get_string_index(phandle,
264*644d8f85Sjmcneill 		    "clock-output-names", clkid);
265*644d8f85Sjmcneill 		sc->sc_clk[clkid].domain = &sc->sc_clkdom;
266*644d8f85Sjmcneill 		if (clkname != NULL) {
267*644d8f85Sjmcneill 			sc->sc_clk[clkid].name = kmem_asprintf("%s", clkname);
268*644d8f85Sjmcneill 		}
269*644d8f85Sjmcneill 		clk_attach(&sc->sc_clk[clkid]);
270*644d8f85Sjmcneill 	}
271*644d8f85Sjmcneill 
272*644d8f85Sjmcneill 	aprint_naive("\n");
273*644d8f85Sjmcneill 	aprint_normal(": FU540 PRCI (HF %u Hz, RTC %u Hz)\n",
274*644d8f85Sjmcneill 	    sc->sc_hfclk, sc->sc_rtcclk);
275*644d8f85Sjmcneill 
276*644d8f85Sjmcneill 	for (clkid = 0; clkid < num_clkid; clkid++) {
277*644d8f85Sjmcneill 		aprint_debug_dev(self, "clkid %u [%s]: %u Hz\n", clkid,
278*644d8f85Sjmcneill 		    sc->sc_clk[clkid].name ? sc->sc_clk[clkid].name : "<none>",
279*644d8f85Sjmcneill 		    clk_get_rate(&sc->sc_clk[clkid]));
280*644d8f85Sjmcneill 	}
281*644d8f85Sjmcneill 
282*644d8f85Sjmcneill 	fdtbus_register_clock_controller(self, phandle, &fu540_prci_fdt_funcs);
283*644d8f85Sjmcneill }
284