1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2010,2015 Broadcom
4 * Copyright (C) 2012 Stephen Warren
8 * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
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
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
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>
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>
41 #define CM_PASSWORD 0x5a000000
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)
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
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
148 #define CM_OSCCOUNT 0x100
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)
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)
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)
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)
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)
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
201 #define CM_PLLB 0x170
202 # define CM_PLLB_HOLDARM BIT(1)
203 # define CM_PLLB_LOADARM BIT(0)
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
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
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)
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)
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)
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
257 #define A2W_PLL_CHANNEL_DISABLE BIT(8)
258 #define A2W_PLL_DIV_BITS 8
259 #define A2W_PLL_DIV_SHIFT 0
261 #define A2W_PLLA_DSI0 0x1300
262 #define A2W_PLLA_CORE 0x1400
263 #define A2W_PLLA_PER 0x1500
264 #define A2W_PLLA_CCP2 0x1600
266 #define A2W_PLLC_CORE2 0x1320
267 #define A2W_PLLC_CORE1 0x1420
268 #define A2W_PLLC_PER 0x1520
269 #define A2W_PLLC_CORE0 0x1620
271 #define A2W_PLLD_DSI0 0x1340
272 #define A2W_PLLD_CORE 0x1440
273 #define A2W_PLLD_PER 0x1540
274 #define A2W_PLLD_DSI1 0x1640
276 #define A2W_PLLH_AUX 0x1360
277 #define A2W_PLLH_RCAL 0x1460
278 #define A2W_PLLH_PIX 0x1560
279 #define A2W_PLLH_STS 0x1660
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
288 #define A2W_PLLB_ARM 0x13e0
289 #define A2W_PLLB_SP0 0x14e0
290 #define A2W_PLLB_SP1 0x15e0
291 #define A2W_PLLB_SP2 0x16e0
293 #define LOCK_TIMEOUT_NS 100000000
294 #define BCM2835_MAX_FB_RATE 1750000000u
296 #define SOC_BCM2835 BIT(0)
297 #define SOC_BCM2711 BIT(1)
298 #define SOC_ALL (SOC_BCM2835 | SOC_BCM2711)
300 #define VCMSG_ID_CORE_CLOCK 4
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.
307 static const char *const cprman_parent_names[] = {
317 struct bcm2835_cprman {
320 struct rpi_firmware *fw;
321 spinlock_t regs_lock; /* spinlock for all clocks */
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.
329 const char *real_parent_names[ARRAY_SIZE(cprman_parent_names)];
332 struct clk_hw_onecell_data onecell;
335 struct cprman_plat_data {
339 static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
341 writel(CM_PASSWORD | val, cprman->regs + reg);
344 static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
346 return readl(cprman->regs + reg);
349 /* Does a cycle of measuring a clock through the TCNT clock, which may
350 * source from many other clocks in the system.
352 static unsigned long bcm2835_measure_tcnt_mux(struct bcm2835_cprman *cprman,
355 u32 osccount = 19200; /* 1ms */
359 spin_lock(&cprman->regs_lock);
361 cprman_write(cprman, CM_TCNTCTL, CM_KILL);
363 cprman_write(cprman, CM_TCNTCTL,
364 (tcnt_mux & CM_SRC_MASK) |
365 (tcnt_mux >> CM_SRC_BITS) << CM_TCNT_SRC1_SHIFT);
367 cprman_write(cprman, CM_OSCCOUNT, osccount);
369 /* do a kind delay at the start */
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");
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");
394 count = cprman_read(cprman, CM_TCNTCNT);
396 cprman_write(cprman, CM_TCNTCTL, 0);
399 spin_unlock(&cprman->regs_lock);
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)
408 struct debugfs_regset32 *regset;
410 regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL);
415 regset->nregs = nregs;
416 regset->base = cprman->regs + base;
418 debugfs_create_regset32("regdump", S_IRUGO, dentry, regset);
421 struct bcm2835_pll_data {
427 u32 reference_enable_mask;
428 /* Bit in CM_LOCK to indicate when the PLL has locked. */
432 const struct bcm2835_pll_ana_bits *ana;
434 unsigned long min_rate;
435 unsigned long max_rate;
437 * Highest rate for the VCO before we have to use the
440 unsigned long max_fb_rate;
443 struct bcm2835_pll_ana_bits {
453 static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
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),
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),
470 .fb_prediv_mask = BIT(11),
473 struct bcm2835_pll_divider_data {
475 const char *source_pll;
486 struct bcm2835_clock_data {
489 const char *const *parents;
492 /* Bitmap encoding which parents accept rate change propagation. */
493 unsigned int set_rate_parent;
498 /* Number of integer bits in the divider */
500 /* Number of fractional bits in the divider */
514 struct bcm2835_gate_data {
523 struct bcm2835_cprman *cprman;
524 const struct bcm2835_pll_data *data;
527 static int bcm2835_pll_is_on(struct clk_hw *hw)
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;
533 return cprman_read(cprman, data->a2w_ctrl_reg) &
534 A2W_PLL_CTRL_PRST_DISABLE;
537 static u32 bcm2835_pll_get_prediv_mask(struct bcm2835_cprman *cprman,
538 const struct bcm2835_pll_data *data)
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.
545 if (cprman->soc & SOC_BCM2711)
548 return data->ana->fb_prediv_mask;
551 static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
552 unsigned long parent_rate,
553 u32 *ndiv, u32 *fdiv)
557 div = (u64)rate << A2W_PLL_FRAC_BITS;
558 do_div(div, parent_rate);
560 *ndiv = div >> A2W_PLL_FRAC_BITS;
561 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
564 static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
565 u32 ndiv, u32 fdiv, u32 pdiv)
572 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
574 return rate >> A2W_PLL_FRAC_BITS;
577 static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
578 unsigned long *parent_rate)
580 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
581 const struct bcm2835_pll_data *data = pll->data;
584 rate = clamp(rate, data->min_rate, data->max_rate);
586 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
588 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
591 static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
592 unsigned long parent_rate)
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;
601 if (parent_rate == 0)
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);
615 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
618 static void bcm2835_pll_off(struct clk_hw *hw)
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;
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) |
629 spin_unlock(&cprman->regs_lock);
632 static int bcm2835_pll_on(struct clk_hw *hw)
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;
639 cprman_write(cprman, data->a2w_ctrl_reg,
640 cprman_read(cprman, data->a2w_ctrl_reg) &
641 ~A2W_PLL_CTRL_PWRDN);
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);
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));
663 cprman_write(cprman, data->a2w_ctrl_reg,
664 cprman_read(cprman, data->a2w_ctrl_reg) |
665 A2W_PLL_CTRL_PRST_DISABLE);
671 bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
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.
683 for (i = 3; i >= 0; i--)
684 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
687 static int bcm2835_pll_set_rate(struct clk_hw *hw,
688 unsigned long rate, unsigned long parent_rate)
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;
699 if (rate > data->max_fb_rate) {
700 use_fb_prediv = true;
703 use_fb_prediv = false;
706 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
708 for (i = 3; i >= 0; i--)
709 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
711 was_using_prediv = ana[1] & prediv_mask;
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;
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;
727 do_ana_setup_first = true;
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);
737 if (do_ana_setup_first)
738 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
740 /* Set the PLL multiplier from the oscillator. */
741 cprman_write(cprman, data->frac_reg, fdiv);
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);
750 if (!do_ana_setup_first)
751 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
756 static void bcm2835_pll_debug_init(struct clk_hw *hw,
757 struct dentry *dentry)
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;
764 regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
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;
783 bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry);
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,
796 struct bcm2835_pll_divider {
797 struct clk_divider div;
798 struct bcm2835_cprman *cprman;
799 const struct bcm2835_pll_divider_data *data;
802 static struct bcm2835_pll_divider *
803 bcm2835_pll_divider_from_hw(struct clk_hw *hw)
805 return container_of(hw, struct bcm2835_pll_divider, div.hw);
808 static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
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;
814 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
817 static int bcm2835_pll_divider_determine_rate(struct clk_hw *hw,
818 struct clk_rate_request *req)
820 return clk_divider_ops.determine_rate(hw, req);
823 static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
824 unsigned long parent_rate)
826 return clk_divider_ops.recalc_rate(hw, parent_rate);
829 static void bcm2835_pll_divider_off(struct clk_hw *hw)
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;
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);
845 static int bcm2835_pll_divider_on(struct clk_hw *hw)
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;
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);
856 cprman_write(cprman, data->cm_reg,
857 cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
858 spin_unlock(&cprman->regs_lock);
863 static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
865 unsigned long parent_rate)
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;
872 div = DIV_ROUND_UP_ULL(parent_rate, rate);
874 div = min(div, max_div);
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);
886 static void bcm2835_pll_divider_debug_init(struct clk_hw *hw,
887 struct dentry *dentry)
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;
894 regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
899 regs[0].offset = data->cm_reg;
900 regs[1].name = "a2w";
901 regs[1].offset = data->a2w_reg;
903 bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry);
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,
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).
922 struct bcm2835_clock {
924 struct bcm2835_cprman *cprman;
925 const struct bcm2835_clock_data *data;
928 static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
930 return container_of(hw, struct bcm2835_clock, hw);
933 static int bcm2835_clock_is_on(struct clk_hw *hw)
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;
939 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
942 static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
944 unsigned long parent_rate)
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;
952 u32 div, mindiv, maxdiv;
954 rem = do_div(temp, rate);
956 div &= ~unused_frac_mask;
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;
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);
972 /* apply the clamping limits */
973 div = max_t(u32, div, mindiv);
974 div = min_t(u32, div, maxdiv);
979 static unsigned long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
980 unsigned long parent_rate,
983 const struct bcm2835_clock_data *data = clock->data;
986 if (data->int_bits == 0 && data->frac_bits == 0)
990 * The divisor is a 12.12 fixed point field, but only some of
991 * the bits are populated in any given clock.
993 div >>= CM_DIV_FRAC_BITS - data->frac_bits;
994 div &= (1 << (data->int_bits + data->frac_bits)) - 1;
999 temp = (u64)parent_rate << data->frac_bits;
1006 static unsigned long bcm2835_round_rate(unsigned long rate)
1008 unsigned long scaler;
1009 unsigned long limit;
1011 limit = rate / 100000;
1014 while (scaler < limit)
1018 * If increasing a clock by less than 0.1% changes it
1019 * from ..999.. to ..000.., round up.
1021 if ((rate + scaler - 1) / scaler % 1000 == 0)
1022 rate = roundup(rate, scaler);
1027 static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
1028 unsigned long parent_rate)
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;
1036 if (data->int_bits == 0 && data->frac_bits == 0)
1039 div = cprman_read(cprman, data->div_reg);
1041 rate = bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
1044 rate = bcm2835_round_rate(rate);
1049 static unsigned long bcm2835_clock_get_rate_vpu(struct clk_hw *hw,
1050 unsigned long parent_rate)
1052 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1053 struct bcm2835_cprman *cprman = clock->cprman;
1061 packet.id = VCMSG_ID_CORE_CLOCK;
1064 if (!rpi_firmware_property(cprman->fw,
1065 RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
1066 &packet, sizeof(packet)))
1070 return bcm2835_clock_get_rate(hw, parent_rate);
1073 static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
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);
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));
1089 static void bcm2835_clock_off(struct clk_hw *hw)
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;
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);
1100 /* BUSY will remain high until the divider completes its cycle. */
1101 bcm2835_clock_wait_busy(clock);
1104 static int bcm2835_clock_on(struct clk_hw *hw)
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;
1110 spin_lock(&cprman->regs_lock);
1111 cprman_write(cprman, data->ctl_reg,
1112 cprman_read(cprman, data->ctl_reg) |
1115 spin_unlock(&cprman->regs_lock);
1117 /* Debug code to measure the clock once it's turned on to see
1118 * if it's ticking at the rate we expect.
1120 if (data->tcnt_mux && false) {
1121 dev_info(cprman->dev,
1122 "clk %s: rate %ld, measure %ld\n",
1124 clk_hw_get_rate(hw),
1125 bcm2835_measure_tcnt_mux(cprman, data->tcnt_mux));
1131 static int bcm2835_clock_set_rate_and_parent(struct clk_hw *hw,
1133 unsigned long parent_rate,
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);
1142 spin_lock(&cprman->regs_lock);
1144 ctl = cprman_read(cprman, data->ctl_reg);
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.
1151 if (ctl & CM_ENABLE) {
1152 cprman_write(cprman, data->ctl_reg, ctl & ~CM_ENABLE);
1153 bcm2835_clock_wait_busy(clock);
1156 if (parent != 0xff) {
1157 ctl &= ~(CM_SRC_MASK << CM_SRC_SHIFT);
1158 ctl |= parent << CM_SRC_SHIFT;
1162 ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
1163 cprman_write(cprman, data->ctl_reg, ctl);
1165 cprman_write(cprman, data->div_reg, div);
1167 spin_unlock(&cprman->regs_lock);
1172 static int bcm2835_clock_set_rate(struct clk_hw *hw,
1173 unsigned long rate, unsigned long parent_rate)
1175 return bcm2835_clock_set_rate_and_parent(hw, rate, parent_rate, 0xff);
1179 bcm2835_clk_is_pllc(struct clk_hw *hw)
1184 return strncmp(clk_hw_get_name(hw), "pllc", 4) == 0;
1187 static unsigned long bcm2835_clock_choose_div_and_prate(struct clk_hw *hw,
1191 unsigned long *prate,
1192 unsigned long *avgrate)
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;
1201 parent = clk_hw_get_parent_by_index(hw, parent_idx);
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);
1207 *avgrate = bcm2835_clock_rate_from_divisor(clock, *prate, *div);
1209 if (data->low_jitter && (*div & CM_DIV_FRAC_MASK)) {
1210 unsigned long high, low;
1211 u32 int_div = *div & ~CM_DIV_FRAC_MASK;
1213 high = bcm2835_clock_rate_from_divisor(clock, *prate,
1215 int_div += CM_DIV_FRAC_MASK + 1;
1216 low = bcm2835_clock_rate_from_divisor(clock, *prate,
1220 * Return a value which is the maximum deviation
1221 * below the ideal rate, for use as a metric.
1223 return *avgrate - max(*avgrate - low, high - *avgrate);
1228 if (data->frac_bits)
1229 dev_warn(cprman->dev,
1230 "frac bits are not used when propagating rate change");
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;
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;
1240 tmp_rate = clk_hw_round_rate(parent, rate * curdiv);
1242 if (curdiv == mindiv ||
1243 (tmp_rate > best_rate && tmp_rate <= rate))
1244 best_rate = tmp_rate;
1246 if (best_rate == rate)
1250 *div = curdiv << CM_DIV_FRAC_BITS;
1251 *prate = curdiv * best_rate;
1252 *avgrate = best_rate;
1257 static int bcm2835_clock_determine_rate(struct clk_hw *hw,
1258 struct clk_rate_request *req)
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;
1268 current_parent_is_pllc = bcm2835_clk_is_pllc(clk_hw_get_parent(hw));
1271 * Select parent clock that results in the closest but lower rate
1273 for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
1274 parent = clk_hw_get_parent_by_index(hw, i);
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.
1285 if (bcm2835_clk_is_pllc(parent) && !current_parent_is_pllc)
1288 rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate,
1291 if (abs(req->rate - rate) < abs(req->rate - best_rate)) {
1292 best_parent = parent;
1295 best_avgrate = avgrate;
1302 req->best_parent_hw = best_parent;
1303 req->best_parent_rate = best_prate;
1305 req->rate = best_avgrate;
1310 static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
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;
1317 cprman_write(cprman, data->ctl_reg, src);
1321 static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
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);
1328 return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
1331 static const struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = {
1342 static void bcm2835_clock_debug_init(struct clk_hw *hw,
1343 struct dentry *dentry)
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;
1349 bcm2835_debugfs_regset(cprman, data->ctl_reg,
1350 bcm2835_debugfs_clock_reg32,
1351 ARRAY_SIZE(bcm2835_debugfs_clock_reg32),
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,
1368 static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
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.
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,
1387 static bool bcm2835_clk_is_claimed(const char *name);
1389 static struct clk_hw *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1392 const struct bcm2835_pll_data *pll_data = data;
1393 struct bcm2835_pll *pll;
1394 struct clk_init_data init;
1397 memset(&init, 0, sizeof(init));
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;
1406 if (!bcm2835_clk_is_claimed(pll_data->name))
1407 init.flags |= CLK_IS_CRITICAL;
1409 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1413 pll->cprman = cprman;
1414 pll->data = pll_data;
1415 pll->hw.init = &init;
1417 ret = devm_clk_hw_register(cprman->dev, &pll->hw);
1425 static struct clk_hw *
1426 bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
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;
1435 if (divider_data->fixed_divider != 1) {
1436 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1437 "%s_prediv", divider_data->name);
1441 divider_name = divider_data->name;
1444 memset(&init, 0, sizeof(init));
1446 init.parent_names = ÷r_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;
1452 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
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;
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;
1471 divider->cprman = cprman;
1472 divider->data = divider_data;
1474 ret = devm_clk_hw_register(cprman->dev, ÷r->div.hw);
1476 return ERR_PTR(ret);
1479 * PLLH's channels have a fixed divide by 10 afterwards, which
1480 * is what our consumers are actually using.
1482 if (divider_data->fixed_divider != 1) {
1483 return clk_hw_register_fixed_factor(cprman->dev,
1486 CLK_SET_RATE_PARENT,
1488 divider_data->fixed_divider);
1491 return ÷r->div.hw;
1494 static struct clk_hw *bcm2835_register_clock(struct bcm2835_cprman *cprman,
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];
1505 * Replace our strings referencing parent clocks with the
1506 * actual clock-output-name of the parent.
1508 for (i = 0; i < clock_data->num_mux_parents; i++) {
1509 parents[i] = clock_data->parents[i];
1511 ret = match_string(cprman_parent_names,
1512 ARRAY_SIZE(cprman_parent_names),
1515 parents[i] = cprman->real_parent_names[ret];
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;
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.
1530 if ((cprman_read(cprman, clock_data->ctl_reg) & CM_ENABLE) == 0)
1531 init.flags &= ~CLK_IS_CRITICAL;
1534 * Pass the CLK_SET_RATE_PARENT flag if we are allowed to propagate
1535 * rate changes on at least of the parents.
1537 if (clock_data->set_rate_parent)
1538 init.flags |= CLK_SET_RATE_PARENT;
1540 if (clock_data->is_vpu_clock) {
1541 init.ops = &bcm2835_vpu_clock_clk_ops;
1543 init.ops = &bcm2835_clock_clk_ops;
1545 /* If the clock wasn't actually enabled at boot, it's not
1548 if (!(cprman_read(cprman, clock_data->ctl_reg) & CM_ENABLE))
1549 init.flags &= ~CLK_IS_CRITICAL;
1552 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1556 clock->cprman = cprman;
1557 clock->data = clock_data;
1558 clock->hw.init = &init;
1560 ret = devm_clk_hw_register(cprman->dev, &clock->hw);
1562 return ERR_PTR(ret);
1566 static struct clk_hw *bcm2835_register_gate(struct bcm2835_cprman *cprman,
1569 const struct bcm2835_gate_data *gate_data = data;
1571 return clk_hw_register_gate(cprman->dev, gate_data->name,
1573 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1574 cprman->regs + gate_data->ctl_reg,
1575 CM_GATE_BIT, 0, &cprman->regs_lock);
1578 struct bcm2835_clk_desc {
1579 struct clk_hw *(*clk_register)(struct bcm2835_cprman *cprman,
1581 unsigned int supported;
1585 /* assignment helper macros for different clock types */
1586 #define _REGISTER(f, s, ...) { .clk_register = f, \
1588 .data = __VA_ARGS__ }
1589 #define REGISTER_PLL(s, ...) _REGISTER(&bcm2835_register_pll, \
1591 &(struct bcm2835_pll_data) \
1593 #define REGISTER_PLL_DIV(s, ...) _REGISTER(&bcm2835_register_pll_divider, \
1595 &(struct bcm2835_pll_divider_data) \
1597 #define REGISTER_CLK(s, ...) _REGISTER(&bcm2835_register_clock, \
1599 &(struct bcm2835_clock_data) \
1601 #define REGISTER_GATE(s, ...) _REGISTER(&bcm2835_register_gate, \
1603 &(struct bcm2835_gate_data) \
1606 /* parent mux arrays plus helper macros */
1608 /* main oscillator parent mux */
1609 static const char *const bcm2835_clock_osc_parents[] = {
1616 #define REGISTER_OSC_CLK(s, ...) REGISTER_CLK( \
1618 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), \
1619 .parents = bcm2835_clock_osc_parents, \
1622 /* main peripherial parent mux */
1623 static const char *const bcm2835_clock_per_parents[] = {
1634 #define REGISTER_PER_CLK(s, ...) REGISTER_CLK( \
1636 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), \
1637 .parents = bcm2835_clock_per_parents, \
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
1645 * Prevent other sources from being selected by replacing their names in
1646 * the list of potential parents with dummy entries (entry index is
1649 static const char *const bcm2835_pcm_per_parents[] = {
1660 #define REGISTER_PCM_CLK(s, ...) REGISTER_CLK( \
1662 .num_mux_parents = ARRAY_SIZE(bcm2835_pcm_per_parents), \
1663 .parents = bcm2835_pcm_per_parents, \
1666 /* main vpu parent mux */
1667 static const char *const bcm2835_clock_vpu_parents[] = {
1680 #define REGISTER_VPU_CLK(s, ...) REGISTER_CLK( \
1682 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), \
1683 .parents = bcm2835_clock_vpu_parents, \
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.
1691 static const char *const bcm2835_clock_dsi0_parents[] = {
1704 static const char *const bcm2835_clock_dsi1_parents[] = {
1717 #define REGISTER_DSI0_CLK(s, ...) REGISTER_CLK( \
1719 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi0_parents), \
1720 .parents = bcm2835_clock_dsi0_parents, \
1723 #define REGISTER_DSI1_CLK(s, ...) REGISTER_CLK( \
1725 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi1_parents), \
1726 .parents = bcm2835_clock_dsi1_parents, \
1730 * the real definition of all the pll, pll_dividers and clocks
1731 * these make use of the above REGISTER_* macros
1733 static const struct bcm2835_clk_desc clk_desc_array[] = {
1734 /* the PLL + PLL dividers */
1737 * PLLA is the auxiliary PLL, used to drive the CCP2
1738 * (Compact Camera Port 2) transmitter clock.
1740 * It is in the PX LDO power domain, which is on when the
1741 * AUDIO domain is on.
1743 [BCM2835_PLLA] = REGISTER_PLL(
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,
1753 .ana = &bcm2835_ana_default,
1755 .min_rate = 600000000u,
1756 .max_rate = 2400000000u,
1757 .max_fb_rate = BCM2835_MAX_FB_RATE),
1758 [BCM2835_PLLA_CORE] = REGISTER_PLL_DIV(
1760 .name = "plla_core",
1761 .source_pll = "plla",
1763 .a2w_reg = A2W_PLLA_CORE,
1764 .load_mask = CM_PLLA_LOADCORE,
1765 .hold_mask = CM_PLLA_HOLDCORE,
1767 .flags = CLK_SET_RATE_PARENT),
1770 * PLLA_PER is used for gpu clocks. Controlled by firmware, see
1771 * clk-raspberrypi.c.
1774 [BCM2835_PLLA_DSI0] = REGISTER_PLL_DIV(
1776 .name = "plla_dsi0",
1777 .source_pll = "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(
1785 .name = "plla_ccp2",
1786 .source_pll = "plla",
1788 .a2w_reg = A2W_PLLA_CCP2,
1789 .load_mask = CM_PLLA_LOADCCP2,
1790 .hold_mask = CM_PLLA_HOLDCCP2,
1792 .flags = CLK_SET_RATE_PARENT),
1794 /* PLLB is used for the ARM's clock. */
1795 [BCM2835_PLLB] = REGISTER_PLL(
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,
1805 .ana = &bcm2835_ana_default,
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(
1814 .source_pll = "pllb",
1816 .a2w_reg = A2W_PLLB_ARM,
1817 .load_mask = CM_PLLB_LOADARM,
1818 .hold_mask = CM_PLLB_HOLDARM,
1820 .flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE),
1823 * PLLC is the core PLL, used to drive the core VPU clock.
1825 * It is in the PX LDO power domain, which is on when the
1826 * AUDIO domain is on.
1828 [BCM2835_PLLC] = REGISTER_PLL(
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,
1838 .ana = &bcm2835_ana_default,
1840 .min_rate = 600000000u,
1841 .max_rate = 3000000000u,
1842 .max_fb_rate = BCM2835_MAX_FB_RATE),
1843 [BCM2835_PLLC_CORE0] = REGISTER_PLL_DIV(
1845 .name = "pllc_core0",
1846 .source_pll = "pllc",
1848 .a2w_reg = A2W_PLLC_CORE0,
1849 .load_mask = CM_PLLC_LOADCORE0,
1850 .hold_mask = CM_PLLC_HOLDCORE0,
1852 .flags = CLK_SET_RATE_PARENT),
1853 [BCM2835_PLLC_CORE1] = REGISTER_PLL_DIV(
1855 .name = "pllc_core1",
1856 .source_pll = "pllc",
1858 .a2w_reg = A2W_PLLC_CORE1,
1859 .load_mask = CM_PLLC_LOADCORE1,
1860 .hold_mask = CM_PLLC_HOLDCORE1,
1862 .flags = CLK_SET_RATE_PARENT),
1863 [BCM2835_PLLC_CORE2] = REGISTER_PLL_DIV(
1865 .name = "pllc_core2",
1866 .source_pll = "pllc",
1868 .a2w_reg = A2W_PLLC_CORE2,
1869 .load_mask = CM_PLLC_LOADCORE2,
1870 .hold_mask = CM_PLLC_HOLDCORE2,
1872 .flags = CLK_SET_RATE_PARENT),
1873 [BCM2835_PLLC_PER] = REGISTER_PLL_DIV(
1876 .source_pll = "pllc",
1878 .a2w_reg = A2W_PLLC_PER,
1879 .load_mask = CM_PLLC_LOADPER,
1880 .hold_mask = CM_PLLC_HOLDPER,
1882 .flags = CLK_IS_CRITICAL | CLK_SET_RATE_PARENT),
1885 * PLLD is the display PLL, used to drive DSI display panels.
1887 * It is in the PX LDO power domain, which is on when the
1888 * AUDIO domain is on.
1890 [BCM2835_PLLD] = REGISTER_PLL(
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,
1900 .ana = &bcm2835_ana_default,
1902 .min_rate = 600000000u,
1903 .max_rate = 2400000000u,
1904 .max_fb_rate = BCM2835_MAX_FB_RATE),
1905 [BCM2835_PLLD_CORE] = REGISTER_PLL_DIV(
1907 .name = "plld_core",
1908 .source_pll = "plld",
1910 .a2w_reg = A2W_PLLD_CORE,
1911 .load_mask = CM_PLLD_LOADCORE,
1912 .hold_mask = CM_PLLD_HOLDCORE,
1914 .flags = CLK_SET_RATE_PARENT),
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
1920 [BCM2835_PLLD_PER] = REGISTER_PLL_DIV(
1923 .source_pll = "plld",
1925 .a2w_reg = A2W_PLLD_PER,
1926 .load_mask = CM_PLLD_LOADPER,
1927 .hold_mask = CM_PLLD_HOLDPER,
1929 .flags = CLK_IS_CRITICAL | CLK_SET_RATE_PARENT),
1930 [BCM2835_PLLD_DSI0] = REGISTER_PLL_DIV(
1932 .name = "plld_dsi0",
1933 .source_pll = "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(
1941 .name = "plld_dsi1",
1942 .source_pll = "plld",
1944 .a2w_reg = A2W_PLLD_DSI1,
1945 .load_mask = CM_PLLD_LOADDSI1,
1946 .hold_mask = CM_PLLD_HOLDDSI1,
1947 .fixed_divider = 1),
1950 * PLLH is used to supply the pixel clock or the AUX clock for the
1953 * It is in the HDMI power domain.
1955 [BCM2835_PLLH] = REGISTER_PLL(
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,
1965 .ana = &bcm2835_ana_pllh,
1967 .min_rate = 600000000u,
1968 .max_rate = 3000000000u,
1969 .max_fb_rate = BCM2835_MAX_FB_RATE),
1970 [BCM2835_PLLH_RCAL] = REGISTER_PLL_DIV(
1972 .name = "pllh_rcal",
1973 .source_pll = "pllh",
1975 .a2w_reg = A2W_PLLH_RCAL,
1976 .load_mask = CM_PLLH_LOADRCAL,
1978 .fixed_divider = 10,
1979 .flags = CLK_SET_RATE_PARENT),
1980 [BCM2835_PLLH_AUX] = REGISTER_PLL_DIV(
1983 .source_pll = "pllh",
1985 .a2w_reg = A2W_PLLH_AUX,
1986 .load_mask = CM_PLLH_LOADAUX,
1989 .flags = CLK_SET_RATE_PARENT),
1990 [BCM2835_PLLH_PIX] = REGISTER_PLL_DIV(
1993 .source_pll = "pllh",
1995 .a2w_reg = A2W_PLLH_PIX,
1996 .load_mask = CM_PLLH_LOADPIX,
1998 .fixed_divider = 10,
1999 .flags = CLK_SET_RATE_PARENT),
2003 /* clocks with oscillator parent mux */
2005 /* One Time Programmable Memory clock. Maximum 10Mhz. */
2006 [BCM2835_CLOCK_OTP] = REGISTER_OSC_CLK(
2009 .ctl_reg = CM_OTPCTL,
2010 .div_reg = CM_OTPDIV,
2015 * Used for a 1Mhz clock for the system clocksource, and also used
2016 * bythe watchdog timer and the camera pulse generator.
2018 [BCM2835_CLOCK_TIMER] = REGISTER_OSC_CLK(
2021 .ctl_reg = CM_TIMERCTL,
2022 .div_reg = CM_TIMERDIV,
2026 * Clock for the temperature sensor.
2027 * Generally run at 2Mhz, max 5Mhz.
2029 [BCM2835_CLOCK_TSENS] = REGISTER_OSC_CLK(
2032 .ctl_reg = CM_TSENSCTL,
2033 .div_reg = CM_TSENSDIV,
2036 [BCM2835_CLOCK_TEC] = REGISTER_OSC_CLK(
2039 .ctl_reg = CM_TECCTL,
2040 .div_reg = CM_TECDIV,
2044 /* clocks with vpu parent mux */
2045 [BCM2835_CLOCK_H264] = REGISTER_VPU_CLK(
2048 .ctl_reg = CM_H264CTL,
2049 .div_reg = CM_H264DIV,
2053 [BCM2835_CLOCK_ISP] = REGISTER_VPU_CLK(
2056 .ctl_reg = CM_ISPCTL,
2057 .div_reg = CM_ISPDIV,
2063 * Secondary SDRAM clock. Used for low-voltage modes when the PLL
2064 * in the SDRAM controller can't be used.
2066 [BCM2835_CLOCK_SDRAM] = REGISTER_VPU_CLK(
2069 .ctl_reg = CM_SDCCTL,
2070 .div_reg = CM_SDCDIV,
2076 * CLOCK_V3D is used for v3d clock. Controlled by firmware, see
2077 * clk-raspberrypi.c.
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.
2086 [BCM2835_CLOCK_VPU] = REGISTER_VPU_CLK(
2089 .ctl_reg = CM_VPUCTL,
2090 .div_reg = CM_VPUDIV,
2093 .flags = CLK_IS_CRITICAL,
2094 .is_vpu_clock = true,
2097 /* clocks with per parent mux */
2098 [BCM2835_CLOCK_AVEO] = REGISTER_PER_CLK(
2101 .ctl_reg = CM_AVEOCTL,
2102 .div_reg = CM_AVEODIV,
2106 [BCM2835_CLOCK_CAM0] = REGISTER_PER_CLK(
2109 .ctl_reg = CM_CAM0CTL,
2110 .div_reg = CM_CAM0DIV,
2114 [BCM2835_CLOCK_CAM1] = REGISTER_PER_CLK(
2117 .ctl_reg = CM_CAM1CTL,
2118 .div_reg = CM_CAM1DIV,
2122 [BCM2835_CLOCK_DFT] = REGISTER_PER_CLK(
2125 .ctl_reg = CM_DFTCTL,
2126 .div_reg = CM_DFTDIV,
2129 [BCM2835_CLOCK_DPI] = REGISTER_PER_CLK(
2132 .ctl_reg = CM_DPICTL,
2133 .div_reg = CM_DPIDIV,
2138 /* Arasan EMMC clock */
2139 [BCM2835_CLOCK_EMMC] = REGISTER_PER_CLK(
2142 .ctl_reg = CM_EMMCCTL,
2143 .div_reg = CM_EMMCDIV,
2148 /* EMMC2 clock (only available for BCM2711) */
2149 [BCM2711_CLOCK_EMMC2] = REGISTER_PER_CLK(
2152 .ctl_reg = CM_EMMC2CTL,
2153 .div_reg = CM_EMMC2DIV,
2158 /* General purpose (GPIO) clocks */
2159 [BCM2835_CLOCK_GP0] = REGISTER_PER_CLK(
2162 .ctl_reg = CM_GP0CTL,
2163 .div_reg = CM_GP0DIV,
2166 .is_mash_clock = true,
2168 [BCM2835_CLOCK_GP1] = REGISTER_PER_CLK(
2171 .ctl_reg = CM_GP1CTL,
2172 .div_reg = CM_GP1DIV,
2175 .flags = CLK_IS_CRITICAL,
2176 .is_mash_clock = true,
2178 [BCM2835_CLOCK_GP2] = REGISTER_PER_CLK(
2181 .ctl_reg = CM_GP2CTL,
2182 .div_reg = CM_GP2DIV,
2185 .flags = CLK_IS_CRITICAL),
2187 /* HDMI state machine */
2188 [BCM2835_CLOCK_HSM] = REGISTER_PER_CLK(
2191 .ctl_reg = CM_HSMCTL,
2192 .div_reg = CM_HSMDIV,
2196 [BCM2835_CLOCK_PCM] = REGISTER_PCM_CLK(
2199 .ctl_reg = CM_PCMCTL,
2200 .div_reg = CM_PCMDIV,
2203 .is_mash_clock = true,
2206 [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK(
2209 .ctl_reg = CM_PWMCTL,
2210 .div_reg = CM_PWMDIV,
2213 .is_mash_clock = true,
2215 [BCM2835_CLOCK_SLIM] = REGISTER_PER_CLK(
2218 .ctl_reg = CM_SLIMCTL,
2219 .div_reg = CM_SLIMDIV,
2222 .is_mash_clock = true,
2224 [BCM2835_CLOCK_SMI] = REGISTER_PER_CLK(
2227 .ctl_reg = CM_SMICTL,
2228 .div_reg = CM_SMIDIV,
2232 [BCM2835_CLOCK_UART] = REGISTER_PER_CLK(
2235 .ctl_reg = CM_UARTCTL,
2236 .div_reg = CM_UARTDIV,
2243 [BCM2835_CLOCK_DSI0E] = REGISTER_PER_CLK(
2246 .ctl_reg = CM_DSI0ECTL,
2247 .div_reg = CM_DSI0EDIV,
2251 [BCM2835_CLOCK_DSI1E] = REGISTER_PER_CLK(
2254 .ctl_reg = CM_DSI1ECTL,
2255 .div_reg = CM_DSI1EDIV,
2259 [BCM2835_CLOCK_DSI0P] = REGISTER_DSI0_CLK(
2262 .ctl_reg = CM_DSI0PCTL,
2263 .div_reg = CM_DSI0PDIV,
2267 [BCM2835_CLOCK_DSI1P] = REGISTER_DSI1_CLK(
2270 .ctl_reg = CM_DSI1PCTL,
2271 .div_reg = CM_DSI1PDIV,
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.
2284 [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE(
2286 .name = "peri_image",
2288 .ctl_reg = CM_PERIICTL),
2291 static bool bcm2835_clk_claimed[ARRAY_SIZE(clk_desc_array)];
2294 * Permanently take a reference on the parent of the SDRAM clock.
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.
2302 static int bcm2835_mark_sdc_parent_critical(struct clk *sdc)
2304 struct clk *parent = clk_get_parent(sdc);
2307 return PTR_ERR(parent);
2309 return clk_prepare_enable(parent);
2312 static bool bcm2835_clk_is_claimed(const char *name)
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];
2327 static int bcm2835_clk_probe(struct platform_device *pdev)
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;
2340 pdata = of_device_get_match_data(&pdev->dev);
2344 cprman = devm_kzalloc(dev,
2345 struct_size(cprman, onecell.hws, asize),
2350 spin_lock_init(&cprman->regs_lock);
2352 cprman->regs = devm_platform_ioremap_resource(pdev, 0);
2353 if (IS_ERR(cprman->regs))
2354 return PTR_ERR(cprman->regs);
2356 fw_node = of_parse_phandle(dev->of_node, "firmware", 0);
2358 struct rpi_firmware *fw = rpi_firmware_get(fw_node);
2360 return -EPROBE_DEFER;
2364 memset(bcm2835_clk_claimed, 0, sizeof(bcm2835_clk_claimed));
2366 !of_property_read_u32_index(pdev->dev.of_node, "claim-clocks",
2369 bcm2835_clk_claimed[clk_id]= true;
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));
2377 * Make sure the external oscillator has been registered.
2379 * The other (DSI) clocks are not present on older device
2380 * trees, which we still need to support for backwards
2383 if (!cprman->real_parent_names[0])
2386 platform_set_drvdata(pdev, cprman);
2388 cprman->onecell.num = asize;
2389 cprman->soc = pdata->soc;
2390 hws = cprman->onecell.hws;
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);
2400 ret = bcm2835_mark_sdc_parent_critical(hws[BCM2835_CLOCK_SDRAM]->clk);
2404 ret = of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
2409 /* note that we have registered all the clocks */
2410 dev_dbg(dev, "registered %zd clocks\n", asize);
2415 static const struct cprman_plat_data cprman_bcm2835_plat_data = {
2419 static const struct cprman_plat_data cprman_bcm2711_plat_data = {
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 },
2428 MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
2430 static struct platform_driver bcm2835_clk_driver = {
2432 .name = "bcm2835-clk",
2433 .of_match_table = bcm2835_clk_of_match,
2435 .probe = bcm2835_clk_probe,
2438 static int __init __bcm2835_clk_driver_init(void)
2440 return platform_driver_register(&bcm2835_clk_driver);
2443 subsys_initcall(__bcm2835_clk_driver_init);
2445 postcore_initcall(__bcm2835_clk_driver_init);
2448 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
2449 MODULE_DESCRIPTION("BCM2835 clock driver");
2450 MODULE_LICENSE("GPL");