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