sunxi_ccu_fractional.c revision 1.1 1 1.1 bouyer /* $NetBSD: sunxi_ccu_fractional.c,v 1.1 2018/03/19 16:18:30 bouyer Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*-
4 1.1 bouyer * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 bouyer * All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.1 bouyer *
16 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 bouyer * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 bouyer * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 bouyer * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 bouyer * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 bouyer * SUCH DAMAGE.
27 1.1 bouyer */
28 1.1 bouyer
29 1.1 bouyer #include <sys/cdefs.h>
30 1.1 bouyer __KERNEL_RCSID(0, "$NetBSD: sunxi_ccu_fractional.c,v 1.1 2018/03/19 16:18:30 bouyer Exp $");
31 1.1 bouyer
32 1.1 bouyer #include <sys/param.h>
33 1.1 bouyer #include <sys/bus.h>
34 1.1 bouyer
35 1.1 bouyer #include <dev/clk/clk_backend.h>
36 1.1 bouyer
37 1.1 bouyer #include <arm/sunxi/sunxi_ccu.h>
38 1.1 bouyer
39 1.1 bouyer int
40 1.1 bouyer sunxi_ccu_fractional_enable(struct sunxi_ccu_softc *sc,
41 1.1 bouyer struct sunxi_ccu_clk *clk, int enable)
42 1.1 bouyer {
43 1.1 bouyer struct sunxi_ccu_fractional *fractional = &clk->u.fractional;
44 1.1 bouyer uint32_t val;
45 1.1 bouyer
46 1.1 bouyer KASSERT(clk->type == SUNXI_CCU_FRACTIONAL);
47 1.1 bouyer
48 1.1 bouyer if (!fractional->enable)
49 1.1 bouyer return enable ? 0 : EINVAL;
50 1.1 bouyer
51 1.1 bouyer val = CCU_READ(sc, fractional->reg);
52 1.1 bouyer if (enable)
53 1.1 bouyer val |= fractional->enable;
54 1.1 bouyer else
55 1.1 bouyer val &= ~fractional->enable;
56 1.1 bouyer CCU_WRITE(sc, fractional->reg, val);
57 1.1 bouyer
58 1.1 bouyer return 0;
59 1.1 bouyer }
60 1.1 bouyer
61 1.1 bouyer u_int
62 1.1 bouyer sunxi_ccu_fractional_get_rate(struct sunxi_ccu_softc *sc,
63 1.1 bouyer struct sunxi_ccu_clk *clk)
64 1.1 bouyer {
65 1.1 bouyer struct sunxi_ccu_fractional *fractional = &clk->u.fractional;
66 1.1 bouyer struct clk *clkp, *clkp_parent;
67 1.1 bouyer u_int rate, m;
68 1.1 bouyer uint32_t val;
69 1.1 bouyer
70 1.1 bouyer KASSERT(clk->type == SUNXI_CCU_FRACTIONAL);
71 1.1 bouyer
72 1.1 bouyer clkp = &clk->base;
73 1.1 bouyer clkp_parent = clk_get_parent(clkp);
74 1.1 bouyer if (clkp_parent == NULL)
75 1.1 bouyer return 0;
76 1.1 bouyer
77 1.1 bouyer rate = clk_get_rate(clkp_parent);
78 1.1 bouyer if (rate == 0)
79 1.1 bouyer return 0;
80 1.1 bouyer
81 1.1 bouyer if (fractional->prediv > 0)
82 1.1 bouyer rate = rate / fractional->prediv;
83 1.1 bouyer
84 1.1 bouyer val = CCU_READ(sc, fractional->reg);
85 1.1 bouyer
86 1.1 bouyer if (fractional->enable && !(val & fractional->enable))
87 1.1 bouyer return 0;
88 1.1 bouyer
89 1.1 bouyer if (val & fractional->frac_en) {
90 1.1 bouyer int sel = __SHIFTOUT(val, fractional->frac_sel);
91 1.1 bouyer return fractional->frac[sel];
92 1.1 bouyer }
93 1.1 bouyer m = __SHIFTOUT(val, fractional->m);
94 1.1 bouyer
95 1.1 bouyer return rate * m;
96 1.1 bouyer }
97 1.1 bouyer
98 1.1 bouyer int
99 1.1 bouyer sunxi_ccu_fractional_set_rate(struct sunxi_ccu_softc *sc,
100 1.1 bouyer struct sunxi_ccu_clk *clk, u_int new_rate)
101 1.1 bouyer {
102 1.1 bouyer struct sunxi_ccu_fractional *fractional = &clk->u.fractional;
103 1.1 bouyer struct clk *clkp, *clkp_parent;
104 1.1 bouyer u_int parent_rate, best_rate, best_m;
105 1.1 bouyer u_int m, rate;
106 1.1 bouyer int best_diff;
107 1.1 bouyer uint32_t val;
108 1.1 bouyer int i;
109 1.1 bouyer
110 1.1 bouyer clkp = &clk->base;
111 1.1 bouyer clkp_parent = clk_get_parent(clkp);
112 1.1 bouyer if (clkp_parent == NULL)
113 1.1 bouyer return ENXIO;
114 1.1 bouyer
115 1.1 bouyer parent_rate = clk_get_rate(clkp_parent);
116 1.1 bouyer if (parent_rate == 0)
117 1.1 bouyer return (new_rate == 0) ? 0 : ERANGE;
118 1.1 bouyer
119 1.1 bouyer if (fractional->prediv > 0)
120 1.1 bouyer parent_rate = parent_rate / fractional->prediv;
121 1.1 bouyer
122 1.1 bouyer val = CCU_READ(sc, fractional->reg);
123 1.1 bouyer for (i = 0; i < __arraycount(fractional->frac); i++) {
124 1.1 bouyer if (fractional->frac[i] == new_rate) {
125 1.1 bouyer val |= fractional->frac_en;
126 1.1 bouyer val &= ~fractional->frac_sel;
127 1.1 bouyer val |= __SHIFTIN(i, fractional->frac_sel);
128 1.1 bouyer CCU_WRITE(sc, fractional->reg, val);
129 1.1 bouyer return 0;
130 1.1 bouyer }
131 1.1 bouyer }
132 1.1 bouyer val &= ~fractional->frac_en;
133 1.1 bouyer
134 1.1 bouyer best_rate = 0;
135 1.1 bouyer best_diff = INT_MAX;
136 1.1 bouyer
137 1.1 bouyer for (m = fractional->m_min; m <= fractional->m_max; m++) {
138 1.1 bouyer rate = parent_rate * m;
139 1.1 bouyer const int diff = abs(new_rate - rate);
140 1.1 bouyer if (diff < best_diff) {
141 1.1 bouyer best_diff = diff;
142 1.1 bouyer best_rate = rate;
143 1.1 bouyer best_m = m;
144 1.1 bouyer }
145 1.1 bouyer }
146 1.1 bouyer
147 1.1 bouyer if (best_rate == 0)
148 1.1 bouyer return ERANGE;
149 1.1 bouyer
150 1.1 bouyer val &= ~fractional->m;
151 1.1 bouyer val |= __SHIFTIN(best_m, fractional->m);
152 1.1 bouyer CCU_WRITE(sc, fractional->reg, val);
153 1.1 bouyer
154 1.1 bouyer return 0;
155 1.1 bouyer }
156 1.1 bouyer
157 1.1 bouyer const char *
158 1.1 bouyer sunxi_ccu_fractional_get_parent(struct sunxi_ccu_softc *sc,
159 1.1 bouyer struct sunxi_ccu_clk *clk)
160 1.1 bouyer {
161 1.1 bouyer struct sunxi_ccu_fractional *fractional = &clk->u.fractional;
162 1.1 bouyer
163 1.1 bouyer KASSERT(clk->type == SUNXI_CCU_FRACTIONAL);
164 1.1 bouyer
165 1.1 bouyer return fractional->parent;
166 1.1 bouyer }
167