]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/clk/bcm/clk-bcm2835.c
185a4d859638c79455ca87464bd458c745285f7d
[mirror_ubuntu-artful-kernel.git] / drivers / clk / bcm / clk-bcm2835.c
1 /*
2 * Copyright (C) 2010,2015 Broadcom
3 * Copyright (C) 2012 Stephen Warren
4 *
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.
9 *
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.
14 *
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
18 */
19
20 /**
21 * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
22 *
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.
31 *
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).
38 */
39
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>
48
49 #define CM_PASSWORD 0x5a000000
50
51 #define CM_GNRICCTL 0x000
52 #define CM_GNRICDIV 0x004
53 # define CM_DIV_FRAC_BITS 12
54
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
110
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
135
136 #define CM_OSCCOUNT 0x100
137
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)
148
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)
158
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)
168
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)
173
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)
180
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
188
189 #define CM_PLLB 0x170
190 # define CM_PLLB_HOLDARM BIT(1)
191 # define CM_PLLB_LOADARM BIT(0)
192
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
204
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
210
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)
217
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)
226
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)
236
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
244
245 #define A2W_PLL_CHANNEL_DISABLE BIT(8)
246 #define A2W_PLL_DIV_BITS 8
247 #define A2W_PLL_DIV_SHIFT 0
248
249 #define A2W_PLLA_DSI0 0x1300
250 #define A2W_PLLA_CORE 0x1400
251 #define A2W_PLLA_PER 0x1500
252 #define A2W_PLLA_CCP2 0x1600
253
254 #define A2W_PLLC_CORE2 0x1320
255 #define A2W_PLLC_CORE1 0x1420
256 #define A2W_PLLC_PER 0x1520
257 #define A2W_PLLC_CORE0 0x1620
258
259 #define A2W_PLLD_DSI0 0x1340
260 #define A2W_PLLD_CORE 0x1440
261 #define A2W_PLLD_PER 0x1540
262 #define A2W_PLLD_DSI1 0x1640
263
264 #define A2W_PLLH_AUX 0x1360
265 #define A2W_PLLH_RCAL 0x1460
266 #define A2W_PLLH_PIX 0x1560
267 #define A2W_PLLH_STS 0x1660
268
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
275
276 #define A2W_PLLB_ARM 0x13e0
277 #define A2W_PLLB_SP0 0x14e0
278 #define A2W_PLLB_SP1 0x15e0
279 #define A2W_PLLB_SP2 0x16e0
280
281 #define LOCK_TIMEOUT_NS 100000000
282 #define BCM2835_MAX_FB_RATE 1750000000u
283
284 struct bcm2835_cprman {
285 struct device *dev;
286 void __iomem *regs;
287 spinlock_t regs_lock;
288 const char *osc_name;
289
290 struct clk_onecell_data onecell;
291 struct clk *clks[BCM2835_CLOCK_COUNT];
292 };
293
294 static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
295 {
296 writel(CM_PASSWORD | val, cprman->regs + reg);
297 }
298
299 static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
300 {
301 return readl(cprman->regs + reg);
302 }
303
304 /*
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.
308 */
309 void __init bcm2835_init_clocks(void)
310 {
311 struct clk *clk;
312 int ret;
313
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");
318
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");
326
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");
334 }
335
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;
345
346 const struct bcm2835_pll_ana_bits *ana;
347
348 unsigned long min_rate;
349 unsigned long max_rate;
350 /*
351 * Highest rate for the VCO before we have to use the
352 * pre-divide-by-2.
353 */
354 unsigned long max_fb_rate;
355 };
356
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;
365 };
366
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),
375 };
376
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),
385 };
386
387 /*
388 * PLLA is the auxiliary PLL, used to drive the CCP2 (Compact Camera
389 * Port 2) transmitter clock.
390 *
391 * It is in the PX LDO power domain, which is on when the AUDIO domain
392 * is on.
393 */
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,
402
403 .ana = &bcm2835_ana_default,
404
405 .min_rate = 600000000u,
406 .max_rate = 2400000000u,
407 .max_fb_rate = BCM2835_MAX_FB_RATE,
408 };
409
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,
419
420 .ana = &bcm2835_ana_default,
421
422 .min_rate = 600000000u,
423 .max_rate = 3000000000u,
424 .max_fb_rate = BCM2835_MAX_FB_RATE,
425 };
426
427 /*
428 * PLLC is the core PLL, used to drive the core VPU clock.
429 *
430 * It is in the PX LDO power domain, which is on when the AUDIO domain
431 * is on.
432 */
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,
441
442 .ana = &bcm2835_ana_default,
443
444 .min_rate = 600000000u,
445 .max_rate = 3000000000u,
446 .max_fb_rate = BCM2835_MAX_FB_RATE,
447 };
448
449 /*
450 * PLLD is the display PLL, used to drive DSI display panels.
451 *
452 * It is in the PX LDO power domain, which is on when the AUDIO domain
453 * is on.
454 */
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,
463
464 .ana = &bcm2835_ana_default,
465
466 .min_rate = 600000000u,
467 .max_rate = 2400000000u,
468 .max_fb_rate = BCM2835_MAX_FB_RATE,
469 };
470
471 /*
472 * PLLH is used to supply the pixel clock or the AUX clock for the TV
473 * encoder.
474 *
475 * It is in the HDMI power domain.
476 */
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,
485
486 .ana = &bcm2835_ana_pllh,
487
488 .min_rate = 600000000u,
489 .max_rate = 3000000000u,
490 .max_fb_rate = BCM2835_MAX_FB_RATE,
491 };
492
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;
498
499 u32 load_mask;
500 u32 hold_mask;
501 u32 fixed_divider;
502 };
503
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,
512 };
513
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,
522 };
523
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,
532 };
533
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,
542 };
543
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,
550 };
551
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,
560 };
561
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,
570 };
571
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,
580 };
581
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,
590 };
591
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,
600 };
601
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,
610 };
611
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,
620 };
621
622 struct bcm2835_clock_data {
623 const char *name;
624
625 const char *const *parents;
626 int num_mux_parents;
627
628 u32 ctl_reg;
629 u32 div_reg;
630
631 /* Number of integer bits in the divider */
632 u32 int_bits;
633 /* Number of fractional bits in the divider */
634 u32 frac_bits;
635
636 bool is_vpu_clock;
637 };
638
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",
648 };
649
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",
661 };
662
663 static const char *const bcm2835_clock_osc_parents[] = {
664 "gnd",
665 "xosc",
666 "testdebug0",
667 "testdebug1"
668 };
669
670 /*
671 * Used for a 1Mhz clock for the system clocksource, and also used by
672 * the watchdog timer and the camera pulse generator.
673 */
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,
682 };
683
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,
693 };
694
695 /*
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.
700 */
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,
710 };
711
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,
720 };
721
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,
730 };
731
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,
740 };
741
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,
751 };
752
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,
761 };
762
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,
772 };
773
774 /*
775 * Secondary SDRAM clock. Used for low-voltage modes when the PLL in
776 * the SDRAM controller can't be used.
777 */
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,
786 };
787
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,
797 };
798
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,
808 };
809
810 struct bcm2835_pll {
811 struct clk_hw hw;
812 struct bcm2835_cprman *cprman;
813 const struct bcm2835_pll_data *data;
814 };
815
816 static int bcm2835_pll_is_on(struct clk_hw *hw)
817 {
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;
821
822 return cprman_read(cprman, data->a2w_ctrl_reg) &
823 A2W_PLL_CTRL_PRST_DISABLE;
824 }
825
826 static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
827 unsigned long parent_rate,
828 u32 *ndiv, u32 *fdiv)
829 {
830 u64 div;
831
832 div = (u64)rate << A2W_PLL_FRAC_BITS;
833 do_div(div, parent_rate);
834
835 *ndiv = div >> A2W_PLL_FRAC_BITS;
836 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
837 }
838
839 static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
840 u32 ndiv, u32 fdiv, u32 pdiv)
841 {
842 u64 rate;
843
844 if (pdiv == 0)
845 return 0;
846
847 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
848 do_div(rate, pdiv);
849 return rate >> A2W_PLL_FRAC_BITS;
850 }
851
852 static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
853 unsigned long *parent_rate)
854 {
855 u32 ndiv, fdiv;
856
857 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
858
859 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
860 }
861
862 static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
863 unsigned long parent_rate)
864 {
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;
871
872 if (parent_rate == 0)
873 return 0;
874
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;
880
881 if (using_prediv)
882 ndiv *= 2;
883
884 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
885 }
886
887 static void bcm2835_pll_off(struct clk_hw *hw)
888 {
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;
892
893 cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);
894 cprman_write(cprman, data->a2w_ctrl_reg, A2W_PLL_CTRL_PWRDN);
895 }
896
897 static int bcm2835_pll_on(struct clk_hw *hw)
898 {
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;
903
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);
907
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;
915 }
916
917 cpu_relax();
918 }
919
920 return 0;
921 }
922
923 static void
924 bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
925 {
926 int i;
927
928 /*
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.
935 */
936 for (i = 3; i >= 0; i--)
937 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
938 }
939
940 static int bcm2835_pll_set_rate(struct clk_hw *hw,
941 unsigned long rate, unsigned long parent_rate)
942 {
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;
950
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;
956 }
957
958 if (rate > data->max_fb_rate) {
959 use_fb_prediv = true;
960 rate /= 2;
961 } else {
962 use_fb_prediv = false;
963 }
964
965 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
966
967 for (i = 3; i >= 0; i--)
968 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
969
970 was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
971
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;
978
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;
987 }
988
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);
993
994 if (do_ana_setup_first)
995 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
996
997 /* Set the PLL multiplier from the oscillator. */
998 cprman_write(cprman, data->frac_reg, fdiv);
999
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);
1006
1007 if (!do_ana_setup_first)
1008 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
1009
1010 return 0;
1011 }
1012
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,
1020 };
1021
1022 struct bcm2835_pll_divider {
1023 struct clk_divider div;
1024 struct bcm2835_cprman *cprman;
1025 const struct bcm2835_pll_divider_data *data;
1026 };
1027
1028 static struct bcm2835_pll_divider *
1029 bcm2835_pll_divider_from_hw(struct clk_hw *hw)
1030 {
1031 return container_of(hw, struct bcm2835_pll_divider, div.hw);
1032 }
1033
1034 static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
1035 {
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;
1039
1040 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
1041 }
1042
1043 static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
1044 unsigned long rate,
1045 unsigned long *parent_rate)
1046 {
1047 return clk_divider_ops.round_rate(hw, rate, parent_rate);
1048 }
1049
1050 static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
1051 unsigned long parent_rate)
1052 {
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);
1057
1058 div &= (1 << A2W_PLL_DIV_BITS) - 1;
1059 if (div == 0)
1060 div = 256;
1061
1062 return parent_rate / div;
1063 }
1064
1065 static void bcm2835_pll_divider_off(struct clk_hw *hw)
1066 {
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;
1070
1071 spin_lock(&cprman->regs_lock);
1072 cprman_write(cprman, data->cm_reg,
1073 (cprman_read(cprman, data->cm_reg) &
1074 ~data->load_mask) | data->hold_mask);
1075 cprman_write(cprman, data->a2w_reg, A2W_PLL_CHANNEL_DISABLE);
1076 spin_unlock(&cprman->regs_lock);
1077 }
1078
1079 static int bcm2835_pll_divider_on(struct clk_hw *hw)
1080 {
1081 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1082 struct bcm2835_cprman *cprman = divider->cprman;
1083 const struct bcm2835_pll_divider_data *data = divider->data;
1084
1085 spin_lock(&cprman->regs_lock);
1086 cprman_write(cprman, data->a2w_reg,
1087 cprman_read(cprman, data->a2w_reg) &
1088 ~A2W_PLL_CHANNEL_DISABLE);
1089
1090 cprman_write(cprman, data->cm_reg,
1091 cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
1092 spin_unlock(&cprman->regs_lock);
1093
1094 return 0;
1095 }
1096
1097 static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
1098 unsigned long rate,
1099 unsigned long parent_rate)
1100 {
1101 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1102 struct bcm2835_cprman *cprman = divider->cprman;
1103 const struct bcm2835_pll_divider_data *data = divider->data;
1104 u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
1105
1106 div = DIV_ROUND_UP_ULL(parent_rate, rate);
1107
1108 div = min(div, max_div);
1109 if (div == max_div)
1110 div = 0;
1111
1112 cprman_write(cprman, data->a2w_reg, div);
1113 cm = cprman_read(cprman, data->cm_reg);
1114 cprman_write(cprman, data->cm_reg, cm | data->load_mask);
1115 cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
1116
1117 return 0;
1118 }
1119
1120 static const struct clk_ops bcm2835_pll_divider_clk_ops = {
1121 .is_prepared = bcm2835_pll_divider_is_on,
1122 .prepare = bcm2835_pll_divider_on,
1123 .unprepare = bcm2835_pll_divider_off,
1124 .recalc_rate = bcm2835_pll_divider_get_rate,
1125 .set_rate = bcm2835_pll_divider_set_rate,
1126 .round_rate = bcm2835_pll_divider_round_rate,
1127 };
1128
1129 /*
1130 * The CM dividers do fixed-point division, so we can't use the
1131 * generic integer divider code like the PLL dividers do (and we can't
1132 * fake it by having some fixed shifts preceding it in the clock tree,
1133 * because we'd run out of bits in a 32-bit unsigned long).
1134 */
1135 struct bcm2835_clock {
1136 struct clk_hw hw;
1137 struct bcm2835_cprman *cprman;
1138 const struct bcm2835_clock_data *data;
1139 };
1140
1141 static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
1142 {
1143 return container_of(hw, struct bcm2835_clock, hw);
1144 }
1145
1146 static int bcm2835_clock_is_on(struct clk_hw *hw)
1147 {
1148 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1149 struct bcm2835_cprman *cprman = clock->cprman;
1150 const struct bcm2835_clock_data *data = clock->data;
1151
1152 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
1153 }
1154
1155 static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
1156 unsigned long rate,
1157 unsigned long parent_rate)
1158 {
1159 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1160 const struct bcm2835_clock_data *data = clock->data;
1161 u32 unused_frac_mask = GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0);
1162 u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
1163 u32 div;
1164
1165 do_div(temp, rate);
1166 div = temp;
1167
1168 /* Round and mask off the unused bits */
1169 if (unused_frac_mask != 0) {
1170 div += unused_frac_mask >> 1;
1171 div &= ~unused_frac_mask;
1172 }
1173
1174 /* Clamp to the limits. */
1175 div = max(div, unused_frac_mask + 1);
1176 div = min_t(u32, div, GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
1177 CM_DIV_FRAC_BITS - data->frac_bits));
1178
1179 return div;
1180 }
1181
1182 static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
1183 unsigned long parent_rate,
1184 u32 div)
1185 {
1186 const struct bcm2835_clock_data *data = clock->data;
1187 u64 temp;
1188
1189 /*
1190 * The divisor is a 12.12 fixed point field, but only some of
1191 * the bits are populated in any given clock.
1192 */
1193 div >>= CM_DIV_FRAC_BITS - data->frac_bits;
1194 div &= (1 << (data->int_bits + data->frac_bits)) - 1;
1195
1196 if (div == 0)
1197 return 0;
1198
1199 temp = (u64)parent_rate << data->frac_bits;
1200
1201 do_div(temp, div);
1202
1203 return temp;
1204 }
1205
1206 static long bcm2835_clock_round_rate(struct clk_hw *hw,
1207 unsigned long rate,
1208 unsigned long *parent_rate)
1209 {
1210 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1211 u32 div = bcm2835_clock_choose_div(hw, rate, *parent_rate);
1212
1213 return bcm2835_clock_rate_from_divisor(clock, *parent_rate, div);
1214 }
1215
1216 static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
1217 unsigned long parent_rate)
1218 {
1219 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1220 struct bcm2835_cprman *cprman = clock->cprman;
1221 const struct bcm2835_clock_data *data = clock->data;
1222 u32 div = cprman_read(cprman, data->div_reg);
1223
1224 return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
1225 }
1226
1227 static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
1228 {
1229 struct bcm2835_cprman *cprman = clock->cprman;
1230 const struct bcm2835_clock_data *data = clock->data;
1231 ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
1232
1233 while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
1234 if (ktime_after(ktime_get(), timeout)) {
1235 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
1236 clk_hw_get_name(&clock->hw));
1237 return;
1238 }
1239 cpu_relax();
1240 }
1241 }
1242
1243 static void bcm2835_clock_off(struct clk_hw *hw)
1244 {
1245 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1246 struct bcm2835_cprman *cprman = clock->cprman;
1247 const struct bcm2835_clock_data *data = clock->data;
1248
1249 spin_lock(&cprman->regs_lock);
1250 cprman_write(cprman, data->ctl_reg,
1251 cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
1252 spin_unlock(&cprman->regs_lock);
1253
1254 /* BUSY will remain high until the divider completes its cycle. */
1255 bcm2835_clock_wait_busy(clock);
1256 }
1257
1258 static int bcm2835_clock_on(struct clk_hw *hw)
1259 {
1260 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1261 struct bcm2835_cprman *cprman = clock->cprman;
1262 const struct bcm2835_clock_data *data = clock->data;
1263
1264 spin_lock(&cprman->regs_lock);
1265 cprman_write(cprman, data->ctl_reg,
1266 cprman_read(cprman, data->ctl_reg) |
1267 CM_ENABLE |
1268 CM_GATE);
1269 spin_unlock(&cprman->regs_lock);
1270
1271 return 0;
1272 }
1273
1274 static int bcm2835_clock_set_rate(struct clk_hw *hw,
1275 unsigned long rate, unsigned long parent_rate)
1276 {
1277 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1278 struct bcm2835_cprman *cprman = clock->cprman;
1279 const struct bcm2835_clock_data *data = clock->data;
1280 u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate);
1281
1282 cprman_write(cprman, data->div_reg, div);
1283
1284 return 0;
1285 }
1286
1287 static const struct clk_ops bcm2835_clock_clk_ops = {
1288 .is_prepared = bcm2835_clock_is_on,
1289 .prepare = bcm2835_clock_on,
1290 .unprepare = bcm2835_clock_off,
1291 .recalc_rate = bcm2835_clock_get_rate,
1292 .set_rate = bcm2835_clock_set_rate,
1293 .round_rate = bcm2835_clock_round_rate,
1294 };
1295
1296 static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1297 {
1298 return true;
1299 }
1300
1301 /*
1302 * The VPU clock can never be disabled (it doesn't have an ENABLE
1303 * bit), so it gets its own set of clock ops.
1304 */
1305 static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1306 .is_prepared = bcm2835_vpu_clock_is_on,
1307 .recalc_rate = bcm2835_clock_get_rate,
1308 .set_rate = bcm2835_clock_set_rate,
1309 .round_rate = bcm2835_clock_round_rate,
1310 };
1311
1312 static struct clk *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1313 const struct bcm2835_pll_data *data)
1314 {
1315 struct bcm2835_pll *pll;
1316 struct clk_init_data init;
1317
1318 memset(&init, 0, sizeof(init));
1319
1320 /* All of the PLLs derive from the external oscillator. */
1321 init.parent_names = &cprman->osc_name;
1322 init.num_parents = 1;
1323 init.name = data->name;
1324 init.ops = &bcm2835_pll_clk_ops;
1325 init.flags = CLK_IGNORE_UNUSED;
1326
1327 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1328 if (!pll)
1329 return NULL;
1330
1331 pll->cprman = cprman;
1332 pll->data = data;
1333 pll->hw.init = &init;
1334
1335 return devm_clk_register(cprman->dev, &pll->hw);
1336 }
1337
1338 static struct clk *
1339 bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1340 const struct bcm2835_pll_divider_data *data)
1341 {
1342 struct bcm2835_pll_divider *divider;
1343 struct clk_init_data init;
1344 struct clk *clk;
1345 const char *divider_name;
1346
1347 if (data->fixed_divider != 1) {
1348 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1349 "%s_prediv", data->name);
1350 if (!divider_name)
1351 return NULL;
1352 } else {
1353 divider_name = data->name;
1354 }
1355
1356 memset(&init, 0, sizeof(init));
1357
1358 init.parent_names = &data->source_pll->name;
1359 init.num_parents = 1;
1360 init.name = divider_name;
1361 init.ops = &bcm2835_pll_divider_clk_ops;
1362 init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
1363
1364 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1365 if (!divider)
1366 return NULL;
1367
1368 divider->div.reg = cprman->regs + data->a2w_reg;
1369 divider->div.shift = A2W_PLL_DIV_SHIFT;
1370 divider->div.width = A2W_PLL_DIV_BITS;
1371 divider->div.flags = 0;
1372 divider->div.lock = &cprman->regs_lock;
1373 divider->div.hw.init = &init;
1374 divider->div.table = NULL;
1375
1376 divider->cprman = cprman;
1377 divider->data = data;
1378
1379 clk = devm_clk_register(cprman->dev, &divider->div.hw);
1380 if (IS_ERR(clk))
1381 return clk;
1382
1383 /*
1384 * PLLH's channels have a fixed divide by 10 afterwards, which
1385 * is what our consumers are actually using.
1386 */
1387 if (data->fixed_divider != 1) {
1388 return clk_register_fixed_factor(cprman->dev, data->name,
1389 divider_name,
1390 CLK_SET_RATE_PARENT,
1391 1,
1392 data->fixed_divider);
1393 }
1394
1395 return clk;
1396 }
1397
1398 static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman,
1399 const struct bcm2835_clock_data *data)
1400 {
1401 struct bcm2835_clock *clock;
1402 struct clk_init_data init;
1403 const char *parent;
1404
1405 /*
1406 * Most of the clock generators have a mux field, so we
1407 * instantiate a generic mux as our parent to handle it.
1408 */
1409 if (data->num_mux_parents) {
1410 const char *parents[1 << CM_SRC_BITS];
1411 int i;
1412
1413 parent = devm_kasprintf(cprman->dev, GFP_KERNEL,
1414 "mux_%s", data->name);
1415 if (!parent)
1416 return NULL;
1417
1418 /*
1419 * Replace our "xosc" references with the oscillator's
1420 * actual name.
1421 */
1422 for (i = 0; i < data->num_mux_parents; i++) {
1423 if (strcmp(data->parents[i], "xosc") == 0)
1424 parents[i] = cprman->osc_name;
1425 else
1426 parents[i] = data->parents[i];
1427 }
1428
1429 clk_register_mux(cprman->dev, parent,
1430 parents, data->num_mux_parents,
1431 CLK_SET_RATE_PARENT,
1432 cprman->regs + data->ctl_reg,
1433 CM_SRC_SHIFT, CM_SRC_BITS,
1434 0, &cprman->regs_lock);
1435 } else {
1436 parent = data->parents[0];
1437 }
1438
1439 memset(&init, 0, sizeof(init));
1440 init.parent_names = &parent;
1441 init.num_parents = 1;
1442 init.name = data->name;
1443 init.flags = CLK_IGNORE_UNUSED;
1444
1445 if (data->is_vpu_clock) {
1446 init.ops = &bcm2835_vpu_clock_clk_ops;
1447 } else {
1448 init.ops = &bcm2835_clock_clk_ops;
1449 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
1450 }
1451
1452 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1453 if (!clock)
1454 return NULL;
1455
1456 clock->cprman = cprman;
1457 clock->data = data;
1458 clock->hw.init = &init;
1459
1460 return devm_clk_register(cprman->dev, &clock->hw);
1461 }
1462
1463 static int bcm2835_clk_probe(struct platform_device *pdev)
1464 {
1465 struct device *dev = &pdev->dev;
1466 struct clk **clks;
1467 struct bcm2835_cprman *cprman;
1468 struct resource *res;
1469
1470 cprman = devm_kzalloc(dev, sizeof(*cprman), GFP_KERNEL);
1471 if (!cprman)
1472 return -ENOMEM;
1473
1474 spin_lock_init(&cprman->regs_lock);
1475 cprman->dev = dev;
1476 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1477 cprman->regs = devm_ioremap_resource(dev, res);
1478 if (IS_ERR(cprman->regs))
1479 return PTR_ERR(cprman->regs);
1480
1481 cprman->osc_name = of_clk_get_parent_name(dev->of_node, 0);
1482 if (!cprman->osc_name)
1483 return -ENODEV;
1484
1485 platform_set_drvdata(pdev, cprman);
1486
1487 cprman->onecell.clk_num = BCM2835_CLOCK_COUNT;
1488 cprman->onecell.clks = cprman->clks;
1489 clks = cprman->clks;
1490
1491 clks[BCM2835_PLLA] = bcm2835_register_pll(cprman, &bcm2835_plla_data);
1492 clks[BCM2835_PLLB] = bcm2835_register_pll(cprman, &bcm2835_pllb_data);
1493 clks[BCM2835_PLLC] = bcm2835_register_pll(cprman, &bcm2835_pllc_data);
1494 clks[BCM2835_PLLD] = bcm2835_register_pll(cprman, &bcm2835_plld_data);
1495 clks[BCM2835_PLLH] = bcm2835_register_pll(cprman, &bcm2835_pllh_data);
1496
1497 clks[BCM2835_PLLA_CORE] =
1498 bcm2835_register_pll_divider(cprman, &bcm2835_plla_core_data);
1499 clks[BCM2835_PLLA_PER] =
1500 bcm2835_register_pll_divider(cprman, &bcm2835_plla_per_data);
1501 clks[BCM2835_PLLC_CORE0] =
1502 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core0_data);
1503 clks[BCM2835_PLLC_CORE1] =
1504 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core1_data);
1505 clks[BCM2835_PLLC_CORE2] =
1506 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core2_data);
1507 clks[BCM2835_PLLC_PER] =
1508 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_per_data);
1509 clks[BCM2835_PLLD_CORE] =
1510 bcm2835_register_pll_divider(cprman, &bcm2835_plld_core_data);
1511 clks[BCM2835_PLLD_PER] =
1512 bcm2835_register_pll_divider(cprman, &bcm2835_plld_per_data);
1513 clks[BCM2835_PLLH_RCAL] =
1514 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_rcal_data);
1515 clks[BCM2835_PLLH_AUX] =
1516 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_aux_data);
1517 clks[BCM2835_PLLH_PIX] =
1518 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_pix_data);
1519
1520 clks[BCM2835_CLOCK_TIMER] =
1521 bcm2835_register_clock(cprman, &bcm2835_clock_timer_data);
1522 clks[BCM2835_CLOCK_OTP] =
1523 bcm2835_register_clock(cprman, &bcm2835_clock_otp_data);
1524 clks[BCM2835_CLOCK_TSENS] =
1525 bcm2835_register_clock(cprman, &bcm2835_clock_tsens_data);
1526 clks[BCM2835_CLOCK_VPU] =
1527 bcm2835_register_clock(cprman, &bcm2835_clock_vpu_data);
1528 clks[BCM2835_CLOCK_V3D] =
1529 bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data);
1530 clks[BCM2835_CLOCK_ISP] =
1531 bcm2835_register_clock(cprman, &bcm2835_clock_isp_data);
1532 clks[BCM2835_CLOCK_H264] =
1533 bcm2835_register_clock(cprman, &bcm2835_clock_h264_data);
1534 clks[BCM2835_CLOCK_V3D] =
1535 bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data);
1536 clks[BCM2835_CLOCK_SDRAM] =
1537 bcm2835_register_clock(cprman, &bcm2835_clock_sdram_data);
1538 clks[BCM2835_CLOCK_UART] =
1539 bcm2835_register_clock(cprman, &bcm2835_clock_uart_data);
1540 clks[BCM2835_CLOCK_VEC] =
1541 bcm2835_register_clock(cprman, &bcm2835_clock_vec_data);
1542 clks[BCM2835_CLOCK_HSM] =
1543 bcm2835_register_clock(cprman, &bcm2835_clock_hsm_data);
1544 clks[BCM2835_CLOCK_EMMC] =
1545 bcm2835_register_clock(cprman, &bcm2835_clock_emmc_data);
1546
1547 /*
1548 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
1549 * you have the debug bit set in the power manager, which we
1550 * don't bother exposing) are individual gates off of the
1551 * non-stop vpu clock.
1552 */
1553 clks[BCM2835_CLOCK_PERI_IMAGE] =
1554 clk_register_gate(dev, "peri_image", "vpu",
1555 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1556 cprman->regs + CM_PERIICTL, CM_GATE_BIT,
1557 0, &cprman->regs_lock);
1558
1559 return of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1560 &cprman->onecell);
1561 }
1562
1563 static const struct of_device_id bcm2835_clk_of_match[] = {
1564 { .compatible = "brcm,bcm2835-cprman", },
1565 {}
1566 };
1567 MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
1568
1569 static struct platform_driver bcm2835_clk_driver = {
1570 .driver = {
1571 .name = "bcm2835-clk",
1572 .of_match_table = bcm2835_clk_of_match,
1573 },
1574 .probe = bcm2835_clk_probe,
1575 };
1576
1577 builtin_platform_driver(bcm2835_clk_driver);
1578
1579 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
1580 MODULE_DESCRIPTION("BCM2835 clock driver");
1581 MODULE_LICENSE("GPL v2");