of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / clk / bcm / clk-bcm2835.c
blob39bf5820297e472d95e88ddc28c868a0c80b5f6c
1 /*
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /**
21 * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
23 * The clock tree on the 2835 has several levels. There's a root
24 * oscillator running at 19.2Mhz. After the oscillator there are 5
25 * PLLs, roughly divided as "camera", "ARM", "core", "DSI displays",
26 * and "HDMI displays". Those 5 PLLs each can divide their output to
27 * produce up to 4 channels. Finally, there is the level of clocks to
28 * be consumed by other hardware components (like "H264" or "HDMI
29 * state machine"), which divide off of some subset of the PLL
30 * channels.
32 * All of the clocks in the tree are exposed in the DT, because the DT
33 * may want to make assignments of the final layer of clocks to the
34 * PLL channels, and some components of the hardware will actually
35 * skip layers of the tree (for example, the pixel clock comes
36 * directly from the PLLH PIX channel without using a CM_*CTL clock
37 * generator).
40 #include <linux/clk-provider.h>
41 #include <linux/clkdev.h>
42 #include <linux/clk/bcm2835.h>
43 #include <linux/module.h>
44 #include <linux/of.h>
45 #include <linux/platform_device.h>
46 #include <linux/slab.h>
47 #include <dt-bindings/clock/bcm2835.h>
49 #define CM_PASSWORD 0x5a000000
51 #define CM_GNRICCTL 0x000
52 #define CM_GNRICDIV 0x004
53 # define CM_DIV_FRAC_BITS 12
55 #define CM_VPUCTL 0x008
56 #define CM_VPUDIV 0x00c
57 #define CM_SYSCTL 0x010
58 #define CM_SYSDIV 0x014
59 #define CM_PERIACTL 0x018
60 #define CM_PERIADIV 0x01c
61 #define CM_PERIICTL 0x020
62 #define CM_PERIIDIV 0x024
63 #define CM_H264CTL 0x028
64 #define CM_H264DIV 0x02c
65 #define CM_ISPCTL 0x030
66 #define CM_ISPDIV 0x034
67 #define CM_V3DCTL 0x038
68 #define CM_V3DDIV 0x03c
69 #define CM_CAM0CTL 0x040
70 #define CM_CAM0DIV 0x044
71 #define CM_CAM1CTL 0x048
72 #define CM_CAM1DIV 0x04c
73 #define CM_CCP2CTL 0x050
74 #define CM_CCP2DIV 0x054
75 #define CM_DSI0ECTL 0x058
76 #define CM_DSI0EDIV 0x05c
77 #define CM_DSI0PCTL 0x060
78 #define CM_DSI0PDIV 0x064
79 #define CM_DPICTL 0x068
80 #define CM_DPIDIV 0x06c
81 #define CM_GP0CTL 0x070
82 #define CM_GP0DIV 0x074
83 #define CM_GP1CTL 0x078
84 #define CM_GP1DIV 0x07c
85 #define CM_GP2CTL 0x080
86 #define CM_GP2DIV 0x084
87 #define CM_HSMCTL 0x088
88 #define CM_HSMDIV 0x08c
89 #define CM_OTPCTL 0x090
90 #define CM_OTPDIV 0x094
91 #define CM_PWMCTL 0x0a0
92 #define CM_PWMDIV 0x0a4
93 #define CM_SMICTL 0x0b0
94 #define CM_SMIDIV 0x0b4
95 #define CM_TSENSCTL 0x0e0
96 #define CM_TSENSDIV 0x0e4
97 #define CM_TIMERCTL 0x0e8
98 #define CM_TIMERDIV 0x0ec
99 #define CM_UARTCTL 0x0f0
100 #define CM_UARTDIV 0x0f4
101 #define CM_VECCTL 0x0f8
102 #define CM_VECDIV 0x0fc
103 #define CM_PULSECTL 0x190
104 #define CM_PULSEDIV 0x194
105 #define CM_SDCCTL 0x1a8
106 #define CM_SDCDIV 0x1ac
107 #define CM_ARMCTL 0x1b0
108 #define CM_EMMCCTL 0x1c0
109 #define CM_EMMCDIV 0x1c4
111 /* General bits for the CM_*CTL regs */
112 # define CM_ENABLE BIT(4)
113 # define CM_KILL BIT(5)
114 # define CM_GATE_BIT 6
115 # define CM_GATE BIT(CM_GATE_BIT)
116 # define CM_BUSY BIT(7)
117 # define CM_BUSYD BIT(8)
118 # define CM_SRC_SHIFT 0
119 # define CM_SRC_BITS 4
120 # define CM_SRC_MASK 0xf
121 # define CM_SRC_GND 0
122 # define CM_SRC_OSC 1
123 # define CM_SRC_TESTDEBUG0 2
124 # define CM_SRC_TESTDEBUG1 3
125 # define CM_SRC_PLLA_CORE 4
126 # define CM_SRC_PLLA_PER 4
127 # define CM_SRC_PLLC_CORE0 5
128 # define CM_SRC_PLLC_PER 5
129 # define CM_SRC_PLLC_CORE1 8
130 # define CM_SRC_PLLD_CORE 6
131 # define CM_SRC_PLLD_PER 6
132 # define CM_SRC_PLLH_AUX 7
133 # define CM_SRC_PLLC_CORE1 8
134 # define CM_SRC_PLLC_CORE2 9
136 #define CM_OSCCOUNT 0x100
138 #define CM_PLLA 0x104
139 # define CM_PLL_ANARST BIT(8)
140 # define CM_PLLA_HOLDPER BIT(7)
141 # define CM_PLLA_LOADPER BIT(6)
142 # define CM_PLLA_HOLDCORE BIT(5)
143 # define CM_PLLA_LOADCORE BIT(4)
144 # define CM_PLLA_HOLDCCP2 BIT(3)
145 # define CM_PLLA_LOADCCP2 BIT(2)
146 # define CM_PLLA_HOLDDSI0 BIT(1)
147 # define CM_PLLA_LOADDSI0 BIT(0)
149 #define CM_PLLC 0x108
150 # define CM_PLLC_HOLDPER BIT(7)
151 # define CM_PLLC_LOADPER BIT(6)
152 # define CM_PLLC_HOLDCORE2 BIT(5)
153 # define CM_PLLC_LOADCORE2 BIT(4)
154 # define CM_PLLC_HOLDCORE1 BIT(3)
155 # define CM_PLLC_LOADCORE1 BIT(2)
156 # define CM_PLLC_HOLDCORE0 BIT(1)
157 # define CM_PLLC_LOADCORE0 BIT(0)
159 #define CM_PLLD 0x10c
160 # define CM_PLLD_HOLDPER BIT(7)
161 # define CM_PLLD_LOADPER BIT(6)
162 # define CM_PLLD_HOLDCORE BIT(5)
163 # define CM_PLLD_LOADCORE BIT(4)
164 # define CM_PLLD_HOLDDSI1 BIT(3)
165 # define CM_PLLD_LOADDSI1 BIT(2)
166 # define CM_PLLD_HOLDDSI0 BIT(1)
167 # define CM_PLLD_LOADDSI0 BIT(0)
169 #define CM_PLLH 0x110
170 # define CM_PLLH_LOADRCAL BIT(2)
171 # define CM_PLLH_LOADAUX BIT(1)
172 # define CM_PLLH_LOADPIX BIT(0)
174 #define CM_LOCK 0x114
175 # define CM_LOCK_FLOCKH BIT(12)
176 # define CM_LOCK_FLOCKD BIT(11)
177 # define CM_LOCK_FLOCKC BIT(10)
178 # define CM_LOCK_FLOCKB BIT(9)
179 # define CM_LOCK_FLOCKA BIT(8)
181 #define CM_EVENT 0x118
182 #define CM_DSI1ECTL 0x158
183 #define CM_DSI1EDIV 0x15c
184 #define CM_DSI1PCTL 0x160
185 #define CM_DSI1PDIV 0x164
186 #define CM_DFTCTL 0x168
187 #define CM_DFTDIV 0x16c
189 #define CM_PLLB 0x170
190 # define CM_PLLB_HOLDARM BIT(1)
191 # define CM_PLLB_LOADARM BIT(0)
193 #define A2W_PLLA_CTRL 0x1100
194 #define A2W_PLLC_CTRL 0x1120
195 #define A2W_PLLD_CTRL 0x1140
196 #define A2W_PLLH_CTRL 0x1160
197 #define A2W_PLLB_CTRL 0x11e0
198 # define A2W_PLL_CTRL_PRST_DISABLE BIT(17)
199 # define A2W_PLL_CTRL_PWRDN BIT(16)
200 # define A2W_PLL_CTRL_PDIV_MASK 0x000007000
201 # define A2W_PLL_CTRL_PDIV_SHIFT 12
202 # define A2W_PLL_CTRL_NDIV_MASK 0x0000003ff
203 # define A2W_PLL_CTRL_NDIV_SHIFT 0
205 #define A2W_PLLA_ANA0 0x1010
206 #define A2W_PLLC_ANA0 0x1030
207 #define A2W_PLLD_ANA0 0x1050
208 #define A2W_PLLH_ANA0 0x1070
209 #define A2W_PLLB_ANA0 0x10f0
211 #define A2W_PLL_KA_SHIFT 7
212 #define A2W_PLL_KA_MASK GENMASK(9, 7)
213 #define A2W_PLL_KI_SHIFT 19
214 #define A2W_PLL_KI_MASK GENMASK(21, 19)
215 #define A2W_PLL_KP_SHIFT 15
216 #define A2W_PLL_KP_MASK GENMASK(18, 15)
218 #define A2W_PLLH_KA_SHIFT 19
219 #define A2W_PLLH_KA_MASK GENMASK(21, 19)
220 #define A2W_PLLH_KI_LOW_SHIFT 22
221 #define A2W_PLLH_KI_LOW_MASK GENMASK(23, 22)
222 #define A2W_PLLH_KI_HIGH_SHIFT 0
223 #define A2W_PLLH_KI_HIGH_MASK GENMASK(0, 0)
224 #define A2W_PLLH_KP_SHIFT 1
225 #define A2W_PLLH_KP_MASK GENMASK(4, 1)
227 #define A2W_XOSC_CTRL 0x1190
228 # define A2W_XOSC_CTRL_PLLB_ENABLE BIT(7)
229 # define A2W_XOSC_CTRL_PLLA_ENABLE BIT(6)
230 # define A2W_XOSC_CTRL_PLLD_ENABLE BIT(5)
231 # define A2W_XOSC_CTRL_DDR_ENABLE BIT(4)
232 # define A2W_XOSC_CTRL_CPR1_ENABLE BIT(3)
233 # define A2W_XOSC_CTRL_USB_ENABLE BIT(2)
234 # define A2W_XOSC_CTRL_HDMI_ENABLE BIT(1)
235 # define A2W_XOSC_CTRL_PLLC_ENABLE BIT(0)
237 #define A2W_PLLA_FRAC 0x1200
238 #define A2W_PLLC_FRAC 0x1220
239 #define A2W_PLLD_FRAC 0x1240
240 #define A2W_PLLH_FRAC 0x1260
241 #define A2W_PLLB_FRAC 0x12e0
242 # define A2W_PLL_FRAC_MASK ((1 << A2W_PLL_FRAC_BITS) - 1)
243 # define A2W_PLL_FRAC_BITS 20
245 #define A2W_PLL_CHANNEL_DISABLE BIT(8)
246 #define A2W_PLL_DIV_BITS 8
247 #define A2W_PLL_DIV_SHIFT 0
249 #define A2W_PLLA_DSI0 0x1300
250 #define A2W_PLLA_CORE 0x1400
251 #define A2W_PLLA_PER 0x1500
252 #define A2W_PLLA_CCP2 0x1600
254 #define A2W_PLLC_CORE2 0x1320
255 #define A2W_PLLC_CORE1 0x1420
256 #define A2W_PLLC_PER 0x1520
257 #define A2W_PLLC_CORE0 0x1620
259 #define A2W_PLLD_DSI0 0x1340
260 #define A2W_PLLD_CORE 0x1440
261 #define A2W_PLLD_PER 0x1540
262 #define A2W_PLLD_DSI1 0x1640
264 #define A2W_PLLH_AUX 0x1360
265 #define A2W_PLLH_RCAL 0x1460
266 #define A2W_PLLH_PIX 0x1560
267 #define A2W_PLLH_STS 0x1660
269 #define A2W_PLLH_CTRLR 0x1960
270 #define A2W_PLLH_FRACR 0x1a60
271 #define A2W_PLLH_AUXR 0x1b60
272 #define A2W_PLLH_RCALR 0x1c60
273 #define A2W_PLLH_PIXR 0x1d60
274 #define A2W_PLLH_STSR 0x1e60
276 #define A2W_PLLB_ARM 0x13e0
277 #define A2W_PLLB_SP0 0x14e0
278 #define A2W_PLLB_SP1 0x15e0
279 #define A2W_PLLB_SP2 0x16e0
281 #define LOCK_TIMEOUT_NS 100000000
282 #define BCM2835_MAX_FB_RATE 1750000000u
284 struct bcm2835_cprman {
285 struct device *dev;
286 void __iomem *regs;
287 spinlock_t regs_lock;
288 const char *osc_name;
290 struct clk_onecell_data onecell;
291 struct clk *clks[BCM2835_CLOCK_COUNT];
294 static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
296 writel(CM_PASSWORD | val, cprman->regs + reg);
299 static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
301 return readl(cprman->regs + reg);
305 * These are fixed clocks. They're probably not all root clocks and it may
306 * be possible to turn them on and off but until this is mapped out better
307 * it's the only way they can be used.
309 void __init bcm2835_init_clocks(void)
311 struct clk *clk;
312 int ret;
314 clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, CLK_IS_ROOT,
315 126000000);
316 if (IS_ERR(clk))
317 pr_err("apb_pclk not registered\n");
319 clk = clk_register_fixed_rate(NULL, "uart0_pclk", NULL, CLK_IS_ROOT,
320 3000000);
321 if (IS_ERR(clk))
322 pr_err("uart0_pclk not registered\n");
323 ret = clk_register_clkdev(clk, NULL, "20201000.uart");
324 if (ret)
325 pr_err("uart0_pclk alias not registered\n");
327 clk = clk_register_fixed_rate(NULL, "uart1_pclk", NULL, CLK_IS_ROOT,
328 125000000);
329 if (IS_ERR(clk))
330 pr_err("uart1_pclk not registered\n");
331 ret = clk_register_clkdev(clk, NULL, "20215000.uart");
332 if (ret)
333 pr_err("uart1_pclk alias not registered\n");
336 struct bcm2835_pll_data {
337 const char *name;
338 u32 cm_ctrl_reg;
339 u32 a2w_ctrl_reg;
340 u32 frac_reg;
341 u32 ana_reg_base;
342 u32 reference_enable_mask;
343 /* Bit in CM_LOCK to indicate when the PLL has locked. */
344 u32 lock_mask;
346 const struct bcm2835_pll_ana_bits *ana;
348 unsigned long min_rate;
349 unsigned long max_rate;
351 * Highest rate for the VCO before we have to use the
352 * pre-divide-by-2.
354 unsigned long max_fb_rate;
357 struct bcm2835_pll_ana_bits {
358 u32 mask0;
359 u32 set0;
360 u32 mask1;
361 u32 set1;
362 u32 mask3;
363 u32 set3;
364 u32 fb_prediv_mask;
367 static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
368 .mask0 = 0,
369 .set0 = 0,
370 .mask1 = ~(A2W_PLL_KI_MASK | A2W_PLL_KP_MASK),
371 .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
372 .mask3 = ~A2W_PLL_KA_MASK,
373 .set3 = (2 << A2W_PLL_KA_SHIFT),
374 .fb_prediv_mask = BIT(14),
377 static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
378 .mask0 = ~(A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK),
379 .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
380 .mask1 = ~(A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK),
381 .set1 = (6 << A2W_PLLH_KP_SHIFT),
382 .mask3 = 0,
383 .set3 = 0,
384 .fb_prediv_mask = BIT(11),
388 * PLLA is the auxiliary PLL, used to drive the CCP2 (Compact Camera
389 * Port 2) transmitter clock.
391 * It is in the PX LDO power domain, which is on when the AUDIO domain
392 * is on.
394 static const struct bcm2835_pll_data bcm2835_plla_data = {
395 .name = "plla",
396 .cm_ctrl_reg = CM_PLLA,
397 .a2w_ctrl_reg = A2W_PLLA_CTRL,
398 .frac_reg = A2W_PLLA_FRAC,
399 .ana_reg_base = A2W_PLLA_ANA0,
400 .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
401 .lock_mask = CM_LOCK_FLOCKA,
403 .ana = &bcm2835_ana_default,
405 .min_rate = 600000000u,
406 .max_rate = 2400000000u,
407 .max_fb_rate = BCM2835_MAX_FB_RATE,
410 /* PLLB is used for the ARM's clock. */
411 static const struct bcm2835_pll_data bcm2835_pllb_data = {
412 .name = "pllb",
413 .cm_ctrl_reg = CM_PLLB,
414 .a2w_ctrl_reg = A2W_PLLB_CTRL,
415 .frac_reg = A2W_PLLB_FRAC,
416 .ana_reg_base = A2W_PLLB_ANA0,
417 .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
418 .lock_mask = CM_LOCK_FLOCKB,
420 .ana = &bcm2835_ana_default,
422 .min_rate = 600000000u,
423 .max_rate = 3000000000u,
424 .max_fb_rate = BCM2835_MAX_FB_RATE,
428 * PLLC is the core PLL, used to drive the core VPU clock.
430 * It is in the PX LDO power domain, which is on when the AUDIO domain
431 * is on.
433 static const struct bcm2835_pll_data bcm2835_pllc_data = {
434 .name = "pllc",
435 .cm_ctrl_reg = CM_PLLC,
436 .a2w_ctrl_reg = A2W_PLLC_CTRL,
437 .frac_reg = A2W_PLLC_FRAC,
438 .ana_reg_base = A2W_PLLC_ANA0,
439 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
440 .lock_mask = CM_LOCK_FLOCKC,
442 .ana = &bcm2835_ana_default,
444 .min_rate = 600000000u,
445 .max_rate = 3000000000u,
446 .max_fb_rate = BCM2835_MAX_FB_RATE,
450 * PLLD is the display PLL, used to drive DSI display panels.
452 * It is in the PX LDO power domain, which is on when the AUDIO domain
453 * is on.
455 static const struct bcm2835_pll_data bcm2835_plld_data = {
456 .name = "plld",
457 .cm_ctrl_reg = CM_PLLD,
458 .a2w_ctrl_reg = A2W_PLLD_CTRL,
459 .frac_reg = A2W_PLLD_FRAC,
460 .ana_reg_base = A2W_PLLD_ANA0,
461 .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
462 .lock_mask = CM_LOCK_FLOCKD,
464 .ana = &bcm2835_ana_default,
466 .min_rate = 600000000u,
467 .max_rate = 2400000000u,
468 .max_fb_rate = BCM2835_MAX_FB_RATE,
472 * PLLH is used to supply the pixel clock or the AUX clock for the TV
473 * encoder.
475 * It is in the HDMI power domain.
477 static const struct bcm2835_pll_data bcm2835_pllh_data = {
478 "pllh",
479 .cm_ctrl_reg = CM_PLLH,
480 .a2w_ctrl_reg = A2W_PLLH_CTRL,
481 .frac_reg = A2W_PLLH_FRAC,
482 .ana_reg_base = A2W_PLLH_ANA0,
483 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
484 .lock_mask = CM_LOCK_FLOCKH,
486 .ana = &bcm2835_ana_pllh,
488 .min_rate = 600000000u,
489 .max_rate = 3000000000u,
490 .max_fb_rate = BCM2835_MAX_FB_RATE,
493 struct bcm2835_pll_divider_data {
494 const char *name;
495 const struct bcm2835_pll_data *source_pll;
496 u32 cm_reg;
497 u32 a2w_reg;
499 u32 load_mask;
500 u32 hold_mask;
501 u32 fixed_divider;
504 static const struct bcm2835_pll_divider_data bcm2835_plla_core_data = {
505 .name = "plla_core",
506 .source_pll = &bcm2835_plla_data,
507 .cm_reg = CM_PLLA,
508 .a2w_reg = A2W_PLLA_CORE,
509 .load_mask = CM_PLLA_LOADCORE,
510 .hold_mask = CM_PLLA_HOLDCORE,
511 .fixed_divider = 1,
514 static const struct bcm2835_pll_divider_data bcm2835_plla_per_data = {
515 .name = "plla_per",
516 .source_pll = &bcm2835_plla_data,
517 .cm_reg = CM_PLLA,
518 .a2w_reg = A2W_PLLA_PER,
519 .load_mask = CM_PLLA_LOADPER,
520 .hold_mask = CM_PLLA_HOLDPER,
521 .fixed_divider = 1,
524 static const struct bcm2835_pll_divider_data bcm2835_pllb_arm_data = {
525 .name = "pllb_arm",
526 .source_pll = &bcm2835_pllb_data,
527 .cm_reg = CM_PLLB,
528 .a2w_reg = A2W_PLLB_ARM,
529 .load_mask = CM_PLLB_LOADARM,
530 .hold_mask = CM_PLLB_HOLDARM,
531 .fixed_divider = 1,
534 static const struct bcm2835_pll_divider_data bcm2835_pllc_core0_data = {
535 .name = "pllc_core0",
536 .source_pll = &bcm2835_pllc_data,
537 .cm_reg = CM_PLLC,
538 .a2w_reg = A2W_PLLC_CORE0,
539 .load_mask = CM_PLLC_LOADCORE0,
540 .hold_mask = CM_PLLC_HOLDCORE0,
541 .fixed_divider = 1,
544 static const struct bcm2835_pll_divider_data bcm2835_pllc_core1_data = {
545 .name = "pllc_core1", .source_pll = &bcm2835_pllc_data,
546 .cm_reg = CM_PLLC, A2W_PLLC_CORE1,
547 .load_mask = CM_PLLC_LOADCORE1,
548 .hold_mask = CM_PLLC_HOLDCORE1,
549 .fixed_divider = 1,
552 static const struct bcm2835_pll_divider_data bcm2835_pllc_core2_data = {
553 .name = "pllc_core2",
554 .source_pll = &bcm2835_pllc_data,
555 .cm_reg = CM_PLLC,
556 .a2w_reg = A2W_PLLC_CORE2,
557 .load_mask = CM_PLLC_LOADCORE2,
558 .hold_mask = CM_PLLC_HOLDCORE2,
559 .fixed_divider = 1,
562 static const struct bcm2835_pll_divider_data bcm2835_pllc_per_data = {
563 .name = "pllc_per",
564 .source_pll = &bcm2835_pllc_data,
565 .cm_reg = CM_PLLC,
566 .a2w_reg = A2W_PLLC_PER,
567 .load_mask = CM_PLLC_LOADPER,
568 .hold_mask = CM_PLLC_HOLDPER,
569 .fixed_divider = 1,
572 static const struct bcm2835_pll_divider_data bcm2835_plld_core_data = {
573 .name = "plld_core",
574 .source_pll = &bcm2835_plld_data,
575 .cm_reg = CM_PLLD,
576 .a2w_reg = A2W_PLLD_CORE,
577 .load_mask = CM_PLLD_LOADCORE,
578 .hold_mask = CM_PLLD_HOLDCORE,
579 .fixed_divider = 1,
582 static const struct bcm2835_pll_divider_data bcm2835_plld_per_data = {
583 .name = "plld_per",
584 .source_pll = &bcm2835_plld_data,
585 .cm_reg = CM_PLLD,
586 .a2w_reg = A2W_PLLD_PER,
587 .load_mask = CM_PLLD_LOADPER,
588 .hold_mask = CM_PLLD_HOLDPER,
589 .fixed_divider = 1,
592 static const struct bcm2835_pll_divider_data bcm2835_pllh_rcal_data = {
593 .name = "pllh_rcal",
594 .source_pll = &bcm2835_pllh_data,
595 .cm_reg = CM_PLLH,
596 .a2w_reg = A2W_PLLH_RCAL,
597 .load_mask = CM_PLLH_LOADRCAL,
598 .hold_mask = 0,
599 .fixed_divider = 10,
602 static const struct bcm2835_pll_divider_data bcm2835_pllh_aux_data = {
603 .name = "pllh_aux",
604 .source_pll = &bcm2835_pllh_data,
605 .cm_reg = CM_PLLH,
606 .a2w_reg = A2W_PLLH_AUX,
607 .load_mask = CM_PLLH_LOADAUX,
608 .hold_mask = 0,
609 .fixed_divider = 10,
612 static const struct bcm2835_pll_divider_data bcm2835_pllh_pix_data = {
613 .name = "pllh_pix",
614 .source_pll = &bcm2835_pllh_data,
615 .cm_reg = CM_PLLH,
616 .a2w_reg = A2W_PLLH_PIX,
617 .load_mask = CM_PLLH_LOADPIX,
618 .hold_mask = 0,
619 .fixed_divider = 10,
622 struct bcm2835_clock_data {
623 const char *name;
625 const char *const *parents;
626 int num_mux_parents;
628 u32 ctl_reg;
629 u32 div_reg;
631 /* Number of integer bits in the divider */
632 u32 int_bits;
633 /* Number of fractional bits in the divider */
634 u32 frac_bits;
636 bool is_vpu_clock;
639 static const char *const bcm2835_clock_per_parents[] = {
640 "gnd",
641 "xosc",
642 "testdebug0",
643 "testdebug1",
644 "plla_per",
645 "pllc_per",
646 "plld_per",
647 "pllh_aux",
650 static const char *const bcm2835_clock_vpu_parents[] = {
651 "gnd",
652 "xosc",
653 "testdebug0",
654 "testdebug1",
655 "plla_core",
656 "pllc_core0",
657 "plld_core",
658 "pllh_aux",
659 "pllc_core1",
660 "pllc_core2",
663 static const char *const bcm2835_clock_osc_parents[] = {
664 "gnd",
665 "xosc",
666 "testdebug0",
667 "testdebug1"
671 * Used for a 1Mhz clock for the system clocksource, and also used by
672 * the watchdog timer and the camera pulse generator.
674 static const struct bcm2835_clock_data bcm2835_clock_timer_data = {
675 .name = "timer",
676 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
677 .parents = bcm2835_clock_osc_parents,
678 .ctl_reg = CM_TIMERCTL,
679 .div_reg = CM_TIMERDIV,
680 .int_bits = 6,
681 .frac_bits = 12,
684 /* One Time Programmable Memory clock. Maximum 10Mhz. */
685 static const struct bcm2835_clock_data bcm2835_clock_otp_data = {
686 .name = "otp",
687 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
688 .parents = bcm2835_clock_osc_parents,
689 .ctl_reg = CM_OTPCTL,
690 .div_reg = CM_OTPDIV,
691 .int_bits = 4,
692 .frac_bits = 0,
696 * VPU clock. This doesn't have an enable bit, since it drives the
697 * bus for everything else, and is special so it doesn't need to be
698 * gated for rate changes. It is also known as "clk_audio" in various
699 * hardware documentation.
701 static const struct bcm2835_clock_data bcm2835_clock_vpu_data = {
702 .name = "vpu",
703 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
704 .parents = bcm2835_clock_vpu_parents,
705 .ctl_reg = CM_VPUCTL,
706 .div_reg = CM_VPUDIV,
707 .int_bits = 12,
708 .frac_bits = 8,
709 .is_vpu_clock = true,
712 static const struct bcm2835_clock_data bcm2835_clock_v3d_data = {
713 .name = "v3d",
714 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
715 .parents = bcm2835_clock_vpu_parents,
716 .ctl_reg = CM_V3DCTL,
717 .div_reg = CM_V3DDIV,
718 .int_bits = 4,
719 .frac_bits = 8,
722 static const struct bcm2835_clock_data bcm2835_clock_isp_data = {
723 .name = "isp",
724 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
725 .parents = bcm2835_clock_vpu_parents,
726 .ctl_reg = CM_ISPCTL,
727 .div_reg = CM_ISPDIV,
728 .int_bits = 4,
729 .frac_bits = 8,
732 static const struct bcm2835_clock_data bcm2835_clock_h264_data = {
733 .name = "h264",
734 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
735 .parents = bcm2835_clock_vpu_parents,
736 .ctl_reg = CM_H264CTL,
737 .div_reg = CM_H264DIV,
738 .int_bits = 4,
739 .frac_bits = 8,
742 /* TV encoder clock. Only operating frequency is 108Mhz. */
743 static const struct bcm2835_clock_data bcm2835_clock_vec_data = {
744 .name = "vec",
745 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
746 .parents = bcm2835_clock_per_parents,
747 .ctl_reg = CM_VECCTL,
748 .div_reg = CM_VECDIV,
749 .int_bits = 4,
750 .frac_bits = 0,
753 static const struct bcm2835_clock_data bcm2835_clock_uart_data = {
754 .name = "uart",
755 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
756 .parents = bcm2835_clock_per_parents,
757 .ctl_reg = CM_UARTCTL,
758 .div_reg = CM_UARTDIV,
759 .int_bits = 10,
760 .frac_bits = 12,
763 /* HDMI state machine */
764 static const struct bcm2835_clock_data bcm2835_clock_hsm_data = {
765 .name = "hsm",
766 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
767 .parents = bcm2835_clock_per_parents,
768 .ctl_reg = CM_HSMCTL,
769 .div_reg = CM_HSMDIV,
770 .int_bits = 4,
771 .frac_bits = 8,
775 * Secondary SDRAM clock. Used for low-voltage modes when the PLL in
776 * the SDRAM controller can't be used.
778 static const struct bcm2835_clock_data bcm2835_clock_sdram_data = {
779 .name = "sdram",
780 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
781 .parents = bcm2835_clock_vpu_parents,
782 .ctl_reg = CM_SDCCTL,
783 .div_reg = CM_SDCDIV,
784 .int_bits = 6,
785 .frac_bits = 0,
788 /* Clock for the temperature sensor. Generally run at 2Mhz, max 5Mhz. */
789 static const struct bcm2835_clock_data bcm2835_clock_tsens_data = {
790 .name = "tsens",
791 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
792 .parents = bcm2835_clock_osc_parents,
793 .ctl_reg = CM_TSENSCTL,
794 .div_reg = CM_TSENSDIV,
795 .int_bits = 5,
796 .frac_bits = 0,
799 /* Arasan EMMC clock */
800 static const struct bcm2835_clock_data bcm2835_clock_emmc_data = {
801 .name = "emmc",
802 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
803 .parents = bcm2835_clock_per_parents,
804 .ctl_reg = CM_EMMCCTL,
805 .div_reg = CM_EMMCDIV,
806 .int_bits = 4,
807 .frac_bits = 8,
810 struct bcm2835_pll {
811 struct clk_hw hw;
812 struct bcm2835_cprman *cprman;
813 const struct bcm2835_pll_data *data;
816 static int bcm2835_pll_is_on(struct clk_hw *hw)
818 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
819 struct bcm2835_cprman *cprman = pll->cprman;
820 const struct bcm2835_pll_data *data = pll->data;
822 return cprman_read(cprman, data->a2w_ctrl_reg) &
823 A2W_PLL_CTRL_PRST_DISABLE;
826 static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
827 unsigned long parent_rate,
828 u32 *ndiv, u32 *fdiv)
830 u64 div;
832 div = (u64)rate << A2W_PLL_FRAC_BITS;
833 do_div(div, parent_rate);
835 *ndiv = div >> A2W_PLL_FRAC_BITS;
836 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
839 static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
840 u32 ndiv, u32 fdiv, u32 pdiv)
842 u64 rate;
844 if (pdiv == 0)
845 return 0;
847 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
848 do_div(rate, pdiv);
849 return rate >> A2W_PLL_FRAC_BITS;
852 static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
853 unsigned long *parent_rate)
855 u32 ndiv, fdiv;
857 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
859 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
862 static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
863 unsigned long parent_rate)
865 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
866 struct bcm2835_cprman *cprman = pll->cprman;
867 const struct bcm2835_pll_data *data = pll->data;
868 u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
869 u32 ndiv, pdiv, fdiv;
870 bool using_prediv;
872 if (parent_rate == 0)
873 return 0;
875 fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
876 ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
877 pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
878 using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
879 data->ana->fb_prediv_mask;
881 if (using_prediv)
882 ndiv *= 2;
884 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
887 static void bcm2835_pll_off(struct clk_hw *hw)
889 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
890 struct bcm2835_cprman *cprman = pll->cprman;
891 const struct bcm2835_pll_data *data = pll->data;
893 cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);
894 cprman_write(cprman, data->a2w_ctrl_reg, A2W_PLL_CTRL_PWRDN);
897 static int bcm2835_pll_on(struct clk_hw *hw)
899 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
900 struct bcm2835_cprman *cprman = pll->cprman;
901 const struct bcm2835_pll_data *data = pll->data;
902 ktime_t timeout;
904 /* Take the PLL out of reset. */
905 cprman_write(cprman, data->cm_ctrl_reg,
906 cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
908 /* Wait for the PLL to lock. */
909 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
910 while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
911 if (ktime_after(ktime_get(), timeout)) {
912 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
913 clk_hw_get_name(hw));
914 return -ETIMEDOUT;
917 cpu_relax();
920 return 0;
923 static void
924 bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
926 int i;
929 * ANA register setup is done as a series of writes to
930 * ANA3-ANA0, in that order. This lets us write all 4
931 * registers as a single cycle of the serdes interface (taking
932 * 100 xosc clocks), whereas if we were to update ana0, 1, and
933 * 3 individually through their partial-write registers, each
934 * would be their own serdes cycle.
936 for (i = 3; i >= 0; i--)
937 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
940 static int bcm2835_pll_set_rate(struct clk_hw *hw,
941 unsigned long rate, unsigned long parent_rate)
943 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
944 struct bcm2835_cprman *cprman = pll->cprman;
945 const struct bcm2835_pll_data *data = pll->data;
946 bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
947 u32 ndiv, fdiv, a2w_ctl;
948 u32 ana[4];
949 int i;
951 if (rate < data->min_rate || rate > data->max_rate) {
952 dev_err(cprman->dev, "%s: rate out of spec: %lu vs (%lu, %lu)\n",
953 clk_hw_get_name(hw), rate,
954 data->min_rate, data->max_rate);
955 return -EINVAL;
958 if (rate > data->max_fb_rate) {
959 use_fb_prediv = true;
960 rate /= 2;
961 } else {
962 use_fb_prediv = false;
965 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
967 for (i = 3; i >= 0; i--)
968 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
970 was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
972 ana[0] &= ~data->ana->mask0;
973 ana[0] |= data->ana->set0;
974 ana[1] &= ~data->ana->mask1;
975 ana[1] |= data->ana->set1;
976 ana[3] &= ~data->ana->mask3;
977 ana[3] |= data->ana->set3;
979 if (was_using_prediv && !use_fb_prediv) {
980 ana[1] &= ~data->ana->fb_prediv_mask;
981 do_ana_setup_first = true;
982 } else if (!was_using_prediv && use_fb_prediv) {
983 ana[1] |= data->ana->fb_prediv_mask;
984 do_ana_setup_first = false;
985 } else {
986 do_ana_setup_first = true;
989 /* Unmask the reference clock from the oscillator. */
990 cprman_write(cprman, A2W_XOSC_CTRL,
991 cprman_read(cprman, A2W_XOSC_CTRL) |
992 data->reference_enable_mask);
994 if (do_ana_setup_first)
995 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
997 /* Set the PLL multiplier from the oscillator. */
998 cprman_write(cprman, data->frac_reg, fdiv);
1000 a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
1001 a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
1002 a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
1003 a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
1004 a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
1005 cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
1007 if (!do_ana_setup_first)
1008 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
1010 return 0;
1013 static const struct clk_ops bcm2835_pll_clk_ops = {
1014 .is_prepared = bcm2835_pll_is_on,
1015 .prepare = bcm2835_pll_on,
1016 .unprepare = bcm2835_pll_off,
1017 .recalc_rate = bcm2835_pll_get_rate,
1018 .set_rate = bcm2835_pll_set_rate,
1019 .round_rate = bcm2835_pll_round_rate,
1022 struct bcm2835_pll_divider {
1023 struct clk_divider div;
1024 struct bcm2835_cprman *cprman;
1025 const struct bcm2835_pll_divider_data *data;
1028 static struct bcm2835_pll_divider *
1029 bcm2835_pll_divider_from_hw(struct clk_hw *hw)
1031 return container_of(hw, struct bcm2835_pll_divider, div.hw);
1034 static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
1036 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1037 struct bcm2835_cprman *cprman = divider->cprman;
1038 const struct bcm2835_pll_divider_data *data = divider->data;
1040 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
1043 static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
1044 unsigned long rate,
1045 unsigned long *parent_rate)
1047 return clk_divider_ops.round_rate(hw, rate, parent_rate);
1050 static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
1051 unsigned long parent_rate)
1053 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1054 struct bcm2835_cprman *cprman = divider->cprman;
1055 const struct bcm2835_pll_divider_data *data = divider->data;
1056 u32 div = cprman_read(cprman, data->a2w_reg);
1058 div &= (1 << A2W_PLL_DIV_BITS) - 1;
1059 if (div == 0)
1060 div = 256;
1062 return parent_rate / div;
1065 static void bcm2835_pll_divider_off(struct clk_hw *hw)
1067 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1068 struct bcm2835_cprman *cprman = divider->cprman;
1069 const struct bcm2835_pll_divider_data *data = divider->data;
1071 cprman_write(cprman, data->cm_reg,
1072 (cprman_read(cprman, data->cm_reg) &
1073 ~data->load_mask) | data->hold_mask);
1074 cprman_write(cprman, data->a2w_reg, A2W_PLL_CHANNEL_DISABLE);
1077 static int bcm2835_pll_divider_on(struct clk_hw *hw)
1079 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1080 struct bcm2835_cprman *cprman = divider->cprman;
1081 const struct bcm2835_pll_divider_data *data = divider->data;
1083 cprman_write(cprman, data->a2w_reg,
1084 cprman_read(cprman, data->a2w_reg) &
1085 ~A2W_PLL_CHANNEL_DISABLE);
1087 cprman_write(cprman, data->cm_reg,
1088 cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
1090 return 0;
1093 static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
1094 unsigned long rate,
1095 unsigned long parent_rate)
1097 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1098 struct bcm2835_cprman *cprman = divider->cprman;
1099 const struct bcm2835_pll_divider_data *data = divider->data;
1100 u32 cm;
1101 int ret;
1103 ret = clk_divider_ops.set_rate(hw, rate, parent_rate);
1104 if (ret)
1105 return ret;
1107 cm = cprman_read(cprman, data->cm_reg);
1108 cprman_write(cprman, data->cm_reg, cm | data->load_mask);
1109 cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
1111 return 0;
1114 static const struct clk_ops bcm2835_pll_divider_clk_ops = {
1115 .is_prepared = bcm2835_pll_divider_is_on,
1116 .prepare = bcm2835_pll_divider_on,
1117 .unprepare = bcm2835_pll_divider_off,
1118 .recalc_rate = bcm2835_pll_divider_get_rate,
1119 .set_rate = bcm2835_pll_divider_set_rate,
1120 .round_rate = bcm2835_pll_divider_round_rate,
1124 * The CM dividers do fixed-point division, so we can't use the
1125 * generic integer divider code like the PLL dividers do (and we can't
1126 * fake it by having some fixed shifts preceding it in the clock tree,
1127 * because we'd run out of bits in a 32-bit unsigned long).
1129 struct bcm2835_clock {
1130 struct clk_hw hw;
1131 struct bcm2835_cprman *cprman;
1132 const struct bcm2835_clock_data *data;
1135 static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
1137 return container_of(hw, struct bcm2835_clock, hw);
1140 static int bcm2835_clock_is_on(struct clk_hw *hw)
1142 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1143 struct bcm2835_cprman *cprman = clock->cprman;
1144 const struct bcm2835_clock_data *data = clock->data;
1146 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
1149 static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
1150 unsigned long rate,
1151 unsigned long parent_rate)
1153 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1154 const struct bcm2835_clock_data *data = clock->data;
1155 u32 unused_frac_mask = GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0);
1156 u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
1157 u32 div;
1159 do_div(temp, rate);
1160 div = temp;
1162 /* Round and mask off the unused bits */
1163 if (unused_frac_mask != 0) {
1164 div += unused_frac_mask >> 1;
1165 div &= ~unused_frac_mask;
1168 /* Clamp to the limits. */
1169 div = max(div, unused_frac_mask + 1);
1170 div = min_t(u32, div, GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
1171 CM_DIV_FRAC_BITS - data->frac_bits));
1173 return div;
1176 static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
1177 unsigned long parent_rate,
1178 u32 div)
1180 const struct bcm2835_clock_data *data = clock->data;
1181 u64 temp;
1184 * The divisor is a 12.12 fixed point field, but only some of
1185 * the bits are populated in any given clock.
1187 div >>= CM_DIV_FRAC_BITS - data->frac_bits;
1188 div &= (1 << (data->int_bits + data->frac_bits)) - 1;
1190 if (div == 0)
1191 return 0;
1193 temp = (u64)parent_rate << data->frac_bits;
1195 do_div(temp, div);
1197 return temp;
1200 static long bcm2835_clock_round_rate(struct clk_hw *hw,
1201 unsigned long rate,
1202 unsigned long *parent_rate)
1204 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1205 u32 div = bcm2835_clock_choose_div(hw, rate, *parent_rate);
1207 return bcm2835_clock_rate_from_divisor(clock, *parent_rate, div);
1210 static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
1211 unsigned long parent_rate)
1213 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1214 struct bcm2835_cprman *cprman = clock->cprman;
1215 const struct bcm2835_clock_data *data = clock->data;
1216 u32 div = cprman_read(cprman, data->div_reg);
1218 return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
1221 static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
1223 struct bcm2835_cprman *cprman = clock->cprman;
1224 const struct bcm2835_clock_data *data = clock->data;
1225 ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
1227 while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
1228 if (ktime_after(ktime_get(), timeout)) {
1229 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
1230 clk_hw_get_name(&clock->hw));
1231 return;
1233 cpu_relax();
1237 static void bcm2835_clock_off(struct clk_hw *hw)
1239 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1240 struct bcm2835_cprman *cprman = clock->cprman;
1241 const struct bcm2835_clock_data *data = clock->data;
1243 spin_lock(&cprman->regs_lock);
1244 cprman_write(cprman, data->ctl_reg,
1245 cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
1246 spin_unlock(&cprman->regs_lock);
1248 /* BUSY will remain high until the divider completes its cycle. */
1249 bcm2835_clock_wait_busy(clock);
1252 static int bcm2835_clock_on(struct clk_hw *hw)
1254 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1255 struct bcm2835_cprman *cprman = clock->cprman;
1256 const struct bcm2835_clock_data *data = clock->data;
1258 spin_lock(&cprman->regs_lock);
1259 cprman_write(cprman, data->ctl_reg,
1260 cprman_read(cprman, data->ctl_reg) |
1261 CM_ENABLE |
1262 CM_GATE);
1263 spin_unlock(&cprman->regs_lock);
1265 return 0;
1268 static int bcm2835_clock_set_rate(struct clk_hw *hw,
1269 unsigned long rate, unsigned long parent_rate)
1271 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1272 struct bcm2835_cprman *cprman = clock->cprman;
1273 const struct bcm2835_clock_data *data = clock->data;
1274 u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate);
1276 cprman_write(cprman, data->div_reg, div);
1278 return 0;
1281 static const struct clk_ops bcm2835_clock_clk_ops = {
1282 .is_prepared = bcm2835_clock_is_on,
1283 .prepare = bcm2835_clock_on,
1284 .unprepare = bcm2835_clock_off,
1285 .recalc_rate = bcm2835_clock_get_rate,
1286 .set_rate = bcm2835_clock_set_rate,
1287 .round_rate = bcm2835_clock_round_rate,
1290 static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1292 return true;
1296 * The VPU clock can never be disabled (it doesn't have an ENABLE
1297 * bit), so it gets its own set of clock ops.
1299 static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1300 .is_prepared = bcm2835_vpu_clock_is_on,
1301 .recalc_rate = bcm2835_clock_get_rate,
1302 .set_rate = bcm2835_clock_set_rate,
1303 .round_rate = bcm2835_clock_round_rate,
1306 static struct clk *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1307 const struct bcm2835_pll_data *data)
1309 struct bcm2835_pll *pll;
1310 struct clk_init_data init;
1312 memset(&init, 0, sizeof(init));
1314 /* All of the PLLs derive from the external oscillator. */
1315 init.parent_names = &cprman->osc_name;
1316 init.num_parents = 1;
1317 init.name = data->name;
1318 init.ops = &bcm2835_pll_clk_ops;
1319 init.flags = CLK_IGNORE_UNUSED;
1321 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1322 if (!pll)
1323 return NULL;
1325 pll->cprman = cprman;
1326 pll->data = data;
1327 pll->hw.init = &init;
1329 return devm_clk_register(cprman->dev, &pll->hw);
1332 static struct clk *
1333 bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1334 const struct bcm2835_pll_divider_data *data)
1336 struct bcm2835_pll_divider *divider;
1337 struct clk_init_data init;
1338 struct clk *clk;
1339 const char *divider_name;
1341 if (data->fixed_divider != 1) {
1342 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1343 "%s_prediv", data->name);
1344 if (!divider_name)
1345 return NULL;
1346 } else {
1347 divider_name = data->name;
1350 memset(&init, 0, sizeof(init));
1352 init.parent_names = &data->source_pll->name;
1353 init.num_parents = 1;
1354 init.name = divider_name;
1355 init.ops = &bcm2835_pll_divider_clk_ops;
1356 init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
1358 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1359 if (!divider)
1360 return NULL;
1362 divider->div.reg = cprman->regs + data->a2w_reg;
1363 divider->div.shift = A2W_PLL_DIV_SHIFT;
1364 divider->div.width = A2W_PLL_DIV_BITS;
1365 divider->div.flags = 0;
1366 divider->div.lock = &cprman->regs_lock;
1367 divider->div.hw.init = &init;
1368 divider->div.table = NULL;
1370 divider->cprman = cprman;
1371 divider->data = data;
1373 clk = devm_clk_register(cprman->dev, &divider->div.hw);
1374 if (IS_ERR(clk))
1375 return clk;
1378 * PLLH's channels have a fixed divide by 10 afterwards, which
1379 * is what our consumers are actually using.
1381 if (data->fixed_divider != 1) {
1382 return clk_register_fixed_factor(cprman->dev, data->name,
1383 divider_name,
1384 CLK_SET_RATE_PARENT,
1386 data->fixed_divider);
1389 return clk;
1392 static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman,
1393 const struct bcm2835_clock_data *data)
1395 struct bcm2835_clock *clock;
1396 struct clk_init_data init;
1397 const char *parent;
1400 * Most of the clock generators have a mux field, so we
1401 * instantiate a generic mux as our parent to handle it.
1403 if (data->num_mux_parents) {
1404 const char *parents[1 << CM_SRC_BITS];
1405 int i;
1407 parent = devm_kasprintf(cprman->dev, GFP_KERNEL,
1408 "mux_%s", data->name);
1409 if (!parent)
1410 return NULL;
1413 * Replace our "xosc" references with the oscillator's
1414 * actual name.
1416 for (i = 0; i < data->num_mux_parents; i++) {
1417 if (strcmp(data->parents[i], "xosc") == 0)
1418 parents[i] = cprman->osc_name;
1419 else
1420 parents[i] = data->parents[i];
1423 clk_register_mux(cprman->dev, parent,
1424 parents, data->num_mux_parents,
1425 CLK_SET_RATE_PARENT,
1426 cprman->regs + data->ctl_reg,
1427 CM_SRC_SHIFT, CM_SRC_BITS,
1428 0, &cprman->regs_lock);
1429 } else {
1430 parent = data->parents[0];
1433 memset(&init, 0, sizeof(init));
1434 init.parent_names = &parent;
1435 init.num_parents = 1;
1436 init.name = data->name;
1437 init.flags = CLK_IGNORE_UNUSED;
1439 if (data->is_vpu_clock) {
1440 init.ops = &bcm2835_vpu_clock_clk_ops;
1441 } else {
1442 init.ops = &bcm2835_clock_clk_ops;
1443 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
1446 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1447 if (!clock)
1448 return NULL;
1450 clock->cprman = cprman;
1451 clock->data = data;
1452 clock->hw.init = &init;
1454 return devm_clk_register(cprman->dev, &clock->hw);
1457 static int bcm2835_clk_probe(struct platform_device *pdev)
1459 struct device *dev = &pdev->dev;
1460 struct clk **clks;
1461 struct bcm2835_cprman *cprman;
1462 struct resource *res;
1464 cprman = devm_kzalloc(dev, sizeof(*cprman), GFP_KERNEL);
1465 if (!cprman)
1466 return -ENOMEM;
1468 spin_lock_init(&cprman->regs_lock);
1469 cprman->dev = dev;
1470 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1471 cprman->regs = devm_ioremap_resource(dev, res);
1472 if (IS_ERR(cprman->regs))
1473 return PTR_ERR(cprman->regs);
1475 cprman->osc_name = of_clk_get_parent_name(dev->of_node, 0);
1476 if (!cprman->osc_name)
1477 return -ENODEV;
1479 platform_set_drvdata(pdev, cprman);
1481 cprman->onecell.clk_num = BCM2835_CLOCK_COUNT;
1482 cprman->onecell.clks = cprman->clks;
1483 clks = cprman->clks;
1485 clks[BCM2835_PLLA] = bcm2835_register_pll(cprman, &bcm2835_plla_data);
1486 clks[BCM2835_PLLB] = bcm2835_register_pll(cprman, &bcm2835_pllb_data);
1487 clks[BCM2835_PLLC] = bcm2835_register_pll(cprman, &bcm2835_pllc_data);
1488 clks[BCM2835_PLLD] = bcm2835_register_pll(cprman, &bcm2835_plld_data);
1489 clks[BCM2835_PLLH] = bcm2835_register_pll(cprman, &bcm2835_pllh_data);
1491 clks[BCM2835_PLLA_CORE] =
1492 bcm2835_register_pll_divider(cprman, &bcm2835_plla_core_data);
1493 clks[BCM2835_PLLA_PER] =
1494 bcm2835_register_pll_divider(cprman, &bcm2835_plla_per_data);
1495 clks[BCM2835_PLLC_CORE0] =
1496 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core0_data);
1497 clks[BCM2835_PLLC_CORE1] =
1498 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core1_data);
1499 clks[BCM2835_PLLC_CORE2] =
1500 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core2_data);
1501 clks[BCM2835_PLLC_PER] =
1502 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_per_data);
1503 clks[BCM2835_PLLD_CORE] =
1504 bcm2835_register_pll_divider(cprman, &bcm2835_plld_core_data);
1505 clks[BCM2835_PLLD_PER] =
1506 bcm2835_register_pll_divider(cprman, &bcm2835_plld_per_data);
1507 clks[BCM2835_PLLH_RCAL] =
1508 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_rcal_data);
1509 clks[BCM2835_PLLH_AUX] =
1510 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_aux_data);
1511 clks[BCM2835_PLLH_PIX] =
1512 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_pix_data);
1514 clks[BCM2835_CLOCK_TIMER] =
1515 bcm2835_register_clock(cprman, &bcm2835_clock_timer_data);
1516 clks[BCM2835_CLOCK_OTP] =
1517 bcm2835_register_clock(cprman, &bcm2835_clock_otp_data);
1518 clks[BCM2835_CLOCK_TSENS] =
1519 bcm2835_register_clock(cprman, &bcm2835_clock_tsens_data);
1520 clks[BCM2835_CLOCK_VPU] =
1521 bcm2835_register_clock(cprman, &bcm2835_clock_vpu_data);
1522 clks[BCM2835_CLOCK_V3D] =
1523 bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data);
1524 clks[BCM2835_CLOCK_ISP] =
1525 bcm2835_register_clock(cprman, &bcm2835_clock_isp_data);
1526 clks[BCM2835_CLOCK_H264] =
1527 bcm2835_register_clock(cprman, &bcm2835_clock_h264_data);
1528 clks[BCM2835_CLOCK_V3D] =
1529 bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data);
1530 clks[BCM2835_CLOCK_SDRAM] =
1531 bcm2835_register_clock(cprman, &bcm2835_clock_sdram_data);
1532 clks[BCM2835_CLOCK_UART] =
1533 bcm2835_register_clock(cprman, &bcm2835_clock_uart_data);
1534 clks[BCM2835_CLOCK_VEC] =
1535 bcm2835_register_clock(cprman, &bcm2835_clock_vec_data);
1536 clks[BCM2835_CLOCK_HSM] =
1537 bcm2835_register_clock(cprman, &bcm2835_clock_hsm_data);
1538 clks[BCM2835_CLOCK_EMMC] =
1539 bcm2835_register_clock(cprman, &bcm2835_clock_emmc_data);
1542 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
1543 * you have the debug bit set in the power manager, which we
1544 * don't bother exposing) are individual gates off of the
1545 * non-stop vpu clock.
1547 clks[BCM2835_CLOCK_PERI_IMAGE] =
1548 clk_register_gate(dev, "peri_image", "vpu",
1549 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1550 cprman->regs + CM_PERIICTL, CM_GATE_BIT,
1551 0, &cprman->regs_lock);
1553 return of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1554 &cprman->onecell);
1557 static const struct of_device_id bcm2835_clk_of_match[] = {
1558 { .compatible = "brcm,bcm2835-cprman", },
1561 MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
1563 static struct platform_driver bcm2835_clk_driver = {
1564 .driver = {
1565 .name = "bcm2835-clk",
1566 .of_match_table = bcm2835_clk_of_match,
1568 .probe = bcm2835_clk_probe,
1571 builtin_platform_driver(bcm2835_clk_driver);
1573 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
1574 MODULE_DESCRIPTION("BCM2835 clock driver");
1575 MODULE_LICENSE("GPL v2");