Home | History | Annotate | Line # | Download | only in rockchip
rk_cru_pll.c revision 1.2.2.2
      1  1.2.2.2  pgoyette /* $NetBSD: rk_cru_pll.c,v 1.2.2.2 2018/06/25 07:25:39 pgoyette Exp $ */
      2  1.2.2.2  pgoyette 
      3  1.2.2.2  pgoyette /*-
      4  1.2.2.2  pgoyette  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.2.2.2  pgoyette  * All rights reserved.
      6  1.2.2.2  pgoyette  *
      7  1.2.2.2  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.2.2.2  pgoyette  * modification, are permitted provided that the following conditions
      9  1.2.2.2  pgoyette  * are met:
     10  1.2.2.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.2.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.2.2.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.2.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.2.2.2  pgoyette  *
     16  1.2.2.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.2.2.2  pgoyette  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.2.2.2  pgoyette  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.2.2.2  pgoyette  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.2.2.2  pgoyette  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.2.2.2  pgoyette  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.2.2.2  pgoyette  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.2.2.2  pgoyette  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.2.2.2  pgoyette  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.2.2.2  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.2.2.2  pgoyette  * SUCH DAMAGE.
     27  1.2.2.2  pgoyette  */
     28  1.2.2.2  pgoyette 
     29  1.2.2.2  pgoyette #include <sys/cdefs.h>
     30  1.2.2.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: rk_cru_pll.c,v 1.2.2.2 2018/06/25 07:25:39 pgoyette Exp $");
     31  1.2.2.2  pgoyette 
     32  1.2.2.2  pgoyette #include <sys/param.h>
     33  1.2.2.2  pgoyette #include <sys/bus.h>
     34  1.2.2.2  pgoyette 
     35  1.2.2.2  pgoyette #include <dev/clk/clk_backend.h>
     36  1.2.2.2  pgoyette 
     37  1.2.2.2  pgoyette #include <arm/rockchip/rk_cru.h>
     38  1.2.2.2  pgoyette 
     39  1.2.2.2  pgoyette #define	PLL_CON0		0x00
     40  1.2.2.2  pgoyette #define	 PLL_BYPASS		__BIT(15)
     41  1.2.2.2  pgoyette #define	 PLL_POSTDIV1		__BITS(14,12)
     42  1.2.2.2  pgoyette #define	 PLL_FBDIV		__BITS(11,0)
     43  1.2.2.2  pgoyette 
     44  1.2.2.2  pgoyette #define	PLL_CON1		0x04
     45  1.2.2.2  pgoyette #define	 PLL_PDSEL		__BIT(15)
     46  1.2.2.2  pgoyette #define	 PLL_PD1		__BIT(14)
     47  1.2.2.2  pgoyette #define	 PLL_PD0		__BIT(13)
     48  1.2.2.2  pgoyette #define	 PLL_DSMPD		__BIT(12)
     49  1.2.2.2  pgoyette #define	 PLL_LOCK		__BIT(10)
     50  1.2.2.2  pgoyette #define	 PLL_POSTDIV2		__BITS(8,6)
     51  1.2.2.2  pgoyette #define	 PLL_REFDIV		__BITS(5,0)
     52  1.2.2.2  pgoyette 
     53  1.2.2.2  pgoyette #define	PLL_CON2		0x08
     54  1.2.2.2  pgoyette #define	 PLL_FOUT4PHASEPD	__BIT(27)
     55  1.2.2.2  pgoyette #define	 PLL_FOUTVCOPD		__BIT(26)
     56  1.2.2.2  pgoyette #define	 PLL_FOUTPOSTDIVPD	__BIT(25)
     57  1.2.2.2  pgoyette #define	 PLL_DACPD		__BIT(24)
     58  1.2.2.2  pgoyette #define	 PLL_FRACDIV		__BITS(23,0)
     59  1.2.2.2  pgoyette 
     60  1.2.2.2  pgoyette #define	PLL_WRITE_MASK		0xffff0000	/* for CON0 and CON1 */
     61  1.2.2.2  pgoyette 
     62  1.2.2.2  pgoyette #define	GRF_SOC_STATUS0		0x0480
     63  1.2.2.2  pgoyette 
     64  1.2.2.2  pgoyette u_int
     65  1.2.2.2  pgoyette rk_cru_pll_get_rate(struct rk_cru_softc *sc,
     66  1.2.2.2  pgoyette     struct rk_cru_clk *clk)
     67  1.2.2.2  pgoyette {
     68  1.2.2.2  pgoyette 	struct rk_cru_pll *pll = &clk->u.pll;
     69  1.2.2.2  pgoyette 	struct clk *clkp, *clkp_parent;
     70  1.2.2.2  pgoyette 	u_int foutvco, foutpostdiv;
     71  1.2.2.2  pgoyette 
     72  1.2.2.2  pgoyette 	KASSERT(clk->type == RK_CRU_PLL);
     73  1.2.2.2  pgoyette 
     74  1.2.2.2  pgoyette 	clkp = &clk->base;
     75  1.2.2.2  pgoyette 	clkp_parent = clk_get_parent(clkp);
     76  1.2.2.2  pgoyette 	if (clkp_parent == NULL)
     77  1.2.2.2  pgoyette 		return 0;
     78  1.2.2.2  pgoyette 
     79  1.2.2.2  pgoyette 	const u_int fref = clk_get_rate(clkp_parent);
     80  1.2.2.2  pgoyette 	if (fref == 0)
     81  1.2.2.2  pgoyette 		return 0;
     82  1.2.2.2  pgoyette 
     83  1.2.2.2  pgoyette 	const uint32_t con0 = CRU_READ(sc, pll->con_base + PLL_CON0);
     84  1.2.2.2  pgoyette 	const uint32_t con1 = CRU_READ(sc, pll->con_base + PLL_CON1);
     85  1.2.2.2  pgoyette 	const uint32_t con2 = CRU_READ(sc, pll->con_base + PLL_CON2);
     86  1.2.2.2  pgoyette 
     87  1.2.2.2  pgoyette 	const u_int postdiv1 = __SHIFTOUT(con0, PLL_POSTDIV1);
     88  1.2.2.2  pgoyette 	const u_int fbdiv = __SHIFTOUT(con0, PLL_FBDIV);
     89  1.2.2.2  pgoyette 	const u_int dsmpd = __SHIFTOUT(con1, PLL_DSMPD);
     90  1.2.2.2  pgoyette 	const u_int refdiv = __SHIFTOUT(con1, PLL_REFDIV);
     91  1.2.2.2  pgoyette 	const u_int postdiv2 = __SHIFTOUT(con1, PLL_POSTDIV2);
     92  1.2.2.2  pgoyette 	const u_int fracdiv = __SHIFTOUT(con2, PLL_FRACDIV);
     93  1.2.2.2  pgoyette 
     94  1.2.2.2  pgoyette 	if (dsmpd == 1) {
     95  1.2.2.2  pgoyette 		/* integer mode */
     96  1.2.2.2  pgoyette 		foutvco = fref / refdiv * fbdiv;
     97  1.2.2.2  pgoyette 	} else {
     98  1.2.2.2  pgoyette 		/* fractional mode */
     99  1.2.2.2  pgoyette 		foutvco = fref / refdiv * fbdiv + ((fref * fracdiv) >> 24);
    100  1.2.2.2  pgoyette 	}
    101  1.2.2.2  pgoyette 	foutpostdiv = foutvco / postdiv1 / postdiv2;
    102  1.2.2.2  pgoyette 
    103  1.2.2.2  pgoyette 	return foutpostdiv;
    104  1.2.2.2  pgoyette }
    105  1.2.2.2  pgoyette 
    106  1.2.2.2  pgoyette int
    107  1.2.2.2  pgoyette rk_cru_pll_set_rate(struct rk_cru_softc *sc,
    108  1.2.2.2  pgoyette     struct rk_cru_clk *clk, u_int rate)
    109  1.2.2.2  pgoyette {
    110  1.2.2.2  pgoyette 	struct rk_cru_pll *pll = &clk->u.pll;
    111  1.2.2.2  pgoyette 	const struct rk_cru_pll_rate *pll_rate = NULL;
    112  1.2.2.2  pgoyette 	uint32_t val;
    113  1.2.2.2  pgoyette 	int retry;
    114  1.2.2.2  pgoyette 
    115  1.2.2.2  pgoyette 	KASSERT(clk->type == RK_CRU_PLL);
    116  1.2.2.2  pgoyette 
    117  1.2.2.2  pgoyette 	if (pll->rates == NULL || rate == 0 || !HAS_GRF(sc))
    118  1.2.2.2  pgoyette 		return EIO;
    119  1.2.2.2  pgoyette 
    120  1.2.2.2  pgoyette 	for (int i = 0; i < pll->nrates; i++)
    121  1.2.2.2  pgoyette 		if (pll->rates[i].rate == rate) {
    122  1.2.2.2  pgoyette 			pll_rate = &pll->rates[i];
    123  1.2.2.2  pgoyette 			break;
    124  1.2.2.2  pgoyette 		}
    125  1.2.2.2  pgoyette 	if (pll_rate == NULL)
    126  1.2.2.2  pgoyette 		return EINVAL;
    127  1.2.2.2  pgoyette 
    128  1.2.2.2  pgoyette 	CRU_WRITE(sc, pll->con_base + PLL_CON0,
    129  1.2.2.2  pgoyette 	    __SHIFTIN(pll_rate->postdiv1, PLL_POSTDIV1) |
    130  1.2.2.2  pgoyette 	    __SHIFTIN(pll_rate->fbdiv, PLL_FBDIV) |
    131  1.2.2.2  pgoyette 	    PLL_WRITE_MASK);
    132  1.2.2.2  pgoyette 
    133  1.2.2.2  pgoyette 	CRU_WRITE(sc, pll->con_base + PLL_CON1,
    134  1.2.2.2  pgoyette 	    __SHIFTIN(pll_rate->dsmpd, PLL_DSMPD) |
    135  1.2.2.2  pgoyette 	    __SHIFTIN(pll_rate->postdiv2, PLL_POSTDIV2) |
    136  1.2.2.2  pgoyette 	    __SHIFTIN(pll_rate->refdiv, PLL_REFDIV) |
    137  1.2.2.2  pgoyette 	    PLL_WRITE_MASK);
    138  1.2.2.2  pgoyette 
    139  1.2.2.2  pgoyette 	val = CRU_READ(sc, pll->con_base + PLL_CON2);
    140  1.2.2.2  pgoyette 	val &= ~PLL_FRACDIV;
    141  1.2.2.2  pgoyette 	val |= __SHIFTIN(pll_rate->fracdiv, PLL_FRACDIV);
    142  1.2.2.2  pgoyette 	CRU_WRITE(sc, pll->con_base + PLL_CON2, val);
    143  1.2.2.2  pgoyette 
    144  1.2.2.2  pgoyette 	/* Set PLL work mode to normal */
    145  1.2.2.2  pgoyette 	const uint32_t write_mask = pll->mode_mask << 16;
    146  1.2.2.2  pgoyette 	const uint32_t write_val = pll->mode_mask;
    147  1.2.2.2  pgoyette 	CRU_WRITE(sc, pll->mode_reg, write_mask | write_val);
    148  1.2.2.2  pgoyette 
    149  1.2.2.2  pgoyette 	for (retry = 1000; retry > 0; retry--) {
    150  1.2.2.2  pgoyette 		if (GRF_READ(sc, GRF_SOC_STATUS0) & pll->lock_mask)
    151  1.2.2.2  pgoyette 			break;
    152  1.2.2.2  pgoyette 		delay(1);
    153  1.2.2.2  pgoyette 	}
    154  1.2.2.2  pgoyette 	if (retry == 0)
    155  1.2.2.2  pgoyette 		device_printf(sc->sc_dev, "WARNING: %s failed to lock\n",
    156  1.2.2.2  pgoyette 		    clk->base.name);
    157  1.2.2.2  pgoyette 
    158  1.2.2.2  pgoyette 	return 0;
    159  1.2.2.2  pgoyette }
    160  1.2.2.2  pgoyette 
    161  1.2.2.2  pgoyette const char *
    162  1.2.2.2  pgoyette rk_cru_pll_get_parent(struct rk_cru_softc *sc,
    163  1.2.2.2  pgoyette     struct rk_cru_clk *clk)
    164  1.2.2.2  pgoyette {
    165  1.2.2.2  pgoyette 	struct rk_cru_pll *pll = &clk->u.pll;
    166  1.2.2.2  pgoyette 
    167  1.2.2.2  pgoyette 	KASSERT(clk->type == RK_CRU_PLL);
    168  1.2.2.2  pgoyette 
    169  1.2.2.2  pgoyette 	return pll->parent;
    170  1.2.2.2  pgoyette }
    171