rk_cru_pll.c revision 1.1 1 1.1 jmcneill /* $NetBSD: rk_cru_pll.c,v 1.1 2018/06/16 00:19:04 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: rk_cru_pll.c,v 1.1 2018/06/16 00:19:04 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.1 jmcneill #define PLL_WRITE_MASK 0xffff0000 /* for CON0 and CON1 */
61 1.1 jmcneill
62 1.1 jmcneill #define GRF_SOC_STATUS0 0x0480
63 1.1 jmcneill
64 1.1 jmcneill u_int
65 1.1 jmcneill rk_cru_pll_get_rate(struct rk_cru_softc *sc,
66 1.1 jmcneill struct rk_cru_clk *clk)
67 1.1 jmcneill {
68 1.1 jmcneill struct rk_cru_pll *pll = &clk->u.pll;
69 1.1 jmcneill struct clk *clkp, *clkp_parent;
70 1.1 jmcneill u_int foutvco, foutpostdiv;
71 1.1 jmcneill
72 1.1 jmcneill KASSERT(clk->type == RK_CRU_PLL);
73 1.1 jmcneill
74 1.1 jmcneill clkp = &clk->base;
75 1.1 jmcneill clkp_parent = clk_get_parent(clkp);
76 1.1 jmcneill if (clkp_parent == NULL)
77 1.1 jmcneill return 0;
78 1.1 jmcneill
79 1.1 jmcneill const u_int fref = clk_get_rate(clkp_parent);
80 1.1 jmcneill if (fref == 0)
81 1.1 jmcneill return 0;
82 1.1 jmcneill
83 1.1 jmcneill const uint32_t con0 = CRU_READ(sc, pll->con_base + PLL_CON0);
84 1.1 jmcneill const uint32_t con1 = CRU_READ(sc, pll->con_base + PLL_CON1);
85 1.1 jmcneill const uint32_t con2 = CRU_READ(sc, pll->con_base + PLL_CON2);
86 1.1 jmcneill
87 1.1 jmcneill const u_int postdiv1 = __SHIFTOUT(con0, PLL_POSTDIV1);
88 1.1 jmcneill const u_int fbdiv = __SHIFTOUT(con0, PLL_FBDIV);
89 1.1 jmcneill const u_int dsmpd = __SHIFTOUT(con1, PLL_DSMPD);
90 1.1 jmcneill const u_int refdiv = __SHIFTOUT(con1, PLL_REFDIV);
91 1.1 jmcneill const u_int postdiv2 = __SHIFTOUT(con1, PLL_POSTDIV2);
92 1.1 jmcneill const u_int fracdiv = __SHIFTOUT(con2, PLL_FRACDIV);
93 1.1 jmcneill
94 1.1 jmcneill if (dsmpd == 1) {
95 1.1 jmcneill /* integer mode */
96 1.1 jmcneill foutvco = fref / refdiv * fbdiv;
97 1.1 jmcneill } else {
98 1.1 jmcneill /* fractional mode */
99 1.1 jmcneill foutvco = fref / refdiv * (fbdiv + fracdiv / 224);
100 1.1 jmcneill }
101 1.1 jmcneill foutpostdiv = foutvco / postdiv1 / postdiv2;
102 1.1 jmcneill
103 1.1 jmcneill return foutpostdiv;
104 1.1 jmcneill }
105 1.1 jmcneill
106 1.1 jmcneill int
107 1.1 jmcneill rk_cru_pll_set_rate(struct rk_cru_softc *sc,
108 1.1 jmcneill struct rk_cru_clk *clk, u_int rate)
109 1.1 jmcneill {
110 1.1 jmcneill struct rk_cru_pll *pll = &clk->u.pll;
111 1.1 jmcneill const struct rk_cru_pll_rate *pll_rate = NULL;
112 1.1 jmcneill uint32_t val;
113 1.1 jmcneill int retry;
114 1.1 jmcneill
115 1.1 jmcneill KASSERT(clk->type == RK_CRU_PLL);
116 1.1 jmcneill
117 1.1 jmcneill if (pll->rates == NULL || rate == 0 || !HAS_GRF(sc))
118 1.1 jmcneill return EIO;
119 1.1 jmcneill
120 1.1 jmcneill for (int i = 0; i < pll->nrates; i++)
121 1.1 jmcneill if (pll->rates[i].rate == rate) {
122 1.1 jmcneill pll_rate = &pll->rates[i];
123 1.1 jmcneill break;
124 1.1 jmcneill }
125 1.1 jmcneill if (pll_rate == NULL)
126 1.1 jmcneill return EINVAL;
127 1.1 jmcneill
128 1.1 jmcneill CRU_WRITE(sc, pll->con_base + PLL_CON0,
129 1.1 jmcneill __SHIFTIN(pll_rate->postdiv1, PLL_POSTDIV1) |
130 1.1 jmcneill __SHIFTIN(pll_rate->fbdiv, PLL_FBDIV) |
131 1.1 jmcneill PLL_WRITE_MASK);
132 1.1 jmcneill
133 1.1 jmcneill CRU_WRITE(sc, pll->con_base + PLL_CON1,
134 1.1 jmcneill __SHIFTIN(pll_rate->dsmpd, PLL_DSMPD) |
135 1.1 jmcneill __SHIFTIN(pll_rate->postdiv2, PLL_POSTDIV2) |
136 1.1 jmcneill __SHIFTIN(pll_rate->refdiv, PLL_REFDIV) |
137 1.1 jmcneill PLL_WRITE_MASK);
138 1.1 jmcneill
139 1.1 jmcneill val = CRU_READ(sc, pll->con_base + PLL_CON2);
140 1.1 jmcneill val &= ~PLL_FRACDIV;
141 1.1 jmcneill val |= __SHIFTIN(pll_rate->fracdiv, PLL_FRACDIV);
142 1.1 jmcneill CRU_WRITE(sc, pll->con_base + PLL_CON2, val);
143 1.1 jmcneill
144 1.1 jmcneill /* Set PLL work mode to normal */
145 1.1 jmcneill const uint32_t write_mask = pll->mode_mask << 16;
146 1.1 jmcneill const uint32_t write_val = pll->mode_mask;
147 1.1 jmcneill CRU_WRITE(sc, pll->mode_reg, write_mask | write_val);
148 1.1 jmcneill
149 1.1 jmcneill for (retry = 1000; retry > 0; retry--) {
150 1.1 jmcneill if (GRF_READ(sc, GRF_SOC_STATUS0) & pll->lock_mask)
151 1.1 jmcneill break;
152 1.1 jmcneill delay(1);
153 1.1 jmcneill }
154 1.1 jmcneill if (retry == 0)
155 1.1 jmcneill device_printf(sc->sc_dev, "WARNING: %s failed to lock\n",
156 1.1 jmcneill clk->base.name);
157 1.1 jmcneill
158 1.1 jmcneill return 0;
159 1.1 jmcneill }
160 1.1 jmcneill
161 1.1 jmcneill const char *
162 1.1 jmcneill rk_cru_pll_get_parent(struct rk_cru_softc *sc,
163 1.1 jmcneill struct rk_cru_clk *clk)
164 1.1 jmcneill {
165 1.1 jmcneill struct rk_cru_pll *pll = &clk->u.pll;
166 1.1 jmcneill
167 1.1 jmcneill KASSERT(clk->type == RK_CRU_PLL);
168 1.1 jmcneill
169 1.1 jmcneill return pll->parent;
170 1.1 jmcneill }
171