Home | History | Annotate | Line # | Download | only in rockchip
rk_cru_composite.c revision 1.4
      1 /* $NetBSD: rk_cru_composite.c,v 1.4 2019/11/10 11:43:04 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: rk_cru_composite.c,v 1.4 2019/11/10 11:43:04 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 
     35 #include <dev/clk/clk_backend.h>
     36 
     37 #include <arm/rockchip/rk_cru.h>
     38 
     39 #include <dev/fdt/fdtvar.h>
     40 
     41 int
     42 rk_cru_composite_enable(struct rk_cru_softc *sc, struct rk_cru_clk *clk,
     43     int enable)
     44 {
     45 	struct rk_cru_composite *composite = &clk->u.composite;
     46 
     47 	KASSERT(clk->type == RK_CRU_COMPOSITE);
     48 
     49 	if (composite->gate_mask == 0)
     50 		return enable ? 0 : ENXIO;
     51 
     52 	const uint32_t write_mask = composite->gate_mask << 16;
     53 	const uint32_t write_val = enable ? 0 : composite->gate_mask;
     54 
     55 	CRU_WRITE(sc, composite->gate_reg, write_mask | write_val);
     56 
     57 	return 0;
     58 }
     59 
     60 u_int
     61 rk_cru_composite_get_rate(struct rk_cru_softc *sc,
     62     struct rk_cru_clk *clk)
     63 {
     64 	struct rk_cru_composite *composite = &clk->u.composite;
     65 	struct clk *clkp, *clkp_parent;
     66 
     67 	KASSERT(clk->type == RK_CRU_COMPOSITE);
     68 
     69 	clkp = &clk->base;
     70 	clkp_parent = clk_get_parent(clkp);
     71 	if (clkp_parent == NULL)
     72 		return 0;
     73 
     74 	const u_int prate = clk_get_rate(clkp_parent);
     75 	if (prate == 0)
     76 		return 0;
     77 
     78 	const uint32_t val = CRU_READ(sc, composite->muxdiv_reg);
     79 	const u_int div = __SHIFTOUT(val, composite->div_mask) + 1;
     80 
     81 	return prate / div;
     82 }
     83 
     84 int
     85 rk_cru_composite_set_rate(struct rk_cru_softc *sc,
     86     struct rk_cru_clk *clk, u_int rate)
     87 {
     88 	struct rk_cru_composite *composite = &clk->u.composite;
     89 	u_int best_div, best_mux, best_diff;
     90 	struct rk_cru_clk *rclk_parent;
     91 	struct clk *clk_parent;
     92 
     93 	KASSERT(clk->type == RK_CRU_COMPOSITE);
     94 
     95 	if (composite->flags & RK_COMPOSITE_SET_RATE_PARENT) {
     96 		clk_parent = clk_get_parent(&clk->base);
     97 		if (clk_parent == NULL)
     98 			return ENXIO;
     99 		return clk_set_rate(clk_parent, rate);
    100 	}
    101 
    102 	best_div = 0;
    103 	best_mux = 0;
    104 	best_diff = INT_MAX;
    105 	for (u_int mux = 0; mux < composite->nparents; mux++) {
    106 		rclk_parent = rk_cru_clock_find(sc, composite->parents[mux]);
    107 		if (rclk_parent != NULL)
    108 			clk_parent = &rclk_parent->base;
    109 		else
    110 			clk_parent = fdtbus_clock_byname(composite->parents[mux]);
    111 		if (clk_parent == NULL)
    112 			continue;
    113 
    114 		const u_int prate = clk_get_rate(clk_parent);
    115 		if (prate == 0)
    116 			continue;
    117 
    118 		for (u_int div = 1; div <= __SHIFTOUT_MASK(composite->div_mask) + 1; div++) {
    119 			const u_int cur_rate = prate / div;
    120 			const int diff = (int)rate - (int)cur_rate;
    121 			if (composite->flags & RK_COMPOSITE_ROUND_DOWN) {
    122 				if (diff >= 0 && diff < best_diff) {
    123 					best_diff = diff;
    124 					best_mux = mux;
    125 					best_div = div;
    126 				}
    127 			} else {
    128 				if (abs(diff) < best_diff) {
    129 					best_diff = abs(diff);
    130 					best_mux = mux;
    131 					best_div = div;
    132 				}
    133 			}
    134 		}
    135 	}
    136 	if (best_diff == INT_MAX)
    137 		return ERANGE;
    138 
    139 	uint32_t write_mask = composite->div_mask << 16;
    140 	uint32_t write_val = __SHIFTIN(best_div - 1, composite->div_mask);
    141 	if (composite->mux_mask) {
    142 		write_mask |= composite->mux_mask << 16;
    143 		write_val |= __SHIFTIN(best_mux, composite->mux_mask);
    144 	}
    145 
    146 	CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
    147 
    148 	return 0;
    149 }
    150 
    151 const char *
    152 rk_cru_composite_get_parent(struct rk_cru_softc *sc,
    153     struct rk_cru_clk *clk)
    154 {
    155 	struct rk_cru_composite *composite = &clk->u.composite;
    156 	uint32_t val;
    157 	u_int mux;
    158 
    159 	KASSERT(clk->type == RK_CRU_COMPOSITE);
    160 
    161 	if (composite->mux_mask) {
    162 		val = CRU_READ(sc, composite->muxdiv_reg);
    163 		mux = __SHIFTOUT(val, composite->mux_mask);
    164 	} else {
    165 		mux = 0;
    166 	}
    167 
    168 	return composite->parents[mux];
    169 }
    170 
    171 int
    172 rk_cru_composite_set_parent(struct rk_cru_softc *sc,
    173     struct rk_cru_clk *clk, const char *parent)
    174 {
    175 	struct rk_cru_composite *composite = &clk->u.composite;
    176 
    177 	KASSERT(clk->type == RK_CRU_COMPOSITE);
    178 
    179 	if (!composite->mux_mask)
    180 		return EINVAL;
    181 
    182 	for (u_int mux = 0; mux < composite->nparents; mux++) {
    183 		if (strcmp(composite->parents[mux], parent) == 0) {
    184 			const uint32_t write_mask = composite->mux_mask << 16;
    185 			const uint32_t write_val = __SHIFTIN(mux, composite->mux_mask);
    186 
    187 			CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
    188 			return 0;
    189 		}
    190 	}
    191 
    192 	return EINVAL;
    193 }
    194