Home | History | Annotate | Line # | Download | only in rockchip
rk_cru_arm.c revision 1.1
      1 /* $NetBSD: rk_cru_arm.c,v 1.1 2018/06/16 00:19: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_arm.c,v 1.1 2018/06/16 00:19: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 u_int
     40 rk_cru_arm_get_rate(struct rk_cru_softc *sc,
     41     struct rk_cru_clk *clk)
     42 {
     43 	struct rk_cru_arm *arm = &clk->u.arm;
     44 	struct clk *clkp, *clkp_parent;
     45 
     46 	KASSERT(clk->type == RK_CRU_ARM);
     47 
     48 	clkp = &clk->base;
     49 	clkp_parent = clk_get_parent(clkp);
     50 	if (clkp_parent == NULL)
     51 		return 0;
     52 
     53 	const u_int fref = clk_get_rate(clkp_parent);
     54 	if (fref == 0)
     55 		return 0;
     56 
     57 	const uint32_t val = CRU_READ(sc, arm->reg);
     58 	const u_int div = __SHIFTOUT(val, arm->div_mask) + 1;
     59 
     60 	return fref / div;
     61 }
     62 
     63 int
     64 rk_cru_arm_set_rate(struct rk_cru_softc *sc,
     65     struct rk_cru_clk *clk, u_int rate)
     66 {
     67 	struct rk_cru_arm *arm = &clk->u.arm;
     68 	const struct rk_cru_arm_rate *arm_rate = NULL;
     69 	struct clk *clkp_parent;
     70 	int error;
     71 
     72 	KASSERT(clk->type == RK_CRU_ARM);
     73 
     74 	if (arm->rates == NULL || rate == 0)
     75 		return EIO;
     76 
     77 	for (int i = 0; i < arm->nrates; i++)
     78 		if (arm->rates[i].rate == rate) {
     79 			arm_rate = &arm->rates[i];
     80 			break;
     81 		}
     82 	if (arm_rate == NULL)
     83 		return EINVAL;
     84 
     85 	error = rk_cru_arm_set_parent(sc, clk, arm->parents[arm->mux_main]);
     86 	if (error != 0)
     87 		return error;
     88 
     89 	clkp_parent = clk_get_parent(&clk->base);
     90 	if (clkp_parent == NULL)
     91 		return ENXIO;
     92 
     93 	const u_int parent_rate = arm_rate->rate / arm_rate->div;
     94 
     95 	error = clk_set_rate(clkp_parent, parent_rate);
     96 	if (error != 0)
     97 		return error;
     98 
     99 	const uint32_t write_mask = arm->div_mask << 16;
    100 	const uint32_t write_val = __SHIFTIN(arm_rate->div - 1, arm->div_mask);
    101 
    102 	CRU_WRITE(sc, arm->reg, write_mask | write_val);
    103 
    104 	return 0;
    105 }
    106 
    107 const char *
    108 rk_cru_arm_get_parent(struct rk_cru_softc *sc,
    109     struct rk_cru_clk *clk)
    110 {
    111 	struct rk_cru_arm *arm = &clk->u.arm;
    112 
    113 	KASSERT(clk->type == RK_CRU_ARM);
    114 
    115 	const uint32_t val = CRU_READ(sc, arm->reg);
    116 	const u_int mux = __SHIFTOUT(val, arm->mux_mask);
    117 
    118 	return arm->parents[mux];
    119 }
    120 
    121 int
    122 rk_cru_arm_set_parent(struct rk_cru_softc *sc,
    123     struct rk_cru_clk *clk, const char *parent)
    124 {
    125 	struct rk_cru_arm *arm = &clk->u.arm;
    126 
    127 	KASSERT(clk->type == RK_CRU_ARM);
    128 
    129 	for (u_int mux = 0; mux < arm->nparents; mux++)
    130 		if (strcmp(arm->parents[mux], parent) == 0) {
    131 			const uint32_t write_mask = arm->mux_mask << 16;
    132 			const uint32_t write_val = __SHIFTIN(mux, arm->mux_mask);
    133 
    134 			CRU_WRITE(sc, arm->reg, write_mask | write_val);
    135 			return 0;
    136 		}
    137 
    138 	return EINVAL;
    139 }
    140