Home | History | Annotate | Line # | Download | only in amlogic
meson_clk_div.c revision 1.2.2.2
      1  1.2.2.2  pgoyette /* $NetBSD: meson_clk_div.c,v 1.2.2.2 2019/01/26 21:59:59 pgoyette Exp $ */
      2  1.2.2.2  pgoyette 
      3  1.2.2.2  pgoyette /*-
      4  1.2.2.2  pgoyette  * Copyright (c) 2017-2019 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: meson_clk_div.c,v 1.2.2.2 2019/01/26 21:59:59 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/amlogic/meson_clk.h>
     38  1.2.2.2  pgoyette 
     39  1.2.2.2  pgoyette u_int
     40  1.2.2.2  pgoyette meson_clk_div_get_rate(struct meson_clk_softc *sc,
     41  1.2.2.2  pgoyette     struct meson_clk_clk *clk)
     42  1.2.2.2  pgoyette {
     43  1.2.2.2  pgoyette 	struct meson_clk_div *div = &clk->u.div;
     44  1.2.2.2  pgoyette 	struct clk *clkp, *clkp_parent;
     45  1.2.2.2  pgoyette 	u_int rate, ratio;
     46  1.2.2.2  pgoyette 	uint32_t val;
     47  1.2.2.2  pgoyette 
     48  1.2.2.2  pgoyette 	KASSERT(clk->type == MESON_CLK_DIV);
     49  1.2.2.2  pgoyette 
     50  1.2.2.2  pgoyette 	clkp = &clk->base;
     51  1.2.2.2  pgoyette 	clkp_parent = clk_get_parent(clkp);
     52  1.2.2.2  pgoyette 	if (clkp_parent == NULL)
     53  1.2.2.2  pgoyette 		return 0;
     54  1.2.2.2  pgoyette 
     55  1.2.2.2  pgoyette 	rate = clk_get_rate(clkp_parent);
     56  1.2.2.2  pgoyette 	if (rate == 0)
     57  1.2.2.2  pgoyette 		return 0;
     58  1.2.2.2  pgoyette 
     59  1.2.2.2  pgoyette 	val = CLK_READ(sc, div->reg);
     60  1.2.2.2  pgoyette 	if (div->div)
     61  1.2.2.2  pgoyette 		ratio = __SHIFTOUT(val, div->div);
     62  1.2.2.2  pgoyette 	else
     63  1.2.2.2  pgoyette 		ratio = 0;
     64  1.2.2.2  pgoyette 
     65  1.2.2.2  pgoyette 	if (div->flags & MESON_CLK_DIV_POWER_OF_TWO) {
     66  1.2.2.2  pgoyette 		return rate >> ratio;
     67  1.2.2.2  pgoyette 	} else if (div->flags & MESON_CLK_DIV_CPU_SCALE_TABLE) {
     68  1.2.2.2  pgoyette 		if (ratio < 1 || ratio > 8)
     69  1.2.2.2  pgoyette 			return 0;
     70  1.2.2.2  pgoyette 		return rate / ((ratio + 1) * 2);
     71  1.2.2.2  pgoyette 	} else {
     72  1.2.2.2  pgoyette 		return rate / (ratio + 1);
     73  1.2.2.2  pgoyette 	}
     74  1.2.2.2  pgoyette }
     75  1.2.2.2  pgoyette 
     76  1.2.2.2  pgoyette int
     77  1.2.2.2  pgoyette meson_clk_div_set_rate(struct meson_clk_softc *sc,
     78  1.2.2.2  pgoyette     struct meson_clk_clk *clk, u_int new_rate)
     79  1.2.2.2  pgoyette {
     80  1.2.2.2  pgoyette 	struct meson_clk_div *div = &clk->u.div;
     81  1.2.2.2  pgoyette 	struct clk *clkp, *clkp_parent;
     82  1.2.2.2  pgoyette 	int parent_rate;
     83  1.2.2.2  pgoyette 	uint32_t val, raw_div;
     84  1.2.2.2  pgoyette 	int ratio;
     85  1.2.2.2  pgoyette 
     86  1.2.2.2  pgoyette 	KASSERT(clk->type == MESON_CLK_DIV);
     87  1.2.2.2  pgoyette 
     88  1.2.2.2  pgoyette 	clkp = &clk->base;
     89  1.2.2.2  pgoyette 	clkp_parent = clk_get_parent(clkp);
     90  1.2.2.2  pgoyette 	if (clkp_parent == NULL)
     91  1.2.2.2  pgoyette 		return ENXIO;
     92  1.2.2.2  pgoyette 
     93  1.2.2.2  pgoyette 	if ((div->flags & MESON_CLK_DIV_SET_RATE_PARENT) != 0)
     94  1.2.2.2  pgoyette 		return clk_set_rate(clkp_parent, new_rate);
     95  1.2.2.2  pgoyette 
     96  1.2.2.2  pgoyette 	if (div->div == 0)
     97  1.2.2.2  pgoyette 		return ENXIO;
     98  1.2.2.2  pgoyette 
     99  1.2.2.2  pgoyette 	val = CLK_READ(sc, div->reg);
    100  1.2.2.2  pgoyette 
    101  1.2.2.2  pgoyette 	parent_rate = clk_get_rate(clkp_parent);
    102  1.2.2.2  pgoyette 	if (parent_rate == 0)
    103  1.2.2.2  pgoyette 		return (new_rate == 0) ? 0 : ERANGE;
    104  1.2.2.2  pgoyette 
    105  1.2.2.2  pgoyette 	ratio = howmany(parent_rate, new_rate);
    106  1.2.2.2  pgoyette 	if ((div->flags & MESON_CLK_DIV_POWER_OF_TWO) != 0) {
    107  1.2.2.2  pgoyette 		return EINVAL;
    108  1.2.2.2  pgoyette 	} else if ((div->flags & MESON_CLK_DIV_CPU_SCALE_TABLE) != 0) {
    109  1.2.2.2  pgoyette 		return EINVAL;
    110  1.2.2.2  pgoyette 	} else {
    111  1.2.2.2  pgoyette 		raw_div = (ratio > 0) ? ratio - 1 : 0;
    112  1.2.2.2  pgoyette 	}
    113  1.2.2.2  pgoyette 	if (raw_div > __SHIFTOUT_MASK(div->div))
    114  1.2.2.2  pgoyette 		return ERANGE;
    115  1.2.2.2  pgoyette 
    116  1.2.2.2  pgoyette 	val &= ~div->div;
    117  1.2.2.2  pgoyette 	val |= __SHIFTIN(raw_div, div->div);
    118  1.2.2.2  pgoyette 	CLK_WRITE(sc, div->reg, val);
    119  1.2.2.2  pgoyette 
    120  1.2.2.2  pgoyette 	return 0;
    121  1.2.2.2  pgoyette }
    122  1.2.2.2  pgoyette 
    123  1.2.2.2  pgoyette const char *
    124  1.2.2.2  pgoyette meson_clk_div_get_parent(struct meson_clk_softc *sc,
    125  1.2.2.2  pgoyette     struct meson_clk_clk *clk)
    126  1.2.2.2  pgoyette {
    127  1.2.2.2  pgoyette 	struct meson_clk_div *div = &clk->u.div;
    128  1.2.2.2  pgoyette 
    129  1.2.2.2  pgoyette 	KASSERT(clk->type == MESON_CLK_DIV);
    130  1.2.2.2  pgoyette 
    131  1.2.2.2  pgoyette 	return div->parent;
    132  1.2.2.2  pgoyette }
    133