2 * Copyright (C) 2014 STMicroelectronics (R&D) Limited
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
13 * Stephen Gallimore <stephen.gallimore@st.com>,
14 * Pankaj Dev <pankaj.dev@st.com>.
17 #include <linux/slab.h>
18 #include <linux/of_address.h>
19 #include <linux/clk.h>
20 #include <linux/clk-provider.h>
21 #include <linux/iopoll.h>
25 static DEFINE_SPINLOCK(clkgena_c32_odf_lock);
26 DEFINE_SPINLOCK(clkgen_a9_lock);
29 * Common PLL configuration register bits for PLL800 and PLL1600 C65
31 #define C65_MDIV_PLL800_MASK (0xff)
32 #define C65_MDIV_PLL1600_MASK (0x7)
33 #define C65_NDIV_MASK (0xff)
34 #define C65_PDIV_MASK (0x7)
37 * PLL configuration register bits for PLL3200 C32
39 #define C32_NDIV_MASK (0xff)
40 #define C32_IDF_MASK (0x7)
41 #define C32_ODF_MASK (0x3f)
42 #define C32_LDF_MASK (0x7f)
43 #define C32_CP_MASK (0x1f)
45 #define C32_MAX_ODFS (4)
48 * PLL configuration register bits for PLL4600 C28
50 #define C28_NDIV_MASK (0xff)
51 #define C28_IDF_MASK (0x7)
52 #define C28_ODF_MASK (0x3f)
54 struct clkgen_pll_data {
55 struct clkgen_field pdn_status;
56 struct clkgen_field pdn_ctrl;
57 struct clkgen_field locked_status;
58 struct clkgen_field mdiv;
59 struct clkgen_field ndiv;
60 struct clkgen_field pdiv;
61 struct clkgen_field idf;
62 struct clkgen_field ldf;
63 struct clkgen_field cp;
64 unsigned int num_odfs;
65 struct clkgen_field odf[C32_MAX_ODFS];
66 struct clkgen_field odf_gate[C32_MAX_ODFS];
68 struct clkgen_field switch2pll;
70 const struct clk_ops *ops;
73 static const struct clk_ops st_pll1600c65_ops;
74 static const struct clk_ops st_pll800c65_ops;
75 static const struct clk_ops stm_pll3200c32_ops;
76 static const struct clk_ops stm_pll3200c32_a9_ops;
77 static const struct clk_ops st_pll1200c32_ops;
78 static const struct clk_ops stm_pll4600c28_ops;
80 static const struct clkgen_pll_data st_pll1600c65_ax = {
81 .pdn_status = CLKGEN_FIELD(0x0, 0x1, 19),
82 .pdn_ctrl = CLKGEN_FIELD(0x10, 0x1, 0),
83 .locked_status = CLKGEN_FIELD(0x0, 0x1, 31),
84 .mdiv = CLKGEN_FIELD(0x0, C65_MDIV_PLL1600_MASK, 0),
85 .ndiv = CLKGEN_FIELD(0x0, C65_NDIV_MASK, 8),
86 .ops = &st_pll1600c65_ops
89 static const struct clkgen_pll_data st_pll800c65_ax = {
90 .pdn_status = CLKGEN_FIELD(0x0, 0x1, 19),
91 .pdn_ctrl = CLKGEN_FIELD(0xC, 0x1, 1),
92 .locked_status = CLKGEN_FIELD(0x0, 0x1, 31),
93 .mdiv = CLKGEN_FIELD(0x0, C65_MDIV_PLL800_MASK, 0),
94 .ndiv = CLKGEN_FIELD(0x0, C65_NDIV_MASK, 8),
95 .pdiv = CLKGEN_FIELD(0x0, C65_PDIV_MASK, 16),
96 .ops = &st_pll800c65_ops
99 static const struct clkgen_pll_data st_pll3200c32_a1x_0 = {
100 .pdn_status = CLKGEN_FIELD(0x0, 0x1, 31),
101 .pdn_ctrl = CLKGEN_FIELD(0x18, 0x1, 0),
102 .locked_status = CLKGEN_FIELD(0x4, 0x1, 31),
103 .ndiv = CLKGEN_FIELD(0x0, C32_NDIV_MASK, 0x0),
104 .idf = CLKGEN_FIELD(0x4, C32_IDF_MASK, 0x0),
106 .odf = { CLKGEN_FIELD(0x54, C32_ODF_MASK, 4),
107 CLKGEN_FIELD(0x54, C32_ODF_MASK, 10),
108 CLKGEN_FIELD(0x54, C32_ODF_MASK, 16),
109 CLKGEN_FIELD(0x54, C32_ODF_MASK, 22) },
110 .odf_gate = { CLKGEN_FIELD(0x54, 0x1, 0),
111 CLKGEN_FIELD(0x54, 0x1, 1),
112 CLKGEN_FIELD(0x54, 0x1, 2),
113 CLKGEN_FIELD(0x54, 0x1, 3) },
114 .ops = &stm_pll3200c32_ops,
117 static const struct clkgen_pll_data st_pll3200c32_a1x_1 = {
118 .pdn_status = CLKGEN_FIELD(0xC, 0x1, 31),
119 .pdn_ctrl = CLKGEN_FIELD(0x18, 0x1, 1),
120 .locked_status = CLKGEN_FIELD(0x10, 0x1, 31),
121 .ndiv = CLKGEN_FIELD(0xC, C32_NDIV_MASK, 0x0),
122 .idf = CLKGEN_FIELD(0x10, C32_IDF_MASK, 0x0),
124 .odf = { CLKGEN_FIELD(0x58, C32_ODF_MASK, 4),
125 CLKGEN_FIELD(0x58, C32_ODF_MASK, 10),
126 CLKGEN_FIELD(0x58, C32_ODF_MASK, 16),
127 CLKGEN_FIELD(0x58, C32_ODF_MASK, 22) },
128 .odf_gate = { CLKGEN_FIELD(0x58, 0x1, 0),
129 CLKGEN_FIELD(0x58, 0x1, 1),
130 CLKGEN_FIELD(0x58, 0x1, 2),
131 CLKGEN_FIELD(0x58, 0x1, 3) },
132 .ops = &stm_pll3200c32_ops,
136 static const struct clkgen_pll_data st_pll3200c32_a9_415 = {
137 .pdn_status = CLKGEN_FIELD(0x0, 0x1, 0),
138 .pdn_ctrl = CLKGEN_FIELD(0x0, 0x1, 0),
139 .locked_status = CLKGEN_FIELD(0x6C, 0x1, 0),
140 .ndiv = CLKGEN_FIELD(0x0, C32_NDIV_MASK, 9),
141 .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 22),
143 .odf = { CLKGEN_FIELD(0x0, C32_ODF_MASK, 3) },
144 .odf_gate = { CLKGEN_FIELD(0x0, 0x1, 28) },
145 .ops = &stm_pll3200c32_ops,
148 static const struct clkgen_pll_data st_pll3200c32_ddr_415 = {
149 .pdn_status = CLKGEN_FIELD(0x0, 0x1, 0),
150 .pdn_ctrl = CLKGEN_FIELD(0x0, 0x1, 0),
151 .locked_status = CLKGEN_FIELD(0x100, 0x1, 0),
152 .ndiv = CLKGEN_FIELD(0x8, C32_NDIV_MASK, 0),
153 .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 25),
155 .odf = { CLKGEN_FIELD(0x8, C32_ODF_MASK, 8),
156 CLKGEN_FIELD(0x8, C32_ODF_MASK, 14) },
157 .odf_gate = { CLKGEN_FIELD(0x4, 0x1, 28),
158 CLKGEN_FIELD(0x4, 0x1, 29) },
159 .ops = &stm_pll3200c32_ops,
162 static const struct clkgen_pll_data st_pll1200c32_gpu_415 = {
163 .pdn_status = CLKGEN_FIELD(0x4, 0x1, 0),
164 .pdn_ctrl = CLKGEN_FIELD(0x4, 0x1, 0),
165 .locked_status = CLKGEN_FIELD(0x168, 0x1, 0),
166 .ldf = CLKGEN_FIELD(0x0, C32_LDF_MASK, 3),
167 .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 0),
169 .odf = { CLKGEN_FIELD(0x0, C32_ODF_MASK, 10) },
170 .ops = &st_pll1200c32_ops,
174 static const struct clkgen_pll_data st_pll3200c32_a9_416 = {
175 .pdn_status = CLKGEN_FIELD(0x0, 0x1, 0),
176 .pdn_ctrl = CLKGEN_FIELD(0x0, 0x1, 0),
177 .locked_status = CLKGEN_FIELD(0x6C, 0x1, 0),
178 .ndiv = CLKGEN_FIELD(0x8, C32_NDIV_MASK, 0),
179 .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 25),
181 .odf = { CLKGEN_FIELD(0x8, C32_ODF_MASK, 8) },
182 .odf_gate = { CLKGEN_FIELD(0x4, 0x1, 28) },
183 .ops = &stm_pll3200c32_ops,
186 static const struct clkgen_pll_data st_pll3200c32_ddr_416 = {
187 .pdn_status = CLKGEN_FIELD(0x0, 0x1, 0),
188 .pdn_ctrl = CLKGEN_FIELD(0x0, 0x1, 0),
189 .locked_status = CLKGEN_FIELD(0x10C, 0x1, 0),
190 .ndiv = CLKGEN_FIELD(0x8, C32_NDIV_MASK, 0),
191 .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 25),
193 .odf = { CLKGEN_FIELD(0x8, C32_ODF_MASK, 8),
194 CLKGEN_FIELD(0x8, C32_ODF_MASK, 14) },
195 .odf_gate = { CLKGEN_FIELD(0x4, 0x1, 28),
196 CLKGEN_FIELD(0x4, 0x1, 29) },
197 .ops = &stm_pll3200c32_ops,
200 static const struct clkgen_pll_data st_pll1200c32_gpu_416 = {
201 .pdn_status = CLKGEN_FIELD(0x8E4, 0x1, 3),
202 .pdn_ctrl = CLKGEN_FIELD(0x8E4, 0x1, 3),
203 .locked_status = CLKGEN_FIELD(0x90C, 0x1, 0),
204 .ldf = CLKGEN_FIELD(0x0, C32_LDF_MASK, 3),
205 .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 0),
207 .odf = { CLKGEN_FIELD(0x0, C32_ODF_MASK, 10) },
208 .ops = &st_pll1200c32_ops,
211 static const struct clkgen_pll_data st_pll3200c32_407_a0 = {
213 .pdn_status = CLKGEN_FIELD(0x2a0, 0x1, 8),
214 .pdn_ctrl = CLKGEN_FIELD(0x2a0, 0x1, 8),
215 .locked_status = CLKGEN_FIELD(0x2a0, 0x1, 24),
216 .ndiv = CLKGEN_FIELD(0x2a4, C32_NDIV_MASK, 16),
217 .idf = CLKGEN_FIELD(0x2a4, C32_IDF_MASK, 0x0),
219 .odf = { CLKGEN_FIELD(0x2b4, C32_ODF_MASK, 0) },
220 .odf_gate = { CLKGEN_FIELD(0x2b4, 0x1, 6) },
221 .ops = &stm_pll3200c32_ops,
224 static const struct clkgen_pll_data st_pll3200c32_cx_0 = {
226 .pdn_status = CLKGEN_FIELD(0x2a0, 0x1, 8),
227 .pdn_ctrl = CLKGEN_FIELD(0x2a0, 0x1, 8),
228 .locked_status = CLKGEN_FIELD(0x2a0, 0x1, 24),
229 .ndiv = CLKGEN_FIELD(0x2a4, C32_NDIV_MASK, 16),
230 .idf = CLKGEN_FIELD(0x2a4, C32_IDF_MASK, 0x0),
232 .odf = { CLKGEN_FIELD(0x2b4, C32_ODF_MASK, 0) },
233 .odf_gate = { CLKGEN_FIELD(0x2b4, 0x1, 6) },
234 .ops = &stm_pll3200c32_ops,
237 static const struct clkgen_pll_data st_pll3200c32_cx_1 = {
239 .pdn_status = CLKGEN_FIELD(0x2c8, 0x1, 8),
240 .pdn_ctrl = CLKGEN_FIELD(0x2c8, 0x1, 8),
241 .locked_status = CLKGEN_FIELD(0x2c8, 0x1, 24),
242 .ndiv = CLKGEN_FIELD(0x2cc, C32_NDIV_MASK, 16),
243 .idf = CLKGEN_FIELD(0x2cc, C32_IDF_MASK, 0x0),
245 .odf = { CLKGEN_FIELD(0x2dc, C32_ODF_MASK, 0) },
246 .odf_gate = { CLKGEN_FIELD(0x2dc, 0x1, 6) },
247 .ops = &stm_pll3200c32_ops,
250 static const struct clkgen_pll_data st_pll3200c32_407_a9 = {
252 .pdn_status = CLKGEN_FIELD(0x1a8, 0x1, 0),
253 .pdn_ctrl = CLKGEN_FIELD(0x1a8, 0x1, 0),
254 .locked_status = CLKGEN_FIELD(0x87c, 0x1, 0),
255 .ndiv = CLKGEN_FIELD(0x1b0, C32_NDIV_MASK, 0),
256 .idf = CLKGEN_FIELD(0x1a8, C32_IDF_MASK, 25),
258 .odf = { CLKGEN_FIELD(0x1b0, C32_ODF_MASK, 8) },
259 .odf_gate = { CLKGEN_FIELD(0x1ac, 0x1, 28) },
260 .switch2pll_en = true,
261 .cp = CLKGEN_FIELD(0x1a8, C32_CP_MASK, 1),
262 .switch2pll = CLKGEN_FIELD(0x1a4, 0x1, 1),
263 .lock = &clkgen_a9_lock,
264 .ops = &stm_pll3200c32_a9_ops,
267 static struct clkgen_pll_data st_pll4600c28_418_a9 = {
269 .pdn_status = CLKGEN_FIELD(0x1a8, 0x1, 0),
270 .pdn_ctrl = CLKGEN_FIELD(0x1a8, 0x1, 0),
271 .locked_status = CLKGEN_FIELD(0x87c, 0x1, 0),
272 .ndiv = CLKGEN_FIELD(0x1b0, C28_NDIV_MASK, 0),
273 .idf = CLKGEN_FIELD(0x1a8, C28_IDF_MASK, 25),
275 .odf = { CLKGEN_FIELD(0x1b0, C28_ODF_MASK, 8) },
276 .odf_gate = { CLKGEN_FIELD(0x1ac, 0x1, 28) },
277 .switch2pll_en = true,
278 .switch2pll = CLKGEN_FIELD(0x1a4, 0x1, 1),
279 .lock = &clkgen_a9_lock,
280 .ops = &stm_pll4600c28_ops,
284 * DOC: Clock Generated by PLL, rate set and enabled by bootloader
286 * Traits of this clock:
287 * prepare - clk_(un)prepare only ensures parent is (un)prepared
288 * enable - clk_enable/disable only ensures parent is enabled
289 * rate - rate is fixed. No clk_set_rate support
290 * parent - fixed parent. No clk_set_parent support
294 * PLL clock that is integrated in the ClockGenA instances on the STiH415
297 * @hw: handle between common and hardware-specific interfaces.
298 * @type: PLL instance type.
299 * @regs_base: base of the PLL configuration register(s).
304 struct clkgen_pll_data *data;
305 void __iomem *regs_base;
314 #define to_clkgen_pll(_hw) container_of(_hw, struct clkgen_pll, hw)
326 static int clkgen_pll_is_locked(struct clk_hw *hw)
328 struct clkgen_pll *pll = to_clkgen_pll(hw);
329 u32 locked = CLKGEN_READ(pll, locked_status);
334 static int clkgen_pll_is_enabled(struct clk_hw *hw)
336 struct clkgen_pll *pll = to_clkgen_pll(hw);
337 u32 poweroff = CLKGEN_READ(pll, pdn_status);
341 static int __clkgen_pll_enable(struct clk_hw *hw)
343 struct clkgen_pll *pll = to_clkgen_pll(hw);
344 void __iomem *base = pll->regs_base;
345 struct clkgen_field *field = &pll->data->locked_status;
349 if (clkgen_pll_is_enabled(hw))
352 CLKGEN_WRITE(pll, pdn_ctrl, 0);
354 ret = readl_relaxed_poll_timeout(base + field->offset, reg,
355 !!((reg >> field->shift) & field->mask), 0, 10000);
358 if (pll->data->switch2pll_en)
359 CLKGEN_WRITE(pll, switch2pll, 0);
361 pr_debug("%s:%s enabled\n", __clk_get_name(hw->clk), __func__);
367 static int clkgen_pll_enable(struct clk_hw *hw)
369 struct clkgen_pll *pll = to_clkgen_pll(hw);
370 unsigned long flags = 0;
374 spin_lock_irqsave(pll->lock, flags);
376 ret = __clkgen_pll_enable(hw);
379 spin_unlock_irqrestore(pll->lock, flags);
384 static void __clkgen_pll_disable(struct clk_hw *hw)
386 struct clkgen_pll *pll = to_clkgen_pll(hw);
388 if (!clkgen_pll_is_enabled(hw))
391 if (pll->data->switch2pll_en)
392 CLKGEN_WRITE(pll, switch2pll, 1);
394 CLKGEN_WRITE(pll, pdn_ctrl, 1);
396 pr_debug("%s:%s disabled\n", __clk_get_name(hw->clk), __func__);
399 static void clkgen_pll_disable(struct clk_hw *hw)
401 struct clkgen_pll *pll = to_clkgen_pll(hw);
402 unsigned long flags = 0;
405 spin_lock_irqsave(pll->lock, flags);
407 __clkgen_pll_disable(hw);
410 spin_unlock_irqrestore(pll->lock, flags);
413 static unsigned long recalc_stm_pll800c65(struct clk_hw *hw,
414 unsigned long parent_rate)
416 struct clkgen_pll *pll = to_clkgen_pll(hw);
417 unsigned long mdiv, ndiv, pdiv;
421 if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
424 pdiv = CLKGEN_READ(pll, pdiv);
425 mdiv = CLKGEN_READ(pll, mdiv);
426 ndiv = CLKGEN_READ(pll, ndiv);
429 mdiv++; /* mdiv=0 or 1 => MDIV=1 */
431 res = (uint64_t)2 * (uint64_t)parent_rate * (uint64_t)ndiv;
432 rate = (unsigned long)div64_u64(res, mdiv * (1 << pdiv));
434 pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
440 static unsigned long recalc_stm_pll1600c65(struct clk_hw *hw,
441 unsigned long parent_rate)
443 struct clkgen_pll *pll = to_clkgen_pll(hw);
444 unsigned long mdiv, ndiv;
447 if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
450 mdiv = CLKGEN_READ(pll, mdiv);
451 ndiv = CLKGEN_READ(pll, ndiv);
456 /* Note: input is divided by 1000 to avoid overflow */
457 rate = ((2 * (parent_rate / 1000) * ndiv) / mdiv) * 1000;
459 pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
464 static int clk_pll3200c32_get_params(unsigned long input, unsigned long output,
468 unsigned long deviation = ~0;
469 unsigned long new_freq;
471 /* Charge pump table: highest ndiv value for cp=6 to 25 */
472 static const unsigned char cp_table[] = {
473 48, 56, 64, 72, 80, 88, 96, 104, 112, 120,
474 128, 136, 144, 152, 160, 168, 176, 184, 192
477 /* Output clock range: 800Mhz to 1600Mhz */
478 if (output < 800000000 || output > 1600000000)
484 for (i = 1; i <= 7 && deviation; i++) {
485 n = i * output / (2 * input);
493 new_freq = (input * 2 * n) / i;
495 new_deviation = abs(new_freq - output);
497 if (!new_deviation || new_deviation < deviation) {
500 deviation = new_deviation;
504 if (deviation == ~0) /* No solution found */
507 /* Computing recommended charge pump value */
508 for (pll->cp = 6; pll->ndiv > cp_table[pll->cp-6]; (pll->cp)++)
514 static int clk_pll3200c32_get_rate(unsigned long input, struct stm_pll *pll,
520 *rate = ((2 * (input / 1000) * pll->ndiv) / pll->idf) * 1000;
525 static unsigned long recalc_stm_pll3200c32(struct clk_hw *hw,
526 unsigned long parent_rate)
528 struct clkgen_pll *pll = to_clkgen_pll(hw);
529 unsigned long ndiv, idf;
530 unsigned long rate = 0;
532 if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
535 ndiv = CLKGEN_READ(pll, ndiv);
536 idf = CLKGEN_READ(pll, idf);
539 /* Note: input is divided to avoid overflow */
540 rate = ((2 * (parent_rate/1000) * ndiv) / idf) * 1000;
542 pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
547 static long round_rate_stm_pll3200c32(struct clk_hw *hw, unsigned long rate,
548 unsigned long *prate)
550 struct stm_pll params;
552 if (!clk_pll3200c32_get_params(*prate, rate, ¶ms))
553 clk_pll3200c32_get_rate(*prate, ¶ms, &rate);
555 pr_debug("%s: %s rate %ld Invalid\n", __func__,
556 __clk_get_name(hw->clk), rate);
560 pr_debug("%s: %s new rate %ld [ndiv=%u] [idf=%u]\n",
561 __func__, __clk_get_name(hw->clk),
562 rate, (unsigned int)params.ndiv,
563 (unsigned int)params.idf);
568 static int set_rate_stm_pll3200c32(struct clk_hw *hw, unsigned long rate,
569 unsigned long parent_rate)
571 struct clkgen_pll *pll = to_clkgen_pll(hw);
572 struct stm_pll params;
574 unsigned long flags = 0;
576 if (!rate || !parent_rate)
579 if (!clk_pll3200c32_get_params(parent_rate, rate, ¶ms))
580 clk_pll3200c32_get_rate(parent_rate, ¶ms, &hwrate);
582 pr_debug("%s: %s new rate %ld [ndiv=0x%x] [idf=0x%x]\n",
583 __func__, __clk_get_name(hw->clk),
584 hwrate, (unsigned int)params.ndiv,
585 (unsigned int)params.idf);
590 pll->ndiv = params.ndiv;
591 pll->idf = params.idf;
594 __clkgen_pll_disable(hw);
597 spin_lock_irqsave(pll->lock, flags);
599 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
600 CLKGEN_WRITE(pll, idf, pll->idf);
601 CLKGEN_WRITE(pll, cp, pll->cp);
604 spin_unlock_irqrestore(pll->lock, flags);
606 __clkgen_pll_enable(hw);
611 static unsigned long recalc_stm_pll1200c32(struct clk_hw *hw,
612 unsigned long parent_rate)
614 struct clkgen_pll *pll = to_clkgen_pll(hw);
615 unsigned long odf, ldf, idf;
618 if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
621 odf = CLKGEN_READ(pll, odf[0]);
622 ldf = CLKGEN_READ(pll, ldf);
623 idf = CLKGEN_READ(pll, idf);
625 if (!idf) /* idf==0 means 1 */
627 if (!odf) /* odf==0 means 1 */
630 /* Note: input is divided by 1000 to avoid overflow */
631 rate = (((parent_rate / 1000) * ldf) / (odf * idf)) * 1000;
633 pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
638 /* PLL output structure
639 * FVCO >> /2 >> FVCOBY2 (no output)
640 * |> Divider (ODF) >> PHI
642 * FVCOby2 output = (input * 2 * NDIV) / IDF (assuming FRAC_CONTROL==L)
645 * 4Mhz <= INFF input <= 350Mhz
646 * 4Mhz <= INFIN (INFF / IDF) <= 50Mhz
647 * 19.05Mhz <= FVCOby2 output (PHI w ODF=1) <= 3000Mhz
648 * 1 <= i (register/dec value for IDF) <= 7
649 * 8 <= n (register/dec value for NDIV) <= 246
652 static int clk_pll4600c28_get_params(unsigned long input, unsigned long output,
656 unsigned long i, infin, n;
657 unsigned long deviation = ~0;
658 unsigned long new_freq, new_deviation;
660 /* Output clock range: 19Mhz to 3000Mhz */
661 if (output < 19000000 || output > 3000000000u)
664 /* For better jitter, IDF should be smallest and NDIV must be maximum */
665 for (i = 1; i <= 7 && deviation; i++) {
668 if (infin < 4000000 || infin > 50000000)
669 continue; /* Invalid case */
671 n = output / (infin * 2);
672 if (n < 8 || n > 246)
673 continue; /* Invalid case */
675 n++; /* To work around 'y' when n=x.y */
677 for (; n >= 8 && deviation; n--) {
678 new_freq = infin * 2 * n;
679 if (new_freq < output)
680 break; /* Optimization: shorting loop */
682 new_deviation = new_freq - output;
683 if (!new_deviation || new_deviation < deviation) {
686 deviation = new_deviation;
691 if (deviation == ~0) /* No solution found */
697 static int clk_pll4600c28_get_rate(unsigned long input, struct stm_pll *pll,
703 *rate = (input / pll->idf) * 2 * pll->ndiv;
708 static unsigned long recalc_stm_pll4600c28(struct clk_hw *hw,
709 unsigned long parent_rate)
711 struct clkgen_pll *pll = to_clkgen_pll(hw);
712 struct stm_pll params;
715 if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
718 params.ndiv = CLKGEN_READ(pll, ndiv);
719 params.idf = CLKGEN_READ(pll, idf);
721 clk_pll4600c28_get_rate(parent_rate, ¶ms, &rate);
723 pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
728 static long round_rate_stm_pll4600c28(struct clk_hw *hw, unsigned long rate,
729 unsigned long *prate)
731 struct stm_pll params;
733 if (!clk_pll4600c28_get_params(*prate, rate, ¶ms)) {
734 clk_pll4600c28_get_rate(*prate, ¶ms, &rate);
736 pr_debug("%s: %s rate %ld Invalid\n", __func__,
737 __clk_get_name(hw->clk), rate);
741 pr_debug("%s: %s new rate %ld [ndiv=%u] [idf=%u]\n",
742 __func__, __clk_get_name(hw->clk),
743 rate, (unsigned int)params.ndiv,
744 (unsigned int)params.idf);
749 static int set_rate_stm_pll4600c28(struct clk_hw *hw, unsigned long rate,
750 unsigned long parent_rate)
752 struct clkgen_pll *pll = to_clkgen_pll(hw);
753 struct stm_pll params;
755 unsigned long flags = 0;
757 if (!rate || !parent_rate)
760 if (!clk_pll4600c28_get_params(parent_rate, rate, ¶ms)) {
761 clk_pll4600c28_get_rate(parent_rate, ¶ms, &hwrate);
763 pr_debug("%s: %s rate %ld Invalid\n", __func__,
764 __clk_get_name(hw->clk), rate);
768 pr_debug("%s: %s new rate %ld [ndiv=0x%x] [idf=0x%x]\n",
769 __func__, __clk_get_name(hw->clk),
770 hwrate, (unsigned int)params.ndiv,
771 (unsigned int)params.idf);
776 pll->ndiv = params.ndiv;
777 pll->idf = params.idf;
779 __clkgen_pll_disable(hw);
782 spin_lock_irqsave(pll->lock, flags);
784 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
785 CLKGEN_WRITE(pll, idf, pll->idf);
788 spin_unlock_irqrestore(pll->lock, flags);
790 __clkgen_pll_enable(hw);
795 static const struct clk_ops st_pll1600c65_ops = {
796 .enable = clkgen_pll_enable,
797 .disable = clkgen_pll_disable,
798 .is_enabled = clkgen_pll_is_enabled,
799 .recalc_rate = recalc_stm_pll1600c65,
802 static const struct clk_ops st_pll800c65_ops = {
803 .enable = clkgen_pll_enable,
804 .disable = clkgen_pll_disable,
805 .is_enabled = clkgen_pll_is_enabled,
806 .recalc_rate = recalc_stm_pll800c65,
809 static const struct clk_ops stm_pll3200c32_ops = {
810 .enable = clkgen_pll_enable,
811 .disable = clkgen_pll_disable,
812 .is_enabled = clkgen_pll_is_enabled,
813 .recalc_rate = recalc_stm_pll3200c32,
816 static const struct clk_ops stm_pll3200c32_a9_ops = {
817 .enable = clkgen_pll_enable,
818 .disable = clkgen_pll_disable,
819 .is_enabled = clkgen_pll_is_enabled,
820 .recalc_rate = recalc_stm_pll3200c32,
821 .round_rate = round_rate_stm_pll3200c32,
822 .set_rate = set_rate_stm_pll3200c32,
825 static const struct clk_ops st_pll1200c32_ops = {
826 .enable = clkgen_pll_enable,
827 .disable = clkgen_pll_disable,
828 .is_enabled = clkgen_pll_is_enabled,
829 .recalc_rate = recalc_stm_pll1200c32,
832 static const struct clk_ops stm_pll4600c28_ops = {
833 .enable = clkgen_pll_enable,
834 .disable = clkgen_pll_disable,
835 .is_enabled = clkgen_pll_is_enabled,
836 .recalc_rate = recalc_stm_pll4600c28,
837 .round_rate = round_rate_stm_pll4600c28,
838 .set_rate = set_rate_stm_pll4600c28,
841 static struct clk * __init clkgen_pll_register(const char *parent_name,
842 struct clkgen_pll_data *pll_data,
844 const char *clk_name, spinlock_t *lock)
846 struct clkgen_pll *pll;
848 struct clk_init_data init;
850 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
852 return ERR_PTR(-ENOMEM);
854 init.name = clk_name;
855 init.ops = pll_data->ops;
857 init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE;
858 init.parent_names = &parent_name;
859 init.num_parents = 1;
861 pll->data = pll_data;
862 pll->regs_base = reg;
863 pll->hw.init = &init;
866 clk = clk_register(NULL, &pll->hw);
872 pr_debug("%s: parent %s rate %lu\n",
874 __clk_get_name(clk_get_parent(clk)),
880 static struct clk * __init clkgen_c65_lsdiv_register(const char *parent_name,
881 const char *clk_name)
885 clk = clk_register_fixed_factor(NULL, clk_name, parent_name, 0, 1, 2);
889 pr_debug("%s: parent %s rate %lu\n",
891 __clk_get_name(clk_get_parent(clk)),
896 static void __iomem * __init clkgen_get_register_base(
897 struct device_node *np)
899 struct device_node *pnode;
900 void __iomem *reg = NULL;
902 pnode = of_get_parent(np);
906 reg = of_iomap(pnode, 0);
912 #define CLKGENAx_PLL0_OFFSET 0x0
913 #define CLKGENAx_PLL1_OFFSET 0x4
915 static void __init clkgena_c65_pll_setup(struct device_node *np)
917 const int num_pll_outputs = 3;
918 struct clk_onecell_data *clk_data;
919 const char *parent_name;
921 const char *clk_name;
923 parent_name = of_clk_get_parent_name(np, 0);
927 reg = clkgen_get_register_base(np);
931 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
935 clk_data->clk_num = num_pll_outputs;
936 clk_data->clks = kzalloc(clk_data->clk_num * sizeof(struct clk *),
942 if (of_property_read_string_index(np, "clock-output-names",
947 * PLL0 HS (high speed) output
949 clk_data->clks[0] = clkgen_pll_register(parent_name,
950 (struct clkgen_pll_data *) &st_pll1600c65_ax,
951 reg + CLKGENAx_PLL0_OFFSET, clk_name, NULL);
953 if (IS_ERR(clk_data->clks[0]))
956 if (of_property_read_string_index(np, "clock-output-names",
961 * PLL0 LS (low speed) output, which is a fixed divide by 2 of the
964 clk_data->clks[1] = clkgen_c65_lsdiv_register(__clk_get_name
968 if (IS_ERR(clk_data->clks[1]))
971 if (of_property_read_string_index(np, "clock-output-names",
978 clk_data->clks[2] = clkgen_pll_register(parent_name,
979 (struct clkgen_pll_data *) &st_pll800c65_ax,
980 reg + CLKGENAx_PLL1_OFFSET, clk_name, NULL);
982 if (IS_ERR(clk_data->clks[2]))
985 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
989 kfree(clk_data->clks);
992 CLK_OF_DECLARE(clkgena_c65_plls,
993 "st,clkgena-plls-c65", clkgena_c65_pll_setup);
995 static struct clk * __init clkgen_odf_register(const char *parent_name,
997 struct clkgen_pll_data *pll_data,
999 spinlock_t *odf_lock,
1000 const char *odf_name)
1003 unsigned long flags;
1004 struct clk_gate *gate;
1005 struct clk_divider *div;
1007 flags = CLK_GET_RATE_NOCACHE | CLK_SET_RATE_PARENT;
1009 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1011 return ERR_PTR(-ENOMEM);
1013 gate->flags = CLK_GATE_SET_TO_DISABLE;
1014 gate->reg = reg + pll_data->odf_gate[odf].offset;
1015 gate->bit_idx = pll_data->odf_gate[odf].shift;
1016 gate->lock = odf_lock;
1018 div = kzalloc(sizeof(*div), GFP_KERNEL);
1021 return ERR_PTR(-ENOMEM);
1024 div->flags = CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO;
1025 div->reg = reg + pll_data->odf[odf].offset;
1026 div->shift = pll_data->odf[odf].shift;
1027 div->width = fls(pll_data->odf[odf].mask);
1028 div->lock = odf_lock;
1030 clk = clk_register_composite(NULL, odf_name, &parent_name, 1,
1032 &div->hw, &clk_divider_ops,
1033 &gate->hw, &clk_gate_ops,
1038 pr_debug("%s: parent %s rate %lu\n",
1039 __clk_get_name(clk),
1040 __clk_get_name(clk_get_parent(clk)),
1045 static const struct of_device_id c32_pll_of_match[] = {
1047 .compatible = "st,plls-c32-a1x-0",
1048 .data = &st_pll3200c32_a1x_0,
1051 .compatible = "st,plls-c32-a1x-1",
1052 .data = &st_pll3200c32_a1x_1,
1055 .compatible = "st,stih415-plls-c32-a9",
1056 .data = &st_pll3200c32_a9_415,
1059 .compatible = "st,stih415-plls-c32-ddr",
1060 .data = &st_pll3200c32_ddr_415,
1063 .compatible = "st,stih416-plls-c32-a9",
1064 .data = &st_pll3200c32_a9_416,
1067 .compatible = "st,stih416-plls-c32-ddr",
1068 .data = &st_pll3200c32_ddr_416,
1071 .compatible = "st,stih407-plls-c32-a0",
1072 .data = &st_pll3200c32_407_a0,
1075 .compatible = "st,plls-c32-cx_0",
1076 .data = &st_pll3200c32_cx_0,
1079 .compatible = "st,plls-c32-cx_1",
1080 .data = &st_pll3200c32_cx_1,
1083 .compatible = "st,stih407-plls-c32-a9",
1084 .data = &st_pll3200c32_407_a9,
1087 .compatible = "st,stih418-plls-c28-a9",
1088 .data = &st_pll4600c28_418_a9,
1093 static void __init clkgen_c32_pll_setup(struct device_node *np)
1095 const struct of_device_id *match;
1097 const char *parent_name, *pll_name;
1098 void __iomem *pll_base;
1100 struct clk_onecell_data *clk_data;
1101 struct clkgen_pll_data *data;
1103 match = of_match_node(c32_pll_of_match, np);
1105 pr_err("%s: No matching data\n", __func__);
1109 data = (struct clkgen_pll_data *) match->data;
1111 parent_name = of_clk_get_parent_name(np, 0);
1115 pll_base = clkgen_get_register_base(np);
1119 clk = clkgen_pll_register(parent_name, data, pll_base, np->name,
1124 pll_name = __clk_get_name(clk);
1126 num_odfs = data->num_odfs;
1128 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
1132 clk_data->clk_num = num_odfs;
1133 clk_data->clks = kzalloc(clk_data->clk_num * sizeof(struct clk *),
1136 if (!clk_data->clks)
1139 for (odf = 0; odf < num_odfs; odf++) {
1141 const char *clk_name;
1143 if (of_property_read_string_index(np, "clock-output-names",
1147 clk = clkgen_odf_register(pll_name, pll_base, data,
1148 odf, &clkgena_c32_odf_lock, clk_name);
1152 clk_data->clks[odf] = clk;
1155 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
1160 kfree(clk_data->clks);
1163 CLK_OF_DECLARE(clkgen_c32_pll, "st,clkgen-plls-c32", clkgen_c32_pll_setup);
1165 static const struct of_device_id c32_gpu_pll_of_match[] = {
1167 .compatible = "st,stih415-gpu-pll-c32",
1168 .data = &st_pll1200c32_gpu_415,
1171 .compatible = "st,stih416-gpu-pll-c32",
1172 .data = &st_pll1200c32_gpu_416,
1177 static void __init clkgengpu_c32_pll_setup(struct device_node *np)
1179 const struct of_device_id *match;
1181 const char *parent_name;
1183 const char *clk_name;
1184 struct clkgen_pll_data *data;
1186 match = of_match_node(c32_gpu_pll_of_match, np);
1188 pr_err("%s: No matching data\n", __func__);
1192 data = (struct clkgen_pll_data *)match->data;
1194 parent_name = of_clk_get_parent_name(np, 0);
1198 reg = clkgen_get_register_base(np);
1202 if (of_property_read_string_index(np, "clock-output-names",
1207 * PLL 1200MHz output
1209 clk = clkgen_pll_register(parent_name, data, reg, clk_name, data->lock);
1212 of_clk_add_provider(np, of_clk_src_simple_get, clk);
1216 CLK_OF_DECLARE(clkgengpu_c32_pll,
1217 "st,clkgengpu-pll-c32", clkgengpu_c32_pll_setup);