Home | History | Annotate | Line # | Download | only in ti
ti_dpll_clock.c revision 1.3
      1  1.3  jmcneill /* $NetBSD: ti_dpll_clock.c,v 1.3 2020/06/03 18:26:06 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.3  jmcneill __KERNEL_RCSID(0, "$NetBSD: ti_dpll_clock.c,v 1.3 2020/06/03 18:26:06 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 #define	DPLL_MULT		__BITS(18,8)
     43  1.1  jmcneill #define	DPLL_DIV		__BITS(6,0)
     44  1.1  jmcneill 
     45  1.2  jmcneill #define	AM3_ST_MN_BYPASS	__BIT(8)
     46  1.2  jmcneill #define	AM3_ST_DPLL_CLK		__BIT(0)
     47  1.2  jmcneill 
     48  1.2  jmcneill #define	AM3_DPLL_EN		__BITS(2,0)
     49  1.2  jmcneill #define	 AM3_DPLL_EN_NM_BYPASS	4
     50  1.2  jmcneill #define	 AM3_DPLL_EN_LOCK	7
     51  1.2  jmcneill 
     52  1.2  jmcneill #define	OMAP3_ST_MPU_CLK	__BIT(0)
     53  1.2  jmcneill 
     54  1.2  jmcneill #define	OMAP3_EN_MPU_DPLL	__BITS(2,0)
     55  1.2  jmcneill #define	 OMAP3_EN_MPU_DPLL_BYPASS	5
     56  1.2  jmcneill #define	 OMAP3_EN_MPU_DPLL_LOCK		7
     57  1.2  jmcneill 
     58  1.2  jmcneill #define	OMAP3_CORE_DPLL_CLKOUT_DIV __BITS(31,27)
     59  1.2  jmcneill #define	OMAP3_CORE_DPLL_MULT	__BITS(26,16)
     60  1.2  jmcneill #define	OMAP3_CORE_DPLL_DIV	__BITS(14,8)
     61  1.1  jmcneill 
     62  1.1  jmcneill static int	ti_dpll_clock_match(device_t, cfdata_t, void *);
     63  1.1  jmcneill static void	ti_dpll_clock_attach(device_t, device_t, void *);
     64  1.1  jmcneill 
     65  1.1  jmcneill static struct clk *ti_dpll_clock_decode(device_t, int, const void *, size_t);
     66  1.1  jmcneill 
     67  1.1  jmcneill static const struct fdtbus_clock_controller_func ti_dpll_clock_fdt_funcs = {
     68  1.1  jmcneill 	.decode = ti_dpll_clock_decode
     69  1.1  jmcneill };
     70  1.1  jmcneill 
     71  1.1  jmcneill static struct clk *ti_dpll_clock_get(void *, const char *);
     72  1.1  jmcneill static void	ti_dpll_clock_put(void *, struct clk *);
     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.2  jmcneill static int	am3_dpll_clock_set_rate(void *, struct clk *, u_int);
     77  1.2  jmcneill 
     78  1.2  jmcneill static const struct clk_funcs am3_dpll_clock_clk_funcs = {
     79  1.1  jmcneill 	.get = ti_dpll_clock_get,
     80  1.1  jmcneill 	.put = ti_dpll_clock_put,
     81  1.2  jmcneill 	.set_rate = am3_dpll_clock_set_rate,
     82  1.1  jmcneill 	.get_rate = ti_dpll_clock_get_rate,
     83  1.1  jmcneill 	.get_parent = ti_dpll_clock_get_parent,
     84  1.1  jmcneill };
     85  1.1  jmcneill 
     86  1.2  jmcneill static int	omap3_dpll_clock_set_rate(void *, struct clk *, u_int);
     87  1.2  jmcneill 
     88  1.2  jmcneill static const struct clk_funcs omap3_dpll_clock_clk_funcs = {
     89  1.2  jmcneill 	.get = ti_dpll_clock_get,
     90  1.2  jmcneill 	.put = ti_dpll_clock_put,
     91  1.2  jmcneill 	.set_rate = omap3_dpll_clock_set_rate,
     92  1.2  jmcneill 	.get_rate = ti_dpll_clock_get_rate,
     93  1.2  jmcneill 	.get_parent = ti_dpll_clock_get_parent,
     94  1.2  jmcneill };
     95  1.2  jmcneill 
     96  1.2  jmcneill static u_int	omap3_dpll_core_clock_get_rate(void *, struct clk *);
     97  1.2  jmcneill 
     98  1.2  jmcneill static const struct clk_funcs omap3_dpll_core_clock_clk_funcs = {
     99  1.2  jmcneill 	.get = ti_dpll_clock_get,
    100  1.2  jmcneill 	.put = ti_dpll_clock_put,
    101  1.2  jmcneill 	.get_rate = omap3_dpll_core_clock_get_rate,
    102  1.2  jmcneill 	.get_parent = ti_dpll_clock_get_parent,
    103  1.2  jmcneill };
    104  1.2  jmcneill 
    105  1.2  jmcneill static const struct of_compat_data compat_data[] = {
    106  1.2  jmcneill 	{ "ti,am3-dpll-clock",		(uintptr_t)&am3_dpll_clock_clk_funcs },
    107  1.2  jmcneill 	{ "ti,omap3-dpll-clock",	(uintptr_t)&omap3_dpll_clock_clk_funcs },
    108  1.2  jmcneill 	{ "ti,omap3-dpll-core-clock",	(uintptr_t)&omap3_dpll_core_clock_clk_funcs },
    109  1.2  jmcneill 	{ NULL }
    110  1.2  jmcneill };
    111  1.2  jmcneill 
    112  1.1  jmcneill enum {
    113  1.1  jmcneill 	REG_CONTROL,
    114  1.1  jmcneill 	REG_IDLEST,
    115  1.1  jmcneill 	REG_MULT_DIV1,
    116  1.1  jmcneill 	NREG
    117  1.1  jmcneill };
    118  1.1  jmcneill 
    119  1.1  jmcneill struct ti_dpll_clock_softc {
    120  1.1  jmcneill 	device_t		sc_dev;
    121  1.1  jmcneill 	int			sc_phandle;
    122  1.1  jmcneill 	bus_space_tag_t		sc_bst;
    123  1.1  jmcneill 	bus_space_handle_t	sc_bsh[NREG];
    124  1.1  jmcneill 
    125  1.1  jmcneill 	struct clk_domain	sc_clkdom;
    126  1.1  jmcneill 	struct clk		sc_clk;
    127  1.1  jmcneill };
    128  1.1  jmcneill 
    129  1.1  jmcneill #define	RD4(sc, space)			\
    130  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh[(space)], 0)
    131  1.1  jmcneill #define	WR4(sc, space, val)		\
    132  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh[(space)], 0, (val))
    133  1.1  jmcneill 
    134  1.1  jmcneill CFATTACH_DECL_NEW(ti_dpll_clock, sizeof(struct ti_dpll_clock_softc),
    135  1.1  jmcneill     ti_dpll_clock_match, ti_dpll_clock_attach, NULL, NULL);
    136  1.1  jmcneill 
    137  1.1  jmcneill static int
    138  1.1  jmcneill ti_dpll_clock_match(device_t parent, cfdata_t cf, void *aux)
    139  1.1  jmcneill {
    140  1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
    141  1.1  jmcneill 
    142  1.2  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    143  1.1  jmcneill }
    144  1.1  jmcneill 
    145  1.1  jmcneill static void
    146  1.1  jmcneill ti_dpll_clock_attach(device_t parent, device_t self, void *aux)
    147  1.1  jmcneill {
    148  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = device_private(self);
    149  1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
    150  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    151  1.2  jmcneill 	const struct clk_funcs *clkfuncs;
    152  1.1  jmcneill 	bus_addr_t addr[NREG], base_addr;
    153  1.1  jmcneill 	u_int n;
    154  1.1  jmcneill 
    155  1.1  jmcneill 	const int prcm_phandle = OF_parent(OF_parent(phandle));
    156  1.1  jmcneill 	if (fdtbus_get_reg(prcm_phandle, 0, &base_addr, NULL) != 0) {
    157  1.1  jmcneill 		aprint_error(": couldn't get prcm registers\n");
    158  1.1  jmcneill 		return;
    159  1.1  jmcneill 	}
    160  1.1  jmcneill 
    161  1.1  jmcneill 	for (n = 0; n < NREG; n++) {
    162  1.1  jmcneill 		if (fdtbus_get_reg(phandle, n, &addr[n], NULL) != 0) {
    163  1.1  jmcneill 			aprint_error(": couldn't get registers\n");
    164  1.1  jmcneill 			return;
    165  1.1  jmcneill 		}
    166  1.1  jmcneill 	}
    167  1.1  jmcneill 
    168  1.1  jmcneill 	sc->sc_dev = self;
    169  1.1  jmcneill 	sc->sc_phandle = phandle;
    170  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    171  1.1  jmcneill 	for (n = 0; n < NREG; n++) {
    172  1.1  jmcneill 		if (bus_space_map(sc->sc_bst, base_addr + addr[n], 4, 0, &sc->sc_bsh[n]) != 0) {
    173  1.1  jmcneill 			aprint_error(": couldn't map registers\n");
    174  1.1  jmcneill 			return;
    175  1.1  jmcneill 		}
    176  1.1  jmcneill 	}
    177  1.1  jmcneill 
    178  1.2  jmcneill 	clkfuncs = (const void *)of_search_compatible(phandle, compat_data)->data;
    179  1.2  jmcneill 
    180  1.1  jmcneill 	sc->sc_clkdom.name = device_xname(self);
    181  1.2  jmcneill 	sc->sc_clkdom.funcs = clkfuncs;
    182  1.1  jmcneill 	sc->sc_clkdom.priv = sc;
    183  1.1  jmcneill 
    184  1.1  jmcneill 	sc->sc_clk.domain = &sc->sc_clkdom;
    185  1.1  jmcneill 	sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name);
    186  1.1  jmcneill 	clk_attach(&sc->sc_clk);
    187  1.1  jmcneill 
    188  1.1  jmcneill 	aprint_naive("\n");
    189  1.1  jmcneill 	aprint_normal(": TI DPLL clock (%s)\n", sc->sc_clk.name);
    190  1.1  jmcneill 
    191  1.1  jmcneill 	fdtbus_register_clock_controller(self, phandle, &ti_dpll_clock_fdt_funcs);
    192  1.1  jmcneill }
    193  1.1  jmcneill 
    194  1.1  jmcneill static struct clk *
    195  1.1  jmcneill ti_dpll_clock_decode(device_t dev, int cc_phandle, const void *data,
    196  1.1  jmcneill 		     size_t len)
    197  1.1  jmcneill {
    198  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = device_private(dev);
    199  1.1  jmcneill 
    200  1.1  jmcneill 	return &sc->sc_clk;
    201  1.1  jmcneill }
    202  1.1  jmcneill 
    203  1.1  jmcneill static struct clk *
    204  1.1  jmcneill ti_dpll_clock_get(void *priv, const char *name)
    205  1.1  jmcneill {
    206  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    207  1.1  jmcneill 
    208  1.1  jmcneill 	return &sc->sc_clk;
    209  1.1  jmcneill }
    210  1.1  jmcneill 
    211  1.1  jmcneill static void
    212  1.1  jmcneill ti_dpll_clock_put(void *priv, struct clk *clk)
    213  1.1  jmcneill {
    214  1.1  jmcneill }
    215  1.1  jmcneill 
    216  1.1  jmcneill static u_int
    217  1.1  jmcneill ti_dpll_clock_get_rate(void *priv, struct clk *clk)
    218  1.1  jmcneill {
    219  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    220  1.1  jmcneill 	struct clk *clk_parent = clk_get_parent(clk);
    221  1.1  jmcneill 	uint32_t val;
    222  1.1  jmcneill 	uint64_t parent_rate;
    223  1.1  jmcneill 
    224  1.1  jmcneill 	if (clk_parent == NULL)
    225  1.1  jmcneill 		return 0;
    226  1.1  jmcneill 
    227  1.1  jmcneill 	val = RD4(sc, REG_MULT_DIV1);
    228  1.1  jmcneill 	const u_int mult = __SHIFTOUT(val, DPLL_MULT);
    229  1.1  jmcneill 	const u_int div = __SHIFTOUT(val, DPLL_DIV) + 1;
    230  1.1  jmcneill 
    231  1.1  jmcneill 	parent_rate = clk_get_rate(clk_parent);
    232  1.1  jmcneill 
    233  1.1  jmcneill 	return (u_int)((mult * parent_rate) / div);
    234  1.1  jmcneill }
    235  1.1  jmcneill 
    236  1.2  jmcneill static struct clk *
    237  1.2  jmcneill ti_dpll_clock_get_parent(void *priv, struct clk *clk)
    238  1.2  jmcneill {
    239  1.2  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    240  1.2  jmcneill 
    241  1.2  jmcneill 	/* XXX assume ref clk */
    242  1.2  jmcneill 	return fdtbus_clock_get_index(sc->sc_phandle, 0);
    243  1.2  jmcneill }
    244  1.2  jmcneill 
    245  1.1  jmcneill static int
    246  1.2  jmcneill am3_dpll_clock_set_rate(void *priv, struct clk *clk, u_int rate)
    247  1.1  jmcneill {
    248  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    249  1.1  jmcneill 	struct clk *clk_parent = clk_get_parent(clk);
    250  1.1  jmcneill 	uint64_t parent_rate;
    251  1.1  jmcneill 	uint32_t control, mult_div1;
    252  1.1  jmcneill 
    253  1.1  jmcneill 	if (clk_parent == NULL)
    254  1.1  jmcneill 		return ENXIO;
    255  1.1  jmcneill 
    256  1.1  jmcneill 	parent_rate = clk_get_rate(clk_parent);
    257  1.2  jmcneill 	if (parent_rate == 0)
    258  1.2  jmcneill 		return EIO;
    259  1.1  jmcneill 
    260  1.1  jmcneill 	const u_int div = (parent_rate / 1000000) - 1;
    261  1.1  jmcneill 	const u_int mult = rate / (parent_rate / (div + 1));
    262  1.1  jmcneill 	if (mult < 2 || mult > 2047)
    263  1.1  jmcneill 		return EINVAL;
    264  1.1  jmcneill 
    265  1.1  jmcneill 	control = RD4(sc, REG_CONTROL);
    266  1.2  jmcneill 	control &= ~AM3_DPLL_EN;
    267  1.2  jmcneill 	control |= __SHIFTIN(AM3_DPLL_EN_NM_BYPASS, AM3_DPLL_EN);
    268  1.1  jmcneill 	WR4(sc, REG_CONTROL, control);
    269  1.1  jmcneill 
    270  1.3  jmcneill 	while (RD4(sc, REG_IDLEST) != AM3_ST_MN_BYPASS)
    271  1.1  jmcneill 		;
    272  1.1  jmcneill 
    273  1.1  jmcneill 	mult_div1 = __SHIFTIN(mult, DPLL_MULT);
    274  1.1  jmcneill 	mult_div1 |= __SHIFTIN(div, DPLL_DIV);
    275  1.1  jmcneill 	WR4(sc, REG_MULT_DIV1, mult_div1);
    276  1.1  jmcneill 
    277  1.2  jmcneill 	control &= ~AM3_DPLL_EN;
    278  1.2  jmcneill 	control |= __SHIFTIN(AM3_DPLL_EN_LOCK, AM3_DPLL_EN);
    279  1.2  jmcneill 	WR4(sc, REG_CONTROL, control);
    280  1.2  jmcneill 
    281  1.3  jmcneill 	while (RD4(sc, REG_IDLEST) != AM3_ST_DPLL_CLK)
    282  1.2  jmcneill 		;
    283  1.2  jmcneill 
    284  1.2  jmcneill 	return 0;
    285  1.2  jmcneill }
    286  1.2  jmcneill 
    287  1.2  jmcneill static int
    288  1.2  jmcneill omap3_dpll_clock_set_rate(void *priv, struct clk *clk, u_int rate)
    289  1.2  jmcneill {
    290  1.2  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    291  1.2  jmcneill 	struct clk *clk_parent = clk_get_parent(clk);
    292  1.2  jmcneill 	uint64_t parent_rate;
    293  1.2  jmcneill 	uint32_t control, mult_div1;
    294  1.2  jmcneill 
    295  1.2  jmcneill 	if (clk_parent == NULL)
    296  1.2  jmcneill 		return ENXIO;
    297  1.2  jmcneill 
    298  1.2  jmcneill 	parent_rate = clk_get_rate(clk_parent);
    299  1.2  jmcneill 
    300  1.2  jmcneill 	const u_int div = (parent_rate / 1000000) - 1;
    301  1.2  jmcneill 	const u_int mult = rate / (parent_rate / (div + 1));
    302  1.2  jmcneill 	if (mult < 2 || mult > 2047)
    303  1.2  jmcneill 		return EINVAL;
    304  1.2  jmcneill 
    305  1.2  jmcneill 	control = RD4(sc, REG_CONTROL);
    306  1.2  jmcneill 	control &= ~OMAP3_EN_MPU_DPLL;
    307  1.2  jmcneill 	control |= __SHIFTIN(OMAP3_EN_MPU_DPLL_BYPASS, OMAP3_EN_MPU_DPLL);
    308  1.2  jmcneill 	WR4(sc, REG_CONTROL, control);
    309  1.2  jmcneill 
    310  1.2  jmcneill 	delay(10);
    311  1.2  jmcneill 
    312  1.2  jmcneill 	mult_div1 = __SHIFTIN(mult, DPLL_MULT);
    313  1.2  jmcneill 	mult_div1 |= __SHIFTIN(div, DPLL_DIV);
    314  1.2  jmcneill 	WR4(sc, REG_MULT_DIV1, mult_div1);
    315  1.2  jmcneill 
    316  1.2  jmcneill 	control &= ~OMAP3_EN_MPU_DPLL;
    317  1.2  jmcneill 	control |= __SHIFTIN(OMAP3_EN_MPU_DPLL_LOCK, OMAP3_EN_MPU_DPLL);
    318  1.1  jmcneill 	WR4(sc, REG_CONTROL, control);
    319  1.1  jmcneill 
    320  1.2  jmcneill 	while ((RD4(sc, REG_IDLEST) & OMAP3_ST_MPU_CLK) != 0)
    321  1.1  jmcneill 		;
    322  1.1  jmcneill 
    323  1.1  jmcneill 	return 0;
    324  1.1  jmcneill }
    325  1.1  jmcneill 
    326  1.2  jmcneill static u_int
    327  1.2  jmcneill omap3_dpll_core_clock_get_rate(void *priv, struct clk *clk)
    328  1.1  jmcneill {
    329  1.1  jmcneill 	struct ti_dpll_clock_softc * const sc = priv;
    330  1.2  jmcneill 	struct clk *clk_parent = clk_get_parent(clk);
    331  1.2  jmcneill 	uint32_t val;
    332  1.2  jmcneill 	uint64_t parent_rate;
    333  1.1  jmcneill 
    334  1.2  jmcneill 	if (clk_parent == NULL)
    335  1.2  jmcneill 		return 0;
    336  1.2  jmcneill 
    337  1.2  jmcneill 	val = RD4(sc, REG_MULT_DIV1);
    338  1.2  jmcneill 	const u_int mult = __SHIFTOUT(val, OMAP3_CORE_DPLL_MULT);
    339  1.2  jmcneill 	const u_int div = __SHIFTOUT(val, OMAP3_CORE_DPLL_DIV) + 1;
    340  1.2  jmcneill 	const u_int postdiv = __SHIFTOUT(val, OMAP3_CORE_DPLL_CLKOUT_DIV);
    341  1.2  jmcneill 
    342  1.2  jmcneill 	parent_rate = clk_get_rate(clk_parent);
    343  1.2  jmcneill 
    344  1.2  jmcneill 	return (u_int)((mult * parent_rate) / div) / postdiv;
    345  1.1  jmcneill }
    346