blob: d9db03cb3fd84163f320dca6f2e533c61f61b5ad [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>
39#include <linux/clk/bcm2835.h>
Martin Sperl96bf9c62016-02-29 14:20:15 +000040#include <linux/debugfs.h>
Eric Anholt41691b82015-10-08 18:37:24 -070041#include <linux/module.h>
Stephen Warren526d2392012-12-24 21:55:01 -070042#include <linux/of.h>
Eric Anholt41691b82015-10-08 18:37:24 -070043#include <linux/platform_device.h>
44#include <linux/slab.h>
45#include <dt-bindings/clock/bcm2835.h>
46
47#define CM_PASSWORD 0x5a000000
48
49#define CM_GNRICCTL 0x000
50#define CM_GNRICDIV 0x004
51# define CM_DIV_FRAC_BITS 12
Martin Sperl959ca922016-02-29 11:39:21 +000052# define CM_DIV_FRAC_MASK GENMASK(CM_DIV_FRAC_BITS - 1, 0)
Eric Anholt41691b82015-10-08 18:37:24 -070053
54#define CM_VPUCTL 0x008
55#define CM_VPUDIV 0x00c
56#define CM_SYSCTL 0x010
57#define CM_SYSDIV 0x014
58#define CM_PERIACTL 0x018
59#define CM_PERIADIV 0x01c
60#define CM_PERIICTL 0x020
61#define CM_PERIIDIV 0x024
62#define CM_H264CTL 0x028
63#define CM_H264DIV 0x02c
64#define CM_ISPCTL 0x030
65#define CM_ISPDIV 0x034
66#define CM_V3DCTL 0x038
67#define CM_V3DDIV 0x03c
68#define CM_CAM0CTL 0x040
69#define CM_CAM0DIV 0x044
70#define CM_CAM1CTL 0x048
71#define CM_CAM1DIV 0x04c
72#define CM_CCP2CTL 0x050
73#define CM_CCP2DIV 0x054
74#define CM_DSI0ECTL 0x058
75#define CM_DSI0EDIV 0x05c
76#define CM_DSI0PCTL 0x060
77#define CM_DSI0PDIV 0x064
78#define CM_DPICTL 0x068
79#define CM_DPIDIV 0x06c
80#define CM_GP0CTL 0x070
81#define CM_GP0DIV 0x074
82#define CM_GP1CTL 0x078
83#define CM_GP1DIV 0x07c
84#define CM_GP2CTL 0x080
85#define CM_GP2DIV 0x084
86#define CM_HSMCTL 0x088
87#define CM_HSMDIV 0x08c
88#define CM_OTPCTL 0x090
89#define CM_OTPDIV 0x094
Martin Sperl2103a212015-12-22 20:13:08 +000090#define CM_PCMCTL 0x098
91#define CM_PCMDIV 0x09c
Eric Anholt41691b82015-10-08 18:37:24 -070092#define CM_PWMCTL 0x0a0
93#define CM_PWMDIV 0x0a4
Martin Sperl2103a212015-12-22 20:13:08 +000094#define CM_SLIMCTL 0x0a8
95#define CM_SLIMDIV 0x0ac
Eric Anholt41691b82015-10-08 18:37:24 -070096#define CM_SMICTL 0x0b0
97#define CM_SMIDIV 0x0b4
Martin Sperl2103a212015-12-22 20:13:08 +000098/* no definition for 0x0b8 and 0x0bc */
99#define CM_TCNTCTL 0x0c0
100#define CM_TCNTDIV 0x0c4
101#define CM_TECCTL 0x0c8
102#define CM_TECDIV 0x0cc
103#define CM_TD0CTL 0x0d0
104#define CM_TD0DIV 0x0d4
105#define CM_TD1CTL 0x0d8
106#define CM_TD1DIV 0x0dc
Eric Anholt41691b82015-10-08 18:37:24 -0700107#define CM_TSENSCTL 0x0e0
108#define CM_TSENSDIV 0x0e4
109#define CM_TIMERCTL 0x0e8
110#define CM_TIMERDIV 0x0ec
111#define CM_UARTCTL 0x0f0
112#define CM_UARTDIV 0x0f4
113#define CM_VECCTL 0x0f8
114#define CM_VECDIV 0x0fc
115#define CM_PULSECTL 0x190
116#define CM_PULSEDIV 0x194
117#define CM_SDCCTL 0x1a8
118#define CM_SDCDIV 0x1ac
119#define CM_ARMCTL 0x1b0
Martin Sperld3d6f152016-02-29 15:43:57 +0000120#define CM_AVEOCTL 0x1b8
121#define CM_AVEODIV 0x1bc
Eric Anholt41691b82015-10-08 18:37:24 -0700122#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)
Martin Sperl959ca922016-02-29 11:39:21 +0000132# define CM_FRAC BIT(9)
Eric Anholt41691b82015-10-08 18:37:24 -0700133# 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;
Martin Sperl6e1e60d2016-02-29 11:39:22 +0000302 spinlock_t regs_lock; /* spinlock for all clocks */
Eric Anholt41691b82015-10-08 18:37:24 -0700303 const char *osc_name;
304
305 struct clk_onecell_data onecell;
Martin Sperl56eb3a22016-02-29 12:51:41 +0000306 struct clk *clks[];
Eric Anholt41691b82015-10-08 18:37:24 -0700307};
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}
Stephen Warren526d2392012-12-24 21:55:01 -0700318
Martin Sperl96bf9c62016-02-29 14:20:15 +0000319static int bcm2835_debugfs_regset(struct bcm2835_cprman *cprman, u32 base,
320 struct debugfs_reg32 *regs, size_t nregs,
321 struct dentry *dentry)
322{
323 struct dentry *regdump;
324 struct debugfs_regset32 *regset;
325
326 regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL);
327 if (!regset)
328 return -ENOMEM;
329
330 regset->regs = regs;
331 regset->nregs = nregs;
332 regset->base = cprman->regs + base;
333
334 regdump = debugfs_create_regset32("regdump", S_IRUGO, dentry,
335 regset);
336
337 return regdump ? 0 : -ENOMEM;
338}
339
Simon Arlott75fabc32012-09-10 23:26:15 -0600340/*
341 * These are fixed clocks. They're probably not all root clocks and it may
342 * be possible to turn them on and off but until this is mapped out better
343 * it's the only way they can be used.
344 */
345void __init bcm2835_init_clocks(void)
346{
347 struct clk *clk;
348 int ret;
349
Stephen Boydbd41aa672016-03-01 10:59:47 -0800350 clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, 0, 126000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800351 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600352 pr_err("apb_pclk not registered\n");
353
Stephen Boydbd41aa672016-03-01 10:59:47 -0800354 clk = clk_register_fixed_rate(NULL, "uart0_pclk", NULL, 0, 3000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800355 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600356 pr_err("uart0_pclk not registered\n");
357 ret = clk_register_clkdev(clk, NULL, "20201000.uart");
358 if (ret)
359 pr_err("uart0_pclk alias not registered\n");
360
Stephen Boydbd41aa672016-03-01 10:59:47 -0800361 clk = clk_register_fixed_rate(NULL, "uart1_pclk", NULL, 0, 125000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800362 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600363 pr_err("uart1_pclk not registered\n");
364 ret = clk_register_clkdev(clk, NULL, "20215000.uart");
365 if (ret)
Domenico Andreoli686ea582012-10-20 03:35:28 +0200366 pr_err("uart1_pclk alias not registered\n");
Simon Arlott75fabc32012-09-10 23:26:15 -0600367}
Eric Anholt41691b82015-10-08 18:37:24 -0700368
369struct bcm2835_pll_data {
370 const char *name;
371 u32 cm_ctrl_reg;
372 u32 a2w_ctrl_reg;
373 u32 frac_reg;
374 u32 ana_reg_base;
375 u32 reference_enable_mask;
376 /* Bit in CM_LOCK to indicate when the PLL has locked. */
377 u32 lock_mask;
378
379 const struct bcm2835_pll_ana_bits *ana;
380
381 unsigned long min_rate;
382 unsigned long max_rate;
383 /*
384 * Highest rate for the VCO before we have to use the
385 * pre-divide-by-2.
386 */
387 unsigned long max_fb_rate;
388};
389
390struct bcm2835_pll_ana_bits {
391 u32 mask0;
392 u32 set0;
393 u32 mask1;
394 u32 set1;
395 u32 mask3;
396 u32 set3;
397 u32 fb_prediv_mask;
398};
399
400static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
401 .mask0 = 0,
402 .set0 = 0,
Eric Anholt286259e2016-04-13 13:05:02 -0700403 .mask1 = (u32)~(A2W_PLL_KI_MASK | A2W_PLL_KP_MASK),
Eric Anholt41691b82015-10-08 18:37:24 -0700404 .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
Eric Anholt286259e2016-04-13 13:05:02 -0700405 .mask3 = (u32)~A2W_PLL_KA_MASK,
Eric Anholt41691b82015-10-08 18:37:24 -0700406 .set3 = (2 << A2W_PLL_KA_SHIFT),
407 .fb_prediv_mask = BIT(14),
408};
409
410static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
Eric Anholt286259e2016-04-13 13:05:02 -0700411 .mask0 = (u32)~(A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK),
Eric Anholt41691b82015-10-08 18:37:24 -0700412 .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
Eric Anholt286259e2016-04-13 13:05:02 -0700413 .mask1 = (u32)~(A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK),
Eric Anholt41691b82015-10-08 18:37:24 -0700414 .set1 = (6 << A2W_PLLH_KP_SHIFT),
415 .mask3 = 0,
416 .set3 = 0,
417 .fb_prediv_mask = BIT(11),
418};
419
Eric Anholt41691b82015-10-08 18:37:24 -0700420struct bcm2835_pll_divider_data {
421 const char *name;
Martin Sperl3b15afe2016-02-29 12:51:42 +0000422 const char *source_pll;
423
Eric Anholt41691b82015-10-08 18:37:24 -0700424 u32 cm_reg;
425 u32 a2w_reg;
426
427 u32 load_mask;
428 u32 hold_mask;
429 u32 fixed_divider;
430};
431
Eric Anholt41691b82015-10-08 18:37:24 -0700432struct bcm2835_clock_data {
433 const char *name;
434
435 const char *const *parents;
436 int num_mux_parents;
437
438 u32 ctl_reg;
439 u32 div_reg;
440
441 /* Number of integer bits in the divider */
442 u32 int_bits;
443 /* Number of fractional bits in the divider */
444 u32 frac_bits;
445
Eric Anholte69fdcc2016-06-01 12:05:33 -0700446 u32 flags;
447
Eric Anholt41691b82015-10-08 18:37:24 -0700448 bool is_vpu_clock;
Martin Sperl959ca922016-02-29 11:39:21 +0000449 bool is_mash_clock;
Eric Anholt41691b82015-10-08 18:37:24 -0700450};
451
Martin Sperl56eb3a22016-02-29 12:51:41 +0000452struct bcm2835_gate_data {
453 const char *name;
454 const char *parent;
455
456 u32 ctl_reg;
457};
458
Eric Anholt41691b82015-10-08 18:37:24 -0700459struct bcm2835_pll {
460 struct clk_hw hw;
461 struct bcm2835_cprman *cprman;
462 const struct bcm2835_pll_data *data;
463};
464
465static int bcm2835_pll_is_on(struct clk_hw *hw)
466{
467 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
468 struct bcm2835_cprman *cprman = pll->cprman;
469 const struct bcm2835_pll_data *data = pll->data;
470
471 return cprman_read(cprman, data->a2w_ctrl_reg) &
472 A2W_PLL_CTRL_PRST_DISABLE;
473}
474
475static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
476 unsigned long parent_rate,
477 u32 *ndiv, u32 *fdiv)
478{
479 u64 div;
480
481 div = (u64)rate << A2W_PLL_FRAC_BITS;
482 do_div(div, parent_rate);
483
484 *ndiv = div >> A2W_PLL_FRAC_BITS;
485 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
486}
487
488static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
489 u32 ndiv, u32 fdiv, u32 pdiv)
490{
491 u64 rate;
492
493 if (pdiv == 0)
494 return 0;
495
496 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
497 do_div(rate, pdiv);
498 return rate >> A2W_PLL_FRAC_BITS;
499}
500
501static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
502 unsigned long *parent_rate)
503{
504 u32 ndiv, fdiv;
505
506 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
507
508 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
509}
510
511static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
512 unsigned long parent_rate)
513{
514 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
515 struct bcm2835_cprman *cprman = pll->cprman;
516 const struct bcm2835_pll_data *data = pll->data;
517 u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
518 u32 ndiv, pdiv, fdiv;
519 bool using_prediv;
520
521 if (parent_rate == 0)
522 return 0;
523
524 fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
525 ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
526 pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
527 using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
528 data->ana->fb_prediv_mask;
529
530 if (using_prediv)
531 ndiv *= 2;
532
533 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
534}
535
536static void bcm2835_pll_off(struct clk_hw *hw)
537{
538 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
539 struct bcm2835_cprman *cprman = pll->cprman;
540 const struct bcm2835_pll_data *data = pll->data;
541
Martin Sperl6727f082016-02-29 11:39:17 +0000542 spin_lock(&cprman->regs_lock);
543 cprman_write(cprman, data->cm_ctrl_reg,
544 cprman_read(cprman, data->cm_ctrl_reg) |
545 CM_PLL_ANARST);
546 cprman_write(cprman, data->a2w_ctrl_reg,
547 cprman_read(cprman, data->a2w_ctrl_reg) |
548 A2W_PLL_CTRL_PWRDN);
549 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700550}
551
552static int bcm2835_pll_on(struct clk_hw *hw)
553{
554 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
555 struct bcm2835_cprman *cprman = pll->cprman;
556 const struct bcm2835_pll_data *data = pll->data;
557 ktime_t timeout;
558
Eric Anholte708b382016-04-13 13:05:03 -0700559 cprman_write(cprman, data->a2w_ctrl_reg,
560 cprman_read(cprman, data->a2w_ctrl_reg) &
561 ~A2W_PLL_CTRL_PWRDN);
562
Eric Anholt41691b82015-10-08 18:37:24 -0700563 /* Take the PLL out of reset. */
564 cprman_write(cprman, data->cm_ctrl_reg,
565 cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
566
567 /* Wait for the PLL to lock. */
568 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
569 while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
570 if (ktime_after(ktime_get(), timeout)) {
571 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
572 clk_hw_get_name(hw));
573 return -ETIMEDOUT;
574 }
575
576 cpu_relax();
577 }
578
579 return 0;
580}
581
582static void
583bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
584{
585 int i;
586
587 /*
588 * ANA register setup is done as a series of writes to
589 * ANA3-ANA0, in that order. This lets us write all 4
590 * registers as a single cycle of the serdes interface (taking
591 * 100 xosc clocks), whereas if we were to update ana0, 1, and
592 * 3 individually through their partial-write registers, each
593 * would be their own serdes cycle.
594 */
595 for (i = 3; i >= 0; i--)
596 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
597}
598
599static int bcm2835_pll_set_rate(struct clk_hw *hw,
600 unsigned long rate, unsigned long parent_rate)
601{
602 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
603 struct bcm2835_cprman *cprman = pll->cprman;
604 const struct bcm2835_pll_data *data = pll->data;
605 bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
606 u32 ndiv, fdiv, a2w_ctl;
607 u32 ana[4];
608 int i;
609
610 if (rate < data->min_rate || rate > data->max_rate) {
611 dev_err(cprman->dev, "%s: rate out of spec: %lu vs (%lu, %lu)\n",
612 clk_hw_get_name(hw), rate,
613 data->min_rate, data->max_rate);
614 return -EINVAL;
615 }
616
617 if (rate > data->max_fb_rate) {
618 use_fb_prediv = true;
619 rate /= 2;
620 } else {
621 use_fb_prediv = false;
622 }
623
624 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
625
626 for (i = 3; i >= 0; i--)
627 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
628
629 was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
630
631 ana[0] &= ~data->ana->mask0;
632 ana[0] |= data->ana->set0;
633 ana[1] &= ~data->ana->mask1;
634 ana[1] |= data->ana->set1;
635 ana[3] &= ~data->ana->mask3;
636 ana[3] |= data->ana->set3;
637
638 if (was_using_prediv && !use_fb_prediv) {
639 ana[1] &= ~data->ana->fb_prediv_mask;
640 do_ana_setup_first = true;
641 } else if (!was_using_prediv && use_fb_prediv) {
642 ana[1] |= data->ana->fb_prediv_mask;
643 do_ana_setup_first = false;
644 } else {
645 do_ana_setup_first = true;
646 }
647
648 /* Unmask the reference clock from the oscillator. */
649 cprman_write(cprman, A2W_XOSC_CTRL,
650 cprman_read(cprman, A2W_XOSC_CTRL) |
651 data->reference_enable_mask);
652
653 if (do_ana_setup_first)
654 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
655
656 /* Set the PLL multiplier from the oscillator. */
657 cprman_write(cprman, data->frac_reg, fdiv);
658
659 a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
660 a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
661 a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
662 a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
663 a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
664 cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
665
666 if (!do_ana_setup_first)
667 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
668
669 return 0;
670}
671
Martin Sperl96bf9c62016-02-29 14:20:15 +0000672static int bcm2835_pll_debug_init(struct clk_hw *hw,
673 struct dentry *dentry)
674{
675 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
676 struct bcm2835_cprman *cprman = pll->cprman;
677 const struct bcm2835_pll_data *data = pll->data;
678 struct debugfs_reg32 *regs;
679
680 regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
681 if (!regs)
682 return -ENOMEM;
683
684 regs[0].name = "cm_ctrl";
685 regs[0].offset = data->cm_ctrl_reg;
686 regs[1].name = "a2w_ctrl";
687 regs[1].offset = data->a2w_ctrl_reg;
688 regs[2].name = "frac";
689 regs[2].offset = data->frac_reg;
690 regs[3].name = "ana0";
691 regs[3].offset = data->ana_reg_base + 0 * 4;
692 regs[4].name = "ana1";
693 regs[4].offset = data->ana_reg_base + 1 * 4;
694 regs[5].name = "ana2";
695 regs[5].offset = data->ana_reg_base + 2 * 4;
696 regs[6].name = "ana3";
697 regs[6].offset = data->ana_reg_base + 3 * 4;
698
699 return bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry);
700}
701
Eric Anholt41691b82015-10-08 18:37:24 -0700702static const struct clk_ops bcm2835_pll_clk_ops = {
703 .is_prepared = bcm2835_pll_is_on,
704 .prepare = bcm2835_pll_on,
705 .unprepare = bcm2835_pll_off,
706 .recalc_rate = bcm2835_pll_get_rate,
707 .set_rate = bcm2835_pll_set_rate,
708 .round_rate = bcm2835_pll_round_rate,
Martin Sperl96bf9c62016-02-29 14:20:15 +0000709 .debug_init = bcm2835_pll_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -0700710};
711
712struct bcm2835_pll_divider {
713 struct clk_divider div;
714 struct bcm2835_cprman *cprman;
715 const struct bcm2835_pll_divider_data *data;
716};
717
718static struct bcm2835_pll_divider *
719bcm2835_pll_divider_from_hw(struct clk_hw *hw)
720{
721 return container_of(hw, struct bcm2835_pll_divider, div.hw);
722}
723
724static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
725{
726 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
727 struct bcm2835_cprman *cprman = divider->cprman;
728 const struct bcm2835_pll_divider_data *data = divider->data;
729
730 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
731}
732
733static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
734 unsigned long rate,
735 unsigned long *parent_rate)
736{
737 return clk_divider_ops.round_rate(hw, rate, parent_rate);
738}
739
740static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
741 unsigned long parent_rate)
742{
Eric Anholt79c1e2f2016-02-15 19:03:58 -0800743 return clk_divider_ops.recalc_rate(hw, parent_rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700744}
745
746static void bcm2835_pll_divider_off(struct clk_hw *hw)
747{
748 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
749 struct bcm2835_cprman *cprman = divider->cprman;
750 const struct bcm2835_pll_divider_data *data = divider->data;
751
Martin Sperlec36a5c2016-02-29 11:39:18 +0000752 spin_lock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700753 cprman_write(cprman, data->cm_reg,
754 (cprman_read(cprman, data->cm_reg) &
755 ~data->load_mask) | data->hold_mask);
756 cprman_write(cprman, data->a2w_reg, A2W_PLL_CHANNEL_DISABLE);
Martin Sperlec36a5c2016-02-29 11:39:18 +0000757 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700758}
759
760static int bcm2835_pll_divider_on(struct clk_hw *hw)
761{
762 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
763 struct bcm2835_cprman *cprman = divider->cprman;
764 const struct bcm2835_pll_divider_data *data = divider->data;
765
Martin Sperlec36a5c2016-02-29 11:39:18 +0000766 spin_lock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700767 cprman_write(cprman, data->a2w_reg,
768 cprman_read(cprman, data->a2w_reg) &
769 ~A2W_PLL_CHANNEL_DISABLE);
770
771 cprman_write(cprman, data->cm_reg,
772 cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
Martin Sperlec36a5c2016-02-29 11:39:18 +0000773 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700774
775 return 0;
776}
777
778static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
779 unsigned long rate,
780 unsigned long parent_rate)
781{
782 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
783 struct bcm2835_cprman *cprman = divider->cprman;
784 const struct bcm2835_pll_divider_data *data = divider->data;
Eric Anholt773b3962016-02-15 19:03:57 -0800785 u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
Eric Anholt41691b82015-10-08 18:37:24 -0700786
Eric Anholt773b3962016-02-15 19:03:57 -0800787 div = DIV_ROUND_UP_ULL(parent_rate, rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700788
Eric Anholt773b3962016-02-15 19:03:57 -0800789 div = min(div, max_div);
790 if (div == max_div)
791 div = 0;
792
793 cprman_write(cprman, data->a2w_reg, div);
Eric Anholt41691b82015-10-08 18:37:24 -0700794 cm = cprman_read(cprman, data->cm_reg);
795 cprman_write(cprman, data->cm_reg, cm | data->load_mask);
796 cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
797
798 return 0;
799}
800
Martin Sperl96bf9c62016-02-29 14:20:15 +0000801static int bcm2835_pll_divider_debug_init(struct clk_hw *hw,
802 struct dentry *dentry)
803{
804 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
805 struct bcm2835_cprman *cprman = divider->cprman;
806 const struct bcm2835_pll_divider_data *data = divider->data;
807 struct debugfs_reg32 *regs;
808
809 regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
810 if (!regs)
811 return -ENOMEM;
812
813 regs[0].name = "cm";
814 regs[0].offset = data->cm_reg;
815 regs[1].name = "a2w";
816 regs[1].offset = data->a2w_reg;
817
818 return bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry);
819}
820
Eric Anholt41691b82015-10-08 18:37:24 -0700821static const struct clk_ops bcm2835_pll_divider_clk_ops = {
822 .is_prepared = bcm2835_pll_divider_is_on,
823 .prepare = bcm2835_pll_divider_on,
824 .unprepare = bcm2835_pll_divider_off,
825 .recalc_rate = bcm2835_pll_divider_get_rate,
826 .set_rate = bcm2835_pll_divider_set_rate,
827 .round_rate = bcm2835_pll_divider_round_rate,
Martin Sperl96bf9c62016-02-29 14:20:15 +0000828 .debug_init = bcm2835_pll_divider_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -0700829};
830
831/*
832 * The CM dividers do fixed-point division, so we can't use the
833 * generic integer divider code like the PLL dividers do (and we can't
834 * fake it by having some fixed shifts preceding it in the clock tree,
835 * because we'd run out of bits in a 32-bit unsigned long).
836 */
837struct bcm2835_clock {
838 struct clk_hw hw;
839 struct bcm2835_cprman *cprman;
840 const struct bcm2835_clock_data *data;
841};
842
843static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
844{
845 return container_of(hw, struct bcm2835_clock, hw);
846}
847
848static int bcm2835_clock_is_on(struct clk_hw *hw)
849{
850 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
851 struct bcm2835_cprman *cprman = clock->cprman;
852 const struct bcm2835_clock_data *data = clock->data;
853
854 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
855}
856
857static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
858 unsigned long rate,
Remi Pommarel9c95b322015-12-06 17:22:46 +0100859 unsigned long parent_rate,
860 bool round_up)
Eric Anholt41691b82015-10-08 18:37:24 -0700861{
862 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
863 const struct bcm2835_clock_data *data = clock->data;
Remi Pommarel9c95b322015-12-06 17:22:46 +0100864 u32 unused_frac_mask =
865 GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
Eric Anholt41691b82015-10-08 18:37:24 -0700866 u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
Remi Pommarel9c95b322015-12-06 17:22:46 +0100867 u64 rem;
Martin Sperl959ca922016-02-29 11:39:21 +0000868 u32 div, mindiv, maxdiv;
Eric Anholt41691b82015-10-08 18:37:24 -0700869
Remi Pommarel9c95b322015-12-06 17:22:46 +0100870 rem = do_div(temp, rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700871 div = temp;
872
Remi Pommarel9c95b322015-12-06 17:22:46 +0100873 /* Round up and mask off the unused bits */
874 if (round_up && ((div & unused_frac_mask) != 0 || rem != 0))
875 div += unused_frac_mask + 1;
876 div &= ~unused_frac_mask;
Eric Anholt41691b82015-10-08 18:37:24 -0700877
Martin Sperl959ca922016-02-29 11:39:21 +0000878 /* different clamping limits apply for a mash clock */
879 if (data->is_mash_clock) {
880 /* clamp to min divider of 2 */
881 mindiv = 2 << CM_DIV_FRAC_BITS;
882 /* clamp to the highest possible integer divider */
883 maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
884 } else {
885 /* clamp to min divider of 1 */
886 mindiv = 1 << CM_DIV_FRAC_BITS;
887 /* clamp to the highest possible fractional divider */
888 maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
889 CM_DIV_FRAC_BITS - data->frac_bits);
890 }
891
892 /* apply the clamping limits */
893 div = max_t(u32, div, mindiv);
894 div = min_t(u32, div, maxdiv);
Eric Anholt41691b82015-10-08 18:37:24 -0700895
896 return div;
897}
898
899static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
900 unsigned long parent_rate,
901 u32 div)
902{
903 const struct bcm2835_clock_data *data = clock->data;
904 u64 temp;
905
906 /*
907 * The divisor is a 12.12 fixed point field, but only some of
908 * the bits are populated in any given clock.
909 */
910 div >>= CM_DIV_FRAC_BITS - data->frac_bits;
911 div &= (1 << (data->int_bits + data->frac_bits)) - 1;
912
913 if (div == 0)
914 return 0;
915
916 temp = (u64)parent_rate << data->frac_bits;
917
918 do_div(temp, div);
919
920 return temp;
921}
922
Eric Anholt41691b82015-10-08 18:37:24 -0700923static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
924 unsigned long parent_rate)
925{
926 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
927 struct bcm2835_cprman *cprman = clock->cprman;
928 const struct bcm2835_clock_data *data = clock->data;
929 u32 div = cprman_read(cprman, data->div_reg);
930
931 return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
932}
933
934static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
935{
936 struct bcm2835_cprman *cprman = clock->cprman;
937 const struct bcm2835_clock_data *data = clock->data;
938 ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
939
940 while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
941 if (ktime_after(ktime_get(), timeout)) {
942 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
943 clk_hw_get_name(&clock->hw));
944 return;
945 }
946 cpu_relax();
947 }
948}
949
950static void bcm2835_clock_off(struct clk_hw *hw)
951{
952 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
953 struct bcm2835_cprman *cprman = clock->cprman;
954 const struct bcm2835_clock_data *data = clock->data;
955
956 spin_lock(&cprman->regs_lock);
957 cprman_write(cprman, data->ctl_reg,
958 cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
959 spin_unlock(&cprman->regs_lock);
960
961 /* BUSY will remain high until the divider completes its cycle. */
962 bcm2835_clock_wait_busy(clock);
963}
964
965static int bcm2835_clock_on(struct clk_hw *hw)
966{
967 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
968 struct bcm2835_cprman *cprman = clock->cprman;
969 const struct bcm2835_clock_data *data = clock->data;
970
971 spin_lock(&cprman->regs_lock);
972 cprman_write(cprman, data->ctl_reg,
973 cprman_read(cprman, data->ctl_reg) |
974 CM_ENABLE |
975 CM_GATE);
976 spin_unlock(&cprman->regs_lock);
977
978 return 0;
979}
980
981static int bcm2835_clock_set_rate(struct clk_hw *hw,
982 unsigned long rate, unsigned long parent_rate)
983{
984 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
985 struct bcm2835_cprman *cprman = clock->cprman;
986 const struct bcm2835_clock_data *data = clock->data;
Remi Pommarel9c95b322015-12-06 17:22:46 +0100987 u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false);
Martin Sperl959ca922016-02-29 11:39:21 +0000988 u32 ctl;
989
990 spin_lock(&cprman->regs_lock);
991
992 /*
993 * Setting up frac support
994 *
995 * In principle it is recommended to stop/start the clock first,
996 * but as we set CLK_SET_RATE_GATE during registration of the
997 * clock this requirement should be take care of by the
998 * clk-framework.
999 */
1000 ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
1001 ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
1002 cprman_write(cprman, data->ctl_reg, ctl);
Eric Anholt41691b82015-10-08 18:37:24 -07001003
1004 cprman_write(cprman, data->div_reg, div);
1005
Martin Sperl959ca922016-02-29 11:39:21 +00001006 spin_unlock(&cprman->regs_lock);
1007
Eric Anholt41691b82015-10-08 18:37:24 -07001008 return 0;
1009}
1010
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001011static int bcm2835_clock_determine_rate(struct clk_hw *hw,
Martin Sperl6e1e60d2016-02-29 11:39:22 +00001012 struct clk_rate_request *req)
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001013{
1014 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1015 struct clk_hw *parent, *best_parent = NULL;
1016 unsigned long rate, best_rate = 0;
1017 unsigned long prate, best_prate = 0;
1018 size_t i;
1019 u32 div;
1020
1021 /*
1022 * Select parent clock that results in the closest but lower rate
1023 */
1024 for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
1025 parent = clk_hw_get_parent_by_index(hw, i);
1026 if (!parent)
1027 continue;
1028 prate = clk_hw_get_rate(parent);
1029 div = bcm2835_clock_choose_div(hw, req->rate, prate, true);
1030 rate = bcm2835_clock_rate_from_divisor(clock, prate, div);
1031 if (rate > best_rate && rate <= req->rate) {
1032 best_parent = parent;
1033 best_prate = prate;
1034 best_rate = rate;
1035 }
1036 }
1037
1038 if (!best_parent)
1039 return -EINVAL;
1040
1041 req->best_parent_hw = best_parent;
1042 req->best_parent_rate = best_prate;
1043
1044 req->rate = best_rate;
1045
1046 return 0;
1047}
1048
1049static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
1050{
1051 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1052 struct bcm2835_cprman *cprman = clock->cprman;
1053 const struct bcm2835_clock_data *data = clock->data;
1054 u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
1055
1056 cprman_write(cprman, data->ctl_reg, src);
1057 return 0;
1058}
1059
1060static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
1061{
1062 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1063 struct bcm2835_cprman *cprman = clock->cprman;
1064 const struct bcm2835_clock_data *data = clock->data;
1065 u32 src = cprman_read(cprman, data->ctl_reg);
1066
1067 return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
1068}
1069
Martin Sperl96bf9c62016-02-29 14:20:15 +00001070static struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = {
1071 {
1072 .name = "ctl",
1073 .offset = 0,
1074 },
1075 {
1076 .name = "div",
1077 .offset = 4,
1078 },
1079};
1080
1081static int bcm2835_clock_debug_init(struct clk_hw *hw,
1082 struct dentry *dentry)
1083{
1084 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1085 struct bcm2835_cprman *cprman = clock->cprman;
1086 const struct bcm2835_clock_data *data = clock->data;
1087
1088 return bcm2835_debugfs_regset(
1089 cprman, data->ctl_reg,
1090 bcm2835_debugfs_clock_reg32,
1091 ARRAY_SIZE(bcm2835_debugfs_clock_reg32),
1092 dentry);
1093}
1094
Eric Anholt41691b82015-10-08 18:37:24 -07001095static const struct clk_ops bcm2835_clock_clk_ops = {
1096 .is_prepared = bcm2835_clock_is_on,
1097 .prepare = bcm2835_clock_on,
1098 .unprepare = bcm2835_clock_off,
1099 .recalc_rate = bcm2835_clock_get_rate,
1100 .set_rate = bcm2835_clock_set_rate,
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001101 .determine_rate = bcm2835_clock_determine_rate,
1102 .set_parent = bcm2835_clock_set_parent,
1103 .get_parent = bcm2835_clock_get_parent,
Martin Sperl96bf9c62016-02-29 14:20:15 +00001104 .debug_init = bcm2835_clock_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -07001105};
1106
1107static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1108{
1109 return true;
1110}
1111
1112/*
1113 * The VPU clock can never be disabled (it doesn't have an ENABLE
1114 * bit), so it gets its own set of clock ops.
1115 */
1116static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1117 .is_prepared = bcm2835_vpu_clock_is_on,
1118 .recalc_rate = bcm2835_clock_get_rate,
1119 .set_rate = bcm2835_clock_set_rate,
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001120 .determine_rate = bcm2835_clock_determine_rate,
1121 .set_parent = bcm2835_clock_set_parent,
1122 .get_parent = bcm2835_clock_get_parent,
Martin Sperl96bf9c62016-02-29 14:20:15 +00001123 .debug_init = bcm2835_clock_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -07001124};
1125
1126static struct clk *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1127 const struct bcm2835_pll_data *data)
1128{
1129 struct bcm2835_pll *pll;
1130 struct clk_init_data init;
1131
1132 memset(&init, 0, sizeof(init));
1133
1134 /* All of the PLLs derive from the external oscillator. */
1135 init.parent_names = &cprman->osc_name;
1136 init.num_parents = 1;
1137 init.name = data->name;
1138 init.ops = &bcm2835_pll_clk_ops;
1139 init.flags = CLK_IGNORE_UNUSED;
1140
1141 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1142 if (!pll)
1143 return NULL;
1144
1145 pll->cprman = cprman;
1146 pll->data = data;
1147 pll->hw.init = &init;
1148
1149 return devm_clk_register(cprman->dev, &pll->hw);
1150}
1151
1152static struct clk *
1153bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1154 const struct bcm2835_pll_divider_data *data)
1155{
1156 struct bcm2835_pll_divider *divider;
1157 struct clk_init_data init;
1158 struct clk *clk;
1159 const char *divider_name;
1160
1161 if (data->fixed_divider != 1) {
1162 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1163 "%s_prediv", data->name);
1164 if (!divider_name)
1165 return NULL;
1166 } else {
1167 divider_name = data->name;
1168 }
1169
1170 memset(&init, 0, sizeof(init));
1171
Martin Sperl3b15afe2016-02-29 12:51:42 +00001172 init.parent_names = &data->source_pll;
Eric Anholt41691b82015-10-08 18:37:24 -07001173 init.num_parents = 1;
1174 init.name = divider_name;
1175 init.ops = &bcm2835_pll_divider_clk_ops;
1176 init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
1177
1178 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1179 if (!divider)
1180 return NULL;
1181
1182 divider->div.reg = cprman->regs + data->a2w_reg;
1183 divider->div.shift = A2W_PLL_DIV_SHIFT;
1184 divider->div.width = A2W_PLL_DIV_BITS;
Eric Anholt79c1e2f2016-02-15 19:03:58 -08001185 divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
Eric Anholt41691b82015-10-08 18:37:24 -07001186 divider->div.lock = &cprman->regs_lock;
1187 divider->div.hw.init = &init;
1188 divider->div.table = NULL;
1189
1190 divider->cprman = cprman;
1191 divider->data = data;
1192
1193 clk = devm_clk_register(cprman->dev, &divider->div.hw);
1194 if (IS_ERR(clk))
1195 return clk;
1196
1197 /*
1198 * PLLH's channels have a fixed divide by 10 afterwards, which
1199 * is what our consumers are actually using.
1200 */
1201 if (data->fixed_divider != 1) {
1202 return clk_register_fixed_factor(cprman->dev, data->name,
1203 divider_name,
1204 CLK_SET_RATE_PARENT,
1205 1,
1206 data->fixed_divider);
1207 }
1208
1209 return clk;
1210}
1211
1212static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman,
1213 const struct bcm2835_clock_data *data)
1214{
1215 struct bcm2835_clock *clock;
1216 struct clk_init_data init;
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001217 const char *parents[1 << CM_SRC_BITS];
1218 size_t i;
Eric Anholt41691b82015-10-08 18:37:24 -07001219
1220 /*
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001221 * Replace our "xosc" references with the oscillator's
1222 * actual name.
Eric Anholt41691b82015-10-08 18:37:24 -07001223 */
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001224 for (i = 0; i < data->num_mux_parents; i++) {
1225 if (strcmp(data->parents[i], "xosc") == 0)
1226 parents[i] = cprman->osc_name;
1227 else
1228 parents[i] = data->parents[i];
Eric Anholt41691b82015-10-08 18:37:24 -07001229 }
1230
1231 memset(&init, 0, sizeof(init));
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001232 init.parent_names = parents;
1233 init.num_parents = data->num_mux_parents;
Eric Anholt41691b82015-10-08 18:37:24 -07001234 init.name = data->name;
Eric Anholte69fdcc2016-06-01 12:05:33 -07001235 init.flags = data->flags | CLK_IGNORE_UNUSED;
Eric Anholt41691b82015-10-08 18:37:24 -07001236
1237 if (data->is_vpu_clock) {
1238 init.ops = &bcm2835_vpu_clock_clk_ops;
1239 } else {
1240 init.ops = &bcm2835_clock_clk_ops;
1241 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
1242 }
1243
1244 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1245 if (!clock)
1246 return NULL;
1247
1248 clock->cprman = cprman;
1249 clock->data = data;
1250 clock->hw.init = &init;
1251
1252 return devm_clk_register(cprman->dev, &clock->hw);
1253}
1254
Martin Sperl56eb3a22016-02-29 12:51:41 +00001255static struct clk *bcm2835_register_gate(struct bcm2835_cprman *cprman,
1256 const struct bcm2835_gate_data *data)
1257{
1258 return clk_register_gate(cprman->dev, data->name, data->parent,
1259 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1260 cprman->regs + data->ctl_reg,
1261 CM_GATE_BIT, 0, &cprman->regs_lock);
1262}
1263
1264typedef struct clk *(*bcm2835_clk_register)(struct bcm2835_cprman *cprman,
1265 const void *data);
1266struct bcm2835_clk_desc {
1267 bcm2835_clk_register clk_register;
1268 const void *data;
1269};
1270
Martin Sperl3b15afe2016-02-29 12:51:42 +00001271/* assignment helper macros for different clock types */
1272#define _REGISTER(f, ...) { .clk_register = (bcm2835_clk_register)f, \
1273 .data = __VA_ARGS__ }
1274#define REGISTER_PLL(...) _REGISTER(&bcm2835_register_pll, \
1275 &(struct bcm2835_pll_data) \
1276 {__VA_ARGS__})
1277#define REGISTER_PLL_DIV(...) _REGISTER(&bcm2835_register_pll_divider, \
1278 &(struct bcm2835_pll_divider_data) \
1279 {__VA_ARGS__})
1280#define REGISTER_CLK(...) _REGISTER(&bcm2835_register_clock, \
1281 &(struct bcm2835_clock_data) \
1282 {__VA_ARGS__})
1283#define REGISTER_GATE(...) _REGISTER(&bcm2835_register_gate, \
1284 &(struct bcm2835_gate_data) \
1285 {__VA_ARGS__})
Martin Sperl56eb3a22016-02-29 12:51:41 +00001286
Martin Sperl3b15afe2016-02-29 12:51:42 +00001287/* parent mux arrays plus helper macros */
1288
1289/* main oscillator parent mux */
1290static const char *const bcm2835_clock_osc_parents[] = {
1291 "gnd",
1292 "xosc",
1293 "testdebug0",
1294 "testdebug1"
1295};
1296
1297#define REGISTER_OSC_CLK(...) REGISTER_CLK( \
1298 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), \
1299 .parents = bcm2835_clock_osc_parents, \
1300 __VA_ARGS__)
1301
1302/* main peripherial parent mux */
1303static const char *const bcm2835_clock_per_parents[] = {
1304 "gnd",
1305 "xosc",
1306 "testdebug0",
1307 "testdebug1",
1308 "plla_per",
1309 "pllc_per",
1310 "plld_per",
1311 "pllh_aux",
1312};
1313
1314#define REGISTER_PER_CLK(...) REGISTER_CLK( \
1315 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), \
1316 .parents = bcm2835_clock_per_parents, \
1317 __VA_ARGS__)
1318
1319/* main vpu parent mux */
1320static const char *const bcm2835_clock_vpu_parents[] = {
1321 "gnd",
1322 "xosc",
1323 "testdebug0",
1324 "testdebug1",
1325 "plla_core",
1326 "pllc_core0",
1327 "plld_core",
1328 "pllh_aux",
1329 "pllc_core1",
1330 "pllc_core2",
1331};
1332
1333#define REGISTER_VPU_CLK(...) REGISTER_CLK( \
1334 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), \
1335 .parents = bcm2835_clock_vpu_parents, \
1336 __VA_ARGS__)
1337
1338/*
1339 * the real definition of all the pll, pll_dividers and clocks
1340 * these make use of the above REGISTER_* macros
1341 */
Martin Sperl56eb3a22016-02-29 12:51:41 +00001342static const struct bcm2835_clk_desc clk_desc_array[] = {
Martin Sperl3b15afe2016-02-29 12:51:42 +00001343 /* the PLL + PLL dividers */
1344
1345 /*
1346 * PLLA is the auxiliary PLL, used to drive the CCP2
1347 * (Compact Camera Port 2) transmitter clock.
1348 *
1349 * It is in the PX LDO power domain, which is on when the
1350 * AUDIO domain is on.
1351 */
1352 [BCM2835_PLLA] = REGISTER_PLL(
1353 .name = "plla",
1354 .cm_ctrl_reg = CM_PLLA,
1355 .a2w_ctrl_reg = A2W_PLLA_CTRL,
1356 .frac_reg = A2W_PLLA_FRAC,
1357 .ana_reg_base = A2W_PLLA_ANA0,
1358 .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
1359 .lock_mask = CM_LOCK_FLOCKA,
1360
1361 .ana = &bcm2835_ana_default,
1362
1363 .min_rate = 600000000u,
1364 .max_rate = 2400000000u,
1365 .max_fb_rate = BCM2835_MAX_FB_RATE),
1366 [BCM2835_PLLA_CORE] = REGISTER_PLL_DIV(
1367 .name = "plla_core",
1368 .source_pll = "plla",
1369 .cm_reg = CM_PLLA,
1370 .a2w_reg = A2W_PLLA_CORE,
1371 .load_mask = CM_PLLA_LOADCORE,
1372 .hold_mask = CM_PLLA_HOLDCORE,
1373 .fixed_divider = 1),
1374 [BCM2835_PLLA_PER] = REGISTER_PLL_DIV(
1375 .name = "plla_per",
1376 .source_pll = "plla",
1377 .cm_reg = CM_PLLA,
1378 .a2w_reg = A2W_PLLA_PER,
1379 .load_mask = CM_PLLA_LOADPER,
1380 .hold_mask = CM_PLLA_HOLDPER,
1381 .fixed_divider = 1),
Martin Sperl72843692016-02-29 15:43:56 +00001382 [BCM2835_PLLA_DSI0] = REGISTER_PLL_DIV(
1383 .name = "plla_dsi0",
1384 .source_pll = "plla",
1385 .cm_reg = CM_PLLA,
1386 .a2w_reg = A2W_PLLA_DSI0,
1387 .load_mask = CM_PLLA_LOADDSI0,
1388 .hold_mask = CM_PLLA_HOLDDSI0,
1389 .fixed_divider = 1),
1390 [BCM2835_PLLA_CCP2] = REGISTER_PLL_DIV(
1391 .name = "plla_ccp2",
1392 .source_pll = "plla",
1393 .cm_reg = CM_PLLA,
1394 .a2w_reg = A2W_PLLA_CCP2,
1395 .load_mask = CM_PLLA_LOADCCP2,
1396 .hold_mask = CM_PLLA_HOLDCCP2,
1397 .fixed_divider = 1),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001398
1399 /* PLLB is used for the ARM's clock. */
1400 [BCM2835_PLLB] = REGISTER_PLL(
1401 .name = "pllb",
1402 .cm_ctrl_reg = CM_PLLB,
1403 .a2w_ctrl_reg = A2W_PLLB_CTRL,
1404 .frac_reg = A2W_PLLB_FRAC,
1405 .ana_reg_base = A2W_PLLB_ANA0,
1406 .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
1407 .lock_mask = CM_LOCK_FLOCKB,
1408
1409 .ana = &bcm2835_ana_default,
1410
1411 .min_rate = 600000000u,
1412 .max_rate = 3000000000u,
1413 .max_fb_rate = BCM2835_MAX_FB_RATE),
1414 [BCM2835_PLLB_ARM] = REGISTER_PLL_DIV(
1415 .name = "pllb_arm",
1416 .source_pll = "pllb",
1417 .cm_reg = CM_PLLB,
1418 .a2w_reg = A2W_PLLB_ARM,
1419 .load_mask = CM_PLLB_LOADARM,
1420 .hold_mask = CM_PLLB_HOLDARM,
1421 .fixed_divider = 1),
1422
1423 /*
1424 * PLLC is the core PLL, used to drive the core VPU clock.
1425 *
1426 * It is in the PX LDO power domain, which is on when the
1427 * AUDIO domain is on.
1428 */
1429 [BCM2835_PLLC] = REGISTER_PLL(
1430 .name = "pllc",
1431 .cm_ctrl_reg = CM_PLLC,
1432 .a2w_ctrl_reg = A2W_PLLC_CTRL,
1433 .frac_reg = A2W_PLLC_FRAC,
1434 .ana_reg_base = A2W_PLLC_ANA0,
1435 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1436 .lock_mask = CM_LOCK_FLOCKC,
1437
1438 .ana = &bcm2835_ana_default,
1439
1440 .min_rate = 600000000u,
1441 .max_rate = 3000000000u,
1442 .max_fb_rate = BCM2835_MAX_FB_RATE),
1443 [BCM2835_PLLC_CORE0] = REGISTER_PLL_DIV(
1444 .name = "pllc_core0",
1445 .source_pll = "pllc",
1446 .cm_reg = CM_PLLC,
1447 .a2w_reg = A2W_PLLC_CORE0,
1448 .load_mask = CM_PLLC_LOADCORE0,
1449 .hold_mask = CM_PLLC_HOLDCORE0,
1450 .fixed_divider = 1),
1451 [BCM2835_PLLC_CORE1] = REGISTER_PLL_DIV(
1452 .name = "pllc_core1",
1453 .source_pll = "pllc",
1454 .cm_reg = CM_PLLC,
1455 .a2w_reg = A2W_PLLC_CORE1,
1456 .load_mask = CM_PLLC_LOADCORE1,
1457 .hold_mask = CM_PLLC_HOLDCORE1,
1458 .fixed_divider = 1),
1459 [BCM2835_PLLC_CORE2] = REGISTER_PLL_DIV(
1460 .name = "pllc_core2",
1461 .source_pll = "pllc",
1462 .cm_reg = CM_PLLC,
1463 .a2w_reg = A2W_PLLC_CORE2,
1464 .load_mask = CM_PLLC_LOADCORE2,
1465 .hold_mask = CM_PLLC_HOLDCORE2,
1466 .fixed_divider = 1),
1467 [BCM2835_PLLC_PER] = REGISTER_PLL_DIV(
1468 .name = "pllc_per",
1469 .source_pll = "pllc",
1470 .cm_reg = CM_PLLC,
1471 .a2w_reg = A2W_PLLC_PER,
1472 .load_mask = CM_PLLC_LOADPER,
1473 .hold_mask = CM_PLLC_HOLDPER,
1474 .fixed_divider = 1),
1475
1476 /*
1477 * PLLD is the display PLL, used to drive DSI display panels.
1478 *
1479 * It is in the PX LDO power domain, which is on when the
1480 * AUDIO domain is on.
1481 */
1482 [BCM2835_PLLD] = REGISTER_PLL(
1483 .name = "plld",
1484 .cm_ctrl_reg = CM_PLLD,
1485 .a2w_ctrl_reg = A2W_PLLD_CTRL,
1486 .frac_reg = A2W_PLLD_FRAC,
1487 .ana_reg_base = A2W_PLLD_ANA0,
1488 .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
1489 .lock_mask = CM_LOCK_FLOCKD,
1490
1491 .ana = &bcm2835_ana_default,
1492
1493 .min_rate = 600000000u,
1494 .max_rate = 2400000000u,
1495 .max_fb_rate = BCM2835_MAX_FB_RATE),
1496 [BCM2835_PLLD_CORE] = REGISTER_PLL_DIV(
1497 .name = "plld_core",
1498 .source_pll = "plld",
1499 .cm_reg = CM_PLLD,
1500 .a2w_reg = A2W_PLLD_CORE,
1501 .load_mask = CM_PLLD_LOADCORE,
1502 .hold_mask = CM_PLLD_HOLDCORE,
1503 .fixed_divider = 1),
1504 [BCM2835_PLLD_PER] = REGISTER_PLL_DIV(
1505 .name = "plld_per",
1506 .source_pll = "plld",
1507 .cm_reg = CM_PLLD,
1508 .a2w_reg = A2W_PLLD_PER,
1509 .load_mask = CM_PLLD_LOADPER,
1510 .hold_mask = CM_PLLD_HOLDPER,
1511 .fixed_divider = 1),
Martin Sperl72843692016-02-29 15:43:56 +00001512 [BCM2835_PLLD_DSI0] = REGISTER_PLL_DIV(
1513 .name = "plld_dsi0",
1514 .source_pll = "plld",
1515 .cm_reg = CM_PLLD,
1516 .a2w_reg = A2W_PLLD_DSI0,
1517 .load_mask = CM_PLLD_LOADDSI0,
1518 .hold_mask = CM_PLLD_HOLDDSI0,
1519 .fixed_divider = 1),
1520 [BCM2835_PLLD_DSI1] = REGISTER_PLL_DIV(
1521 .name = "plld_dsi1",
1522 .source_pll = "plld",
1523 .cm_reg = CM_PLLD,
1524 .a2w_reg = A2W_PLLD_DSI1,
1525 .load_mask = CM_PLLD_LOADDSI1,
1526 .hold_mask = CM_PLLD_HOLDDSI1,
1527 .fixed_divider = 1),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001528
1529 /*
1530 * PLLH is used to supply the pixel clock or the AUX clock for the
1531 * TV encoder.
1532 *
1533 * It is in the HDMI power domain.
1534 */
1535 [BCM2835_PLLH] = REGISTER_PLL(
1536 "pllh",
1537 .cm_ctrl_reg = CM_PLLH,
1538 .a2w_ctrl_reg = A2W_PLLH_CTRL,
1539 .frac_reg = A2W_PLLH_FRAC,
1540 .ana_reg_base = A2W_PLLH_ANA0,
1541 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1542 .lock_mask = CM_LOCK_FLOCKH,
1543
1544 .ana = &bcm2835_ana_pllh,
1545
1546 .min_rate = 600000000u,
1547 .max_rate = 3000000000u,
1548 .max_fb_rate = BCM2835_MAX_FB_RATE),
1549 [BCM2835_PLLH_RCAL] = REGISTER_PLL_DIV(
1550 .name = "pllh_rcal",
1551 .source_pll = "pllh",
1552 .cm_reg = CM_PLLH,
1553 .a2w_reg = A2W_PLLH_RCAL,
1554 .load_mask = CM_PLLH_LOADRCAL,
1555 .hold_mask = 0,
1556 .fixed_divider = 10),
1557 [BCM2835_PLLH_AUX] = REGISTER_PLL_DIV(
1558 .name = "pllh_aux",
1559 .source_pll = "pllh",
1560 .cm_reg = CM_PLLH,
1561 .a2w_reg = A2W_PLLH_AUX,
1562 .load_mask = CM_PLLH_LOADAUX,
1563 .hold_mask = 0,
1564 .fixed_divider = 10),
1565 [BCM2835_PLLH_PIX] = REGISTER_PLL_DIV(
1566 .name = "pllh_pix",
1567 .source_pll = "pllh",
1568 .cm_reg = CM_PLLH,
1569 .a2w_reg = A2W_PLLH_PIX,
1570 .load_mask = CM_PLLH_LOADPIX,
1571 .hold_mask = 0,
1572 .fixed_divider = 10),
1573
Martin Sperl56eb3a22016-02-29 12:51:41 +00001574 /* the clocks */
Martin Sperl3b15afe2016-02-29 12:51:42 +00001575
1576 /* clocks with oscillator parent mux */
1577
1578 /* One Time Programmable Memory clock. Maximum 10Mhz. */
1579 [BCM2835_CLOCK_OTP] = REGISTER_OSC_CLK(
1580 .name = "otp",
1581 .ctl_reg = CM_OTPCTL,
1582 .div_reg = CM_OTPDIV,
1583 .int_bits = 4,
1584 .frac_bits = 0),
1585 /*
1586 * Used for a 1Mhz clock for the system clocksource, and also used
1587 * bythe watchdog timer and the camera pulse generator.
1588 */
1589 [BCM2835_CLOCK_TIMER] = REGISTER_OSC_CLK(
1590 .name = "timer",
1591 .ctl_reg = CM_TIMERCTL,
1592 .div_reg = CM_TIMERDIV,
1593 .int_bits = 6,
1594 .frac_bits = 12),
1595 /*
1596 * Clock for the temperature sensor.
1597 * Generally run at 2Mhz, max 5Mhz.
1598 */
1599 [BCM2835_CLOCK_TSENS] = REGISTER_OSC_CLK(
1600 .name = "tsens",
1601 .ctl_reg = CM_TSENSCTL,
1602 .div_reg = CM_TSENSDIV,
1603 .int_bits = 5,
1604 .frac_bits = 0),
Martin Sperld3d6f152016-02-29 15:43:57 +00001605 [BCM2835_CLOCK_TEC] = REGISTER_OSC_CLK(
1606 .name = "tec",
1607 .ctl_reg = CM_TECCTL,
1608 .div_reg = CM_TECDIV,
1609 .int_bits = 6,
1610 .frac_bits = 0),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001611
1612 /* clocks with vpu parent mux */
1613 [BCM2835_CLOCK_H264] = REGISTER_VPU_CLK(
1614 .name = "h264",
1615 .ctl_reg = CM_H264CTL,
1616 .div_reg = CM_H264DIV,
1617 .int_bits = 4,
1618 .frac_bits = 8),
1619 [BCM2835_CLOCK_ISP] = REGISTER_VPU_CLK(
1620 .name = "isp",
1621 .ctl_reg = CM_ISPCTL,
1622 .div_reg = CM_ISPDIV,
1623 .int_bits = 4,
1624 .frac_bits = 8),
Martin Sperld3d6f152016-02-29 15:43:57 +00001625
Martin Sperl3b15afe2016-02-29 12:51:42 +00001626 /*
1627 * Secondary SDRAM clock. Used for low-voltage modes when the PLL
1628 * in the SDRAM controller can't be used.
1629 */
1630 [BCM2835_CLOCK_SDRAM] = REGISTER_VPU_CLK(
1631 .name = "sdram",
1632 .ctl_reg = CM_SDCCTL,
1633 .div_reg = CM_SDCDIV,
1634 .int_bits = 6,
1635 .frac_bits = 0),
1636 [BCM2835_CLOCK_V3D] = REGISTER_VPU_CLK(
1637 .name = "v3d",
1638 .ctl_reg = CM_V3DCTL,
1639 .div_reg = CM_V3DDIV,
1640 .int_bits = 4,
1641 .frac_bits = 8),
1642 /*
1643 * VPU clock. This doesn't have an enable bit, since it drives
1644 * the bus for everything else, and is special so it doesn't need
1645 * to be gated for rate changes. It is also known as "clk_audio"
1646 * in various hardware documentation.
1647 */
1648 [BCM2835_CLOCK_VPU] = REGISTER_VPU_CLK(
1649 .name = "vpu",
1650 .ctl_reg = CM_VPUCTL,
1651 .div_reg = CM_VPUDIV,
1652 .int_bits = 12,
1653 .frac_bits = 8,
Eric Anholte69fdcc2016-06-01 12:05:33 -07001654 .flags = CLK_IS_CRITICAL,
Martin Sperl3b15afe2016-02-29 12:51:42 +00001655 .is_vpu_clock = true),
1656
1657 /* clocks with per parent mux */
Martin Sperld3d6f152016-02-29 15:43:57 +00001658 [BCM2835_CLOCK_AVEO] = REGISTER_PER_CLK(
1659 .name = "aveo",
1660 .ctl_reg = CM_AVEOCTL,
1661 .div_reg = CM_AVEODIV,
1662 .int_bits = 4,
1663 .frac_bits = 0),
1664 [BCM2835_CLOCK_CAM0] = REGISTER_PER_CLK(
1665 .name = "cam0",
1666 .ctl_reg = CM_CAM0CTL,
1667 .div_reg = CM_CAM0DIV,
1668 .int_bits = 4,
1669 .frac_bits = 8),
1670 [BCM2835_CLOCK_CAM1] = REGISTER_PER_CLK(
1671 .name = "cam1",
1672 .ctl_reg = CM_CAM1CTL,
1673 .div_reg = CM_CAM1DIV,
1674 .int_bits = 4,
1675 .frac_bits = 8),
1676 [BCM2835_CLOCK_DFT] = REGISTER_PER_CLK(
1677 .name = "dft",
1678 .ctl_reg = CM_DFTCTL,
1679 .div_reg = CM_DFTDIV,
1680 .int_bits = 5,
1681 .frac_bits = 0),
1682 [BCM2835_CLOCK_DPI] = REGISTER_PER_CLK(
1683 .name = "dpi",
1684 .ctl_reg = CM_DPICTL,
1685 .div_reg = CM_DPIDIV,
1686 .int_bits = 4,
1687 .frac_bits = 8),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001688
1689 /* Arasan EMMC clock */
1690 [BCM2835_CLOCK_EMMC] = REGISTER_PER_CLK(
1691 .name = "emmc",
1692 .ctl_reg = CM_EMMCCTL,
1693 .div_reg = CM_EMMCDIV,
1694 .int_bits = 4,
1695 .frac_bits = 8),
Martin Sperld3d6f152016-02-29 15:43:57 +00001696
1697 /* General purpose (GPIO) clocks */
1698 [BCM2835_CLOCK_GP0] = REGISTER_PER_CLK(
1699 .name = "gp0",
1700 .ctl_reg = CM_GP0CTL,
1701 .div_reg = CM_GP0DIV,
1702 .int_bits = 12,
1703 .frac_bits = 12,
1704 .is_mash_clock = true),
1705 [BCM2835_CLOCK_GP1] = REGISTER_PER_CLK(
1706 .name = "gp1",
1707 .ctl_reg = CM_GP1CTL,
1708 .div_reg = CM_GP1DIV,
1709 .int_bits = 12,
1710 .frac_bits = 12,
1711 .is_mash_clock = true),
1712 [BCM2835_CLOCK_GP2] = REGISTER_PER_CLK(
1713 .name = "gp2",
1714 .ctl_reg = CM_GP2CTL,
1715 .div_reg = CM_GP2DIV,
1716 .int_bits = 12,
1717 .frac_bits = 12),
1718
Martin Sperl3b15afe2016-02-29 12:51:42 +00001719 /* HDMI state machine */
1720 [BCM2835_CLOCK_HSM] = REGISTER_PER_CLK(
1721 .name = "hsm",
1722 .ctl_reg = CM_HSMCTL,
1723 .div_reg = CM_HSMDIV,
1724 .int_bits = 4,
1725 .frac_bits = 8),
Martin Sperl33b68962016-02-29 12:51:43 +00001726 [BCM2835_CLOCK_PCM] = REGISTER_PER_CLK(
1727 .name = "pcm",
1728 .ctl_reg = CM_PCMCTL,
1729 .div_reg = CM_PCMDIV,
1730 .int_bits = 12,
1731 .frac_bits = 12,
1732 .is_mash_clock = true),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001733 [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK(
1734 .name = "pwm",
1735 .ctl_reg = CM_PWMCTL,
1736 .div_reg = CM_PWMDIV,
1737 .int_bits = 12,
1738 .frac_bits = 12,
1739 .is_mash_clock = true),
Martin Sperld3d6f152016-02-29 15:43:57 +00001740 [BCM2835_CLOCK_SLIM] = REGISTER_PER_CLK(
1741 .name = "slim",
1742 .ctl_reg = CM_SLIMCTL,
1743 .div_reg = CM_SLIMDIV,
1744 .int_bits = 12,
1745 .frac_bits = 12,
1746 .is_mash_clock = true),
1747 [BCM2835_CLOCK_SMI] = REGISTER_PER_CLK(
1748 .name = "smi",
1749 .ctl_reg = CM_SMICTL,
1750 .div_reg = CM_SMIDIV,
1751 .int_bits = 4,
1752 .frac_bits = 8),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001753 [BCM2835_CLOCK_UART] = REGISTER_PER_CLK(
1754 .name = "uart",
1755 .ctl_reg = CM_UARTCTL,
1756 .div_reg = CM_UARTDIV,
1757 .int_bits = 10,
1758 .frac_bits = 12),
Martin Sperld3d6f152016-02-29 15:43:57 +00001759
Martin Sperl3b15afe2016-02-29 12:51:42 +00001760 /* TV encoder clock. Only operating frequency is 108Mhz. */
1761 [BCM2835_CLOCK_VEC] = REGISTER_PER_CLK(
1762 .name = "vec",
1763 .ctl_reg = CM_VECCTL,
1764 .div_reg = CM_VECDIV,
1765 .int_bits = 4,
1766 .frac_bits = 0),
1767
Martin Sperld3d6f152016-02-29 15:43:57 +00001768 /* dsi clocks */
1769 [BCM2835_CLOCK_DSI0E] = REGISTER_PER_CLK(
1770 .name = "dsi0e",
1771 .ctl_reg = CM_DSI0ECTL,
1772 .div_reg = CM_DSI0EDIV,
1773 .int_bits = 4,
1774 .frac_bits = 8),
1775 [BCM2835_CLOCK_DSI1E] = REGISTER_PER_CLK(
1776 .name = "dsi1e",
1777 .ctl_reg = CM_DSI1ECTL,
1778 .div_reg = CM_DSI1EDIV,
1779 .int_bits = 4,
1780 .frac_bits = 8),
1781
Martin Sperl56eb3a22016-02-29 12:51:41 +00001782 /* the gates */
Martin Sperl3b15afe2016-02-29 12:51:42 +00001783
1784 /*
1785 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
1786 * you have the debug bit set in the power manager, which we
1787 * don't bother exposing) are individual gates off of the
1788 * non-stop vpu clock.
1789 */
Martin Sperl56eb3a22016-02-29 12:51:41 +00001790 [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE(
Martin Sperl3b15afe2016-02-29 12:51:42 +00001791 .name = "peri_image",
1792 .parent = "vpu",
1793 .ctl_reg = CM_PERIICTL),
Martin Sperl56eb3a22016-02-29 12:51:41 +00001794};
1795
Eric Anholt41691b82015-10-08 18:37:24 -07001796static int bcm2835_clk_probe(struct platform_device *pdev)
1797{
1798 struct device *dev = &pdev->dev;
1799 struct clk **clks;
1800 struct bcm2835_cprman *cprman;
1801 struct resource *res;
Martin Sperl56eb3a22016-02-29 12:51:41 +00001802 const struct bcm2835_clk_desc *desc;
1803 const size_t asize = ARRAY_SIZE(clk_desc_array);
1804 size_t i;
Eric Anholt41691b82015-10-08 18:37:24 -07001805
Martin Sperl56eb3a22016-02-29 12:51:41 +00001806 cprman = devm_kzalloc(dev,
1807 sizeof(*cprman) + asize * sizeof(*clks),
1808 GFP_KERNEL);
Eric Anholt41691b82015-10-08 18:37:24 -07001809 if (!cprman)
1810 return -ENOMEM;
1811
1812 spin_lock_init(&cprman->regs_lock);
1813 cprman->dev = dev;
1814 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1815 cprman->regs = devm_ioremap_resource(dev, res);
1816 if (IS_ERR(cprman->regs))
1817 return PTR_ERR(cprman->regs);
1818
1819 cprman->osc_name = of_clk_get_parent_name(dev->of_node, 0);
1820 if (!cprman->osc_name)
1821 return -ENODEV;
1822
1823 platform_set_drvdata(pdev, cprman);
1824
Martin Sperl56eb3a22016-02-29 12:51:41 +00001825 cprman->onecell.clk_num = asize;
Eric Anholt41691b82015-10-08 18:37:24 -07001826 cprman->onecell.clks = cprman->clks;
1827 clks = cprman->clks;
1828
Martin Sperl56eb3a22016-02-29 12:51:41 +00001829 for (i = 0; i < asize; i++) {
1830 desc = &clk_desc_array[i];
1831 if (desc->clk_register && desc->data)
1832 clks[i] = desc->clk_register(cprman, desc->data);
1833 }
Remi Pommarelcfbab8f2015-12-06 17:22:48 +01001834
Eric Anholt41691b82015-10-08 18:37:24 -07001835 return of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1836 &cprman->onecell);
1837}
1838
1839static const struct of_device_id bcm2835_clk_of_match[] = {
1840 { .compatible = "brcm,bcm2835-cprman", },
1841 {}
1842};
1843MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
1844
1845static struct platform_driver bcm2835_clk_driver = {
1846 .driver = {
1847 .name = "bcm2835-clk",
1848 .of_match_table = bcm2835_clk_of_match,
1849 },
1850 .probe = bcm2835_clk_probe,
1851};
1852
1853builtin_platform_driver(bcm2835_clk_driver);
1854
1855MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
1856MODULE_DESCRIPTION("BCM2835 clock driver");
1857MODULE_LICENSE("GPL v2");