meson_clk_pll.c revision 1.3 1 1.3 ryo /* $NetBSD: meson_clk_pll.c,v 1.3 2021/01/01 07:21:58 ryo Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 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.3 ryo __KERNEL_RCSID(0, "$NetBSD: meson_clk_pll.c,v 1.3 2021/01/01 07:21:58 ryo 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/amlogic/meson_clk.h>
38 1.1 jmcneill
39 1.1 jmcneill u_int
40 1.1 jmcneill meson_clk_pll_get_rate(struct meson_clk_softc *sc,
41 1.1 jmcneill struct meson_clk_clk *clk)
42 1.1 jmcneill {
43 1.1 jmcneill struct meson_clk_pll *pll = &clk->u.pll;
44 1.1 jmcneill struct clk *clkp, *clkp_parent;
45 1.1 jmcneill u_int n, m, frac;
46 1.1 jmcneill uint64_t parent_rate, rate;
47 1.1 jmcneill uint32_t val;
48 1.1 jmcneill
49 1.1 jmcneill KASSERT(clk->type == MESON_CLK_PLL);
50 1.1 jmcneill
51 1.1 jmcneill clkp = &clk->base;
52 1.1 jmcneill clkp_parent = clk_get_parent(clkp);
53 1.1 jmcneill if (clkp_parent == NULL)
54 1.1 jmcneill return 0;
55 1.1 jmcneill
56 1.1 jmcneill parent_rate = clk_get_rate(clkp_parent);
57 1.1 jmcneill if (parent_rate == 0)
58 1.1 jmcneill return 0;
59 1.1 jmcneill
60 1.2 jmcneill CLK_LOCK(sc);
61 1.2 jmcneill
62 1.1 jmcneill val = CLK_READ(sc, pll->n.reg);
63 1.1 jmcneill n = __SHIFTOUT(val, pll->n.mask);
64 1.1 jmcneill
65 1.1 jmcneill val = CLK_READ(sc, pll->m.reg);
66 1.1 jmcneill m = __SHIFTOUT(val, pll->m.mask);
67 1.1 jmcneill
68 1.1 jmcneill if (pll->frac.mask) {
69 1.1 jmcneill val = CLK_READ(sc, pll->frac.reg);
70 1.1 jmcneill frac = __SHIFTOUT(val, pll->frac.mask);
71 1.1 jmcneill } else {
72 1.1 jmcneill frac = 0;
73 1.1 jmcneill }
74 1.1 jmcneill
75 1.2 jmcneill CLK_UNLOCK(sc);
76 1.2 jmcneill
77 1.1 jmcneill rate = parent_rate * m;
78 1.1 jmcneill if (frac) {
79 1.1 jmcneill uint64_t frac_rate = parent_rate * frac;
80 1.1 jmcneill rate += howmany(frac_rate, __SHIFTOUT_MASK(pll->frac.mask) + 1);
81 1.1 jmcneill }
82 1.1 jmcneill
83 1.1 jmcneill return (u_int)howmany(rate, n);
84 1.1 jmcneill }
85 1.1 jmcneill
86 1.3 ryo /* the lock must have been acquired with CLK_LOCK() */
87 1.3 ryo int
88 1.3 ryo meson_clk_pll_wait_lock(struct meson_clk_softc *sc, struct meson_clk_pll *pll)
89 1.3 ryo {
90 1.3 ryo int i;
91 1.3 ryo for (i = 24000000; i > 0; i--) {
92 1.3 ryo if ((CLK_READ(sc, pll->l.reg) & pll->l.mask) != 0)
93 1.3 ryo return 0;
94 1.3 ryo }
95 1.3 ryo return ETIMEDOUT;
96 1.3 ryo }
97 1.3 ryo
98 1.3 ryo int
99 1.3 ryo meson_clk_pll_set_rate(struct meson_clk_softc *sc, struct meson_clk_clk *clk,
100 1.3 ryo u_int new_rate)
101 1.3 ryo {
102 1.3 ryo struct meson_clk_pll *pll = &clk->u.pll;
103 1.3 ryo struct clk *clkp, *clkp_parent;
104 1.3 ryo uint64_t parent_rate, tmp;
105 1.3 ryo uint32_t n, m, m_max, frac, frac_max;
106 1.3 ryo int error;
107 1.3 ryo
108 1.3 ryo KASSERT(clk->type == MESON_CLK_PLL);
109 1.3 ryo
110 1.3 ryo clkp = &clk->base;
111 1.3 ryo clkp_parent = clk_get_parent(clkp);
112 1.3 ryo if (clkp_parent == NULL)
113 1.3 ryo return ENXIO;
114 1.3 ryo
115 1.3 ryo if ((pll->flags & MESON_CLK_DIV_SET_RATE_PARENT) != 0)
116 1.3 ryo return clk_set_rate(clkp_parent, new_rate);
117 1.3 ryo
118 1.3 ryo parent_rate = clk_get_rate(clkp_parent);
119 1.3 ryo if (parent_rate == 0) {
120 1.3 ryo error = (new_rate == 0) ? 0 : ERANGE;
121 1.3 ryo return error;
122 1.3 ryo }
123 1.3 ryo
124 1.3 ryo if (parent_rate > new_rate) {
125 1.3 ryo n = parent_rate / new_rate;
126 1.3 ryo parent_rate /= n;
127 1.3 ryo } else {
128 1.3 ryo n = 1;
129 1.3 ryo }
130 1.3 ryo
131 1.3 ryo #define DIV_ROUND_OFF(x, y) (((x) + (y) / 2) / (y))
132 1.3 ryo m_max = __SHIFTOUT(pll->m.mask, pll->m.mask);
133 1.3 ryo frac_max = __SHIFTOUT(pll->frac.mask, pll->frac.mask);
134 1.3 ryo tmp = DIV_ROUND_OFF(new_rate * (frac_max + 1), parent_rate);
135 1.3 ryo m = tmp / (frac_max + 1);
136 1.3 ryo frac = tmp & frac_max;
137 1.3 ryo
138 1.3 ryo if (m > m_max)
139 1.3 ryo return ERANGE;
140 1.3 ryo
141 1.3 ryo CLK_LOCK(sc);
142 1.3 ryo
143 1.3 ryo /* reset */
144 1.3 ryo CLK_WRITE_BITS(sc, pll->reset.reg, pll->reset.mask, 1);
145 1.3 ryo CLK_WRITE_BITS(sc, pll->reset.reg, pll->reset.mask, 0);
146 1.3 ryo error = meson_clk_pll_wait_lock(sc, pll);
147 1.3 ryo if (error != 0)
148 1.3 ryo goto failure;
149 1.3 ryo
150 1.3 ryo /* disable */
151 1.3 ryo CLK_WRITE_BITS(sc, pll->reset.reg, pll->reset.mask, 1);
152 1.3 ryo CLK_WRITE_BITS(sc, pll->enable.reg, pll->enable.mask, 0);
153 1.3 ryo
154 1.3 ryo /* write new M, N, and FRAC */
155 1.3 ryo CLK_WRITE_BITS(sc, pll->m.reg, pll->m.mask, m);
156 1.3 ryo CLK_WRITE_BITS(sc, pll->n.reg, pll->n.mask, n);
157 1.3 ryo if (pll->frac.mask) {
158 1.3 ryo CLK_WRITE_BITS(sc, pll->frac.reg, pll->frac.mask, frac);
159 1.3 ryo }
160 1.3 ryo
161 1.3 ryo /* enable */
162 1.3 ryo CLK_WRITE_BITS(sc, pll->reset.reg, pll->reset.mask, 1);
163 1.3 ryo CLK_WRITE_BITS(sc, pll->enable.reg, pll->enable.mask, 1);
164 1.3 ryo DELAY(1000);
165 1.3 ryo CLK_WRITE_BITS(sc, pll->reset.reg, pll->reset.mask, 0);
166 1.3 ryo error = meson_clk_pll_wait_lock(sc, pll);
167 1.3 ryo failure:
168 1.3 ryo CLK_UNLOCK(sc);
169 1.3 ryo
170 1.3 ryo return error;
171 1.3 ryo }
172 1.3 ryo
173 1.1 jmcneill const char *
174 1.1 jmcneill meson_clk_pll_get_parent(struct meson_clk_softc *sc,
175 1.1 jmcneill struct meson_clk_clk *clk)
176 1.1 jmcneill {
177 1.1 jmcneill struct meson_clk_pll *pll = &clk->u.pll;
178 1.1 jmcneill
179 1.1 jmcneill KASSERT(clk->type == MESON_CLK_PLL);
180 1.1 jmcneill
181 1.1 jmcneill return pll->parent;
182 1.1 jmcneill }
183