Home | History | Annotate | Line # | Download | only in rockchip
rk_cru_composite.c revision 1.3.4.1
      1  1.3.4.1    martin /* $NetBSD: rk_cru_composite.c,v 1.3.4.1 2020/04/13 08:03:37 martin Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2018 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.4.1    martin __KERNEL_RCSID(0, "$NetBSD: rk_cru_composite.c,v 1.3.4.1 2020/04/13 08:03:37 martin 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 
     35      1.1  jmcneill #include <dev/clk/clk_backend.h>
     36      1.1  jmcneill 
     37      1.1  jmcneill #include <arm/rockchip/rk_cru.h>
     38      1.1  jmcneill 
     39      1.3  jmcneill #include <dev/fdt/fdtvar.h>
     40      1.3  jmcneill 
     41      1.1  jmcneill int
     42      1.1  jmcneill rk_cru_composite_enable(struct rk_cru_softc *sc, struct rk_cru_clk *clk,
     43      1.1  jmcneill     int enable)
     44      1.1  jmcneill {
     45      1.1  jmcneill 	struct rk_cru_composite *composite = &clk->u.composite;
     46      1.1  jmcneill 
     47      1.1  jmcneill 	KASSERT(clk->type == RK_CRU_COMPOSITE);
     48      1.1  jmcneill 
     49      1.1  jmcneill 	if (composite->gate_mask == 0)
     50      1.1  jmcneill 		return enable ? 0 : ENXIO;
     51      1.1  jmcneill 
     52      1.1  jmcneill 	const uint32_t write_mask = composite->gate_mask << 16;
     53      1.2  jmcneill 	const uint32_t write_val = enable ? 0 : composite->gate_mask;
     54      1.1  jmcneill 
     55      1.1  jmcneill 	CRU_WRITE(sc, composite->gate_reg, write_mask | write_val);
     56      1.1  jmcneill 
     57      1.1  jmcneill 	return 0;
     58      1.1  jmcneill }
     59      1.1  jmcneill 
     60      1.1  jmcneill u_int
     61      1.1  jmcneill rk_cru_composite_get_rate(struct rk_cru_softc *sc,
     62      1.1  jmcneill     struct rk_cru_clk *clk)
     63      1.1  jmcneill {
     64      1.1  jmcneill 	struct rk_cru_composite *composite = &clk->u.composite;
     65      1.1  jmcneill 	struct clk *clkp, *clkp_parent;
     66      1.1  jmcneill 
     67      1.1  jmcneill 	KASSERT(clk->type == RK_CRU_COMPOSITE);
     68      1.1  jmcneill 
     69      1.1  jmcneill 	clkp = &clk->base;
     70      1.1  jmcneill 	clkp_parent = clk_get_parent(clkp);
     71      1.1  jmcneill 	if (clkp_parent == NULL)
     72      1.1  jmcneill 		return 0;
     73      1.1  jmcneill 
     74      1.1  jmcneill 	const u_int prate = clk_get_rate(clkp_parent);
     75      1.1  jmcneill 	if (prate == 0)
     76      1.1  jmcneill 		return 0;
     77      1.1  jmcneill 
     78  1.3.4.1    martin 	if (composite->flags & RK_COMPOSITE_FRACDIV) {
     79  1.3.4.1    martin 		const uint32_t val = CRU_READ(sc, composite->frac_reg);
     80  1.3.4.1    martin 		const u_int num = (val >> 16) & 0xffff;
     81  1.3.4.1    martin 		const u_int den = val & 0xffff;
     82      1.1  jmcneill 
     83  1.3.4.1    martin 		return (u_int)((uint64_t)prate * num / den);
     84  1.3.4.1    martin 	} else {
     85  1.3.4.1    martin 		const uint32_t val = CRU_READ(sc, composite->muxdiv_reg);
     86  1.3.4.1    martin 		const u_int div = __SHIFTOUT(val, composite->div_mask) + 1;
     87  1.3.4.1    martin 
     88  1.3.4.1    martin 		return prate / div;
     89  1.3.4.1    martin 	}
     90  1.3.4.1    martin }
     91  1.3.4.1    martin 
     92  1.3.4.1    martin static u_int
     93  1.3.4.1    martin rk_cru_composite_get_frac_div(u_int n, u_int d)
     94  1.3.4.1    martin {
     95  1.3.4.1    martin 	u_int tmp;
     96  1.3.4.1    martin 
     97  1.3.4.1    martin 	while (d > 0) {
     98  1.3.4.1    martin 		tmp = d;
     99  1.3.4.1    martin 		d = n % d;
    100  1.3.4.1    martin 		n = tmp;
    101  1.3.4.1    martin 	}
    102  1.3.4.1    martin 
    103  1.3.4.1    martin 	return n;
    104  1.3.4.1    martin }
    105  1.3.4.1    martin 
    106  1.3.4.1    martin static int
    107  1.3.4.1    martin rk_cru_composite_set_rate_frac(struct rk_cru_softc *sc,
    108  1.3.4.1    martin     struct rk_cru_clk *clk, u_int rate)
    109  1.3.4.1    martin {
    110  1.3.4.1    martin 	struct rk_cru_composite *composite = &clk->u.composite;
    111  1.3.4.1    martin 	struct clk *clk_parent;
    112  1.3.4.1    martin 
    113  1.3.4.1    martin 	clk_parent = clk_get_parent(&clk->base);
    114  1.3.4.1    martin 	if (clk_parent == NULL)
    115  1.3.4.1    martin 		return ENXIO;
    116  1.3.4.1    martin 
    117  1.3.4.1    martin 	const u_int prate = clk_get_rate(clk_parent);
    118  1.3.4.1    martin 	const u_int v = rk_cru_composite_get_frac_div(prate, rate);
    119  1.3.4.1    martin 	const u_int num = (prate / v) & 0xffff;
    120  1.3.4.1    martin 	const u_int den = (rate / v) & 0xffff;
    121  1.3.4.1    martin 	if (prate / num * den != rate)
    122  1.3.4.1    martin 		return EINVAL;
    123  1.3.4.1    martin 
    124  1.3.4.1    martin 	CRU_WRITE(sc, composite->frac_reg, (den << 16) | num);
    125  1.3.4.1    martin 
    126  1.3.4.1    martin 	return 0;
    127      1.1  jmcneill }
    128      1.1  jmcneill 
    129      1.1  jmcneill int
    130      1.1  jmcneill rk_cru_composite_set_rate(struct rk_cru_softc *sc,
    131      1.1  jmcneill     struct rk_cru_clk *clk, u_int rate)
    132      1.1  jmcneill {
    133      1.1  jmcneill 	struct rk_cru_composite *composite = &clk->u.composite;
    134      1.1  jmcneill 	u_int best_div, best_mux, best_diff;
    135      1.3  jmcneill 	struct rk_cru_clk *rclk_parent;
    136      1.3  jmcneill 	struct clk *clk_parent;
    137      1.1  jmcneill 
    138      1.1  jmcneill 	KASSERT(clk->type == RK_CRU_COMPOSITE);
    139      1.1  jmcneill 
    140  1.3.4.1    martin 	if (composite->flags & RK_COMPOSITE_SET_RATE_PARENT) {
    141  1.3.4.1    martin 		clk_parent = clk_get_parent(&clk->base);
    142  1.3.4.1    martin 		if (clk_parent == NULL)
    143  1.3.4.1    martin 			return ENXIO;
    144  1.3.4.1    martin 		return clk_set_rate(clk_parent, rate);
    145  1.3.4.1    martin 	}
    146  1.3.4.1    martin 
    147  1.3.4.1    martin 	if (composite->flags & RK_COMPOSITE_FRACDIV) {
    148  1.3.4.1    martin 		return rk_cru_composite_set_rate_frac(sc, clk, rate);
    149  1.3.4.1    martin 	}
    150  1.3.4.1    martin 
    151      1.1  jmcneill 	best_div = 0;
    152      1.1  jmcneill 	best_mux = 0;
    153      1.1  jmcneill 	best_diff = INT_MAX;
    154      1.1  jmcneill 	for (u_int mux = 0; mux < composite->nparents; mux++) {
    155      1.3  jmcneill 		rclk_parent = rk_cru_clock_find(sc, composite->parents[mux]);
    156      1.3  jmcneill 		if (rclk_parent != NULL)
    157      1.3  jmcneill 			clk_parent = &rclk_parent->base;
    158      1.3  jmcneill 		else
    159      1.3  jmcneill 			clk_parent = fdtbus_clock_byname(composite->parents[mux]);
    160      1.1  jmcneill 		if (clk_parent == NULL)
    161      1.1  jmcneill 			continue;
    162      1.3  jmcneill 
    163      1.3  jmcneill 		const u_int prate = clk_get_rate(clk_parent);
    164      1.1  jmcneill 		if (prate == 0)
    165      1.1  jmcneill 			continue;
    166      1.1  jmcneill 
    167      1.1  jmcneill 		for (u_int div = 1; div <= __SHIFTOUT_MASK(composite->div_mask) + 1; div++) {
    168      1.1  jmcneill 			const u_int cur_rate = prate / div;
    169      1.1  jmcneill 			const int diff = (int)rate - (int)cur_rate;
    170      1.1  jmcneill 			if (composite->flags & RK_COMPOSITE_ROUND_DOWN) {
    171      1.1  jmcneill 				if (diff >= 0 && diff < best_diff) {
    172      1.1  jmcneill 					best_diff = diff;
    173      1.1  jmcneill 					best_mux = mux;
    174      1.1  jmcneill 					best_div = div;
    175      1.1  jmcneill 				}
    176      1.1  jmcneill 			} else {
    177      1.1  jmcneill 				if (abs(diff) < best_diff) {
    178      1.1  jmcneill 					best_diff = abs(diff);
    179      1.1  jmcneill 					best_mux = mux;
    180      1.1  jmcneill 					best_div = div;
    181      1.1  jmcneill 				}
    182      1.1  jmcneill 			}
    183      1.1  jmcneill 		}
    184      1.1  jmcneill 	}
    185      1.1  jmcneill 	if (best_diff == INT_MAX)
    186      1.1  jmcneill 		return ERANGE;
    187      1.1  jmcneill 
    188      1.1  jmcneill 	uint32_t write_mask = composite->div_mask << 16;
    189      1.1  jmcneill 	uint32_t write_val = __SHIFTIN(best_div - 1, composite->div_mask);
    190      1.1  jmcneill 	if (composite->mux_mask) {
    191      1.1  jmcneill 		write_mask |= composite->mux_mask << 16;
    192      1.1  jmcneill 		write_val |= __SHIFTIN(best_mux, composite->mux_mask);
    193      1.1  jmcneill 	}
    194      1.1  jmcneill 
    195      1.1  jmcneill 	CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
    196      1.1  jmcneill 
    197      1.1  jmcneill 	return 0;
    198      1.1  jmcneill }
    199      1.1  jmcneill 
    200      1.1  jmcneill const char *
    201      1.1  jmcneill rk_cru_composite_get_parent(struct rk_cru_softc *sc,
    202      1.1  jmcneill     struct rk_cru_clk *clk)
    203      1.1  jmcneill {
    204      1.1  jmcneill 	struct rk_cru_composite *composite = &clk->u.composite;
    205      1.1  jmcneill 	uint32_t val;
    206      1.1  jmcneill 	u_int mux;
    207      1.1  jmcneill 
    208      1.1  jmcneill 	KASSERT(clk->type == RK_CRU_COMPOSITE);
    209      1.1  jmcneill 
    210      1.1  jmcneill 	if (composite->mux_mask) {
    211      1.1  jmcneill 		val = CRU_READ(sc, composite->muxdiv_reg);
    212      1.1  jmcneill 		mux = __SHIFTOUT(val, composite->mux_mask);
    213      1.1  jmcneill 	} else {
    214      1.1  jmcneill 		mux = 0;
    215      1.1  jmcneill 	}
    216      1.1  jmcneill 
    217      1.1  jmcneill 	return composite->parents[mux];
    218      1.1  jmcneill }
    219      1.1  jmcneill 
    220      1.1  jmcneill int
    221      1.1  jmcneill rk_cru_composite_set_parent(struct rk_cru_softc *sc,
    222      1.1  jmcneill     struct rk_cru_clk *clk, const char *parent)
    223      1.1  jmcneill {
    224      1.1  jmcneill 	struct rk_cru_composite *composite = &clk->u.composite;
    225      1.1  jmcneill 
    226      1.1  jmcneill 	KASSERT(clk->type == RK_CRU_COMPOSITE);
    227      1.1  jmcneill 
    228      1.1  jmcneill 	if (!composite->mux_mask)
    229      1.1  jmcneill 		return EINVAL;
    230      1.1  jmcneill 
    231      1.1  jmcneill 	for (u_int mux = 0; mux < composite->nparents; mux++) {
    232      1.1  jmcneill 		if (strcmp(composite->parents[mux], parent) == 0) {
    233      1.1  jmcneill 			const uint32_t write_mask = composite->mux_mask << 16;
    234      1.1  jmcneill 			const uint32_t write_val = __SHIFTIN(mux, composite->mux_mask);
    235      1.1  jmcneill 
    236      1.1  jmcneill 			CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
    237      1.1  jmcneill 			return 0;
    238      1.1  jmcneill 		}
    239      1.1  jmcneill 	}
    240      1.1  jmcneill 
    241      1.1  jmcneill 	return EINVAL;
    242      1.1  jmcneill }
    243