1*deeed1ddSjmcneill /* $NetBSD: sunxi_ccu_phase.c,v 1.1 2017/07/17 23:26:17 jmcneill Exp $ */
2*deeed1ddSjmcneill
3*deeed1ddSjmcneill /*-
4*deeed1ddSjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5*deeed1ddSjmcneill * All rights reserved.
6*deeed1ddSjmcneill *
7*deeed1ddSjmcneill * Redistribution and use in source and binary forms, with or without
8*deeed1ddSjmcneill * modification, are permitted provided that the following conditions
9*deeed1ddSjmcneill * are met:
10*deeed1ddSjmcneill * 1. Redistributions of source code must retain the above copyright
11*deeed1ddSjmcneill * notice, this list of conditions and the following disclaimer.
12*deeed1ddSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13*deeed1ddSjmcneill * notice, this list of conditions and the following disclaimer in the
14*deeed1ddSjmcneill * documentation and/or other materials provided with the distribution.
15*deeed1ddSjmcneill *
16*deeed1ddSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*deeed1ddSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*deeed1ddSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*deeed1ddSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*deeed1ddSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*deeed1ddSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*deeed1ddSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*deeed1ddSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*deeed1ddSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*deeed1ddSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*deeed1ddSjmcneill * SUCH DAMAGE.
27*deeed1ddSjmcneill */
28*deeed1ddSjmcneill
29*deeed1ddSjmcneill #include <sys/cdefs.h>
30*deeed1ddSjmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_ccu_phase.c,v 1.1 2017/07/17 23:26:17 jmcneill Exp $");
31*deeed1ddSjmcneill
32*deeed1ddSjmcneill #include <sys/param.h>
33*deeed1ddSjmcneill #include <sys/bus.h>
34*deeed1ddSjmcneill
35*deeed1ddSjmcneill #include <dev/clk/clk_backend.h>
36*deeed1ddSjmcneill
37*deeed1ddSjmcneill #include <arm/sunxi/sunxi_ccu.h>
38*deeed1ddSjmcneill
39*deeed1ddSjmcneill static u_int
sunxi_ccu_phase_get_parent_rate(struct clk * clkp)40*deeed1ddSjmcneill sunxi_ccu_phase_get_parent_rate(struct clk *clkp)
41*deeed1ddSjmcneill {
42*deeed1ddSjmcneill struct clk *clkp_parent;
43*deeed1ddSjmcneill
44*deeed1ddSjmcneill clkp_parent = clk_get_parent(clkp);
45*deeed1ddSjmcneill if (clkp_parent == NULL)
46*deeed1ddSjmcneill return 0;
47*deeed1ddSjmcneill
48*deeed1ddSjmcneill return clk_get_rate(clkp_parent);
49*deeed1ddSjmcneill }
50*deeed1ddSjmcneill
51*deeed1ddSjmcneill static u_int
sunxi_ccu_phase_div(u_int n,u_int d)52*deeed1ddSjmcneill sunxi_ccu_phase_div(u_int n, u_int d)
53*deeed1ddSjmcneill {
54*deeed1ddSjmcneill return (n + (d/2)) / d;
55*deeed1ddSjmcneill }
56*deeed1ddSjmcneill
57*deeed1ddSjmcneill u_int
sunxi_ccu_phase_get_rate(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk)58*deeed1ddSjmcneill sunxi_ccu_phase_get_rate(struct sunxi_ccu_softc *sc,
59*deeed1ddSjmcneill struct sunxi_ccu_clk *clk)
60*deeed1ddSjmcneill {
61*deeed1ddSjmcneill struct sunxi_ccu_phase *phase = &clk->u.phase;
62*deeed1ddSjmcneill struct clk *clkp = &clk->base;
63*deeed1ddSjmcneill u_int p_rate, gp_rate, p_div, delay;
64*deeed1ddSjmcneill uint32_t val;
65*deeed1ddSjmcneill
66*deeed1ddSjmcneill KASSERT(clk->type == SUNXI_CCU_PHASE);
67*deeed1ddSjmcneill
68*deeed1ddSjmcneill p_rate = sunxi_ccu_phase_get_parent_rate(clkp);
69*deeed1ddSjmcneill if (p_rate == 0)
70*deeed1ddSjmcneill return 0;
71*deeed1ddSjmcneill gp_rate = sunxi_ccu_phase_get_parent_rate(clk_get_parent(clkp));
72*deeed1ddSjmcneill if (gp_rate == 0)
73*deeed1ddSjmcneill return 0;
74*deeed1ddSjmcneill
75*deeed1ddSjmcneill p_div = gp_rate / p_rate;
76*deeed1ddSjmcneill
77*deeed1ddSjmcneill val = CCU_READ(sc, phase->reg);
78*deeed1ddSjmcneill delay = __SHIFTOUT(val, phase->mask);
79*deeed1ddSjmcneill
80*deeed1ddSjmcneill return delay * sunxi_ccu_phase_div(360, p_div);
81*deeed1ddSjmcneill }
82*deeed1ddSjmcneill
83*deeed1ddSjmcneill int
sunxi_ccu_phase_set_rate(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk,u_int new_rate)84*deeed1ddSjmcneill sunxi_ccu_phase_set_rate(struct sunxi_ccu_softc *sc,
85*deeed1ddSjmcneill struct sunxi_ccu_clk *clk, u_int new_rate)
86*deeed1ddSjmcneill {
87*deeed1ddSjmcneill struct sunxi_ccu_phase *phase = &clk->u.phase;
88*deeed1ddSjmcneill struct clk *clkp = &clk->base;
89*deeed1ddSjmcneill u_int p_rate, gp_rate, p_div, delay;
90*deeed1ddSjmcneill uint32_t val;
91*deeed1ddSjmcneill
92*deeed1ddSjmcneill KASSERT(clk->type == SUNXI_CCU_PHASE);
93*deeed1ddSjmcneill
94*deeed1ddSjmcneill clkp = &clk->base;
95*deeed1ddSjmcneill
96*deeed1ddSjmcneill p_rate = sunxi_ccu_phase_get_parent_rate(clkp);
97*deeed1ddSjmcneill if (p_rate == 0)
98*deeed1ddSjmcneill return 0;
99*deeed1ddSjmcneill gp_rate = sunxi_ccu_phase_get_parent_rate(clk_get_parent(clkp));
100*deeed1ddSjmcneill if (gp_rate == 0)
101*deeed1ddSjmcneill return 0;
102*deeed1ddSjmcneill
103*deeed1ddSjmcneill p_div = gp_rate / p_rate;
104*deeed1ddSjmcneill
105*deeed1ddSjmcneill delay = new_rate == 180 ? 0 :
106*deeed1ddSjmcneill sunxi_ccu_phase_div(new_rate,
107*deeed1ddSjmcneill sunxi_ccu_phase_div(360, p_div));
108*deeed1ddSjmcneill
109*deeed1ddSjmcneill val = CCU_READ(sc, phase->reg);
110*deeed1ddSjmcneill val &= ~phase->mask;
111*deeed1ddSjmcneill val |= __SHIFTIN(delay, phase->mask);
112*deeed1ddSjmcneill CCU_WRITE(sc, phase->reg, val);
113*deeed1ddSjmcneill
114*deeed1ddSjmcneill return 0;
115*deeed1ddSjmcneill }
116*deeed1ddSjmcneill
117*deeed1ddSjmcneill const char *
sunxi_ccu_phase_get_parent(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk)118*deeed1ddSjmcneill sunxi_ccu_phase_get_parent(struct sunxi_ccu_softc *sc,
119*deeed1ddSjmcneill struct sunxi_ccu_clk *clk)
120*deeed1ddSjmcneill {
121*deeed1ddSjmcneill struct sunxi_ccu_phase *phase = &clk->u.phase;
122*deeed1ddSjmcneill
123*deeed1ddSjmcneill KASSERT(clk->type == SUNXI_CCU_PHASE);
124*deeed1ddSjmcneill
125*deeed1ddSjmcneill return phase->parent;
126*deeed1ddSjmcneill }
127