Home | History | Annotate | Line # | Download | only in ti
ti_dpll_clock.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: ti_dpll_clock.c,v 1.1 2019/10/28 21:16:47 jmcneill 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  jmcneill __KERNEL_RCSID(0, "$NetBSD: ti_dpll_clock.c,v 1.1 2019/10/28 21:16:47 jmcneill 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  jmcneill /* CM_IDLEST_DPLL_MPU */
     43  1.1  jmcneill #define	ST_MN_BYPASS		__BIT(8)
     44  1.1  jmcneill #define	ST_DPLL_CLK		__BIT(0)
     45  1.1  jmcneill 
     46  1.1  jmcneill /* CM_CLKSEL_DPLL_MPU */
     47  1.1  jmcneill #define	DPLL_BYP_CLKSEL		__BIT(23)
     48  1.1  jmcneill #define	DPLL_MULT		__BITS(18,8)
     49  1.1  jmcneill #define	DPLL_DIV		__BITS(6,0)
     50  1.1  jmcneill 
     51  1.1  jmcneill /* CM_CLKMODE_DPLL_MPU */
     52  1.1  jmcneill #define	DPLL_EN			__BITS(2,0)
     53  1.1  jmcneill #define	 DPLL_EN_NM_BYPASS	4
     54  1.1  jmcneill #define	 DPLL_EN_LOCK		7
     55  1.1  jmcneill 
     56  1.1  jmcneill static const char * const compatible[] = {
     57  1.1  jmcneill 	"ti,am3-dpll-clock",
     58  1.1  jmcneill 	NULL
     59  1.1  jmcneill };
     60  1.1  jmcneill 
     61  1.1  jmcneill static int	ti_dpll_clock_match(device_t, cfdata_t, void *);
     62  1.1  jmcneill static void	ti_dpll_clock_attach(device_t, device_t, void *);
     63  1.1  jmcneill 
     64  1.1  jmcneill static struct clk *ti_dpll_clock_decode(device_t, int, const void *, size_t);
     65  1.1  jmcneill 
     66  1.1  jmcneill static const struct fdtbus_clock_controller_func ti_dpll_clock_fdt_funcs = {
     67  1.1  jmcneill 	.decode = ti_dpll_clock_decode
     68  1.1  jmcneill };
     69  1.1  jmcneill 
     70  1.1  jmcneill static struct clk *ti_dpll_clock_get(void *, const char *);
     71  1.1  jmcneill static void	ti_dpll_clock_put(void *, struct clk *);
     72  1.1  jmcneill static int	ti_dpll_clock_set_rate(void *, struct clk *, u_int);
     73  1.1  jmcneill static u_int	ti_dpll_clock_get_rate(void *, struct clk *);
     74  1.1  jmcneill static struct clk *ti_dpll_clock_get_parent(void *, struct clk *);
     75  1.1  jmcneill 
     76  1.1  jmcneill static const struct clk_funcs ti_dpll_clock_clk_funcs = {
     77  1.1  jmcneill 	.get = ti_dpll_clock_get,
     78  1.1  jmcneill 	.put = ti_dpll_clock_put,
     79  1.1  jmcneill 	.set_rate = ti_dpll_clock_set_rate,
     80  1.1  jmcneill 	.get_rate = ti_dpll_clock_get_rate,
     81  1.1  jmcneill 	.get_parent = ti_dpll_clock_get_parent,
     82  1.1  jmcneill };
     83  1.1  jmcneill 
     84  1.1  jmcneill enum {
     85  1.1  jmcneill 	REG_CONTROL,
     86  1.1  jmcneill 	REG_IDLEST,
     87  1.1  jmcneill 	REG_MULT_DIV1,
     88  1.1  jmcneill 	NREG
     89  1.1  jmcneill };
     90  1.1  jmcneill 
     91  1.1  jmcneill struct ti_dpll_clock_softc {
     92  1.1  jmcneill 	device_t		sc_dev;
     93  1.1  jmcneill 	int			sc_phandle;
     94  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     95  1.1  jmcneill 	bus_space_handle_t	sc_bsh[NREG];
     96  1.1  jmcneill 
     97  1.1  jmcneill 	struct clk_domain	sc_clkdom;
     98  1.1  jmcneill 	struct clk		sc_clk;
     99  1.1  jmcneill };
    100  1.1  jmcneill 
    101  1.1  jmcneill #define	RD4(sc, space)			\
    102  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh[(space)], 0)
    103  1.1  jmcneill #define	WR4(sc, space, val)		\
    104  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh[(space)], 0, (val))
    105  1.1  jmcneill 
    106  1.1  jmcneill CFATTACH_DECL_NEW(ti_dpll_clock, sizeof(struct ti_dpll_clock_softc),
    107  1.1  jmcneill     ti_dpll_clock_match, ti_dpll_clock_attach, NULL, NULL);
    108  1.1  jmcneill 
    109  1.1  jmcneill static int
    110  1.1  jmcneill ti_dpll_clock_match(device_t parent, cfdata_t cf, void *aux)
    111  1.1  jmcneill {
    112  1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
    113  1.1  jmcneill 
    114  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    115  1.1  jmcneill }
    116  1.1  jmcneill 
    117  1.1  jmcneill static void
    118  1.1  jmcneill ti_dpll_clock_attach(device_t parent, device_t self, void *aux)
    119  1.1  jmcneill {
    120  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = device_private(self);
    121  1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
    122  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    123  1.1  jmcneill 	bus_addr_t addr[NREG], base_addr;
    124  1.1  jmcneill 	u_int n;
    125  1.1  jmcneill 
    126  1.1  jmcneill 	const int prcm_phandle = OF_parent(OF_parent(phandle));
    127  1.1  jmcneill 	if (fdtbus_get_reg(prcm_phandle, 0, &base_addr, NULL) != 0) {
    128  1.1  jmcneill 		aprint_error(": couldn't get prcm registers\n");
    129  1.1  jmcneill 		return;
    130  1.1  jmcneill 	}
    131  1.1  jmcneill 
    132  1.1  jmcneill 	for (n = 0; n < NREG; n++) {
    133  1.1  jmcneill 		if (fdtbus_get_reg(phandle, n, &addr[n], NULL) != 0) {
    134  1.1  jmcneill 			aprint_error(": couldn't get registers\n");
    135  1.1  jmcneill 			return;
    136  1.1  jmcneill 		}
    137  1.1  jmcneill 	}
    138  1.1  jmcneill 
    139  1.1  jmcneill 	sc->sc_dev = self;
    140  1.1  jmcneill 	sc->sc_phandle = phandle;
    141  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    142  1.1  jmcneill 	for (n = 0; n < NREG; n++) {
    143  1.1  jmcneill 		if (bus_space_map(sc->sc_bst, base_addr + addr[n], 4, 0, &sc->sc_bsh[n]) != 0) {
    144  1.1  jmcneill 			aprint_error(": couldn't map registers\n");
    145  1.1  jmcneill 			return;
    146  1.1  jmcneill 		}
    147  1.1  jmcneill 	}
    148  1.1  jmcneill 
    149  1.1  jmcneill 	sc->sc_clkdom.name = device_xname(self);
    150  1.1  jmcneill 	sc->sc_clkdom.funcs = &ti_dpll_clock_clk_funcs;
    151  1.1  jmcneill 	sc->sc_clkdom.priv = sc;
    152  1.1  jmcneill 
    153  1.1  jmcneill 	sc->sc_clk.domain = &sc->sc_clkdom;
    154  1.1  jmcneill 	sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name);
    155  1.1  jmcneill 	clk_attach(&sc->sc_clk);
    156  1.1  jmcneill 
    157  1.1  jmcneill 	aprint_naive("\n");
    158  1.1  jmcneill 	aprint_normal(": TI DPLL clock (%s)\n", sc->sc_clk.name);
    159  1.1  jmcneill 
    160  1.1  jmcneill 	fdtbus_register_clock_controller(self, phandle, &ti_dpll_clock_fdt_funcs);
    161  1.1  jmcneill }
    162  1.1  jmcneill 
    163  1.1  jmcneill static struct clk *
    164  1.1  jmcneill ti_dpll_clock_decode(device_t dev, int cc_phandle, const void *data,
    165  1.1  jmcneill 		     size_t len)
    166  1.1  jmcneill {
    167  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = device_private(dev);
    168  1.1  jmcneill 
    169  1.1  jmcneill 	return &sc->sc_clk;
    170  1.1  jmcneill }
    171  1.1  jmcneill 
    172  1.1  jmcneill static struct clk *
    173  1.1  jmcneill ti_dpll_clock_get(void *priv, const char *name)
    174  1.1  jmcneill {
    175  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    176  1.1  jmcneill 
    177  1.1  jmcneill 	return &sc->sc_clk;
    178  1.1  jmcneill }
    179  1.1  jmcneill 
    180  1.1  jmcneill static void
    181  1.1  jmcneill ti_dpll_clock_put(void *priv, struct clk *clk)
    182  1.1  jmcneill {
    183  1.1  jmcneill }
    184  1.1  jmcneill 
    185  1.1  jmcneill static u_int
    186  1.1  jmcneill ti_dpll_clock_get_rate(void *priv, struct clk *clk)
    187  1.1  jmcneill {
    188  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    189  1.1  jmcneill 	struct clk *clk_parent = clk_get_parent(clk);
    190  1.1  jmcneill 	uint32_t val;
    191  1.1  jmcneill 	uint64_t parent_rate;
    192  1.1  jmcneill 
    193  1.1  jmcneill 	if (clk_parent == NULL)
    194  1.1  jmcneill 		return 0;
    195  1.1  jmcneill 
    196  1.1  jmcneill 	val = RD4(sc, REG_MULT_DIV1);
    197  1.1  jmcneill 	const u_int mult = __SHIFTOUT(val, DPLL_MULT);
    198  1.1  jmcneill 	const u_int div = __SHIFTOUT(val, DPLL_DIV) + 1;
    199  1.1  jmcneill 
    200  1.1  jmcneill 	parent_rate = clk_get_rate(clk_parent);
    201  1.1  jmcneill 
    202  1.1  jmcneill 	return (u_int)((mult * parent_rate) / div);
    203  1.1  jmcneill }
    204  1.1  jmcneill 
    205  1.1  jmcneill static int
    206  1.1  jmcneill ti_dpll_clock_set_rate(void *priv, struct clk *clk, u_int rate)
    207  1.1  jmcneill {
    208  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    209  1.1  jmcneill 	struct clk *clk_parent = clk_get_parent(clk);
    210  1.1  jmcneill 	uint64_t parent_rate;
    211  1.1  jmcneill 	uint32_t control, mult_div1;
    212  1.1  jmcneill 
    213  1.1  jmcneill 	if (clk_parent == NULL)
    214  1.1  jmcneill 		return ENXIO;
    215  1.1  jmcneill 
    216  1.1  jmcneill 	parent_rate = clk_get_rate(clk_parent);
    217  1.1  jmcneill 
    218  1.1  jmcneill 	const u_int div = (parent_rate / 1000000) - 1;
    219  1.1  jmcneill 	const u_int mult = rate / (parent_rate / (div + 1));
    220  1.1  jmcneill 	if (mult < 2 || mult > 2047)
    221  1.1  jmcneill 		return EINVAL;
    222  1.1  jmcneill 
    223  1.1  jmcneill 	control = RD4(sc, REG_CONTROL);
    224  1.1  jmcneill 	control &= ~DPLL_EN;
    225  1.1  jmcneill 	control |= __SHIFTIN(DPLL_EN_LOCK, DPLL_EN);
    226  1.1  jmcneill 	WR4(sc, REG_CONTROL, control);
    227  1.1  jmcneill 
    228  1.1  jmcneill 	while ((RD4(sc, REG_IDLEST) & DPLL_EN_NM_BYPASS) != 0)
    229  1.1  jmcneill 		;
    230  1.1  jmcneill 
    231  1.1  jmcneill 	mult_div1 = __SHIFTIN(mult, DPLL_MULT);
    232  1.1  jmcneill 	mult_div1 |= __SHIFTIN(div, DPLL_DIV);
    233  1.1  jmcneill 	WR4(sc, REG_MULT_DIV1, mult_div1);
    234  1.1  jmcneill 
    235  1.1  jmcneill 	control &= ~DPLL_EN;
    236  1.1  jmcneill 	control |= __SHIFTIN(DPLL_EN_LOCK, DPLL_EN);
    237  1.1  jmcneill 	WR4(sc, REG_CONTROL, control);
    238  1.1  jmcneill 
    239  1.1  jmcneill 	while ((RD4(sc, REG_IDLEST) & ST_DPLL_CLK) != 0)
    240  1.1  jmcneill 		;
    241  1.1  jmcneill 
    242  1.1  jmcneill 	return 0;
    243  1.1  jmcneill }
    244  1.1  jmcneill 
    245  1.1  jmcneill static struct clk *
    246  1.1  jmcneill ti_dpll_clock_get_parent(void *priv, struct clk *clk)
    247  1.1  jmcneill {
    248  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    249  1.1  jmcneill 
    250  1.1  jmcneill 	/* XXX assume ref clk */
    251  1.1  jmcneill 	return fdtbus_clock_get_index(sc->sc_phandle, 0);
    252  1.1  jmcneill }
    253