]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/clk/bcm/clk-bcm2835.c
clk: bcm2835: correctly enable fractional clock support
[mirror_ubuntu-eoan-kernel.git] / drivers / clk / bcm / clk-bcm2835.c
CommitLineData
75fabc3f 1/*
41691b88 2 * Copyright (C) 2010,2015 Broadcom
75fabc3f
SA
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
41691b88
EA
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
75fabc3f
SA
40#include <linux/clk-provider.h>
41#include <linux/clkdev.h>
42#include <linux/clk/bcm2835.h>
41691b88 43#include <linux/module.h>
526d239c 44#include <linux/of.h>
41691b88
EA
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
959ca92a 54# define CM_DIV_FRAC_MASK GENMASK(CM_DIV_FRAC_BITS - 1, 0)
41691b88
EA
55
56#define CM_VPUCTL 0x008
57#define CM_VPUDIV 0x00c
58#define CM_SYSCTL 0x010
59#define CM_SYSDIV 0x014
60#define CM_PERIACTL 0x018
61#define CM_PERIADIV 0x01c
62#define CM_PERIICTL 0x020
63#define CM_PERIIDIV 0x024
64#define CM_H264CTL 0x028
65#define CM_H264DIV 0x02c
66#define CM_ISPCTL 0x030
67#define CM_ISPDIV 0x034
68#define CM_V3DCTL 0x038
69#define CM_V3DDIV 0x03c
70#define CM_CAM0CTL 0x040
71#define CM_CAM0DIV 0x044
72#define CM_CAM1CTL 0x048
73#define CM_CAM1DIV 0x04c
74#define CM_CCP2CTL 0x050
75#define CM_CCP2DIV 0x054
76#define CM_DSI0ECTL 0x058
77#define CM_DSI0EDIV 0x05c
78#define CM_DSI0PCTL 0x060
79#define CM_DSI0PDIV 0x064
80#define CM_DPICTL 0x068
81#define CM_DPIDIV 0x06c
82#define CM_GP0CTL 0x070
83#define CM_GP0DIV 0x074
84#define CM_GP1CTL 0x078
85#define CM_GP1DIV 0x07c
86#define CM_GP2CTL 0x080
87#define CM_GP2DIV 0x084
88#define CM_HSMCTL 0x088
89#define CM_HSMDIV 0x08c
90#define CM_OTPCTL 0x090
91#define CM_OTPDIV 0x094
2103a215
MS
92#define CM_PCMCTL 0x098
93#define CM_PCMDIV 0x09c
41691b88
EA
94#define CM_PWMCTL 0x0a0
95#define CM_PWMDIV 0x0a4
2103a215
MS
96#define CM_SLIMCTL 0x0a8
97#define CM_SLIMDIV 0x0ac
41691b88
EA
98#define CM_SMICTL 0x0b0
99#define CM_SMIDIV 0x0b4
2103a215
MS
100/* no definition for 0x0b8 and 0x0bc */
101#define CM_TCNTCTL 0x0c0
102#define CM_TCNTDIV 0x0c4
103#define CM_TECCTL 0x0c8
104#define CM_TECDIV 0x0cc
105#define CM_TD0CTL 0x0d0
106#define CM_TD0DIV 0x0d4
107#define CM_TD1CTL 0x0d8
108#define CM_TD1DIV 0x0dc
41691b88
EA
109#define CM_TSENSCTL 0x0e0
110#define CM_TSENSDIV 0x0e4
111#define CM_TIMERCTL 0x0e8
112#define CM_TIMERDIV 0x0ec
113#define CM_UARTCTL 0x0f0
114#define CM_UARTDIV 0x0f4
115#define CM_VECCTL 0x0f8
116#define CM_VECDIV 0x0fc
117#define CM_PULSECTL 0x190
118#define CM_PULSEDIV 0x194
119#define CM_SDCCTL 0x1a8
120#define CM_SDCDIV 0x1ac
121#define CM_ARMCTL 0x1b0
122#define CM_EMMCCTL 0x1c0
123#define CM_EMMCDIV 0x1c4
124
125/* General bits for the CM_*CTL regs */
126# define CM_ENABLE BIT(4)
127# define CM_KILL BIT(5)
128# define CM_GATE_BIT 6
129# define CM_GATE BIT(CM_GATE_BIT)
130# define CM_BUSY BIT(7)
131# define CM_BUSYD BIT(8)
959ca92a 132# define CM_FRAC BIT(9)
41691b88
EA
133# define CM_SRC_SHIFT 0
134# define CM_SRC_BITS 4
135# define CM_SRC_MASK 0xf
136# define CM_SRC_GND 0
137# define CM_SRC_OSC 1
138# define CM_SRC_TESTDEBUG0 2
139# define CM_SRC_TESTDEBUG1 3
140# define CM_SRC_PLLA_CORE 4
141# define CM_SRC_PLLA_PER 4
142# define CM_SRC_PLLC_CORE0 5
143# define CM_SRC_PLLC_PER 5
144# define CM_SRC_PLLC_CORE1 8
145# define CM_SRC_PLLD_CORE 6
146# define CM_SRC_PLLD_PER 6
147# define CM_SRC_PLLH_AUX 7
148# define CM_SRC_PLLC_CORE1 8
149# define CM_SRC_PLLC_CORE2 9
150
151#define CM_OSCCOUNT 0x100
152
153#define CM_PLLA 0x104
154# define CM_PLL_ANARST BIT(8)
155# define CM_PLLA_HOLDPER BIT(7)
156# define CM_PLLA_LOADPER BIT(6)
157# define CM_PLLA_HOLDCORE BIT(5)
158# define CM_PLLA_LOADCORE BIT(4)
159# define CM_PLLA_HOLDCCP2 BIT(3)
160# define CM_PLLA_LOADCCP2 BIT(2)
161# define CM_PLLA_HOLDDSI0 BIT(1)
162# define CM_PLLA_LOADDSI0 BIT(0)
163
164#define CM_PLLC 0x108
165# define CM_PLLC_HOLDPER BIT(7)
166# define CM_PLLC_LOADPER BIT(6)
167# define CM_PLLC_HOLDCORE2 BIT(5)
168# define CM_PLLC_LOADCORE2 BIT(4)
169# define CM_PLLC_HOLDCORE1 BIT(3)
170# define CM_PLLC_LOADCORE1 BIT(2)
171# define CM_PLLC_HOLDCORE0 BIT(1)
172# define CM_PLLC_LOADCORE0 BIT(0)
173
174#define CM_PLLD 0x10c
175# define CM_PLLD_HOLDPER BIT(7)
176# define CM_PLLD_LOADPER BIT(6)
177# define CM_PLLD_HOLDCORE BIT(5)
178# define CM_PLLD_LOADCORE BIT(4)
179# define CM_PLLD_HOLDDSI1 BIT(3)
180# define CM_PLLD_LOADDSI1 BIT(2)
181# define CM_PLLD_HOLDDSI0 BIT(1)
182# define CM_PLLD_LOADDSI0 BIT(0)
183
184#define CM_PLLH 0x110
185# define CM_PLLH_LOADRCAL BIT(2)
186# define CM_PLLH_LOADAUX BIT(1)
187# define CM_PLLH_LOADPIX BIT(0)
188
189#define CM_LOCK 0x114
190# define CM_LOCK_FLOCKH BIT(12)
191# define CM_LOCK_FLOCKD BIT(11)
192# define CM_LOCK_FLOCKC BIT(10)
193# define CM_LOCK_FLOCKB BIT(9)
194# define CM_LOCK_FLOCKA BIT(8)
195
196#define CM_EVENT 0x118
197#define CM_DSI1ECTL 0x158
198#define CM_DSI1EDIV 0x15c
199#define CM_DSI1PCTL 0x160
200#define CM_DSI1PDIV 0x164
201#define CM_DFTCTL 0x168
202#define CM_DFTDIV 0x16c
203
204#define CM_PLLB 0x170
205# define CM_PLLB_HOLDARM BIT(1)
206# define CM_PLLB_LOADARM BIT(0)
207
208#define A2W_PLLA_CTRL 0x1100
209#define A2W_PLLC_CTRL 0x1120
210#define A2W_PLLD_CTRL 0x1140
211#define A2W_PLLH_CTRL 0x1160
212#define A2W_PLLB_CTRL 0x11e0
213# define A2W_PLL_CTRL_PRST_DISABLE BIT(17)
214# define A2W_PLL_CTRL_PWRDN BIT(16)
215# define A2W_PLL_CTRL_PDIV_MASK 0x000007000
216# define A2W_PLL_CTRL_PDIV_SHIFT 12
217# define A2W_PLL_CTRL_NDIV_MASK 0x0000003ff
218# define A2W_PLL_CTRL_NDIV_SHIFT 0
219
220#define A2W_PLLA_ANA0 0x1010
221#define A2W_PLLC_ANA0 0x1030
222#define A2W_PLLD_ANA0 0x1050
223#define A2W_PLLH_ANA0 0x1070
224#define A2W_PLLB_ANA0 0x10f0
225
226#define A2W_PLL_KA_SHIFT 7
227#define A2W_PLL_KA_MASK GENMASK(9, 7)
228#define A2W_PLL_KI_SHIFT 19
229#define A2W_PLL_KI_MASK GENMASK(21, 19)
230#define A2W_PLL_KP_SHIFT 15
231#define A2W_PLL_KP_MASK GENMASK(18, 15)
232
233#define A2W_PLLH_KA_SHIFT 19
234#define A2W_PLLH_KA_MASK GENMASK(21, 19)
235#define A2W_PLLH_KI_LOW_SHIFT 22
236#define A2W_PLLH_KI_LOW_MASK GENMASK(23, 22)
237#define A2W_PLLH_KI_HIGH_SHIFT 0
238#define A2W_PLLH_KI_HIGH_MASK GENMASK(0, 0)
239#define A2W_PLLH_KP_SHIFT 1
240#define A2W_PLLH_KP_MASK GENMASK(4, 1)
241
242#define A2W_XOSC_CTRL 0x1190
243# define A2W_XOSC_CTRL_PLLB_ENABLE BIT(7)
244# define A2W_XOSC_CTRL_PLLA_ENABLE BIT(6)
245# define A2W_XOSC_CTRL_PLLD_ENABLE BIT(5)
246# define A2W_XOSC_CTRL_DDR_ENABLE BIT(4)
247# define A2W_XOSC_CTRL_CPR1_ENABLE BIT(3)
248# define A2W_XOSC_CTRL_USB_ENABLE BIT(2)
249# define A2W_XOSC_CTRL_HDMI_ENABLE BIT(1)
250# define A2W_XOSC_CTRL_PLLC_ENABLE BIT(0)
251
252#define A2W_PLLA_FRAC 0x1200
253#define A2W_PLLC_FRAC 0x1220
254#define A2W_PLLD_FRAC 0x1240
255#define A2W_PLLH_FRAC 0x1260
256#define A2W_PLLB_FRAC 0x12e0
257# define A2W_PLL_FRAC_MASK ((1 << A2W_PLL_FRAC_BITS) - 1)
258# define A2W_PLL_FRAC_BITS 20
259
260#define A2W_PLL_CHANNEL_DISABLE BIT(8)
261#define A2W_PLL_DIV_BITS 8
262#define A2W_PLL_DIV_SHIFT 0
263
264#define A2W_PLLA_DSI0 0x1300
265#define A2W_PLLA_CORE 0x1400
266#define A2W_PLLA_PER 0x1500
267#define A2W_PLLA_CCP2 0x1600
268
269#define A2W_PLLC_CORE2 0x1320
270#define A2W_PLLC_CORE1 0x1420
271#define A2W_PLLC_PER 0x1520
272#define A2W_PLLC_CORE0 0x1620
273
274#define A2W_PLLD_DSI0 0x1340
275#define A2W_PLLD_CORE 0x1440
276#define A2W_PLLD_PER 0x1540
277#define A2W_PLLD_DSI1 0x1640
278
279#define A2W_PLLH_AUX 0x1360
280#define A2W_PLLH_RCAL 0x1460
281#define A2W_PLLH_PIX 0x1560
282#define A2W_PLLH_STS 0x1660
283
284#define A2W_PLLH_CTRLR 0x1960
285#define A2W_PLLH_FRACR 0x1a60
286#define A2W_PLLH_AUXR 0x1b60
287#define A2W_PLLH_RCALR 0x1c60
288#define A2W_PLLH_PIXR 0x1d60
289#define A2W_PLLH_STSR 0x1e60
290
291#define A2W_PLLB_ARM 0x13e0
292#define A2W_PLLB_SP0 0x14e0
293#define A2W_PLLB_SP1 0x15e0
294#define A2W_PLLB_SP2 0x16e0
295
296#define LOCK_TIMEOUT_NS 100000000
297#define BCM2835_MAX_FB_RATE 1750000000u
298
299struct bcm2835_cprman {
300 struct device *dev;
301 void __iomem *regs;
302 spinlock_t regs_lock;
303 const char *osc_name;
304
305 struct clk_onecell_data onecell;
306 struct clk *clks[BCM2835_CLOCK_COUNT];
307};
308
309static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
310{
311 writel(CM_PASSWORD | val, cprman->regs + reg);
312}
313
314static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
315{
316 return readl(cprman->regs + reg);
317}
526d239c 318
75fabc3f
SA
319/*
320 * These are fixed clocks. They're probably not all root clocks and it may
321 * be possible to turn them on and off but until this is mapped out better
322 * it's the only way they can be used.
323 */
324void __init bcm2835_init_clocks(void)
325{
326 struct clk *clk;
327 int ret;
328
bd41aa67 329 clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, 0, 126000000);
0de9f23a 330 if (IS_ERR(clk))
75fabc3f
SA
331 pr_err("apb_pclk not registered\n");
332
bd41aa67 333 clk = clk_register_fixed_rate(NULL, "uart0_pclk", NULL, 0, 3000000);
0de9f23a 334 if (IS_ERR(clk))
75fabc3f
SA
335 pr_err("uart0_pclk not registered\n");
336 ret = clk_register_clkdev(clk, NULL, "20201000.uart");
337 if (ret)
338 pr_err("uart0_pclk alias not registered\n");
339
bd41aa67 340 clk = clk_register_fixed_rate(NULL, "uart1_pclk", NULL, 0, 125000000);
0de9f23a 341 if (IS_ERR(clk))
75fabc3f
SA
342 pr_err("uart1_pclk not registered\n");
343 ret = clk_register_clkdev(clk, NULL, "20215000.uart");
344 if (ret)
686ea585 345 pr_err("uart1_pclk alias not registered\n");
75fabc3f 346}
41691b88
EA
347
348struct bcm2835_pll_data {
349 const char *name;
350 u32 cm_ctrl_reg;
351 u32 a2w_ctrl_reg;
352 u32 frac_reg;
353 u32 ana_reg_base;
354 u32 reference_enable_mask;
355 /* Bit in CM_LOCK to indicate when the PLL has locked. */
356 u32 lock_mask;
357
358 const struct bcm2835_pll_ana_bits *ana;
359
360 unsigned long min_rate;
361 unsigned long max_rate;
362 /*
363 * Highest rate for the VCO before we have to use the
364 * pre-divide-by-2.
365 */
366 unsigned long max_fb_rate;
367};
368
369struct bcm2835_pll_ana_bits {
370 u32 mask0;
371 u32 set0;
372 u32 mask1;
373 u32 set1;
374 u32 mask3;
375 u32 set3;
376 u32 fb_prediv_mask;
377};
378
379static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
380 .mask0 = 0,
381 .set0 = 0,
382 .mask1 = ~(A2W_PLL_KI_MASK | A2W_PLL_KP_MASK),
383 .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
384 .mask3 = ~A2W_PLL_KA_MASK,
385 .set3 = (2 << A2W_PLL_KA_SHIFT),
386 .fb_prediv_mask = BIT(14),
387};
388
389static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
390 .mask0 = ~(A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK),
391 .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
392 .mask1 = ~(A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK),
393 .set1 = (6 << A2W_PLLH_KP_SHIFT),
394 .mask3 = 0,
395 .set3 = 0,
396 .fb_prediv_mask = BIT(11),
397};
398
399/*
400 * PLLA is the auxiliary PLL, used to drive the CCP2 (Compact Camera
401 * Port 2) transmitter clock.
402 *
403 * It is in the PX LDO power domain, which is on when the AUDIO domain
404 * is on.
405 */
406static const struct bcm2835_pll_data bcm2835_plla_data = {
407 .name = "plla",
408 .cm_ctrl_reg = CM_PLLA,
409 .a2w_ctrl_reg = A2W_PLLA_CTRL,
410 .frac_reg = A2W_PLLA_FRAC,
411 .ana_reg_base = A2W_PLLA_ANA0,
412 .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
413 .lock_mask = CM_LOCK_FLOCKA,
414
415 .ana = &bcm2835_ana_default,
416
417 .min_rate = 600000000u,
418 .max_rate = 2400000000u,
419 .max_fb_rate = BCM2835_MAX_FB_RATE,
420};
421
422/* PLLB is used for the ARM's clock. */
423static const struct bcm2835_pll_data bcm2835_pllb_data = {
424 .name = "pllb",
425 .cm_ctrl_reg = CM_PLLB,
426 .a2w_ctrl_reg = A2W_PLLB_CTRL,
427 .frac_reg = A2W_PLLB_FRAC,
428 .ana_reg_base = A2W_PLLB_ANA0,
429 .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
430 .lock_mask = CM_LOCK_FLOCKB,
431
432 .ana = &bcm2835_ana_default,
433
434 .min_rate = 600000000u,
435 .max_rate = 3000000000u,
436 .max_fb_rate = BCM2835_MAX_FB_RATE,
437};
438
439/*
440 * PLLC is the core PLL, used to drive the core VPU clock.
441 *
442 * It is in the PX LDO power domain, which is on when the AUDIO domain
443 * is on.
444*/
445static const struct bcm2835_pll_data bcm2835_pllc_data = {
446 .name = "pllc",
447 .cm_ctrl_reg = CM_PLLC,
448 .a2w_ctrl_reg = A2W_PLLC_CTRL,
449 .frac_reg = A2W_PLLC_FRAC,
450 .ana_reg_base = A2W_PLLC_ANA0,
451 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
452 .lock_mask = CM_LOCK_FLOCKC,
453
454 .ana = &bcm2835_ana_default,
455
456 .min_rate = 600000000u,
457 .max_rate = 3000000000u,
458 .max_fb_rate = BCM2835_MAX_FB_RATE,
459};
460
461/*
462 * PLLD is the display PLL, used to drive DSI display panels.
463 *
464 * It is in the PX LDO power domain, which is on when the AUDIO domain
465 * is on.
466 */
467static const struct bcm2835_pll_data bcm2835_plld_data = {
468 .name = "plld",
469 .cm_ctrl_reg = CM_PLLD,
470 .a2w_ctrl_reg = A2W_PLLD_CTRL,
471 .frac_reg = A2W_PLLD_FRAC,
472 .ana_reg_base = A2W_PLLD_ANA0,
473 .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
474 .lock_mask = CM_LOCK_FLOCKD,
475
476 .ana = &bcm2835_ana_default,
477
478 .min_rate = 600000000u,
479 .max_rate = 2400000000u,
480 .max_fb_rate = BCM2835_MAX_FB_RATE,
481};
482
483/*
484 * PLLH is used to supply the pixel clock or the AUX clock for the TV
485 * encoder.
486 *
487 * It is in the HDMI power domain.
488 */
489static const struct bcm2835_pll_data bcm2835_pllh_data = {
490 "pllh",
491 .cm_ctrl_reg = CM_PLLH,
492 .a2w_ctrl_reg = A2W_PLLH_CTRL,
493 .frac_reg = A2W_PLLH_FRAC,
494 .ana_reg_base = A2W_PLLH_ANA0,
495 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
496 .lock_mask = CM_LOCK_FLOCKH,
497
498 .ana = &bcm2835_ana_pllh,
499
500 .min_rate = 600000000u,
501 .max_rate = 3000000000u,
502 .max_fb_rate = BCM2835_MAX_FB_RATE,
503};
504
505struct bcm2835_pll_divider_data {
506 const char *name;
507 const struct bcm2835_pll_data *source_pll;
508 u32 cm_reg;
509 u32 a2w_reg;
510
511 u32 load_mask;
512 u32 hold_mask;
513 u32 fixed_divider;
514};
515
516static const struct bcm2835_pll_divider_data bcm2835_plla_core_data = {
517 .name = "plla_core",
518 .source_pll = &bcm2835_plla_data,
519 .cm_reg = CM_PLLA,
520 .a2w_reg = A2W_PLLA_CORE,
521 .load_mask = CM_PLLA_LOADCORE,
522 .hold_mask = CM_PLLA_HOLDCORE,
523 .fixed_divider = 1,
524};
525
526static const struct bcm2835_pll_divider_data bcm2835_plla_per_data = {
527 .name = "plla_per",
528 .source_pll = &bcm2835_plla_data,
529 .cm_reg = CM_PLLA,
530 .a2w_reg = A2W_PLLA_PER,
531 .load_mask = CM_PLLA_LOADPER,
532 .hold_mask = CM_PLLA_HOLDPER,
533 .fixed_divider = 1,
534};
535
536static const struct bcm2835_pll_divider_data bcm2835_pllb_arm_data = {
537 .name = "pllb_arm",
538 .source_pll = &bcm2835_pllb_data,
539 .cm_reg = CM_PLLB,
540 .a2w_reg = A2W_PLLB_ARM,
541 .load_mask = CM_PLLB_LOADARM,
542 .hold_mask = CM_PLLB_HOLDARM,
543 .fixed_divider = 1,
544};
545
546static const struct bcm2835_pll_divider_data bcm2835_pllc_core0_data = {
547 .name = "pllc_core0",
548 .source_pll = &bcm2835_pllc_data,
549 .cm_reg = CM_PLLC,
550 .a2w_reg = A2W_PLLC_CORE0,
551 .load_mask = CM_PLLC_LOADCORE0,
552 .hold_mask = CM_PLLC_HOLDCORE0,
553 .fixed_divider = 1,
554};
555
556static const struct bcm2835_pll_divider_data bcm2835_pllc_core1_data = {
557 .name = "pllc_core1", .source_pll = &bcm2835_pllc_data,
558 .cm_reg = CM_PLLC, A2W_PLLC_CORE1,
559 .load_mask = CM_PLLC_LOADCORE1,
560 .hold_mask = CM_PLLC_HOLDCORE1,
561 .fixed_divider = 1,
562};
563
564static const struct bcm2835_pll_divider_data bcm2835_pllc_core2_data = {
565 .name = "pllc_core2",
566 .source_pll = &bcm2835_pllc_data,
567 .cm_reg = CM_PLLC,
568 .a2w_reg = A2W_PLLC_CORE2,
569 .load_mask = CM_PLLC_LOADCORE2,
570 .hold_mask = CM_PLLC_HOLDCORE2,
571 .fixed_divider = 1,
572};
573
574static const struct bcm2835_pll_divider_data bcm2835_pllc_per_data = {
575 .name = "pllc_per",
576 .source_pll = &bcm2835_pllc_data,
577 .cm_reg = CM_PLLC,
578 .a2w_reg = A2W_PLLC_PER,
579 .load_mask = CM_PLLC_LOADPER,
580 .hold_mask = CM_PLLC_HOLDPER,
581 .fixed_divider = 1,
582};
583
584static const struct bcm2835_pll_divider_data bcm2835_plld_core_data = {
585 .name = "plld_core",
586 .source_pll = &bcm2835_plld_data,
587 .cm_reg = CM_PLLD,
588 .a2w_reg = A2W_PLLD_CORE,
589 .load_mask = CM_PLLD_LOADCORE,
590 .hold_mask = CM_PLLD_HOLDCORE,
591 .fixed_divider = 1,
592};
593
594static const struct bcm2835_pll_divider_data bcm2835_plld_per_data = {
595 .name = "plld_per",
596 .source_pll = &bcm2835_plld_data,
597 .cm_reg = CM_PLLD,
598 .a2w_reg = A2W_PLLD_PER,
599 .load_mask = CM_PLLD_LOADPER,
600 .hold_mask = CM_PLLD_HOLDPER,
601 .fixed_divider = 1,
602};
603
604static const struct bcm2835_pll_divider_data bcm2835_pllh_rcal_data = {
605 .name = "pllh_rcal",
606 .source_pll = &bcm2835_pllh_data,
607 .cm_reg = CM_PLLH,
608 .a2w_reg = A2W_PLLH_RCAL,
609 .load_mask = CM_PLLH_LOADRCAL,
610 .hold_mask = 0,
611 .fixed_divider = 10,
612};
613
614static const struct bcm2835_pll_divider_data bcm2835_pllh_aux_data = {
615 .name = "pllh_aux",
616 .source_pll = &bcm2835_pllh_data,
617 .cm_reg = CM_PLLH,
618 .a2w_reg = A2W_PLLH_AUX,
619 .load_mask = CM_PLLH_LOADAUX,
620 .hold_mask = 0,
621 .fixed_divider = 10,
622};
623
624static const struct bcm2835_pll_divider_data bcm2835_pllh_pix_data = {
625 .name = "pllh_pix",
626 .source_pll = &bcm2835_pllh_data,
627 .cm_reg = CM_PLLH,
628 .a2w_reg = A2W_PLLH_PIX,
629 .load_mask = CM_PLLH_LOADPIX,
630 .hold_mask = 0,
631 .fixed_divider = 10,
632};
633
634struct bcm2835_clock_data {
635 const char *name;
636
637 const char *const *parents;
638 int num_mux_parents;
639
640 u32 ctl_reg;
641 u32 div_reg;
642
643 /* Number of integer bits in the divider */
644 u32 int_bits;
645 /* Number of fractional bits in the divider */
646 u32 frac_bits;
647
648 bool is_vpu_clock;
959ca92a 649 bool is_mash_clock;
41691b88
EA
650};
651
652static const char *const bcm2835_clock_per_parents[] = {
653 "gnd",
654 "xosc",
655 "testdebug0",
656 "testdebug1",
657 "plla_per",
658 "pllc_per",
659 "plld_per",
660 "pllh_aux",
661};
662
663static const char *const bcm2835_clock_vpu_parents[] = {
664 "gnd",
665 "xosc",
666 "testdebug0",
667 "testdebug1",
668 "plla_core",
669 "pllc_core0",
670 "plld_core",
671 "pllh_aux",
672 "pllc_core1",
673 "pllc_core2",
674};
675
676static const char *const bcm2835_clock_osc_parents[] = {
677 "gnd",
678 "xosc",
679 "testdebug0",
680 "testdebug1"
681};
682
683/*
684 * Used for a 1Mhz clock for the system clocksource, and also used by
685 * the watchdog timer and the camera pulse generator.
686 */
687static const struct bcm2835_clock_data bcm2835_clock_timer_data = {
688 .name = "timer",
689 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
690 .parents = bcm2835_clock_osc_parents,
691 .ctl_reg = CM_TIMERCTL,
692 .div_reg = CM_TIMERDIV,
693 .int_bits = 6,
694 .frac_bits = 12,
695};
696
697/* One Time Programmable Memory clock. Maximum 10Mhz. */
698static const struct bcm2835_clock_data bcm2835_clock_otp_data = {
699 .name = "otp",
700 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
701 .parents = bcm2835_clock_osc_parents,
702 .ctl_reg = CM_OTPCTL,
703 .div_reg = CM_OTPDIV,
704 .int_bits = 4,
705 .frac_bits = 0,
706};
707
708/*
709 * VPU clock. This doesn't have an enable bit, since it drives the
710 * bus for everything else, and is special so it doesn't need to be
711 * gated for rate changes. It is also known as "clk_audio" in various
712 * hardware documentation.
713 */
714static const struct bcm2835_clock_data bcm2835_clock_vpu_data = {
715 .name = "vpu",
716 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
717 .parents = bcm2835_clock_vpu_parents,
718 .ctl_reg = CM_VPUCTL,
719 .div_reg = CM_VPUDIV,
720 .int_bits = 12,
721 .frac_bits = 8,
722 .is_vpu_clock = true,
723};
724
725static const struct bcm2835_clock_data bcm2835_clock_v3d_data = {
726 .name = "v3d",
727 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
728 .parents = bcm2835_clock_vpu_parents,
729 .ctl_reg = CM_V3DCTL,
730 .div_reg = CM_V3DDIV,
731 .int_bits = 4,
732 .frac_bits = 8,
733};
734
735static const struct bcm2835_clock_data bcm2835_clock_isp_data = {
736 .name = "isp",
737 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
738 .parents = bcm2835_clock_vpu_parents,
739 .ctl_reg = CM_ISPCTL,
740 .div_reg = CM_ISPDIV,
741 .int_bits = 4,
742 .frac_bits = 8,
743};
744
745static const struct bcm2835_clock_data bcm2835_clock_h264_data = {
746 .name = "h264",
747 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
748 .parents = bcm2835_clock_vpu_parents,
749 .ctl_reg = CM_H264CTL,
750 .div_reg = CM_H264DIV,
751 .int_bits = 4,
752 .frac_bits = 8,
753};
754
755/* TV encoder clock. Only operating frequency is 108Mhz. */
756static const struct bcm2835_clock_data bcm2835_clock_vec_data = {
757 .name = "vec",
758 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
759 .parents = bcm2835_clock_per_parents,
760 .ctl_reg = CM_VECCTL,
761 .div_reg = CM_VECDIV,
762 .int_bits = 4,
763 .frac_bits = 0,
764};
765
766static const struct bcm2835_clock_data bcm2835_clock_uart_data = {
767 .name = "uart",
768 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
769 .parents = bcm2835_clock_per_parents,
770 .ctl_reg = CM_UARTCTL,
771 .div_reg = CM_UARTDIV,
772 .int_bits = 10,
773 .frac_bits = 12,
774};
775
776/* HDMI state machine */
777static const struct bcm2835_clock_data bcm2835_clock_hsm_data = {
778 .name = "hsm",
779 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
780 .parents = bcm2835_clock_per_parents,
781 .ctl_reg = CM_HSMCTL,
782 .div_reg = CM_HSMDIV,
783 .int_bits = 4,
784 .frac_bits = 8,
785};
786
787/*
788 * Secondary SDRAM clock. Used for low-voltage modes when the PLL in
789 * the SDRAM controller can't be used.
790 */
791static const struct bcm2835_clock_data bcm2835_clock_sdram_data = {
792 .name = "sdram",
793 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
794 .parents = bcm2835_clock_vpu_parents,
795 .ctl_reg = CM_SDCCTL,
796 .div_reg = CM_SDCDIV,
797 .int_bits = 6,
798 .frac_bits = 0,
799};
800
801/* Clock for the temperature sensor. Generally run at 2Mhz, max 5Mhz. */
802static const struct bcm2835_clock_data bcm2835_clock_tsens_data = {
803 .name = "tsens",
804 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
805 .parents = bcm2835_clock_osc_parents,
806 .ctl_reg = CM_TSENSCTL,
807 .div_reg = CM_TSENSDIV,
808 .int_bits = 5,
809 .frac_bits = 0,
810};
811
812/* Arasan EMMC clock */
813static const struct bcm2835_clock_data bcm2835_clock_emmc_data = {
814 .name = "emmc",
815 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
816 .parents = bcm2835_clock_per_parents,
817 .ctl_reg = CM_EMMCCTL,
818 .div_reg = CM_EMMCDIV,
819 .int_bits = 4,
820 .frac_bits = 8,
821};
822
cfbab8fb
RP
823static const struct bcm2835_clock_data bcm2835_clock_pwm_data = {
824 .name = "pwm",
825 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
826 .parents = bcm2835_clock_per_parents,
827 .ctl_reg = CM_PWMCTL,
828 .div_reg = CM_PWMDIV,
829 .int_bits = 12,
830 .frac_bits = 12,
959ca92a 831 .is_mash_clock = true,
cfbab8fb
RP
832};
833
41691b88
EA
834struct bcm2835_pll {
835 struct clk_hw hw;
836 struct bcm2835_cprman *cprman;
837 const struct bcm2835_pll_data *data;
838};
839
840static int bcm2835_pll_is_on(struct clk_hw *hw)
841{
842 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
843 struct bcm2835_cprman *cprman = pll->cprman;
844 const struct bcm2835_pll_data *data = pll->data;
845
846 return cprman_read(cprman, data->a2w_ctrl_reg) &
847 A2W_PLL_CTRL_PRST_DISABLE;
848}
849
850static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
851 unsigned long parent_rate,
852 u32 *ndiv, u32 *fdiv)
853{
854 u64 div;
855
856 div = (u64)rate << A2W_PLL_FRAC_BITS;
857 do_div(div, parent_rate);
858
859 *ndiv = div >> A2W_PLL_FRAC_BITS;
860 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
861}
862
863static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
864 u32 ndiv, u32 fdiv, u32 pdiv)
865{
866 u64 rate;
867
868 if (pdiv == 0)
869 return 0;
870
871 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
872 do_div(rate, pdiv);
873 return rate >> A2W_PLL_FRAC_BITS;
874}
875
876static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
877 unsigned long *parent_rate)
878{
879 u32 ndiv, fdiv;
880
881 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
882
883 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
884}
885
886static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
887 unsigned long parent_rate)
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 u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
893 u32 ndiv, pdiv, fdiv;
894 bool using_prediv;
895
896 if (parent_rate == 0)
897 return 0;
898
899 fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
900 ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
901 pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
902 using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
903 data->ana->fb_prediv_mask;
904
905 if (using_prediv)
906 ndiv *= 2;
907
908 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
909}
910
911static void bcm2835_pll_off(struct clk_hw *hw)
912{
913 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
914 struct bcm2835_cprman *cprman = pll->cprman;
915 const struct bcm2835_pll_data *data = pll->data;
916
6727f086
MS
917 spin_lock(&cprman->regs_lock);
918 cprman_write(cprman, data->cm_ctrl_reg,
919 cprman_read(cprman, data->cm_ctrl_reg) |
920 CM_PLL_ANARST);
921 cprman_write(cprman, data->a2w_ctrl_reg,
922 cprman_read(cprman, data->a2w_ctrl_reg) |
923 A2W_PLL_CTRL_PWRDN);
924 spin_unlock(&cprman->regs_lock);
41691b88
EA
925}
926
927static int bcm2835_pll_on(struct clk_hw *hw)
928{
929 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
930 struct bcm2835_cprman *cprman = pll->cprman;
931 const struct bcm2835_pll_data *data = pll->data;
932 ktime_t timeout;
933
934 /* Take the PLL out of reset. */
935 cprman_write(cprman, data->cm_ctrl_reg,
936 cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
937
938 /* Wait for the PLL to lock. */
939 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
940 while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
941 if (ktime_after(ktime_get(), timeout)) {
942 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
943 clk_hw_get_name(hw));
944 return -ETIMEDOUT;
945 }
946
947 cpu_relax();
948 }
949
950 return 0;
951}
952
953static void
954bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
955{
956 int i;
957
958 /*
959 * ANA register setup is done as a series of writes to
960 * ANA3-ANA0, in that order. This lets us write all 4
961 * registers as a single cycle of the serdes interface (taking
962 * 100 xosc clocks), whereas if we were to update ana0, 1, and
963 * 3 individually through their partial-write registers, each
964 * would be their own serdes cycle.
965 */
966 for (i = 3; i >= 0; i--)
967 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
968}
969
970static int bcm2835_pll_set_rate(struct clk_hw *hw,
971 unsigned long rate, unsigned long parent_rate)
972{
973 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
974 struct bcm2835_cprman *cprman = pll->cprman;
975 const struct bcm2835_pll_data *data = pll->data;
976 bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
977 u32 ndiv, fdiv, a2w_ctl;
978 u32 ana[4];
979 int i;
980
981 if (rate < data->min_rate || rate > data->max_rate) {
982 dev_err(cprman->dev, "%s: rate out of spec: %lu vs (%lu, %lu)\n",
983 clk_hw_get_name(hw), rate,
984 data->min_rate, data->max_rate);
985 return -EINVAL;
986 }
987
988 if (rate > data->max_fb_rate) {
989 use_fb_prediv = true;
990 rate /= 2;
991 } else {
992 use_fb_prediv = false;
993 }
994
995 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
996
997 for (i = 3; i >= 0; i--)
998 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
999
1000 was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
1001
1002 ana[0] &= ~data->ana->mask0;
1003 ana[0] |= data->ana->set0;
1004 ana[1] &= ~data->ana->mask1;
1005 ana[1] |= data->ana->set1;
1006 ana[3] &= ~data->ana->mask3;
1007 ana[3] |= data->ana->set3;
1008
1009 if (was_using_prediv && !use_fb_prediv) {
1010 ana[1] &= ~data->ana->fb_prediv_mask;
1011 do_ana_setup_first = true;
1012 } else if (!was_using_prediv && use_fb_prediv) {
1013 ana[1] |= data->ana->fb_prediv_mask;
1014 do_ana_setup_first = false;
1015 } else {
1016 do_ana_setup_first = true;
1017 }
1018
1019 /* Unmask the reference clock from the oscillator. */
1020 cprman_write(cprman, A2W_XOSC_CTRL,
1021 cprman_read(cprman, A2W_XOSC_CTRL) |
1022 data->reference_enable_mask);
1023
1024 if (do_ana_setup_first)
1025 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
1026
1027 /* Set the PLL multiplier from the oscillator. */
1028 cprman_write(cprman, data->frac_reg, fdiv);
1029
1030 a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
1031 a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
1032 a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
1033 a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
1034 a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
1035 cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
1036
1037 if (!do_ana_setup_first)
1038 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
1039
1040 return 0;
1041}
1042
1043static const struct clk_ops bcm2835_pll_clk_ops = {
1044 .is_prepared = bcm2835_pll_is_on,
1045 .prepare = bcm2835_pll_on,
1046 .unprepare = bcm2835_pll_off,
1047 .recalc_rate = bcm2835_pll_get_rate,
1048 .set_rate = bcm2835_pll_set_rate,
1049 .round_rate = bcm2835_pll_round_rate,
1050};
1051
1052struct bcm2835_pll_divider {
1053 struct clk_divider div;
1054 struct bcm2835_cprman *cprman;
1055 const struct bcm2835_pll_divider_data *data;
1056};
1057
1058static struct bcm2835_pll_divider *
1059bcm2835_pll_divider_from_hw(struct clk_hw *hw)
1060{
1061 return container_of(hw, struct bcm2835_pll_divider, div.hw);
1062}
1063
1064static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
1065{
1066 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1067 struct bcm2835_cprman *cprman = divider->cprman;
1068 const struct bcm2835_pll_divider_data *data = divider->data;
1069
1070 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
1071}
1072
1073static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
1074 unsigned long rate,
1075 unsigned long *parent_rate)
1076{
1077 return clk_divider_ops.round_rate(hw, rate, parent_rate);
1078}
1079
1080static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
1081 unsigned long parent_rate)
1082{
79c1e2fc 1083 return clk_divider_ops.recalc_rate(hw, parent_rate);
41691b88
EA
1084}
1085
1086static void bcm2835_pll_divider_off(struct clk_hw *hw)
1087{
1088 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1089 struct bcm2835_cprman *cprman = divider->cprman;
1090 const struct bcm2835_pll_divider_data *data = divider->data;
1091
ec36a5c6 1092 spin_lock(&cprman->regs_lock);
41691b88
EA
1093 cprman_write(cprman, data->cm_reg,
1094 (cprman_read(cprman, data->cm_reg) &
1095 ~data->load_mask) | data->hold_mask);
1096 cprman_write(cprman, data->a2w_reg, A2W_PLL_CHANNEL_DISABLE);
ec36a5c6 1097 spin_unlock(&cprman->regs_lock);
41691b88
EA
1098}
1099
1100static int bcm2835_pll_divider_on(struct clk_hw *hw)
1101{
1102 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1103 struct bcm2835_cprman *cprman = divider->cprman;
1104 const struct bcm2835_pll_divider_data *data = divider->data;
1105
ec36a5c6 1106 spin_lock(&cprman->regs_lock);
41691b88
EA
1107 cprman_write(cprman, data->a2w_reg,
1108 cprman_read(cprman, data->a2w_reg) &
1109 ~A2W_PLL_CHANNEL_DISABLE);
1110
1111 cprman_write(cprman, data->cm_reg,
1112 cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
ec36a5c6 1113 spin_unlock(&cprman->regs_lock);
41691b88
EA
1114
1115 return 0;
1116}
1117
1118static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
1119 unsigned long rate,
1120 unsigned long parent_rate)
1121{
1122 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1123 struct bcm2835_cprman *cprman = divider->cprman;
1124 const struct bcm2835_pll_divider_data *data = divider->data;
773b3966 1125 u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
41691b88 1126
773b3966
EA
1127 div = DIV_ROUND_UP_ULL(parent_rate, rate);
1128
1129 div = min(div, max_div);
1130 if (div == max_div)
1131 div = 0;
41691b88 1132
773b3966 1133 cprman_write(cprman, data->a2w_reg, div);
41691b88
EA
1134 cm = cprman_read(cprman, data->cm_reg);
1135 cprman_write(cprman, data->cm_reg, cm | data->load_mask);
1136 cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
1137
1138 return 0;
1139}
1140
1141static const struct clk_ops bcm2835_pll_divider_clk_ops = {
1142 .is_prepared = bcm2835_pll_divider_is_on,
1143 .prepare = bcm2835_pll_divider_on,
1144 .unprepare = bcm2835_pll_divider_off,
1145 .recalc_rate = bcm2835_pll_divider_get_rate,
1146 .set_rate = bcm2835_pll_divider_set_rate,
1147 .round_rate = bcm2835_pll_divider_round_rate,
1148};
1149
1150/*
1151 * The CM dividers do fixed-point division, so we can't use the
1152 * generic integer divider code like the PLL dividers do (and we can't
1153 * fake it by having some fixed shifts preceding it in the clock tree,
1154 * because we'd run out of bits in a 32-bit unsigned long).
1155 */
1156struct bcm2835_clock {
1157 struct clk_hw hw;
1158 struct bcm2835_cprman *cprman;
1159 const struct bcm2835_clock_data *data;
1160};
1161
1162static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
1163{
1164 return container_of(hw, struct bcm2835_clock, hw);
1165}
1166
1167static int bcm2835_clock_is_on(struct clk_hw *hw)
1168{
1169 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1170 struct bcm2835_cprman *cprman = clock->cprman;
1171 const struct bcm2835_clock_data *data = clock->data;
1172
1173 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
1174}
1175
1176static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
1177 unsigned long rate,
9c95b32c
RP
1178 unsigned long parent_rate,
1179 bool round_up)
41691b88
EA
1180{
1181 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1182 const struct bcm2835_clock_data *data = clock->data;
9c95b32c
RP
1183 u32 unused_frac_mask =
1184 GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
41691b88 1185 u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
9c95b32c 1186 u64 rem;
959ca92a 1187 u32 div, mindiv, maxdiv;
41691b88 1188
9c95b32c 1189 rem = do_div(temp, rate);
41691b88
EA
1190 div = temp;
1191
9c95b32c
RP
1192 /* Round up and mask off the unused bits */
1193 if (round_up && ((div & unused_frac_mask) != 0 || rem != 0))
1194 div += unused_frac_mask + 1;
1195 div &= ~unused_frac_mask;
41691b88 1196
959ca92a
MS
1197 /* different clamping limits apply for a mash clock */
1198 if (data->is_mash_clock) {
1199 /* clamp to min divider of 2 */
1200 mindiv = 2 << CM_DIV_FRAC_BITS;
1201 /* clamp to the highest possible integer divider */
1202 maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
1203 } else {
1204 /* clamp to min divider of 1 */
1205 mindiv = 1 << CM_DIV_FRAC_BITS;
1206 /* clamp to the highest possible fractional divider */
1207 maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
1208 CM_DIV_FRAC_BITS - data->frac_bits);
1209 }
1210
1211 /* apply the clamping limits */
1212 div = max_t(u32, div, mindiv);
1213 div = min_t(u32, div, maxdiv);
41691b88
EA
1214
1215 return div;
1216}
1217
1218static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
1219 unsigned long parent_rate,
1220 u32 div)
1221{
1222 const struct bcm2835_clock_data *data = clock->data;
1223 u64 temp;
1224
1225 /*
1226 * The divisor is a 12.12 fixed point field, but only some of
1227 * the bits are populated in any given clock.
1228 */
1229 div >>= CM_DIV_FRAC_BITS - data->frac_bits;
1230 div &= (1 << (data->int_bits + data->frac_bits)) - 1;
1231
1232 if (div == 0)
1233 return 0;
1234
1235 temp = (u64)parent_rate << data->frac_bits;
1236
1237 do_div(temp, div);
1238
1239 return temp;
1240}
1241
41691b88
EA
1242static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
1243 unsigned long parent_rate)
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 u32 div = cprman_read(cprman, data->div_reg);
1249
1250 return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
1251}
1252
1253static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
1254{
1255 struct bcm2835_cprman *cprman = clock->cprman;
1256 const struct bcm2835_clock_data *data = clock->data;
1257 ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
1258
1259 while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
1260 if (ktime_after(ktime_get(), timeout)) {
1261 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
1262 clk_hw_get_name(&clock->hw));
1263 return;
1264 }
1265 cpu_relax();
1266 }
1267}
1268
1269static void bcm2835_clock_off(struct clk_hw *hw)
1270{
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
1275 spin_lock(&cprman->regs_lock);
1276 cprman_write(cprman, data->ctl_reg,
1277 cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
1278 spin_unlock(&cprman->regs_lock);
1279
1280 /* BUSY will remain high until the divider completes its cycle. */
1281 bcm2835_clock_wait_busy(clock);
1282}
1283
1284static int bcm2835_clock_on(struct clk_hw *hw)
1285{
1286 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1287 struct bcm2835_cprman *cprman = clock->cprman;
1288 const struct bcm2835_clock_data *data = clock->data;
1289
1290 spin_lock(&cprman->regs_lock);
1291 cprman_write(cprman, data->ctl_reg,
1292 cprman_read(cprman, data->ctl_reg) |
1293 CM_ENABLE |
1294 CM_GATE);
1295 spin_unlock(&cprman->regs_lock);
1296
1297 return 0;
1298}
1299
1300static int bcm2835_clock_set_rate(struct clk_hw *hw,
1301 unsigned long rate, unsigned long parent_rate)
1302{
1303 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1304 struct bcm2835_cprman *cprman = clock->cprman;
1305 const struct bcm2835_clock_data *data = clock->data;
9c95b32c 1306 u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false);
959ca92a
MS
1307 u32 ctl;
1308
1309 spin_lock(&cprman->regs_lock);
1310
1311 /*
1312 * Setting up frac support
1313 *
1314 * In principle it is recommended to stop/start the clock first,
1315 * but as we set CLK_SET_RATE_GATE during registration of the
1316 * clock this requirement should be take care of by the
1317 * clk-framework.
1318 */
1319 ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
1320 ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
1321 cprman_write(cprman, data->ctl_reg, ctl);
41691b88
EA
1322
1323 cprman_write(cprman, data->div_reg, div);
1324
959ca92a
MS
1325 spin_unlock(&cprman->regs_lock);
1326
41691b88
EA
1327 return 0;
1328}
1329
6d18b8ad
RP
1330static int bcm2835_clock_determine_rate(struct clk_hw *hw,
1331 struct clk_rate_request *req)
1332{
1333 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1334 struct clk_hw *parent, *best_parent = NULL;
1335 unsigned long rate, best_rate = 0;
1336 unsigned long prate, best_prate = 0;
1337 size_t i;
1338 u32 div;
1339
1340 /*
1341 * Select parent clock that results in the closest but lower rate
1342 */
1343 for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
1344 parent = clk_hw_get_parent_by_index(hw, i);
1345 if (!parent)
1346 continue;
1347 prate = clk_hw_get_rate(parent);
1348 div = bcm2835_clock_choose_div(hw, req->rate, prate, true);
1349 rate = bcm2835_clock_rate_from_divisor(clock, prate, div);
1350 if (rate > best_rate && rate <= req->rate) {
1351 best_parent = parent;
1352 best_prate = prate;
1353 best_rate = rate;
1354 }
1355 }
1356
1357 if (!best_parent)
1358 return -EINVAL;
1359
1360 req->best_parent_hw = best_parent;
1361 req->best_parent_rate = best_prate;
1362
1363 req->rate = best_rate;
1364
1365 return 0;
1366}
1367
1368static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
1369{
1370 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1371 struct bcm2835_cprman *cprman = clock->cprman;
1372 const struct bcm2835_clock_data *data = clock->data;
1373 u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
1374
1375 cprman_write(cprman, data->ctl_reg, src);
1376 return 0;
1377}
1378
1379static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
1380{
1381 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1382 struct bcm2835_cprman *cprman = clock->cprman;
1383 const struct bcm2835_clock_data *data = clock->data;
1384 u32 src = cprman_read(cprman, data->ctl_reg);
1385
1386 return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
1387}
1388
1389
41691b88
EA
1390static const struct clk_ops bcm2835_clock_clk_ops = {
1391 .is_prepared = bcm2835_clock_is_on,
1392 .prepare = bcm2835_clock_on,
1393 .unprepare = bcm2835_clock_off,
1394 .recalc_rate = bcm2835_clock_get_rate,
1395 .set_rate = bcm2835_clock_set_rate,
6d18b8ad
RP
1396 .determine_rate = bcm2835_clock_determine_rate,
1397 .set_parent = bcm2835_clock_set_parent,
1398 .get_parent = bcm2835_clock_get_parent,
41691b88
EA
1399};
1400
1401static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1402{
1403 return true;
1404}
1405
1406/*
1407 * The VPU clock can never be disabled (it doesn't have an ENABLE
1408 * bit), so it gets its own set of clock ops.
1409 */
1410static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1411 .is_prepared = bcm2835_vpu_clock_is_on,
1412 .recalc_rate = bcm2835_clock_get_rate,
1413 .set_rate = bcm2835_clock_set_rate,
6d18b8ad
RP
1414 .determine_rate = bcm2835_clock_determine_rate,
1415 .set_parent = bcm2835_clock_set_parent,
1416 .get_parent = bcm2835_clock_get_parent,
41691b88
EA
1417};
1418
1419static struct clk *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1420 const struct bcm2835_pll_data *data)
1421{
1422 struct bcm2835_pll *pll;
1423 struct clk_init_data init;
1424
1425 memset(&init, 0, sizeof(init));
1426
1427 /* All of the PLLs derive from the external oscillator. */
1428 init.parent_names = &cprman->osc_name;
1429 init.num_parents = 1;
1430 init.name = data->name;
1431 init.ops = &bcm2835_pll_clk_ops;
1432 init.flags = CLK_IGNORE_UNUSED;
1433
1434 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1435 if (!pll)
1436 return NULL;
1437
1438 pll->cprman = cprman;
1439 pll->data = data;
1440 pll->hw.init = &init;
1441
1442 return devm_clk_register(cprman->dev, &pll->hw);
1443}
1444
1445static struct clk *
1446bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1447 const struct bcm2835_pll_divider_data *data)
1448{
1449 struct bcm2835_pll_divider *divider;
1450 struct clk_init_data init;
1451 struct clk *clk;
1452 const char *divider_name;
1453
1454 if (data->fixed_divider != 1) {
1455 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1456 "%s_prediv", data->name);
1457 if (!divider_name)
1458 return NULL;
1459 } else {
1460 divider_name = data->name;
1461 }
1462
1463 memset(&init, 0, sizeof(init));
1464
1465 init.parent_names = &data->source_pll->name;
1466 init.num_parents = 1;
1467 init.name = divider_name;
1468 init.ops = &bcm2835_pll_divider_clk_ops;
1469 init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
1470
1471 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1472 if (!divider)
1473 return NULL;
1474
1475 divider->div.reg = cprman->regs + data->a2w_reg;
1476 divider->div.shift = A2W_PLL_DIV_SHIFT;
1477 divider->div.width = A2W_PLL_DIV_BITS;
79c1e2fc 1478 divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
41691b88
EA
1479 divider->div.lock = &cprman->regs_lock;
1480 divider->div.hw.init = &init;
1481 divider->div.table = NULL;
1482
1483 divider->cprman = cprman;
1484 divider->data = data;
1485
1486 clk = devm_clk_register(cprman->dev, &divider->div.hw);
1487 if (IS_ERR(clk))
1488 return clk;
1489
1490 /*
1491 * PLLH's channels have a fixed divide by 10 afterwards, which
1492 * is what our consumers are actually using.
1493 */
1494 if (data->fixed_divider != 1) {
1495 return clk_register_fixed_factor(cprman->dev, data->name,
1496 divider_name,
1497 CLK_SET_RATE_PARENT,
1498 1,
1499 data->fixed_divider);
1500 }
1501
1502 return clk;
1503}
1504
1505static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman,
1506 const struct bcm2835_clock_data *data)
1507{
1508 struct bcm2835_clock *clock;
1509 struct clk_init_data init;
6d18b8ad
RP
1510 const char *parents[1 << CM_SRC_BITS];
1511 size_t i;
41691b88
EA
1512
1513 /*
6d18b8ad
RP
1514 * Replace our "xosc" references with the oscillator's
1515 * actual name.
41691b88 1516 */
6d18b8ad
RP
1517 for (i = 0; i < data->num_mux_parents; i++) {
1518 if (strcmp(data->parents[i], "xosc") == 0)
1519 parents[i] = cprman->osc_name;
1520 else
1521 parents[i] = data->parents[i];
41691b88
EA
1522 }
1523
1524 memset(&init, 0, sizeof(init));
6d18b8ad
RP
1525 init.parent_names = parents;
1526 init.num_parents = data->num_mux_parents;
41691b88
EA
1527 init.name = data->name;
1528 init.flags = CLK_IGNORE_UNUSED;
1529
1530 if (data->is_vpu_clock) {
1531 init.ops = &bcm2835_vpu_clock_clk_ops;
1532 } else {
1533 init.ops = &bcm2835_clock_clk_ops;
1534 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
1535 }
1536
1537 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1538 if (!clock)
1539 return NULL;
1540
1541 clock->cprman = cprman;
1542 clock->data = data;
1543 clock->hw.init = &init;
1544
1545 return devm_clk_register(cprman->dev, &clock->hw);
1546}
1547
1548static int bcm2835_clk_probe(struct platform_device *pdev)
1549{
1550 struct device *dev = &pdev->dev;
1551 struct clk **clks;
1552 struct bcm2835_cprman *cprman;
1553 struct resource *res;
1554
1555 cprman = devm_kzalloc(dev, sizeof(*cprman), GFP_KERNEL);
1556 if (!cprman)
1557 return -ENOMEM;
1558
1559 spin_lock_init(&cprman->regs_lock);
1560 cprman->dev = dev;
1561 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1562 cprman->regs = devm_ioremap_resource(dev, res);
1563 if (IS_ERR(cprman->regs))
1564 return PTR_ERR(cprman->regs);
1565
1566 cprman->osc_name = of_clk_get_parent_name(dev->of_node, 0);
1567 if (!cprman->osc_name)
1568 return -ENODEV;
1569
1570 platform_set_drvdata(pdev, cprman);
1571
1572 cprman->onecell.clk_num = BCM2835_CLOCK_COUNT;
1573 cprman->onecell.clks = cprman->clks;
1574 clks = cprman->clks;
1575
1576 clks[BCM2835_PLLA] = bcm2835_register_pll(cprman, &bcm2835_plla_data);
1577 clks[BCM2835_PLLB] = bcm2835_register_pll(cprman, &bcm2835_pllb_data);
1578 clks[BCM2835_PLLC] = bcm2835_register_pll(cprman, &bcm2835_pllc_data);
1579 clks[BCM2835_PLLD] = bcm2835_register_pll(cprman, &bcm2835_plld_data);
1580 clks[BCM2835_PLLH] = bcm2835_register_pll(cprman, &bcm2835_pllh_data);
1581
1582 clks[BCM2835_PLLA_CORE] =
1583 bcm2835_register_pll_divider(cprman, &bcm2835_plla_core_data);
1584 clks[BCM2835_PLLA_PER] =
1585 bcm2835_register_pll_divider(cprman, &bcm2835_plla_per_data);
1586 clks[BCM2835_PLLC_CORE0] =
1587 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core0_data);
1588 clks[BCM2835_PLLC_CORE1] =
1589 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core1_data);
1590 clks[BCM2835_PLLC_CORE2] =
1591 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core2_data);
1592 clks[BCM2835_PLLC_PER] =
1593 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_per_data);
1594 clks[BCM2835_PLLD_CORE] =
1595 bcm2835_register_pll_divider(cprman, &bcm2835_plld_core_data);
1596 clks[BCM2835_PLLD_PER] =
1597 bcm2835_register_pll_divider(cprman, &bcm2835_plld_per_data);
1598 clks[BCM2835_PLLH_RCAL] =
1599 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_rcal_data);
1600 clks[BCM2835_PLLH_AUX] =
1601 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_aux_data);
1602 clks[BCM2835_PLLH_PIX] =
1603 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_pix_data);
1604
1605 clks[BCM2835_CLOCK_TIMER] =
1606 bcm2835_register_clock(cprman, &bcm2835_clock_timer_data);
1607 clks[BCM2835_CLOCK_OTP] =
1608 bcm2835_register_clock(cprman, &bcm2835_clock_otp_data);
1609 clks[BCM2835_CLOCK_TSENS] =
1610 bcm2835_register_clock(cprman, &bcm2835_clock_tsens_data);
1611 clks[BCM2835_CLOCK_VPU] =
1612 bcm2835_register_clock(cprman, &bcm2835_clock_vpu_data);
1613 clks[BCM2835_CLOCK_V3D] =
1614 bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data);
1615 clks[BCM2835_CLOCK_ISP] =
1616 bcm2835_register_clock(cprman, &bcm2835_clock_isp_data);
1617 clks[BCM2835_CLOCK_H264] =
1618 bcm2835_register_clock(cprman, &bcm2835_clock_h264_data);
1619 clks[BCM2835_CLOCK_V3D] =
1620 bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data);
1621 clks[BCM2835_CLOCK_SDRAM] =
1622 bcm2835_register_clock(cprman, &bcm2835_clock_sdram_data);
1623 clks[BCM2835_CLOCK_UART] =
1624 bcm2835_register_clock(cprman, &bcm2835_clock_uart_data);
1625 clks[BCM2835_CLOCK_VEC] =
1626 bcm2835_register_clock(cprman, &bcm2835_clock_vec_data);
1627 clks[BCM2835_CLOCK_HSM] =
1628 bcm2835_register_clock(cprman, &bcm2835_clock_hsm_data);
1629 clks[BCM2835_CLOCK_EMMC] =
1630 bcm2835_register_clock(cprman, &bcm2835_clock_emmc_data);
1631
1632 /*
1633 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
1634 * you have the debug bit set in the power manager, which we
1635 * don't bother exposing) are individual gates off of the
1636 * non-stop vpu clock.
1637 */
1638 clks[BCM2835_CLOCK_PERI_IMAGE] =
1639 clk_register_gate(dev, "peri_image", "vpu",
1640 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1641 cprman->regs + CM_PERIICTL, CM_GATE_BIT,
1642 0, &cprman->regs_lock);
1643
cfbab8fb
RP
1644 clks[BCM2835_CLOCK_PWM] =
1645 bcm2835_register_clock(cprman, &bcm2835_clock_pwm_data);
1646
41691b88
EA
1647 return of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1648 &cprman->onecell);
1649}
1650
1651static const struct of_device_id bcm2835_clk_of_match[] = {
1652 { .compatible = "brcm,bcm2835-cprman", },
1653 {}
1654};
1655MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
1656
1657static struct platform_driver bcm2835_clk_driver = {
1658 .driver = {
1659 .name = "bcm2835-clk",
1660 .of_match_table = bcm2835_clk_of_match,
1661 },
1662 .probe = bcm2835_clk_probe,
1663};
1664
1665builtin_platform_driver(bcm2835_clk_driver);
1666
1667MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
1668MODULE_DESCRIPTION("BCM2835 clock driver");
1669MODULE_LICENSE("GPL v2");