sunxi_ccu_div.c revision 1.2.2.2 1 /* $NetBSD: sunxi_ccu_div.c,v 1.2.2.2 2017/08/28 17:51:32 skrll 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_div.c,v 1.2.2.2 2017/08/28 17:51:32 skrll 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_div_get_rate(struct sunxi_ccu_softc *sc,
41 struct sunxi_ccu_clk *clk)
42 {
43 struct sunxi_ccu_div *div = &clk->u.div;
44 struct clk *clkp, *clkp_parent;
45 u_int rate, ratio;
46 uint32_t val;
47
48 KASSERT(clk->type == SUNXI_CCU_DIV);
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, div->reg);
60 if (div->div)
61 ratio = __SHIFTOUT(val, div->div);
62 else
63 ratio = 0;
64
65 if ((div->flags & SUNXI_CCU_DIV_ZERO_IS_ONE) != 0 && ratio == 0)
66 ratio = 1;
67 if (div->flags & SUNXI_CCU_DIV_POWER_OF_TWO)
68 ratio = 1 << ratio;
69 else
70 ratio++;
71
72 return rate / ratio;
73 }
74
75 int
76 sunxi_ccu_div_set_rate(struct sunxi_ccu_softc *sc,
77 struct sunxi_ccu_clk *clk, u_int new_rate)
78 {
79 return EINVAL;
80 }
81
82 int
83 sunxi_ccu_div_set_parent(struct sunxi_ccu_softc *sc,
84 struct sunxi_ccu_clk *clk, const char *name)
85 {
86 struct sunxi_ccu_div *div = &clk->u.div;
87 uint32_t val;
88 u_int index;
89
90 KASSERT(clk->type == SUNXI_CCU_DIV);
91
92 if (div->sel == 0)
93 return ENODEV;
94
95 for (index = 0; index < div->nparents; index++) {
96 if (div->parents[index] != NULL &&
97 strcmp(div->parents[index], name) == 0)
98 break;
99 }
100 if (index == div->nparents)
101 return EINVAL;
102
103 val = CCU_READ(sc, div->reg);
104 val &= ~div->sel;
105 val |= __SHIFTIN(index, div->sel);
106 CCU_WRITE(sc, div->reg, val);
107
108 return 0;
109 }
110
111 const char *
112 sunxi_ccu_div_get_parent(struct sunxi_ccu_softc *sc,
113 struct sunxi_ccu_clk *clk)
114 {
115 struct sunxi_ccu_div *div = &clk->u.div;
116 u_int index;
117 uint32_t val;
118
119 KASSERT(clk->type == SUNXI_CCU_DIV);
120
121 if (div->sel == 0)
122 return div->parents[0];
123
124 val = CCU_READ(sc, div->reg);
125 index = __SHIFTOUT(val, div->sel);
126
127 return div->parents[index];
128 }
129