GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / clk / bcm / clk-bcm2835.c
1 /*
2  * Copyright (C) 2010,2015 Broadcom
3  * Copyright (C) 2012 Stephen Warren
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
17 /**
18  * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
19  *
20  * The clock tree on the 2835 has several levels.  There's a root
21  * oscillator running at 19.2Mhz.  After the oscillator there are 5
22  * PLLs, roughly divided as "camera", "ARM", "core", "DSI displays",
23  * and "HDMI displays".  Those 5 PLLs each can divide their output to
24  * produce up to 4 channels.  Finally, there is the level of clocks to
25  * be consumed by other hardware components (like "H264" or "HDMI
26  * state machine"), which divide off of some subset of the PLL
27  * channels.
28  *
29  * All of the clocks in the tree are exposed in the DT, because the DT
30  * may want to make assignments of the final layer of clocks to the
31  * PLL channels, and some components of the hardware will actually
32  * skip layers of the tree (for example, the pixel clock comes
33  * directly from the PLLH PIX channel without using a CM_*CTL clock
34  * generator).
35  */
36
37 #include <linux/clk-provider.h>
38 #include <linux/clkdev.h>
39 #include <linux/clk.h>
40 #include <linux/debugfs.h>
41 #include <linux/delay.h>
42 #include <linux/module.h>
43 #include <linux/of.h>
44 #include <linux/platform_device.h>
45 #include <linux/slab.h>
46 #include <dt-bindings/clock/bcm2835.h>
47
48 #define CM_PASSWORD             0x5a000000
49
50 #define CM_GNRICCTL             0x000
51 #define CM_GNRICDIV             0x004
52 # define CM_DIV_FRAC_BITS       12
53 # define CM_DIV_FRAC_MASK       GENMASK(CM_DIV_FRAC_BITS - 1, 0)
54
55 #define CM_VPUCTL               0x008
56 #define CM_VPUDIV               0x00c
57 #define CM_SYSCTL               0x010
58 #define CM_SYSDIV               0x014
59 #define CM_PERIACTL             0x018
60 #define CM_PERIADIV             0x01c
61 #define CM_PERIICTL             0x020
62 #define CM_PERIIDIV             0x024
63 #define CM_H264CTL              0x028
64 #define CM_H264DIV              0x02c
65 #define CM_ISPCTL               0x030
66 #define CM_ISPDIV               0x034
67 #define CM_V3DCTL               0x038
68 #define CM_V3DDIV               0x03c
69 #define CM_CAM0CTL              0x040
70 #define CM_CAM0DIV              0x044
71 #define CM_CAM1CTL              0x048
72 #define CM_CAM1DIV              0x04c
73 #define CM_CCP2CTL              0x050
74 #define CM_CCP2DIV              0x054
75 #define CM_DSI0ECTL             0x058
76 #define CM_DSI0EDIV             0x05c
77 #define CM_DSI0PCTL             0x060
78 #define CM_DSI0PDIV             0x064
79 #define CM_DPICTL               0x068
80 #define CM_DPIDIV               0x06c
81 #define CM_GP0CTL               0x070
82 #define CM_GP0DIV               0x074
83 #define CM_GP1CTL               0x078
84 #define CM_GP1DIV               0x07c
85 #define CM_GP2CTL               0x080
86 #define CM_GP2DIV               0x084
87 #define CM_HSMCTL               0x088
88 #define CM_HSMDIV               0x08c
89 #define CM_OTPCTL               0x090
90 #define CM_OTPDIV               0x094
91 #define CM_PCMCTL               0x098
92 #define CM_PCMDIV               0x09c
93 #define CM_PWMCTL               0x0a0
94 #define CM_PWMDIV               0x0a4
95 #define CM_SLIMCTL              0x0a8
96 #define CM_SLIMDIV              0x0ac
97 #define CM_SMICTL               0x0b0
98 #define CM_SMIDIV               0x0b4
99 /* no definition for 0x0b8  and 0x0bc */
100 #define CM_TCNTCTL              0x0c0
101 # define CM_TCNT_SRC1_SHIFT             12
102 #define CM_TCNTCNT              0x0c4
103 #define CM_TECCTL               0x0c8
104 #define CM_TECDIV               0x0cc
105 #define CM_TD0CTL               0x0d0
106 #define CM_TD0DIV               0x0d4
107 #define CM_TD1CTL               0x0d8
108 #define CM_TD1DIV               0x0dc
109 #define CM_TSENSCTL             0x0e0
110 #define CM_TSENSDIV             0x0e4
111 #define CM_TIMERCTL             0x0e8
112 #define CM_TIMERDIV             0x0ec
113 #define CM_UARTCTL              0x0f0
114 #define CM_UARTDIV              0x0f4
115 #define CM_VECCTL               0x0f8
116 #define CM_VECDIV               0x0fc
117 #define CM_PULSECTL             0x190
118 #define CM_PULSEDIV             0x194
119 #define CM_SDCCTL               0x1a8
120 #define CM_SDCDIV               0x1ac
121 #define CM_ARMCTL               0x1b0
122 #define CM_AVEOCTL              0x1b8
123 #define CM_AVEODIV              0x1bc
124 #define CM_EMMCCTL              0x1c0
125 #define CM_EMMCDIV              0x1c4
126
127 /* General bits for the CM_*CTL regs */
128 # define CM_ENABLE                      BIT(4)
129 # define CM_KILL                        BIT(5)
130 # define CM_GATE_BIT                    6
131 # define CM_GATE                        BIT(CM_GATE_BIT)
132 # define CM_BUSY                        BIT(7)
133 # define CM_BUSYD                       BIT(8)
134 # define CM_FRAC                        BIT(9)
135 # define CM_SRC_SHIFT                   0
136 # define CM_SRC_BITS                    4
137 # define CM_SRC_MASK                    0xf
138 # define CM_SRC_GND                     0
139 # define CM_SRC_OSC                     1
140 # define CM_SRC_TESTDEBUG0              2
141 # define CM_SRC_TESTDEBUG1              3
142 # define CM_SRC_PLLA_CORE               4
143 # define CM_SRC_PLLA_PER                4
144 # define CM_SRC_PLLC_CORE0              5
145 # define CM_SRC_PLLC_PER                5
146 # define CM_SRC_PLLC_CORE1              8
147 # define CM_SRC_PLLD_CORE               6
148 # define CM_SRC_PLLD_PER                6
149 # define CM_SRC_PLLH_AUX                7
150 # define CM_SRC_PLLC_CORE1              8
151 # define CM_SRC_PLLC_CORE2              9
152
153 #define CM_OSCCOUNT             0x100
154
155 #define CM_PLLA                 0x104
156 # define CM_PLL_ANARST                  BIT(8)
157 # define CM_PLLA_HOLDPER                BIT(7)
158 # define CM_PLLA_LOADPER                BIT(6)
159 # define CM_PLLA_HOLDCORE               BIT(5)
160 # define CM_PLLA_LOADCORE               BIT(4)
161 # define CM_PLLA_HOLDCCP2               BIT(3)
162 # define CM_PLLA_LOADCCP2               BIT(2)
163 # define CM_PLLA_HOLDDSI0               BIT(1)
164 # define CM_PLLA_LOADDSI0               BIT(0)
165
166 #define CM_PLLC                 0x108
167 # define CM_PLLC_HOLDPER                BIT(7)
168 # define CM_PLLC_LOADPER                BIT(6)
169 # define CM_PLLC_HOLDCORE2              BIT(5)
170 # define CM_PLLC_LOADCORE2              BIT(4)
171 # define CM_PLLC_HOLDCORE1              BIT(3)
172 # define CM_PLLC_LOADCORE1              BIT(2)
173 # define CM_PLLC_HOLDCORE0              BIT(1)
174 # define CM_PLLC_LOADCORE0              BIT(0)
175
176 #define CM_PLLD                 0x10c
177 # define CM_PLLD_HOLDPER                BIT(7)
178 # define CM_PLLD_LOADPER                BIT(6)
179 # define CM_PLLD_HOLDCORE               BIT(5)
180 # define CM_PLLD_LOADCORE               BIT(4)
181 # define CM_PLLD_HOLDDSI1               BIT(3)
182 # define CM_PLLD_LOADDSI1               BIT(2)
183 # define CM_PLLD_HOLDDSI0               BIT(1)
184 # define CM_PLLD_LOADDSI0               BIT(0)
185
186 #define CM_PLLH                 0x110
187 # define CM_PLLH_LOADRCAL               BIT(2)
188 # define CM_PLLH_LOADAUX                BIT(1)
189 # define CM_PLLH_LOADPIX                BIT(0)
190
191 #define CM_LOCK                 0x114
192 # define CM_LOCK_FLOCKH                 BIT(12)
193 # define CM_LOCK_FLOCKD                 BIT(11)
194 # define CM_LOCK_FLOCKC                 BIT(10)
195 # define CM_LOCK_FLOCKB                 BIT(9)
196 # define CM_LOCK_FLOCKA                 BIT(8)
197
198 #define CM_EVENT                0x118
199 #define CM_DSI1ECTL             0x158
200 #define CM_DSI1EDIV             0x15c
201 #define CM_DSI1PCTL             0x160
202 #define CM_DSI1PDIV             0x164
203 #define CM_DFTCTL               0x168
204 #define CM_DFTDIV               0x16c
205
206 #define CM_PLLB                 0x170
207 # define CM_PLLB_HOLDARM                BIT(1)
208 # define CM_PLLB_LOADARM                BIT(0)
209
210 #define A2W_PLLA_CTRL           0x1100
211 #define A2W_PLLC_CTRL           0x1120
212 #define A2W_PLLD_CTRL           0x1140
213 #define A2W_PLLH_CTRL           0x1160
214 #define A2W_PLLB_CTRL           0x11e0
215 # define A2W_PLL_CTRL_PRST_DISABLE      BIT(17)
216 # define A2W_PLL_CTRL_PWRDN             BIT(16)
217 # define A2W_PLL_CTRL_PDIV_MASK         0x000007000
218 # define A2W_PLL_CTRL_PDIV_SHIFT        12
219 # define A2W_PLL_CTRL_NDIV_MASK         0x0000003ff
220 # define A2W_PLL_CTRL_NDIV_SHIFT        0
221
222 #define A2W_PLLA_ANA0           0x1010
223 #define A2W_PLLC_ANA0           0x1030
224 #define A2W_PLLD_ANA0           0x1050
225 #define A2W_PLLH_ANA0           0x1070
226 #define A2W_PLLB_ANA0           0x10f0
227
228 #define A2W_PLL_KA_SHIFT        7
229 #define A2W_PLL_KA_MASK         GENMASK(9, 7)
230 #define A2W_PLL_KI_SHIFT        19
231 #define A2W_PLL_KI_MASK         GENMASK(21, 19)
232 #define A2W_PLL_KP_SHIFT        15
233 #define A2W_PLL_KP_MASK         GENMASK(18, 15)
234
235 #define A2W_PLLH_KA_SHIFT       19
236 #define A2W_PLLH_KA_MASK        GENMASK(21, 19)
237 #define A2W_PLLH_KI_LOW_SHIFT   22
238 #define A2W_PLLH_KI_LOW_MASK    GENMASK(23, 22)
239 #define A2W_PLLH_KI_HIGH_SHIFT  0
240 #define A2W_PLLH_KI_HIGH_MASK   GENMASK(0, 0)
241 #define A2W_PLLH_KP_SHIFT       1
242 #define A2W_PLLH_KP_MASK        GENMASK(4, 1)
243
244 #define A2W_XOSC_CTRL           0x1190
245 # define A2W_XOSC_CTRL_PLLB_ENABLE      BIT(7)
246 # define A2W_XOSC_CTRL_PLLA_ENABLE      BIT(6)
247 # define A2W_XOSC_CTRL_PLLD_ENABLE      BIT(5)
248 # define A2W_XOSC_CTRL_DDR_ENABLE       BIT(4)
249 # define A2W_XOSC_CTRL_CPR1_ENABLE      BIT(3)
250 # define A2W_XOSC_CTRL_USB_ENABLE       BIT(2)
251 # define A2W_XOSC_CTRL_HDMI_ENABLE      BIT(1)
252 # define A2W_XOSC_CTRL_PLLC_ENABLE      BIT(0)
253
254 #define A2W_PLLA_FRAC           0x1200
255 #define A2W_PLLC_FRAC           0x1220
256 #define A2W_PLLD_FRAC           0x1240
257 #define A2W_PLLH_FRAC           0x1260
258 #define A2W_PLLB_FRAC           0x12e0
259 # define A2W_PLL_FRAC_MASK              ((1 << A2W_PLL_FRAC_BITS) - 1)
260 # define A2W_PLL_FRAC_BITS              20
261
262 #define A2W_PLL_CHANNEL_DISABLE         BIT(8)
263 #define A2W_PLL_DIV_BITS                8
264 #define A2W_PLL_DIV_SHIFT               0
265
266 #define A2W_PLLA_DSI0           0x1300
267 #define A2W_PLLA_CORE           0x1400
268 #define A2W_PLLA_PER            0x1500
269 #define A2W_PLLA_CCP2           0x1600
270
271 #define A2W_PLLC_CORE2          0x1320
272 #define A2W_PLLC_CORE1          0x1420
273 #define A2W_PLLC_PER            0x1520
274 #define A2W_PLLC_CORE0          0x1620
275
276 #define A2W_PLLD_DSI0           0x1340
277 #define A2W_PLLD_CORE           0x1440
278 #define A2W_PLLD_PER            0x1540
279 #define A2W_PLLD_DSI1           0x1640
280
281 #define A2W_PLLH_AUX            0x1360
282 #define A2W_PLLH_RCAL           0x1460
283 #define A2W_PLLH_PIX            0x1560
284 #define A2W_PLLH_STS            0x1660
285
286 #define A2W_PLLH_CTRLR          0x1960
287 #define A2W_PLLH_FRACR          0x1a60
288 #define A2W_PLLH_AUXR           0x1b60
289 #define A2W_PLLH_RCALR          0x1c60
290 #define A2W_PLLH_PIXR           0x1d60
291 #define A2W_PLLH_STSR           0x1e60
292
293 #define A2W_PLLB_ARM            0x13e0
294 #define A2W_PLLB_SP0            0x14e0
295 #define A2W_PLLB_SP1            0x15e0
296 #define A2W_PLLB_SP2            0x16e0
297
298 #define LOCK_TIMEOUT_NS         100000000
299 #define BCM2835_MAX_FB_RATE     1750000000u
300
301 /*
302  * Names of clocks used within the driver that need to be replaced
303  * with an external parent's name.  This array is in the order that
304  * the clocks node in the DT references external clocks.
305  */
306 static const char *const cprman_parent_names[] = {
307         "xosc",
308         "dsi0_byte",
309         "dsi0_ddr2",
310         "dsi0_ddr",
311         "dsi1_byte",
312         "dsi1_ddr2",
313         "dsi1_ddr",
314 };
315
316 struct bcm2835_cprman {
317         struct device *dev;
318         void __iomem *regs;
319         spinlock_t regs_lock; /* spinlock for all clocks */
320
321         /*
322          * Real names of cprman clock parents looked up through
323          * of_clk_get_parent_name(), which will be used in the
324          * parent_names[] arrays for clock registration.
325          */
326         const char *real_parent_names[ARRAY_SIZE(cprman_parent_names)];
327
328         /* Must be last */
329         struct clk_hw_onecell_data onecell;
330 };
331
332 static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
333 {
334         writel(CM_PASSWORD | val, cprman->regs + reg);
335 }
336
337 static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
338 {
339         return readl(cprman->regs + reg);
340 }
341
342 /* Does a cycle of measuring a clock through the TCNT clock, which may
343  * source from many other clocks in the system.
344  */
345 static unsigned long bcm2835_measure_tcnt_mux(struct bcm2835_cprman *cprman,
346                                               u32 tcnt_mux)
347 {
348         u32 osccount = 19200; /* 1ms */
349         u32 count;
350         ktime_t timeout;
351
352         spin_lock(&cprman->regs_lock);
353
354         cprman_write(cprman, CM_TCNTCTL, CM_KILL);
355
356         cprman_write(cprman, CM_TCNTCTL,
357                      (tcnt_mux & CM_SRC_MASK) |
358                      (tcnt_mux >> CM_SRC_BITS) << CM_TCNT_SRC1_SHIFT);
359
360         cprman_write(cprman, CM_OSCCOUNT, osccount);
361
362         /* do a kind delay at the start */
363         mdelay(1);
364
365         /* Finish off whatever is left of OSCCOUNT */
366         timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
367         while (cprman_read(cprman, CM_OSCCOUNT)) {
368                 if (ktime_after(ktime_get(), timeout)) {
369                         dev_err(cprman->dev, "timeout waiting for OSCCOUNT\n");
370                         count = 0;
371                         goto out;
372                 }
373                 cpu_relax();
374         }
375
376         /* Wait for BUSY to clear. */
377         timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
378         while (cprman_read(cprman, CM_TCNTCTL) & CM_BUSY) {
379                 if (ktime_after(ktime_get(), timeout)) {
380                         dev_err(cprman->dev, "timeout waiting for !BUSY\n");
381                         count = 0;
382                         goto out;
383                 }
384                 cpu_relax();
385         }
386
387         count = cprman_read(cprman, CM_TCNTCNT);
388
389         cprman_write(cprman, CM_TCNTCTL, 0);
390
391 out:
392         spin_unlock(&cprman->regs_lock);
393
394         return count * 1000;
395 }
396
397 static void bcm2835_debugfs_regset(struct bcm2835_cprman *cprman, u32 base,
398                                   struct debugfs_reg32 *regs, size_t nregs,
399                                   struct dentry *dentry)
400 {
401         struct debugfs_regset32 *regset;
402
403         regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL);
404         if (!regset)
405                 return;
406
407         regset->regs = regs;
408         regset->nregs = nregs;
409         regset->base = cprman->regs + base;
410
411         debugfs_create_regset32("regdump", S_IRUGO, dentry, regset);
412 }
413
414 struct bcm2835_pll_data {
415         const char *name;
416         u32 cm_ctrl_reg;
417         u32 a2w_ctrl_reg;
418         u32 frac_reg;
419         u32 ana_reg_base;
420         u32 reference_enable_mask;
421         /* Bit in CM_LOCK to indicate when the PLL has locked. */
422         u32 lock_mask;
423
424         const struct bcm2835_pll_ana_bits *ana;
425
426         unsigned long min_rate;
427         unsigned long max_rate;
428         /*
429          * Highest rate for the VCO before we have to use the
430          * pre-divide-by-2.
431          */
432         unsigned long max_fb_rate;
433 };
434
435 struct bcm2835_pll_ana_bits {
436         u32 mask0;
437         u32 set0;
438         u32 mask1;
439         u32 set1;
440         u32 mask3;
441         u32 set3;
442         u32 fb_prediv_mask;
443 };
444
445 static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
446         .mask0 = 0,
447         .set0 = 0,
448         .mask1 = A2W_PLL_KI_MASK | A2W_PLL_KP_MASK,
449         .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
450         .mask3 = A2W_PLL_KA_MASK,
451         .set3 = (2 << A2W_PLL_KA_SHIFT),
452         .fb_prediv_mask = BIT(14),
453 };
454
455 static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
456         .mask0 = A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK,
457         .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
458         .mask1 = A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK,
459         .set1 = (6 << A2W_PLLH_KP_SHIFT),
460         .mask3 = 0,
461         .set3 = 0,
462         .fb_prediv_mask = BIT(11),
463 };
464
465 struct bcm2835_pll_divider_data {
466         const char *name;
467         const char *source_pll;
468
469         u32 cm_reg;
470         u32 a2w_reg;
471
472         u32 load_mask;
473         u32 hold_mask;
474         u32 fixed_divider;
475         u32 flags;
476 };
477
478 struct bcm2835_clock_data {
479         const char *name;
480
481         const char *const *parents;
482         int num_mux_parents;
483
484         /* Bitmap encoding which parents accept rate change propagation. */
485         unsigned int set_rate_parent;
486
487         u32 ctl_reg;
488         u32 div_reg;
489
490         /* Number of integer bits in the divider */
491         u32 int_bits;
492         /* Number of fractional bits in the divider */
493         u32 frac_bits;
494
495         u32 flags;
496
497         bool is_vpu_clock;
498         bool is_mash_clock;
499         bool low_jitter;
500
501         u32 tcnt_mux;
502 };
503
504 struct bcm2835_gate_data {
505         const char *name;
506         const char *parent;
507
508         u32 ctl_reg;
509 };
510
511 struct bcm2835_pll {
512         struct clk_hw hw;
513         struct bcm2835_cprman *cprman;
514         const struct bcm2835_pll_data *data;
515 };
516
517 static int bcm2835_pll_is_on(struct clk_hw *hw)
518 {
519         struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
520         struct bcm2835_cprman *cprman = pll->cprman;
521         const struct bcm2835_pll_data *data = pll->data;
522
523         return cprman_read(cprman, data->a2w_ctrl_reg) &
524                 A2W_PLL_CTRL_PRST_DISABLE;
525 }
526
527 static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
528                                              unsigned long parent_rate,
529                                              u32 *ndiv, u32 *fdiv)
530 {
531         u64 div;
532
533         div = (u64)rate << A2W_PLL_FRAC_BITS;
534         do_div(div, parent_rate);
535
536         *ndiv = div >> A2W_PLL_FRAC_BITS;
537         *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
538 }
539
540 static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
541                                            u32 ndiv, u32 fdiv, u32 pdiv)
542 {
543         u64 rate;
544
545         if (pdiv == 0)
546                 return 0;
547
548         rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
549         do_div(rate, pdiv);
550         return rate >> A2W_PLL_FRAC_BITS;
551 }
552
553 static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
554                                    unsigned long *parent_rate)
555 {
556         struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
557         const struct bcm2835_pll_data *data = pll->data;
558         u32 ndiv, fdiv;
559
560         rate = clamp(rate, data->min_rate, data->max_rate);
561
562         bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
563
564         return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
565 }
566
567 static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
568                                           unsigned long parent_rate)
569 {
570         struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
571         struct bcm2835_cprman *cprman = pll->cprman;
572         const struct bcm2835_pll_data *data = pll->data;
573         u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
574         u32 ndiv, pdiv, fdiv;
575         bool using_prediv;
576
577         if (parent_rate == 0)
578                 return 0;
579
580         fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
581         ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
582         pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
583         using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
584                 data->ana->fb_prediv_mask;
585
586         if (using_prediv) {
587                 ndiv *= 2;
588                 fdiv *= 2;
589         }
590
591         return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
592 }
593
594 static void bcm2835_pll_off(struct clk_hw *hw)
595 {
596         struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
597         struct bcm2835_cprman *cprman = pll->cprman;
598         const struct bcm2835_pll_data *data = pll->data;
599
600         spin_lock(&cprman->regs_lock);
601         cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);
602         cprman_write(cprman, data->a2w_ctrl_reg,
603                      cprman_read(cprman, data->a2w_ctrl_reg) |
604                      A2W_PLL_CTRL_PWRDN);
605         spin_unlock(&cprman->regs_lock);
606 }
607
608 static int bcm2835_pll_on(struct clk_hw *hw)
609 {
610         struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
611         struct bcm2835_cprman *cprman = pll->cprman;
612         const struct bcm2835_pll_data *data = pll->data;
613         ktime_t timeout;
614
615         cprman_write(cprman, data->a2w_ctrl_reg,
616                      cprman_read(cprman, data->a2w_ctrl_reg) &
617                      ~A2W_PLL_CTRL_PWRDN);
618
619         /* Take the PLL out of reset. */
620         spin_lock(&cprman->regs_lock);
621         cprman_write(cprman, data->cm_ctrl_reg,
622                      cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
623         spin_unlock(&cprman->regs_lock);
624
625         /* Wait for the PLL to lock. */
626         timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
627         while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
628                 if (ktime_after(ktime_get(), timeout)) {
629                         dev_err(cprman->dev, "%s: couldn't lock PLL\n",
630                                 clk_hw_get_name(hw));
631                         return -ETIMEDOUT;
632                 }
633
634                 cpu_relax();
635         }
636
637         cprman_write(cprman, data->a2w_ctrl_reg,
638                      cprman_read(cprman, data->a2w_ctrl_reg) |
639                      A2W_PLL_CTRL_PRST_DISABLE);
640
641         return 0;
642 }
643
644 static void
645 bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
646 {
647         int i;
648
649         /*
650          * ANA register setup is done as a series of writes to
651          * ANA3-ANA0, in that order.  This lets us write all 4
652          * registers as a single cycle of the serdes interface (taking
653          * 100 xosc clocks), whereas if we were to update ana0, 1, and
654          * 3 individually through their partial-write registers, each
655          * would be their own serdes cycle.
656          */
657         for (i = 3; i >= 0; i--)
658                 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
659 }
660
661 static int bcm2835_pll_set_rate(struct clk_hw *hw,
662                                 unsigned long rate, unsigned long parent_rate)
663 {
664         struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
665         struct bcm2835_cprman *cprman = pll->cprman;
666         const struct bcm2835_pll_data *data = pll->data;
667         bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
668         u32 ndiv, fdiv, a2w_ctl;
669         u32 ana[4];
670         int i;
671
672         if (rate > data->max_fb_rate) {
673                 use_fb_prediv = true;
674                 rate /= 2;
675         } else {
676                 use_fb_prediv = false;
677         }
678
679         bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
680
681         for (i = 3; i >= 0; i--)
682                 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
683
684         was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
685
686         ana[0] &= ~data->ana->mask0;
687         ana[0] |= data->ana->set0;
688         ana[1] &= ~data->ana->mask1;
689         ana[1] |= data->ana->set1;
690         ana[3] &= ~data->ana->mask3;
691         ana[3] |= data->ana->set3;
692
693         if (was_using_prediv && !use_fb_prediv) {
694                 ana[1] &= ~data->ana->fb_prediv_mask;
695                 do_ana_setup_first = true;
696         } else if (!was_using_prediv && use_fb_prediv) {
697                 ana[1] |= data->ana->fb_prediv_mask;
698                 do_ana_setup_first = false;
699         } else {
700                 do_ana_setup_first = true;
701         }
702
703         /* Unmask the reference clock from the oscillator. */
704         spin_lock(&cprman->regs_lock);
705         cprman_write(cprman, A2W_XOSC_CTRL,
706                      cprman_read(cprman, A2W_XOSC_CTRL) |
707                      data->reference_enable_mask);
708         spin_unlock(&cprman->regs_lock);
709
710         if (do_ana_setup_first)
711                 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
712
713         /* Set the PLL multiplier from the oscillator. */
714         cprman_write(cprman, data->frac_reg, fdiv);
715
716         a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
717         a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
718         a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
719         a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
720         a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
721         cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
722
723         if (!do_ana_setup_first)
724                 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
725
726         return 0;
727 }
728
729 static void bcm2835_pll_debug_init(struct clk_hw *hw,
730                                   struct dentry *dentry)
731 {
732         struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
733         struct bcm2835_cprman *cprman = pll->cprman;
734         const struct bcm2835_pll_data *data = pll->data;
735         struct debugfs_reg32 *regs;
736
737         regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
738         if (!regs)
739                 return;
740
741         regs[0].name = "cm_ctrl";
742         regs[0].offset = data->cm_ctrl_reg;
743         regs[1].name = "a2w_ctrl";
744         regs[1].offset = data->a2w_ctrl_reg;
745         regs[2].name = "frac";
746         regs[2].offset = data->frac_reg;
747         regs[3].name = "ana0";
748         regs[3].offset = data->ana_reg_base + 0 * 4;
749         regs[4].name = "ana1";
750         regs[4].offset = data->ana_reg_base + 1 * 4;
751         regs[5].name = "ana2";
752         regs[5].offset = data->ana_reg_base + 2 * 4;
753         regs[6].name = "ana3";
754         regs[6].offset = data->ana_reg_base + 3 * 4;
755
756         bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry);
757 }
758
759 static const struct clk_ops bcm2835_pll_clk_ops = {
760         .is_prepared = bcm2835_pll_is_on,
761         .prepare = bcm2835_pll_on,
762         .unprepare = bcm2835_pll_off,
763         .recalc_rate = bcm2835_pll_get_rate,
764         .set_rate = bcm2835_pll_set_rate,
765         .round_rate = bcm2835_pll_round_rate,
766         .debug_init = bcm2835_pll_debug_init,
767 };
768
769 struct bcm2835_pll_divider {
770         struct clk_divider div;
771         struct bcm2835_cprman *cprman;
772         const struct bcm2835_pll_divider_data *data;
773 };
774
775 static struct bcm2835_pll_divider *
776 bcm2835_pll_divider_from_hw(struct clk_hw *hw)
777 {
778         return container_of(hw, struct bcm2835_pll_divider, div.hw);
779 }
780
781 static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
782 {
783         struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
784         struct bcm2835_cprman *cprman = divider->cprman;
785         const struct bcm2835_pll_divider_data *data = divider->data;
786
787         return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
788 }
789
790 static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
791                                            unsigned long rate,
792                                            unsigned long *parent_rate)
793 {
794         return clk_divider_ops.round_rate(hw, rate, parent_rate);
795 }
796
797 static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
798                                                   unsigned long parent_rate)
799 {
800         return clk_divider_ops.recalc_rate(hw, parent_rate);
801 }
802
803 static void bcm2835_pll_divider_off(struct clk_hw *hw)
804 {
805         struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
806         struct bcm2835_cprman *cprman = divider->cprman;
807         const struct bcm2835_pll_divider_data *data = divider->data;
808
809         spin_lock(&cprman->regs_lock);
810         cprman_write(cprman, data->cm_reg,
811                      (cprman_read(cprman, data->cm_reg) &
812                       ~data->load_mask) | data->hold_mask);
813         cprman_write(cprman, data->a2w_reg,
814                      cprman_read(cprman, data->a2w_reg) |
815                      A2W_PLL_CHANNEL_DISABLE);
816         spin_unlock(&cprman->regs_lock);
817 }
818
819 static int bcm2835_pll_divider_on(struct clk_hw *hw)
820 {
821         struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
822         struct bcm2835_cprman *cprman = divider->cprman;
823         const struct bcm2835_pll_divider_data *data = divider->data;
824
825         spin_lock(&cprman->regs_lock);
826         cprman_write(cprman, data->a2w_reg,
827                      cprman_read(cprman, data->a2w_reg) &
828                      ~A2W_PLL_CHANNEL_DISABLE);
829
830         cprman_write(cprman, data->cm_reg,
831                      cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
832         spin_unlock(&cprman->regs_lock);
833
834         return 0;
835 }
836
837 static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
838                                         unsigned long rate,
839                                         unsigned long parent_rate)
840 {
841         struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
842         struct bcm2835_cprman *cprman = divider->cprman;
843         const struct bcm2835_pll_divider_data *data = divider->data;
844         u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
845
846         div = DIV_ROUND_UP_ULL(parent_rate, rate);
847
848         div = min(div, max_div);
849         if (div == max_div)
850                 div = 0;
851
852         cprman_write(cprman, data->a2w_reg, div);
853         cm = cprman_read(cprman, data->cm_reg);
854         cprman_write(cprman, data->cm_reg, cm | data->load_mask);
855         cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
856
857         return 0;
858 }
859
860 static void bcm2835_pll_divider_debug_init(struct clk_hw *hw,
861                                            struct dentry *dentry)
862 {
863         struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
864         struct bcm2835_cprman *cprman = divider->cprman;
865         const struct bcm2835_pll_divider_data *data = divider->data;
866         struct debugfs_reg32 *regs;
867
868         regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
869         if (!regs)
870                 return;
871
872         regs[0].name = "cm";
873         regs[0].offset = data->cm_reg;
874         regs[1].name = "a2w";
875         regs[1].offset = data->a2w_reg;
876
877         bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry);
878 }
879
880 static const struct clk_ops bcm2835_pll_divider_clk_ops = {
881         .is_prepared = bcm2835_pll_divider_is_on,
882         .prepare = bcm2835_pll_divider_on,
883         .unprepare = bcm2835_pll_divider_off,
884         .recalc_rate = bcm2835_pll_divider_get_rate,
885         .set_rate = bcm2835_pll_divider_set_rate,
886         .round_rate = bcm2835_pll_divider_round_rate,
887         .debug_init = bcm2835_pll_divider_debug_init,
888 };
889
890 /*
891  * The CM dividers do fixed-point division, so we can't use the
892  * generic integer divider code like the PLL dividers do (and we can't
893  * fake it by having some fixed shifts preceding it in the clock tree,
894  * because we'd run out of bits in a 32-bit unsigned long).
895  */
896 struct bcm2835_clock {
897         struct clk_hw hw;
898         struct bcm2835_cprman *cprman;
899         const struct bcm2835_clock_data *data;
900 };
901
902 static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
903 {
904         return container_of(hw, struct bcm2835_clock, hw);
905 }
906
907 static int bcm2835_clock_is_on(struct clk_hw *hw)
908 {
909         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
910         struct bcm2835_cprman *cprman = clock->cprman;
911         const struct bcm2835_clock_data *data = clock->data;
912
913         return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
914 }
915
916 static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
917                                     unsigned long rate,
918                                     unsigned long parent_rate)
919 {
920         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
921         const struct bcm2835_clock_data *data = clock->data;
922         u32 unused_frac_mask =
923                 GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
924         u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
925         u64 rem;
926         u32 div, mindiv, maxdiv;
927
928         rem = do_div(temp, rate);
929         div = temp;
930         div &= ~unused_frac_mask;
931
932         /* different clamping limits apply for a mash clock */
933         if (data->is_mash_clock) {
934                 /* clamp to min divider of 2 */
935                 mindiv = 2 << CM_DIV_FRAC_BITS;
936                 /* clamp to the highest possible integer divider */
937                 maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
938         } else {
939                 /* clamp to min divider of 1 */
940                 mindiv = 1 << CM_DIV_FRAC_BITS;
941                 /* clamp to the highest possible fractional divider */
942                 maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
943                                  CM_DIV_FRAC_BITS - data->frac_bits);
944         }
945
946         /* apply the clamping  limits */
947         div = max_t(u32, div, mindiv);
948         div = min_t(u32, div, maxdiv);
949
950         return div;
951 }
952
953 static unsigned long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
954                                                      unsigned long parent_rate,
955                                                      u32 div)
956 {
957         const struct bcm2835_clock_data *data = clock->data;
958         u64 temp;
959
960         if (data->int_bits == 0 && data->frac_bits == 0)
961                 return parent_rate;
962
963         /*
964          * The divisor is a 12.12 fixed point field, but only some of
965          * the bits are populated in any given clock.
966          */
967         div >>= CM_DIV_FRAC_BITS - data->frac_bits;
968         div &= (1 << (data->int_bits + data->frac_bits)) - 1;
969
970         if (div == 0)
971                 return 0;
972
973         temp = (u64)parent_rate << data->frac_bits;
974
975         do_div(temp, div);
976
977         return temp;
978 }
979
980 static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
981                                             unsigned long parent_rate)
982 {
983         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
984         struct bcm2835_cprman *cprman = clock->cprman;
985         const struct bcm2835_clock_data *data = clock->data;
986         u32 div;
987
988         if (data->int_bits == 0 && data->frac_bits == 0)
989                 return parent_rate;
990
991         div = cprman_read(cprman, data->div_reg);
992
993         return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
994 }
995
996 static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
997 {
998         struct bcm2835_cprman *cprman = clock->cprman;
999         const struct bcm2835_clock_data *data = clock->data;
1000         ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
1001
1002         while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
1003                 if (ktime_after(ktime_get(), timeout)) {
1004                         dev_err(cprman->dev, "%s: couldn't lock PLL\n",
1005                                 clk_hw_get_name(&clock->hw));
1006                         return;
1007                 }
1008                 cpu_relax();
1009         }
1010 }
1011
1012 static void bcm2835_clock_off(struct clk_hw *hw)
1013 {
1014         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1015         struct bcm2835_cprman *cprman = clock->cprman;
1016         const struct bcm2835_clock_data *data = clock->data;
1017
1018         spin_lock(&cprman->regs_lock);
1019         cprman_write(cprman, data->ctl_reg,
1020                      cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
1021         spin_unlock(&cprman->regs_lock);
1022
1023         /* BUSY will remain high until the divider completes its cycle. */
1024         bcm2835_clock_wait_busy(clock);
1025 }
1026
1027 static int bcm2835_clock_on(struct clk_hw *hw)
1028 {
1029         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1030         struct bcm2835_cprman *cprman = clock->cprman;
1031         const struct bcm2835_clock_data *data = clock->data;
1032
1033         spin_lock(&cprman->regs_lock);
1034         cprman_write(cprman, data->ctl_reg,
1035                      cprman_read(cprman, data->ctl_reg) |
1036                      CM_ENABLE |
1037                      CM_GATE);
1038         spin_unlock(&cprman->regs_lock);
1039
1040         /* Debug code to measure the clock once it's turned on to see
1041          * if it's ticking at the rate we expect.
1042          */
1043         if (data->tcnt_mux && false) {
1044                 dev_info(cprman->dev,
1045                          "clk %s: rate %ld, measure %ld\n",
1046                          data->name,
1047                          clk_hw_get_rate(hw),
1048                          bcm2835_measure_tcnt_mux(cprman, data->tcnt_mux));
1049         }
1050
1051         return 0;
1052 }
1053
1054 static int bcm2835_clock_set_rate(struct clk_hw *hw,
1055                                   unsigned long rate, unsigned long parent_rate)
1056 {
1057         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1058         struct bcm2835_cprman *cprman = clock->cprman;
1059         const struct bcm2835_clock_data *data = clock->data;
1060         u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate);
1061         u32 ctl;
1062
1063         spin_lock(&cprman->regs_lock);
1064
1065         /*
1066          * Setting up frac support
1067          *
1068          * In principle it is recommended to stop/start the clock first,
1069          * but as we set CLK_SET_RATE_GATE during registration of the
1070          * clock this requirement should be take care of by the
1071          * clk-framework.
1072          */
1073         ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
1074         ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
1075         cprman_write(cprman, data->ctl_reg, ctl);
1076
1077         cprman_write(cprman, data->div_reg, div);
1078
1079         spin_unlock(&cprman->regs_lock);
1080
1081         return 0;
1082 }
1083
1084 static bool
1085 bcm2835_clk_is_pllc(struct clk_hw *hw)
1086 {
1087         if (!hw)
1088                 return false;
1089
1090         return strncmp(clk_hw_get_name(hw), "pllc", 4) == 0;
1091 }
1092
1093 static unsigned long bcm2835_clock_choose_div_and_prate(struct clk_hw *hw,
1094                                                         int parent_idx,
1095                                                         unsigned long rate,
1096                                                         u32 *div,
1097                                                         unsigned long *prate,
1098                                                         unsigned long *avgrate)
1099 {
1100         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1101         struct bcm2835_cprman *cprman = clock->cprman;
1102         const struct bcm2835_clock_data *data = clock->data;
1103         unsigned long best_rate = 0;
1104         u32 curdiv, mindiv, maxdiv;
1105         struct clk_hw *parent;
1106
1107         parent = clk_hw_get_parent_by_index(hw, parent_idx);
1108
1109         if (!(BIT(parent_idx) & data->set_rate_parent)) {
1110                 *prate = clk_hw_get_rate(parent);
1111                 *div = bcm2835_clock_choose_div(hw, rate, *prate);
1112
1113                 *avgrate = bcm2835_clock_rate_from_divisor(clock, *prate, *div);
1114
1115                 if (data->low_jitter && (*div & CM_DIV_FRAC_MASK)) {
1116                         unsigned long high, low;
1117                         u32 int_div = *div & ~CM_DIV_FRAC_MASK;
1118
1119                         high = bcm2835_clock_rate_from_divisor(clock, *prate,
1120                                                                int_div);
1121                         int_div += CM_DIV_FRAC_MASK + 1;
1122                         low = bcm2835_clock_rate_from_divisor(clock, *prate,
1123                                                               int_div);
1124
1125                         /*
1126                          * Return a value which is the maximum deviation
1127                          * below the ideal rate, for use as a metric.
1128                          */
1129                         return *avgrate - max(*avgrate - low, high - *avgrate);
1130                 }
1131                 return *avgrate;
1132         }
1133
1134         if (data->frac_bits)
1135                 dev_warn(cprman->dev,
1136                         "frac bits are not used when propagating rate change");
1137
1138         /* clamp to min divider of 2 if we're dealing with a mash clock */
1139         mindiv = data->is_mash_clock ? 2 : 1;
1140         maxdiv = BIT(data->int_bits) - 1;
1141
1142         /* TODO: Be smart, and only test a subset of the available divisors. */
1143         for (curdiv = mindiv; curdiv <= maxdiv; curdiv++) {
1144                 unsigned long tmp_rate;
1145
1146                 tmp_rate = clk_hw_round_rate(parent, rate * curdiv);
1147                 tmp_rate /= curdiv;
1148                 if (curdiv == mindiv ||
1149                     (tmp_rate > best_rate && tmp_rate <= rate))
1150                         best_rate = tmp_rate;
1151
1152                 if (best_rate == rate)
1153                         break;
1154         }
1155
1156         *div = curdiv << CM_DIV_FRAC_BITS;
1157         *prate = curdiv * best_rate;
1158         *avgrate = best_rate;
1159
1160         return best_rate;
1161 }
1162
1163 static int bcm2835_clock_determine_rate(struct clk_hw *hw,
1164                                         struct clk_rate_request *req)
1165 {
1166         struct clk_hw *parent, *best_parent = NULL;
1167         bool current_parent_is_pllc;
1168         unsigned long rate, best_rate = 0;
1169         unsigned long prate, best_prate = 0;
1170         unsigned long avgrate, best_avgrate = 0;
1171         size_t i;
1172         u32 div;
1173
1174         current_parent_is_pllc = bcm2835_clk_is_pllc(clk_hw_get_parent(hw));
1175
1176         /*
1177          * Select parent clock that results in the closest but lower rate
1178          */
1179         for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
1180                 parent = clk_hw_get_parent_by_index(hw, i);
1181                 if (!parent)
1182                         continue;
1183
1184                 /*
1185                  * Don't choose a PLLC-derived clock as our parent
1186                  * unless it had been manually set that way.  PLLC's
1187                  * frequency gets adjusted by the firmware due to
1188                  * over-temp or under-voltage conditions, without
1189                  * prior notification to our clock consumer.
1190                  */
1191                 if (bcm2835_clk_is_pllc(parent) && !current_parent_is_pllc)
1192                         continue;
1193
1194                 rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate,
1195                                                           &div, &prate,
1196                                                           &avgrate);
1197                 if (abs(req->rate - rate) < abs(req->rate - best_rate)) {
1198                         best_parent = parent;
1199                         best_prate = prate;
1200                         best_rate = rate;
1201                         best_avgrate = avgrate;
1202                 }
1203         }
1204
1205         if (!best_parent)
1206                 return -EINVAL;
1207
1208         req->best_parent_hw = best_parent;
1209         req->best_parent_rate = best_prate;
1210
1211         req->rate = best_avgrate;
1212
1213         return 0;
1214 }
1215
1216 static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
1217 {
1218         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1219         struct bcm2835_cprman *cprman = clock->cprman;
1220         const struct bcm2835_clock_data *data = clock->data;
1221         u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
1222
1223         cprman_write(cprman, data->ctl_reg, src);
1224         return 0;
1225 }
1226
1227 static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
1228 {
1229         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1230         struct bcm2835_cprman *cprman = clock->cprman;
1231         const struct bcm2835_clock_data *data = clock->data;
1232         u32 src = cprman_read(cprman, data->ctl_reg);
1233
1234         return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
1235 }
1236
1237 static struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = {
1238         {
1239                 .name = "ctl",
1240                 .offset = 0,
1241         },
1242         {
1243                 .name = "div",
1244                 .offset = 4,
1245         },
1246 };
1247
1248 static void bcm2835_clock_debug_init(struct clk_hw *hw,
1249                                     struct dentry *dentry)
1250 {
1251         struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1252         struct bcm2835_cprman *cprman = clock->cprman;
1253         const struct bcm2835_clock_data *data = clock->data;
1254
1255         bcm2835_debugfs_regset(cprman, data->ctl_reg,
1256                 bcm2835_debugfs_clock_reg32,
1257                 ARRAY_SIZE(bcm2835_debugfs_clock_reg32),
1258                 dentry);
1259 }
1260
1261 static const struct clk_ops bcm2835_clock_clk_ops = {
1262         .is_prepared = bcm2835_clock_is_on,
1263         .prepare = bcm2835_clock_on,
1264         .unprepare = bcm2835_clock_off,
1265         .recalc_rate = bcm2835_clock_get_rate,
1266         .set_rate = bcm2835_clock_set_rate,
1267         .determine_rate = bcm2835_clock_determine_rate,
1268         .set_parent = bcm2835_clock_set_parent,
1269         .get_parent = bcm2835_clock_get_parent,
1270         .debug_init = bcm2835_clock_debug_init,
1271 };
1272
1273 static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1274 {
1275         return true;
1276 }
1277
1278 /*
1279  * The VPU clock can never be disabled (it doesn't have an ENABLE
1280  * bit), so it gets its own set of clock ops.
1281  */
1282 static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1283         .is_prepared = bcm2835_vpu_clock_is_on,
1284         .recalc_rate = bcm2835_clock_get_rate,
1285         .set_rate = bcm2835_clock_set_rate,
1286         .determine_rate = bcm2835_clock_determine_rate,
1287         .set_parent = bcm2835_clock_set_parent,
1288         .get_parent = bcm2835_clock_get_parent,
1289         .debug_init = bcm2835_clock_debug_init,
1290 };
1291
1292 static struct clk_hw *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1293                                            const struct bcm2835_pll_data *data)
1294 {
1295         struct bcm2835_pll *pll;
1296         struct clk_init_data init;
1297         int ret;
1298
1299         memset(&init, 0, sizeof(init));
1300
1301         /* All of the PLLs derive from the external oscillator. */
1302         init.parent_names = &cprman->real_parent_names[0];
1303         init.num_parents = 1;
1304         init.name = data->name;
1305         init.ops = &bcm2835_pll_clk_ops;
1306         init.flags = CLK_IGNORE_UNUSED;
1307
1308         pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1309         if (!pll)
1310                 return NULL;
1311
1312         pll->cprman = cprman;
1313         pll->data = data;
1314         pll->hw.init = &init;
1315
1316         ret = devm_clk_hw_register(cprman->dev, &pll->hw);
1317         if (ret) {
1318                 kfree(pll);
1319                 return NULL;
1320         }
1321         return &pll->hw;
1322 }
1323
1324 static struct clk_hw *
1325 bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1326                              const struct bcm2835_pll_divider_data *data)
1327 {
1328         struct bcm2835_pll_divider *divider;
1329         struct clk_init_data init;
1330         const char *divider_name;
1331         int ret;
1332
1333         if (data->fixed_divider != 1) {
1334                 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1335                                               "%s_prediv", data->name);
1336                 if (!divider_name)
1337                         return NULL;
1338         } else {
1339                 divider_name = data->name;
1340         }
1341
1342         memset(&init, 0, sizeof(init));
1343
1344         init.parent_names = &data->source_pll;
1345         init.num_parents = 1;
1346         init.name = divider_name;
1347         init.ops = &bcm2835_pll_divider_clk_ops;
1348         init.flags = data->flags | CLK_IGNORE_UNUSED;
1349
1350         divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1351         if (!divider)
1352                 return NULL;
1353
1354         divider->div.reg = cprman->regs + data->a2w_reg;
1355         divider->div.shift = A2W_PLL_DIV_SHIFT;
1356         divider->div.width = A2W_PLL_DIV_BITS;
1357         divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
1358         divider->div.lock = &cprman->regs_lock;
1359         divider->div.hw.init = &init;
1360         divider->div.table = NULL;
1361
1362         divider->cprman = cprman;
1363         divider->data = data;
1364
1365         ret = devm_clk_hw_register(cprman->dev, &divider->div.hw);
1366         if (ret)
1367                 return ERR_PTR(ret);
1368
1369         /*
1370          * PLLH's channels have a fixed divide by 10 afterwards, which
1371          * is what our consumers are actually using.
1372          */
1373         if (data->fixed_divider != 1) {
1374                 return clk_hw_register_fixed_factor(cprman->dev, data->name,
1375                                                     divider_name,
1376                                                     CLK_SET_RATE_PARENT,
1377                                                     1,
1378                                                     data->fixed_divider);
1379         }
1380
1381         return &divider->div.hw;
1382 }
1383
1384 static struct clk_hw *bcm2835_register_clock(struct bcm2835_cprman *cprman,
1385                                           const struct bcm2835_clock_data *data)
1386 {
1387         struct bcm2835_clock *clock;
1388         struct clk_init_data init;
1389         const char *parents[1 << CM_SRC_BITS];
1390         size_t i;
1391         int ret;
1392
1393         /*
1394          * Replace our strings referencing parent clocks with the
1395          * actual clock-output-name of the parent.
1396          */
1397         for (i = 0; i < data->num_mux_parents; i++) {
1398                 parents[i] = data->parents[i];
1399
1400                 ret = match_string(cprman_parent_names,
1401                                    ARRAY_SIZE(cprman_parent_names),
1402                                    parents[i]);
1403                 if (ret >= 0)
1404                         parents[i] = cprman->real_parent_names[ret];
1405         }
1406
1407         memset(&init, 0, sizeof(init));
1408         init.parent_names = parents;
1409         init.num_parents = data->num_mux_parents;
1410         init.name = data->name;
1411         init.flags = data->flags | CLK_IGNORE_UNUSED;
1412
1413         /*
1414          * Pass the CLK_SET_RATE_PARENT flag if we are allowed to propagate
1415          * rate changes on at least of the parents.
1416          */
1417         if (data->set_rate_parent)
1418                 init.flags |= CLK_SET_RATE_PARENT;
1419
1420         if (data->is_vpu_clock) {
1421                 init.ops = &bcm2835_vpu_clock_clk_ops;
1422         } else {
1423                 init.ops = &bcm2835_clock_clk_ops;
1424                 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
1425
1426                 /* If the clock wasn't actually enabled at boot, it's not
1427                  * critical.
1428                  */
1429                 if (!(cprman_read(cprman, data->ctl_reg) & CM_ENABLE))
1430                         init.flags &= ~CLK_IS_CRITICAL;
1431         }
1432
1433         clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1434         if (!clock)
1435                 return NULL;
1436
1437         clock->cprman = cprman;
1438         clock->data = data;
1439         clock->hw.init = &init;
1440
1441         ret = devm_clk_hw_register(cprman->dev, &clock->hw);
1442         if (ret)
1443                 return ERR_PTR(ret);
1444         return &clock->hw;
1445 }
1446
1447 static struct clk_hw *bcm2835_register_gate(struct bcm2835_cprman *cprman,
1448                                          const struct bcm2835_gate_data *data)
1449 {
1450         return clk_hw_register_gate(cprman->dev, data->name, data->parent,
1451                                     CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1452                                     cprman->regs + data->ctl_reg,
1453                                     CM_GATE_BIT, 0, &cprman->regs_lock);
1454 }
1455
1456 typedef struct clk_hw *(*bcm2835_clk_register)(struct bcm2835_cprman *cprman,
1457                                                const void *data);
1458 struct bcm2835_clk_desc {
1459         bcm2835_clk_register clk_register;
1460         const void *data;
1461 };
1462
1463 /* assignment helper macros for different clock types */
1464 #define _REGISTER(f, ...) { .clk_register = (bcm2835_clk_register)f, \
1465                             .data = __VA_ARGS__ }
1466 #define REGISTER_PLL(...)       _REGISTER(&bcm2835_register_pll,        \
1467                                           &(struct bcm2835_pll_data)    \
1468                                           {__VA_ARGS__})
1469 #define REGISTER_PLL_DIV(...)   _REGISTER(&bcm2835_register_pll_divider, \
1470                                           &(struct bcm2835_pll_divider_data) \
1471                                           {__VA_ARGS__})
1472 #define REGISTER_CLK(...)       _REGISTER(&bcm2835_register_clock,      \
1473                                           &(struct bcm2835_clock_data)  \
1474                                           {__VA_ARGS__})
1475 #define REGISTER_GATE(...)      _REGISTER(&bcm2835_register_gate,       \
1476                                           &(struct bcm2835_gate_data)   \
1477                                           {__VA_ARGS__})
1478
1479 /* parent mux arrays plus helper macros */
1480
1481 /* main oscillator parent mux */
1482 static const char *const bcm2835_clock_osc_parents[] = {
1483         "gnd",
1484         "xosc",
1485         "testdebug0",
1486         "testdebug1"
1487 };
1488
1489 #define REGISTER_OSC_CLK(...)   REGISTER_CLK(                           \
1490         .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),       \
1491         .parents = bcm2835_clock_osc_parents,                           \
1492         __VA_ARGS__)
1493
1494 /* main peripherial parent mux */
1495 static const char *const bcm2835_clock_per_parents[] = {
1496         "gnd",
1497         "xosc",
1498         "testdebug0",
1499         "testdebug1",
1500         "plla_per",
1501         "pllc_per",
1502         "plld_per",
1503         "pllh_aux",
1504 };
1505
1506 #define REGISTER_PER_CLK(...)   REGISTER_CLK(                           \
1507         .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),       \
1508         .parents = bcm2835_clock_per_parents,                           \
1509         __VA_ARGS__)
1510
1511 /*
1512  * Restrict clock sources for the PCM peripheral to the oscillator and
1513  * PLLD_PER because other source may have varying rates or be switched
1514  * off.
1515  *
1516  * Prevent other sources from being selected by replacing their names in
1517  * the list of potential parents with dummy entries (entry index is
1518  * significant).
1519  */
1520 static const char *const bcm2835_pcm_per_parents[] = {
1521         "-",
1522         "xosc",
1523         "-",
1524         "-",
1525         "-",
1526         "-",
1527         "plld_per",
1528         "-",
1529 };
1530
1531 #define REGISTER_PCM_CLK(...)   REGISTER_CLK(                           \
1532         .num_mux_parents = ARRAY_SIZE(bcm2835_pcm_per_parents),         \
1533         .parents = bcm2835_pcm_per_parents,                             \
1534         __VA_ARGS__)
1535
1536 /* main vpu parent mux */
1537 static const char *const bcm2835_clock_vpu_parents[] = {
1538         "gnd",
1539         "xosc",
1540         "testdebug0",
1541         "testdebug1",
1542         "plla_core",
1543         "pllc_core0",
1544         "plld_core",
1545         "pllh_aux",
1546         "pllc_core1",
1547         "pllc_core2",
1548 };
1549
1550 #define REGISTER_VPU_CLK(...)   REGISTER_CLK(                           \
1551         .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),       \
1552         .parents = bcm2835_clock_vpu_parents,                           \
1553         __VA_ARGS__)
1554
1555 /*
1556  * DSI parent clocks.  The DSI byte/DDR/DDR2 clocks come from the DSI
1557  * analog PHY.  The _inv variants are generated internally to cprman,
1558  * but we don't use them so they aren't hooked up.
1559  */
1560 static const char *const bcm2835_clock_dsi0_parents[] = {
1561         "gnd",
1562         "xosc",
1563         "testdebug0",
1564         "testdebug1",
1565         "dsi0_ddr",
1566         "dsi0_ddr_inv",
1567         "dsi0_ddr2",
1568         "dsi0_ddr2_inv",
1569         "dsi0_byte",
1570         "dsi0_byte_inv",
1571 };
1572
1573 static const char *const bcm2835_clock_dsi1_parents[] = {
1574         "gnd",
1575         "xosc",
1576         "testdebug0",
1577         "testdebug1",
1578         "dsi1_ddr",
1579         "dsi1_ddr_inv",
1580         "dsi1_ddr2",
1581         "dsi1_ddr2_inv",
1582         "dsi1_byte",
1583         "dsi1_byte_inv",
1584 };
1585
1586 #define REGISTER_DSI0_CLK(...)  REGISTER_CLK(                           \
1587         .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi0_parents),      \
1588         .parents = bcm2835_clock_dsi0_parents,                          \
1589         __VA_ARGS__)
1590
1591 #define REGISTER_DSI1_CLK(...)  REGISTER_CLK(                           \
1592         .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi1_parents),      \
1593         .parents = bcm2835_clock_dsi1_parents,                          \
1594         __VA_ARGS__)
1595
1596 /*
1597  * the real definition of all the pll, pll_dividers and clocks
1598  * these make use of the above REGISTER_* macros
1599  */
1600 static const struct bcm2835_clk_desc clk_desc_array[] = {
1601         /* the PLL + PLL dividers */
1602
1603         /*
1604          * PLLA is the auxiliary PLL, used to drive the CCP2
1605          * (Compact Camera Port 2) transmitter clock.
1606          *
1607          * It is in the PX LDO power domain, which is on when the
1608          * AUDIO domain is on.
1609          */
1610         [BCM2835_PLLA]          = REGISTER_PLL(
1611                 .name = "plla",
1612                 .cm_ctrl_reg = CM_PLLA,
1613                 .a2w_ctrl_reg = A2W_PLLA_CTRL,
1614                 .frac_reg = A2W_PLLA_FRAC,
1615                 .ana_reg_base = A2W_PLLA_ANA0,
1616                 .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
1617                 .lock_mask = CM_LOCK_FLOCKA,
1618
1619                 .ana = &bcm2835_ana_default,
1620
1621                 .min_rate = 600000000u,
1622                 .max_rate = 2400000000u,
1623                 .max_fb_rate = BCM2835_MAX_FB_RATE),
1624         [BCM2835_PLLA_CORE]     = REGISTER_PLL_DIV(
1625                 .name = "plla_core",
1626                 .source_pll = "plla",
1627                 .cm_reg = CM_PLLA,
1628                 .a2w_reg = A2W_PLLA_CORE,
1629                 .load_mask = CM_PLLA_LOADCORE,
1630                 .hold_mask = CM_PLLA_HOLDCORE,
1631                 .fixed_divider = 1,
1632                 .flags = CLK_SET_RATE_PARENT),
1633         [BCM2835_PLLA_PER]      = REGISTER_PLL_DIV(
1634                 .name = "plla_per",
1635                 .source_pll = "plla",
1636                 .cm_reg = CM_PLLA,
1637                 .a2w_reg = A2W_PLLA_PER,
1638                 .load_mask = CM_PLLA_LOADPER,
1639                 .hold_mask = CM_PLLA_HOLDPER,
1640                 .fixed_divider = 1,
1641                 .flags = CLK_SET_RATE_PARENT),
1642         [BCM2835_PLLA_DSI0]     = REGISTER_PLL_DIV(
1643                 .name = "plla_dsi0",
1644                 .source_pll = "plla",
1645                 .cm_reg = CM_PLLA,
1646                 .a2w_reg = A2W_PLLA_DSI0,
1647                 .load_mask = CM_PLLA_LOADDSI0,
1648                 .hold_mask = CM_PLLA_HOLDDSI0,
1649                 .fixed_divider = 1),
1650         [BCM2835_PLLA_CCP2]     = REGISTER_PLL_DIV(
1651                 .name = "plla_ccp2",
1652                 .source_pll = "plla",
1653                 .cm_reg = CM_PLLA,
1654                 .a2w_reg = A2W_PLLA_CCP2,
1655                 .load_mask = CM_PLLA_LOADCCP2,
1656                 .hold_mask = CM_PLLA_HOLDCCP2,
1657                 .fixed_divider = 1,
1658                 .flags = CLK_SET_RATE_PARENT),
1659
1660         /* PLLB is used for the ARM's clock. */
1661         [BCM2835_PLLB]          = REGISTER_PLL(
1662                 .name = "pllb",
1663                 .cm_ctrl_reg = CM_PLLB,
1664                 .a2w_ctrl_reg = A2W_PLLB_CTRL,
1665                 .frac_reg = A2W_PLLB_FRAC,
1666                 .ana_reg_base = A2W_PLLB_ANA0,
1667                 .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
1668                 .lock_mask = CM_LOCK_FLOCKB,
1669
1670                 .ana = &bcm2835_ana_default,
1671
1672                 .min_rate = 600000000u,
1673                 .max_rate = 3000000000u,
1674                 .max_fb_rate = BCM2835_MAX_FB_RATE),
1675         [BCM2835_PLLB_ARM]      = REGISTER_PLL_DIV(
1676                 .name = "pllb_arm",
1677                 .source_pll = "pllb",
1678                 .cm_reg = CM_PLLB,
1679                 .a2w_reg = A2W_PLLB_ARM,
1680                 .load_mask = CM_PLLB_LOADARM,
1681                 .hold_mask = CM_PLLB_HOLDARM,
1682                 .fixed_divider = 1,
1683                 .flags = CLK_SET_RATE_PARENT),
1684
1685         /*
1686          * PLLC is the core PLL, used to drive the core VPU clock.
1687          *
1688          * It is in the PX LDO power domain, which is on when the
1689          * AUDIO domain is on.
1690          */
1691         [BCM2835_PLLC]          = REGISTER_PLL(
1692                 .name = "pllc",
1693                 .cm_ctrl_reg = CM_PLLC,
1694                 .a2w_ctrl_reg = A2W_PLLC_CTRL,
1695                 .frac_reg = A2W_PLLC_FRAC,
1696                 .ana_reg_base = A2W_PLLC_ANA0,
1697                 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1698                 .lock_mask = CM_LOCK_FLOCKC,
1699
1700                 .ana = &bcm2835_ana_default,
1701
1702                 .min_rate = 600000000u,
1703                 .max_rate = 3000000000u,
1704                 .max_fb_rate = BCM2835_MAX_FB_RATE),
1705         [BCM2835_PLLC_CORE0]    = REGISTER_PLL_DIV(
1706                 .name = "pllc_core0",
1707                 .source_pll = "pllc",
1708                 .cm_reg = CM_PLLC,
1709                 .a2w_reg = A2W_PLLC_CORE0,
1710                 .load_mask = CM_PLLC_LOADCORE0,
1711                 .hold_mask = CM_PLLC_HOLDCORE0,
1712                 .fixed_divider = 1,
1713                 .flags = CLK_SET_RATE_PARENT),
1714         [BCM2835_PLLC_CORE1]    = REGISTER_PLL_DIV(
1715                 .name = "pllc_core1",
1716                 .source_pll = "pllc",
1717                 .cm_reg = CM_PLLC,
1718                 .a2w_reg = A2W_PLLC_CORE1,
1719                 .load_mask = CM_PLLC_LOADCORE1,
1720                 .hold_mask = CM_PLLC_HOLDCORE1,
1721                 .fixed_divider = 1,
1722                 .flags = CLK_SET_RATE_PARENT),
1723         [BCM2835_PLLC_CORE2]    = REGISTER_PLL_DIV(
1724                 .name = "pllc_core2",
1725                 .source_pll = "pllc",
1726                 .cm_reg = CM_PLLC,
1727                 .a2w_reg = A2W_PLLC_CORE2,
1728                 .load_mask = CM_PLLC_LOADCORE2,
1729                 .hold_mask = CM_PLLC_HOLDCORE2,
1730                 .fixed_divider = 1,
1731                 .flags = CLK_SET_RATE_PARENT),
1732         [BCM2835_PLLC_PER]      = REGISTER_PLL_DIV(
1733                 .name = "pllc_per",
1734                 .source_pll = "pllc",
1735                 .cm_reg = CM_PLLC,
1736                 .a2w_reg = A2W_PLLC_PER,
1737                 .load_mask = CM_PLLC_LOADPER,
1738                 .hold_mask = CM_PLLC_HOLDPER,
1739                 .fixed_divider = 1,
1740                 .flags = CLK_IS_CRITICAL | CLK_SET_RATE_PARENT),
1741
1742         /*
1743          * PLLD is the display PLL, used to drive DSI display panels.
1744          *
1745          * It is in the PX LDO power domain, which is on when the
1746          * AUDIO domain is on.
1747          */
1748         [BCM2835_PLLD]          = REGISTER_PLL(
1749                 .name = "plld",
1750                 .cm_ctrl_reg = CM_PLLD,
1751                 .a2w_ctrl_reg = A2W_PLLD_CTRL,
1752                 .frac_reg = A2W_PLLD_FRAC,
1753                 .ana_reg_base = A2W_PLLD_ANA0,
1754                 .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
1755                 .lock_mask = CM_LOCK_FLOCKD,
1756
1757                 .ana = &bcm2835_ana_default,
1758
1759                 .min_rate = 600000000u,
1760                 .max_rate = 2400000000u,
1761                 .max_fb_rate = BCM2835_MAX_FB_RATE),
1762         [BCM2835_PLLD_CORE]     = REGISTER_PLL_DIV(
1763                 .name = "plld_core",
1764                 .source_pll = "plld",
1765                 .cm_reg = CM_PLLD,
1766                 .a2w_reg = A2W_PLLD_CORE,
1767                 .load_mask = CM_PLLD_LOADCORE,
1768                 .hold_mask = CM_PLLD_HOLDCORE,
1769                 .fixed_divider = 1,
1770                 .flags = CLK_SET_RATE_PARENT),
1771         [BCM2835_PLLD_PER]      = REGISTER_PLL_DIV(
1772                 .name = "plld_per",
1773                 .source_pll = "plld",
1774                 .cm_reg = CM_PLLD,
1775                 .a2w_reg = A2W_PLLD_PER,
1776                 .load_mask = CM_PLLD_LOADPER,
1777                 .hold_mask = CM_PLLD_HOLDPER,
1778                 .fixed_divider = 1,
1779                 .flags = CLK_SET_RATE_PARENT),
1780         [BCM2835_PLLD_DSI0]     = REGISTER_PLL_DIV(
1781                 .name = "plld_dsi0",
1782                 .source_pll = "plld",
1783                 .cm_reg = CM_PLLD,
1784                 .a2w_reg = A2W_PLLD_DSI0,
1785                 .load_mask = CM_PLLD_LOADDSI0,
1786                 .hold_mask = CM_PLLD_HOLDDSI0,
1787                 .fixed_divider = 1),
1788         [BCM2835_PLLD_DSI1]     = REGISTER_PLL_DIV(
1789                 .name = "plld_dsi1",
1790                 .source_pll = "plld",
1791                 .cm_reg = CM_PLLD,
1792                 .a2w_reg = A2W_PLLD_DSI1,
1793                 .load_mask = CM_PLLD_LOADDSI1,
1794                 .hold_mask = CM_PLLD_HOLDDSI1,
1795                 .fixed_divider = 1),
1796
1797         /*
1798          * PLLH is used to supply the pixel clock or the AUX clock for the
1799          * TV encoder.
1800          *
1801          * It is in the HDMI power domain.
1802          */
1803         [BCM2835_PLLH]          = REGISTER_PLL(
1804                 "pllh",
1805                 .cm_ctrl_reg = CM_PLLH,
1806                 .a2w_ctrl_reg = A2W_PLLH_CTRL,
1807                 .frac_reg = A2W_PLLH_FRAC,
1808                 .ana_reg_base = A2W_PLLH_ANA0,
1809                 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1810                 .lock_mask = CM_LOCK_FLOCKH,
1811
1812                 .ana = &bcm2835_ana_pllh,
1813
1814                 .min_rate = 600000000u,
1815                 .max_rate = 3000000000u,
1816                 .max_fb_rate = BCM2835_MAX_FB_RATE),
1817         [BCM2835_PLLH_RCAL]     = REGISTER_PLL_DIV(
1818                 .name = "pllh_rcal",
1819                 .source_pll = "pllh",
1820                 .cm_reg = CM_PLLH,
1821                 .a2w_reg = A2W_PLLH_RCAL,
1822                 .load_mask = CM_PLLH_LOADRCAL,
1823                 .hold_mask = 0,
1824                 .fixed_divider = 10,
1825                 .flags = CLK_SET_RATE_PARENT),
1826         [BCM2835_PLLH_AUX]      = REGISTER_PLL_DIV(
1827                 .name = "pllh_aux",
1828                 .source_pll = "pllh",
1829                 .cm_reg = CM_PLLH,
1830                 .a2w_reg = A2W_PLLH_AUX,
1831                 .load_mask = CM_PLLH_LOADAUX,
1832                 .hold_mask = 0,
1833                 .fixed_divider = 1,
1834                 .flags = CLK_SET_RATE_PARENT),
1835         [BCM2835_PLLH_PIX]      = REGISTER_PLL_DIV(
1836                 .name = "pllh_pix",
1837                 .source_pll = "pllh",
1838                 .cm_reg = CM_PLLH,
1839                 .a2w_reg = A2W_PLLH_PIX,
1840                 .load_mask = CM_PLLH_LOADPIX,
1841                 .hold_mask = 0,
1842                 .fixed_divider = 10,
1843                 .flags = CLK_SET_RATE_PARENT),
1844
1845         /* the clocks */
1846
1847         /* clocks with oscillator parent mux */
1848
1849         /* One Time Programmable Memory clock.  Maximum 10Mhz. */
1850         [BCM2835_CLOCK_OTP]     = REGISTER_OSC_CLK(
1851                 .name = "otp",
1852                 .ctl_reg = CM_OTPCTL,
1853                 .div_reg = CM_OTPDIV,
1854                 .int_bits = 4,
1855                 .frac_bits = 0,
1856                 .tcnt_mux = 6),
1857         /*
1858          * Used for a 1Mhz clock for the system clocksource, and also used
1859          * bythe watchdog timer and the camera pulse generator.
1860          */
1861         [BCM2835_CLOCK_TIMER]   = REGISTER_OSC_CLK(
1862                 .name = "timer",
1863                 .ctl_reg = CM_TIMERCTL,
1864                 .div_reg = CM_TIMERDIV,
1865                 .int_bits = 6,
1866                 .frac_bits = 12),
1867         /*
1868          * Clock for the temperature sensor.
1869          * Generally run at 2Mhz, max 5Mhz.
1870          */
1871         [BCM2835_CLOCK_TSENS]   = REGISTER_OSC_CLK(
1872                 .name = "tsens",
1873                 .ctl_reg = CM_TSENSCTL,
1874                 .div_reg = CM_TSENSDIV,
1875                 .int_bits = 5,
1876                 .frac_bits = 0),
1877         [BCM2835_CLOCK_TEC]     = REGISTER_OSC_CLK(
1878                 .name = "tec",
1879                 .ctl_reg = CM_TECCTL,
1880                 .div_reg = CM_TECDIV,
1881                 .int_bits = 6,
1882                 .frac_bits = 0),
1883
1884         /* clocks with vpu parent mux */
1885         [BCM2835_CLOCK_H264]    = REGISTER_VPU_CLK(
1886                 .name = "h264",
1887                 .ctl_reg = CM_H264CTL,
1888                 .div_reg = CM_H264DIV,
1889                 .int_bits = 4,
1890                 .frac_bits = 8,
1891                 .tcnt_mux = 1),
1892         [BCM2835_CLOCK_ISP]     = REGISTER_VPU_CLK(
1893                 .name = "isp",
1894                 .ctl_reg = CM_ISPCTL,
1895                 .div_reg = CM_ISPDIV,
1896                 .int_bits = 4,
1897                 .frac_bits = 8,
1898                 .tcnt_mux = 2),
1899
1900         /*
1901          * Secondary SDRAM clock.  Used for low-voltage modes when the PLL
1902          * in the SDRAM controller can't be used.
1903          */
1904         [BCM2835_CLOCK_SDRAM]   = REGISTER_VPU_CLK(
1905                 .name = "sdram",
1906                 .ctl_reg = CM_SDCCTL,
1907                 .div_reg = CM_SDCDIV,
1908                 .int_bits = 6,
1909                 .frac_bits = 0,
1910                 .tcnt_mux = 3),
1911         [BCM2835_CLOCK_V3D]     = REGISTER_VPU_CLK(
1912                 .name = "v3d",
1913                 .ctl_reg = CM_V3DCTL,
1914                 .div_reg = CM_V3DDIV,
1915                 .int_bits = 4,
1916                 .frac_bits = 8,
1917                 .tcnt_mux = 4),
1918         /*
1919          * VPU clock.  This doesn't have an enable bit, since it drives
1920          * the bus for everything else, and is special so it doesn't need
1921          * to be gated for rate changes.  It is also known as "clk_audio"
1922          * in various hardware documentation.
1923          */
1924         [BCM2835_CLOCK_VPU]     = REGISTER_VPU_CLK(
1925                 .name = "vpu",
1926                 .ctl_reg = CM_VPUCTL,
1927                 .div_reg = CM_VPUDIV,
1928                 .int_bits = 12,
1929                 .frac_bits = 8,
1930                 .flags = CLK_IS_CRITICAL,
1931                 .is_vpu_clock = true,
1932                 .tcnt_mux = 5),
1933
1934         /* clocks with per parent mux */
1935         [BCM2835_CLOCK_AVEO]    = REGISTER_PER_CLK(
1936                 .name = "aveo",
1937                 .ctl_reg = CM_AVEOCTL,
1938                 .div_reg = CM_AVEODIV,
1939                 .int_bits = 4,
1940                 .frac_bits = 0,
1941                 .tcnt_mux = 38),
1942         [BCM2835_CLOCK_CAM0]    = REGISTER_PER_CLK(
1943                 .name = "cam0",
1944                 .ctl_reg = CM_CAM0CTL,
1945                 .div_reg = CM_CAM0DIV,
1946                 .int_bits = 4,
1947                 .frac_bits = 8,
1948                 .tcnt_mux = 14),
1949         [BCM2835_CLOCK_CAM1]    = REGISTER_PER_CLK(
1950                 .name = "cam1",
1951                 .ctl_reg = CM_CAM1CTL,
1952                 .div_reg = CM_CAM1DIV,
1953                 .int_bits = 4,
1954                 .frac_bits = 8,
1955                 .tcnt_mux = 15),
1956         [BCM2835_CLOCK_DFT]     = REGISTER_PER_CLK(
1957                 .name = "dft",
1958                 .ctl_reg = CM_DFTCTL,
1959                 .div_reg = CM_DFTDIV,
1960                 .int_bits = 5,
1961                 .frac_bits = 0),
1962         [BCM2835_CLOCK_DPI]     = REGISTER_PER_CLK(
1963                 .name = "dpi",
1964                 .ctl_reg = CM_DPICTL,
1965                 .div_reg = CM_DPIDIV,
1966                 .int_bits = 4,
1967                 .frac_bits = 8,
1968                 .tcnt_mux = 17),
1969
1970         /* Arasan EMMC clock */
1971         [BCM2835_CLOCK_EMMC]    = REGISTER_PER_CLK(
1972                 .name = "emmc",
1973                 .ctl_reg = CM_EMMCCTL,
1974                 .div_reg = CM_EMMCDIV,
1975                 .int_bits = 4,
1976                 .frac_bits = 8,
1977                 .tcnt_mux = 39),
1978
1979         /* General purpose (GPIO) clocks */
1980         [BCM2835_CLOCK_GP0]     = REGISTER_PER_CLK(
1981                 .name = "gp0",
1982                 .ctl_reg = CM_GP0CTL,
1983                 .div_reg = CM_GP0DIV,
1984                 .int_bits = 12,
1985                 .frac_bits = 12,
1986                 .is_mash_clock = true,
1987                 .tcnt_mux = 20),
1988         [BCM2835_CLOCK_GP1]     = REGISTER_PER_CLK(
1989                 .name = "gp1",
1990                 .ctl_reg = CM_GP1CTL,
1991                 .div_reg = CM_GP1DIV,
1992                 .int_bits = 12,
1993                 .frac_bits = 12,
1994                 .flags = CLK_IS_CRITICAL,
1995                 .is_mash_clock = true,
1996                 .tcnt_mux = 21),
1997         [BCM2835_CLOCK_GP2]     = REGISTER_PER_CLK(
1998                 .name = "gp2",
1999                 .ctl_reg = CM_GP2CTL,
2000                 .div_reg = CM_GP2DIV,
2001                 .int_bits = 12,
2002                 .frac_bits = 12,
2003                 .flags = CLK_IS_CRITICAL),
2004
2005         /* HDMI state machine */
2006         [BCM2835_CLOCK_HSM]     = REGISTER_PER_CLK(
2007                 .name = "hsm",
2008                 .ctl_reg = CM_HSMCTL,
2009                 .div_reg = CM_HSMDIV,
2010                 .int_bits = 4,
2011                 .frac_bits = 8,
2012                 .tcnt_mux = 22),
2013         [BCM2835_CLOCK_PCM]     = REGISTER_PCM_CLK(
2014                 .name = "pcm",
2015                 .ctl_reg = CM_PCMCTL,
2016                 .div_reg = CM_PCMDIV,
2017                 .int_bits = 12,
2018                 .frac_bits = 12,
2019                 .is_mash_clock = true,
2020                 .low_jitter = true,
2021                 .tcnt_mux = 23),
2022         [BCM2835_CLOCK_PWM]     = REGISTER_PER_CLK(
2023                 .name = "pwm",
2024                 .ctl_reg = CM_PWMCTL,
2025                 .div_reg = CM_PWMDIV,
2026                 .int_bits = 12,
2027                 .frac_bits = 12,
2028                 .is_mash_clock = true,
2029                 .tcnt_mux = 24),
2030         [BCM2835_CLOCK_SLIM]    = REGISTER_PER_CLK(
2031                 .name = "slim",
2032                 .ctl_reg = CM_SLIMCTL,
2033                 .div_reg = CM_SLIMDIV,
2034                 .int_bits = 12,
2035                 .frac_bits = 12,
2036                 .is_mash_clock = true,
2037                 .tcnt_mux = 25),
2038         [BCM2835_CLOCK_SMI]     = REGISTER_PER_CLK(
2039                 .name = "smi",
2040                 .ctl_reg = CM_SMICTL,
2041                 .div_reg = CM_SMIDIV,
2042                 .int_bits = 4,
2043                 .frac_bits = 8,
2044                 .tcnt_mux = 27),
2045         [BCM2835_CLOCK_UART]    = REGISTER_PER_CLK(
2046                 .name = "uart",
2047                 .ctl_reg = CM_UARTCTL,
2048                 .div_reg = CM_UARTDIV,
2049                 .int_bits = 10,
2050                 .frac_bits = 12,
2051                 .tcnt_mux = 28),
2052
2053         /* TV encoder clock.  Only operating frequency is 108Mhz.  */
2054         [BCM2835_CLOCK_VEC]     = REGISTER_PER_CLK(
2055                 .name = "vec",
2056                 .ctl_reg = CM_VECCTL,
2057                 .div_reg = CM_VECDIV,
2058                 .int_bits = 4,
2059                 .frac_bits = 0,
2060                 /*
2061                  * Allow rate change propagation only on PLLH_AUX which is
2062                  * assigned index 7 in the parent array.
2063                  */
2064                 .set_rate_parent = BIT(7),
2065                 .tcnt_mux = 29),
2066
2067         /* dsi clocks */
2068         [BCM2835_CLOCK_DSI0E]   = REGISTER_PER_CLK(
2069                 .name = "dsi0e",
2070                 .ctl_reg = CM_DSI0ECTL,
2071                 .div_reg = CM_DSI0EDIV,
2072                 .int_bits = 4,
2073                 .frac_bits = 8,
2074                 .tcnt_mux = 18),
2075         [BCM2835_CLOCK_DSI1E]   = REGISTER_PER_CLK(
2076                 .name = "dsi1e",
2077                 .ctl_reg = CM_DSI1ECTL,
2078                 .div_reg = CM_DSI1EDIV,
2079                 .int_bits = 4,
2080                 .frac_bits = 8,
2081                 .tcnt_mux = 19),
2082         [BCM2835_CLOCK_DSI0P]   = REGISTER_DSI0_CLK(
2083                 .name = "dsi0p",
2084                 .ctl_reg = CM_DSI0PCTL,
2085                 .div_reg = CM_DSI0PDIV,
2086                 .int_bits = 0,
2087                 .frac_bits = 0,
2088                 .tcnt_mux = 12),
2089         [BCM2835_CLOCK_DSI1P]   = REGISTER_DSI1_CLK(
2090                 .name = "dsi1p",
2091                 .ctl_reg = CM_DSI1PCTL,
2092                 .div_reg = CM_DSI1PDIV,
2093                 .int_bits = 0,
2094                 .frac_bits = 0,
2095                 .tcnt_mux = 13),
2096
2097         /* the gates */
2098
2099         /*
2100          * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
2101          * you have the debug bit set in the power manager, which we
2102          * don't bother exposing) are individual gates off of the
2103          * non-stop vpu clock.
2104          */
2105         [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE(
2106                 .name = "peri_image",
2107                 .parent = "vpu",
2108                 .ctl_reg = CM_PERIICTL),
2109 };
2110
2111 /*
2112  * Permanently take a reference on the parent of the SDRAM clock.
2113  *
2114  * While the SDRAM is being driven by its dedicated PLL most of the
2115  * time, there is a little loop running in the firmware that
2116  * periodically switches the SDRAM to using our CM clock to do PVT
2117  * recalibration, with the assumption that the previously configured
2118  * SDRAM parent is still enabled and running.
2119  */
2120 static int bcm2835_mark_sdc_parent_critical(struct clk *sdc)
2121 {
2122         struct clk *parent = clk_get_parent(sdc);
2123
2124         if (IS_ERR(parent))
2125                 return PTR_ERR(parent);
2126
2127         return clk_prepare_enable(parent);
2128 }
2129
2130 static int bcm2835_clk_probe(struct platform_device *pdev)
2131 {
2132         struct device *dev = &pdev->dev;
2133         struct clk_hw **hws;
2134         struct bcm2835_cprman *cprman;
2135         struct resource *res;
2136         const struct bcm2835_clk_desc *desc;
2137         const size_t asize = ARRAY_SIZE(clk_desc_array);
2138         size_t i;
2139         int ret;
2140
2141         cprman = devm_kzalloc(dev,
2142                               struct_size(cprman, onecell.hws, asize),
2143                               GFP_KERNEL);
2144         if (!cprman)
2145                 return -ENOMEM;
2146
2147         spin_lock_init(&cprman->regs_lock);
2148         cprman->dev = dev;
2149         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2150         cprman->regs = devm_ioremap_resource(dev, res);
2151         if (IS_ERR(cprman->regs))
2152                 return PTR_ERR(cprman->regs);
2153
2154         memcpy(cprman->real_parent_names, cprman_parent_names,
2155                sizeof(cprman_parent_names));
2156         of_clk_parent_fill(dev->of_node, cprman->real_parent_names,
2157                            ARRAY_SIZE(cprman_parent_names));
2158
2159         /*
2160          * Make sure the external oscillator has been registered.
2161          *
2162          * The other (DSI) clocks are not present on older device
2163          * trees, which we still need to support for backwards
2164          * compatibility.
2165          */
2166         if (!cprman->real_parent_names[0])
2167                 return -ENODEV;
2168
2169         platform_set_drvdata(pdev, cprman);
2170
2171         cprman->onecell.num = asize;
2172         hws = cprman->onecell.hws;
2173
2174         for (i = 0; i < asize; i++) {
2175                 desc = &clk_desc_array[i];
2176                 if (desc->clk_register && desc->data)
2177                         hws[i] = desc->clk_register(cprman, desc->data);
2178         }
2179
2180         ret = bcm2835_mark_sdc_parent_critical(hws[BCM2835_CLOCK_SDRAM]->clk);
2181         if (ret)
2182                 return ret;
2183
2184         return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
2185                                       &cprman->onecell);
2186 }
2187
2188 static const struct of_device_id bcm2835_clk_of_match[] = {
2189         { .compatible = "brcm,bcm2835-cprman", },
2190         {}
2191 };
2192 MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
2193
2194 static struct platform_driver bcm2835_clk_driver = {
2195         .driver = {
2196                 .name = "bcm2835-clk",
2197                 .of_match_table = bcm2835_clk_of_match,
2198         },
2199         .probe          = bcm2835_clk_probe,
2200 };
2201
2202 builtin_platform_driver(bcm2835_clk_driver);
2203
2204 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
2205 MODULE_DESCRIPTION("BCM2835 clock driver");
2206 MODULE_LICENSE("GPL v2");