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