blob: 96903ae8b192447ae665a1cf3b783856d614ca99 [file] [log] [blame]
Heiko Stübner90c59022014-07-03 01:59:10 +02001/*
2 * Copyright (c) 2014 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <asm/div64.h>
17#include <linux/slab.h>
18#include <linux/io.h>
19#include <linux/delay.h>
Heiko Stübner90c59022014-07-03 01:59:10 +020020#include <linux/clk-provider.h>
21#include <linux/regmap.h>
22#include "clk.h"
23
24#define PLL_MODE_MASK 0x3
25#define PLL_MODE_SLOW 0x0
26#define PLL_MODE_NORM 0x1
27#define PLL_MODE_DEEP 0x2
28
29struct rockchip_clk_pll {
30 struct clk_hw hw;
31
32 struct clk_mux pll_mux;
33 const struct clk_ops *pll_mux_ops;
34
35 struct notifier_block clk_nb;
Heiko Stübner90c59022014-07-03 01:59:10 +020036
37 void __iomem *reg_base;
38 int lock_offset;
39 unsigned int lock_shift;
40 enum rockchip_pll_type type;
Heiko Stuebner4f8a7c52014-11-20 20:38:50 +010041 u8 flags;
Heiko Stübner90c59022014-07-03 01:59:10 +020042 const struct rockchip_pll_rate_table *rate_table;
43 unsigned int rate_count;
44 spinlock_t *lock;
45};
46
47#define to_rockchip_clk_pll(_hw) container_of(_hw, struct rockchip_clk_pll, hw)
48#define to_rockchip_clk_pll_nb(nb) \
49 container_of(nb, struct rockchip_clk_pll, clk_nb)
50
51static const struct rockchip_pll_rate_table *rockchip_get_pll_settings(
52 struct rockchip_clk_pll *pll, unsigned long rate)
53{
54 const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
55 int i;
56
57 for (i = 0; i < pll->rate_count; i++) {
58 if (rate == rate_table[i].rate)
59 return &rate_table[i];
60 }
61
62 return NULL;
63}
64
65static long rockchip_pll_round_rate(struct clk_hw *hw,
66 unsigned long drate, unsigned long *prate)
67{
68 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
69 const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
70 int i;
71
72 /* Assumming rate_table is in descending order */
73 for (i = 0; i < pll->rate_count; i++) {
74 if (drate >= rate_table[i].rate)
75 return rate_table[i].rate;
76 }
77
78 /* return minimum supported value */
79 return rate_table[i - 1].rate;
80}
81
82/*
83 * Wait for the pll to reach the locked state.
84 * The calling set_rate function is responsible for making sure the
85 * grf regmap is available.
86 */
87static int rockchip_pll_wait_lock(struct rockchip_clk_pll *pll)
88{
89 struct regmap *grf = rockchip_clk_get_grf();
90 unsigned int val;
91 int delay = 24000000, ret;
92
93 while (delay > 0) {
94 ret = regmap_read(grf, pll->lock_offset, &val);
95 if (ret) {
96 pr_err("%s: failed to read pll lock status: %d\n",
97 __func__, ret);
98 return ret;
99 }
100
101 if (val & BIT(pll->lock_shift))
102 return 0;
103 delay--;
104 }
105
106 pr_err("%s: timeout waiting for pll to lock\n", __func__);
107 return -ETIMEDOUT;
108}
109
110/**
Heiko Stübner90c59022014-07-03 01:59:10 +0200111 * PLL used in RK3066, RK3188 and RK3288
112 */
113
114#define RK3066_PLL_RESET_DELAY(nr) ((nr * 500) / 24 + 1)
115
116#define RK3066_PLLCON(i) (i * 0x4)
117#define RK3066_PLLCON0_OD_MASK 0xf
118#define RK3066_PLLCON0_OD_SHIFT 0
119#define RK3066_PLLCON0_NR_MASK 0x3f
120#define RK3066_PLLCON0_NR_SHIFT 8
121#define RK3066_PLLCON1_NF_MASK 0x1fff
122#define RK3066_PLLCON1_NF_SHIFT 0
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700123#define RK3066_PLLCON2_NB_MASK 0xfff
124#define RK3066_PLLCON2_NB_SHIFT 0
Heiko Stübner90c59022014-07-03 01:59:10 +0200125#define RK3066_PLLCON3_RESET (1 << 5)
126#define RK3066_PLLCON3_PWRDOWN (1 << 1)
127#define RK3066_PLLCON3_BYPASS (1 << 0)
128
129static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw,
130 unsigned long prate)
131{
132 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
133 u64 nf, nr, no, rate64 = prate;
134 u32 pllcon;
135
136 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3));
137 if (pllcon & RK3066_PLLCON3_BYPASS) {
138 pr_debug("%s: pll %s is bypassed\n", __func__,
139 __clk_get_name(hw->clk));
140 return prate;
141 }
142
143 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
144 nf = (pllcon >> RK3066_PLLCON1_NF_SHIFT) & RK3066_PLLCON1_NF_MASK;
145
146 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
147 nr = (pllcon >> RK3066_PLLCON0_NR_SHIFT) & RK3066_PLLCON0_NR_MASK;
148 no = (pllcon >> RK3066_PLLCON0_OD_SHIFT) & RK3066_PLLCON0_OD_MASK;
149
150 rate64 *= (nf + 1);
151 do_div(rate64, nr + 1);
152 do_div(rate64, no + 1);
153
154 return (unsigned long)rate64;
155}
156
157static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate,
158 unsigned long prate)
159{
160 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
161 const struct rockchip_pll_rate_table *rate;
162 unsigned long old_rate = rockchip_rk3066_pll_recalc_rate(hw, prate);
163 struct regmap *grf = rockchip_clk_get_grf();
Doug Anderson9c030ea2014-09-15 21:07:57 -0700164 struct clk_mux *pll_mux = &pll->pll_mux;
165 const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
166 int rate_change_remuxed = 0;
167 int cur_parent;
Heiko Stübner90c59022014-07-03 01:59:10 +0200168 int ret;
169
170 if (IS_ERR(grf)) {
171 pr_debug("%s: grf regmap not available, aborting rate change\n",
172 __func__);
173 return PTR_ERR(grf);
174 }
175
176 pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
177 __func__, __clk_get_name(hw->clk), old_rate, drate, prate);
178
179 /* Get required rate settings from table */
180 rate = rockchip_get_pll_settings(pll, drate);
181 if (!rate) {
182 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
183 drate, __clk_get_name(hw->clk));
184 return -EINVAL;
185 }
186
187 pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n",
188 __func__, rate->rate, rate->nr, rate->no, rate->nf);
189
Doug Anderson9c030ea2014-09-15 21:07:57 -0700190 cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
191 if (cur_parent == PLL_MODE_NORM) {
192 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
193 rate_change_remuxed = 1;
194 }
195
Heiko Stübner90c59022014-07-03 01:59:10 +0200196 /* enter reset mode */
197 writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0),
198 pll->reg_base + RK3066_PLLCON(3));
199
200 /* update pll values */
201 writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK,
202 RK3066_PLLCON0_NR_SHIFT) |
203 HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK,
204 RK3066_PLLCON0_OD_SHIFT),
205 pll->reg_base + RK3066_PLLCON(0));
206
207 writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK,
208 RK3066_PLLCON1_NF_SHIFT),
209 pll->reg_base + RK3066_PLLCON(1));
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700210 writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK,
211 RK3066_PLLCON2_NB_SHIFT),
Heiko Stübner90c59022014-07-03 01:59:10 +0200212 pll->reg_base + RK3066_PLLCON(2));
213
214 /* leave reset and wait the reset_delay */
215 writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0),
216 pll->reg_base + RK3066_PLLCON(3));
217 udelay(RK3066_PLL_RESET_DELAY(rate->nr));
218
219 /* wait for the pll to lock */
220 ret = rockchip_pll_wait_lock(pll);
221 if (ret) {
222 pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
223 __func__, old_rate);
224 rockchip_rk3066_pll_set_rate(hw, old_rate, prate);
225 }
226
Doug Anderson9c030ea2014-09-15 21:07:57 -0700227 if (rate_change_remuxed)
228 pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
229
Heiko Stübner90c59022014-07-03 01:59:10 +0200230 return ret;
231}
232
233static int rockchip_rk3066_pll_enable(struct clk_hw *hw)
234{
235 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
236
237 writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0),
238 pll->reg_base + RK3066_PLLCON(3));
239
240 return 0;
241}
242
243static void rockchip_rk3066_pll_disable(struct clk_hw *hw)
244{
245 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
246
247 writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN,
248 RK3066_PLLCON3_PWRDOWN, 0),
249 pll->reg_base + RK3066_PLLCON(3));
250}
251
252static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw)
253{
254 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
255 u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3));
256
257 return !(pllcon & RK3066_PLLCON3_PWRDOWN);
258}
259
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100260static void rockchip_rk3066_pll_init(struct clk_hw *hw)
261{
262 struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
263 const struct rockchip_pll_rate_table *rate;
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700264 unsigned int nf, nr, no, nb;
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100265 unsigned long drate;
266 u32 pllcon;
267
268 if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
269 return;
270
271 drate = __clk_get_rate(hw->clk);
272 rate = rockchip_get_pll_settings(pll, drate);
273
274 /* when no rate setting for the current rate, rely on clk_set_rate */
275 if (!rate)
276 return;
277
278 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
279 nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT) & RK3066_PLLCON0_NR_MASK) + 1;
280 no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT) & RK3066_PLLCON0_OD_MASK) + 1;
281
282 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
283 nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT) & RK3066_PLLCON1_NF_MASK) + 1;
284
285 pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2));
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700286 nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT) & RK3066_PLLCON2_NB_MASK) + 1;
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100287
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700288 pr_debug("%s: pll %s@%lu: nr (%d:%d); no (%d:%d); nf(%d:%d), nb(%d:%d)\n",
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100289 __func__, __clk_get_name(hw->clk), drate, rate->nr, nr,
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700290 rate->no, no, rate->nf, nf, rate->nb, nb);
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100291 if (rate->nr != nr || rate->no != no || rate->nf != nf
Douglas Anderson2bbfe002015-07-21 13:41:23 -0700292 || rate->nb != nb) {
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100293 struct clk *parent = __clk_get_parent(hw->clk);
294 unsigned long prate;
295
296 if (!parent) {
297 pr_warn("%s: parent of %s not available\n",
298 __func__, __clk_get_name(hw->clk));
299 return;
300 }
301
302 pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
303 __func__, __clk_get_name(hw->clk));
304 prate = __clk_get_rate(parent);
305 rockchip_rk3066_pll_set_rate(hw, drate, prate);
306 }
307}
308
Heiko Stübner90c59022014-07-03 01:59:10 +0200309static const struct clk_ops rockchip_rk3066_pll_clk_norate_ops = {
310 .recalc_rate = rockchip_rk3066_pll_recalc_rate,
311 .enable = rockchip_rk3066_pll_enable,
312 .disable = rockchip_rk3066_pll_disable,
313 .is_enabled = rockchip_rk3066_pll_is_enabled,
314};
315
316static const struct clk_ops rockchip_rk3066_pll_clk_ops = {
317 .recalc_rate = rockchip_rk3066_pll_recalc_rate,
318 .round_rate = rockchip_pll_round_rate,
319 .set_rate = rockchip_rk3066_pll_set_rate,
320 .enable = rockchip_rk3066_pll_enable,
321 .disable = rockchip_rk3066_pll_disable,
322 .is_enabled = rockchip_rk3066_pll_is_enabled,
Heiko Stuebner0bb66d32014-11-20 20:38:52 +0100323 .init = rockchip_rk3066_pll_init,
Heiko Stübner90c59022014-07-03 01:59:10 +0200324};
325
326/*
327 * Common registering of pll clocks
328 */
329
330struct clk *rockchip_clk_register_pll(enum rockchip_pll_type pll_type,
Uwe Kleine-König4a1caed2015-05-28 10:45:51 +0200331 const char *name, const char *const *parent_names,
332 u8 num_parents, void __iomem *base, int con_offset,
333 int grf_lock_offset, int lock_shift, int mode_offset,
334 int mode_shift, struct rockchip_pll_rate_table *rate_table,
Heiko Stuebner4f8a7c52014-11-20 20:38:50 +0100335 u8 clk_pll_flags, spinlock_t *lock)
Heiko Stübner90c59022014-07-03 01:59:10 +0200336{
337 const char *pll_parents[3];
338 struct clk_init_data init;
339 struct rockchip_clk_pll *pll;
340 struct clk_mux *pll_mux;
341 struct clk *pll_clk, *mux_clk;
342 char pll_name[20];
Heiko Stübner90c59022014-07-03 01:59:10 +0200343
344 if (num_parents != 2) {
345 pr_err("%s: needs two parent clocks\n", __func__);
346 return ERR_PTR(-EINVAL);
347 }
348
349 /* name the actual pll */
350 snprintf(pll_name, sizeof(pll_name), "pll_%s", name);
351
352 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
353 if (!pll)
354 return ERR_PTR(-ENOMEM);
355
356 init.name = pll_name;
357
358 /* keep all plls untouched for now */
359 init.flags = CLK_IGNORE_UNUSED;
360
361 init.parent_names = &parent_names[0];
362 init.num_parents = 1;
363
364 if (rate_table) {
365 int len;
366
367 /* find count of rates in rate_table */
368 for (len = 0; rate_table[len].rate != 0; )
369 len++;
370
371 pll->rate_count = len;
372 pll->rate_table = kmemdup(rate_table,
373 pll->rate_count *
374 sizeof(struct rockchip_pll_rate_table),
375 GFP_KERNEL);
376 WARN(!pll->rate_table,
377 "%s: could not allocate rate table for %s\n",
378 __func__, name);
379 }
380
381 switch (pll_type) {
382 case pll_rk3066:
383 if (!pll->rate_table)
384 init.ops = &rockchip_rk3066_pll_clk_norate_ops;
385 else
386 init.ops = &rockchip_rk3066_pll_clk_ops;
387 break;
388 default:
389 pr_warn("%s: Unknown pll type for pll clk %s\n",
390 __func__, name);
391 }
392
393 pll->hw.init = &init;
394 pll->type = pll_type;
395 pll->reg_base = base + con_offset;
396 pll->lock_offset = grf_lock_offset;
397 pll->lock_shift = lock_shift;
Heiko Stuebner4f8a7c52014-11-20 20:38:50 +0100398 pll->flags = clk_pll_flags;
Heiko Stübner90c59022014-07-03 01:59:10 +0200399 pll->lock = lock;
Heiko Stübner90c59022014-07-03 01:59:10 +0200400
Heiko Stuebnerd0e7a0c2014-11-20 20:38:51 +0100401 /* create the mux on top of the real pll */
402 pll->pll_mux_ops = &clk_mux_ops;
403 pll_mux = &pll->pll_mux;
404 pll_mux->reg = base + mode_offset;
405 pll_mux->shift = mode_shift;
406 pll_mux->mask = PLL_MODE_MASK;
407 pll_mux->flags = 0;
408 pll_mux->lock = lock;
409 pll_mux->hw.init = &init;
410
411 if (pll_type == pll_rk3066)
412 pll_mux->flags |= CLK_MUX_HIWORD_MASK;
413
Heiko Stübner90c59022014-07-03 01:59:10 +0200414 pll_clk = clk_register(NULL, &pll->hw);
415 if (IS_ERR(pll_clk)) {
416 pr_err("%s: failed to register pll clock %s : %ld\n",
417 __func__, name, PTR_ERR(pll_clk));
418 mux_clk = pll_clk;
419 goto err_pll;
420 }
421
Heiko Stübner90c59022014-07-03 01:59:10 +0200422 /* the actual muxing is xin24m, pll-output, xin32k */
423 pll_parents[0] = parent_names[0];
424 pll_parents[1] = pll_name;
425 pll_parents[2] = parent_names[1];
426
427 init.name = name;
428 init.flags = CLK_SET_RATE_PARENT;
429 init.ops = pll->pll_mux_ops;
430 init.parent_names = pll_parents;
431 init.num_parents = ARRAY_SIZE(pll_parents);
432
Heiko Stübner90c59022014-07-03 01:59:10 +0200433 mux_clk = clk_register(NULL, &pll_mux->hw);
434 if (IS_ERR(mux_clk))
435 goto err_mux;
436
437 return mux_clk;
438
439err_mux:
Heiko Stübner90c59022014-07-03 01:59:10 +0200440 clk_unregister(pll_clk);
441err_pll:
442 kfree(pll);
443 return mux_clk;
444}