blob: 342b85d4e22a14f5a3769d5c30b977dae380725c [file] [log] [blame]
Michael Turquette1c50da42016-06-06 23:16:17 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright (c) 2016 AmLogic, Inc.
8 * Author: Michael Turquette <mturquette@baylibre.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING
24 *
25 * BSD LICENSE
26 *
27 * Copyright (c) 2016 AmLogic, Inc.
28 * Author: Michael Turquette <mturquette@baylibre.com>
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 *
34 * * Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * * Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in
38 * the documentation and/or other materials provided with the
39 * distribution.
40 * * Neither the name of Intel Corporation nor the names of its
41 * contributors may be used to endorse or promote products derived
42 * from this software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
48 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57/*
58 * MultiPhase Locked Loops are outputs from a PLL with additional frequency
59 * scaling capabilities. MPLL rates are calculated as:
60 *
61 * f(N2_integer, SDM_IN ) = 2.0G/(N2_integer + SDM_IN/16384)
62 */
63
64#include <linux/clk-provider.h>
65#include "clkc.h"
66
Jerome Brunet007e6e52017-03-09 11:41:50 +010067#define SDM_DEN 16384
68#define SDM_MIN 1
69#define SDM_MAX 16383
70#define N2_MIN 4
71#define N2_MAX 127
Michael Turquette1c50da42016-06-06 23:16:17 -070072
73#define to_meson_clk_mpll(_hw) container_of(_hw, struct meson_clk_mpll, hw)
74
Jerome Brunet007e6e52017-03-09 11:41:50 +010075static unsigned long rate_from_params(unsigned long parent_rate,
76 unsigned long sdm,
77 unsigned long n2)
78{
79 return (parent_rate * SDM_DEN) / ((SDM_DEN * n2) + sdm);
80}
81
82static void params_from_rate(unsigned long requested_rate,
83 unsigned long parent_rate,
84 unsigned long *sdm,
85 unsigned long *n2)
86{
87 uint64_t div = parent_rate;
88 unsigned long rem = do_div(div, requested_rate);
89
90 if (div < N2_MIN) {
91 *n2 = N2_MIN;
92 *sdm = SDM_MIN;
93 } else if (div > N2_MAX) {
94 *n2 = N2_MAX;
95 *sdm = SDM_MAX;
96 } else {
97 *n2 = div;
98 *sdm = DIV_ROUND_UP(rem * SDM_DEN, requested_rate);
99 if (*sdm < SDM_MIN)
100 *sdm = SDM_MIN;
101 else if (*sdm > SDM_MAX)
102 *sdm = SDM_MAX;
103 }
104}
105
Michael Turquette1c50da42016-06-06 23:16:17 -0700106static unsigned long mpll_recalc_rate(struct clk_hw *hw,
107 unsigned long parent_rate)
108{
109 struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
110 struct parm *p;
Michael Turquette1c50da42016-06-06 23:16:17 -0700111 unsigned long reg, sdm, n2;
112
113 p = &mpll->sdm;
114 reg = readl(mpll->base + p->reg_off);
115 sdm = PARM_GET(p->width, p->shift, reg);
116
117 p = &mpll->n2;
118 reg = readl(mpll->base + p->reg_off);
119 n2 = PARM_GET(p->width, p->shift, reg);
120
Jerome Brunet007e6e52017-03-09 11:41:50 +0100121 return rate_from_params(parent_rate, sdm, n2);
122}
Michael Turquette1c50da42016-06-06 23:16:17 -0700123
Jerome Brunet007e6e52017-03-09 11:41:50 +0100124static long mpll_round_rate(struct clk_hw *hw,
125 unsigned long rate,
126 unsigned long *parent_rate)
127{
128 unsigned long sdm, n2;
129
130 params_from_rate(rate, *parent_rate, &sdm, &n2);
131 return rate_from_params(*parent_rate, sdm, n2);
132}
133
134static int mpll_set_rate(struct clk_hw *hw,
135 unsigned long rate,
136 unsigned long parent_rate)
137{
138 struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
139 struct parm *p;
140 unsigned long reg, sdm, n2;
141 unsigned long flags = 0;
142
143 params_from_rate(rate, parent_rate, &sdm, &n2);
144
145 if (mpll->lock)
146 spin_lock_irqsave(mpll->lock, flags);
147 else
148 __acquire(mpll->lock);
149
150 p = &mpll->sdm;
151 reg = readl(mpll->base + p->reg_off);
152 reg = PARM_SET(p->width, p->shift, reg, sdm);
153 writel(reg, mpll->base + p->reg_off);
154
155 p = &mpll->sdm_en;
156 reg = readl(mpll->base + p->reg_off);
157 reg = PARM_SET(p->width, p->shift, reg, 1);
158 writel(reg, mpll->base + p->reg_off);
159
160 p = &mpll->n2;
161 reg = readl(mpll->base + p->reg_off);
162 reg = PARM_SET(p->width, p->shift, reg, n2);
163 writel(reg, mpll->base + p->reg_off);
164
165 if (mpll->lock)
166 spin_unlock_irqrestore(mpll->lock, flags);
167 else
168 __release(mpll->lock);
169
170 return 0;
171}
172
173static void mpll_enable_core(struct clk_hw *hw, int enable)
174{
175 struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
176 struct parm *p;
177 unsigned long reg;
178 unsigned long flags = 0;
179
180 if (mpll->lock)
181 spin_lock_irqsave(mpll->lock, flags);
182 else
183 __acquire(mpll->lock);
184
185 p = &mpll->en;
186 reg = readl(mpll->base + p->reg_off);
187 reg = PARM_SET(p->width, p->shift, reg, enable ? 1 : 0);
188 writel(reg, mpll->base + p->reg_off);
189
190 if (mpll->lock)
191 spin_unlock_irqrestore(mpll->lock, flags);
192 else
193 __release(mpll->lock);
194}
195
196
197static int mpll_enable(struct clk_hw *hw)
198{
199 mpll_enable_core(hw, 1);
200
201 return 0;
202}
203
204static void mpll_disable(struct clk_hw *hw)
205{
206 mpll_enable_core(hw, 0);
207}
208
209static int mpll_is_enabled(struct clk_hw *hw)
210{
211 struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
212 struct parm *p;
213 unsigned long reg;
214 int en;
215
216 p = &mpll->en;
217 reg = readl(mpll->base + p->reg_off);
218 en = PARM_GET(p->width, p->shift, reg);
219
220 return en;
Michael Turquette1c50da42016-06-06 23:16:17 -0700221}
222
223const struct clk_ops meson_clk_mpll_ro_ops = {
Jerome Brunet007e6e52017-03-09 11:41:50 +0100224 .recalc_rate = mpll_recalc_rate,
225 .round_rate = mpll_round_rate,
226 .is_enabled = mpll_is_enabled,
227};
228
229const struct clk_ops meson_clk_mpll_ops = {
230 .recalc_rate = mpll_recalc_rate,
231 .round_rate = mpll_round_rate,
232 .set_rate = mpll_set_rate,
233 .enable = mpll_enable,
234 .disable = mpll_disable,
235 .is_enabled = mpll_is_enabled,
Michael Turquette1c50da42016-06-06 23:16:17 -0700236};