Home | History | Annotate | Line # | Download | only in sunxi
      1 /* $NetBSD: sunxi_ccu_prediv.c,v 1.3 2017/08/25 00:07:03 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 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: sunxi_ccu_prediv.c,v 1.3 2017/08/25 00:07:03 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/sunxi/sunxi_ccu.h>
     38 
     39 u_int
     40 sunxi_ccu_prediv_get_rate(struct sunxi_ccu_softc *sc,
     41     struct sunxi_ccu_clk *clk)
     42 {
     43 	struct sunxi_ccu_prediv *prediv = &clk->u.prediv;
     44 	struct clk *clkp, *clkp_parent;
     45 	u_int rate, pre, div, sel;
     46 	uint32_t val;
     47 
     48 	KASSERT(clk->type == SUNXI_CCU_PREDIV);
     49 
     50 	clkp = &clk->base;
     51 	clkp_parent = clk_get_parent(clkp);
     52 	if (clkp_parent == NULL)
     53 		return 0;
     54 
     55 	rate = clk_get_rate(clkp_parent);
     56 	if (rate == 0)
     57 		return 0;
     58 
     59 	val = CCU_READ(sc, prediv->reg);
     60 	if (prediv->prediv)
     61 		pre = __SHIFTOUT(val, prediv->prediv);
     62 	else
     63 		pre = 0;
     64 	if (prediv->div)
     65 		div = __SHIFTOUT(val, prediv->div);
     66 	else
     67 		div = 0;
     68 	sel = __SHIFTOUT(val, prediv->sel);
     69 
     70 	if (prediv->flags & SUNXI_CCU_PREDIV_POWER_OF_TWO)
     71 		div = 1 << div;
     72 	else
     73 		div++;
     74 
     75 	if (prediv->prediv_fixed)
     76 		pre = prediv->prediv_fixed;
     77 	else
     78 		pre++;
     79 
     80 	if (prediv->flags & SUNXI_CCU_PREDIV_DIVIDE_BY_TWO)
     81 		pre *= 2;
     82 
     83 	if (prediv->prediv_sel & __BIT(sel))
     84 		return rate / pre / div;
     85 	else
     86 		return rate / div;
     87 }
     88 
     89 int
     90 sunxi_ccu_prediv_set_rate(struct sunxi_ccu_softc *sc,
     91     struct sunxi_ccu_clk *clk, u_int new_rate)
     92 {
     93 	return EINVAL;
     94 }
     95 
     96 int
     97 sunxi_ccu_prediv_set_parent(struct sunxi_ccu_softc *sc,
     98     struct sunxi_ccu_clk *clk, const char *name)
     99 {
    100 	struct sunxi_ccu_prediv *prediv = &clk->u.prediv;
    101 	uint32_t val;
    102 	u_int index;
    103 
    104 	KASSERT(clk->type == SUNXI_CCU_PREDIV);
    105 
    106 	if (prediv->sel == 0)
    107 		return ENODEV;
    108 
    109 	for (index = 0; index < prediv->nparents; index++) {
    110 		if (prediv->parents[index] != NULL &&
    111 		    strcmp(prediv->parents[index], name) == 0)
    112 			break;
    113 	}
    114 	if (index == prediv->nparents)
    115 		return EINVAL;
    116 
    117 	val = CCU_READ(sc, prediv->reg);
    118 	val &= ~prediv->sel;
    119 	val |= __SHIFTIN(index, prediv->sel);
    120 	CCU_WRITE(sc, prediv->reg, val);
    121 
    122 	return 0;
    123 }
    124 
    125 const char *
    126 sunxi_ccu_prediv_get_parent(struct sunxi_ccu_softc *sc,
    127     struct sunxi_ccu_clk *clk)
    128 {
    129 	struct sunxi_ccu_prediv *prediv = &clk->u.prediv;
    130 	u_int index;
    131 	uint32_t val;
    132 
    133 	KASSERT(clk->type == SUNXI_CCU_PREDIV);
    134 
    135 	val = CCU_READ(sc, prediv->reg);
    136 	index = __SHIFTOUT(val, prediv->sel);
    137 
    138 	return prediv->parents[index];
    139 }
    140