Home | History | Annotate | Line # | Download | only in sunxi
      1  1.4   thorpej /* $NetBSD: sunxi_gates.c,v 1.4 2021/01/27 03:10:20 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.4   thorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_gates.c,v 1.4 2021/01/27 03:10:20 thorpej Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/cpu.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/kmem.h>
     37  1.1  jmcneill 
     38  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <dev/clk/clk_backend.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #define	GATE_REG(index)		(((index) / 32) * 4)
     43  1.1  jmcneill #define	GATE_MASK(index)	__BIT((index) % 32)
     44  1.1  jmcneill 
     45  1.4   thorpej static const struct device_compatible_entry compat_data[] = {
     46  1.4   thorpej 	{ .compat = "allwinner,sun4i-a10-gates-clk" },
     47  1.4   thorpej 	{ .compat = "allwinner,sun9i-a80-apbs-gates-clk" },
     48  1.4   thorpej 	DEVICE_COMPAT_EOL
     49  1.1  jmcneill };
     50  1.1  jmcneill 
     51  1.1  jmcneill struct sunxi_gate {
     52  1.1  jmcneill 	struct clk		base;
     53  1.1  jmcneill 	u_int			index;
     54  1.1  jmcneill 
     55  1.1  jmcneill 	TAILQ_ENTRY(sunxi_gate)	gates;
     56  1.1  jmcneill };
     57  1.1  jmcneill 
     58  1.1  jmcneill struct sunxi_gates_softc {
     59  1.1  jmcneill 	device_t		sc_dev;
     60  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     61  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     62  1.1  jmcneill 	int			sc_phandle;
     63  1.1  jmcneill 
     64  1.1  jmcneill 	struct clk_domain	sc_clkdom;
     65  1.1  jmcneill 	TAILQ_HEAD(, sunxi_gate) sc_gates;
     66  1.1  jmcneill };
     67  1.1  jmcneill 
     68  1.1  jmcneill #define	GATE_READ(sc, reg)		\
     69  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     70  1.1  jmcneill #define	GATE_WRITE(sc, reg, val)	\
     71  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     72  1.1  jmcneill 
     73  1.1  jmcneill static struct clk *
     74  1.2   aymeric sunxi_gates_clock_decode(device_t dev, int cc_phandle, const void *data,
     75  1.2   aymeric 			 size_t len)
     76  1.1  jmcneill {
     77  1.1  jmcneill 	struct sunxi_gates_softc * const sc = device_private(dev);
     78  1.1  jmcneill 	struct sunxi_gate *gate;
     79  1.1  jmcneill 
     80  1.1  jmcneill 	if (len != 4)
     81  1.1  jmcneill 		return NULL;
     82  1.1  jmcneill 
     83  1.1  jmcneill 	const u_int index = be32dec(data);
     84  1.1  jmcneill 
     85  1.1  jmcneill 	TAILQ_FOREACH(gate, &sc->sc_gates, gates)
     86  1.1  jmcneill 		if (gate->index == index)
     87  1.1  jmcneill 			return &gate->base;
     88  1.1  jmcneill 
     89  1.1  jmcneill 	return NULL;
     90  1.1  jmcneill }
     91  1.1  jmcneill 
     92  1.1  jmcneill static const struct fdtbus_clock_controller_func sunxi_gates_fdtclock_funcs = {
     93  1.1  jmcneill 	.decode = sunxi_gates_clock_decode,
     94  1.1  jmcneill };
     95  1.1  jmcneill 
     96  1.1  jmcneill static struct clk *
     97  1.1  jmcneill sunxi_gates_clock_get(void *priv, const char *name)
     98  1.1  jmcneill {
     99  1.1  jmcneill 	struct sunxi_gates_softc * const sc = priv;
    100  1.1  jmcneill 	struct sunxi_gate *gate;
    101  1.1  jmcneill 
    102  1.1  jmcneill 	TAILQ_FOREACH(gate, &sc->sc_gates, gates)
    103  1.1  jmcneill 		if (strcmp(gate->base.name, name) == 0)
    104  1.1  jmcneill 			return &gate->base;
    105  1.1  jmcneill 
    106  1.1  jmcneill 	return NULL;
    107  1.1  jmcneill }
    108  1.1  jmcneill 
    109  1.1  jmcneill static void
    110  1.1  jmcneill sunxi_gates_clock_put(void *priv, struct clk *clk)
    111  1.1  jmcneill {
    112  1.1  jmcneill }
    113  1.1  jmcneill 
    114  1.1  jmcneill static u_int
    115  1.1  jmcneill sunxi_gates_clock_get_rate(void *priv, struct clk *clkp)
    116  1.1  jmcneill {
    117  1.1  jmcneill 	struct sunxi_gates_softc * const sc = priv;
    118  1.1  jmcneill 	struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
    119  1.1  jmcneill 	struct clk *clkp_parent;
    120  1.1  jmcneill 
    121  1.1  jmcneill 	clkp_parent = clk_get_parent(clkp);
    122  1.1  jmcneill 	if (clkp_parent == NULL)
    123  1.1  jmcneill 		return 0;
    124  1.1  jmcneill 
    125  1.1  jmcneill 	const bus_size_t gate_reg = GATE_REG(gate->index);
    126  1.1  jmcneill 	const uint32_t gate_mask = GATE_MASK(gate->index);
    127  1.1  jmcneill 
    128  1.1  jmcneill 	if ((GATE_READ(sc, gate_reg) & gate_mask) == 0)
    129  1.1  jmcneill 		return 0;
    130  1.1  jmcneill 
    131  1.1  jmcneill 	return clk_get_rate(clkp_parent);
    132  1.1  jmcneill }
    133  1.1  jmcneill 
    134  1.1  jmcneill static int
    135  1.1  jmcneill sunxi_gates_clock_enable(void *priv, struct clk *clkp)
    136  1.1  jmcneill {
    137  1.1  jmcneill 	struct sunxi_gates_softc * const sc = priv;
    138  1.1  jmcneill 	struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
    139  1.1  jmcneill 	uint32_t val;
    140  1.1  jmcneill 
    141  1.1  jmcneill 	const bus_size_t gate_reg = GATE_REG(gate->index);
    142  1.1  jmcneill 	const uint32_t gate_mask = GATE_MASK(gate->index);
    143  1.1  jmcneill 
    144  1.1  jmcneill 	val = GATE_READ(sc, gate_reg);
    145  1.1  jmcneill 	val |= gate_mask;
    146  1.1  jmcneill 	GATE_WRITE(sc, gate_reg, val);
    147  1.1  jmcneill 
    148  1.1  jmcneill 	return 0;
    149  1.1  jmcneill }
    150  1.1  jmcneill 
    151  1.1  jmcneill static int
    152  1.1  jmcneill sunxi_gates_clock_disable(void *priv, struct clk *clkp)
    153  1.1  jmcneill {
    154  1.1  jmcneill 	struct sunxi_gates_softc * const sc = priv;
    155  1.1  jmcneill 	struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
    156  1.1  jmcneill 	uint32_t val;
    157  1.1  jmcneill 
    158  1.1  jmcneill 	const bus_size_t gate_reg = GATE_REG(gate->index);
    159  1.1  jmcneill 	const uint32_t gate_mask = GATE_MASK(gate->index);
    160  1.1  jmcneill 
    161  1.1  jmcneill 	val = GATE_READ(sc, gate_reg);
    162  1.1  jmcneill 	val &= ~gate_mask;
    163  1.1  jmcneill 	GATE_WRITE(sc, gate_reg, val);
    164  1.1  jmcneill 
    165  1.1  jmcneill 	return 0;
    166  1.1  jmcneill }
    167  1.1  jmcneill 
    168  1.1  jmcneill static struct clk *
    169  1.1  jmcneill sunxi_gates_clock_get_parent(void *priv, struct clk *clkp)
    170  1.1  jmcneill {
    171  1.1  jmcneill 	struct sunxi_gates_softc * const sc = priv;
    172  1.1  jmcneill 
    173  1.1  jmcneill 	return fdtbus_clock_get_index(sc->sc_phandle, 0);
    174  1.1  jmcneill }
    175  1.1  jmcneill 
    176  1.1  jmcneill static const struct clk_funcs sunxi_gates_clock_funcs = {
    177  1.1  jmcneill 	.get = sunxi_gates_clock_get,
    178  1.1  jmcneill 	.put = sunxi_gates_clock_put,
    179  1.1  jmcneill 	.get_rate = sunxi_gates_clock_get_rate,
    180  1.1  jmcneill 	.enable = sunxi_gates_clock_enable,
    181  1.1  jmcneill 	.disable = sunxi_gates_clock_disable,
    182  1.1  jmcneill 	.get_parent = sunxi_gates_clock_get_parent,
    183  1.1  jmcneill };
    184  1.1  jmcneill 
    185  1.1  jmcneill static void
    186  1.1  jmcneill sunxi_gates_print(struct sunxi_gates_softc *sc)
    187  1.1  jmcneill {
    188  1.1  jmcneill 	struct sunxi_gate *gate;
    189  1.1  jmcneill 	struct clk *clkp_parent;
    190  1.1  jmcneill 
    191  1.1  jmcneill 	TAILQ_FOREACH(gate, &sc->sc_gates, gates) {
    192  1.1  jmcneill 		clkp_parent = clk_get_parent(&gate->base);
    193  1.1  jmcneill 
    194  1.1  jmcneill         	aprint_debug_dev(sc->sc_dev,
    195  1.1  jmcneill 		    "%3d %-12s %2s %-12s %-7s ",
    196  1.1  jmcneill 		    gate->index,
    197  1.1  jmcneill         	    gate->base.name,
    198  1.1  jmcneill         	    clkp_parent ? "<-" : "",
    199  1.1  jmcneill         	    clkp_parent ? clkp_parent->name : "",
    200  1.1  jmcneill         	    "gate");
    201  1.1  jmcneill 		aprint_debug("%10d Hz\n", clk_get_rate(&gate->base));
    202  1.1  jmcneill 	}
    203  1.1  jmcneill }
    204  1.1  jmcneill 
    205  1.1  jmcneill static int
    206  1.1  jmcneill sunxi_gates_match(device_t parent, cfdata_t cf, void *aux)
    207  1.1  jmcneill {
    208  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    209  1.1  jmcneill 
    210  1.4   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    211  1.1  jmcneill }
    212  1.1  jmcneill 
    213  1.1  jmcneill static void
    214  1.1  jmcneill sunxi_gates_attach(device_t parent, device_t self, void *aux)
    215  1.1  jmcneill {
    216  1.1  jmcneill 	struct sunxi_gates_softc * const sc = device_private(self);
    217  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    218  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    219  1.1  jmcneill 	struct sunxi_gate *gate;
    220  1.1  jmcneill 	const u_int *indices;
    221  1.1  jmcneill 	bus_addr_t addr;
    222  1.1  jmcneill 	bus_size_t size;
    223  1.1  jmcneill 	int len, i;
    224  1.1  jmcneill 
    225  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    226  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    227  1.1  jmcneill 		return;
    228  1.1  jmcneill 	}
    229  1.1  jmcneill 
    230  1.1  jmcneill 	sc->sc_dev = self;
    231  1.1  jmcneill 	sc->sc_phandle = phandle;
    232  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    233  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    234  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    235  1.1  jmcneill 		return;
    236  1.1  jmcneill 	}
    237  1.1  jmcneill 	TAILQ_INIT(&sc->sc_gates);
    238  1.1  jmcneill 
    239  1.1  jmcneill 	aprint_naive("\n");
    240  1.1  jmcneill 	aprint_normal("\n");
    241  1.1  jmcneill 
    242  1.1  jmcneill 	sc->sc_clkdom.funcs = &sunxi_gates_clock_funcs;
    243  1.1  jmcneill 	sc->sc_clkdom.priv = sc;
    244  1.1  jmcneill 
    245  1.1  jmcneill 	indices = fdtbus_get_prop(phandle, "clock-indices", &len);
    246  1.1  jmcneill 	if (indices == NULL) {
    247  1.1  jmcneill 		aprint_error_dev(self, "no clock-indices property\n");
    248  1.1  jmcneill 		return;
    249  1.1  jmcneill 	}
    250  1.1  jmcneill 
    251  1.1  jmcneill 	for (i = 0;
    252  1.1  jmcneill 	     len >= sizeof(u_int);
    253  1.1  jmcneill 	     len -= sizeof(u_int), i++, indices++) {
    254  1.1  jmcneill 		const u_int index = be32dec(indices);
    255  1.1  jmcneill 		const char *name = fdtbus_get_string_index(phandle,
    256  1.1  jmcneill 		    "clock-output-names", i);
    257  1.1  jmcneill 
    258  1.1  jmcneill 		if (name == NULL) {
    259  1.1  jmcneill 			aprint_error_dev(self, "no name for clk index %d\n",
    260  1.1  jmcneill 			    index);
    261  1.1  jmcneill 			continue;
    262  1.1  jmcneill 		}
    263  1.1  jmcneill 
    264  1.1  jmcneill 		gate = kmem_zalloc(sizeof(*gate), KM_SLEEP);
    265  1.1  jmcneill 		gate->base.domain = &sc->sc_clkdom;
    266  1.1  jmcneill 		gate->base.name = name;
    267  1.1  jmcneill 		gate->index = index;
    268  1.1  jmcneill 
    269  1.1  jmcneill 		TAILQ_INSERT_TAIL(&sc->sc_gates, gate, gates);
    270  1.1  jmcneill 	}
    271  1.1  jmcneill 
    272  1.1  jmcneill 	fdtbus_register_clock_controller(sc->sc_dev, phandle,
    273  1.1  jmcneill 	    &sunxi_gates_fdtclock_funcs);
    274  1.1  jmcneill 
    275  1.1  jmcneill 	sunxi_gates_print(sc);
    276  1.1  jmcneill }
    277  1.1  jmcneill 
    278  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_gates, sizeof(struct sunxi_gates_softc),
    279  1.1  jmcneill     sunxi_gates_match, sunxi_gates_attach, NULL, NULL);
    280