clk: iproc: Fix error in the pll post divider rate calculation
[linux-2.6-microblaze.git] / drivers / clk / bcm / clk-iproc-pll.c
1 /*
2  * Copyright (C) 2014 Broadcom Corporation
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation version 2.
7  *
8  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9  * kind, whether express or implied; without even the implied warranty
10  * of 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/err.h>
16 #include <linux/clk-provider.h>
17 #include <linux/io.h>
18 #include <linux/of.h>
19 #include <linux/clkdev.h>
20 #include <linux/of_address.h>
21 #include <linux/delay.h>
22
23 #include "clk-iproc.h"
24
25 #define PLL_VCO_HIGH_SHIFT 19
26 #define PLL_VCO_LOW_SHIFT  30
27
28 /*
29  * PLL MACRO_SELECT modes 0 to 5 choose pre-calculated PLL output frequencies
30  * from a look-up table. Mode 7 allows user to manipulate PLL clock dividers
31  */
32 #define PLL_USER_MODE 7
33
34 /* number of delay loops waiting for PLL to lock */
35 #define LOCK_DELAY 100
36
37 /* number of VCO frequency bands */
38 #define NUM_FREQ_BANDS 8
39
40 #define NUM_KP_BANDS 3
41 enum kp_band {
42         KP_BAND_MID = 0,
43         KP_BAND_HIGH,
44         KP_BAND_HIGH_HIGH
45 };
46
47 static const unsigned int kp_table[NUM_KP_BANDS][NUM_FREQ_BANDS] = {
48         { 5, 6, 6, 7, 7, 8, 9, 10 },
49         { 4, 4, 5, 5, 6, 7, 8, 9  },
50         { 4, 5, 5, 6, 7, 8, 9, 10 },
51 };
52
53 static const unsigned long ref_freq_table[NUM_FREQ_BANDS][2] = {
54         { 10000000,  12500000  },
55         { 12500000,  15000000  },
56         { 15000000,  20000000  },
57         { 20000000,  25000000  },
58         { 25000000,  50000000  },
59         { 50000000,  75000000  },
60         { 75000000,  100000000 },
61         { 100000000, 125000000 },
62 };
63
64 enum vco_freq_range {
65         VCO_LOW       = 700000000U,
66         VCO_MID       = 1200000000U,
67         VCO_HIGH      = 2200000000U,
68         VCO_HIGH_HIGH = 3100000000U,
69         VCO_MAX       = 4000000000U,
70 };
71
72 struct iproc_pll;
73
74 struct iproc_clk {
75         struct clk_hw hw;
76         const char *name;
77         struct iproc_pll *pll;
78         unsigned long rate;
79         const struct iproc_clk_ctrl *ctrl;
80 };
81
82 struct iproc_pll {
83         void __iomem *status_base;
84         void __iomem *control_base;
85         void __iomem *pwr_base;
86         void __iomem *asiu_base;
87
88         const struct iproc_pll_ctrl *ctrl;
89         const struct iproc_pll_vco_param *vco_param;
90         unsigned int num_vco_entries;
91
92         struct clk_hw_onecell_data *clk_data;
93         struct iproc_clk *clks;
94 };
95
96 #define to_iproc_clk(hw) container_of(hw, struct iproc_clk, hw)
97
98 static int pll_calc_param(unsigned long target_rate,
99                         unsigned long parent_rate,
100                         struct iproc_pll_vco_param *vco_out)
101 {
102         u64 ndiv_int, ndiv_frac, residual;
103
104         ndiv_int = target_rate / parent_rate;
105
106         if (!ndiv_int || (ndiv_int > 255))
107                 return -EINVAL;
108
109         residual = target_rate - (ndiv_int * parent_rate);
110         residual <<= 20;
111
112         /*
113          * Add half of the divisor so the result will be rounded to closest
114          * instead of rounded down.
115          */
116         residual += (parent_rate / 2);
117         ndiv_frac = div64_u64((u64)residual, (u64)parent_rate);
118
119         vco_out->ndiv_int = ndiv_int;
120         vco_out->ndiv_frac = ndiv_frac;
121         vco_out->pdiv = 1;
122
123         vco_out->rate = vco_out->ndiv_int * parent_rate;
124         residual = (u64)vco_out->ndiv_frac * (u64)parent_rate;
125         residual >>= 20;
126         vco_out->rate += residual;
127
128         return 0;
129 }
130
131 /*
132  * Based on the target frequency, find a match from the VCO frequency parameter
133  * table and return its index
134  */
135 static int pll_get_rate_index(struct iproc_pll *pll, unsigned int target_rate)
136 {
137         int i;
138
139         for (i = 0; i < pll->num_vco_entries; i++)
140                 if (target_rate == pll->vco_param[i].rate)
141                         break;
142
143         if (i >= pll->num_vco_entries)
144                 return -EINVAL;
145
146         return i;
147 }
148
149 static int get_kp(unsigned long ref_freq, enum kp_band kp_index)
150 {
151         int i;
152
153         if (ref_freq < ref_freq_table[0][0])
154                 return -EINVAL;
155
156         for (i = 0; i < NUM_FREQ_BANDS; i++) {
157                 if (ref_freq >= ref_freq_table[i][0] &&
158                     ref_freq < ref_freq_table[i][1])
159                         return kp_table[kp_index][i];
160         }
161         return -EINVAL;
162 }
163
164 static int pll_wait_for_lock(struct iproc_pll *pll)
165 {
166         int i;
167         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
168
169         for (i = 0; i < LOCK_DELAY; i++) {
170                 u32 val = readl(pll->status_base + ctrl->status.offset);
171
172                 if (val & (1 << ctrl->status.shift))
173                         return 0;
174                 udelay(10);
175         }
176
177         return -EIO;
178 }
179
180 static void iproc_pll_write(const struct iproc_pll *pll, void __iomem *base,
181                             const u32 offset, u32 val)
182 {
183         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
184
185         writel(val, base + offset);
186
187         if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK &&
188                      (base == pll->status_base || base == pll->control_base)))
189                 val = readl(base + offset);
190 }
191
192 static void __pll_disable(struct iproc_pll *pll)
193 {
194         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
195         u32 val;
196
197         if (ctrl->flags & IPROC_CLK_PLL_ASIU) {
198                 val = readl(pll->asiu_base + ctrl->asiu.offset);
199                 val &= ~(1 << ctrl->asiu.en_shift);
200                 iproc_pll_write(pll, pll->asiu_base, ctrl->asiu.offset, val);
201         }
202
203         if (ctrl->flags & IPROC_CLK_EMBED_PWRCTRL) {
204                 val = readl(pll->control_base + ctrl->aon.offset);
205                 val |= bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift;
206                 iproc_pll_write(pll, pll->control_base, ctrl->aon.offset, val);
207         }
208
209         if (pll->pwr_base) {
210                 /* latch input value so core power can be shut down */
211                 val = readl(pll->pwr_base + ctrl->aon.offset);
212                 val |= 1 << ctrl->aon.iso_shift;
213                 iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
214
215                 /* power down the core */
216                 val &= ~(bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift);
217                 iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
218         }
219 }
220
221 static int __pll_enable(struct iproc_pll *pll)
222 {
223         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
224         u32 val;
225
226         if (ctrl->flags & IPROC_CLK_EMBED_PWRCTRL) {
227                 val = readl(pll->control_base + ctrl->aon.offset);
228                 val &= ~(bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift);
229                 iproc_pll_write(pll, pll->control_base, ctrl->aon.offset, val);
230         }
231
232         if (pll->pwr_base) {
233                 /* power up the PLL and make sure it's not latched */
234                 val = readl(pll->pwr_base + ctrl->aon.offset);
235                 val |= bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift;
236                 val &= ~(1 << ctrl->aon.iso_shift);
237                 iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
238         }
239
240         /* certain PLLs also need to be ungated from the ASIU top level */
241         if (ctrl->flags & IPROC_CLK_PLL_ASIU) {
242                 val = readl(pll->asiu_base + ctrl->asiu.offset);
243                 val |= (1 << ctrl->asiu.en_shift);
244                 iproc_pll_write(pll, pll->asiu_base, ctrl->asiu.offset, val);
245         }
246
247         return 0;
248 }
249
250 static void __pll_put_in_reset(struct iproc_pll *pll)
251 {
252         u32 val;
253         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
254         const struct iproc_pll_reset_ctrl *reset = &ctrl->reset;
255
256         val = readl(pll->control_base + reset->offset);
257         if (ctrl->flags & IPROC_CLK_PLL_RESET_ACTIVE_LOW)
258                 val |= BIT(reset->reset_shift) | BIT(reset->p_reset_shift);
259         else
260                 val &= ~(BIT(reset->reset_shift) | BIT(reset->p_reset_shift));
261         iproc_pll_write(pll, pll->control_base, reset->offset, val);
262 }
263
264 static void __pll_bring_out_reset(struct iproc_pll *pll, unsigned int kp,
265                                   unsigned int ka, unsigned int ki)
266 {
267         u32 val;
268         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
269         const struct iproc_pll_reset_ctrl *reset = &ctrl->reset;
270         const struct iproc_pll_dig_filter_ctrl *dig_filter = &ctrl->dig_filter;
271
272         val = readl(pll->control_base + dig_filter->offset);
273         val &= ~(bit_mask(dig_filter->ki_width) << dig_filter->ki_shift |
274                 bit_mask(dig_filter->kp_width) << dig_filter->kp_shift |
275                 bit_mask(dig_filter->ka_width) << dig_filter->ka_shift);
276         val |= ki << dig_filter->ki_shift | kp << dig_filter->kp_shift |
277                ka << dig_filter->ka_shift;
278         iproc_pll_write(pll, pll->control_base, dig_filter->offset, val);
279
280         val = readl(pll->control_base + reset->offset);
281         if (ctrl->flags & IPROC_CLK_PLL_RESET_ACTIVE_LOW)
282                 val &= ~(BIT(reset->reset_shift) | BIT(reset->p_reset_shift));
283         else
284                 val |= BIT(reset->reset_shift) | BIT(reset->p_reset_shift);
285         iproc_pll_write(pll, pll->control_base, reset->offset, val);
286 }
287
288 static int pll_set_rate(struct iproc_clk *clk, struct iproc_pll_vco_param *vco,
289                         unsigned long parent_rate)
290 {
291         struct iproc_pll *pll = clk->pll;
292         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
293         int ka = 0, ki, kp, ret;
294         unsigned long rate = vco->rate;
295         u32 val;
296         enum kp_band kp_index;
297         unsigned long ref_freq;
298
299         /*
300          * reference frequency = parent frequency / PDIV
301          * If PDIV = 0, then it becomes a multiplier (x2)
302          */
303         if (vco->pdiv == 0)
304                 ref_freq = parent_rate * 2;
305         else
306                 ref_freq = parent_rate / vco->pdiv;
307
308         /* determine Ki and Kp index based on target VCO frequency */
309         if (rate >= VCO_LOW && rate < VCO_HIGH) {
310                 ki = 4;
311                 kp_index = KP_BAND_MID;
312         } else if (rate >= VCO_HIGH && rate < VCO_HIGH_HIGH) {
313                 ki = 3;
314                 kp_index = KP_BAND_HIGH;
315         } else if (rate >= VCO_HIGH_HIGH && rate < VCO_MAX) {
316                 ki = 3;
317                 kp_index = KP_BAND_HIGH_HIGH;
318         } else {
319                 pr_err("%s: pll: %s has invalid rate: %lu\n", __func__,
320                                 clk->name, rate);
321                 return -EINVAL;
322         }
323
324         kp = get_kp(ref_freq, kp_index);
325         if (kp < 0) {
326                 pr_err("%s: pll: %s has invalid kp\n", __func__, clk->name);
327                 return kp;
328         }
329
330         ret = __pll_enable(pll);
331         if (ret) {
332                 pr_err("%s: pll: %s fails to enable\n", __func__, clk->name);
333                 return ret;
334         }
335
336         /* put PLL in reset */
337         __pll_put_in_reset(pll);
338
339         /* set PLL in user mode before modifying PLL controls */
340         if (ctrl->flags & IPROC_CLK_PLL_USER_MODE_ON) {
341                 val = readl(pll->control_base + ctrl->macro_mode.offset);
342                 val &= ~(bit_mask(ctrl->macro_mode.width) <<
343                         ctrl->macro_mode.shift);
344                 val |= PLL_USER_MODE << ctrl->macro_mode.shift;
345                 iproc_pll_write(pll, pll->control_base,
346                         ctrl->macro_mode.offset, val);
347         }
348
349         iproc_pll_write(pll, pll->control_base, ctrl->vco_ctrl.u_offset, 0);
350
351         val = readl(pll->control_base + ctrl->vco_ctrl.l_offset);
352
353         if (rate >= VCO_LOW && rate < VCO_MID)
354                 val |= (1 << PLL_VCO_LOW_SHIFT);
355
356         if (rate < VCO_HIGH)
357                 val &= ~(1 << PLL_VCO_HIGH_SHIFT);
358         else
359                 val |= (1 << PLL_VCO_HIGH_SHIFT);
360
361         iproc_pll_write(pll, pll->control_base, ctrl->vco_ctrl.l_offset, val);
362
363         /* program integer part of NDIV */
364         val = readl(pll->control_base + ctrl->ndiv_int.offset);
365         val &= ~(bit_mask(ctrl->ndiv_int.width) << ctrl->ndiv_int.shift);
366         val |= vco->ndiv_int << ctrl->ndiv_int.shift;
367         iproc_pll_write(pll, pll->control_base, ctrl->ndiv_int.offset, val);
368
369         /* program fractional part of NDIV */
370         if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
371                 val = readl(pll->control_base + ctrl->ndiv_frac.offset);
372                 val &= ~(bit_mask(ctrl->ndiv_frac.width) <<
373                          ctrl->ndiv_frac.shift);
374                 val |= vco->ndiv_frac << ctrl->ndiv_frac.shift;
375                 iproc_pll_write(pll, pll->control_base, ctrl->ndiv_frac.offset,
376                                 val);
377         }
378
379         /* program PDIV */
380         val = readl(pll->control_base + ctrl->pdiv.offset);
381         val &= ~(bit_mask(ctrl->pdiv.width) << ctrl->pdiv.shift);
382         val |= vco->pdiv << ctrl->pdiv.shift;
383         iproc_pll_write(pll, pll->control_base, ctrl->pdiv.offset, val);
384
385         __pll_bring_out_reset(pll, kp, ka, ki);
386
387         ret = pll_wait_for_lock(pll);
388         if (ret < 0) {
389                 pr_err("%s: pll: %s failed to lock\n", __func__, clk->name);
390                 return ret;
391         }
392
393         return 0;
394 }
395
396 static int iproc_pll_enable(struct clk_hw *hw)
397 {
398         struct iproc_clk *clk = to_iproc_clk(hw);
399         struct iproc_pll *pll = clk->pll;
400
401         return __pll_enable(pll);
402 }
403
404 static void iproc_pll_disable(struct clk_hw *hw)
405 {
406         struct iproc_clk *clk = to_iproc_clk(hw);
407         struct iproc_pll *pll = clk->pll;
408         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
409
410         if (ctrl->flags & IPROC_CLK_AON)
411                 return;
412
413         __pll_disable(pll);
414 }
415
416 static unsigned long iproc_pll_recalc_rate(struct clk_hw *hw,
417                                            unsigned long parent_rate)
418 {
419         struct iproc_clk *clk = to_iproc_clk(hw);
420         struct iproc_pll *pll = clk->pll;
421         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
422         u32 val;
423         u64 ndiv, ndiv_int, ndiv_frac;
424         unsigned int pdiv;
425
426         if (parent_rate == 0)
427                 return 0;
428
429         /* PLL needs to be locked */
430         val = readl(pll->status_base + ctrl->status.offset);
431         if ((val & (1 << ctrl->status.shift)) == 0) {
432                 clk->rate = 0;
433                 return 0;
434         }
435
436         /*
437          * PLL output frequency =
438          *
439          * ((ndiv_int + ndiv_frac / 2^20) * (parent clock rate / pdiv)
440          */
441         val = readl(pll->control_base + ctrl->ndiv_int.offset);
442         ndiv_int = (val >> ctrl->ndiv_int.shift) &
443                 bit_mask(ctrl->ndiv_int.width);
444         ndiv = ndiv_int << 20;
445
446         if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
447                 val = readl(pll->control_base + ctrl->ndiv_frac.offset);
448                 ndiv_frac = (val >> ctrl->ndiv_frac.shift) &
449                         bit_mask(ctrl->ndiv_frac.width);
450                 ndiv += ndiv_frac;
451         }
452
453         val = readl(pll->control_base + ctrl->pdiv.offset);
454         pdiv = (val >> ctrl->pdiv.shift) & bit_mask(ctrl->pdiv.width);
455
456         clk->rate = (ndiv * parent_rate) >> 20;
457
458         if (pdiv == 0)
459                 clk->rate *= 2;
460         else
461                 clk->rate /= pdiv;
462
463         return clk->rate;
464 }
465
466 static int iproc_pll_determine_rate(struct clk_hw *hw,
467                 struct clk_rate_request *req)
468 {
469         unsigned int  i;
470         struct iproc_clk *clk = to_iproc_clk(hw);
471         struct iproc_pll *pll = clk->pll;
472         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
473         unsigned long  diff, best_diff;
474         unsigned int  best_idx = 0;
475         int ret;
476
477         if (req->rate == 0 || req->best_parent_rate == 0)
478                 return -EINVAL;
479
480         if (ctrl->flags & IPROC_CLK_PLL_CALC_PARAM) {
481                 struct iproc_pll_vco_param vco_param;
482
483                 ret = pll_calc_param(req->rate, req->best_parent_rate,
484                                         &vco_param);
485                 if (ret)
486                         return ret;
487
488                 req->rate = vco_param.rate;
489                 return 0;
490         }
491
492         if (!pll->vco_param)
493                 return -EINVAL;
494
495         best_diff = ULONG_MAX;
496         for (i = 0; i < pll->num_vco_entries; i++) {
497                 diff = abs(req->rate - pll->vco_param[i].rate);
498                 if (diff <= best_diff) {
499                         best_diff = diff;
500                         best_idx = i;
501                 }
502                 /* break now if perfect match */
503                 if (diff == 0)
504                         break;
505         }
506
507         req->rate = pll->vco_param[best_idx].rate;
508
509         return 0;
510 }
511
512 static int iproc_pll_set_rate(struct clk_hw *hw, unsigned long rate,
513                 unsigned long parent_rate)
514 {
515         struct iproc_clk *clk = to_iproc_clk(hw);
516         struct iproc_pll *pll = clk->pll;
517         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
518         struct iproc_pll_vco_param vco_param;
519         int rate_index, ret;
520
521         if (ctrl->flags & IPROC_CLK_PLL_CALC_PARAM) {
522                 ret = pll_calc_param(rate, parent_rate, &vco_param);
523                 if (ret)
524                         return ret;
525         } else {
526                 rate_index = pll_get_rate_index(pll, rate);
527                 if (rate_index < 0)
528                         return rate_index;
529
530                 vco_param = pll->vco_param[rate_index];
531         }
532
533         ret = pll_set_rate(clk, &vco_param, parent_rate);
534         return ret;
535 }
536
537 static const struct clk_ops iproc_pll_ops = {
538         .enable = iproc_pll_enable,
539         .disable = iproc_pll_disable,
540         .recalc_rate = iproc_pll_recalc_rate,
541         .determine_rate = iproc_pll_determine_rate,
542         .set_rate = iproc_pll_set_rate,
543 };
544
545 static int iproc_clk_enable(struct clk_hw *hw)
546 {
547         struct iproc_clk *clk = to_iproc_clk(hw);
548         const struct iproc_clk_ctrl *ctrl = clk->ctrl;
549         struct iproc_pll *pll = clk->pll;
550         u32 val;
551
552         /* channel enable is active low */
553         val = readl(pll->control_base + ctrl->enable.offset);
554         val &= ~(1 << ctrl->enable.enable_shift);
555         iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
556
557         /* also make sure channel is not held */
558         val = readl(pll->control_base + ctrl->enable.offset);
559         val &= ~(1 << ctrl->enable.hold_shift);
560         iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
561
562         return 0;
563 }
564
565 static void iproc_clk_disable(struct clk_hw *hw)
566 {
567         struct iproc_clk *clk = to_iproc_clk(hw);
568         const struct iproc_clk_ctrl *ctrl = clk->ctrl;
569         struct iproc_pll *pll = clk->pll;
570         u32 val;
571
572         if (ctrl->flags & IPROC_CLK_AON)
573                 return;
574
575         val = readl(pll->control_base + ctrl->enable.offset);
576         val |= 1 << ctrl->enable.enable_shift;
577         iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
578 }
579
580 static unsigned long iproc_clk_recalc_rate(struct clk_hw *hw,
581                 unsigned long parent_rate)
582 {
583         struct iproc_clk *clk = to_iproc_clk(hw);
584         const struct iproc_clk_ctrl *ctrl = clk->ctrl;
585         struct iproc_pll *pll = clk->pll;
586         u32 val;
587         unsigned int mdiv;
588
589         if (parent_rate == 0)
590                 return 0;
591
592         val = readl(pll->control_base + ctrl->mdiv.offset);
593         mdiv = (val >> ctrl->mdiv.shift) & bit_mask(ctrl->mdiv.width);
594         if (mdiv == 0)
595                 mdiv = 256;
596
597         if (ctrl->flags & IPROC_CLK_MCLK_DIV_BY_2)
598                 clk->rate = parent_rate / (mdiv * 2);
599         else
600                 clk->rate = parent_rate / mdiv;
601
602         return clk->rate;
603 }
604
605 static int iproc_clk_determine_rate(struct clk_hw *hw,
606                 struct clk_rate_request *req)
607 {
608         unsigned int bestdiv;
609
610         if (req->rate == 0)
611                 return -EINVAL;
612         if (req->rate == req->best_parent_rate)
613                 return 0;
614
615         bestdiv = DIV_ROUND_CLOSEST(req->best_parent_rate, req->rate);
616         if (bestdiv < 2)
617                 req->rate = req->best_parent_rate;
618
619         if (bestdiv > 256)
620                 bestdiv = 256;
621
622         req->rate = req->best_parent_rate / bestdiv;
623
624         return 0;
625 }
626
627 static int iproc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
628                 unsigned long parent_rate)
629 {
630         struct iproc_clk *clk = to_iproc_clk(hw);
631         const struct iproc_clk_ctrl *ctrl = clk->ctrl;
632         struct iproc_pll *pll = clk->pll;
633         u32 val;
634         unsigned int div;
635
636         if (rate == 0 || parent_rate == 0)
637                 return -EINVAL;
638
639         div = DIV_ROUND_CLOSEST(parent_rate, rate);
640         if (ctrl->flags & IPROC_CLK_MCLK_DIV_BY_2)
641                 div /=  2;
642
643         if (div > 256)
644                 return -EINVAL;
645
646         val = readl(pll->control_base + ctrl->mdiv.offset);
647         if (div == 256) {
648                 val &= ~(bit_mask(ctrl->mdiv.width) << ctrl->mdiv.shift);
649         } else {
650                 val &= ~(bit_mask(ctrl->mdiv.width) << ctrl->mdiv.shift);
651                 val |= div << ctrl->mdiv.shift;
652         }
653         iproc_pll_write(pll, pll->control_base, ctrl->mdiv.offset, val);
654         if (ctrl->flags & IPROC_CLK_MCLK_DIV_BY_2)
655                 clk->rate = parent_rate / (div * 2);
656         else
657                 clk->rate = parent_rate / div;
658
659         return 0;
660 }
661
662 static const struct clk_ops iproc_clk_ops = {
663         .enable = iproc_clk_enable,
664         .disable = iproc_clk_disable,
665         .recalc_rate = iproc_clk_recalc_rate,
666         .determine_rate = iproc_clk_determine_rate,
667         .set_rate = iproc_clk_set_rate,
668 };
669
670 /**
671  * Some PLLs require the PLL SW override bit to be set before changes can be
672  * applied to the PLL
673  */
674 static void iproc_pll_sw_cfg(struct iproc_pll *pll)
675 {
676         const struct iproc_pll_ctrl *ctrl = pll->ctrl;
677
678         if (ctrl->flags & IPROC_CLK_PLL_NEEDS_SW_CFG) {
679                 u32 val;
680
681                 val = readl(pll->control_base + ctrl->sw_ctrl.offset);
682                 val |= BIT(ctrl->sw_ctrl.shift);
683                 iproc_pll_write(pll, pll->control_base, ctrl->sw_ctrl.offset,
684                                 val);
685         }
686 }
687
688 void iproc_pll_clk_setup(struct device_node *node,
689                          const struct iproc_pll_ctrl *pll_ctrl,
690                          const struct iproc_pll_vco_param *vco,
691                          unsigned int num_vco_entries,
692                          const struct iproc_clk_ctrl *clk_ctrl,
693                          unsigned int num_clks)
694 {
695         int i, ret;
696         struct iproc_pll *pll;
697         struct iproc_clk *iclk;
698         struct clk_init_data init;
699         const char *parent_name;
700
701         if (WARN_ON(!pll_ctrl) || WARN_ON(!clk_ctrl))
702                 return;
703
704         pll = kzalloc(sizeof(*pll), GFP_KERNEL);
705         if (WARN_ON(!pll))
706                 return;
707
708         pll->clk_data = kzalloc(sizeof(*pll->clk_data->hws) * num_clks +
709                                 sizeof(*pll->clk_data), GFP_KERNEL);
710         if (WARN_ON(!pll->clk_data))
711                 goto err_clk_data;
712         pll->clk_data->num = num_clks;
713
714         pll->clks = kcalloc(num_clks, sizeof(*pll->clks), GFP_KERNEL);
715         if (WARN_ON(!pll->clks))
716                 goto err_clks;
717
718         pll->control_base = of_iomap(node, 0);
719         if (WARN_ON(!pll->control_base))
720                 goto err_pll_iomap;
721
722         /* Some SoCs do not require the pwr_base, thus failing is not fatal */
723         pll->pwr_base = of_iomap(node, 1);
724
725         /* some PLLs require gating control at the top ASIU level */
726         if (pll_ctrl->flags & IPROC_CLK_PLL_ASIU) {
727                 pll->asiu_base = of_iomap(node, 2);
728                 if (WARN_ON(!pll->asiu_base))
729                         goto err_asiu_iomap;
730         }
731
732         if (pll_ctrl->flags & IPROC_CLK_PLL_SPLIT_STAT_CTRL) {
733                 /* Some SoCs have a split status/control.  If this does not
734                  * exist, assume they are unified.
735                  */
736                 pll->status_base = of_iomap(node, 2);
737                 if (!pll->status_base)
738                         goto err_status_iomap;
739         } else
740                 pll->status_base = pll->control_base;
741
742         /* initialize and register the PLL itself */
743         pll->ctrl = pll_ctrl;
744
745         iclk = &pll->clks[0];
746         iclk->pll = pll;
747         iclk->name = node->name;
748
749         init.name = node->name;
750         init.ops = &iproc_pll_ops;
751         init.flags = 0;
752         parent_name = of_clk_get_parent_name(node, 0);
753         init.parent_names = (parent_name ? &parent_name : NULL);
754         init.num_parents = (parent_name ? 1 : 0);
755         iclk->hw.init = &init;
756
757         if (vco) {
758                 pll->num_vco_entries = num_vco_entries;
759                 pll->vco_param = vco;
760         }
761
762         iproc_pll_sw_cfg(pll);
763
764         ret = clk_hw_register(NULL, &iclk->hw);
765         if (WARN_ON(ret))
766                 goto err_pll_register;
767
768         pll->clk_data->hws[0] = &iclk->hw;
769
770         /* now initialize and register all leaf clocks */
771         for (i = 1; i < num_clks; i++) {
772                 const char *clk_name;
773
774                 memset(&init, 0, sizeof(init));
775                 parent_name = node->name;
776
777                 ret = of_property_read_string_index(node, "clock-output-names",
778                                                     i, &clk_name);
779                 if (WARN_ON(ret))
780                         goto err_clk_register;
781
782                 iclk = &pll->clks[i];
783                 iclk->name = clk_name;
784                 iclk->pll = pll;
785                 iclk->ctrl = &clk_ctrl[i];
786
787                 init.name = clk_name;
788                 init.ops = &iproc_clk_ops;
789                 init.flags = 0;
790                 init.parent_names = (parent_name ? &parent_name : NULL);
791                 init.num_parents = (parent_name ? 1 : 0);
792                 iclk->hw.init = &init;
793
794                 ret = clk_hw_register(NULL, &iclk->hw);
795                 if (WARN_ON(ret))
796                         goto err_clk_register;
797
798                 pll->clk_data->hws[i] = &iclk->hw;
799         }
800
801         ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
802                                      pll->clk_data);
803         if (WARN_ON(ret))
804                 goto err_clk_register;
805
806         return;
807
808 err_clk_register:
809         while (--i >= 0)
810                 clk_hw_unregister(pll->clk_data->hws[i]);
811
812 err_pll_register:
813         if (pll->status_base != pll->control_base)
814                 iounmap(pll->status_base);
815
816 err_status_iomap:
817         if (pll->asiu_base)
818                 iounmap(pll->asiu_base);
819
820 err_asiu_iomap:
821         if (pll->pwr_base)
822                 iounmap(pll->pwr_base);
823
824         iounmap(pll->control_base);
825
826 err_pll_iomap:
827         kfree(pll->clks);
828
829 err_clks:
830         kfree(pll->clk_data);
831
832 err_clk_data:
833         kfree(pll);
834 }