xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_gates.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: sunxi_gates.c,v 1.4 2021/01/27 03:10:20 thorpej Exp $ */
27283846bSjmcneill 
37283846bSjmcneill /*-
47283846bSjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
57283846bSjmcneill  * All rights reserved.
67283846bSjmcneill  *
77283846bSjmcneill  * Redistribution and use in source and binary forms, with or without
87283846bSjmcneill  * modification, are permitted provided that the following conditions
97283846bSjmcneill  * are met:
107283846bSjmcneill  * 1. Redistributions of source code must retain the above copyright
117283846bSjmcneill  *    notice, this list of conditions and the following disclaimer.
127283846bSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
137283846bSjmcneill  *    notice, this list of conditions and the following disclaimer in the
147283846bSjmcneill  *    documentation and/or other materials provided with the distribution.
157283846bSjmcneill  *
167283846bSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177283846bSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187283846bSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197283846bSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
207283846bSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
217283846bSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
227283846bSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
237283846bSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
247283846bSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257283846bSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267283846bSjmcneill  * SUCH DAMAGE.
277283846bSjmcneill  */
287283846bSjmcneill 
297283846bSjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_gates.c,v 1.4 2021/01/27 03:10:20 thorpej Exp $");
317283846bSjmcneill 
327283846bSjmcneill #include <sys/param.h>
337283846bSjmcneill #include <sys/bus.h>
347283846bSjmcneill #include <sys/cpu.h>
357283846bSjmcneill #include <sys/device.h>
367283846bSjmcneill #include <sys/kmem.h>
377283846bSjmcneill 
387283846bSjmcneill #include <dev/fdt/fdtvar.h>
397283846bSjmcneill 
407283846bSjmcneill #include <dev/clk/clk_backend.h>
417283846bSjmcneill 
427283846bSjmcneill #define	GATE_REG(index)		(((index) / 32) * 4)
437283846bSjmcneill #define	GATE_MASK(index)	__BIT((index) % 32)
447283846bSjmcneill 
45*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
46*6e54367aSthorpej 	{ .compat = "allwinner,sun4i-a10-gates-clk" },
47*6e54367aSthorpej 	{ .compat = "allwinner,sun9i-a80-apbs-gates-clk" },
48*6e54367aSthorpej 	DEVICE_COMPAT_EOL
497283846bSjmcneill };
507283846bSjmcneill 
517283846bSjmcneill struct sunxi_gate {
527283846bSjmcneill 	struct clk		base;
537283846bSjmcneill 	u_int			index;
547283846bSjmcneill 
557283846bSjmcneill 	TAILQ_ENTRY(sunxi_gate)	gates;
567283846bSjmcneill };
577283846bSjmcneill 
587283846bSjmcneill struct sunxi_gates_softc {
597283846bSjmcneill 	device_t		sc_dev;
607283846bSjmcneill 	bus_space_tag_t		sc_bst;
617283846bSjmcneill 	bus_space_handle_t	sc_bsh;
627283846bSjmcneill 	int			sc_phandle;
637283846bSjmcneill 
647283846bSjmcneill 	struct clk_domain	sc_clkdom;
657283846bSjmcneill 	TAILQ_HEAD(, sunxi_gate) sc_gates;
667283846bSjmcneill };
677283846bSjmcneill 
687283846bSjmcneill #define	GATE_READ(sc, reg)		\
697283846bSjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
707283846bSjmcneill #define	GATE_WRITE(sc, reg, val)	\
717283846bSjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
727283846bSjmcneill 
737283846bSjmcneill static struct clk *
sunxi_gates_clock_decode(device_t dev,int cc_phandle,const void * data,size_t len)7451b425efSaymeric sunxi_gates_clock_decode(device_t dev, int cc_phandle, const void *data,
7551b425efSaymeric 			 size_t len)
767283846bSjmcneill {
777283846bSjmcneill 	struct sunxi_gates_softc * const sc = device_private(dev);
787283846bSjmcneill 	struct sunxi_gate *gate;
797283846bSjmcneill 
807283846bSjmcneill 	if (len != 4)
817283846bSjmcneill 		return NULL;
827283846bSjmcneill 
837283846bSjmcneill 	const u_int index = be32dec(data);
847283846bSjmcneill 
857283846bSjmcneill 	TAILQ_FOREACH(gate, &sc->sc_gates, gates)
867283846bSjmcneill 		if (gate->index == index)
877283846bSjmcneill 			return &gate->base;
887283846bSjmcneill 
897283846bSjmcneill 	return NULL;
907283846bSjmcneill }
917283846bSjmcneill 
927283846bSjmcneill static const struct fdtbus_clock_controller_func sunxi_gates_fdtclock_funcs = {
937283846bSjmcneill 	.decode = sunxi_gates_clock_decode,
947283846bSjmcneill };
957283846bSjmcneill 
967283846bSjmcneill static struct clk *
sunxi_gates_clock_get(void * priv,const char * name)977283846bSjmcneill sunxi_gates_clock_get(void *priv, const char *name)
987283846bSjmcneill {
997283846bSjmcneill 	struct sunxi_gates_softc * const sc = priv;
1007283846bSjmcneill 	struct sunxi_gate *gate;
1017283846bSjmcneill 
1027283846bSjmcneill 	TAILQ_FOREACH(gate, &sc->sc_gates, gates)
1037283846bSjmcneill 		if (strcmp(gate->base.name, name) == 0)
1047283846bSjmcneill 			return &gate->base;
1057283846bSjmcneill 
1067283846bSjmcneill 	return NULL;
1077283846bSjmcneill }
1087283846bSjmcneill 
1097283846bSjmcneill static void
sunxi_gates_clock_put(void * priv,struct clk * clk)1107283846bSjmcneill sunxi_gates_clock_put(void *priv, struct clk *clk)
1117283846bSjmcneill {
1127283846bSjmcneill }
1137283846bSjmcneill 
1147283846bSjmcneill static u_int
sunxi_gates_clock_get_rate(void * priv,struct clk * clkp)1157283846bSjmcneill sunxi_gates_clock_get_rate(void *priv, struct clk *clkp)
1167283846bSjmcneill {
1177283846bSjmcneill 	struct sunxi_gates_softc * const sc = priv;
1187283846bSjmcneill 	struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
1197283846bSjmcneill 	struct clk *clkp_parent;
1207283846bSjmcneill 
1217283846bSjmcneill 	clkp_parent = clk_get_parent(clkp);
1227283846bSjmcneill 	if (clkp_parent == NULL)
1237283846bSjmcneill 		return 0;
1247283846bSjmcneill 
1257283846bSjmcneill 	const bus_size_t gate_reg = GATE_REG(gate->index);
1267283846bSjmcneill 	const uint32_t gate_mask = GATE_MASK(gate->index);
1277283846bSjmcneill 
1287283846bSjmcneill 	if ((GATE_READ(sc, gate_reg) & gate_mask) == 0)
1297283846bSjmcneill 		return 0;
1307283846bSjmcneill 
1317283846bSjmcneill 	return clk_get_rate(clkp_parent);
1327283846bSjmcneill }
1337283846bSjmcneill 
1347283846bSjmcneill static int
sunxi_gates_clock_enable(void * priv,struct clk * clkp)1357283846bSjmcneill sunxi_gates_clock_enable(void *priv, struct clk *clkp)
1367283846bSjmcneill {
1377283846bSjmcneill 	struct sunxi_gates_softc * const sc = priv;
1387283846bSjmcneill 	struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
1397283846bSjmcneill 	uint32_t val;
1407283846bSjmcneill 
1417283846bSjmcneill 	const bus_size_t gate_reg = GATE_REG(gate->index);
1427283846bSjmcneill 	const uint32_t gate_mask = GATE_MASK(gate->index);
1437283846bSjmcneill 
1447283846bSjmcneill 	val = GATE_READ(sc, gate_reg);
1457283846bSjmcneill 	val |= gate_mask;
1467283846bSjmcneill 	GATE_WRITE(sc, gate_reg, val);
1477283846bSjmcneill 
1487283846bSjmcneill 	return 0;
1497283846bSjmcneill }
1507283846bSjmcneill 
1517283846bSjmcneill static int
sunxi_gates_clock_disable(void * priv,struct clk * clkp)1527283846bSjmcneill sunxi_gates_clock_disable(void *priv, struct clk *clkp)
1537283846bSjmcneill {
1547283846bSjmcneill 	struct sunxi_gates_softc * const sc = priv;
1557283846bSjmcneill 	struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
1567283846bSjmcneill 	uint32_t val;
1577283846bSjmcneill 
1587283846bSjmcneill 	const bus_size_t gate_reg = GATE_REG(gate->index);
1597283846bSjmcneill 	const uint32_t gate_mask = GATE_MASK(gate->index);
1607283846bSjmcneill 
1617283846bSjmcneill 	val = GATE_READ(sc, gate_reg);
1627283846bSjmcneill 	val &= ~gate_mask;
1637283846bSjmcneill 	GATE_WRITE(sc, gate_reg, val);
1647283846bSjmcneill 
1657283846bSjmcneill 	return 0;
1667283846bSjmcneill }
1677283846bSjmcneill 
1687283846bSjmcneill static struct clk *
sunxi_gates_clock_get_parent(void * priv,struct clk * clkp)1697283846bSjmcneill sunxi_gates_clock_get_parent(void *priv, struct clk *clkp)
1707283846bSjmcneill {
1717283846bSjmcneill 	struct sunxi_gates_softc * const sc = priv;
1727283846bSjmcneill 
1737283846bSjmcneill 	return fdtbus_clock_get_index(sc->sc_phandle, 0);
1747283846bSjmcneill }
1757283846bSjmcneill 
1767283846bSjmcneill static const struct clk_funcs sunxi_gates_clock_funcs = {
1777283846bSjmcneill 	.get = sunxi_gates_clock_get,
1787283846bSjmcneill 	.put = sunxi_gates_clock_put,
1797283846bSjmcneill 	.get_rate = sunxi_gates_clock_get_rate,
1807283846bSjmcneill 	.enable = sunxi_gates_clock_enable,
1817283846bSjmcneill 	.disable = sunxi_gates_clock_disable,
1827283846bSjmcneill 	.get_parent = sunxi_gates_clock_get_parent,
1837283846bSjmcneill };
1847283846bSjmcneill 
1857283846bSjmcneill static void
sunxi_gates_print(struct sunxi_gates_softc * sc)1867283846bSjmcneill sunxi_gates_print(struct sunxi_gates_softc *sc)
1877283846bSjmcneill {
1887283846bSjmcneill 	struct sunxi_gate *gate;
1897283846bSjmcneill 	struct clk *clkp_parent;
1907283846bSjmcneill 
1917283846bSjmcneill 	TAILQ_FOREACH(gate, &sc->sc_gates, gates) {
1927283846bSjmcneill 		clkp_parent = clk_get_parent(&gate->base);
1937283846bSjmcneill 
1947283846bSjmcneill         	aprint_debug_dev(sc->sc_dev,
1957283846bSjmcneill 		    "%3d %-12s %2s %-12s %-7s ",
1967283846bSjmcneill 		    gate->index,
1977283846bSjmcneill         	    gate->base.name,
1987283846bSjmcneill         	    clkp_parent ? "<-" : "",
1997283846bSjmcneill         	    clkp_parent ? clkp_parent->name : "",
2007283846bSjmcneill         	    "gate");
2017283846bSjmcneill 		aprint_debug("%10d Hz\n", clk_get_rate(&gate->base));
2027283846bSjmcneill 	}
2037283846bSjmcneill }
2047283846bSjmcneill 
2057283846bSjmcneill static int
sunxi_gates_match(device_t parent,cfdata_t cf,void * aux)2067283846bSjmcneill sunxi_gates_match(device_t parent, cfdata_t cf, void *aux)
2077283846bSjmcneill {
2087283846bSjmcneill 	struct fdt_attach_args * const faa = aux;
2097283846bSjmcneill 
210*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
2117283846bSjmcneill }
2127283846bSjmcneill 
2137283846bSjmcneill static void
sunxi_gates_attach(device_t parent,device_t self,void * aux)2147283846bSjmcneill sunxi_gates_attach(device_t parent, device_t self, void *aux)
2157283846bSjmcneill {
2167283846bSjmcneill 	struct sunxi_gates_softc * const sc = device_private(self);
2177283846bSjmcneill 	struct fdt_attach_args * const faa = aux;
2187283846bSjmcneill 	const int phandle = faa->faa_phandle;
2197283846bSjmcneill 	struct sunxi_gate *gate;
2207283846bSjmcneill 	const u_int *indices;
2217283846bSjmcneill 	bus_addr_t addr;
2227283846bSjmcneill 	bus_size_t size;
2237283846bSjmcneill 	int len, i;
2247283846bSjmcneill 
2257283846bSjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
2267283846bSjmcneill 		aprint_error(": couldn't get registers\n");
2277283846bSjmcneill 		return;
2287283846bSjmcneill 	}
2297283846bSjmcneill 
2307283846bSjmcneill 	sc->sc_dev = self;
2317283846bSjmcneill 	sc->sc_phandle = phandle;
2327283846bSjmcneill 	sc->sc_bst = faa->faa_bst;
2337283846bSjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
2347283846bSjmcneill 		aprint_error(": couldn't map registers\n");
2357283846bSjmcneill 		return;
2367283846bSjmcneill 	}
2377283846bSjmcneill 	TAILQ_INIT(&sc->sc_gates);
2387283846bSjmcneill 
2397283846bSjmcneill 	aprint_naive("\n");
2407283846bSjmcneill 	aprint_normal("\n");
2417283846bSjmcneill 
2427283846bSjmcneill 	sc->sc_clkdom.funcs = &sunxi_gates_clock_funcs;
2437283846bSjmcneill 	sc->sc_clkdom.priv = sc;
2447283846bSjmcneill 
2457283846bSjmcneill 	indices = fdtbus_get_prop(phandle, "clock-indices", &len);
2467283846bSjmcneill 	if (indices == NULL) {
2477283846bSjmcneill 		aprint_error_dev(self, "no clock-indices property\n");
2487283846bSjmcneill 		return;
2497283846bSjmcneill 	}
2507283846bSjmcneill 
2517283846bSjmcneill 	for (i = 0;
2527283846bSjmcneill 	     len >= sizeof(u_int);
2537283846bSjmcneill 	     len -= sizeof(u_int), i++, indices++) {
2547283846bSjmcneill 		const u_int index = be32dec(indices);
2557283846bSjmcneill 		const char *name = fdtbus_get_string_index(phandle,
2567283846bSjmcneill 		    "clock-output-names", i);
2577283846bSjmcneill 
2587283846bSjmcneill 		if (name == NULL) {
2597283846bSjmcneill 			aprint_error_dev(self, "no name for clk index %d\n",
2607283846bSjmcneill 			    index);
2617283846bSjmcneill 			continue;
2627283846bSjmcneill 		}
2637283846bSjmcneill 
2647283846bSjmcneill 		gate = kmem_zalloc(sizeof(*gate), KM_SLEEP);
2657283846bSjmcneill 		gate->base.domain = &sc->sc_clkdom;
2667283846bSjmcneill 		gate->base.name = name;
2677283846bSjmcneill 		gate->index = index;
2687283846bSjmcneill 
2697283846bSjmcneill 		TAILQ_INSERT_TAIL(&sc->sc_gates, gate, gates);
2707283846bSjmcneill 	}
2717283846bSjmcneill 
2727283846bSjmcneill 	fdtbus_register_clock_controller(sc->sc_dev, phandle,
2737283846bSjmcneill 	    &sunxi_gates_fdtclock_funcs);
2747283846bSjmcneill 
2757283846bSjmcneill 	sunxi_gates_print(sc);
2767283846bSjmcneill }
2777283846bSjmcneill 
2787283846bSjmcneill CFATTACH_DECL_NEW(sunxi_gates, sizeof(struct sunxi_gates_softc),
2797283846bSjmcneill     sunxi_gates_match, sunxi_gates_attach, NULL, NULL);
280