blob: 5e1843321bc8c7c4aa63c9d3c8d17a7c15b3b306 [file] [log] [blame]
Simon Arlott75fabc32012-09-10 23:26:15 -06001/*
Eric Anholt41691b82015-10-08 18:37:24 -07002 * Copyright (C) 2010,2015 Broadcom
Simon Arlott75fabc32012-09-10 23:26:15 -06003 * 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 *
Simon Arlott75fabc32012-09-10 23:26:15 -060015 */
16
Eric Anholt41691b82015-10-08 18:37:24 -070017/**
18 * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
19 *
20 * The clock tree on the 2835 has several levels. There's a root
21 * oscillator running at 19.2Mhz. After the oscillator there are 5
22 * PLLs, roughly divided as "camera", "ARM", "core", "DSI displays",
23 * and "HDMI displays". Those 5 PLLs each can divide their output to
24 * produce up to 4 channels. Finally, there is the level of clocks to
25 * be consumed by other hardware components (like "H264" or "HDMI
26 * state machine"), which divide off of some subset of the PLL
27 * channels.
28 *
29 * All of the clocks in the tree are exposed in the DT, because the DT
30 * may want to make assignments of the final layer of clocks to the
31 * PLL channels, and some components of the hardware will actually
32 * skip layers of the tree (for example, the pixel clock comes
33 * directly from the PLLH PIX channel without using a CM_*CTL clock
34 * generator).
35 */
36
Simon Arlott75fabc32012-09-10 23:26:15 -060037#include <linux/clk-provider.h>
38#include <linux/clkdev.h>
Eric Anholt9e400c52016-06-01 12:05:35 -070039#include <linux/clk.h>
Martin Sperl96bf9c62016-02-29 14:20:15 +000040#include <linux/debugfs.h>
Eric Anholt3f919582017-01-18 07:31:57 +110041#include <linux/delay.h>
Eric Anholt41691b82015-10-08 18:37:24 -070042#include <linux/module.h>
Stephen Warren526d2392012-12-24 21:55:01 -070043#include <linux/of.h>
Eric Anholt41691b82015-10-08 18:37:24 -070044#include <linux/platform_device.h>
45#include <linux/slab.h>
46#include <dt-bindings/clock/bcm2835.h>
47
48#define CM_PASSWORD 0x5a000000
49
50#define CM_GNRICCTL 0x000
51#define CM_GNRICDIV 0x004
52# define CM_DIV_FRAC_BITS 12
Martin Sperl959ca922016-02-29 11:39:21 +000053# define CM_DIV_FRAC_MASK GENMASK(CM_DIV_FRAC_BITS - 1, 0)
Eric Anholt41691b82015-10-08 18:37:24 -070054
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
Martin Sperl2103a212015-12-22 20:13:08 +000091#define CM_PCMCTL 0x098
92#define CM_PCMDIV 0x09c
Eric Anholt41691b82015-10-08 18:37:24 -070093#define CM_PWMCTL 0x0a0
94#define CM_PWMDIV 0x0a4
Martin Sperl2103a212015-12-22 20:13:08 +000095#define CM_SLIMCTL 0x0a8
96#define CM_SLIMDIV 0x0ac
Eric Anholt41691b82015-10-08 18:37:24 -070097#define CM_SMICTL 0x0b0
98#define CM_SMIDIV 0x0b4
Martin Sperl2103a212015-12-22 20:13:08 +000099/* no definition for 0x0b8 and 0x0bc */
100#define CM_TCNTCTL 0x0c0
Eric Anholt3f919582017-01-18 07:31:57 +1100101# define CM_TCNT_SRC1_SHIFT 12
102#define CM_TCNTCNT 0x0c4
Martin Sperl2103a212015-12-22 20:13:08 +0000103#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
Eric Anholt41691b82015-10-08 18:37:24 -0700109#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
Martin Sperld3d6f152016-02-29 15:43:57 +0000122#define CM_AVEOCTL 0x1b8
123#define CM_AVEODIV 0x1bc
Eric Anholt41691b82015-10-08 18:37:24 -0700124#define CM_EMMCCTL 0x1c0
125#define CM_EMMCDIV 0x1c4
126
127/* General bits for the CM_*CTL regs */
128# define CM_ENABLE BIT(4)
129# define CM_KILL BIT(5)
130# define CM_GATE_BIT 6
131# define CM_GATE BIT(CM_GATE_BIT)
132# define CM_BUSY BIT(7)
133# define CM_BUSYD BIT(8)
Martin Sperl959ca922016-02-29 11:39:21 +0000134# define CM_FRAC BIT(9)
Eric Anholt41691b82015-10-08 18:37:24 -0700135# define CM_SRC_SHIFT 0
136# define CM_SRC_BITS 4
137# define CM_SRC_MASK 0xf
138# define CM_SRC_GND 0
139# define CM_SRC_OSC 1
140# define CM_SRC_TESTDEBUG0 2
141# define CM_SRC_TESTDEBUG1 3
142# define CM_SRC_PLLA_CORE 4
143# define CM_SRC_PLLA_PER 4
144# define CM_SRC_PLLC_CORE0 5
145# define CM_SRC_PLLC_PER 5
146# define CM_SRC_PLLC_CORE1 8
147# define CM_SRC_PLLD_CORE 6
148# define CM_SRC_PLLD_PER 6
149# define CM_SRC_PLLH_AUX 7
150# define CM_SRC_PLLC_CORE1 8
151# define CM_SRC_PLLC_CORE2 9
152
153#define CM_OSCCOUNT 0x100
154
155#define CM_PLLA 0x104
156# define CM_PLL_ANARST BIT(8)
157# define CM_PLLA_HOLDPER BIT(7)
158# define CM_PLLA_LOADPER BIT(6)
159# define CM_PLLA_HOLDCORE BIT(5)
160# define CM_PLLA_LOADCORE BIT(4)
161# define CM_PLLA_HOLDCCP2 BIT(3)
162# define CM_PLLA_LOADCCP2 BIT(2)
163# define CM_PLLA_HOLDDSI0 BIT(1)
164# define CM_PLLA_LOADDSI0 BIT(0)
165
166#define CM_PLLC 0x108
167# define CM_PLLC_HOLDPER BIT(7)
168# define CM_PLLC_LOADPER BIT(6)
169# define CM_PLLC_HOLDCORE2 BIT(5)
170# define CM_PLLC_LOADCORE2 BIT(4)
171# define CM_PLLC_HOLDCORE1 BIT(3)
172# define CM_PLLC_LOADCORE1 BIT(2)
173# define CM_PLLC_HOLDCORE0 BIT(1)
174# define CM_PLLC_LOADCORE0 BIT(0)
175
176#define CM_PLLD 0x10c
177# define CM_PLLD_HOLDPER BIT(7)
178# define CM_PLLD_LOADPER BIT(6)
179# define CM_PLLD_HOLDCORE BIT(5)
180# define CM_PLLD_LOADCORE BIT(4)
181# define CM_PLLD_HOLDDSI1 BIT(3)
182# define CM_PLLD_LOADDSI1 BIT(2)
183# define CM_PLLD_HOLDDSI0 BIT(1)
184# define CM_PLLD_LOADDSI0 BIT(0)
185
186#define CM_PLLH 0x110
187# define CM_PLLH_LOADRCAL BIT(2)
188# define CM_PLLH_LOADAUX BIT(1)
189# define CM_PLLH_LOADPIX BIT(0)
190
191#define CM_LOCK 0x114
192# define CM_LOCK_FLOCKH BIT(12)
193# define CM_LOCK_FLOCKD BIT(11)
194# define CM_LOCK_FLOCKC BIT(10)
195# define CM_LOCK_FLOCKB BIT(9)
196# define CM_LOCK_FLOCKA BIT(8)
197
198#define CM_EVENT 0x118
199#define CM_DSI1ECTL 0x158
200#define CM_DSI1EDIV 0x15c
201#define CM_DSI1PCTL 0x160
202#define CM_DSI1PDIV 0x164
203#define CM_DFTCTL 0x168
204#define CM_DFTDIV 0x16c
205
206#define CM_PLLB 0x170
207# define CM_PLLB_HOLDARM BIT(1)
208# define CM_PLLB_LOADARM BIT(0)
209
210#define A2W_PLLA_CTRL 0x1100
211#define A2W_PLLC_CTRL 0x1120
212#define A2W_PLLD_CTRL 0x1140
213#define A2W_PLLH_CTRL 0x1160
214#define A2W_PLLB_CTRL 0x11e0
215# define A2W_PLL_CTRL_PRST_DISABLE BIT(17)
216# define A2W_PLL_CTRL_PWRDN BIT(16)
217# define A2W_PLL_CTRL_PDIV_MASK 0x000007000
218# define A2W_PLL_CTRL_PDIV_SHIFT 12
219# define A2W_PLL_CTRL_NDIV_MASK 0x0000003ff
220# define A2W_PLL_CTRL_NDIV_SHIFT 0
221
222#define A2W_PLLA_ANA0 0x1010
223#define A2W_PLLC_ANA0 0x1030
224#define A2W_PLLD_ANA0 0x1050
225#define A2W_PLLH_ANA0 0x1070
226#define A2W_PLLB_ANA0 0x10f0
227
228#define A2W_PLL_KA_SHIFT 7
229#define A2W_PLL_KA_MASK GENMASK(9, 7)
230#define A2W_PLL_KI_SHIFT 19
231#define A2W_PLL_KI_MASK GENMASK(21, 19)
232#define A2W_PLL_KP_SHIFT 15
233#define A2W_PLL_KP_MASK GENMASK(18, 15)
234
235#define A2W_PLLH_KA_SHIFT 19
236#define A2W_PLLH_KA_MASK GENMASK(21, 19)
237#define A2W_PLLH_KI_LOW_SHIFT 22
238#define A2W_PLLH_KI_LOW_MASK GENMASK(23, 22)
239#define A2W_PLLH_KI_HIGH_SHIFT 0
240#define A2W_PLLH_KI_HIGH_MASK GENMASK(0, 0)
241#define A2W_PLLH_KP_SHIFT 1
242#define A2W_PLLH_KP_MASK GENMASK(4, 1)
243
244#define A2W_XOSC_CTRL 0x1190
245# define A2W_XOSC_CTRL_PLLB_ENABLE BIT(7)
246# define A2W_XOSC_CTRL_PLLA_ENABLE BIT(6)
247# define A2W_XOSC_CTRL_PLLD_ENABLE BIT(5)
248# define A2W_XOSC_CTRL_DDR_ENABLE BIT(4)
249# define A2W_XOSC_CTRL_CPR1_ENABLE BIT(3)
250# define A2W_XOSC_CTRL_USB_ENABLE BIT(2)
251# define A2W_XOSC_CTRL_HDMI_ENABLE BIT(1)
252# define A2W_XOSC_CTRL_PLLC_ENABLE BIT(0)
253
254#define A2W_PLLA_FRAC 0x1200
255#define A2W_PLLC_FRAC 0x1220
256#define A2W_PLLD_FRAC 0x1240
257#define A2W_PLLH_FRAC 0x1260
258#define A2W_PLLB_FRAC 0x12e0
259# define A2W_PLL_FRAC_MASK ((1 << A2W_PLL_FRAC_BITS) - 1)
260# define A2W_PLL_FRAC_BITS 20
261
262#define A2W_PLL_CHANNEL_DISABLE BIT(8)
263#define A2W_PLL_DIV_BITS 8
264#define A2W_PLL_DIV_SHIFT 0
265
266#define A2W_PLLA_DSI0 0x1300
267#define A2W_PLLA_CORE 0x1400
268#define A2W_PLLA_PER 0x1500
269#define A2W_PLLA_CCP2 0x1600
270
271#define A2W_PLLC_CORE2 0x1320
272#define A2W_PLLC_CORE1 0x1420
273#define A2W_PLLC_PER 0x1520
274#define A2W_PLLC_CORE0 0x1620
275
276#define A2W_PLLD_DSI0 0x1340
277#define A2W_PLLD_CORE 0x1440
278#define A2W_PLLD_PER 0x1540
279#define A2W_PLLD_DSI1 0x1640
280
281#define A2W_PLLH_AUX 0x1360
282#define A2W_PLLH_RCAL 0x1460
283#define A2W_PLLH_PIX 0x1560
284#define A2W_PLLH_STS 0x1660
285
286#define A2W_PLLH_CTRLR 0x1960
287#define A2W_PLLH_FRACR 0x1a60
288#define A2W_PLLH_AUXR 0x1b60
289#define A2W_PLLH_RCALR 0x1c60
290#define A2W_PLLH_PIXR 0x1d60
291#define A2W_PLLH_STSR 0x1e60
292
293#define A2W_PLLB_ARM 0x13e0
294#define A2W_PLLB_SP0 0x14e0
295#define A2W_PLLB_SP1 0x15e0
296#define A2W_PLLB_SP2 0x16e0
297
298#define LOCK_TIMEOUT_NS 100000000
299#define BCM2835_MAX_FB_RATE 1750000000u
300
Eric Anholt8a39e9f2017-01-18 07:31:56 +1100301/*
302 * Names of clocks used within the driver that need to be replaced
303 * with an external parent's name. This array is in the order that
304 * the clocks node in the DT references external clocks.
305 */
306static const char *const cprman_parent_names[] = {
307 "xosc",
308 "dsi0_byte",
309 "dsi0_ddr2",
310 "dsi0_ddr",
311 "dsi1_byte",
312 "dsi1_ddr2",
313 "dsi1_ddr",
314};
315
Eric Anholt41691b82015-10-08 18:37:24 -0700316struct bcm2835_cprman {
317 struct device *dev;
318 void __iomem *regs;
Martin Sperl6e1e60d2016-02-29 11:39:22 +0000319 spinlock_t regs_lock; /* spinlock for all clocks */
Eric Anholt8a39e9f2017-01-18 07:31:56 +1100320
321 /*
322 * Real names of cprman clock parents looked up through
323 * of_clk_get_parent_name(), which will be used in the
324 * parent_names[] arrays for clock registration.
325 */
326 const char *real_parent_names[ARRAY_SIZE(cprman_parent_names)];
Eric Anholt41691b82015-10-08 18:37:24 -0700327
Stephen Boydb19f0092016-06-01 16:15:03 -0700328 /* Must be last */
329 struct clk_hw_onecell_data onecell;
Eric Anholt41691b82015-10-08 18:37:24 -0700330};
331
332static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
333{
334 writel(CM_PASSWORD | val, cprman->regs + reg);
335}
336
337static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
338{
339 return readl(cprman->regs + reg);
340}
Stephen Warren526d2392012-12-24 21:55:01 -0700341
Eric Anholt3f919582017-01-18 07:31:57 +1100342/* Does a cycle of measuring a clock through the TCNT clock, which may
343 * source from many other clocks in the system.
344 */
345static unsigned long bcm2835_measure_tcnt_mux(struct bcm2835_cprman *cprman,
346 u32 tcnt_mux)
347{
348 u32 osccount = 19200; /* 1ms */
349 u32 count;
350 ktime_t timeout;
351
352 spin_lock(&cprman->regs_lock);
353
354 cprman_write(cprman, CM_TCNTCTL, CM_KILL);
355
356 cprman_write(cprman, CM_TCNTCTL,
357 (tcnt_mux & CM_SRC_MASK) |
358 (tcnt_mux >> CM_SRC_BITS) << CM_TCNT_SRC1_SHIFT);
359
360 cprman_write(cprman, CM_OSCCOUNT, osccount);
361
362 /* do a kind delay at the start */
363 mdelay(1);
364
365 /* Finish off whatever is left of OSCCOUNT */
366 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
367 while (cprman_read(cprman, CM_OSCCOUNT)) {
368 if (ktime_after(ktime_get(), timeout)) {
369 dev_err(cprman->dev, "timeout waiting for OSCCOUNT\n");
370 count = 0;
371 goto out;
372 }
373 cpu_relax();
374 }
375
376 /* Wait for BUSY to clear. */
377 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
378 while (cprman_read(cprman, CM_TCNTCTL) & CM_BUSY) {
379 if (ktime_after(ktime_get(), timeout)) {
380 dev_err(cprman->dev, "timeout waiting for !BUSY\n");
381 count = 0;
382 goto out;
383 }
384 cpu_relax();
385 }
386
387 count = cprman_read(cprman, CM_TCNTCNT);
388
389 cprman_write(cprman, CM_TCNTCTL, 0);
390
391out:
392 spin_unlock(&cprman->regs_lock);
393
394 return count * 1000;
395}
396
Martin Sperl96bf9c62016-02-29 14:20:15 +0000397static int bcm2835_debugfs_regset(struct bcm2835_cprman *cprman, u32 base,
398 struct debugfs_reg32 *regs, size_t nregs,
399 struct dentry *dentry)
400{
401 struct dentry *regdump;
402 struct debugfs_regset32 *regset;
403
404 regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL);
405 if (!regset)
406 return -ENOMEM;
407
408 regset->regs = regs;
409 regset->nregs = nregs;
410 regset->base = cprman->regs + base;
411
412 regdump = debugfs_create_regset32("regdump", S_IRUGO, dentry,
413 regset);
414
415 return regdump ? 0 : -ENOMEM;
416}
417
Eric Anholt41691b82015-10-08 18:37:24 -0700418struct bcm2835_pll_data {
419 const char *name;
420 u32 cm_ctrl_reg;
421 u32 a2w_ctrl_reg;
422 u32 frac_reg;
423 u32 ana_reg_base;
424 u32 reference_enable_mask;
425 /* Bit in CM_LOCK to indicate when the PLL has locked. */
426 u32 lock_mask;
427
428 const struct bcm2835_pll_ana_bits *ana;
429
430 unsigned long min_rate;
431 unsigned long max_rate;
432 /*
433 * Highest rate for the VCO before we have to use the
434 * pre-divide-by-2.
435 */
436 unsigned long max_fb_rate;
437};
438
439struct bcm2835_pll_ana_bits {
440 u32 mask0;
441 u32 set0;
442 u32 mask1;
443 u32 set1;
444 u32 mask3;
445 u32 set3;
446 u32 fb_prediv_mask;
447};
448
449static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
450 .mask0 = 0,
451 .set0 = 0,
Boris Brezillon49012d12018-02-08 14:43:35 +0100452 .mask1 = A2W_PLL_KI_MASK | A2W_PLL_KP_MASK,
Eric Anholt41691b82015-10-08 18:37:24 -0700453 .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
Boris Brezillon49012d12018-02-08 14:43:35 +0100454 .mask3 = A2W_PLL_KA_MASK,
Eric Anholt41691b82015-10-08 18:37:24 -0700455 .set3 = (2 << A2W_PLL_KA_SHIFT),
456 .fb_prediv_mask = BIT(14),
457};
458
459static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
Boris Brezillon49012d12018-02-08 14:43:35 +0100460 .mask0 = A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK,
Eric Anholt41691b82015-10-08 18:37:24 -0700461 .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
Boris Brezillon49012d12018-02-08 14:43:35 +0100462 .mask1 = A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK,
Eric Anholt41691b82015-10-08 18:37:24 -0700463 .set1 = (6 << A2W_PLLH_KP_SHIFT),
464 .mask3 = 0,
465 .set3 = 0,
466 .fb_prediv_mask = BIT(11),
467};
468
Eric Anholt41691b82015-10-08 18:37:24 -0700469struct bcm2835_pll_divider_data {
470 const char *name;
Martin Sperl3b15afe2016-02-29 12:51:42 +0000471 const char *source_pll;
472
Eric Anholt41691b82015-10-08 18:37:24 -0700473 u32 cm_reg;
474 u32 a2w_reg;
475
476 u32 load_mask;
477 u32 hold_mask;
478 u32 fixed_divider;
Eric Anholt55486092017-01-18 07:31:55 +1100479 u32 flags;
Eric Anholt41691b82015-10-08 18:37:24 -0700480};
481
Eric Anholt41691b82015-10-08 18:37:24 -0700482struct bcm2835_clock_data {
483 const char *name;
484
485 const char *const *parents;
486 int num_mux_parents;
487
Boris Brezillon155e8b32016-12-01 22:00:19 +0100488 /* Bitmap encoding which parents accept rate change propagation. */
489 unsigned int set_rate_parent;
490
Eric Anholt41691b82015-10-08 18:37:24 -0700491 u32 ctl_reg;
492 u32 div_reg;
493
494 /* Number of integer bits in the divider */
495 u32 int_bits;
496 /* Number of fractional bits in the divider */
497 u32 frac_bits;
498
Eric Anholte69fdcc2016-06-01 12:05:33 -0700499 u32 flags;
500
Eric Anholt41691b82015-10-08 18:37:24 -0700501 bool is_vpu_clock;
Martin Sperl959ca922016-02-29 11:39:21 +0000502 bool is_mash_clock;
Phil Elwell35429762017-06-01 15:14:22 +0100503 bool low_jitter;
Eric Anholt3f919582017-01-18 07:31:57 +1100504
505 u32 tcnt_mux;
Eric Anholt41691b82015-10-08 18:37:24 -0700506};
507
Martin Sperl56eb3a22016-02-29 12:51:41 +0000508struct bcm2835_gate_data {
509 const char *name;
510 const char *parent;
511
512 u32 ctl_reg;
513};
514
Eric Anholt41691b82015-10-08 18:37:24 -0700515struct bcm2835_pll {
516 struct clk_hw hw;
517 struct bcm2835_cprman *cprman;
518 const struct bcm2835_pll_data *data;
519};
520
521static int bcm2835_pll_is_on(struct clk_hw *hw)
522{
523 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
524 struct bcm2835_cprman *cprman = pll->cprman;
525 const struct bcm2835_pll_data *data = pll->data;
526
527 return cprman_read(cprman, data->a2w_ctrl_reg) &
528 A2W_PLL_CTRL_PRST_DISABLE;
529}
530
531static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
532 unsigned long parent_rate,
533 u32 *ndiv, u32 *fdiv)
534{
535 u64 div;
536
537 div = (u64)rate << A2W_PLL_FRAC_BITS;
538 do_div(div, parent_rate);
539
540 *ndiv = div >> A2W_PLL_FRAC_BITS;
541 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
542}
543
544static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
545 u32 ndiv, u32 fdiv, u32 pdiv)
546{
547 u64 rate;
548
549 if (pdiv == 0)
550 return 0;
551
552 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
553 do_div(rate, pdiv);
554 return rate >> A2W_PLL_FRAC_BITS;
555}
556
557static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
558 unsigned long *parent_rate)
559{
Eric Anholtc4e634c2016-09-30 10:07:27 -0700560 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
561 const struct bcm2835_pll_data *data = pll->data;
Eric Anholt41691b82015-10-08 18:37:24 -0700562 u32 ndiv, fdiv;
563
Eric Anholtc4e634c2016-09-30 10:07:27 -0700564 rate = clamp(rate, data->min_rate, data->max_rate);
565
Eric Anholt41691b82015-10-08 18:37:24 -0700566 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
567
568 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
569}
570
571static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
572 unsigned long parent_rate)
573{
574 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
575 struct bcm2835_cprman *cprman = pll->cprman;
576 const struct bcm2835_pll_data *data = pll->data;
577 u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
578 u32 ndiv, pdiv, fdiv;
579 bool using_prediv;
580
581 if (parent_rate == 0)
582 return 0;
583
584 fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
585 ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
586 pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
587 using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
588 data->ana->fb_prediv_mask;
589
Phil Elwelle45098d2017-05-15 10:35:04 -0700590 if (using_prediv) {
Eric Anholt41691b82015-10-08 18:37:24 -0700591 ndiv *= 2;
Phil Elwelle45098d2017-05-15 10:35:04 -0700592 fdiv *= 2;
593 }
Eric Anholt41691b82015-10-08 18:37:24 -0700594
595 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
596}
597
598static void bcm2835_pll_off(struct clk_hw *hw)
599{
600 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
601 struct bcm2835_cprman *cprman = pll->cprman;
602 const struct bcm2835_pll_data *data = pll->data;
603
Martin Sperl6727f082016-02-29 11:39:17 +0000604 spin_lock(&cprman->regs_lock);
Boris Brezillon75387232018-03-22 10:11:30 +0100605 cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);
Martin Sperl6727f082016-02-29 11:39:17 +0000606 cprman_write(cprman, data->a2w_ctrl_reg,
607 cprman_read(cprman, data->a2w_ctrl_reg) |
608 A2W_PLL_CTRL_PWRDN);
609 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700610}
611
612static int bcm2835_pll_on(struct clk_hw *hw)
613{
614 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
615 struct bcm2835_cprman *cprman = pll->cprman;
616 const struct bcm2835_pll_data *data = pll->data;
617 ktime_t timeout;
618
Eric Anholte708b382016-04-13 13:05:03 -0700619 cprman_write(cprman, data->a2w_ctrl_reg,
620 cprman_read(cprman, data->a2w_ctrl_reg) &
621 ~A2W_PLL_CTRL_PWRDN);
622
Eric Anholt41691b82015-10-08 18:37:24 -0700623 /* Take the PLL out of reset. */
Boris Brezillon7997f3b2018-02-08 14:43:36 +0100624 spin_lock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700625 cprman_write(cprman, data->cm_ctrl_reg,
626 cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
Boris Brezillon7997f3b2018-02-08 14:43:36 +0100627 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700628
629 /* Wait for the PLL to lock. */
630 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
631 while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
632 if (ktime_after(ktime_get(), timeout)) {
633 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
634 clk_hw_get_name(hw));
635 return -ETIMEDOUT;
636 }
637
638 cpu_relax();
639 }
640
Boris Brezillon75387232018-03-22 10:11:30 +0100641 cprman_write(cprman, data->a2w_ctrl_reg,
642 cprman_read(cprman, data->a2w_ctrl_reg) |
643 A2W_PLL_CTRL_PRST_DISABLE);
644
Eric Anholt41691b82015-10-08 18:37:24 -0700645 return 0;
646}
647
648static void
649bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
650{
651 int i;
652
653 /*
654 * ANA register setup is done as a series of writes to
655 * ANA3-ANA0, in that order. This lets us write all 4
656 * registers as a single cycle of the serdes interface (taking
657 * 100 xosc clocks), whereas if we were to update ana0, 1, and
658 * 3 individually through their partial-write registers, each
659 * would be their own serdes cycle.
660 */
661 for (i = 3; i >= 0; i--)
662 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
663}
664
665static int bcm2835_pll_set_rate(struct clk_hw *hw,
666 unsigned long rate, unsigned long parent_rate)
667{
668 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
669 struct bcm2835_cprman *cprman = pll->cprman;
670 const struct bcm2835_pll_data *data = pll->data;
671 bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
672 u32 ndiv, fdiv, a2w_ctl;
673 u32 ana[4];
674 int i;
675
Eric Anholt41691b82015-10-08 18:37:24 -0700676 if (rate > data->max_fb_rate) {
677 use_fb_prediv = true;
678 rate /= 2;
679 } else {
680 use_fb_prediv = false;
681 }
682
683 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
684
685 for (i = 3; i >= 0; i--)
686 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
687
688 was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
689
690 ana[0] &= ~data->ana->mask0;
691 ana[0] |= data->ana->set0;
692 ana[1] &= ~data->ana->mask1;
693 ana[1] |= data->ana->set1;
694 ana[3] &= ~data->ana->mask3;
695 ana[3] |= data->ana->set3;
696
697 if (was_using_prediv && !use_fb_prediv) {
698 ana[1] &= ~data->ana->fb_prediv_mask;
699 do_ana_setup_first = true;
700 } else if (!was_using_prediv && use_fb_prediv) {
701 ana[1] |= data->ana->fb_prediv_mask;
702 do_ana_setup_first = false;
703 } else {
704 do_ana_setup_first = true;
705 }
706
707 /* Unmask the reference clock from the oscillator. */
Boris Brezillon7997f3b2018-02-08 14:43:36 +0100708 spin_lock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700709 cprman_write(cprman, A2W_XOSC_CTRL,
710 cprman_read(cprman, A2W_XOSC_CTRL) |
711 data->reference_enable_mask);
Boris Brezillon7997f3b2018-02-08 14:43:36 +0100712 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700713
714 if (do_ana_setup_first)
715 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
716
717 /* Set the PLL multiplier from the oscillator. */
718 cprman_write(cprman, data->frac_reg, fdiv);
719
720 a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
721 a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
722 a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
723 a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
724 a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
725 cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
726
727 if (!do_ana_setup_first)
728 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
729
730 return 0;
731}
732
Martin Sperl96bf9c62016-02-29 14:20:15 +0000733static int bcm2835_pll_debug_init(struct clk_hw *hw,
734 struct dentry *dentry)
735{
736 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
737 struct bcm2835_cprman *cprman = pll->cprman;
738 const struct bcm2835_pll_data *data = pll->data;
739 struct debugfs_reg32 *regs;
740
741 regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
742 if (!regs)
743 return -ENOMEM;
744
745 regs[0].name = "cm_ctrl";
746 regs[0].offset = data->cm_ctrl_reg;
747 regs[1].name = "a2w_ctrl";
748 regs[1].offset = data->a2w_ctrl_reg;
749 regs[2].name = "frac";
750 regs[2].offset = data->frac_reg;
751 regs[3].name = "ana0";
752 regs[3].offset = data->ana_reg_base + 0 * 4;
753 regs[4].name = "ana1";
754 regs[4].offset = data->ana_reg_base + 1 * 4;
755 regs[5].name = "ana2";
756 regs[5].offset = data->ana_reg_base + 2 * 4;
757 regs[6].name = "ana3";
758 regs[6].offset = data->ana_reg_base + 3 * 4;
759
760 return bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry);
761}
762
Eric Anholt41691b82015-10-08 18:37:24 -0700763static const struct clk_ops bcm2835_pll_clk_ops = {
764 .is_prepared = bcm2835_pll_is_on,
765 .prepare = bcm2835_pll_on,
766 .unprepare = bcm2835_pll_off,
767 .recalc_rate = bcm2835_pll_get_rate,
768 .set_rate = bcm2835_pll_set_rate,
769 .round_rate = bcm2835_pll_round_rate,
Martin Sperl96bf9c62016-02-29 14:20:15 +0000770 .debug_init = bcm2835_pll_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -0700771};
772
773struct bcm2835_pll_divider {
774 struct clk_divider div;
775 struct bcm2835_cprman *cprman;
776 const struct bcm2835_pll_divider_data *data;
777};
778
779static struct bcm2835_pll_divider *
780bcm2835_pll_divider_from_hw(struct clk_hw *hw)
781{
782 return container_of(hw, struct bcm2835_pll_divider, div.hw);
783}
784
785static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
786{
787 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
788 struct bcm2835_cprman *cprman = divider->cprman;
789 const struct bcm2835_pll_divider_data *data = divider->data;
790
791 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
792}
793
794static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
795 unsigned long rate,
796 unsigned long *parent_rate)
797{
798 return clk_divider_ops.round_rate(hw, rate, parent_rate);
799}
800
801static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
802 unsigned long parent_rate)
803{
Eric Anholt79c1e2f2016-02-15 19:03:58 -0800804 return clk_divider_ops.recalc_rate(hw, parent_rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700805}
806
807static void bcm2835_pll_divider_off(struct clk_hw *hw)
808{
809 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
810 struct bcm2835_cprman *cprman = divider->cprman;
811 const struct bcm2835_pll_divider_data *data = divider->data;
812
Martin Sperlec36a5c2016-02-29 11:39:18 +0000813 spin_lock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700814 cprman_write(cprman, data->cm_reg,
815 (cprman_read(cprman, data->cm_reg) &
816 ~data->load_mask) | data->hold_mask);
Boris Brezillon68af4fa2016-12-01 20:27:21 +0100817 cprman_write(cprman, data->a2w_reg,
818 cprman_read(cprman, data->a2w_reg) |
819 A2W_PLL_CHANNEL_DISABLE);
Martin Sperlec36a5c2016-02-29 11:39:18 +0000820 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700821}
822
823static int bcm2835_pll_divider_on(struct clk_hw *hw)
824{
825 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
826 struct bcm2835_cprman *cprman = divider->cprman;
827 const struct bcm2835_pll_divider_data *data = divider->data;
828
Martin Sperlec36a5c2016-02-29 11:39:18 +0000829 spin_lock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700830 cprman_write(cprman, data->a2w_reg,
831 cprman_read(cprman, data->a2w_reg) &
832 ~A2W_PLL_CHANNEL_DISABLE);
833
834 cprman_write(cprman, data->cm_reg,
835 cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
Martin Sperlec36a5c2016-02-29 11:39:18 +0000836 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700837
838 return 0;
839}
840
841static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
842 unsigned long rate,
843 unsigned long parent_rate)
844{
845 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
846 struct bcm2835_cprman *cprman = divider->cprman;
847 const struct bcm2835_pll_divider_data *data = divider->data;
Eric Anholt773b3962016-02-15 19:03:57 -0800848 u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
Eric Anholt41691b82015-10-08 18:37:24 -0700849
Eric Anholt773b3962016-02-15 19:03:57 -0800850 div = DIV_ROUND_UP_ULL(parent_rate, rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700851
Eric Anholt773b3962016-02-15 19:03:57 -0800852 div = min(div, max_div);
853 if (div == max_div)
854 div = 0;
855
856 cprman_write(cprman, data->a2w_reg, div);
Eric Anholt41691b82015-10-08 18:37:24 -0700857 cm = cprman_read(cprman, data->cm_reg);
858 cprman_write(cprman, data->cm_reg, cm | data->load_mask);
859 cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
860
861 return 0;
862}
863
Martin Sperl96bf9c62016-02-29 14:20:15 +0000864static int bcm2835_pll_divider_debug_init(struct clk_hw *hw,
865 struct dentry *dentry)
866{
867 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
868 struct bcm2835_cprman *cprman = divider->cprman;
869 const struct bcm2835_pll_divider_data *data = divider->data;
870 struct debugfs_reg32 *regs;
871
872 regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
873 if (!regs)
874 return -ENOMEM;
875
876 regs[0].name = "cm";
877 regs[0].offset = data->cm_reg;
878 regs[1].name = "a2w";
879 regs[1].offset = data->a2w_reg;
880
881 return bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry);
882}
883
Eric Anholt41691b82015-10-08 18:37:24 -0700884static const struct clk_ops bcm2835_pll_divider_clk_ops = {
885 .is_prepared = bcm2835_pll_divider_is_on,
886 .prepare = bcm2835_pll_divider_on,
887 .unprepare = bcm2835_pll_divider_off,
888 .recalc_rate = bcm2835_pll_divider_get_rate,
889 .set_rate = bcm2835_pll_divider_set_rate,
890 .round_rate = bcm2835_pll_divider_round_rate,
Martin Sperl96bf9c62016-02-29 14:20:15 +0000891 .debug_init = bcm2835_pll_divider_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -0700892};
893
894/*
895 * The CM dividers do fixed-point division, so we can't use the
896 * generic integer divider code like the PLL dividers do (and we can't
897 * fake it by having some fixed shifts preceding it in the clock tree,
898 * because we'd run out of bits in a 32-bit unsigned long).
899 */
900struct bcm2835_clock {
901 struct clk_hw hw;
902 struct bcm2835_cprman *cprman;
903 const struct bcm2835_clock_data *data;
904};
905
906static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
907{
908 return container_of(hw, struct bcm2835_clock, hw);
909}
910
911static int bcm2835_clock_is_on(struct clk_hw *hw)
912{
913 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
914 struct bcm2835_cprman *cprman = clock->cprman;
915 const struct bcm2835_clock_data *data = clock->data;
916
917 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
918}
919
920static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
921 unsigned long rate,
Remi Pommarel9c95b322015-12-06 17:22:46 +0100922 unsigned long parent_rate,
923 bool round_up)
Eric Anholt41691b82015-10-08 18:37:24 -0700924{
925 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
926 const struct bcm2835_clock_data *data = clock->data;
Remi Pommarel9c95b322015-12-06 17:22:46 +0100927 u32 unused_frac_mask =
928 GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
Eric Anholt41691b82015-10-08 18:37:24 -0700929 u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
Remi Pommarel9c95b322015-12-06 17:22:46 +0100930 u64 rem;
Martin Sperl959ca922016-02-29 11:39:21 +0000931 u32 div, mindiv, maxdiv;
Eric Anholt41691b82015-10-08 18:37:24 -0700932
Remi Pommarel9c95b322015-12-06 17:22:46 +0100933 rem = do_div(temp, rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700934 div = temp;
935
Remi Pommarel9c95b322015-12-06 17:22:46 +0100936 /* Round up and mask off the unused bits */
937 if (round_up && ((div & unused_frac_mask) != 0 || rem != 0))
938 div += unused_frac_mask + 1;
939 div &= ~unused_frac_mask;
Eric Anholt41691b82015-10-08 18:37:24 -0700940
Martin Sperl959ca922016-02-29 11:39:21 +0000941 /* different clamping limits apply for a mash clock */
942 if (data->is_mash_clock) {
943 /* clamp to min divider of 2 */
944 mindiv = 2 << CM_DIV_FRAC_BITS;
945 /* clamp to the highest possible integer divider */
946 maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
947 } else {
948 /* clamp to min divider of 1 */
949 mindiv = 1 << CM_DIV_FRAC_BITS;
950 /* clamp to the highest possible fractional divider */
951 maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
952 CM_DIV_FRAC_BITS - data->frac_bits);
953 }
954
955 /* apply the clamping limits */
956 div = max_t(u32, div, mindiv);
957 div = min_t(u32, div, maxdiv);
Eric Anholt41691b82015-10-08 18:37:24 -0700958
959 return div;
960}
961
962static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
963 unsigned long parent_rate,
964 u32 div)
965{
966 const struct bcm2835_clock_data *data = clock->data;
967 u64 temp;
968
Eric Anholt8a39e9f2017-01-18 07:31:56 +1100969 if (data->int_bits == 0 && data->frac_bits == 0)
970 return parent_rate;
971
Eric Anholt41691b82015-10-08 18:37:24 -0700972 /*
973 * The divisor is a 12.12 fixed point field, but only some of
974 * the bits are populated in any given clock.
975 */
976 div >>= CM_DIV_FRAC_BITS - data->frac_bits;
977 div &= (1 << (data->int_bits + data->frac_bits)) - 1;
978
979 if (div == 0)
980 return 0;
981
982 temp = (u64)parent_rate << data->frac_bits;
983
984 do_div(temp, div);
985
986 return temp;
987}
988
Eric Anholt41691b82015-10-08 18:37:24 -0700989static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
990 unsigned long parent_rate)
991{
992 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
993 struct bcm2835_cprman *cprman = clock->cprman;
994 const struct bcm2835_clock_data *data = clock->data;
Eric Anholt8a39e9f2017-01-18 07:31:56 +1100995 u32 div;
996
997 if (data->int_bits == 0 && data->frac_bits == 0)
998 return parent_rate;
999
1000 div = cprman_read(cprman, data->div_reg);
Eric Anholt41691b82015-10-08 18:37:24 -07001001
1002 return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
1003}
1004
1005static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
1006{
1007 struct bcm2835_cprman *cprman = clock->cprman;
1008 const struct bcm2835_clock_data *data = clock->data;
1009 ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
1010
1011 while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
1012 if (ktime_after(ktime_get(), timeout)) {
1013 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
1014 clk_hw_get_name(&clock->hw));
1015 return;
1016 }
1017 cpu_relax();
1018 }
1019}
1020
1021static void bcm2835_clock_off(struct clk_hw *hw)
1022{
1023 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1024 struct bcm2835_cprman *cprman = clock->cprman;
1025 const struct bcm2835_clock_data *data = clock->data;
1026
1027 spin_lock(&cprman->regs_lock);
1028 cprman_write(cprman, data->ctl_reg,
1029 cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
1030 spin_unlock(&cprman->regs_lock);
1031
1032 /* BUSY will remain high until the divider completes its cycle. */
1033 bcm2835_clock_wait_busy(clock);
1034}
1035
1036static int bcm2835_clock_on(struct clk_hw *hw)
1037{
1038 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1039 struct bcm2835_cprman *cprman = clock->cprman;
1040 const struct bcm2835_clock_data *data = clock->data;
1041
1042 spin_lock(&cprman->regs_lock);
1043 cprman_write(cprman, data->ctl_reg,
1044 cprman_read(cprman, data->ctl_reg) |
1045 CM_ENABLE |
1046 CM_GATE);
1047 spin_unlock(&cprman->regs_lock);
1048
Eric Anholt3f919582017-01-18 07:31:57 +11001049 /* Debug code to measure the clock once it's turned on to see
1050 * if it's ticking at the rate we expect.
1051 */
1052 if (data->tcnt_mux && false) {
1053 dev_info(cprman->dev,
1054 "clk %s: rate %ld, measure %ld\n",
1055 data->name,
1056 clk_hw_get_rate(hw),
1057 bcm2835_measure_tcnt_mux(cprman, data->tcnt_mux));
1058 }
1059
Eric Anholt41691b82015-10-08 18:37:24 -07001060 return 0;
1061}
1062
1063static int bcm2835_clock_set_rate(struct clk_hw *hw,
1064 unsigned long rate, unsigned long parent_rate)
1065{
1066 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1067 struct bcm2835_cprman *cprman = clock->cprman;
1068 const struct bcm2835_clock_data *data = clock->data;
Remi Pommarel9c95b322015-12-06 17:22:46 +01001069 u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false);
Martin Sperl959ca922016-02-29 11:39:21 +00001070 u32 ctl;
1071
1072 spin_lock(&cprman->regs_lock);
1073
1074 /*
1075 * Setting up frac support
1076 *
1077 * In principle it is recommended to stop/start the clock first,
1078 * but as we set CLK_SET_RATE_GATE during registration of the
1079 * clock this requirement should be take care of by the
1080 * clk-framework.
1081 */
1082 ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
1083 ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
1084 cprman_write(cprman, data->ctl_reg, ctl);
Eric Anholt41691b82015-10-08 18:37:24 -07001085
1086 cprman_write(cprman, data->div_reg, div);
1087
Martin Sperl959ca922016-02-29 11:39:21 +00001088 spin_unlock(&cprman->regs_lock);
1089
Eric Anholt41691b82015-10-08 18:37:24 -07001090 return 0;
1091}
1092
Eric Anholt67615c52016-06-01 12:05:36 -07001093static bool
1094bcm2835_clk_is_pllc(struct clk_hw *hw)
1095{
1096 if (!hw)
1097 return false;
1098
1099 return strncmp(clk_hw_get_name(hw), "pllc", 4) == 0;
1100}
1101
Boris Brezillon155e8b32016-12-01 22:00:19 +01001102static unsigned long bcm2835_clock_choose_div_and_prate(struct clk_hw *hw,
1103 int parent_idx,
1104 unsigned long rate,
1105 u32 *div,
Phil Elwell35429762017-06-01 15:14:22 +01001106 unsigned long *prate,
1107 unsigned long *avgrate)
Boris Brezillon155e8b32016-12-01 22:00:19 +01001108{
1109 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1110 struct bcm2835_cprman *cprman = clock->cprman;
1111 const struct bcm2835_clock_data *data = clock->data;
Boris Brezillon2aab7a22016-12-12 09:00:53 +01001112 unsigned long best_rate = 0;
Boris Brezillon155e8b32016-12-01 22:00:19 +01001113 u32 curdiv, mindiv, maxdiv;
1114 struct clk_hw *parent;
1115
1116 parent = clk_hw_get_parent_by_index(hw, parent_idx);
1117
1118 if (!(BIT(parent_idx) & data->set_rate_parent)) {
1119 *prate = clk_hw_get_rate(parent);
1120 *div = bcm2835_clock_choose_div(hw, rate, *prate, true);
1121
Phil Elwell35429762017-06-01 15:14:22 +01001122 *avgrate = bcm2835_clock_rate_from_divisor(clock, *prate, *div);
1123
1124 if (data->low_jitter && (*div & CM_DIV_FRAC_MASK)) {
1125 unsigned long high, low;
1126 u32 int_div = *div & ~CM_DIV_FRAC_MASK;
1127
1128 high = bcm2835_clock_rate_from_divisor(clock, *prate,
1129 int_div);
1130 int_div += CM_DIV_FRAC_MASK + 1;
1131 low = bcm2835_clock_rate_from_divisor(clock, *prate,
1132 int_div);
1133
1134 /*
1135 * Return a value which is the maximum deviation
1136 * below the ideal rate, for use as a metric.
1137 */
1138 return *avgrate - max(*avgrate - low, high - *avgrate);
1139 }
1140 return *avgrate;
Boris Brezillon155e8b32016-12-01 22:00:19 +01001141 }
1142
1143 if (data->frac_bits)
1144 dev_warn(cprman->dev,
1145 "frac bits are not used when propagating rate change");
1146
1147 /* clamp to min divider of 2 if we're dealing with a mash clock */
1148 mindiv = data->is_mash_clock ? 2 : 1;
1149 maxdiv = BIT(data->int_bits) - 1;
1150
1151 /* TODO: Be smart, and only test a subset of the available divisors. */
1152 for (curdiv = mindiv; curdiv <= maxdiv; curdiv++) {
1153 unsigned long tmp_rate;
1154
1155 tmp_rate = clk_hw_round_rate(parent, rate * curdiv);
1156 tmp_rate /= curdiv;
1157 if (curdiv == mindiv ||
1158 (tmp_rate > best_rate && tmp_rate <= rate))
1159 best_rate = tmp_rate;
1160
1161 if (best_rate == rate)
1162 break;
1163 }
1164
1165 *div = curdiv << CM_DIV_FRAC_BITS;
1166 *prate = curdiv * best_rate;
Phil Elwell35429762017-06-01 15:14:22 +01001167 *avgrate = best_rate;
Boris Brezillon155e8b32016-12-01 22:00:19 +01001168
1169 return best_rate;
1170}
1171
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001172static int bcm2835_clock_determine_rate(struct clk_hw *hw,
Martin Sperl6e1e60d2016-02-29 11:39:22 +00001173 struct clk_rate_request *req)
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001174{
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001175 struct clk_hw *parent, *best_parent = NULL;
Eric Anholt67615c52016-06-01 12:05:36 -07001176 bool current_parent_is_pllc;
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001177 unsigned long rate, best_rate = 0;
1178 unsigned long prate, best_prate = 0;
Phil Elwell35429762017-06-01 15:14:22 +01001179 unsigned long avgrate, best_avgrate = 0;
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001180 size_t i;
1181 u32 div;
1182
Eric Anholt67615c52016-06-01 12:05:36 -07001183 current_parent_is_pllc = bcm2835_clk_is_pllc(clk_hw_get_parent(hw));
1184
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001185 /*
1186 * Select parent clock that results in the closest but lower rate
1187 */
1188 for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
1189 parent = clk_hw_get_parent_by_index(hw, i);
1190 if (!parent)
1191 continue;
Eric Anholt67615c52016-06-01 12:05:36 -07001192
1193 /*
1194 * Don't choose a PLLC-derived clock as our parent
1195 * unless it had been manually set that way. PLLC's
1196 * frequency gets adjusted by the firmware due to
1197 * over-temp or under-voltage conditions, without
1198 * prior notification to our clock consumer.
1199 */
1200 if (bcm2835_clk_is_pllc(parent) && !current_parent_is_pllc)
1201 continue;
1202
Boris Brezillon155e8b32016-12-01 22:00:19 +01001203 rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate,
Phil Elwell35429762017-06-01 15:14:22 +01001204 &div, &prate,
1205 &avgrate);
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001206 if (rate > best_rate && rate <= req->rate) {
1207 best_parent = parent;
1208 best_prate = prate;
1209 best_rate = rate;
Phil Elwell35429762017-06-01 15:14:22 +01001210 best_avgrate = avgrate;
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001211 }
1212 }
1213
1214 if (!best_parent)
1215 return -EINVAL;
1216
1217 req->best_parent_hw = best_parent;
1218 req->best_parent_rate = best_prate;
1219
Phil Elwell35429762017-06-01 15:14:22 +01001220 req->rate = best_avgrate;
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001221
1222 return 0;
1223}
1224
1225static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
1226{
1227 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1228 struct bcm2835_cprman *cprman = clock->cprman;
1229 const struct bcm2835_clock_data *data = clock->data;
1230 u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
1231
1232 cprman_write(cprman, data->ctl_reg, src);
1233 return 0;
1234}
1235
1236static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
1237{
1238 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1239 struct bcm2835_cprman *cprman = clock->cprman;
1240 const struct bcm2835_clock_data *data = clock->data;
1241 u32 src = cprman_read(cprman, data->ctl_reg);
1242
1243 return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
1244}
1245
Martin Sperl96bf9c62016-02-29 14:20:15 +00001246static struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = {
1247 {
1248 .name = "ctl",
1249 .offset = 0,
1250 },
1251 {
1252 .name = "div",
1253 .offset = 4,
1254 },
1255};
1256
1257static int bcm2835_clock_debug_init(struct clk_hw *hw,
1258 struct dentry *dentry)
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 return bcm2835_debugfs_regset(
1265 cprman, data->ctl_reg,
1266 bcm2835_debugfs_clock_reg32,
1267 ARRAY_SIZE(bcm2835_debugfs_clock_reg32),
1268 dentry);
1269}
1270
Eric Anholt41691b82015-10-08 18:37:24 -07001271static const struct clk_ops bcm2835_clock_clk_ops = {
1272 .is_prepared = bcm2835_clock_is_on,
1273 .prepare = bcm2835_clock_on,
1274 .unprepare = bcm2835_clock_off,
1275 .recalc_rate = bcm2835_clock_get_rate,
1276 .set_rate = bcm2835_clock_set_rate,
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001277 .determine_rate = bcm2835_clock_determine_rate,
1278 .set_parent = bcm2835_clock_set_parent,
1279 .get_parent = bcm2835_clock_get_parent,
Martin Sperl96bf9c62016-02-29 14:20:15 +00001280 .debug_init = bcm2835_clock_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -07001281};
1282
1283static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1284{
1285 return true;
1286}
1287
1288/*
1289 * The VPU clock can never be disabled (it doesn't have an ENABLE
1290 * bit), so it gets its own set of clock ops.
1291 */
1292static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1293 .is_prepared = bcm2835_vpu_clock_is_on,
1294 .recalc_rate = bcm2835_clock_get_rate,
1295 .set_rate = bcm2835_clock_set_rate,
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001296 .determine_rate = bcm2835_clock_determine_rate,
1297 .set_parent = bcm2835_clock_set_parent,
1298 .get_parent = bcm2835_clock_get_parent,
Martin Sperl96bf9c62016-02-29 14:20:15 +00001299 .debug_init = bcm2835_clock_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -07001300};
1301
Stephen Boydb19f0092016-06-01 16:15:03 -07001302static struct clk_hw *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1303 const struct bcm2835_pll_data *data)
Eric Anholt41691b82015-10-08 18:37:24 -07001304{
1305 struct bcm2835_pll *pll;
1306 struct clk_init_data init;
Stephen Boydb19f0092016-06-01 16:15:03 -07001307 int ret;
Eric Anholt41691b82015-10-08 18:37:24 -07001308
1309 memset(&init, 0, sizeof(init));
1310
1311 /* All of the PLLs derive from the external oscillator. */
Eric Anholt8a39e9f2017-01-18 07:31:56 +11001312 init.parent_names = &cprman->real_parent_names[0];
Eric Anholt41691b82015-10-08 18:37:24 -07001313 init.num_parents = 1;
1314 init.name = data->name;
1315 init.ops = &bcm2835_pll_clk_ops;
1316 init.flags = CLK_IGNORE_UNUSED;
1317
1318 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1319 if (!pll)
1320 return NULL;
1321
1322 pll->cprman = cprman;
1323 pll->data = data;
1324 pll->hw.init = &init;
1325
Stephen Boydb19f0092016-06-01 16:15:03 -07001326 ret = devm_clk_hw_register(cprman->dev, &pll->hw);
1327 if (ret)
1328 return NULL;
1329 return &pll->hw;
Eric Anholt41691b82015-10-08 18:37:24 -07001330}
1331
Stephen Boydb19f0092016-06-01 16:15:03 -07001332static struct clk_hw *
Eric Anholt41691b82015-10-08 18:37:24 -07001333bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1334 const struct bcm2835_pll_divider_data *data)
1335{
1336 struct bcm2835_pll_divider *divider;
1337 struct clk_init_data init;
Eric Anholt41691b82015-10-08 18:37:24 -07001338 const char *divider_name;
Stephen Boydb19f0092016-06-01 16:15:03 -07001339 int ret;
Eric Anholt41691b82015-10-08 18:37:24 -07001340
1341 if (data->fixed_divider != 1) {
1342 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1343 "%s_prediv", data->name);
1344 if (!divider_name)
1345 return NULL;
1346 } else {
1347 divider_name = data->name;
1348 }
1349
1350 memset(&init, 0, sizeof(init));
1351
Martin Sperl3b15afe2016-02-29 12:51:42 +00001352 init.parent_names = &data->source_pll;
Eric Anholt41691b82015-10-08 18:37:24 -07001353 init.num_parents = 1;
1354 init.name = divider_name;
1355 init.ops = &bcm2835_pll_divider_clk_ops;
Eric Anholt55486092017-01-18 07:31:55 +11001356 init.flags = data->flags | CLK_IGNORE_UNUSED;
Eric Anholt41691b82015-10-08 18:37:24 -07001357
1358 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1359 if (!divider)
1360 return NULL;
1361
1362 divider->div.reg = cprman->regs + data->a2w_reg;
1363 divider->div.shift = A2W_PLL_DIV_SHIFT;
1364 divider->div.width = A2W_PLL_DIV_BITS;
Eric Anholt79c1e2f2016-02-15 19:03:58 -08001365 divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
Eric Anholt41691b82015-10-08 18:37:24 -07001366 divider->div.lock = &cprman->regs_lock;
1367 divider->div.hw.init = &init;
1368 divider->div.table = NULL;
1369
1370 divider->cprman = cprman;
1371 divider->data = data;
1372
Stephen Boydb19f0092016-06-01 16:15:03 -07001373 ret = devm_clk_hw_register(cprman->dev, &divider->div.hw);
1374 if (ret)
1375 return ERR_PTR(ret);
Eric Anholt41691b82015-10-08 18:37:24 -07001376
1377 /*
1378 * PLLH's channels have a fixed divide by 10 afterwards, which
1379 * is what our consumers are actually using.
1380 */
1381 if (data->fixed_divider != 1) {
Stephen Boydb19f0092016-06-01 16:15:03 -07001382 return clk_hw_register_fixed_factor(cprman->dev, data->name,
1383 divider_name,
1384 CLK_SET_RATE_PARENT,
1385 1,
1386 data->fixed_divider);
Eric Anholt41691b82015-10-08 18:37:24 -07001387 }
1388
Stephen Boydb19f0092016-06-01 16:15:03 -07001389 return &divider->div.hw;
Eric Anholt41691b82015-10-08 18:37:24 -07001390}
1391
Stephen Boydb19f0092016-06-01 16:15:03 -07001392static struct clk_hw *bcm2835_register_clock(struct bcm2835_cprman *cprman,
Eric Anholt41691b82015-10-08 18:37:24 -07001393 const struct bcm2835_clock_data *data)
1394{
1395 struct bcm2835_clock *clock;
1396 struct clk_init_data init;
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001397 const char *parents[1 << CM_SRC_BITS];
Yisheng Xie99e71542018-05-31 19:11:13 +08001398 size_t i;
Stephen Boydb19f0092016-06-01 16:15:03 -07001399 int ret;
Eric Anholt41691b82015-10-08 18:37:24 -07001400
1401 /*
Eric Anholt8a39e9f2017-01-18 07:31:56 +11001402 * Replace our strings referencing parent clocks with the
1403 * actual clock-output-name of the parent.
Eric Anholt41691b82015-10-08 18:37:24 -07001404 */
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001405 for (i = 0; i < data->num_mux_parents; i++) {
Eric Anholt8a39e9f2017-01-18 07:31:56 +11001406 parents[i] = data->parents[i];
1407
Yisheng Xie99e71542018-05-31 19:11:13 +08001408 ret = match_string(cprman_parent_names,
1409 ARRAY_SIZE(cprman_parent_names),
1410 parents[i]);
1411 if (ret >= 0)
1412 parents[i] = cprman->real_parent_names[ret];
Eric Anholt41691b82015-10-08 18:37:24 -07001413 }
1414
1415 memset(&init, 0, sizeof(init));
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001416 init.parent_names = parents;
1417 init.num_parents = data->num_mux_parents;
Eric Anholt41691b82015-10-08 18:37:24 -07001418 init.name = data->name;
Eric Anholte69fdcc2016-06-01 12:05:33 -07001419 init.flags = data->flags | CLK_IGNORE_UNUSED;
Eric Anholt41691b82015-10-08 18:37:24 -07001420
Boris Brezillon155e8b32016-12-01 22:00:19 +01001421 /*
1422 * Pass the CLK_SET_RATE_PARENT flag if we are allowed to propagate
1423 * rate changes on at least of the parents.
1424 */
1425 if (data->set_rate_parent)
1426 init.flags |= CLK_SET_RATE_PARENT;
1427
Eric Anholt41691b82015-10-08 18:37:24 -07001428 if (data->is_vpu_clock) {
1429 init.ops = &bcm2835_vpu_clock_clk_ops;
1430 } else {
1431 init.ops = &bcm2835_clock_clk_ops;
1432 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
Eric Anholteddcbe832016-06-01 12:05:34 -07001433
1434 /* If the clock wasn't actually enabled at boot, it's not
1435 * critical.
1436 */
1437 if (!(cprman_read(cprman, data->ctl_reg) & CM_ENABLE))
1438 init.flags &= ~CLK_IS_CRITICAL;
Eric Anholt41691b82015-10-08 18:37:24 -07001439 }
1440
1441 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1442 if (!clock)
1443 return NULL;
1444
1445 clock->cprman = cprman;
1446 clock->data = data;
1447 clock->hw.init = &init;
1448
Stephen Boydb19f0092016-06-01 16:15:03 -07001449 ret = devm_clk_hw_register(cprman->dev, &clock->hw);
1450 if (ret)
1451 return ERR_PTR(ret);
1452 return &clock->hw;
Eric Anholt41691b82015-10-08 18:37:24 -07001453}
1454
Martin Sperl56eb3a22016-02-29 12:51:41 +00001455static struct clk *bcm2835_register_gate(struct bcm2835_cprman *cprman,
1456 const struct bcm2835_gate_data *data)
1457{
1458 return clk_register_gate(cprman->dev, data->name, data->parent,
1459 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1460 cprman->regs + data->ctl_reg,
1461 CM_GATE_BIT, 0, &cprman->regs_lock);
1462}
1463
Stephen Boydb19f0092016-06-01 16:15:03 -07001464typedef struct clk_hw *(*bcm2835_clk_register)(struct bcm2835_cprman *cprman,
1465 const void *data);
Martin Sperl56eb3a22016-02-29 12:51:41 +00001466struct bcm2835_clk_desc {
1467 bcm2835_clk_register clk_register;
1468 const void *data;
1469};
1470
Martin Sperl3b15afe2016-02-29 12:51:42 +00001471/* assignment helper macros for different clock types */
1472#define _REGISTER(f, ...) { .clk_register = (bcm2835_clk_register)f, \
1473 .data = __VA_ARGS__ }
1474#define REGISTER_PLL(...) _REGISTER(&bcm2835_register_pll, \
1475 &(struct bcm2835_pll_data) \
1476 {__VA_ARGS__})
1477#define REGISTER_PLL_DIV(...) _REGISTER(&bcm2835_register_pll_divider, \
1478 &(struct bcm2835_pll_divider_data) \
1479 {__VA_ARGS__})
1480#define REGISTER_CLK(...) _REGISTER(&bcm2835_register_clock, \
1481 &(struct bcm2835_clock_data) \
1482 {__VA_ARGS__})
1483#define REGISTER_GATE(...) _REGISTER(&bcm2835_register_gate, \
1484 &(struct bcm2835_gate_data) \
1485 {__VA_ARGS__})
Martin Sperl56eb3a22016-02-29 12:51:41 +00001486
Martin Sperl3b15afe2016-02-29 12:51:42 +00001487/* parent mux arrays plus helper macros */
1488
1489/* main oscillator parent mux */
1490static const char *const bcm2835_clock_osc_parents[] = {
1491 "gnd",
1492 "xosc",
1493 "testdebug0",
1494 "testdebug1"
1495};
1496
1497#define REGISTER_OSC_CLK(...) REGISTER_CLK( \
1498 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), \
1499 .parents = bcm2835_clock_osc_parents, \
1500 __VA_ARGS__)
1501
1502/* main peripherial parent mux */
1503static const char *const bcm2835_clock_per_parents[] = {
1504 "gnd",
1505 "xosc",
1506 "testdebug0",
1507 "testdebug1",
1508 "plla_per",
1509 "pllc_per",
1510 "plld_per",
1511 "pllh_aux",
1512};
1513
1514#define REGISTER_PER_CLK(...) REGISTER_CLK( \
1515 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), \
1516 .parents = bcm2835_clock_per_parents, \
1517 __VA_ARGS__)
1518
Phil Elwell8c0de582017-06-01 15:14:16 +01001519/*
1520 * Restrict clock sources for the PCM peripheral to the oscillator and
1521 * PLLD_PER because other source may have varying rates or be switched
1522 * off.
1523 *
1524 * Prevent other sources from being selected by replacing their names in
1525 * the list of potential parents with dummy entries (entry index is
1526 * significant).
1527 */
1528static const char *const bcm2835_pcm_per_parents[] = {
1529 "-",
1530 "xosc",
1531 "-",
1532 "-",
1533 "-",
1534 "-",
1535 "plld_per",
1536 "-",
1537};
1538
1539#define REGISTER_PCM_CLK(...) REGISTER_CLK( \
1540 .num_mux_parents = ARRAY_SIZE(bcm2835_pcm_per_parents), \
1541 .parents = bcm2835_pcm_per_parents, \
1542 __VA_ARGS__)
1543
Martin Sperl3b15afe2016-02-29 12:51:42 +00001544/* main vpu parent mux */
1545static const char *const bcm2835_clock_vpu_parents[] = {
1546 "gnd",
1547 "xosc",
1548 "testdebug0",
1549 "testdebug1",
1550 "plla_core",
1551 "pllc_core0",
1552 "plld_core",
1553 "pllh_aux",
1554 "pllc_core1",
1555 "pllc_core2",
1556};
1557
1558#define REGISTER_VPU_CLK(...) REGISTER_CLK( \
1559 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), \
1560 .parents = bcm2835_clock_vpu_parents, \
1561 __VA_ARGS__)
1562
1563/*
Eric Anholt8a39e9f2017-01-18 07:31:56 +11001564 * DSI parent clocks. The DSI byte/DDR/DDR2 clocks come from the DSI
1565 * analog PHY. The _inv variants are generated internally to cprman,
1566 * but we don't use them so they aren't hooked up.
1567 */
1568static const char *const bcm2835_clock_dsi0_parents[] = {
1569 "gnd",
1570 "xosc",
1571 "testdebug0",
1572 "testdebug1",
1573 "dsi0_ddr",
1574 "dsi0_ddr_inv",
1575 "dsi0_ddr2",
1576 "dsi0_ddr2_inv",
1577 "dsi0_byte",
1578 "dsi0_byte_inv",
1579};
1580
1581static const char *const bcm2835_clock_dsi1_parents[] = {
1582 "gnd",
1583 "xosc",
1584 "testdebug0",
1585 "testdebug1",
1586 "dsi1_ddr",
1587 "dsi1_ddr_inv",
1588 "dsi1_ddr2",
1589 "dsi1_ddr2_inv",
1590 "dsi1_byte",
1591 "dsi1_byte_inv",
1592};
1593
1594#define REGISTER_DSI0_CLK(...) REGISTER_CLK( \
1595 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi0_parents), \
1596 .parents = bcm2835_clock_dsi0_parents, \
1597 __VA_ARGS__)
1598
1599#define REGISTER_DSI1_CLK(...) REGISTER_CLK( \
1600 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi1_parents), \
1601 .parents = bcm2835_clock_dsi1_parents, \
1602 __VA_ARGS__)
1603
1604/*
Martin Sperl3b15afe2016-02-29 12:51:42 +00001605 * the real definition of all the pll, pll_dividers and clocks
1606 * these make use of the above REGISTER_* macros
1607 */
Martin Sperl56eb3a22016-02-29 12:51:41 +00001608static const struct bcm2835_clk_desc clk_desc_array[] = {
Martin Sperl3b15afe2016-02-29 12:51:42 +00001609 /* the PLL + PLL dividers */
1610
1611 /*
1612 * PLLA is the auxiliary PLL, used to drive the CCP2
1613 * (Compact Camera Port 2) transmitter clock.
1614 *
1615 * It is in the PX LDO power domain, which is on when the
1616 * AUDIO domain is on.
1617 */
1618 [BCM2835_PLLA] = REGISTER_PLL(
1619 .name = "plla",
1620 .cm_ctrl_reg = CM_PLLA,
1621 .a2w_ctrl_reg = A2W_PLLA_CTRL,
1622 .frac_reg = A2W_PLLA_FRAC,
1623 .ana_reg_base = A2W_PLLA_ANA0,
1624 .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
1625 .lock_mask = CM_LOCK_FLOCKA,
1626
1627 .ana = &bcm2835_ana_default,
1628
1629 .min_rate = 600000000u,
1630 .max_rate = 2400000000u,
1631 .max_fb_rate = BCM2835_MAX_FB_RATE),
1632 [BCM2835_PLLA_CORE] = REGISTER_PLL_DIV(
1633 .name = "plla_core",
1634 .source_pll = "plla",
1635 .cm_reg = CM_PLLA,
1636 .a2w_reg = A2W_PLLA_CORE,
1637 .load_mask = CM_PLLA_LOADCORE,
1638 .hold_mask = CM_PLLA_HOLDCORE,
Eric Anholt55486092017-01-18 07:31:55 +11001639 .fixed_divider = 1,
1640 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001641 [BCM2835_PLLA_PER] = REGISTER_PLL_DIV(
1642 .name = "plla_per",
1643 .source_pll = "plla",
1644 .cm_reg = CM_PLLA,
1645 .a2w_reg = A2W_PLLA_PER,
1646 .load_mask = CM_PLLA_LOADPER,
1647 .hold_mask = CM_PLLA_HOLDPER,
Eric Anholt55486092017-01-18 07:31:55 +11001648 .fixed_divider = 1,
1649 .flags = CLK_SET_RATE_PARENT),
Martin Sperl72843692016-02-29 15:43:56 +00001650 [BCM2835_PLLA_DSI0] = REGISTER_PLL_DIV(
1651 .name = "plla_dsi0",
1652 .source_pll = "plla",
1653 .cm_reg = CM_PLLA,
1654 .a2w_reg = A2W_PLLA_DSI0,
1655 .load_mask = CM_PLLA_LOADDSI0,
1656 .hold_mask = CM_PLLA_HOLDDSI0,
1657 .fixed_divider = 1),
1658 [BCM2835_PLLA_CCP2] = REGISTER_PLL_DIV(
1659 .name = "plla_ccp2",
1660 .source_pll = "plla",
1661 .cm_reg = CM_PLLA,
1662 .a2w_reg = A2W_PLLA_CCP2,
1663 .load_mask = CM_PLLA_LOADCCP2,
1664 .hold_mask = CM_PLLA_HOLDCCP2,
Eric Anholt55486092017-01-18 07:31:55 +11001665 .fixed_divider = 1,
1666 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001667
1668 /* PLLB is used for the ARM's clock. */
1669 [BCM2835_PLLB] = REGISTER_PLL(
1670 .name = "pllb",
1671 .cm_ctrl_reg = CM_PLLB,
1672 .a2w_ctrl_reg = A2W_PLLB_CTRL,
1673 .frac_reg = A2W_PLLB_FRAC,
1674 .ana_reg_base = A2W_PLLB_ANA0,
1675 .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
1676 .lock_mask = CM_LOCK_FLOCKB,
1677
1678 .ana = &bcm2835_ana_default,
1679
1680 .min_rate = 600000000u,
1681 .max_rate = 3000000000u,
1682 .max_fb_rate = BCM2835_MAX_FB_RATE),
1683 [BCM2835_PLLB_ARM] = REGISTER_PLL_DIV(
1684 .name = "pllb_arm",
1685 .source_pll = "pllb",
1686 .cm_reg = CM_PLLB,
1687 .a2w_reg = A2W_PLLB_ARM,
1688 .load_mask = CM_PLLB_LOADARM,
1689 .hold_mask = CM_PLLB_HOLDARM,
Eric Anholt55486092017-01-18 07:31:55 +11001690 .fixed_divider = 1,
1691 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001692
1693 /*
1694 * PLLC is the core PLL, used to drive the core VPU clock.
1695 *
1696 * It is in the PX LDO power domain, which is on when the
1697 * AUDIO domain is on.
1698 */
1699 [BCM2835_PLLC] = REGISTER_PLL(
1700 .name = "pllc",
1701 .cm_ctrl_reg = CM_PLLC,
1702 .a2w_ctrl_reg = A2W_PLLC_CTRL,
1703 .frac_reg = A2W_PLLC_FRAC,
1704 .ana_reg_base = A2W_PLLC_ANA0,
1705 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1706 .lock_mask = CM_LOCK_FLOCKC,
1707
1708 .ana = &bcm2835_ana_default,
1709
1710 .min_rate = 600000000u,
1711 .max_rate = 3000000000u,
1712 .max_fb_rate = BCM2835_MAX_FB_RATE),
1713 [BCM2835_PLLC_CORE0] = REGISTER_PLL_DIV(
1714 .name = "pllc_core0",
1715 .source_pll = "pllc",
1716 .cm_reg = CM_PLLC,
1717 .a2w_reg = A2W_PLLC_CORE0,
1718 .load_mask = CM_PLLC_LOADCORE0,
1719 .hold_mask = CM_PLLC_HOLDCORE0,
Eric Anholt55486092017-01-18 07:31:55 +11001720 .fixed_divider = 1,
1721 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001722 [BCM2835_PLLC_CORE1] = REGISTER_PLL_DIV(
1723 .name = "pllc_core1",
1724 .source_pll = "pllc",
1725 .cm_reg = CM_PLLC,
1726 .a2w_reg = A2W_PLLC_CORE1,
1727 .load_mask = CM_PLLC_LOADCORE1,
1728 .hold_mask = CM_PLLC_HOLDCORE1,
Eric Anholt55486092017-01-18 07:31:55 +11001729 .fixed_divider = 1,
1730 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001731 [BCM2835_PLLC_CORE2] = REGISTER_PLL_DIV(
1732 .name = "pllc_core2",
1733 .source_pll = "pllc",
1734 .cm_reg = CM_PLLC,
1735 .a2w_reg = A2W_PLLC_CORE2,
1736 .load_mask = CM_PLLC_LOADCORE2,
1737 .hold_mask = CM_PLLC_HOLDCORE2,
Eric Anholt55486092017-01-18 07:31:55 +11001738 .fixed_divider = 1,
1739 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001740 [BCM2835_PLLC_PER] = REGISTER_PLL_DIV(
1741 .name = "pllc_per",
1742 .source_pll = "pllc",
1743 .cm_reg = CM_PLLC,
1744 .a2w_reg = A2W_PLLC_PER,
1745 .load_mask = CM_PLLC_LOADPER,
1746 .hold_mask = CM_PLLC_HOLDPER,
Eric Anholt55486092017-01-18 07:31:55 +11001747 .fixed_divider = 1,
1748 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001749
1750 /*
1751 * PLLD is the display PLL, used to drive DSI display panels.
1752 *
1753 * It is in the PX LDO power domain, which is on when the
1754 * AUDIO domain is on.
1755 */
1756 [BCM2835_PLLD] = REGISTER_PLL(
1757 .name = "plld",
1758 .cm_ctrl_reg = CM_PLLD,
1759 .a2w_ctrl_reg = A2W_PLLD_CTRL,
1760 .frac_reg = A2W_PLLD_FRAC,
1761 .ana_reg_base = A2W_PLLD_ANA0,
1762 .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
1763 .lock_mask = CM_LOCK_FLOCKD,
1764
1765 .ana = &bcm2835_ana_default,
1766
1767 .min_rate = 600000000u,
1768 .max_rate = 2400000000u,
1769 .max_fb_rate = BCM2835_MAX_FB_RATE),
1770 [BCM2835_PLLD_CORE] = REGISTER_PLL_DIV(
1771 .name = "plld_core",
1772 .source_pll = "plld",
1773 .cm_reg = CM_PLLD,
1774 .a2w_reg = A2W_PLLD_CORE,
1775 .load_mask = CM_PLLD_LOADCORE,
1776 .hold_mask = CM_PLLD_HOLDCORE,
Eric Anholt55486092017-01-18 07:31:55 +11001777 .fixed_divider = 1,
1778 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001779 [BCM2835_PLLD_PER] = REGISTER_PLL_DIV(
1780 .name = "plld_per",
1781 .source_pll = "plld",
1782 .cm_reg = CM_PLLD,
1783 .a2w_reg = A2W_PLLD_PER,
1784 .load_mask = CM_PLLD_LOADPER,
1785 .hold_mask = CM_PLLD_HOLDPER,
Eric Anholt55486092017-01-18 07:31:55 +11001786 .fixed_divider = 1,
1787 .flags = CLK_SET_RATE_PARENT),
Martin Sperl72843692016-02-29 15:43:56 +00001788 [BCM2835_PLLD_DSI0] = REGISTER_PLL_DIV(
1789 .name = "plld_dsi0",
1790 .source_pll = "plld",
1791 .cm_reg = CM_PLLD,
1792 .a2w_reg = A2W_PLLD_DSI0,
1793 .load_mask = CM_PLLD_LOADDSI0,
1794 .hold_mask = CM_PLLD_HOLDDSI0,
1795 .fixed_divider = 1),
1796 [BCM2835_PLLD_DSI1] = REGISTER_PLL_DIV(
1797 .name = "plld_dsi1",
1798 .source_pll = "plld",
1799 .cm_reg = CM_PLLD,
1800 .a2w_reg = A2W_PLLD_DSI1,
1801 .load_mask = CM_PLLD_LOADDSI1,
1802 .hold_mask = CM_PLLD_HOLDDSI1,
1803 .fixed_divider = 1),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001804
1805 /*
1806 * PLLH is used to supply the pixel clock or the AUX clock for the
1807 * TV encoder.
1808 *
1809 * It is in the HDMI power domain.
1810 */
1811 [BCM2835_PLLH] = REGISTER_PLL(
1812 "pllh",
1813 .cm_ctrl_reg = CM_PLLH,
1814 .a2w_ctrl_reg = A2W_PLLH_CTRL,
1815 .frac_reg = A2W_PLLH_FRAC,
1816 .ana_reg_base = A2W_PLLH_ANA0,
1817 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1818 .lock_mask = CM_LOCK_FLOCKH,
1819
1820 .ana = &bcm2835_ana_pllh,
1821
1822 .min_rate = 600000000u,
1823 .max_rate = 3000000000u,
1824 .max_fb_rate = BCM2835_MAX_FB_RATE),
1825 [BCM2835_PLLH_RCAL] = REGISTER_PLL_DIV(
1826 .name = "pllh_rcal",
1827 .source_pll = "pllh",
1828 .cm_reg = CM_PLLH,
1829 .a2w_reg = A2W_PLLH_RCAL,
1830 .load_mask = CM_PLLH_LOADRCAL,
1831 .hold_mask = 0,
Eric Anholt55486092017-01-18 07:31:55 +11001832 .fixed_divider = 10,
1833 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001834 [BCM2835_PLLH_AUX] = REGISTER_PLL_DIV(
1835 .name = "pllh_aux",
1836 .source_pll = "pllh",
1837 .cm_reg = CM_PLLH,
1838 .a2w_reg = A2W_PLLH_AUX,
1839 .load_mask = CM_PLLH_LOADAUX,
1840 .hold_mask = 0,
Eric Anholt55486092017-01-18 07:31:55 +11001841 .fixed_divider = 1,
1842 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001843 [BCM2835_PLLH_PIX] = REGISTER_PLL_DIV(
1844 .name = "pllh_pix",
1845 .source_pll = "pllh",
1846 .cm_reg = CM_PLLH,
1847 .a2w_reg = A2W_PLLH_PIX,
1848 .load_mask = CM_PLLH_LOADPIX,
1849 .hold_mask = 0,
Eric Anholt55486092017-01-18 07:31:55 +11001850 .fixed_divider = 10,
1851 .flags = CLK_SET_RATE_PARENT),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001852
Martin Sperl56eb3a22016-02-29 12:51:41 +00001853 /* the clocks */
Martin Sperl3b15afe2016-02-29 12:51:42 +00001854
1855 /* clocks with oscillator parent mux */
1856
1857 /* One Time Programmable Memory clock. Maximum 10Mhz. */
1858 [BCM2835_CLOCK_OTP] = REGISTER_OSC_CLK(
1859 .name = "otp",
1860 .ctl_reg = CM_OTPCTL,
1861 .div_reg = CM_OTPDIV,
1862 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001863 .frac_bits = 0,
1864 .tcnt_mux = 6),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001865 /*
1866 * Used for a 1Mhz clock for the system clocksource, and also used
1867 * bythe watchdog timer and the camera pulse generator.
1868 */
1869 [BCM2835_CLOCK_TIMER] = REGISTER_OSC_CLK(
1870 .name = "timer",
1871 .ctl_reg = CM_TIMERCTL,
1872 .div_reg = CM_TIMERDIV,
1873 .int_bits = 6,
1874 .frac_bits = 12),
1875 /*
1876 * Clock for the temperature sensor.
1877 * Generally run at 2Mhz, max 5Mhz.
1878 */
1879 [BCM2835_CLOCK_TSENS] = REGISTER_OSC_CLK(
1880 .name = "tsens",
1881 .ctl_reg = CM_TSENSCTL,
1882 .div_reg = CM_TSENSDIV,
1883 .int_bits = 5,
1884 .frac_bits = 0),
Martin Sperld3d6f152016-02-29 15:43:57 +00001885 [BCM2835_CLOCK_TEC] = REGISTER_OSC_CLK(
1886 .name = "tec",
1887 .ctl_reg = CM_TECCTL,
1888 .div_reg = CM_TECDIV,
1889 .int_bits = 6,
1890 .frac_bits = 0),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001891
1892 /* clocks with vpu parent mux */
1893 [BCM2835_CLOCK_H264] = REGISTER_VPU_CLK(
1894 .name = "h264",
1895 .ctl_reg = CM_H264CTL,
1896 .div_reg = CM_H264DIV,
1897 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001898 .frac_bits = 8,
1899 .tcnt_mux = 1),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001900 [BCM2835_CLOCK_ISP] = REGISTER_VPU_CLK(
1901 .name = "isp",
1902 .ctl_reg = CM_ISPCTL,
1903 .div_reg = CM_ISPDIV,
1904 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001905 .frac_bits = 8,
1906 .tcnt_mux = 2),
Martin Sperld3d6f152016-02-29 15:43:57 +00001907
Martin Sperl3b15afe2016-02-29 12:51:42 +00001908 /*
1909 * Secondary SDRAM clock. Used for low-voltage modes when the PLL
1910 * in the SDRAM controller can't be used.
1911 */
1912 [BCM2835_CLOCK_SDRAM] = REGISTER_VPU_CLK(
1913 .name = "sdram",
1914 .ctl_reg = CM_SDCCTL,
1915 .div_reg = CM_SDCDIV,
1916 .int_bits = 6,
Eric Anholt3f919582017-01-18 07:31:57 +11001917 .frac_bits = 0,
1918 .tcnt_mux = 3),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001919 [BCM2835_CLOCK_V3D] = REGISTER_VPU_CLK(
1920 .name = "v3d",
1921 .ctl_reg = CM_V3DCTL,
1922 .div_reg = CM_V3DDIV,
1923 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001924 .frac_bits = 8,
1925 .tcnt_mux = 4),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001926 /*
1927 * VPU clock. This doesn't have an enable bit, since it drives
1928 * the bus for everything else, and is special so it doesn't need
1929 * to be gated for rate changes. It is also known as "clk_audio"
1930 * in various hardware documentation.
1931 */
1932 [BCM2835_CLOCK_VPU] = REGISTER_VPU_CLK(
1933 .name = "vpu",
1934 .ctl_reg = CM_VPUCTL,
1935 .div_reg = CM_VPUDIV,
1936 .int_bits = 12,
1937 .frac_bits = 8,
Eric Anholte69fdcc2016-06-01 12:05:33 -07001938 .flags = CLK_IS_CRITICAL,
Eric Anholt3f919582017-01-18 07:31:57 +11001939 .is_vpu_clock = true,
1940 .tcnt_mux = 5),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001941
1942 /* clocks with per parent mux */
Martin Sperld3d6f152016-02-29 15:43:57 +00001943 [BCM2835_CLOCK_AVEO] = REGISTER_PER_CLK(
1944 .name = "aveo",
1945 .ctl_reg = CM_AVEOCTL,
1946 .div_reg = CM_AVEODIV,
1947 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001948 .frac_bits = 0,
1949 .tcnt_mux = 38),
Martin Sperld3d6f152016-02-29 15:43:57 +00001950 [BCM2835_CLOCK_CAM0] = REGISTER_PER_CLK(
1951 .name = "cam0",
1952 .ctl_reg = CM_CAM0CTL,
1953 .div_reg = CM_CAM0DIV,
1954 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001955 .frac_bits = 8,
1956 .tcnt_mux = 14),
Martin Sperld3d6f152016-02-29 15:43:57 +00001957 [BCM2835_CLOCK_CAM1] = REGISTER_PER_CLK(
1958 .name = "cam1",
1959 .ctl_reg = CM_CAM1CTL,
1960 .div_reg = CM_CAM1DIV,
1961 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001962 .frac_bits = 8,
1963 .tcnt_mux = 15),
Martin Sperld3d6f152016-02-29 15:43:57 +00001964 [BCM2835_CLOCK_DFT] = REGISTER_PER_CLK(
1965 .name = "dft",
1966 .ctl_reg = CM_DFTCTL,
1967 .div_reg = CM_DFTDIV,
1968 .int_bits = 5,
1969 .frac_bits = 0),
1970 [BCM2835_CLOCK_DPI] = REGISTER_PER_CLK(
1971 .name = "dpi",
1972 .ctl_reg = CM_DPICTL,
1973 .div_reg = CM_DPIDIV,
1974 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001975 .frac_bits = 8,
1976 .tcnt_mux = 17),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001977
1978 /* Arasan EMMC clock */
1979 [BCM2835_CLOCK_EMMC] = REGISTER_PER_CLK(
1980 .name = "emmc",
1981 .ctl_reg = CM_EMMCCTL,
1982 .div_reg = CM_EMMCDIV,
1983 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11001984 .frac_bits = 8,
1985 .tcnt_mux = 39),
Martin Sperld3d6f152016-02-29 15:43:57 +00001986
1987 /* General purpose (GPIO) clocks */
1988 [BCM2835_CLOCK_GP0] = REGISTER_PER_CLK(
1989 .name = "gp0",
1990 .ctl_reg = CM_GP0CTL,
1991 .div_reg = CM_GP0DIV,
1992 .int_bits = 12,
1993 .frac_bits = 12,
Eric Anholt3f919582017-01-18 07:31:57 +11001994 .is_mash_clock = true,
1995 .tcnt_mux = 20),
Martin Sperld3d6f152016-02-29 15:43:57 +00001996 [BCM2835_CLOCK_GP1] = REGISTER_PER_CLK(
1997 .name = "gp1",
1998 .ctl_reg = CM_GP1CTL,
1999 .div_reg = CM_GP1DIV,
2000 .int_bits = 12,
2001 .frac_bits = 12,
Eric Anholteddcbe832016-06-01 12:05:34 -07002002 .flags = CLK_IS_CRITICAL,
Eric Anholt3f919582017-01-18 07:31:57 +11002003 .is_mash_clock = true,
2004 .tcnt_mux = 21),
Martin Sperld3d6f152016-02-29 15:43:57 +00002005 [BCM2835_CLOCK_GP2] = REGISTER_PER_CLK(
2006 .name = "gp2",
2007 .ctl_reg = CM_GP2CTL,
2008 .div_reg = CM_GP2DIV,
2009 .int_bits = 12,
Eric Anholteddcbe832016-06-01 12:05:34 -07002010 .frac_bits = 12,
2011 .flags = CLK_IS_CRITICAL),
Martin Sperld3d6f152016-02-29 15:43:57 +00002012
Martin Sperl3b15afe2016-02-29 12:51:42 +00002013 /* HDMI state machine */
2014 [BCM2835_CLOCK_HSM] = REGISTER_PER_CLK(
2015 .name = "hsm",
2016 .ctl_reg = CM_HSMCTL,
2017 .div_reg = CM_HSMDIV,
2018 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11002019 .frac_bits = 8,
2020 .tcnt_mux = 22),
Phil Elwell8c0de582017-06-01 15:14:16 +01002021 [BCM2835_CLOCK_PCM] = REGISTER_PCM_CLK(
Martin Sperl33b68962016-02-29 12:51:43 +00002022 .name = "pcm",
2023 .ctl_reg = CM_PCMCTL,
2024 .div_reg = CM_PCMDIV,
2025 .int_bits = 12,
2026 .frac_bits = 12,
Eric Anholt3f919582017-01-18 07:31:57 +11002027 .is_mash_clock = true,
Phil Elwell35429762017-06-01 15:14:22 +01002028 .low_jitter = true,
Eric Anholt3f919582017-01-18 07:31:57 +11002029 .tcnt_mux = 23),
Martin Sperl3b15afe2016-02-29 12:51:42 +00002030 [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK(
2031 .name = "pwm",
2032 .ctl_reg = CM_PWMCTL,
2033 .div_reg = CM_PWMDIV,
2034 .int_bits = 12,
2035 .frac_bits = 12,
Eric Anholt3f919582017-01-18 07:31:57 +11002036 .is_mash_clock = true,
2037 .tcnt_mux = 24),
Martin Sperld3d6f152016-02-29 15:43:57 +00002038 [BCM2835_CLOCK_SLIM] = REGISTER_PER_CLK(
2039 .name = "slim",
2040 .ctl_reg = CM_SLIMCTL,
2041 .div_reg = CM_SLIMDIV,
2042 .int_bits = 12,
2043 .frac_bits = 12,
Eric Anholt3f919582017-01-18 07:31:57 +11002044 .is_mash_clock = true,
2045 .tcnt_mux = 25),
Martin Sperld3d6f152016-02-29 15:43:57 +00002046 [BCM2835_CLOCK_SMI] = REGISTER_PER_CLK(
2047 .name = "smi",
2048 .ctl_reg = CM_SMICTL,
2049 .div_reg = CM_SMIDIV,
2050 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11002051 .frac_bits = 8,
2052 .tcnt_mux = 27),
Martin Sperl3b15afe2016-02-29 12:51:42 +00002053 [BCM2835_CLOCK_UART] = REGISTER_PER_CLK(
2054 .name = "uart",
2055 .ctl_reg = CM_UARTCTL,
2056 .div_reg = CM_UARTDIV,
2057 .int_bits = 10,
Eric Anholt3f919582017-01-18 07:31:57 +11002058 .frac_bits = 12,
2059 .tcnt_mux = 28),
Martin Sperld3d6f152016-02-29 15:43:57 +00002060
Martin Sperl3b15afe2016-02-29 12:51:42 +00002061 /* TV encoder clock. Only operating frequency is 108Mhz. */
2062 [BCM2835_CLOCK_VEC] = REGISTER_PER_CLK(
2063 .name = "vec",
2064 .ctl_reg = CM_VECCTL,
2065 .div_reg = CM_VECDIV,
2066 .int_bits = 4,
Boris Brezillond86d46a2016-12-01 22:00:20 +01002067 .frac_bits = 0,
2068 /*
2069 * Allow rate change propagation only on PLLH_AUX which is
2070 * assigned index 7 in the parent array.
2071 */
Eric Anholt3f919582017-01-18 07:31:57 +11002072 .set_rate_parent = BIT(7),
2073 .tcnt_mux = 29),
Martin Sperl3b15afe2016-02-29 12:51:42 +00002074
Martin Sperld3d6f152016-02-29 15:43:57 +00002075 /* dsi clocks */
2076 [BCM2835_CLOCK_DSI0E] = REGISTER_PER_CLK(
2077 .name = "dsi0e",
2078 .ctl_reg = CM_DSI0ECTL,
2079 .div_reg = CM_DSI0EDIV,
2080 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11002081 .frac_bits = 8,
2082 .tcnt_mux = 18),
Martin Sperld3d6f152016-02-29 15:43:57 +00002083 [BCM2835_CLOCK_DSI1E] = REGISTER_PER_CLK(
2084 .name = "dsi1e",
2085 .ctl_reg = CM_DSI1ECTL,
2086 .div_reg = CM_DSI1EDIV,
2087 .int_bits = 4,
Eric Anholt3f919582017-01-18 07:31:57 +11002088 .frac_bits = 8,
2089 .tcnt_mux = 19),
Eric Anholt8a39e9f2017-01-18 07:31:56 +11002090 [BCM2835_CLOCK_DSI0P] = REGISTER_DSI0_CLK(
2091 .name = "dsi0p",
2092 .ctl_reg = CM_DSI0PCTL,
2093 .div_reg = CM_DSI0PDIV,
2094 .int_bits = 0,
Eric Anholt3f919582017-01-18 07:31:57 +11002095 .frac_bits = 0,
2096 .tcnt_mux = 12),
Eric Anholt8a39e9f2017-01-18 07:31:56 +11002097 [BCM2835_CLOCK_DSI1P] = REGISTER_DSI1_CLK(
2098 .name = "dsi1p",
2099 .ctl_reg = CM_DSI1PCTL,
2100 .div_reg = CM_DSI1PDIV,
2101 .int_bits = 0,
Eric Anholt3f919582017-01-18 07:31:57 +11002102 .frac_bits = 0,
2103 .tcnt_mux = 13),
Martin Sperld3d6f152016-02-29 15:43:57 +00002104
Martin Sperl56eb3a22016-02-29 12:51:41 +00002105 /* the gates */
Martin Sperl3b15afe2016-02-29 12:51:42 +00002106
2107 /*
2108 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
2109 * you have the debug bit set in the power manager, which we
2110 * don't bother exposing) are individual gates off of the
2111 * non-stop vpu clock.
2112 */
Martin Sperl56eb3a22016-02-29 12:51:41 +00002113 [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE(
Martin Sperl3b15afe2016-02-29 12:51:42 +00002114 .name = "peri_image",
2115 .parent = "vpu",
2116 .ctl_reg = CM_PERIICTL),
Martin Sperl56eb3a22016-02-29 12:51:41 +00002117};
2118
Eric Anholt9e400c52016-06-01 12:05:35 -07002119/*
2120 * Permanently take a reference on the parent of the SDRAM clock.
2121 *
2122 * While the SDRAM is being driven by its dedicated PLL most of the
2123 * time, there is a little loop running in the firmware that
2124 * periodically switches the SDRAM to using our CM clock to do PVT
2125 * recalibration, with the assumption that the previously configured
2126 * SDRAM parent is still enabled and running.
2127 */
2128static int bcm2835_mark_sdc_parent_critical(struct clk *sdc)
2129{
2130 struct clk *parent = clk_get_parent(sdc);
2131
2132 if (IS_ERR(parent))
2133 return PTR_ERR(parent);
2134
2135 return clk_prepare_enable(parent);
2136}
2137
Eric Anholt41691b82015-10-08 18:37:24 -07002138static int bcm2835_clk_probe(struct platform_device *pdev)
2139{
2140 struct device *dev = &pdev->dev;
Stephen Boydb19f0092016-06-01 16:15:03 -07002141 struct clk_hw **hws;
Eric Anholt41691b82015-10-08 18:37:24 -07002142 struct bcm2835_cprman *cprman;
2143 struct resource *res;
Martin Sperl56eb3a22016-02-29 12:51:41 +00002144 const struct bcm2835_clk_desc *desc;
2145 const size_t asize = ARRAY_SIZE(clk_desc_array);
2146 size_t i;
Eric Anholt9e400c52016-06-01 12:05:35 -07002147 int ret;
Eric Anholt41691b82015-10-08 18:37:24 -07002148
Stephen Boydb19f0092016-06-01 16:15:03 -07002149 cprman = devm_kzalloc(dev, sizeof(*cprman) +
2150 sizeof(*cprman->onecell.hws) * asize,
Martin Sperl56eb3a22016-02-29 12:51:41 +00002151 GFP_KERNEL);
Eric Anholt41691b82015-10-08 18:37:24 -07002152 if (!cprman)
2153 return -ENOMEM;
2154
2155 spin_lock_init(&cprman->regs_lock);
2156 cprman->dev = dev;
2157 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2158 cprman->regs = devm_ioremap_resource(dev, res);
2159 if (IS_ERR(cprman->regs))
2160 return PTR_ERR(cprman->regs);
2161
Eric Anholt8a39e9f2017-01-18 07:31:56 +11002162 memcpy(cprman->real_parent_names, cprman_parent_names,
2163 sizeof(cprman_parent_names));
2164 of_clk_parent_fill(dev->of_node, cprman->real_parent_names,
2165 ARRAY_SIZE(cprman_parent_names));
2166
2167 /*
2168 * Make sure the external oscillator has been registered.
2169 *
2170 * The other (DSI) clocks are not present on older device
2171 * trees, which we still need to support for backwards
2172 * compatibility.
2173 */
2174 if (!cprman->real_parent_names[0])
Eric Anholt41691b82015-10-08 18:37:24 -07002175 return -ENODEV;
2176
2177 platform_set_drvdata(pdev, cprman);
2178
Stephen Boydb19f0092016-06-01 16:15:03 -07002179 cprman->onecell.num = asize;
2180 hws = cprman->onecell.hws;
Eric Anholt41691b82015-10-08 18:37:24 -07002181
Martin Sperl56eb3a22016-02-29 12:51:41 +00002182 for (i = 0; i < asize; i++) {
2183 desc = &clk_desc_array[i];
2184 if (desc->clk_register && desc->data)
Stephen Boydb19f0092016-06-01 16:15:03 -07002185 hws[i] = desc->clk_register(cprman, desc->data);
Martin Sperl56eb3a22016-02-29 12:51:41 +00002186 }
Remi Pommarelcfbab8f2015-12-06 17:22:48 +01002187
Stephen Boydb19f0092016-06-01 16:15:03 -07002188 ret = bcm2835_mark_sdc_parent_critical(hws[BCM2835_CLOCK_SDRAM]->clk);
Eric Anholt9e400c52016-06-01 12:05:35 -07002189 if (ret)
2190 return ret;
2191
Stephen Boydb19f0092016-06-01 16:15:03 -07002192 return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
2193 &cprman->onecell);
Eric Anholt41691b82015-10-08 18:37:24 -07002194}
2195
2196static const struct of_device_id bcm2835_clk_of_match[] = {
2197 { .compatible = "brcm,bcm2835-cprman", },
2198 {}
2199};
2200MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
2201
2202static struct platform_driver bcm2835_clk_driver = {
2203 .driver = {
2204 .name = "bcm2835-clk",
2205 .of_match_table = bcm2835_clk_of_match,
2206 },
2207 .probe = bcm2835_clk_probe,
2208};
2209
2210builtin_platform_driver(bcm2835_clk_driver);
2211
2212MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
2213MODULE_DESCRIPTION("BCM2835 clock driver");
2214MODULE_LICENSE("GPL v2");