sunxi_ccu_prediv.c revision 1.2.4.2 1 1.2.4.2 snj /* $NetBSD: sunxi_ccu_prediv.c,v 1.2.4.2 2017/07/18 19:13:08 snj Exp $ */
2 1.2.4.2 snj
3 1.2.4.2 snj /*-
4 1.2.4.2 snj * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.2.4.2 snj * All rights reserved.
6 1.2.4.2 snj *
7 1.2.4.2 snj * Redistribution and use in source and binary forms, with or without
8 1.2.4.2 snj * modification, are permitted provided that the following conditions
9 1.2.4.2 snj * are met:
10 1.2.4.2 snj * 1. Redistributions of source code must retain the above copyright
11 1.2.4.2 snj * notice, this list of conditions and the following disclaimer.
12 1.2.4.2 snj * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.4.2 snj * notice, this list of conditions and the following disclaimer in the
14 1.2.4.2 snj * documentation and/or other materials provided with the distribution.
15 1.2.4.2 snj *
16 1.2.4.2 snj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.2.4.2 snj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.2.4.2 snj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.2.4.2 snj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.2.4.2 snj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.2.4.2 snj * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.2.4.2 snj * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.2.4.2 snj * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.2.4.2 snj * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.4.2 snj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.4.2 snj * SUCH DAMAGE.
27 1.2.4.2 snj */
28 1.2.4.2 snj
29 1.2.4.2 snj #include <sys/cdefs.h>
30 1.2.4.2 snj __KERNEL_RCSID(0, "$NetBSD: sunxi_ccu_prediv.c,v 1.2.4.2 2017/07/18 19:13:08 snj Exp $");
31 1.2.4.2 snj
32 1.2.4.2 snj #include <sys/param.h>
33 1.2.4.2 snj #include <sys/bus.h>
34 1.2.4.2 snj
35 1.2.4.2 snj #include <dev/clk/clk_backend.h>
36 1.2.4.2 snj
37 1.2.4.2 snj #include <arm/sunxi/sunxi_ccu.h>
38 1.2.4.2 snj
39 1.2.4.2 snj u_int
40 1.2.4.2 snj sunxi_ccu_prediv_get_rate(struct sunxi_ccu_softc *sc,
41 1.2.4.2 snj struct sunxi_ccu_clk *clk)
42 1.2.4.2 snj {
43 1.2.4.2 snj struct sunxi_ccu_prediv *prediv = &clk->u.prediv;
44 1.2.4.2 snj struct clk *clkp, *clkp_parent;
45 1.2.4.2 snj u_int rate, pre, div, sel;
46 1.2.4.2 snj uint32_t val;
47 1.2.4.2 snj
48 1.2.4.2 snj KASSERT(clk->type == SUNXI_CCU_PREDIV);
49 1.2.4.2 snj
50 1.2.4.2 snj clkp = &clk->base;
51 1.2.4.2 snj clkp_parent = clk_get_parent(clkp);
52 1.2.4.2 snj if (clkp_parent == NULL)
53 1.2.4.2 snj return 0;
54 1.2.4.2 snj
55 1.2.4.2 snj rate = clk_get_rate(clkp_parent);
56 1.2.4.2 snj if (rate == 0)
57 1.2.4.2 snj return 0;
58 1.2.4.2 snj
59 1.2.4.2 snj val = CCU_READ(sc, prediv->reg);
60 1.2.4.2 snj if (prediv->prediv)
61 1.2.4.2 snj pre = __SHIFTOUT(val, prediv->prediv);
62 1.2.4.2 snj else
63 1.2.4.2 snj pre = 0;
64 1.2.4.2 snj if (prediv->div)
65 1.2.4.2 snj div = __SHIFTOUT(val, prediv->div);
66 1.2.4.2 snj else
67 1.2.4.2 snj div = 0;
68 1.2.4.2 snj sel = __SHIFTOUT(val, prediv->sel);
69 1.2.4.2 snj
70 1.2.4.2 snj if (prediv->flags & SUNXI_CCU_PREDIV_POWER_OF_TWO)
71 1.2.4.2 snj div = 1 << div;
72 1.2.4.2 snj else
73 1.2.4.2 snj div++;
74 1.2.4.2 snj
75 1.2.4.2 snj pre++;
76 1.2.4.2 snj
77 1.2.4.2 snj if (prediv->flags & SUNXI_CCU_PREDIV_DIVIDE_BY_TWO)
78 1.2.4.2 snj pre *= 2;
79 1.2.4.2 snj
80 1.2.4.2 snj if (prediv->prediv_sel & __BIT(sel))
81 1.2.4.2 snj return rate / pre / div;
82 1.2.4.2 snj else
83 1.2.4.2 snj return rate / div;
84 1.2.4.2 snj }
85 1.2.4.2 snj
86 1.2.4.2 snj int
87 1.2.4.2 snj sunxi_ccu_prediv_set_rate(struct sunxi_ccu_softc *sc,
88 1.2.4.2 snj struct sunxi_ccu_clk *clk, u_int new_rate)
89 1.2.4.2 snj {
90 1.2.4.2 snj return EINVAL;
91 1.2.4.2 snj }
92 1.2.4.2 snj
93 1.2.4.2 snj int
94 1.2.4.2 snj sunxi_ccu_prediv_set_parent(struct sunxi_ccu_softc *sc,
95 1.2.4.2 snj struct sunxi_ccu_clk *clk, const char *name)
96 1.2.4.2 snj {
97 1.2.4.2 snj struct sunxi_ccu_prediv *prediv = &clk->u.prediv;
98 1.2.4.2 snj uint32_t val;
99 1.2.4.2 snj u_int index;
100 1.2.4.2 snj
101 1.2.4.2 snj KASSERT(clk->type == SUNXI_CCU_PREDIV);
102 1.2.4.2 snj
103 1.2.4.2 snj if (prediv->sel == 0)
104 1.2.4.2 snj return ENODEV;
105 1.2.4.2 snj
106 1.2.4.2 snj for (index = 0; index < prediv->nparents; index++) {
107 1.2.4.2 snj if (prediv->parents[index] != NULL &&
108 1.2.4.2 snj strcmp(prediv->parents[index], name) == 0)
109 1.2.4.2 snj break;
110 1.2.4.2 snj }
111 1.2.4.2 snj if (index == prediv->nparents)
112 1.2.4.2 snj return EINVAL;
113 1.2.4.2 snj
114 1.2.4.2 snj val = CCU_READ(sc, prediv->reg);
115 1.2.4.2 snj val &= ~prediv->sel;
116 1.2.4.2 snj val |= __SHIFTIN(index, prediv->sel);
117 1.2.4.2 snj CCU_WRITE(sc, prediv->reg, val);
118 1.2.4.2 snj
119 1.2.4.2 snj return 0;
120 1.2.4.2 snj }
121 1.2.4.2 snj
122 1.2.4.2 snj const char *
123 1.2.4.2 snj sunxi_ccu_prediv_get_parent(struct sunxi_ccu_softc *sc,
124 1.2.4.2 snj struct sunxi_ccu_clk *clk)
125 1.2.4.2 snj {
126 1.2.4.2 snj struct sunxi_ccu_prediv *prediv = &clk->u.prediv;
127 1.2.4.2 snj u_int index;
128 1.2.4.2 snj uint32_t val;
129 1.2.4.2 snj
130 1.2.4.2 snj KASSERT(clk->type == SUNXI_CCU_PREDIV);
131 1.2.4.2 snj
132 1.2.4.2 snj val = CCU_READ(sc, prediv->reg);
133 1.2.4.2 snj index = __SHIFTOUT(val, prediv->sel);
134 1.2.4.2 snj
135 1.2.4.2 snj return prediv->parents[index];
136 1.2.4.2 snj }
137