sunxi_ccu_prediv.c revision 1.2.4.2 1 /* $NetBSD: sunxi_ccu_prediv.c,v 1.2.4.2 2017/07/18 19:13:08 snj 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.2.4.2 2017/07/18 19:13:08 snj 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 pre++;
76
77 if (prediv->flags & SUNXI_CCU_PREDIV_DIVIDE_BY_TWO)
78 pre *= 2;
79
80 if (prediv->prediv_sel & __BIT(sel))
81 return rate / pre / div;
82 else
83 return rate / div;
84 }
85
86 int
87 sunxi_ccu_prediv_set_rate(struct sunxi_ccu_softc *sc,
88 struct sunxi_ccu_clk *clk, u_int new_rate)
89 {
90 return EINVAL;
91 }
92
93 int
94 sunxi_ccu_prediv_set_parent(struct sunxi_ccu_softc *sc,
95 struct sunxi_ccu_clk *clk, const char *name)
96 {
97 struct sunxi_ccu_prediv *prediv = &clk->u.prediv;
98 uint32_t val;
99 u_int index;
100
101 KASSERT(clk->type == SUNXI_CCU_PREDIV);
102
103 if (prediv->sel == 0)
104 return ENODEV;
105
106 for (index = 0; index < prediv->nparents; index++) {
107 if (prediv->parents[index] != NULL &&
108 strcmp(prediv->parents[index], name) == 0)
109 break;
110 }
111 if (index == prediv->nparents)
112 return EINVAL;
113
114 val = CCU_READ(sc, prediv->reg);
115 val &= ~prediv->sel;
116 val |= __SHIFTIN(index, prediv->sel);
117 CCU_WRITE(sc, prediv->reg, val);
118
119 return 0;
120 }
121
122 const char *
123 sunxi_ccu_prediv_get_parent(struct sunxi_ccu_softc *sc,
124 struct sunxi_ccu_clk *clk)
125 {
126 struct sunxi_ccu_prediv *prediv = &clk->u.prediv;
127 u_int index;
128 uint32_t val;
129
130 KASSERT(clk->type == SUNXI_CCU_PREDIV);
131
132 val = CCU_READ(sc, prediv->reg);
133 index = __SHIFTOUT(val, prediv->sel);
134
135 return prediv->parents[index];
136 }
137