Home | History | Annotate | Line # | Download | only in ti
ti_div_clock.c revision 1.1.12.1
      1  1.1.12.1   thorpej /* $NetBSD: ti_div_clock.c,v 1.1.12.1 2021/04/03 22:28:19 thorpej Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4       1.1  jmcneill  * Copyright (c) 2019 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.1.12.1   thorpej __KERNEL_RCSID(0, "$NetBSD: ti_div_clock.c,v 1.1.12.1 2021/04/03 22:28:19 thorpej Exp $");
     31       1.1  jmcneill 
     32       1.1  jmcneill #include <sys/param.h>
     33       1.1  jmcneill #include <sys/systm.h>
     34       1.1  jmcneill #include <sys/device.h>
     35       1.1  jmcneill #include <sys/kmem.h>
     36       1.1  jmcneill #include <sys/bus.h>
     37       1.1  jmcneill 
     38       1.1  jmcneill #include <dev/clk/clk_backend.h>
     39       1.1  jmcneill 
     40       1.1  jmcneill #include <dev/fdt/fdtvar.h>
     41       1.1  jmcneill 
     42  1.1.12.1   thorpej static const struct device_compatible_entry compat_data[] = {
     43  1.1.12.1   thorpej 	{ .compat = "ti,divider-clock" },
     44  1.1.12.1   thorpej 	DEVICE_COMPAT_EOL
     45       1.1  jmcneill };
     46       1.1  jmcneill 
     47       1.1  jmcneill static int	ti_div_clock_match(device_t, cfdata_t, void *);
     48       1.1  jmcneill static void	ti_div_clock_attach(device_t, device_t, void *);
     49       1.1  jmcneill 
     50       1.1  jmcneill static struct clk *ti_div_clock_decode(device_t, int, const void *, size_t);
     51       1.1  jmcneill 
     52       1.1  jmcneill static const struct fdtbus_clock_controller_func ti_div_clock_fdt_funcs = {
     53       1.1  jmcneill 	.decode = ti_div_clock_decode
     54       1.1  jmcneill };
     55       1.1  jmcneill 
     56       1.1  jmcneill static struct clk *ti_div_clock_get(void *, const char *);
     57       1.1  jmcneill static void	ti_div_clock_put(void *, struct clk *);
     58       1.1  jmcneill static u_int	ti_div_clock_get_rate(void *, struct clk *);
     59       1.1  jmcneill static struct clk *ti_div_clock_get_parent(void *, struct clk *);
     60       1.1  jmcneill 
     61       1.1  jmcneill static const struct clk_funcs ti_div_clock_clk_funcs = {
     62       1.1  jmcneill 	.get = ti_div_clock_get,
     63       1.1  jmcneill 	.put = ti_div_clock_put,
     64       1.1  jmcneill 	.get_rate = ti_div_clock_get_rate,
     65       1.1  jmcneill 	.get_parent = ti_div_clock_get_parent,
     66       1.1  jmcneill };
     67       1.1  jmcneill 
     68       1.1  jmcneill struct ti_div_clock_softc {
     69       1.1  jmcneill 	device_t		sc_dev;
     70       1.1  jmcneill 	int			sc_phandle;
     71       1.1  jmcneill 	bus_space_tag_t		sc_bst;
     72       1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     73       1.1  jmcneill 
     74       1.1  jmcneill 	struct clk_domain	sc_clkdom;
     75       1.1  jmcneill 	struct clk		sc_clk;
     76       1.1  jmcneill };
     77       1.1  jmcneill 
     78       1.1  jmcneill #define	RD4(sc)			\
     79       1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, 0)
     80       1.1  jmcneill #define	WR4(sc, val)		\
     81       1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, 0, (val))
     82       1.1  jmcneill 
     83       1.1  jmcneill CFATTACH_DECL_NEW(ti_div_clock, sizeof(struct ti_div_clock_softc),
     84       1.1  jmcneill     ti_div_clock_match, ti_div_clock_attach, NULL, NULL);
     85       1.1  jmcneill 
     86       1.1  jmcneill static int
     87       1.1  jmcneill ti_div_clock_match(device_t parent, cfdata_t cf, void *aux)
     88       1.1  jmcneill {
     89       1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
     90       1.1  jmcneill 
     91  1.1.12.1   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     92       1.1  jmcneill }
     93       1.1  jmcneill 
     94       1.1  jmcneill static void
     95       1.1  jmcneill ti_div_clock_attach(device_t parent, device_t self, void *aux)
     96       1.1  jmcneill {
     97       1.1  jmcneill 	struct ti_div_clock_softc * const sc = device_private(self);
     98       1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
     99       1.1  jmcneill 	const int phandle = faa->faa_phandle;
    100       1.1  jmcneill 	bus_addr_t addr, base_addr;
    101       1.1  jmcneill 
    102       1.1  jmcneill 	const int prcm_phandle = OF_parent(OF_parent(phandle));
    103       1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, NULL) != 0 ||
    104       1.1  jmcneill 	    fdtbus_get_reg(prcm_phandle, 0, &base_addr, NULL) != 0) {
    105       1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    106       1.1  jmcneill 		return;
    107       1.1  jmcneill 	}
    108       1.1  jmcneill 
    109       1.1  jmcneill 	sc->sc_dev = self;
    110       1.1  jmcneill 	sc->sc_phandle = phandle;
    111       1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    112       1.1  jmcneill 	if (bus_space_map(sc->sc_bst, base_addr + addr, 4, 0, &sc->sc_bsh) != 0) {
    113       1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    114       1.1  jmcneill 		return;
    115       1.1  jmcneill 	}
    116       1.1  jmcneill 
    117       1.1  jmcneill 	sc->sc_clkdom.name = device_xname(self);
    118       1.1  jmcneill 	sc->sc_clkdom.funcs = &ti_div_clock_clk_funcs;
    119       1.1  jmcneill 	sc->sc_clkdom.priv = sc;
    120       1.1  jmcneill 
    121       1.1  jmcneill 	sc->sc_clk.domain = &sc->sc_clkdom;
    122       1.1  jmcneill 	sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name);
    123       1.1  jmcneill 	clk_attach(&sc->sc_clk);
    124       1.1  jmcneill 
    125       1.1  jmcneill 	aprint_naive("\n");
    126       1.1  jmcneill 	aprint_normal(": TI divider clock (%s)\n", sc->sc_clk.name);
    127       1.1  jmcneill 
    128       1.1  jmcneill 	fdtbus_register_clock_controller(self, phandle, &ti_div_clock_fdt_funcs);
    129       1.1  jmcneill }
    130       1.1  jmcneill 
    131       1.1  jmcneill static struct clk *
    132       1.1  jmcneill ti_div_clock_decode(device_t dev, int cc_phandle, const void *data,
    133       1.1  jmcneill 		     size_t len)
    134       1.1  jmcneill {
    135       1.1  jmcneill 	struct ti_div_clock_softc * const sc = device_private(dev);
    136       1.1  jmcneill 
    137       1.1  jmcneill 	return &sc->sc_clk;
    138       1.1  jmcneill }
    139       1.1  jmcneill 
    140       1.1  jmcneill static struct clk *
    141       1.1  jmcneill ti_div_clock_get(void *priv, const char *name)
    142       1.1  jmcneill {
    143       1.1  jmcneill 	struct ti_div_clock_softc * const sc = priv;
    144       1.1  jmcneill 
    145       1.1  jmcneill 	return &sc->sc_clk;
    146       1.1  jmcneill }
    147       1.1  jmcneill 
    148       1.1  jmcneill static void
    149       1.1  jmcneill ti_div_clock_put(void *priv, struct clk *clk)
    150       1.1  jmcneill {
    151       1.1  jmcneill }
    152       1.1  jmcneill 
    153       1.1  jmcneill static u_int
    154       1.1  jmcneill ti_div_clock_get_rate(void *priv, struct clk *clk)
    155       1.1  jmcneill {
    156       1.1  jmcneill 	struct ti_div_clock_softc * const sc = priv;
    157       1.1  jmcneill 	struct clk *clk_parent = clk_get_parent(clk);
    158       1.1  jmcneill 	uint64_t parent_rate;
    159       1.1  jmcneill 	uint32_t val, max_val, mask;
    160       1.1  jmcneill 	u_int div, max_div, bit_shift;
    161       1.1  jmcneill 
    162       1.1  jmcneill 	if (clk_parent == NULL)
    163       1.1  jmcneill 		return 0;
    164       1.1  jmcneill 
    165       1.1  jmcneill 	if (of_hasprop(sc->sc_phandle, "ti,index-power-of-two"))
    166       1.1  jmcneill 		return EINVAL;	/* not supported yet */
    167       1.1  jmcneill 
    168       1.1  jmcneill 	const int start_index = of_hasprop(sc->sc_phandle, "ti,index-starts-at-one") ? 1 : 0;
    169       1.1  jmcneill 
    170       1.1  jmcneill 	if (of_getprop_uint32(sc->sc_phandle, "ti,bit-shift", &bit_shift) != 0)
    171       1.1  jmcneill 		bit_shift = 0;
    172       1.1  jmcneill 
    173       1.1  jmcneill 	if (of_getprop_uint32(sc->sc_phandle, "ti,max-div", &max_div) == 0) {
    174       1.1  jmcneill 		max_val = start_index + max_div;
    175       1.1  jmcneill 		while (!powerof2(max_val))
    176       1.1  jmcneill 			max_val++;
    177       1.1  jmcneill 		mask = (max_val - 1) << bit_shift;
    178       1.1  jmcneill 	} else {
    179       1.1  jmcneill 		/*
    180       1.1  jmcneill 		 * The bindings allow for this, but it is not yet supported
    181       1.1  jmcneill 		 * by this driver.
    182       1.1  jmcneill 		 */
    183       1.1  jmcneill 		return EINVAL;
    184       1.1  jmcneill 	}
    185       1.1  jmcneill 
    186       1.1  jmcneill 	val = RD4(sc);
    187       1.1  jmcneill 	div = __SHIFTOUT(val, mask) + (start_index ? 0 : 1);
    188       1.1  jmcneill 	if (div == 0)
    189       1.1  jmcneill 		return EINVAL;
    190       1.1  jmcneill 
    191       1.1  jmcneill 	parent_rate = clk_get_rate(clk_parent);
    192       1.1  jmcneill 
    193       1.1  jmcneill 	return (u_int)(parent_rate / div);
    194       1.1  jmcneill }
    195       1.1  jmcneill 
    196       1.1  jmcneill static struct clk *
    197       1.1  jmcneill ti_div_clock_get_parent(void *priv, struct clk *clk)
    198       1.1  jmcneill {
    199       1.1  jmcneill 	struct ti_div_clock_softc * const sc = priv;
    200       1.1  jmcneill 
    201       1.1  jmcneill 	return fdtbus_clock_get_index(sc->sc_phandle, 0);
    202       1.1  jmcneill }
    203