rk_cru_pll.c revision 1.5 1 1.5 jmcneill /* $NetBSD: rk_cru_pll.c,v 1.5 2021/11/12 22:02:08 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: rk_cru_pll.c,v 1.5 2021/11/12 22:02:08 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill
35 1.1 jmcneill #include <dev/clk/clk_backend.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <arm/rockchip/rk_cru.h>
38 1.1 jmcneill
39 1.1 jmcneill #define PLL_CON0 0x00
40 1.1 jmcneill #define PLL_BYPASS __BIT(15)
41 1.1 jmcneill #define PLL_POSTDIV1 __BITS(14,12)
42 1.1 jmcneill #define PLL_FBDIV __BITS(11,0)
43 1.1 jmcneill
44 1.1 jmcneill #define PLL_CON1 0x04
45 1.1 jmcneill #define PLL_PDSEL __BIT(15)
46 1.1 jmcneill #define PLL_PD1 __BIT(14)
47 1.1 jmcneill #define PLL_PD0 __BIT(13)
48 1.1 jmcneill #define PLL_DSMPD __BIT(12)
49 1.1 jmcneill #define PLL_LOCK __BIT(10)
50 1.1 jmcneill #define PLL_POSTDIV2 __BITS(8,6)
51 1.1 jmcneill #define PLL_REFDIV __BITS(5,0)
52 1.1 jmcneill
53 1.1 jmcneill #define PLL_CON2 0x08
54 1.1 jmcneill #define PLL_FOUT4PHASEPD __BIT(27)
55 1.1 jmcneill #define PLL_FOUTVCOPD __BIT(26)
56 1.1 jmcneill #define PLL_FOUTPOSTDIVPD __BIT(25)
57 1.1 jmcneill #define PLL_DACPD __BIT(24)
58 1.1 jmcneill #define PLL_FRACDIV __BITS(23,0)
59 1.1 jmcneill
60 1.5 jmcneill #define PLL_CON3 0x0c
61 1.5 jmcneill
62 1.1 jmcneill #define PLL_WRITE_MASK 0xffff0000 /* for CON0 and CON1 */
63 1.1 jmcneill
64 1.5 jmcneill /* RK3288 CON0 */
65 1.5 jmcneill #define RK3288_CLKR __BITS(13,8)
66 1.5 jmcneill #define RK3288_CLKOD __BITS(3,0)
67 1.5 jmcneill /* RK3288 CON1 */
68 1.5 jmcneill #define RK3288_LOCK __BIT(31)
69 1.5 jmcneill #define RK3288_CLKF __BITS(12,0)
70 1.5 jmcneill /* RK3288 CON2 */
71 1.5 jmcneill #define RK3288_BWADJ __BITS(11,0)
72 1.5 jmcneill /* RK3288 CON3 */
73 1.5 jmcneill #define RK3288_BYPASS __BIT(0)
74 1.1 jmcneill
75 1.1 jmcneill u_int
76 1.1 jmcneill rk_cru_pll_get_rate(struct rk_cru_softc *sc,
77 1.1 jmcneill struct rk_cru_clk *clk)
78 1.1 jmcneill {
79 1.1 jmcneill struct rk_cru_pll *pll = &clk->u.pll;
80 1.1 jmcneill struct clk *clkp, *clkp_parent;
81 1.1 jmcneill u_int foutvco, foutpostdiv;
82 1.1 jmcneill
83 1.1 jmcneill KASSERT(clk->type == RK_CRU_PLL);
84 1.1 jmcneill
85 1.1 jmcneill clkp = &clk->base;
86 1.1 jmcneill clkp_parent = clk_get_parent(clkp);
87 1.1 jmcneill if (clkp_parent == NULL)
88 1.1 jmcneill return 0;
89 1.1 jmcneill
90 1.1 jmcneill const u_int fref = clk_get_rate(clkp_parent);
91 1.1 jmcneill if (fref == 0)
92 1.1 jmcneill return 0;
93 1.1 jmcneill
94 1.1 jmcneill const uint32_t con0 = CRU_READ(sc, pll->con_base + PLL_CON0);
95 1.1 jmcneill const uint32_t con1 = CRU_READ(sc, pll->con_base + PLL_CON1);
96 1.1 jmcneill const uint32_t con2 = CRU_READ(sc, pll->con_base + PLL_CON2);
97 1.5 jmcneill const uint32_t con3 = CRU_READ(sc, pll->con_base + PLL_CON3);
98 1.5 jmcneill
99 1.5 jmcneill if ((pll->flags & RK_PLL_RK3288) != 0) {
100 1.5 jmcneill if ((con3 & RK3288_BYPASS) != 0) {
101 1.5 jmcneill return fref;
102 1.5 jmcneill }
103 1.5 jmcneill
104 1.5 jmcneill const u_int nr = __SHIFTOUT(con0, RK3288_CLKR) + 1;
105 1.5 jmcneill const u_int no = __SHIFTOUT(con0, RK3288_CLKOD) + 1;
106 1.5 jmcneill const u_int nf = __SHIFTOUT(con1, RK3288_CLKF) + 1;
107 1.5 jmcneill
108 1.5 jmcneill const uint64_t tmp = (uint64_t)fref * nf / nr / no;
109 1.1 jmcneill
110 1.5 jmcneill return (u_int)tmp;
111 1.1 jmcneill } else {
112 1.5 jmcneill const u_int postdiv1 = __SHIFTOUT(con0, PLL_POSTDIV1);
113 1.5 jmcneill const u_int fbdiv = __SHIFTOUT(con0, PLL_FBDIV);
114 1.5 jmcneill const u_int dsmpd = __SHIFTOUT(con1, PLL_DSMPD);
115 1.5 jmcneill const u_int refdiv = __SHIFTOUT(con1, PLL_REFDIV);
116 1.5 jmcneill const u_int postdiv2 = __SHIFTOUT(con1, PLL_POSTDIV2);
117 1.5 jmcneill const u_int fracdiv = __SHIFTOUT(con2, PLL_FRACDIV);
118 1.5 jmcneill
119 1.5 jmcneill if (dsmpd == 1) {
120 1.5 jmcneill /* integer mode */
121 1.5 jmcneill foutvco = fref / refdiv * fbdiv;
122 1.5 jmcneill } else {
123 1.5 jmcneill /* fractional mode */
124 1.5 jmcneill foutvco = fref / refdiv * fbdiv + ((fref * fracdiv) >> 24);
125 1.5 jmcneill }
126 1.5 jmcneill foutpostdiv = foutvco / postdiv1 / postdiv2;
127 1.5 jmcneill
128 1.5 jmcneill return foutpostdiv;
129 1.1 jmcneill }
130 1.1 jmcneill }
131 1.1 jmcneill
132 1.1 jmcneill int
133 1.1 jmcneill rk_cru_pll_set_rate(struct rk_cru_softc *sc,
134 1.1 jmcneill struct rk_cru_clk *clk, u_int rate)
135 1.1 jmcneill {
136 1.1 jmcneill struct rk_cru_pll *pll = &clk->u.pll;
137 1.1 jmcneill const struct rk_cru_pll_rate *pll_rate = NULL;
138 1.1 jmcneill uint32_t val;
139 1.1 jmcneill int retry;
140 1.1 jmcneill
141 1.1 jmcneill KASSERT(clk->type == RK_CRU_PLL);
142 1.1 jmcneill
143 1.1 jmcneill if (pll->rates == NULL || rate == 0 || !HAS_GRF(sc))
144 1.1 jmcneill return EIO;
145 1.1 jmcneill
146 1.1 jmcneill for (int i = 0; i < pll->nrates; i++)
147 1.1 jmcneill if (pll->rates[i].rate == rate) {
148 1.1 jmcneill pll_rate = &pll->rates[i];
149 1.1 jmcneill break;
150 1.1 jmcneill }
151 1.1 jmcneill if (pll_rate == NULL)
152 1.1 jmcneill return EINVAL;
153 1.1 jmcneill
154 1.5 jmcneill KASSERT((pll->flags & RK_PLL_RK3288) == 0); /* XXX TODO */
155 1.5 jmcneill
156 1.1 jmcneill CRU_WRITE(sc, pll->con_base + PLL_CON0,
157 1.1 jmcneill __SHIFTIN(pll_rate->postdiv1, PLL_POSTDIV1) |
158 1.1 jmcneill __SHIFTIN(pll_rate->fbdiv, PLL_FBDIV) |
159 1.1 jmcneill PLL_WRITE_MASK);
160 1.1 jmcneill
161 1.1 jmcneill CRU_WRITE(sc, pll->con_base + PLL_CON1,
162 1.1 jmcneill __SHIFTIN(pll_rate->dsmpd, PLL_DSMPD) |
163 1.1 jmcneill __SHIFTIN(pll_rate->postdiv2, PLL_POSTDIV2) |
164 1.1 jmcneill __SHIFTIN(pll_rate->refdiv, PLL_REFDIV) |
165 1.1 jmcneill PLL_WRITE_MASK);
166 1.1 jmcneill
167 1.1 jmcneill val = CRU_READ(sc, pll->con_base + PLL_CON2);
168 1.1 jmcneill val &= ~PLL_FRACDIV;
169 1.1 jmcneill val |= __SHIFTIN(pll_rate->fracdiv, PLL_FRACDIV);
170 1.1 jmcneill CRU_WRITE(sc, pll->con_base + PLL_CON2, val);
171 1.1 jmcneill
172 1.1 jmcneill /* Set PLL work mode to normal */
173 1.1 jmcneill const uint32_t write_mask = pll->mode_mask << 16;
174 1.1 jmcneill const uint32_t write_val = pll->mode_mask;
175 1.1 jmcneill CRU_WRITE(sc, pll->mode_reg, write_mask | write_val);
176 1.1 jmcneill
177 1.3 jmcneill syscon_lock(sc->sc_grf);
178 1.1 jmcneill for (retry = 1000; retry > 0; retry--) {
179 1.5 jmcneill if (syscon_read_4(sc->sc_grf, sc->sc_grf_soc_status) & pll->lock_mask)
180 1.1 jmcneill break;
181 1.1 jmcneill delay(1);
182 1.1 jmcneill }
183 1.3 jmcneill syscon_unlock(sc->sc_grf);
184 1.3 jmcneill
185 1.1 jmcneill if (retry == 0)
186 1.1 jmcneill device_printf(sc->sc_dev, "WARNING: %s failed to lock\n",
187 1.1 jmcneill clk->base.name);
188 1.1 jmcneill
189 1.1 jmcneill return 0;
190 1.1 jmcneill }
191 1.1 jmcneill
192 1.1 jmcneill const char *
193 1.1 jmcneill rk_cru_pll_get_parent(struct rk_cru_softc *sc,
194 1.1 jmcneill struct rk_cru_clk *clk)
195 1.1 jmcneill {
196 1.1 jmcneill struct rk_cru_pll *pll = &clk->u.pll;
197 1.1 jmcneill
198 1.1 jmcneill KASSERT(clk->type == RK_CRU_PLL);
199 1.1 jmcneill
200 1.4 jmcneill return pll->parents[0];
201 1.1 jmcneill }
202