2 * Copyright (C) 2010,2015 Broadcom
3 * Copyright (C) 2012 Stephen Warren
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
18 * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
20 * The clock tree on the 2835 has several levels. There's a root
21 * oscillator running at 19.2Mhz. After the oscillator there are 5
22 * PLLs, roughly divided as "camera", "ARM", "core", "DSI displays",
23 * and "HDMI displays". Those 5 PLLs each can divide their output to
24 * produce up to 4 channels. Finally, there is the level of clocks to
25 * be consumed by other hardware components (like "H264" or "HDMI
26 * state machine"), which divide off of some subset of the PLL
29 * All of the clocks in the tree are exposed in the DT, because the DT
30 * may want to make assignments of the final layer of clocks to the
31 * PLL channels, and some components of the hardware will actually
32 * skip layers of the tree (for example, the pixel clock comes
33 * directly from the PLLH PIX channel without using a CM_*CTL clock
37 #include <linux/clk-provider.h>
38 #include <linux/clkdev.h>
39 #include <linux/clk/bcm2835.h>
40 #include <linux/debugfs.h>
41 #include <linux/module.h>
43 #include <linux/platform_device.h>
44 #include <linux/slab.h>
45 #include <dt-bindings/clock/bcm2835.h>
47 #define CM_PASSWORD 0x5a000000
49 #define CM_GNRICCTL 0x000
50 #define CM_GNRICDIV 0x004
51 # define CM_DIV_FRAC_BITS 12
52 # define CM_DIV_FRAC_MASK GENMASK(CM_DIV_FRAC_BITS - 1, 0)
54 #define CM_VPUCTL 0x008
55 #define CM_VPUDIV 0x00c
56 #define CM_SYSCTL 0x010
57 #define CM_SYSDIV 0x014
58 #define CM_PERIACTL 0x018
59 #define CM_PERIADIV 0x01c
60 #define CM_PERIICTL 0x020
61 #define CM_PERIIDIV 0x024
62 #define CM_H264CTL 0x028
63 #define CM_H264DIV 0x02c
64 #define CM_ISPCTL 0x030
65 #define CM_ISPDIV 0x034
66 #define CM_V3DCTL 0x038
67 #define CM_V3DDIV 0x03c
68 #define CM_CAM0CTL 0x040
69 #define CM_CAM0DIV 0x044
70 #define CM_CAM1CTL 0x048
71 #define CM_CAM1DIV 0x04c
72 #define CM_CCP2CTL 0x050
73 #define CM_CCP2DIV 0x054
74 #define CM_DSI0ECTL 0x058
75 #define CM_DSI0EDIV 0x05c
76 #define CM_DSI0PCTL 0x060
77 #define CM_DSI0PDIV 0x064
78 #define CM_DPICTL 0x068
79 #define CM_DPIDIV 0x06c
80 #define CM_GP0CTL 0x070
81 #define CM_GP0DIV 0x074
82 #define CM_GP1CTL 0x078
83 #define CM_GP1DIV 0x07c
84 #define CM_GP2CTL 0x080
85 #define CM_GP2DIV 0x084
86 #define CM_HSMCTL 0x088
87 #define CM_HSMDIV 0x08c
88 #define CM_OTPCTL 0x090
89 #define CM_OTPDIV 0x094
90 #define CM_PCMCTL 0x098
91 #define CM_PCMDIV 0x09c
92 #define CM_PWMCTL 0x0a0
93 #define CM_PWMDIV 0x0a4
94 #define CM_SLIMCTL 0x0a8
95 #define CM_SLIMDIV 0x0ac
96 #define CM_SMICTL 0x0b0
97 #define CM_SMIDIV 0x0b4
98 /* no definition for 0x0b8 and 0x0bc */
99 #define CM_TCNTCTL 0x0c0
100 #define CM_TCNTDIV 0x0c4
101 #define CM_TECCTL 0x0c8
102 #define CM_TECDIV 0x0cc
103 #define CM_TD0CTL 0x0d0
104 #define CM_TD0DIV 0x0d4
105 #define CM_TD1CTL 0x0d8
106 #define CM_TD1DIV 0x0dc
107 #define CM_TSENSCTL 0x0e0
108 #define CM_TSENSDIV 0x0e4
109 #define CM_TIMERCTL 0x0e8
110 #define CM_TIMERDIV 0x0ec
111 #define CM_UARTCTL 0x0f0
112 #define CM_UARTDIV 0x0f4
113 #define CM_VECCTL 0x0f8
114 #define CM_VECDIV 0x0fc
115 #define CM_PULSECTL 0x190
116 #define CM_PULSEDIV 0x194
117 #define CM_SDCCTL 0x1a8
118 #define CM_SDCDIV 0x1ac
119 #define CM_ARMCTL 0x1b0
120 #define CM_EMMCCTL 0x1c0
121 #define CM_EMMCDIV 0x1c4
123 /* General bits for the CM_*CTL regs */
124 # define CM_ENABLE BIT(4)
125 # define CM_KILL BIT(5)
126 # define CM_GATE_BIT 6
127 # define CM_GATE BIT(CM_GATE_BIT)
128 # define CM_BUSY BIT(7)
129 # define CM_BUSYD BIT(8)
130 # define CM_FRAC BIT(9)
131 # define CM_SRC_SHIFT 0
132 # define CM_SRC_BITS 4
133 # define CM_SRC_MASK 0xf
134 # define CM_SRC_GND 0
135 # define CM_SRC_OSC 1
136 # define CM_SRC_TESTDEBUG0 2
137 # define CM_SRC_TESTDEBUG1 3
138 # define CM_SRC_PLLA_CORE 4
139 # define CM_SRC_PLLA_PER 4
140 # define CM_SRC_PLLC_CORE0 5
141 # define CM_SRC_PLLC_PER 5
142 # define CM_SRC_PLLC_CORE1 8
143 # define CM_SRC_PLLD_CORE 6
144 # define CM_SRC_PLLD_PER 6
145 # define CM_SRC_PLLH_AUX 7
146 # define CM_SRC_PLLC_CORE1 8
147 # define CM_SRC_PLLC_CORE2 9
149 #define CM_OSCCOUNT 0x100
151 #define CM_PLLA 0x104
152 # define CM_PLL_ANARST BIT(8)
153 # define CM_PLLA_HOLDPER BIT(7)
154 # define CM_PLLA_LOADPER BIT(6)
155 # define CM_PLLA_HOLDCORE BIT(5)
156 # define CM_PLLA_LOADCORE BIT(4)
157 # define CM_PLLA_HOLDCCP2 BIT(3)
158 # define CM_PLLA_LOADCCP2 BIT(2)
159 # define CM_PLLA_HOLDDSI0 BIT(1)
160 # define CM_PLLA_LOADDSI0 BIT(0)
162 #define CM_PLLC 0x108
163 # define CM_PLLC_HOLDPER BIT(7)
164 # define CM_PLLC_LOADPER BIT(6)
165 # define CM_PLLC_HOLDCORE2 BIT(5)
166 # define CM_PLLC_LOADCORE2 BIT(4)
167 # define CM_PLLC_HOLDCORE1 BIT(3)
168 # define CM_PLLC_LOADCORE1 BIT(2)
169 # define CM_PLLC_HOLDCORE0 BIT(1)
170 # define CM_PLLC_LOADCORE0 BIT(0)
172 #define CM_PLLD 0x10c
173 # define CM_PLLD_HOLDPER BIT(7)
174 # define CM_PLLD_LOADPER BIT(6)
175 # define CM_PLLD_HOLDCORE BIT(5)
176 # define CM_PLLD_LOADCORE BIT(4)
177 # define CM_PLLD_HOLDDSI1 BIT(3)
178 # define CM_PLLD_LOADDSI1 BIT(2)
179 # define CM_PLLD_HOLDDSI0 BIT(1)
180 # define CM_PLLD_LOADDSI0 BIT(0)
182 #define CM_PLLH 0x110
183 # define CM_PLLH_LOADRCAL BIT(2)
184 # define CM_PLLH_LOADAUX BIT(1)
185 # define CM_PLLH_LOADPIX BIT(0)
187 #define CM_LOCK 0x114
188 # define CM_LOCK_FLOCKH BIT(12)
189 # define CM_LOCK_FLOCKD BIT(11)
190 # define CM_LOCK_FLOCKC BIT(10)
191 # define CM_LOCK_FLOCKB BIT(9)
192 # define CM_LOCK_FLOCKA BIT(8)
194 #define CM_EVENT 0x118
195 #define CM_DSI1ECTL 0x158
196 #define CM_DSI1EDIV 0x15c
197 #define CM_DSI1PCTL 0x160
198 #define CM_DSI1PDIV 0x164
199 #define CM_DFTCTL 0x168
200 #define CM_DFTDIV 0x16c
202 #define CM_PLLB 0x170
203 # define CM_PLLB_HOLDARM BIT(1)
204 # define CM_PLLB_LOADARM BIT(0)
206 #define A2W_PLLA_CTRL 0x1100
207 #define A2W_PLLC_CTRL 0x1120
208 #define A2W_PLLD_CTRL 0x1140
209 #define A2W_PLLH_CTRL 0x1160
210 #define A2W_PLLB_CTRL 0x11e0
211 # define A2W_PLL_CTRL_PRST_DISABLE BIT(17)
212 # define A2W_PLL_CTRL_PWRDN BIT(16)
213 # define A2W_PLL_CTRL_PDIV_MASK 0x000007000
214 # define A2W_PLL_CTRL_PDIV_SHIFT 12
215 # define A2W_PLL_CTRL_NDIV_MASK 0x0000003ff
216 # define A2W_PLL_CTRL_NDIV_SHIFT 0
218 #define A2W_PLLA_ANA0 0x1010
219 #define A2W_PLLC_ANA0 0x1030
220 #define A2W_PLLD_ANA0 0x1050
221 #define A2W_PLLH_ANA0 0x1070
222 #define A2W_PLLB_ANA0 0x10f0
224 #define A2W_PLL_KA_SHIFT 7
225 #define A2W_PLL_KA_MASK GENMASK(9, 7)
226 #define A2W_PLL_KI_SHIFT 19
227 #define A2W_PLL_KI_MASK GENMASK(21, 19)
228 #define A2W_PLL_KP_SHIFT 15
229 #define A2W_PLL_KP_MASK GENMASK(18, 15)
231 #define A2W_PLLH_KA_SHIFT 19
232 #define A2W_PLLH_KA_MASK GENMASK(21, 19)
233 #define A2W_PLLH_KI_LOW_SHIFT 22
234 #define A2W_PLLH_KI_LOW_MASK GENMASK(23, 22)
235 #define A2W_PLLH_KI_HIGH_SHIFT 0
236 #define A2W_PLLH_KI_HIGH_MASK GENMASK(0, 0)
237 #define A2W_PLLH_KP_SHIFT 1
238 #define A2W_PLLH_KP_MASK GENMASK(4, 1)
240 #define A2W_XOSC_CTRL 0x1190
241 # define A2W_XOSC_CTRL_PLLB_ENABLE BIT(7)
242 # define A2W_XOSC_CTRL_PLLA_ENABLE BIT(6)
243 # define A2W_XOSC_CTRL_PLLD_ENABLE BIT(5)
244 # define A2W_XOSC_CTRL_DDR_ENABLE BIT(4)
245 # define A2W_XOSC_CTRL_CPR1_ENABLE BIT(3)
246 # define A2W_XOSC_CTRL_USB_ENABLE BIT(2)
247 # define A2W_XOSC_CTRL_HDMI_ENABLE BIT(1)
248 # define A2W_XOSC_CTRL_PLLC_ENABLE BIT(0)
250 #define A2W_PLLA_FRAC 0x1200
251 #define A2W_PLLC_FRAC 0x1220
252 #define A2W_PLLD_FRAC 0x1240
253 #define A2W_PLLH_FRAC 0x1260
254 #define A2W_PLLB_FRAC 0x12e0
255 # define A2W_PLL_FRAC_MASK ((1 << A2W_PLL_FRAC_BITS) - 1)
256 # define A2W_PLL_FRAC_BITS 20
258 #define A2W_PLL_CHANNEL_DISABLE BIT(8)
259 #define A2W_PLL_DIV_BITS 8
260 #define A2W_PLL_DIV_SHIFT 0
262 #define A2W_PLLA_DSI0 0x1300
263 #define A2W_PLLA_CORE 0x1400
264 #define A2W_PLLA_PER 0x1500
265 #define A2W_PLLA_CCP2 0x1600
267 #define A2W_PLLC_CORE2 0x1320
268 #define A2W_PLLC_CORE1 0x1420
269 #define A2W_PLLC_PER 0x1520
270 #define A2W_PLLC_CORE0 0x1620
272 #define A2W_PLLD_DSI0 0x1340
273 #define A2W_PLLD_CORE 0x1440
274 #define A2W_PLLD_PER 0x1540
275 #define A2W_PLLD_DSI1 0x1640
277 #define A2W_PLLH_AUX 0x1360
278 #define A2W_PLLH_RCAL 0x1460
279 #define A2W_PLLH_PIX 0x1560
280 #define A2W_PLLH_STS 0x1660
282 #define A2W_PLLH_CTRLR 0x1960
283 #define A2W_PLLH_FRACR 0x1a60
284 #define A2W_PLLH_AUXR 0x1b60
285 #define A2W_PLLH_RCALR 0x1c60
286 #define A2W_PLLH_PIXR 0x1d60
287 #define A2W_PLLH_STSR 0x1e60
289 #define A2W_PLLB_ARM 0x13e0
290 #define A2W_PLLB_SP0 0x14e0
291 #define A2W_PLLB_SP1 0x15e0
292 #define A2W_PLLB_SP2 0x16e0
294 #define LOCK_TIMEOUT_NS 100000000
295 #define BCM2835_MAX_FB_RATE 1750000000u
297 struct bcm2835_cprman
{
300 spinlock_t regs_lock
; /* spinlock for all clocks */
301 const char *osc_name
;
303 struct clk_onecell_data onecell
;
307 static inline void cprman_write(struct bcm2835_cprman
*cprman
, u32 reg
, u32 val
)
309 writel(CM_PASSWORD
| val
, cprman
->regs
+ reg
);
312 static inline u32
cprman_read(struct bcm2835_cprman
*cprman
, u32 reg
)
314 return readl(cprman
->regs
+ reg
);
317 static int bcm2835_debugfs_regset(struct bcm2835_cprman
*cprman
, u32 base
,
318 struct debugfs_reg32
*regs
, size_t nregs
,
319 struct dentry
*dentry
)
321 struct dentry
*regdump
;
322 struct debugfs_regset32
*regset
;
324 regset
= devm_kzalloc(cprman
->dev
, sizeof(*regset
), GFP_KERNEL
);
329 regset
->nregs
= nregs
;
330 regset
->base
= cprman
->regs
+ base
;
332 regdump
= debugfs_create_regset32("regdump", S_IRUGO
, dentry
,
335 return regdump
? 0 : -ENOMEM
;
339 * These are fixed clocks. They're probably not all root clocks and it may
340 * be possible to turn them on and off but until this is mapped out better
341 * it's the only way they can be used.
343 void __init
bcm2835_init_clocks(void)
348 clk
= clk_register_fixed_rate(NULL
, "apb_pclk", NULL
, 0, 126000000);
350 pr_err("apb_pclk not registered\n");
352 clk
= clk_register_fixed_rate(NULL
, "uart0_pclk", NULL
, 0, 3000000);
354 pr_err("uart0_pclk not registered\n");
355 ret
= clk_register_clkdev(clk
, NULL
, "20201000.uart");
357 pr_err("uart0_pclk alias not registered\n");
359 clk
= clk_register_fixed_rate(NULL
, "uart1_pclk", NULL
, 0, 125000000);
361 pr_err("uart1_pclk not registered\n");
362 ret
= clk_register_clkdev(clk
, NULL
, "20215000.uart");
364 pr_err("uart1_pclk alias not registered\n");
367 struct bcm2835_pll_data
{
373 u32 reference_enable_mask
;
374 /* Bit in CM_LOCK to indicate when the PLL has locked. */
377 const struct bcm2835_pll_ana_bits
*ana
;
379 unsigned long min_rate
;
380 unsigned long max_rate
;
382 * Highest rate for the VCO before we have to use the
385 unsigned long max_fb_rate
;
388 struct bcm2835_pll_ana_bits
{
398 static const struct bcm2835_pll_ana_bits bcm2835_ana_default
= {
401 .mask1
= ~(A2W_PLL_KI_MASK
| A2W_PLL_KP_MASK
),
402 .set1
= (2 << A2W_PLL_KI_SHIFT
) | (8 << A2W_PLL_KP_SHIFT
),
403 .mask3
= ~A2W_PLL_KA_MASK
,
404 .set3
= (2 << A2W_PLL_KA_SHIFT
),
405 .fb_prediv_mask
= BIT(14),
408 static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh
= {
409 .mask0
= ~(A2W_PLLH_KA_MASK
| A2W_PLLH_KI_LOW_MASK
),
410 .set0
= (2 << A2W_PLLH_KA_SHIFT
) | (2 << A2W_PLLH_KI_LOW_SHIFT
),
411 .mask1
= ~(A2W_PLLH_KI_HIGH_MASK
| A2W_PLLH_KP_MASK
),
412 .set1
= (6 << A2W_PLLH_KP_SHIFT
),
415 .fb_prediv_mask
= BIT(11),
419 * PLLA is the auxiliary PLL, used to drive the CCP2 (Compact Camera
420 * Port 2) transmitter clock.
422 * It is in the PX LDO power domain, which is on when the AUDIO domain
425 static const struct bcm2835_pll_data bcm2835_plla_data
= {
427 .cm_ctrl_reg
= CM_PLLA
,
428 .a2w_ctrl_reg
= A2W_PLLA_CTRL
,
429 .frac_reg
= A2W_PLLA_FRAC
,
430 .ana_reg_base
= A2W_PLLA_ANA0
,
431 .reference_enable_mask
= A2W_XOSC_CTRL_PLLA_ENABLE
,
432 .lock_mask
= CM_LOCK_FLOCKA
,
434 .ana
= &bcm2835_ana_default
,
436 .min_rate
= 600000000u,
437 .max_rate
= 2400000000u,
438 .max_fb_rate
= BCM2835_MAX_FB_RATE
,
441 /* PLLB is used for the ARM's clock. */
442 static const struct bcm2835_pll_data bcm2835_pllb_data
= {
444 .cm_ctrl_reg
= CM_PLLB
,
445 .a2w_ctrl_reg
= A2W_PLLB_CTRL
,
446 .frac_reg
= A2W_PLLB_FRAC
,
447 .ana_reg_base
= A2W_PLLB_ANA0
,
448 .reference_enable_mask
= A2W_XOSC_CTRL_PLLB_ENABLE
,
449 .lock_mask
= CM_LOCK_FLOCKB
,
451 .ana
= &bcm2835_ana_default
,
453 .min_rate
= 600000000u,
454 .max_rate
= 3000000000u,
455 .max_fb_rate
= BCM2835_MAX_FB_RATE
,
459 * PLLC is the core PLL, used to drive the core VPU clock.
461 * It is in the PX LDO power domain, which is on when the AUDIO domain
464 static const struct bcm2835_pll_data bcm2835_pllc_data
= {
466 .cm_ctrl_reg
= CM_PLLC
,
467 .a2w_ctrl_reg
= A2W_PLLC_CTRL
,
468 .frac_reg
= A2W_PLLC_FRAC
,
469 .ana_reg_base
= A2W_PLLC_ANA0
,
470 .reference_enable_mask
= A2W_XOSC_CTRL_PLLC_ENABLE
,
471 .lock_mask
= CM_LOCK_FLOCKC
,
473 .ana
= &bcm2835_ana_default
,
475 .min_rate
= 600000000u,
476 .max_rate
= 3000000000u,
477 .max_fb_rate
= BCM2835_MAX_FB_RATE
,
481 * PLLD is the display PLL, used to drive DSI display panels.
483 * It is in the PX LDO power domain, which is on when the AUDIO domain
486 static const struct bcm2835_pll_data bcm2835_plld_data
= {
488 .cm_ctrl_reg
= CM_PLLD
,
489 .a2w_ctrl_reg
= A2W_PLLD_CTRL
,
490 .frac_reg
= A2W_PLLD_FRAC
,
491 .ana_reg_base
= A2W_PLLD_ANA0
,
492 .reference_enable_mask
= A2W_XOSC_CTRL_DDR_ENABLE
,
493 .lock_mask
= CM_LOCK_FLOCKD
,
495 .ana
= &bcm2835_ana_default
,
497 .min_rate
= 600000000u,
498 .max_rate
= 2400000000u,
499 .max_fb_rate
= BCM2835_MAX_FB_RATE
,
503 * PLLH is used to supply the pixel clock or the AUX clock for the TV
506 * It is in the HDMI power domain.
508 static const struct bcm2835_pll_data bcm2835_pllh_data
= {
510 .cm_ctrl_reg
= CM_PLLH
,
511 .a2w_ctrl_reg
= A2W_PLLH_CTRL
,
512 .frac_reg
= A2W_PLLH_FRAC
,
513 .ana_reg_base
= A2W_PLLH_ANA0
,
514 .reference_enable_mask
= A2W_XOSC_CTRL_PLLC_ENABLE
,
515 .lock_mask
= CM_LOCK_FLOCKH
,
517 .ana
= &bcm2835_ana_pllh
,
519 .min_rate
= 600000000u,
520 .max_rate
= 3000000000u,
521 .max_fb_rate
= BCM2835_MAX_FB_RATE
,
524 struct bcm2835_pll_divider_data
{
526 const struct bcm2835_pll_data
*source_pll
;
535 static const struct bcm2835_pll_divider_data bcm2835_plla_core_data
= {
537 .source_pll
= &bcm2835_plla_data
,
539 .a2w_reg
= A2W_PLLA_CORE
,
540 .load_mask
= CM_PLLA_LOADCORE
,
541 .hold_mask
= CM_PLLA_HOLDCORE
,
545 static const struct bcm2835_pll_divider_data bcm2835_plla_per_data
= {
547 .source_pll
= &bcm2835_plla_data
,
549 .a2w_reg
= A2W_PLLA_PER
,
550 .load_mask
= CM_PLLA_LOADPER
,
551 .hold_mask
= CM_PLLA_HOLDPER
,
555 static const struct bcm2835_pll_divider_data bcm2835_pllb_arm_data
= {
557 .source_pll
= &bcm2835_pllb_data
,
559 .a2w_reg
= A2W_PLLB_ARM
,
560 .load_mask
= CM_PLLB_LOADARM
,
561 .hold_mask
= CM_PLLB_HOLDARM
,
565 static const struct bcm2835_pll_divider_data bcm2835_pllc_core0_data
= {
566 .name
= "pllc_core0",
567 .source_pll
= &bcm2835_pllc_data
,
569 .a2w_reg
= A2W_PLLC_CORE0
,
570 .load_mask
= CM_PLLC_LOADCORE0
,
571 .hold_mask
= CM_PLLC_HOLDCORE0
,
575 static const struct bcm2835_pll_divider_data bcm2835_pllc_core1_data
= {
576 .name
= "pllc_core1", .source_pll
= &bcm2835_pllc_data
,
577 .cm_reg
= CM_PLLC
, A2W_PLLC_CORE1
,
578 .load_mask
= CM_PLLC_LOADCORE1
,
579 .hold_mask
= CM_PLLC_HOLDCORE1
,
583 static const struct bcm2835_pll_divider_data bcm2835_pllc_core2_data
= {
584 .name
= "pllc_core2",
585 .source_pll
= &bcm2835_pllc_data
,
587 .a2w_reg
= A2W_PLLC_CORE2
,
588 .load_mask
= CM_PLLC_LOADCORE2
,
589 .hold_mask
= CM_PLLC_HOLDCORE2
,
593 static const struct bcm2835_pll_divider_data bcm2835_pllc_per_data
= {
595 .source_pll
= &bcm2835_pllc_data
,
597 .a2w_reg
= A2W_PLLC_PER
,
598 .load_mask
= CM_PLLC_LOADPER
,
599 .hold_mask
= CM_PLLC_HOLDPER
,
603 static const struct bcm2835_pll_divider_data bcm2835_plld_core_data
= {
605 .source_pll
= &bcm2835_plld_data
,
607 .a2w_reg
= A2W_PLLD_CORE
,
608 .load_mask
= CM_PLLD_LOADCORE
,
609 .hold_mask
= CM_PLLD_HOLDCORE
,
613 static const struct bcm2835_pll_divider_data bcm2835_plld_per_data
= {
615 .source_pll
= &bcm2835_plld_data
,
617 .a2w_reg
= A2W_PLLD_PER
,
618 .load_mask
= CM_PLLD_LOADPER
,
619 .hold_mask
= CM_PLLD_HOLDPER
,
623 static const struct bcm2835_pll_divider_data bcm2835_pllh_rcal_data
= {
625 .source_pll
= &bcm2835_pllh_data
,
627 .a2w_reg
= A2W_PLLH_RCAL
,
628 .load_mask
= CM_PLLH_LOADRCAL
,
633 static const struct bcm2835_pll_divider_data bcm2835_pllh_aux_data
= {
635 .source_pll
= &bcm2835_pllh_data
,
637 .a2w_reg
= A2W_PLLH_AUX
,
638 .load_mask
= CM_PLLH_LOADAUX
,
643 static const struct bcm2835_pll_divider_data bcm2835_pllh_pix_data
= {
645 .source_pll
= &bcm2835_pllh_data
,
647 .a2w_reg
= A2W_PLLH_PIX
,
648 .load_mask
= CM_PLLH_LOADPIX
,
653 struct bcm2835_clock_data
{
656 const char *const *parents
;
662 /* Number of integer bits in the divider */
664 /* Number of fractional bits in the divider */
671 static const char *const bcm2835_clock_per_parents
[] = {
682 static const char *const bcm2835_clock_vpu_parents
[] = {
695 static const char *const bcm2835_clock_osc_parents
[] = {
703 * Used for a 1Mhz clock for the system clocksource, and also used by
704 * the watchdog timer and the camera pulse generator.
706 static const struct bcm2835_clock_data bcm2835_clock_timer_data
= {
708 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_osc_parents
),
709 .parents
= bcm2835_clock_osc_parents
,
710 .ctl_reg
= CM_TIMERCTL
,
711 .div_reg
= CM_TIMERDIV
,
716 /* One Time Programmable Memory clock. Maximum 10Mhz. */
717 static const struct bcm2835_clock_data bcm2835_clock_otp_data
= {
719 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_osc_parents
),
720 .parents
= bcm2835_clock_osc_parents
,
721 .ctl_reg
= CM_OTPCTL
,
722 .div_reg
= CM_OTPDIV
,
728 * VPU clock. This doesn't have an enable bit, since it drives the
729 * bus for everything else, and is special so it doesn't need to be
730 * gated for rate changes. It is also known as "clk_audio" in various
731 * hardware documentation.
733 static const struct bcm2835_clock_data bcm2835_clock_vpu_data
= {
735 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_vpu_parents
),
736 .parents
= bcm2835_clock_vpu_parents
,
737 .ctl_reg
= CM_VPUCTL
,
738 .div_reg
= CM_VPUDIV
,
741 .is_vpu_clock
= true,
744 static const struct bcm2835_clock_data bcm2835_clock_v3d_data
= {
746 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_vpu_parents
),
747 .parents
= bcm2835_clock_vpu_parents
,
748 .ctl_reg
= CM_V3DCTL
,
749 .div_reg
= CM_V3DDIV
,
754 static const struct bcm2835_clock_data bcm2835_clock_isp_data
= {
756 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_vpu_parents
),
757 .parents
= bcm2835_clock_vpu_parents
,
758 .ctl_reg
= CM_ISPCTL
,
759 .div_reg
= CM_ISPDIV
,
764 static const struct bcm2835_clock_data bcm2835_clock_h264_data
= {
766 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_vpu_parents
),
767 .parents
= bcm2835_clock_vpu_parents
,
768 .ctl_reg
= CM_H264CTL
,
769 .div_reg
= CM_H264DIV
,
774 /* TV encoder clock. Only operating frequency is 108Mhz. */
775 static const struct bcm2835_clock_data bcm2835_clock_vec_data
= {
777 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_per_parents
),
778 .parents
= bcm2835_clock_per_parents
,
779 .ctl_reg
= CM_VECCTL
,
780 .div_reg
= CM_VECDIV
,
785 static const struct bcm2835_clock_data bcm2835_clock_uart_data
= {
787 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_per_parents
),
788 .parents
= bcm2835_clock_per_parents
,
789 .ctl_reg
= CM_UARTCTL
,
790 .div_reg
= CM_UARTDIV
,
795 /* HDMI state machine */
796 static const struct bcm2835_clock_data bcm2835_clock_hsm_data
= {
798 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_per_parents
),
799 .parents
= bcm2835_clock_per_parents
,
800 .ctl_reg
= CM_HSMCTL
,
801 .div_reg
= CM_HSMDIV
,
807 * Secondary SDRAM clock. Used for low-voltage modes when the PLL in
808 * the SDRAM controller can't be used.
810 static const struct bcm2835_clock_data bcm2835_clock_sdram_data
= {
812 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_vpu_parents
),
813 .parents
= bcm2835_clock_vpu_parents
,
814 .ctl_reg
= CM_SDCCTL
,
815 .div_reg
= CM_SDCDIV
,
820 /* Clock for the temperature sensor. Generally run at 2Mhz, max 5Mhz. */
821 static const struct bcm2835_clock_data bcm2835_clock_tsens_data
= {
823 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_osc_parents
),
824 .parents
= bcm2835_clock_osc_parents
,
825 .ctl_reg
= CM_TSENSCTL
,
826 .div_reg
= CM_TSENSDIV
,
831 /* Arasan EMMC clock */
832 static const struct bcm2835_clock_data bcm2835_clock_emmc_data
= {
834 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_per_parents
),
835 .parents
= bcm2835_clock_per_parents
,
836 .ctl_reg
= CM_EMMCCTL
,
837 .div_reg
= CM_EMMCDIV
,
842 static const struct bcm2835_clock_data bcm2835_clock_pwm_data
= {
844 .num_mux_parents
= ARRAY_SIZE(bcm2835_clock_per_parents
),
845 .parents
= bcm2835_clock_per_parents
,
846 .ctl_reg
= CM_PWMCTL
,
847 .div_reg
= CM_PWMDIV
,
850 .is_mash_clock
= true,
853 struct bcm2835_gate_data
{
861 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
862 * you have the debug bit set in the power manager, which we
863 * don't bother exposing) are individual gates off of the
864 * non-stop vpu clock.
866 static const struct bcm2835_gate_data bcm2835_clock_peri_image_data
= {
867 .name
= "peri_image",
869 .ctl_reg
= CM_PERIICTL
,
874 struct bcm2835_cprman
*cprman
;
875 const struct bcm2835_pll_data
*data
;
878 static int bcm2835_pll_is_on(struct clk_hw
*hw
)
880 struct bcm2835_pll
*pll
= container_of(hw
, struct bcm2835_pll
, hw
);
881 struct bcm2835_cprman
*cprman
= pll
->cprman
;
882 const struct bcm2835_pll_data
*data
= pll
->data
;
884 return cprman_read(cprman
, data
->a2w_ctrl_reg
) &
885 A2W_PLL_CTRL_PRST_DISABLE
;
888 static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate
,
889 unsigned long parent_rate
,
890 u32
*ndiv
, u32
*fdiv
)
894 div
= (u64
)rate
<< A2W_PLL_FRAC_BITS
;
895 do_div(div
, parent_rate
);
897 *ndiv
= div
>> A2W_PLL_FRAC_BITS
;
898 *fdiv
= div
& ((1 << A2W_PLL_FRAC_BITS
) - 1);
901 static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate
,
902 u32 ndiv
, u32 fdiv
, u32 pdiv
)
909 rate
= (u64
)parent_rate
* ((ndiv
<< A2W_PLL_FRAC_BITS
) + fdiv
);
911 return rate
>> A2W_PLL_FRAC_BITS
;
914 static long bcm2835_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
915 unsigned long *parent_rate
)
919 bcm2835_pll_choose_ndiv_and_fdiv(rate
, *parent_rate
, &ndiv
, &fdiv
);
921 return bcm2835_pll_rate_from_divisors(*parent_rate
, ndiv
, fdiv
, 1);
924 static unsigned long bcm2835_pll_get_rate(struct clk_hw
*hw
,
925 unsigned long parent_rate
)
927 struct bcm2835_pll
*pll
= container_of(hw
, struct bcm2835_pll
, hw
);
928 struct bcm2835_cprman
*cprman
= pll
->cprman
;
929 const struct bcm2835_pll_data
*data
= pll
->data
;
930 u32 a2wctrl
= cprman_read(cprman
, data
->a2w_ctrl_reg
);
931 u32 ndiv
, pdiv
, fdiv
;
934 if (parent_rate
== 0)
937 fdiv
= cprman_read(cprman
, data
->frac_reg
) & A2W_PLL_FRAC_MASK
;
938 ndiv
= (a2wctrl
& A2W_PLL_CTRL_NDIV_MASK
) >> A2W_PLL_CTRL_NDIV_SHIFT
;
939 pdiv
= (a2wctrl
& A2W_PLL_CTRL_PDIV_MASK
) >> A2W_PLL_CTRL_PDIV_SHIFT
;
940 using_prediv
= cprman_read(cprman
, data
->ana_reg_base
+ 4) &
941 data
->ana
->fb_prediv_mask
;
946 return bcm2835_pll_rate_from_divisors(parent_rate
, ndiv
, fdiv
, pdiv
);
949 static void bcm2835_pll_off(struct clk_hw
*hw
)
951 struct bcm2835_pll
*pll
= container_of(hw
, struct bcm2835_pll
, hw
);
952 struct bcm2835_cprman
*cprman
= pll
->cprman
;
953 const struct bcm2835_pll_data
*data
= pll
->data
;
955 spin_lock(&cprman
->regs_lock
);
956 cprman_write(cprman
, data
->cm_ctrl_reg
,
957 cprman_read(cprman
, data
->cm_ctrl_reg
) |
959 cprman_write(cprman
, data
->a2w_ctrl_reg
,
960 cprman_read(cprman
, data
->a2w_ctrl_reg
) |
962 spin_unlock(&cprman
->regs_lock
);
965 static int bcm2835_pll_on(struct clk_hw
*hw
)
967 struct bcm2835_pll
*pll
= container_of(hw
, struct bcm2835_pll
, hw
);
968 struct bcm2835_cprman
*cprman
= pll
->cprman
;
969 const struct bcm2835_pll_data
*data
= pll
->data
;
972 /* Take the PLL out of reset. */
973 cprman_write(cprman
, data
->cm_ctrl_reg
,
974 cprman_read(cprman
, data
->cm_ctrl_reg
) & ~CM_PLL_ANARST
);
976 /* Wait for the PLL to lock. */
977 timeout
= ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS
);
978 while (!(cprman_read(cprman
, CM_LOCK
) & data
->lock_mask
)) {
979 if (ktime_after(ktime_get(), timeout
)) {
980 dev_err(cprman
->dev
, "%s: couldn't lock PLL\n",
981 clk_hw_get_name(hw
));
992 bcm2835_pll_write_ana(struct bcm2835_cprman
*cprman
, u32 ana_reg_base
, u32
*ana
)
997 * ANA register setup is done as a series of writes to
998 * ANA3-ANA0, in that order. This lets us write all 4
999 * registers as a single cycle of the serdes interface (taking
1000 * 100 xosc clocks), whereas if we were to update ana0, 1, and
1001 * 3 individually through their partial-write registers, each
1002 * would be their own serdes cycle.
1004 for (i
= 3; i
>= 0; i
--)
1005 cprman_write(cprman
, ana_reg_base
+ i
* 4, ana
[i
]);
1008 static int bcm2835_pll_set_rate(struct clk_hw
*hw
,
1009 unsigned long rate
, unsigned long parent_rate
)
1011 struct bcm2835_pll
*pll
= container_of(hw
, struct bcm2835_pll
, hw
);
1012 struct bcm2835_cprman
*cprman
= pll
->cprman
;
1013 const struct bcm2835_pll_data
*data
= pll
->data
;
1014 bool was_using_prediv
, use_fb_prediv
, do_ana_setup_first
;
1015 u32 ndiv
, fdiv
, a2w_ctl
;
1019 if (rate
< data
->min_rate
|| rate
> data
->max_rate
) {
1020 dev_err(cprman
->dev
, "%s: rate out of spec: %lu vs (%lu, %lu)\n",
1021 clk_hw_get_name(hw
), rate
,
1022 data
->min_rate
, data
->max_rate
);
1026 if (rate
> data
->max_fb_rate
) {
1027 use_fb_prediv
= true;
1030 use_fb_prediv
= false;
1033 bcm2835_pll_choose_ndiv_and_fdiv(rate
, parent_rate
, &ndiv
, &fdiv
);
1035 for (i
= 3; i
>= 0; i
--)
1036 ana
[i
] = cprman_read(cprman
, data
->ana_reg_base
+ i
* 4);
1038 was_using_prediv
= ana
[1] & data
->ana
->fb_prediv_mask
;
1040 ana
[0] &= ~data
->ana
->mask0
;
1041 ana
[0] |= data
->ana
->set0
;
1042 ana
[1] &= ~data
->ana
->mask1
;
1043 ana
[1] |= data
->ana
->set1
;
1044 ana
[3] &= ~data
->ana
->mask3
;
1045 ana
[3] |= data
->ana
->set3
;
1047 if (was_using_prediv
&& !use_fb_prediv
) {
1048 ana
[1] &= ~data
->ana
->fb_prediv_mask
;
1049 do_ana_setup_first
= true;
1050 } else if (!was_using_prediv
&& use_fb_prediv
) {
1051 ana
[1] |= data
->ana
->fb_prediv_mask
;
1052 do_ana_setup_first
= false;
1054 do_ana_setup_first
= true;
1057 /* Unmask the reference clock from the oscillator. */
1058 cprman_write(cprman
, A2W_XOSC_CTRL
,
1059 cprman_read(cprman
, A2W_XOSC_CTRL
) |
1060 data
->reference_enable_mask
);
1062 if (do_ana_setup_first
)
1063 bcm2835_pll_write_ana(cprman
, data
->ana_reg_base
, ana
);
1065 /* Set the PLL multiplier from the oscillator. */
1066 cprman_write(cprman
, data
->frac_reg
, fdiv
);
1068 a2w_ctl
= cprman_read(cprman
, data
->a2w_ctrl_reg
);
1069 a2w_ctl
&= ~A2W_PLL_CTRL_NDIV_MASK
;
1070 a2w_ctl
|= ndiv
<< A2W_PLL_CTRL_NDIV_SHIFT
;
1071 a2w_ctl
&= ~A2W_PLL_CTRL_PDIV_MASK
;
1072 a2w_ctl
|= 1 << A2W_PLL_CTRL_PDIV_SHIFT
;
1073 cprman_write(cprman
, data
->a2w_ctrl_reg
, a2w_ctl
);
1075 if (!do_ana_setup_first
)
1076 bcm2835_pll_write_ana(cprman
, data
->ana_reg_base
, ana
);
1081 static int bcm2835_pll_debug_init(struct clk_hw
*hw
,
1082 struct dentry
*dentry
)
1084 struct bcm2835_pll
*pll
= container_of(hw
, struct bcm2835_pll
, hw
);
1085 struct bcm2835_cprman
*cprman
= pll
->cprman
;
1086 const struct bcm2835_pll_data
*data
= pll
->data
;
1087 struct debugfs_reg32
*regs
;
1089 regs
= devm_kzalloc(cprman
->dev
, 7 * sizeof(*regs
), GFP_KERNEL
);
1093 regs
[0].name
= "cm_ctrl";
1094 regs
[0].offset
= data
->cm_ctrl_reg
;
1095 regs
[1].name
= "a2w_ctrl";
1096 regs
[1].offset
= data
->a2w_ctrl_reg
;
1097 regs
[2].name
= "frac";
1098 regs
[2].offset
= data
->frac_reg
;
1099 regs
[3].name
= "ana0";
1100 regs
[3].offset
= data
->ana_reg_base
+ 0 * 4;
1101 regs
[4].name
= "ana1";
1102 regs
[4].offset
= data
->ana_reg_base
+ 1 * 4;
1103 regs
[5].name
= "ana2";
1104 regs
[5].offset
= data
->ana_reg_base
+ 2 * 4;
1105 regs
[6].name
= "ana3";
1106 regs
[6].offset
= data
->ana_reg_base
+ 3 * 4;
1108 return bcm2835_debugfs_regset(cprman
, 0, regs
, 7, dentry
);
1111 static const struct clk_ops bcm2835_pll_clk_ops
= {
1112 .is_prepared
= bcm2835_pll_is_on
,
1113 .prepare
= bcm2835_pll_on
,
1114 .unprepare
= bcm2835_pll_off
,
1115 .recalc_rate
= bcm2835_pll_get_rate
,
1116 .set_rate
= bcm2835_pll_set_rate
,
1117 .round_rate
= bcm2835_pll_round_rate
,
1118 .debug_init
= bcm2835_pll_debug_init
,
1121 struct bcm2835_pll_divider
{
1122 struct clk_divider div
;
1123 struct bcm2835_cprman
*cprman
;
1124 const struct bcm2835_pll_divider_data
*data
;
1127 static struct bcm2835_pll_divider
*
1128 bcm2835_pll_divider_from_hw(struct clk_hw
*hw
)
1130 return container_of(hw
, struct bcm2835_pll_divider
, div
.hw
);
1133 static int bcm2835_pll_divider_is_on(struct clk_hw
*hw
)
1135 struct bcm2835_pll_divider
*divider
= bcm2835_pll_divider_from_hw(hw
);
1136 struct bcm2835_cprman
*cprman
= divider
->cprman
;
1137 const struct bcm2835_pll_divider_data
*data
= divider
->data
;
1139 return !(cprman_read(cprman
, data
->a2w_reg
) & A2W_PLL_CHANNEL_DISABLE
);
1142 static long bcm2835_pll_divider_round_rate(struct clk_hw
*hw
,
1144 unsigned long *parent_rate
)
1146 return clk_divider_ops
.round_rate(hw
, rate
, parent_rate
);
1149 static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw
*hw
,
1150 unsigned long parent_rate
)
1152 return clk_divider_ops
.recalc_rate(hw
, parent_rate
);
1155 static void bcm2835_pll_divider_off(struct clk_hw
*hw
)
1157 struct bcm2835_pll_divider
*divider
= bcm2835_pll_divider_from_hw(hw
);
1158 struct bcm2835_cprman
*cprman
= divider
->cprman
;
1159 const struct bcm2835_pll_divider_data
*data
= divider
->data
;
1161 spin_lock(&cprman
->regs_lock
);
1162 cprman_write(cprman
, data
->cm_reg
,
1163 (cprman_read(cprman
, data
->cm_reg
) &
1164 ~data
->load_mask
) | data
->hold_mask
);
1165 cprman_write(cprman
, data
->a2w_reg
, A2W_PLL_CHANNEL_DISABLE
);
1166 spin_unlock(&cprman
->regs_lock
);
1169 static int bcm2835_pll_divider_on(struct clk_hw
*hw
)
1171 struct bcm2835_pll_divider
*divider
= bcm2835_pll_divider_from_hw(hw
);
1172 struct bcm2835_cprman
*cprman
= divider
->cprman
;
1173 const struct bcm2835_pll_divider_data
*data
= divider
->data
;
1175 spin_lock(&cprman
->regs_lock
);
1176 cprman_write(cprman
, data
->a2w_reg
,
1177 cprman_read(cprman
, data
->a2w_reg
) &
1178 ~A2W_PLL_CHANNEL_DISABLE
);
1180 cprman_write(cprman
, data
->cm_reg
,
1181 cprman_read(cprman
, data
->cm_reg
) & ~data
->hold_mask
);
1182 spin_unlock(&cprman
->regs_lock
);
1187 static int bcm2835_pll_divider_set_rate(struct clk_hw
*hw
,
1189 unsigned long parent_rate
)
1191 struct bcm2835_pll_divider
*divider
= bcm2835_pll_divider_from_hw(hw
);
1192 struct bcm2835_cprman
*cprman
= divider
->cprman
;
1193 const struct bcm2835_pll_divider_data
*data
= divider
->data
;
1194 u32 cm
, div
, max_div
= 1 << A2W_PLL_DIV_BITS
;
1196 div
= DIV_ROUND_UP_ULL(parent_rate
, rate
);
1198 div
= min(div
, max_div
);
1202 cprman_write(cprman
, data
->a2w_reg
, div
);
1203 cm
= cprman_read(cprman
, data
->cm_reg
);
1204 cprman_write(cprman
, data
->cm_reg
, cm
| data
->load_mask
);
1205 cprman_write(cprman
, data
->cm_reg
, cm
& ~data
->load_mask
);
1210 static int bcm2835_pll_divider_debug_init(struct clk_hw
*hw
,
1211 struct dentry
*dentry
)
1213 struct bcm2835_pll_divider
*divider
= bcm2835_pll_divider_from_hw(hw
);
1214 struct bcm2835_cprman
*cprman
= divider
->cprman
;
1215 const struct bcm2835_pll_divider_data
*data
= divider
->data
;
1216 struct debugfs_reg32
*regs
;
1218 regs
= devm_kzalloc(cprman
->dev
, 7 * sizeof(*regs
), GFP_KERNEL
);
1222 regs
[0].name
= "cm";
1223 regs
[0].offset
= data
->cm_reg
;
1224 regs
[1].name
= "a2w";
1225 regs
[1].offset
= data
->a2w_reg
;
1227 return bcm2835_debugfs_regset(cprman
, 0, regs
, 2, dentry
);
1230 static const struct clk_ops bcm2835_pll_divider_clk_ops
= {
1231 .is_prepared
= bcm2835_pll_divider_is_on
,
1232 .prepare
= bcm2835_pll_divider_on
,
1233 .unprepare
= bcm2835_pll_divider_off
,
1234 .recalc_rate
= bcm2835_pll_divider_get_rate
,
1235 .set_rate
= bcm2835_pll_divider_set_rate
,
1236 .round_rate
= bcm2835_pll_divider_round_rate
,
1237 .debug_init
= bcm2835_pll_divider_debug_init
,
1241 * The CM dividers do fixed-point division, so we can't use the
1242 * generic integer divider code like the PLL dividers do (and we can't
1243 * fake it by having some fixed shifts preceding it in the clock tree,
1244 * because we'd run out of bits in a 32-bit unsigned long).
1246 struct bcm2835_clock
{
1248 struct bcm2835_cprman
*cprman
;
1249 const struct bcm2835_clock_data
*data
;
1252 static struct bcm2835_clock
*bcm2835_clock_from_hw(struct clk_hw
*hw
)
1254 return container_of(hw
, struct bcm2835_clock
, hw
);
1257 static int bcm2835_clock_is_on(struct clk_hw
*hw
)
1259 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1260 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1261 const struct bcm2835_clock_data
*data
= clock
->data
;
1263 return (cprman_read(cprman
, data
->ctl_reg
) & CM_ENABLE
) != 0;
1266 static u32
bcm2835_clock_choose_div(struct clk_hw
*hw
,
1268 unsigned long parent_rate
,
1271 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1272 const struct bcm2835_clock_data
*data
= clock
->data
;
1273 u32 unused_frac_mask
=
1274 GENMASK(CM_DIV_FRAC_BITS
- data
->frac_bits
, 0) >> 1;
1275 u64 temp
= (u64
)parent_rate
<< CM_DIV_FRAC_BITS
;
1277 u32 div
, mindiv
, maxdiv
;
1279 rem
= do_div(temp
, rate
);
1282 /* Round up and mask off the unused bits */
1283 if (round_up
&& ((div
& unused_frac_mask
) != 0 || rem
!= 0))
1284 div
+= unused_frac_mask
+ 1;
1285 div
&= ~unused_frac_mask
;
1287 /* different clamping limits apply for a mash clock */
1288 if (data
->is_mash_clock
) {
1289 /* clamp to min divider of 2 */
1290 mindiv
= 2 << CM_DIV_FRAC_BITS
;
1291 /* clamp to the highest possible integer divider */
1292 maxdiv
= (BIT(data
->int_bits
) - 1) << CM_DIV_FRAC_BITS
;
1294 /* clamp to min divider of 1 */
1295 mindiv
= 1 << CM_DIV_FRAC_BITS
;
1296 /* clamp to the highest possible fractional divider */
1297 maxdiv
= GENMASK(data
->int_bits
+ CM_DIV_FRAC_BITS
- 1,
1298 CM_DIV_FRAC_BITS
- data
->frac_bits
);
1301 /* apply the clamping limits */
1302 div
= max_t(u32
, div
, mindiv
);
1303 div
= min_t(u32
, div
, maxdiv
);
1308 static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock
*clock
,
1309 unsigned long parent_rate
,
1312 const struct bcm2835_clock_data
*data
= clock
->data
;
1316 * The divisor is a 12.12 fixed point field, but only some of
1317 * the bits are populated in any given clock.
1319 div
>>= CM_DIV_FRAC_BITS
- data
->frac_bits
;
1320 div
&= (1 << (data
->int_bits
+ data
->frac_bits
)) - 1;
1325 temp
= (u64
)parent_rate
<< data
->frac_bits
;
1332 static unsigned long bcm2835_clock_get_rate(struct clk_hw
*hw
,
1333 unsigned long parent_rate
)
1335 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1336 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1337 const struct bcm2835_clock_data
*data
= clock
->data
;
1338 u32 div
= cprman_read(cprman
, data
->div_reg
);
1340 return bcm2835_clock_rate_from_divisor(clock
, parent_rate
, div
);
1343 static void bcm2835_clock_wait_busy(struct bcm2835_clock
*clock
)
1345 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1346 const struct bcm2835_clock_data
*data
= clock
->data
;
1347 ktime_t timeout
= ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS
);
1349 while (cprman_read(cprman
, data
->ctl_reg
) & CM_BUSY
) {
1350 if (ktime_after(ktime_get(), timeout
)) {
1351 dev_err(cprman
->dev
, "%s: couldn't lock PLL\n",
1352 clk_hw_get_name(&clock
->hw
));
1359 static void bcm2835_clock_off(struct clk_hw
*hw
)
1361 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1362 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1363 const struct bcm2835_clock_data
*data
= clock
->data
;
1365 spin_lock(&cprman
->regs_lock
);
1366 cprman_write(cprman
, data
->ctl_reg
,
1367 cprman_read(cprman
, data
->ctl_reg
) & ~CM_ENABLE
);
1368 spin_unlock(&cprman
->regs_lock
);
1370 /* BUSY will remain high until the divider completes its cycle. */
1371 bcm2835_clock_wait_busy(clock
);
1374 static int bcm2835_clock_on(struct clk_hw
*hw
)
1376 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1377 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1378 const struct bcm2835_clock_data
*data
= clock
->data
;
1380 spin_lock(&cprman
->regs_lock
);
1381 cprman_write(cprman
, data
->ctl_reg
,
1382 cprman_read(cprman
, data
->ctl_reg
) |
1385 spin_unlock(&cprman
->regs_lock
);
1390 static int bcm2835_clock_set_rate(struct clk_hw
*hw
,
1391 unsigned long rate
, unsigned long parent_rate
)
1393 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1394 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1395 const struct bcm2835_clock_data
*data
= clock
->data
;
1396 u32 div
= bcm2835_clock_choose_div(hw
, rate
, parent_rate
, false);
1399 spin_lock(&cprman
->regs_lock
);
1402 * Setting up frac support
1404 * In principle it is recommended to stop/start the clock first,
1405 * but as we set CLK_SET_RATE_GATE during registration of the
1406 * clock this requirement should be take care of by the
1409 ctl
= cprman_read(cprman
, data
->ctl_reg
) & ~CM_FRAC
;
1410 ctl
|= (div
& CM_DIV_FRAC_MASK
) ? CM_FRAC
: 0;
1411 cprman_write(cprman
, data
->ctl_reg
, ctl
);
1413 cprman_write(cprman
, data
->div_reg
, div
);
1415 spin_unlock(&cprman
->regs_lock
);
1420 static int bcm2835_clock_determine_rate(struct clk_hw
*hw
,
1421 struct clk_rate_request
*req
)
1423 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1424 struct clk_hw
*parent
, *best_parent
= NULL
;
1425 unsigned long rate
, best_rate
= 0;
1426 unsigned long prate
, best_prate
= 0;
1431 * Select parent clock that results in the closest but lower rate
1433 for (i
= 0; i
< clk_hw_get_num_parents(hw
); ++i
) {
1434 parent
= clk_hw_get_parent_by_index(hw
, i
);
1437 prate
= clk_hw_get_rate(parent
);
1438 div
= bcm2835_clock_choose_div(hw
, req
->rate
, prate
, true);
1439 rate
= bcm2835_clock_rate_from_divisor(clock
, prate
, div
);
1440 if (rate
> best_rate
&& rate
<= req
->rate
) {
1441 best_parent
= parent
;
1450 req
->best_parent_hw
= best_parent
;
1451 req
->best_parent_rate
= best_prate
;
1453 req
->rate
= best_rate
;
1458 static int bcm2835_clock_set_parent(struct clk_hw
*hw
, u8 index
)
1460 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1461 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1462 const struct bcm2835_clock_data
*data
= clock
->data
;
1463 u8 src
= (index
<< CM_SRC_SHIFT
) & CM_SRC_MASK
;
1465 cprman_write(cprman
, data
->ctl_reg
, src
);
1469 static u8
bcm2835_clock_get_parent(struct clk_hw
*hw
)
1471 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1472 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1473 const struct bcm2835_clock_data
*data
= clock
->data
;
1474 u32 src
= cprman_read(cprman
, data
->ctl_reg
);
1476 return (src
& CM_SRC_MASK
) >> CM_SRC_SHIFT
;
1479 static struct debugfs_reg32 bcm2835_debugfs_clock_reg32
[] = {
1490 static int bcm2835_clock_debug_init(struct clk_hw
*hw
,
1491 struct dentry
*dentry
)
1493 struct bcm2835_clock
*clock
= bcm2835_clock_from_hw(hw
);
1494 struct bcm2835_cprman
*cprman
= clock
->cprman
;
1495 const struct bcm2835_clock_data
*data
= clock
->data
;
1497 return bcm2835_debugfs_regset(
1498 cprman
, data
->ctl_reg
,
1499 bcm2835_debugfs_clock_reg32
,
1500 ARRAY_SIZE(bcm2835_debugfs_clock_reg32
),
1504 static const struct clk_ops bcm2835_clock_clk_ops
= {
1505 .is_prepared
= bcm2835_clock_is_on
,
1506 .prepare
= bcm2835_clock_on
,
1507 .unprepare
= bcm2835_clock_off
,
1508 .recalc_rate
= bcm2835_clock_get_rate
,
1509 .set_rate
= bcm2835_clock_set_rate
,
1510 .determine_rate
= bcm2835_clock_determine_rate
,
1511 .set_parent
= bcm2835_clock_set_parent
,
1512 .get_parent
= bcm2835_clock_get_parent
,
1513 .debug_init
= bcm2835_clock_debug_init
,
1516 static int bcm2835_vpu_clock_is_on(struct clk_hw
*hw
)
1522 * The VPU clock can never be disabled (it doesn't have an ENABLE
1523 * bit), so it gets its own set of clock ops.
1525 static const struct clk_ops bcm2835_vpu_clock_clk_ops
= {
1526 .is_prepared
= bcm2835_vpu_clock_is_on
,
1527 .recalc_rate
= bcm2835_clock_get_rate
,
1528 .set_rate
= bcm2835_clock_set_rate
,
1529 .determine_rate
= bcm2835_clock_determine_rate
,
1530 .set_parent
= bcm2835_clock_set_parent
,
1531 .get_parent
= bcm2835_clock_get_parent
,
1532 .debug_init
= bcm2835_clock_debug_init
,
1535 static struct clk
*bcm2835_register_pll(struct bcm2835_cprman
*cprman
,
1536 const struct bcm2835_pll_data
*data
)
1538 struct bcm2835_pll
*pll
;
1539 struct clk_init_data init
;
1541 memset(&init
, 0, sizeof(init
));
1543 /* All of the PLLs derive from the external oscillator. */
1544 init
.parent_names
= &cprman
->osc_name
;
1545 init
.num_parents
= 1;
1546 init
.name
= data
->name
;
1547 init
.ops
= &bcm2835_pll_clk_ops
;
1548 init
.flags
= CLK_IGNORE_UNUSED
;
1550 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
1554 pll
->cprman
= cprman
;
1556 pll
->hw
.init
= &init
;
1558 return devm_clk_register(cprman
->dev
, &pll
->hw
);
1562 bcm2835_register_pll_divider(struct bcm2835_cprman
*cprman
,
1563 const struct bcm2835_pll_divider_data
*data
)
1565 struct bcm2835_pll_divider
*divider
;
1566 struct clk_init_data init
;
1568 const char *divider_name
;
1570 if (data
->fixed_divider
!= 1) {
1571 divider_name
= devm_kasprintf(cprman
->dev
, GFP_KERNEL
,
1572 "%s_prediv", data
->name
);
1576 divider_name
= data
->name
;
1579 memset(&init
, 0, sizeof(init
));
1581 init
.parent_names
= &data
->source_pll
->name
;
1582 init
.num_parents
= 1;
1583 init
.name
= divider_name
;
1584 init
.ops
= &bcm2835_pll_divider_clk_ops
;
1585 init
.flags
= CLK_SET_RATE_PARENT
| CLK_IGNORE_UNUSED
;
1587 divider
= devm_kzalloc(cprman
->dev
, sizeof(*divider
), GFP_KERNEL
);
1591 divider
->div
.reg
= cprman
->regs
+ data
->a2w_reg
;
1592 divider
->div
.shift
= A2W_PLL_DIV_SHIFT
;
1593 divider
->div
.width
= A2W_PLL_DIV_BITS
;
1594 divider
->div
.flags
= CLK_DIVIDER_MAX_AT_ZERO
;
1595 divider
->div
.lock
= &cprman
->regs_lock
;
1596 divider
->div
.hw
.init
= &init
;
1597 divider
->div
.table
= NULL
;
1599 divider
->cprman
= cprman
;
1600 divider
->data
= data
;
1602 clk
= devm_clk_register(cprman
->dev
, ÷r
->div
.hw
);
1607 * PLLH's channels have a fixed divide by 10 afterwards, which
1608 * is what our consumers are actually using.
1610 if (data
->fixed_divider
!= 1) {
1611 return clk_register_fixed_factor(cprman
->dev
, data
->name
,
1613 CLK_SET_RATE_PARENT
,
1615 data
->fixed_divider
);
1621 static struct clk
*bcm2835_register_clock(struct bcm2835_cprman
*cprman
,
1622 const struct bcm2835_clock_data
*data
)
1624 struct bcm2835_clock
*clock
;
1625 struct clk_init_data init
;
1626 const char *parents
[1 << CM_SRC_BITS
];
1630 * Replace our "xosc" references with the oscillator's
1633 for (i
= 0; i
< data
->num_mux_parents
; i
++) {
1634 if (strcmp(data
->parents
[i
], "xosc") == 0)
1635 parents
[i
] = cprman
->osc_name
;
1637 parents
[i
] = data
->parents
[i
];
1640 memset(&init
, 0, sizeof(init
));
1641 init
.parent_names
= parents
;
1642 init
.num_parents
= data
->num_mux_parents
;
1643 init
.name
= data
->name
;
1644 init
.flags
= CLK_IGNORE_UNUSED
;
1646 if (data
->is_vpu_clock
) {
1647 init
.ops
= &bcm2835_vpu_clock_clk_ops
;
1649 init
.ops
= &bcm2835_clock_clk_ops
;
1650 init
.flags
|= CLK_SET_RATE_GATE
| CLK_SET_PARENT_GATE
;
1653 clock
= devm_kzalloc(cprman
->dev
, sizeof(*clock
), GFP_KERNEL
);
1657 clock
->cprman
= cprman
;
1659 clock
->hw
.init
= &init
;
1661 return devm_clk_register(cprman
->dev
, &clock
->hw
);
1664 static struct clk
*bcm2835_register_gate(struct bcm2835_cprman
*cprman
,
1665 const struct bcm2835_gate_data
*data
)
1667 return clk_register_gate(cprman
->dev
, data
->name
, data
->parent
,
1668 CLK_IGNORE_UNUSED
| CLK_SET_RATE_GATE
,
1669 cprman
->regs
+ data
->ctl_reg
,
1670 CM_GATE_BIT
, 0, &cprman
->regs_lock
);
1673 typedef struct clk
*(*bcm2835_clk_register
)(struct bcm2835_cprman
*cprman
,
1675 struct bcm2835_clk_desc
{
1676 bcm2835_clk_register clk_register
;
1680 #define _REGISTER(f, d) { .clk_register = (bcm2835_clk_register)f, \
1682 #define REGISTER_PLL(d) _REGISTER(&bcm2835_register_pll, d)
1683 #define REGISTER_PLL_DIV(d) _REGISTER(&bcm2835_register_pll_divider, d)
1684 #define REGISTER_CLK(d) _REGISTER(&bcm2835_register_clock, d)
1685 #define REGISTER_GATE(d) _REGISTER(&bcm2835_register_gate, d)
1687 static const struct bcm2835_clk_desc clk_desc_array
[] = {
1689 [BCM2835_PLLA
] = REGISTER_PLL(&bcm2835_plla_data
),
1690 [BCM2835_PLLB
] = REGISTER_PLL(&bcm2835_pllb_data
),
1691 [BCM2835_PLLC
] = REGISTER_PLL(&bcm2835_pllc_data
),
1692 [BCM2835_PLLD
] = REGISTER_PLL(&bcm2835_plld_data
),
1693 [BCM2835_PLLH
] = REGISTER_PLL(&bcm2835_pllh_data
),
1694 /* the PLL dividers */
1695 [BCM2835_PLLA_CORE
] = REGISTER_PLL_DIV(&bcm2835_plla_core_data
),
1696 [BCM2835_PLLA_PER
] = REGISTER_PLL_DIV(&bcm2835_plla_per_data
),
1697 [BCM2835_PLLC_CORE0
] = REGISTER_PLL_DIV(&bcm2835_pllc_core0_data
),
1698 [BCM2835_PLLC_CORE1
] = REGISTER_PLL_DIV(&bcm2835_pllc_core1_data
),
1699 [BCM2835_PLLC_CORE2
] = REGISTER_PLL_DIV(&bcm2835_pllc_core2_data
),
1700 [BCM2835_PLLC_PER
] = REGISTER_PLL_DIV(&bcm2835_pllc_per_data
),
1701 [BCM2835_PLLD_CORE
] = REGISTER_PLL_DIV(&bcm2835_plld_core_data
),
1702 [BCM2835_PLLD_PER
] = REGISTER_PLL_DIV(&bcm2835_plld_per_data
),
1703 [BCM2835_PLLH_RCAL
] = REGISTER_PLL_DIV(&bcm2835_pllh_rcal_data
),
1704 [BCM2835_PLLH_AUX
] = REGISTER_PLL_DIV(&bcm2835_pllh_aux_data
),
1705 [BCM2835_PLLH_PIX
] = REGISTER_PLL_DIV(&bcm2835_pllh_pix_data
),
1707 [BCM2835_CLOCK_TIMER
] = REGISTER_CLK(&bcm2835_clock_timer_data
),
1708 [BCM2835_CLOCK_OTP
] = REGISTER_CLK(&bcm2835_clock_otp_data
),
1709 [BCM2835_CLOCK_TSENS
] = REGISTER_CLK(&bcm2835_clock_tsens_data
),
1710 [BCM2835_CLOCK_VPU
] = REGISTER_CLK(&bcm2835_clock_vpu_data
),
1711 [BCM2835_CLOCK_V3D
] = REGISTER_CLK(&bcm2835_clock_v3d_data
),
1712 [BCM2835_CLOCK_ISP
] = REGISTER_CLK(&bcm2835_clock_isp_data
),
1713 [BCM2835_CLOCK_H264
] = REGISTER_CLK(&bcm2835_clock_h264_data
),
1714 [BCM2835_CLOCK_V3D
] = REGISTER_CLK(&bcm2835_clock_v3d_data
),
1715 [BCM2835_CLOCK_SDRAM
] = REGISTER_CLK(&bcm2835_clock_sdram_data
),
1716 [BCM2835_CLOCK_UART
] = REGISTER_CLK(&bcm2835_clock_uart_data
),
1717 [BCM2835_CLOCK_VEC
] = REGISTER_CLK(&bcm2835_clock_vec_data
),
1718 [BCM2835_CLOCK_HSM
] = REGISTER_CLK(&bcm2835_clock_hsm_data
),
1719 [BCM2835_CLOCK_EMMC
] = REGISTER_CLK(&bcm2835_clock_emmc_data
),
1720 [BCM2835_CLOCK_PWM
] = REGISTER_CLK(&bcm2835_clock_pwm_data
),
1722 [BCM2835_CLOCK_PERI_IMAGE
] = REGISTER_GATE(
1723 &bcm2835_clock_peri_image_data
),
1726 static int bcm2835_clk_probe(struct platform_device
*pdev
)
1728 struct device
*dev
= &pdev
->dev
;
1730 struct bcm2835_cprman
*cprman
;
1731 struct resource
*res
;
1732 const struct bcm2835_clk_desc
*desc
;
1733 const size_t asize
= ARRAY_SIZE(clk_desc_array
);
1736 cprman
= devm_kzalloc(dev
,
1737 sizeof(*cprman
) + asize
* sizeof(*clks
),
1742 spin_lock_init(&cprman
->regs_lock
);
1744 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1745 cprman
->regs
= devm_ioremap_resource(dev
, res
);
1746 if (IS_ERR(cprman
->regs
))
1747 return PTR_ERR(cprman
->regs
);
1749 cprman
->osc_name
= of_clk_get_parent_name(dev
->of_node
, 0);
1750 if (!cprman
->osc_name
)
1753 platform_set_drvdata(pdev
, cprman
);
1755 cprman
->onecell
.clk_num
= asize
;
1756 cprman
->onecell
.clks
= cprman
->clks
;
1757 clks
= cprman
->clks
;
1759 for (i
= 0; i
< asize
; i
++) {
1760 desc
= &clk_desc_array
[i
];
1761 if (desc
->clk_register
&& desc
->data
)
1762 clks
[i
] = desc
->clk_register(cprman
, desc
->data
);
1765 return of_clk_add_provider(dev
->of_node
, of_clk_src_onecell_get
,
1769 static const struct of_device_id bcm2835_clk_of_match
[] = {
1770 { .compatible
= "brcm,bcm2835-cprman", },
1773 MODULE_DEVICE_TABLE(of
, bcm2835_clk_of_match
);
1775 static struct platform_driver bcm2835_clk_driver
= {
1777 .name
= "bcm2835-clk",
1778 .of_match_table
= bcm2835_clk_of_match
,
1780 .probe
= bcm2835_clk_probe
,
1783 builtin_platform_driver(bcm2835_clk_driver
);
1785 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
1786 MODULE_DESCRIPTION("BCM2835 clock driver");
1787 MODULE_LICENSE("GPL v2");