]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/clk/qcom/clk-alpha-pll.c
clk: qcom: Enable FSM mode for votable alpha PLLs
[karo-tx-linux.git] / drivers / clk / qcom / clk-alpha-pll.c
1 /*
2  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/export.h>
16 #include <linux/clk-provider.h>
17 #include <linux/regmap.h>
18 #include <linux/delay.h>
19
20 #include "clk-alpha-pll.h"
21 #include "common.h"
22
23 #define PLL_MODE                0x00
24 # define PLL_OUTCTRL            BIT(0)
25 # define PLL_BYPASSNL           BIT(1)
26 # define PLL_RESET_N            BIT(2)
27 # define PLL_OFFLINE_REQ        BIT(7)
28 # define PLL_LOCK_COUNT_SHIFT   8
29 # define PLL_LOCK_COUNT_MASK    0x3f
30 # define PLL_BIAS_COUNT_SHIFT   14
31 # define PLL_BIAS_COUNT_MASK    0x3f
32 # define PLL_VOTE_FSM_ENA       BIT(20)
33 # define PLL_FSM_ENA            BIT(20)
34 # define PLL_VOTE_FSM_RESET     BIT(21)
35 # define PLL_OFFLINE_ACK        BIT(28)
36 # define PLL_ACTIVE_FLAG        BIT(30)
37 # define PLL_LOCK_DET           BIT(31)
38
39 #define PLL_L_VAL               0x04
40 #define PLL_ALPHA_VAL           0x08
41 #define PLL_ALPHA_VAL_U         0x0c
42
43 #define PLL_USER_CTL            0x10
44 # define PLL_POST_DIV_SHIFT     8
45 # define PLL_POST_DIV_MASK      0xf
46 # define PLL_ALPHA_EN           BIT(24)
47 # define PLL_VCO_SHIFT          20
48 # define PLL_VCO_MASK           0x3
49
50 #define PLL_USER_CTL_U          0x14
51
52 #define PLL_CONFIG_CTL          0x18
53 #define PLL_CONFIG_CTL_U        0x20
54 #define PLL_TEST_CTL            0x1c
55 #define PLL_TEST_CTL_U          0x20
56 #define PLL_STATUS              0x24
57
58 /*
59  * Even though 40 bits are present, use only 32 for ease of calculation.
60  */
61 #define ALPHA_REG_BITWIDTH      40
62 #define ALPHA_BITWIDTH          32
63 #define ALPHA_16BIT_MASK        0xffff
64
65 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
66                                            struct clk_alpha_pll, clkr)
67
68 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
69                                            struct clk_alpha_pll_postdiv, clkr)
70
71 static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
72                         const char *action)
73 {
74         u32 val, off;
75         int count;
76         int ret;
77         const char *name = clk_hw_get_name(&pll->clkr.hw);
78
79         off = pll->offset;
80         ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
81         if (ret)
82                 return ret;
83
84         for (count = 100; count > 0; count--) {
85                 ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
86                 if (ret)
87                         return ret;
88                 if (inverse && !(val & mask))
89                         return 0;
90                 else if ((val & mask) == mask)
91                         return 0;
92
93                 udelay(1);
94         }
95
96         WARN(1, "%s failed to %s!\n", name, action);
97         return -ETIMEDOUT;
98 }
99
100 #define wait_for_pll_enable_active(pll) \
101         wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
102
103 #define wait_for_pll_enable_lock(pll) \
104         wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
105
106 #define wait_for_pll_disable(pll) \
107         wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
108
109 #define wait_for_pll_offline(pll) \
110         wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
111
112 void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
113                              const struct alpha_pll_config *config)
114 {
115         u32 val, mask;
116         u32 off = pll->offset;
117
118         regmap_write(regmap, off + PLL_L_VAL, config->l);
119         regmap_write(regmap, off + PLL_ALPHA_VAL, config->alpha);
120         regmap_write(regmap, off + PLL_CONFIG_CTL, config->config_ctl_val);
121         regmap_write(regmap, off + PLL_CONFIG_CTL_U, config->config_ctl_hi_val);
122
123         val = config->main_output_mask;
124         val |= config->aux_output_mask;
125         val |= config->aux2_output_mask;
126         val |= config->early_output_mask;
127         val |= config->pre_div_val;
128         val |= config->post_div_val;
129         val |= config->vco_val;
130
131         mask = config->main_output_mask;
132         mask |= config->aux_output_mask;
133         mask |= config->aux2_output_mask;
134         mask |= config->early_output_mask;
135         mask |= config->pre_div_mask;
136         mask |= config->post_div_mask;
137         mask |= config->vco_mask;
138
139         regmap_update_bits(regmap, off + PLL_USER_CTL, mask, val);
140
141         if (pll->flags & SUPPORTS_FSM_MODE)
142                 qcom_pll_set_fsm_mode(regmap, off + PLL_MODE, 6, 0);
143 }
144
145 static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
146 {
147         int ret;
148         u32 val, off;
149         struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
150
151         off = pll->offset;
152         ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
153         if (ret)
154                 return ret;
155
156         val |= PLL_FSM_ENA;
157
158         if (pll->flags & SUPPORTS_OFFLINE_REQ)
159                 val &= ~PLL_OFFLINE_REQ;
160
161         ret = regmap_write(pll->clkr.regmap, off + PLL_MODE, val);
162         if (ret)
163                 return ret;
164
165         /* Make sure enable request goes through before waiting for update */
166         mb();
167
168         return wait_for_pll_enable_active(pll);
169 }
170
171 static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
172 {
173         int ret;
174         u32 val, off;
175         struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
176
177         off = pll->offset;
178         ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
179         if (ret)
180                 return;
181
182         if (pll->flags & SUPPORTS_OFFLINE_REQ) {
183                 ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
184                                          PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
185                 if (ret)
186                         return;
187
188                 ret = wait_for_pll_offline(pll);
189                 if (ret)
190                         return;
191         }
192
193         /* Disable hwfsm */
194         ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
195                                  PLL_FSM_ENA, 0);
196         if (ret)
197                 return;
198
199         wait_for_pll_disable(pll);
200 }
201
202 static int clk_alpha_pll_enable(struct clk_hw *hw)
203 {
204         int ret;
205         struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
206         u32 val, mask, off;
207
208         off = pll->offset;
209
210         mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
211         ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
212         if (ret)
213                 return ret;
214
215         /* If in FSM mode, just vote for it */
216         if (val & PLL_VOTE_FSM_ENA) {
217                 ret = clk_enable_regmap(hw);
218                 if (ret)
219                         return ret;
220                 return wait_for_pll_enable_active(pll);
221         }
222
223         /* Skip if already enabled */
224         if ((val & mask) == mask)
225                 return 0;
226
227         ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
228                                  PLL_BYPASSNL, PLL_BYPASSNL);
229         if (ret)
230                 return ret;
231
232         /*
233          * H/W requires a 5us delay between disabling the bypass and
234          * de-asserting the reset.
235          */
236         mb();
237         udelay(5);
238
239         ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
240                                  PLL_RESET_N, PLL_RESET_N);
241         if (ret)
242                 return ret;
243
244         ret = wait_for_pll_enable_lock(pll);
245         if (ret)
246                 return ret;
247
248         ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
249                                  PLL_OUTCTRL, PLL_OUTCTRL);
250
251         /* Ensure that the write above goes through before returning. */
252         mb();
253         return ret;
254 }
255
256 static void clk_alpha_pll_disable(struct clk_hw *hw)
257 {
258         int ret;
259         struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
260         u32 val, mask, off;
261
262         off = pll->offset;
263
264         ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
265         if (ret)
266                 return;
267
268         /* If in FSM mode, just unvote it */
269         if (val & PLL_VOTE_FSM_ENA) {
270                 clk_disable_regmap(hw);
271                 return;
272         }
273
274         mask = PLL_OUTCTRL;
275         regmap_update_bits(pll->clkr.regmap, off + PLL_MODE, mask, 0);
276
277         /* Delay of 2 output clock ticks required until output is disabled */
278         mb();
279         udelay(1);
280
281         mask = PLL_RESET_N | PLL_BYPASSNL;
282         regmap_update_bits(pll->clkr.regmap, off + PLL_MODE, mask, 0);
283 }
284
285 static unsigned long alpha_pll_calc_rate(u64 prate, u32 l, u32 a)
286 {
287         return (prate * l) + ((prate * a) >> ALPHA_BITWIDTH);
288 }
289
290 static unsigned long
291 alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a)
292 {
293         u64 remainder;
294         u64 quotient;
295
296         quotient = rate;
297         remainder = do_div(quotient, prate);
298         *l = quotient;
299
300         if (!remainder) {
301                 *a = 0;
302                 return rate;
303         }
304
305         /* Upper ALPHA_BITWIDTH bits of Alpha */
306         quotient = remainder << ALPHA_BITWIDTH;
307         remainder = do_div(quotient, prate);
308
309         if (remainder)
310                 quotient++;
311
312         *a = quotient;
313         return alpha_pll_calc_rate(prate, *l, *a);
314 }
315
316 static const struct pll_vco *
317 alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
318 {
319         const struct pll_vco *v = pll->vco_table;
320         const struct pll_vco *end = v + pll->num_vco;
321
322         for (; v < end; v++)
323                 if (rate >= v->min_freq && rate <= v->max_freq)
324                         return v;
325
326         return NULL;
327 }
328
329 static unsigned long
330 clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
331 {
332         u32 l, low, high, ctl;
333         u64 a = 0, prate = parent_rate;
334         struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
335         u32 off = pll->offset;
336
337         regmap_read(pll->clkr.regmap, off + PLL_L_VAL, &l);
338
339         regmap_read(pll->clkr.regmap, off + PLL_USER_CTL, &ctl);
340         if (ctl & PLL_ALPHA_EN) {
341                 regmap_read(pll->clkr.regmap, off + PLL_ALPHA_VAL, &low);
342                 if (pll->flags & SUPPORTS_16BIT_ALPHA) {
343                         a = low & ALPHA_16BIT_MASK;
344                 } else {
345                         regmap_read(pll->clkr.regmap, off + PLL_ALPHA_VAL_U,
346                                     &high);
347                         a = (u64)high << 32 | low;
348                         a >>= ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH;
349                 }
350         }
351
352         return alpha_pll_calc_rate(prate, l, a);
353 }
354
355 static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
356                                   unsigned long prate)
357 {
358         struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
359         const struct pll_vco *vco;
360         u32 l, off = pll->offset;
361         u64 a;
362
363         rate = alpha_pll_round_rate(rate, prate, &l, &a);
364         vco = alpha_pll_find_vco(pll, rate);
365         if (!vco) {
366                 pr_err("alpha pll not in a valid vco range\n");
367                 return -EINVAL;
368         }
369
370         regmap_write(pll->clkr.regmap, off + PLL_L_VAL, l);
371
372         if (pll->flags & SUPPORTS_16BIT_ALPHA) {
373                 regmap_write(pll->clkr.regmap, off + PLL_ALPHA_VAL,
374                              a & ALPHA_16BIT_MASK);
375         } else {
376                 a <<= (ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH);
377                 regmap_write(pll->clkr.regmap, off + PLL_ALPHA_VAL_U, a >> 32);
378         }
379
380         regmap_update_bits(pll->clkr.regmap, off + PLL_USER_CTL,
381                            PLL_VCO_MASK << PLL_VCO_SHIFT,
382                            vco->val << PLL_VCO_SHIFT);
383
384         regmap_update_bits(pll->clkr.regmap, off + PLL_USER_CTL, PLL_ALPHA_EN,
385                            PLL_ALPHA_EN);
386
387         return 0;
388 }
389
390 static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
391                                      unsigned long *prate)
392 {
393         struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
394         u32 l;
395         u64 a;
396         unsigned long min_freq, max_freq;
397
398         rate = alpha_pll_round_rate(rate, *prate, &l, &a);
399         if (alpha_pll_find_vco(pll, rate))
400                 return rate;
401
402         min_freq = pll->vco_table[0].min_freq;
403         max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
404
405         return clamp(rate, min_freq, max_freq);
406 }
407
408 const struct clk_ops clk_alpha_pll_ops = {
409         .enable = clk_alpha_pll_enable,
410         .disable = clk_alpha_pll_disable,
411         .recalc_rate = clk_alpha_pll_recalc_rate,
412         .round_rate = clk_alpha_pll_round_rate,
413         .set_rate = clk_alpha_pll_set_rate,
414 };
415 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
416
417 const struct clk_ops clk_alpha_pll_hwfsm_ops = {
418         .enable = clk_alpha_pll_hwfsm_enable,
419         .disable = clk_alpha_pll_hwfsm_disable,
420         .recalc_rate = clk_alpha_pll_recalc_rate,
421         .round_rate = clk_alpha_pll_round_rate,
422         .set_rate = clk_alpha_pll_set_rate,
423 };
424 EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
425
426 static unsigned long
427 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
428 {
429         struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
430         u32 ctl;
431
432         regmap_read(pll->clkr.regmap, pll->offset + PLL_USER_CTL, &ctl);
433
434         ctl >>= PLL_POST_DIV_SHIFT;
435         ctl &= PLL_POST_DIV_MASK;
436
437         return parent_rate >> fls(ctl);
438 }
439
440 static const struct clk_div_table clk_alpha_div_table[] = {
441         { 0x0, 1 },
442         { 0x1, 2 },
443         { 0x3, 4 },
444         { 0x7, 8 },
445         { 0xf, 16 },
446         { }
447 };
448
449 static long
450 clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
451                                  unsigned long *prate)
452 {
453         struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
454
455         return divider_round_rate(hw, rate, prate, clk_alpha_div_table,
456                                   pll->width, CLK_DIVIDER_POWER_OF_TWO);
457 }
458
459 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
460                                           unsigned long parent_rate)
461 {
462         struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
463         int div;
464
465         /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
466         div = DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
467
468         return regmap_update_bits(pll->clkr.regmap, pll->offset + PLL_USER_CTL,
469                                   PLL_POST_DIV_MASK << PLL_POST_DIV_SHIFT,
470                                   div << PLL_POST_DIV_SHIFT);
471 }
472
473 const struct clk_ops clk_alpha_pll_postdiv_ops = {
474         .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
475         .round_rate = clk_alpha_pll_postdiv_round_rate,
476         .set_rate = clk_alpha_pll_postdiv_set_rate,
477 };
478 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);