1*2f465beaSjmcneill /* $NetBSD: sunxi_ccu_div.c,v 1.6 2019/11/17 17:33:17 jmcneill Exp $ */
2ddb9dd9cSjmcneill
3ddb9dd9cSjmcneill /*-
4ddb9dd9cSjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5ddb9dd9cSjmcneill * All rights reserved.
6ddb9dd9cSjmcneill *
7ddb9dd9cSjmcneill * Redistribution and use in source and binary forms, with or without
8ddb9dd9cSjmcneill * modification, are permitted provided that the following conditions
9ddb9dd9cSjmcneill * are met:
10ddb9dd9cSjmcneill * 1. Redistributions of source code must retain the above copyright
11ddb9dd9cSjmcneill * notice, this list of conditions and the following disclaimer.
12ddb9dd9cSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13ddb9dd9cSjmcneill * notice, this list of conditions and the following disclaimer in the
14ddb9dd9cSjmcneill * documentation and/or other materials provided with the distribution.
15ddb9dd9cSjmcneill *
16ddb9dd9cSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ddb9dd9cSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ddb9dd9cSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ddb9dd9cSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ddb9dd9cSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21ddb9dd9cSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22ddb9dd9cSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23ddb9dd9cSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24ddb9dd9cSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ddb9dd9cSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ddb9dd9cSjmcneill * SUCH DAMAGE.
27ddb9dd9cSjmcneill */
28ddb9dd9cSjmcneill
29ddb9dd9cSjmcneill #include <sys/cdefs.h>
30*2f465beaSjmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_ccu_div.c,v 1.6 2019/11/17 17:33:17 jmcneill Exp $");
31ddb9dd9cSjmcneill
32ddb9dd9cSjmcneill #include <sys/param.h>
33ddb9dd9cSjmcneill #include <sys/bus.h>
34ddb9dd9cSjmcneill
35ddb9dd9cSjmcneill #include <dev/clk/clk_backend.h>
36ddb9dd9cSjmcneill
37ddb9dd9cSjmcneill #include <arm/sunxi/sunxi_ccu.h>
38ddb9dd9cSjmcneill
39607cd651Sjmcneill int
sunxi_ccu_div_enable(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk,int enable)40607cd651Sjmcneill sunxi_ccu_div_enable(struct sunxi_ccu_softc *sc, struct sunxi_ccu_clk *clk,
41607cd651Sjmcneill int enable)
42607cd651Sjmcneill {
43607cd651Sjmcneill struct sunxi_ccu_div *div = &clk->u.div;
44607cd651Sjmcneill uint32_t val;
45607cd651Sjmcneill
46607cd651Sjmcneill KASSERT(clk->type == SUNXI_CCU_DIV);
47607cd651Sjmcneill
48607cd651Sjmcneill if (!div->enable)
49607cd651Sjmcneill return enable ? 0 : EINVAL;
50607cd651Sjmcneill
51607cd651Sjmcneill val = CCU_READ(sc, div->reg);
52607cd651Sjmcneill if (enable)
53607cd651Sjmcneill val |= div->enable;
54607cd651Sjmcneill else
55607cd651Sjmcneill val &= ~div->enable;
56607cd651Sjmcneill CCU_WRITE(sc, div->reg, val);
57607cd651Sjmcneill
58607cd651Sjmcneill return 0;
59607cd651Sjmcneill }
60607cd651Sjmcneill
61ddb9dd9cSjmcneill u_int
sunxi_ccu_div_get_rate(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk)62ddb9dd9cSjmcneill sunxi_ccu_div_get_rate(struct sunxi_ccu_softc *sc,
63ddb9dd9cSjmcneill struct sunxi_ccu_clk *clk)
64ddb9dd9cSjmcneill {
65ddb9dd9cSjmcneill struct sunxi_ccu_div *div = &clk->u.div;
66ddb9dd9cSjmcneill struct clk *clkp, *clkp_parent;
67ddb9dd9cSjmcneill u_int rate, ratio;
68ddb9dd9cSjmcneill uint32_t val;
69ddb9dd9cSjmcneill
70ddb9dd9cSjmcneill KASSERT(clk->type == SUNXI_CCU_DIV);
71ddb9dd9cSjmcneill
72ddb9dd9cSjmcneill clkp = &clk->base;
73ddb9dd9cSjmcneill clkp_parent = clk_get_parent(clkp);
74ddb9dd9cSjmcneill if (clkp_parent == NULL)
75ddb9dd9cSjmcneill return 0;
76ddb9dd9cSjmcneill
77ddb9dd9cSjmcneill rate = clk_get_rate(clkp_parent);
78ddb9dd9cSjmcneill if (rate == 0)
79ddb9dd9cSjmcneill return 0;
80ddb9dd9cSjmcneill
81ddb9dd9cSjmcneill val = CCU_READ(sc, div->reg);
8269b44ac7Sjmcneill if (div->div)
83ddb9dd9cSjmcneill ratio = __SHIFTOUT(val, div->div);
8469b44ac7Sjmcneill else
8569b44ac7Sjmcneill ratio = 0;
8669b44ac7Sjmcneill
87ddb9dd9cSjmcneill if ((div->flags & SUNXI_CCU_DIV_ZERO_IS_ONE) != 0 && ratio == 0)
88ddb9dd9cSjmcneill ratio = 1;
89ddb9dd9cSjmcneill if (div->flags & SUNXI_CCU_DIV_POWER_OF_TWO)
90ddb9dd9cSjmcneill ratio = 1 << ratio;
91607cd651Sjmcneill else if (div->flags & SUNXI_CCU_DIV_TIMES_TWO) {
92607cd651Sjmcneill ratio = ratio << 1;
93607cd651Sjmcneill if (ratio == 0)
94607cd651Sjmcneill ratio = 1;
95607cd651Sjmcneill } else
96ddb9dd9cSjmcneill ratio++;
97ddb9dd9cSjmcneill
98ddb9dd9cSjmcneill return rate / ratio;
99ddb9dd9cSjmcneill }
100ddb9dd9cSjmcneill
101*2f465beaSjmcneill static int
sunxi_ccu_div_select_parent(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk,u_int new_rate)102*2f465beaSjmcneill sunxi_ccu_div_select_parent(struct sunxi_ccu_softc *sc,
103*2f465beaSjmcneill struct sunxi_ccu_clk *clk, u_int new_rate)
104*2f465beaSjmcneill {
105*2f465beaSjmcneill struct sunxi_ccu_div *div = &clk->u.div;
106*2f465beaSjmcneill struct sunxi_ccu_clk *clk_parent;
107*2f465beaSjmcneill struct clk *best_parent;
108*2f465beaSjmcneill u_int index, best_diff;
109*2f465beaSjmcneill const char *pname;
110*2f465beaSjmcneill
111*2f465beaSjmcneill best_parent = NULL;
112*2f465beaSjmcneill best_diff = ~0u;
113*2f465beaSjmcneill for (index = 0; index < div->nparents; index++) {
114*2f465beaSjmcneill pname = div->parents[index];
115*2f465beaSjmcneill if (pname == NULL)
116*2f465beaSjmcneill continue;
117*2f465beaSjmcneill clk_parent = sunxi_ccu_clock_find(sc, pname);
118*2f465beaSjmcneill if (clk_parent == NULL)
119*2f465beaSjmcneill continue;
120*2f465beaSjmcneill const u_int rate = clk_get_rate(&clk_parent->base);
121*2f465beaSjmcneill const u_int diff = abs((int)rate - (int)new_rate);
122*2f465beaSjmcneill if (diff < best_diff) {
123*2f465beaSjmcneill best_diff = diff;
124*2f465beaSjmcneill best_parent = &clk_parent->base;
125*2f465beaSjmcneill }
126*2f465beaSjmcneill }
127*2f465beaSjmcneill if (best_diff == ~0u)
128*2f465beaSjmcneill return EINVAL;
129*2f465beaSjmcneill
130*2f465beaSjmcneill return clk_set_parent(&clk->base, best_parent);
131*2f465beaSjmcneill }
132*2f465beaSjmcneill
133ddb9dd9cSjmcneill int
sunxi_ccu_div_set_rate(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk,u_int new_rate)134ddb9dd9cSjmcneill sunxi_ccu_div_set_rate(struct sunxi_ccu_softc *sc,
135ddb9dd9cSjmcneill struct sunxi_ccu_clk *clk, u_int new_rate)
136ddb9dd9cSjmcneill {
137607cd651Sjmcneill struct sunxi_ccu_div *div = &clk->u.div;
138607cd651Sjmcneill struct clk *clkp, *clkp_parent;
13914c7c433Sbouyer int parent_rate;
140607cd651Sjmcneill uint32_t val, raw_div;
141607cd651Sjmcneill int ratio;
142607cd651Sjmcneill
143607cd651Sjmcneill KASSERT(clk->type == SUNXI_CCU_DIV);
144607cd651Sjmcneill
145607cd651Sjmcneill clkp = &clk->base;
146607cd651Sjmcneill clkp_parent = clk_get_parent(clkp);
147607cd651Sjmcneill if (clkp_parent == NULL)
148607cd651Sjmcneill return ENXIO;
149607cd651Sjmcneill
150df83149aSjmcneill if (div->div == 0) {
151df83149aSjmcneill if ((div->flags & SUNXI_CCU_DIV_SET_RATE_PARENT) != 0)
152df83149aSjmcneill return clk_set_rate(clkp_parent, new_rate);
153df83149aSjmcneill else
154*2f465beaSjmcneill return sunxi_ccu_div_select_parent(sc, clk, new_rate);
155df83149aSjmcneill }
156607cd651Sjmcneill
157607cd651Sjmcneill val = CCU_READ(sc, div->reg);
158607cd651Sjmcneill
15914c7c433Sbouyer parent_rate = clk_get_rate(clkp_parent);
16014c7c433Sbouyer if (parent_rate == 0)
16114c7c433Sbouyer return (new_rate == 0) ? 0 : ERANGE;
16214c7c433Sbouyer
16314c7c433Sbouyer ratio = howmany(parent_rate, new_rate);
164607cd651Sjmcneill if ((div->flags & SUNXI_CCU_DIV_TIMES_TWO) != 0) {
165607cd651Sjmcneill if (ratio > 1 && (ratio & 1) != 0)
166607cd651Sjmcneill ratio++;
167607cd651Sjmcneill raw_div = ratio >> 1;
16814c7c433Sbouyer } else if ((div->flags & SUNXI_CCU_DIV_POWER_OF_TWO) != 0) {
16914c7c433Sbouyer return EINVAL;
17014c7c433Sbouyer } else {
17114c7c433Sbouyer raw_div = (ratio > 0 ) ? ratio - 1 : 0;
17214c7c433Sbouyer }
173607cd651Sjmcneill if (raw_div > __SHIFTOUT_MASK(div->div))
174607cd651Sjmcneill return ERANGE;
175ddb9dd9cSjmcneill
176607cd651Sjmcneill val &= ~div->div;
177607cd651Sjmcneill val |= __SHIFTIN(raw_div, div->div);
178607cd651Sjmcneill CCU_WRITE(sc, div->reg, val);
179607cd651Sjmcneill
180607cd651Sjmcneill return 0;
181607cd651Sjmcneill }
182607cd651Sjmcneill
183ddb9dd9cSjmcneill int
sunxi_ccu_div_set_parent(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk,const char * name)184ddb9dd9cSjmcneill sunxi_ccu_div_set_parent(struct sunxi_ccu_softc *sc,
185ddb9dd9cSjmcneill struct sunxi_ccu_clk *clk, const char *name)
186ddb9dd9cSjmcneill {
187ddb9dd9cSjmcneill struct sunxi_ccu_div *div = &clk->u.div;
188ddb9dd9cSjmcneill uint32_t val;
189ddb9dd9cSjmcneill u_int index;
190ddb9dd9cSjmcneill
191ddb9dd9cSjmcneill KASSERT(clk->type == SUNXI_CCU_DIV);
192ddb9dd9cSjmcneill
193ddb9dd9cSjmcneill if (div->sel == 0)
194ddb9dd9cSjmcneill return ENODEV;
195ddb9dd9cSjmcneill
196ddb9dd9cSjmcneill for (index = 0; index < div->nparents; index++) {
197ddb9dd9cSjmcneill if (div->parents[index] != NULL &&
198ddb9dd9cSjmcneill strcmp(div->parents[index], name) == 0)
199ddb9dd9cSjmcneill break;
200ddb9dd9cSjmcneill }
201ddb9dd9cSjmcneill if (index == div->nparents)
202ddb9dd9cSjmcneill return EINVAL;
203ddb9dd9cSjmcneill
204ddb9dd9cSjmcneill val = CCU_READ(sc, div->reg);
205ddb9dd9cSjmcneill val &= ~div->sel;
206ddb9dd9cSjmcneill val |= __SHIFTIN(index, div->sel);
207ddb9dd9cSjmcneill CCU_WRITE(sc, div->reg, val);
208ddb9dd9cSjmcneill
209ddb9dd9cSjmcneill return 0;
210ddb9dd9cSjmcneill }
211ddb9dd9cSjmcneill
212ddb9dd9cSjmcneill const char *
sunxi_ccu_div_get_parent(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk)213ddb9dd9cSjmcneill sunxi_ccu_div_get_parent(struct sunxi_ccu_softc *sc,
214ddb9dd9cSjmcneill struct sunxi_ccu_clk *clk)
215ddb9dd9cSjmcneill {
216ddb9dd9cSjmcneill struct sunxi_ccu_div *div = &clk->u.div;
217ddb9dd9cSjmcneill u_int index;
218ddb9dd9cSjmcneill uint32_t val;
219ddb9dd9cSjmcneill
220ddb9dd9cSjmcneill KASSERT(clk->type == SUNXI_CCU_DIV);
221ddb9dd9cSjmcneill
222ddb9dd9cSjmcneill if (div->sel == 0)
223ddb9dd9cSjmcneill return div->parents[0];
224ddb9dd9cSjmcneill
225ddb9dd9cSjmcneill val = CCU_READ(sc, div->reg);
226ddb9dd9cSjmcneill index = __SHIFTOUT(val, div->sel);
227ddb9dd9cSjmcneill
228ddb9dd9cSjmcneill return div->parents[index];
229ddb9dd9cSjmcneill }
230