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