blob: 156ce548ebf50665f9819f6a0f63292284bf1739 [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
120#define CM_EMMCCTL 0x1c0
121#define CM_EMMCDIV 0x1c4
122
123/* General bits for the CM_*CTL regs */
124# define CM_ENABLE BIT(4)
125# define CM_KILL BIT(5)
126# define CM_GATE_BIT 6
127# define CM_GATE BIT(CM_GATE_BIT)
128# define CM_BUSY BIT(7)
129# define CM_BUSYD BIT(8)
Martin Sperl959ca922016-02-29 11:39:21 +0000130# define CM_FRAC BIT(9)
Eric Anholt41691b82015-10-08 18:37:24 -0700131# define CM_SRC_SHIFT 0
132# define CM_SRC_BITS 4
133# define CM_SRC_MASK 0xf
134# define CM_SRC_GND 0
135# define CM_SRC_OSC 1
136# define CM_SRC_TESTDEBUG0 2
137# define CM_SRC_TESTDEBUG1 3
138# define CM_SRC_PLLA_CORE 4
139# define CM_SRC_PLLA_PER 4
140# define CM_SRC_PLLC_CORE0 5
141# define CM_SRC_PLLC_PER 5
142# define CM_SRC_PLLC_CORE1 8
143# define CM_SRC_PLLD_CORE 6
144# define CM_SRC_PLLD_PER 6
145# define CM_SRC_PLLH_AUX 7
146# define CM_SRC_PLLC_CORE1 8
147# define CM_SRC_PLLC_CORE2 9
148
149#define CM_OSCCOUNT 0x100
150
151#define CM_PLLA 0x104
152# define CM_PLL_ANARST BIT(8)
153# define CM_PLLA_HOLDPER BIT(7)
154# define CM_PLLA_LOADPER BIT(6)
155# define CM_PLLA_HOLDCORE BIT(5)
156# define CM_PLLA_LOADCORE BIT(4)
157# define CM_PLLA_HOLDCCP2 BIT(3)
158# define CM_PLLA_LOADCCP2 BIT(2)
159# define CM_PLLA_HOLDDSI0 BIT(1)
160# define CM_PLLA_LOADDSI0 BIT(0)
161
162#define CM_PLLC 0x108
163# define CM_PLLC_HOLDPER BIT(7)
164# define CM_PLLC_LOADPER BIT(6)
165# define CM_PLLC_HOLDCORE2 BIT(5)
166# define CM_PLLC_LOADCORE2 BIT(4)
167# define CM_PLLC_HOLDCORE1 BIT(3)
168# define CM_PLLC_LOADCORE1 BIT(2)
169# define CM_PLLC_HOLDCORE0 BIT(1)
170# define CM_PLLC_LOADCORE0 BIT(0)
171
172#define CM_PLLD 0x10c
173# define CM_PLLD_HOLDPER BIT(7)
174# define CM_PLLD_LOADPER BIT(6)
175# define CM_PLLD_HOLDCORE BIT(5)
176# define CM_PLLD_LOADCORE BIT(4)
177# define CM_PLLD_HOLDDSI1 BIT(3)
178# define CM_PLLD_LOADDSI1 BIT(2)
179# define CM_PLLD_HOLDDSI0 BIT(1)
180# define CM_PLLD_LOADDSI0 BIT(0)
181
182#define CM_PLLH 0x110
183# define CM_PLLH_LOADRCAL BIT(2)
184# define CM_PLLH_LOADAUX BIT(1)
185# define CM_PLLH_LOADPIX BIT(0)
186
187#define CM_LOCK 0x114
188# define CM_LOCK_FLOCKH BIT(12)
189# define CM_LOCK_FLOCKD BIT(11)
190# define CM_LOCK_FLOCKC BIT(10)
191# define CM_LOCK_FLOCKB BIT(9)
192# define CM_LOCK_FLOCKA BIT(8)
193
194#define CM_EVENT 0x118
195#define CM_DSI1ECTL 0x158
196#define CM_DSI1EDIV 0x15c
197#define CM_DSI1PCTL 0x160
198#define CM_DSI1PDIV 0x164
199#define CM_DFTCTL 0x168
200#define CM_DFTDIV 0x16c
201
202#define CM_PLLB 0x170
203# define CM_PLLB_HOLDARM BIT(1)
204# define CM_PLLB_LOADARM BIT(0)
205
206#define A2W_PLLA_CTRL 0x1100
207#define A2W_PLLC_CTRL 0x1120
208#define A2W_PLLD_CTRL 0x1140
209#define A2W_PLLH_CTRL 0x1160
210#define A2W_PLLB_CTRL 0x11e0
211# define A2W_PLL_CTRL_PRST_DISABLE BIT(17)
212# define A2W_PLL_CTRL_PWRDN BIT(16)
213# define A2W_PLL_CTRL_PDIV_MASK 0x000007000
214# define A2W_PLL_CTRL_PDIV_SHIFT 12
215# define A2W_PLL_CTRL_NDIV_MASK 0x0000003ff
216# define A2W_PLL_CTRL_NDIV_SHIFT 0
217
218#define A2W_PLLA_ANA0 0x1010
219#define A2W_PLLC_ANA0 0x1030
220#define A2W_PLLD_ANA0 0x1050
221#define A2W_PLLH_ANA0 0x1070
222#define A2W_PLLB_ANA0 0x10f0
223
224#define A2W_PLL_KA_SHIFT 7
225#define A2W_PLL_KA_MASK GENMASK(9, 7)
226#define A2W_PLL_KI_SHIFT 19
227#define A2W_PLL_KI_MASK GENMASK(21, 19)
228#define A2W_PLL_KP_SHIFT 15
229#define A2W_PLL_KP_MASK GENMASK(18, 15)
230
231#define A2W_PLLH_KA_SHIFT 19
232#define A2W_PLLH_KA_MASK GENMASK(21, 19)
233#define A2W_PLLH_KI_LOW_SHIFT 22
234#define A2W_PLLH_KI_LOW_MASK GENMASK(23, 22)
235#define A2W_PLLH_KI_HIGH_SHIFT 0
236#define A2W_PLLH_KI_HIGH_MASK GENMASK(0, 0)
237#define A2W_PLLH_KP_SHIFT 1
238#define A2W_PLLH_KP_MASK GENMASK(4, 1)
239
240#define A2W_XOSC_CTRL 0x1190
241# define A2W_XOSC_CTRL_PLLB_ENABLE BIT(7)
242# define A2W_XOSC_CTRL_PLLA_ENABLE BIT(6)
243# define A2W_XOSC_CTRL_PLLD_ENABLE BIT(5)
244# define A2W_XOSC_CTRL_DDR_ENABLE BIT(4)
245# define A2W_XOSC_CTRL_CPR1_ENABLE BIT(3)
246# define A2W_XOSC_CTRL_USB_ENABLE BIT(2)
247# define A2W_XOSC_CTRL_HDMI_ENABLE BIT(1)
248# define A2W_XOSC_CTRL_PLLC_ENABLE BIT(0)
249
250#define A2W_PLLA_FRAC 0x1200
251#define A2W_PLLC_FRAC 0x1220
252#define A2W_PLLD_FRAC 0x1240
253#define A2W_PLLH_FRAC 0x1260
254#define A2W_PLLB_FRAC 0x12e0
255# define A2W_PLL_FRAC_MASK ((1 << A2W_PLL_FRAC_BITS) - 1)
256# define A2W_PLL_FRAC_BITS 20
257
258#define A2W_PLL_CHANNEL_DISABLE BIT(8)
259#define A2W_PLL_DIV_BITS 8
260#define A2W_PLL_DIV_SHIFT 0
261
262#define A2W_PLLA_DSI0 0x1300
263#define A2W_PLLA_CORE 0x1400
264#define A2W_PLLA_PER 0x1500
265#define A2W_PLLA_CCP2 0x1600
266
267#define A2W_PLLC_CORE2 0x1320
268#define A2W_PLLC_CORE1 0x1420
269#define A2W_PLLC_PER 0x1520
270#define A2W_PLLC_CORE0 0x1620
271
272#define A2W_PLLD_DSI0 0x1340
273#define A2W_PLLD_CORE 0x1440
274#define A2W_PLLD_PER 0x1540
275#define A2W_PLLD_DSI1 0x1640
276
277#define A2W_PLLH_AUX 0x1360
278#define A2W_PLLH_RCAL 0x1460
279#define A2W_PLLH_PIX 0x1560
280#define A2W_PLLH_STS 0x1660
281
282#define A2W_PLLH_CTRLR 0x1960
283#define A2W_PLLH_FRACR 0x1a60
284#define A2W_PLLH_AUXR 0x1b60
285#define A2W_PLLH_RCALR 0x1c60
286#define A2W_PLLH_PIXR 0x1d60
287#define A2W_PLLH_STSR 0x1e60
288
289#define A2W_PLLB_ARM 0x13e0
290#define A2W_PLLB_SP0 0x14e0
291#define A2W_PLLB_SP1 0x15e0
292#define A2W_PLLB_SP2 0x16e0
293
294#define LOCK_TIMEOUT_NS 100000000
295#define BCM2835_MAX_FB_RATE 1750000000u
296
297struct bcm2835_cprman {
298 struct device *dev;
299 void __iomem *regs;
Martin Sperl6e1e60d2016-02-29 11:39:22 +0000300 spinlock_t regs_lock; /* spinlock for all clocks */
Eric Anholt41691b82015-10-08 18:37:24 -0700301 const char *osc_name;
302
303 struct clk_onecell_data onecell;
Martin Sperl56eb3a2e2016-02-29 12:51:41 +0000304 struct clk *clks[];
Eric Anholt41691b82015-10-08 18:37:24 -0700305};
306
307static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
308{
309 writel(CM_PASSWORD | val, cprman->regs + reg);
310}
311
312static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
313{
314 return readl(cprman->regs + reg);
315}
Stephen Warren526d2392012-12-24 21:55:01 -0700316
Martin Sperl96bf9c62016-02-29 14:20:15 +0000317static int bcm2835_debugfs_regset(struct bcm2835_cprman *cprman, u32 base,
318 struct debugfs_reg32 *regs, size_t nregs,
319 struct dentry *dentry)
320{
321 struct dentry *regdump;
322 struct debugfs_regset32 *regset;
323
324 regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL);
325 if (!regset)
326 return -ENOMEM;
327
328 regset->regs = regs;
329 regset->nregs = nregs;
330 regset->base = cprman->regs + base;
331
332 regdump = debugfs_create_regset32("regdump", S_IRUGO, dentry,
333 regset);
334
335 return regdump ? 0 : -ENOMEM;
336}
337
Simon Arlott75fabc32012-09-10 23:26:15 -0600338/*
339 * These are fixed clocks. They're probably not all root clocks and it may
340 * be possible to turn them on and off but until this is mapped out better
341 * it's the only way they can be used.
342 */
343void __init bcm2835_init_clocks(void)
344{
345 struct clk *clk;
346 int ret;
347
Stephen Boydbd41aa672016-03-01 10:59:47 -0800348 clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, 0, 126000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800349 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600350 pr_err("apb_pclk not registered\n");
351
Stephen Boydbd41aa672016-03-01 10:59:47 -0800352 clk = clk_register_fixed_rate(NULL, "uart0_pclk", NULL, 0, 3000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800353 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600354 pr_err("uart0_pclk not registered\n");
355 ret = clk_register_clkdev(clk, NULL, "20201000.uart");
356 if (ret)
357 pr_err("uart0_pclk alias not registered\n");
358
Stephen Boydbd41aa672016-03-01 10:59:47 -0800359 clk = clk_register_fixed_rate(NULL, "uart1_pclk", NULL, 0, 125000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800360 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600361 pr_err("uart1_pclk not registered\n");
362 ret = clk_register_clkdev(clk, NULL, "20215000.uart");
363 if (ret)
Domenico Andreoli686ea582012-10-20 03:35:28 +0200364 pr_err("uart1_pclk alias not registered\n");
Simon Arlott75fabc32012-09-10 23:26:15 -0600365}
Eric Anholt41691b82015-10-08 18:37:24 -0700366
367struct bcm2835_pll_data {
368 const char *name;
369 u32 cm_ctrl_reg;
370 u32 a2w_ctrl_reg;
371 u32 frac_reg;
372 u32 ana_reg_base;
373 u32 reference_enable_mask;
374 /* Bit in CM_LOCK to indicate when the PLL has locked. */
375 u32 lock_mask;
376
377 const struct bcm2835_pll_ana_bits *ana;
378
379 unsigned long min_rate;
380 unsigned long max_rate;
381 /*
382 * Highest rate for the VCO before we have to use the
383 * pre-divide-by-2.
384 */
385 unsigned long max_fb_rate;
386};
387
388struct bcm2835_pll_ana_bits {
389 u32 mask0;
390 u32 set0;
391 u32 mask1;
392 u32 set1;
393 u32 mask3;
394 u32 set3;
395 u32 fb_prediv_mask;
396};
397
398static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
399 .mask0 = 0,
400 .set0 = 0,
401 .mask1 = ~(A2W_PLL_KI_MASK | A2W_PLL_KP_MASK),
402 .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
403 .mask3 = ~A2W_PLL_KA_MASK,
404 .set3 = (2 << A2W_PLL_KA_SHIFT),
405 .fb_prediv_mask = BIT(14),
406};
407
408static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
409 .mask0 = ~(A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK),
410 .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
411 .mask1 = ~(A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK),
412 .set1 = (6 << A2W_PLLH_KP_SHIFT),
413 .mask3 = 0,
414 .set3 = 0,
415 .fb_prediv_mask = BIT(11),
416};
417
Eric Anholt41691b82015-10-08 18:37:24 -0700418struct bcm2835_pll_divider_data {
419 const char *name;
Martin Sperl3b15afe2016-02-29 12:51:42 +0000420 const char *source_pll;
421
Eric Anholt41691b82015-10-08 18:37:24 -0700422 u32 cm_reg;
423 u32 a2w_reg;
424
425 u32 load_mask;
426 u32 hold_mask;
427 u32 fixed_divider;
428};
429
Eric Anholt41691b82015-10-08 18:37:24 -0700430struct bcm2835_clock_data {
431 const char *name;
432
433 const char *const *parents;
434 int num_mux_parents;
435
436 u32 ctl_reg;
437 u32 div_reg;
438
439 /* Number of integer bits in the divider */
440 u32 int_bits;
441 /* Number of fractional bits in the divider */
442 u32 frac_bits;
443
444 bool is_vpu_clock;
Martin Sperl959ca922016-02-29 11:39:21 +0000445 bool is_mash_clock;
Eric Anholt41691b82015-10-08 18:37:24 -0700446};
447
Martin Sperl56eb3a2e2016-02-29 12:51:41 +0000448struct bcm2835_gate_data {
449 const char *name;
450 const char *parent;
451
452 u32 ctl_reg;
453};
454
Eric Anholt41691b82015-10-08 18:37:24 -0700455struct bcm2835_pll {
456 struct clk_hw hw;
457 struct bcm2835_cprman *cprman;
458 const struct bcm2835_pll_data *data;
459};
460
461static int bcm2835_pll_is_on(struct clk_hw *hw)
462{
463 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
464 struct bcm2835_cprman *cprman = pll->cprman;
465 const struct bcm2835_pll_data *data = pll->data;
466
467 return cprman_read(cprman, data->a2w_ctrl_reg) &
468 A2W_PLL_CTRL_PRST_DISABLE;
469}
470
471static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
472 unsigned long parent_rate,
473 u32 *ndiv, u32 *fdiv)
474{
475 u64 div;
476
477 div = (u64)rate << A2W_PLL_FRAC_BITS;
478 do_div(div, parent_rate);
479
480 *ndiv = div >> A2W_PLL_FRAC_BITS;
481 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
482}
483
484static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
485 u32 ndiv, u32 fdiv, u32 pdiv)
486{
487 u64 rate;
488
489 if (pdiv == 0)
490 return 0;
491
492 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
493 do_div(rate, pdiv);
494 return rate >> A2W_PLL_FRAC_BITS;
495}
496
497static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
498 unsigned long *parent_rate)
499{
500 u32 ndiv, fdiv;
501
502 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
503
504 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
505}
506
507static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
508 unsigned long parent_rate)
509{
510 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
511 struct bcm2835_cprman *cprman = pll->cprman;
512 const struct bcm2835_pll_data *data = pll->data;
513 u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
514 u32 ndiv, pdiv, fdiv;
515 bool using_prediv;
516
517 if (parent_rate == 0)
518 return 0;
519
520 fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
521 ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
522 pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
523 using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
524 data->ana->fb_prediv_mask;
525
526 if (using_prediv)
527 ndiv *= 2;
528
529 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
530}
531
532static void bcm2835_pll_off(struct clk_hw *hw)
533{
534 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
535 struct bcm2835_cprman *cprman = pll->cprman;
536 const struct bcm2835_pll_data *data = pll->data;
537
Martin Sperl6727f082016-02-29 11:39:17 +0000538 spin_lock(&cprman->regs_lock);
539 cprman_write(cprman, data->cm_ctrl_reg,
540 cprman_read(cprman, data->cm_ctrl_reg) |
541 CM_PLL_ANARST);
542 cprman_write(cprman, data->a2w_ctrl_reg,
543 cprman_read(cprman, data->a2w_ctrl_reg) |
544 A2W_PLL_CTRL_PWRDN);
545 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700546}
547
548static int bcm2835_pll_on(struct clk_hw *hw)
549{
550 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
551 struct bcm2835_cprman *cprman = pll->cprman;
552 const struct bcm2835_pll_data *data = pll->data;
553 ktime_t timeout;
554
555 /* Take the PLL out of reset. */
556 cprman_write(cprman, data->cm_ctrl_reg,
557 cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
558
559 /* Wait for the PLL to lock. */
560 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
561 while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
562 if (ktime_after(ktime_get(), timeout)) {
563 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
564 clk_hw_get_name(hw));
565 return -ETIMEDOUT;
566 }
567
568 cpu_relax();
569 }
570
571 return 0;
572}
573
574static void
575bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
576{
577 int i;
578
579 /*
580 * ANA register setup is done as a series of writes to
581 * ANA3-ANA0, in that order. This lets us write all 4
582 * registers as a single cycle of the serdes interface (taking
583 * 100 xosc clocks), whereas if we were to update ana0, 1, and
584 * 3 individually through their partial-write registers, each
585 * would be their own serdes cycle.
586 */
587 for (i = 3; i >= 0; i--)
588 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
589}
590
591static int bcm2835_pll_set_rate(struct clk_hw *hw,
592 unsigned long rate, unsigned long parent_rate)
593{
594 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
595 struct bcm2835_cprman *cprman = pll->cprman;
596 const struct bcm2835_pll_data *data = pll->data;
597 bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
598 u32 ndiv, fdiv, a2w_ctl;
599 u32 ana[4];
600 int i;
601
602 if (rate < data->min_rate || rate > data->max_rate) {
603 dev_err(cprman->dev, "%s: rate out of spec: %lu vs (%lu, %lu)\n",
604 clk_hw_get_name(hw), rate,
605 data->min_rate, data->max_rate);
606 return -EINVAL;
607 }
608
609 if (rate > data->max_fb_rate) {
610 use_fb_prediv = true;
611 rate /= 2;
612 } else {
613 use_fb_prediv = false;
614 }
615
616 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
617
618 for (i = 3; i >= 0; i--)
619 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
620
621 was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
622
623 ana[0] &= ~data->ana->mask0;
624 ana[0] |= data->ana->set0;
625 ana[1] &= ~data->ana->mask1;
626 ana[1] |= data->ana->set1;
627 ana[3] &= ~data->ana->mask3;
628 ana[3] |= data->ana->set3;
629
630 if (was_using_prediv && !use_fb_prediv) {
631 ana[1] &= ~data->ana->fb_prediv_mask;
632 do_ana_setup_first = true;
633 } else if (!was_using_prediv && use_fb_prediv) {
634 ana[1] |= data->ana->fb_prediv_mask;
635 do_ana_setup_first = false;
636 } else {
637 do_ana_setup_first = true;
638 }
639
640 /* Unmask the reference clock from the oscillator. */
641 cprman_write(cprman, A2W_XOSC_CTRL,
642 cprman_read(cprman, A2W_XOSC_CTRL) |
643 data->reference_enable_mask);
644
645 if (do_ana_setup_first)
646 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
647
648 /* Set the PLL multiplier from the oscillator. */
649 cprman_write(cprman, data->frac_reg, fdiv);
650
651 a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
652 a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
653 a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
654 a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
655 a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
656 cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
657
658 if (!do_ana_setup_first)
659 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
660
661 return 0;
662}
663
Martin Sperl96bf9c62016-02-29 14:20:15 +0000664static int bcm2835_pll_debug_init(struct clk_hw *hw,
665 struct dentry *dentry)
666{
667 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
668 struct bcm2835_cprman *cprman = pll->cprman;
669 const struct bcm2835_pll_data *data = pll->data;
670 struct debugfs_reg32 *regs;
671
672 regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
673 if (!regs)
674 return -ENOMEM;
675
676 regs[0].name = "cm_ctrl";
677 regs[0].offset = data->cm_ctrl_reg;
678 regs[1].name = "a2w_ctrl";
679 regs[1].offset = data->a2w_ctrl_reg;
680 regs[2].name = "frac";
681 regs[2].offset = data->frac_reg;
682 regs[3].name = "ana0";
683 regs[3].offset = data->ana_reg_base + 0 * 4;
684 regs[4].name = "ana1";
685 regs[4].offset = data->ana_reg_base + 1 * 4;
686 regs[5].name = "ana2";
687 regs[5].offset = data->ana_reg_base + 2 * 4;
688 regs[6].name = "ana3";
689 regs[6].offset = data->ana_reg_base + 3 * 4;
690
691 return bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry);
692}
693
Eric Anholt41691b82015-10-08 18:37:24 -0700694static const struct clk_ops bcm2835_pll_clk_ops = {
695 .is_prepared = bcm2835_pll_is_on,
696 .prepare = bcm2835_pll_on,
697 .unprepare = bcm2835_pll_off,
698 .recalc_rate = bcm2835_pll_get_rate,
699 .set_rate = bcm2835_pll_set_rate,
700 .round_rate = bcm2835_pll_round_rate,
Martin Sperl96bf9c62016-02-29 14:20:15 +0000701 .debug_init = bcm2835_pll_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -0700702};
703
704struct bcm2835_pll_divider {
705 struct clk_divider div;
706 struct bcm2835_cprman *cprman;
707 const struct bcm2835_pll_divider_data *data;
708};
709
710static struct bcm2835_pll_divider *
711bcm2835_pll_divider_from_hw(struct clk_hw *hw)
712{
713 return container_of(hw, struct bcm2835_pll_divider, div.hw);
714}
715
716static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
717{
718 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
719 struct bcm2835_cprman *cprman = divider->cprman;
720 const struct bcm2835_pll_divider_data *data = divider->data;
721
722 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
723}
724
725static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
726 unsigned long rate,
727 unsigned long *parent_rate)
728{
729 return clk_divider_ops.round_rate(hw, rate, parent_rate);
730}
731
732static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
733 unsigned long parent_rate)
734{
Eric Anholt79c1e2f2016-02-15 19:03:58 -0800735 return clk_divider_ops.recalc_rate(hw, parent_rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700736}
737
738static void bcm2835_pll_divider_off(struct clk_hw *hw)
739{
740 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
741 struct bcm2835_cprman *cprman = divider->cprman;
742 const struct bcm2835_pll_divider_data *data = divider->data;
743
Martin Sperlec36a5c2016-02-29 11:39:18 +0000744 spin_lock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700745 cprman_write(cprman, data->cm_reg,
746 (cprman_read(cprman, data->cm_reg) &
747 ~data->load_mask) | data->hold_mask);
748 cprman_write(cprman, data->a2w_reg, A2W_PLL_CHANNEL_DISABLE);
Martin Sperlec36a5c2016-02-29 11:39:18 +0000749 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700750}
751
752static int bcm2835_pll_divider_on(struct clk_hw *hw)
753{
754 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
755 struct bcm2835_cprman *cprman = divider->cprman;
756 const struct bcm2835_pll_divider_data *data = divider->data;
757
Martin Sperlec36a5c2016-02-29 11:39:18 +0000758 spin_lock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700759 cprman_write(cprman, data->a2w_reg,
760 cprman_read(cprman, data->a2w_reg) &
761 ~A2W_PLL_CHANNEL_DISABLE);
762
763 cprman_write(cprman, data->cm_reg,
764 cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
Martin Sperlec36a5c2016-02-29 11:39:18 +0000765 spin_unlock(&cprman->regs_lock);
Eric Anholt41691b82015-10-08 18:37:24 -0700766
767 return 0;
768}
769
770static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
771 unsigned long rate,
772 unsigned long parent_rate)
773{
774 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
775 struct bcm2835_cprman *cprman = divider->cprman;
776 const struct bcm2835_pll_divider_data *data = divider->data;
Eric Anholt773b3962016-02-15 19:03:57 -0800777 u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
Eric Anholt41691b82015-10-08 18:37:24 -0700778
Eric Anholt773b3962016-02-15 19:03:57 -0800779 div = DIV_ROUND_UP_ULL(parent_rate, rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700780
Eric Anholt773b3962016-02-15 19:03:57 -0800781 div = min(div, max_div);
782 if (div == max_div)
783 div = 0;
784
785 cprman_write(cprman, data->a2w_reg, div);
Eric Anholt41691b82015-10-08 18:37:24 -0700786 cm = cprman_read(cprman, data->cm_reg);
787 cprman_write(cprman, data->cm_reg, cm | data->load_mask);
788 cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
789
790 return 0;
791}
792
Martin Sperl96bf9c62016-02-29 14:20:15 +0000793static int bcm2835_pll_divider_debug_init(struct clk_hw *hw,
794 struct dentry *dentry)
795{
796 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
797 struct bcm2835_cprman *cprman = divider->cprman;
798 const struct bcm2835_pll_divider_data *data = divider->data;
799 struct debugfs_reg32 *regs;
800
801 regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
802 if (!regs)
803 return -ENOMEM;
804
805 regs[0].name = "cm";
806 regs[0].offset = data->cm_reg;
807 regs[1].name = "a2w";
808 regs[1].offset = data->a2w_reg;
809
810 return bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry);
811}
812
Eric Anholt41691b82015-10-08 18:37:24 -0700813static const struct clk_ops bcm2835_pll_divider_clk_ops = {
814 .is_prepared = bcm2835_pll_divider_is_on,
815 .prepare = bcm2835_pll_divider_on,
816 .unprepare = bcm2835_pll_divider_off,
817 .recalc_rate = bcm2835_pll_divider_get_rate,
818 .set_rate = bcm2835_pll_divider_set_rate,
819 .round_rate = bcm2835_pll_divider_round_rate,
Martin Sperl96bf9c62016-02-29 14:20:15 +0000820 .debug_init = bcm2835_pll_divider_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -0700821};
822
823/*
824 * The CM dividers do fixed-point division, so we can't use the
825 * generic integer divider code like the PLL dividers do (and we can't
826 * fake it by having some fixed shifts preceding it in the clock tree,
827 * because we'd run out of bits in a 32-bit unsigned long).
828 */
829struct bcm2835_clock {
830 struct clk_hw hw;
831 struct bcm2835_cprman *cprman;
832 const struct bcm2835_clock_data *data;
833};
834
835static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
836{
837 return container_of(hw, struct bcm2835_clock, hw);
838}
839
840static int bcm2835_clock_is_on(struct clk_hw *hw)
841{
842 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
843 struct bcm2835_cprman *cprman = clock->cprman;
844 const struct bcm2835_clock_data *data = clock->data;
845
846 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
847}
848
849static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
850 unsigned long rate,
Remi Pommarel9c95b322015-12-06 17:22:46 +0100851 unsigned long parent_rate,
852 bool round_up)
Eric Anholt41691b82015-10-08 18:37:24 -0700853{
854 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
855 const struct bcm2835_clock_data *data = clock->data;
Remi Pommarel9c95b322015-12-06 17:22:46 +0100856 u32 unused_frac_mask =
857 GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
Eric Anholt41691b82015-10-08 18:37:24 -0700858 u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
Remi Pommarel9c95b322015-12-06 17:22:46 +0100859 u64 rem;
Martin Sperl959ca922016-02-29 11:39:21 +0000860 u32 div, mindiv, maxdiv;
Eric Anholt41691b82015-10-08 18:37:24 -0700861
Remi Pommarel9c95b322015-12-06 17:22:46 +0100862 rem = do_div(temp, rate);
Eric Anholt41691b82015-10-08 18:37:24 -0700863 div = temp;
864
Remi Pommarel9c95b322015-12-06 17:22:46 +0100865 /* Round up and mask off the unused bits */
866 if (round_up && ((div & unused_frac_mask) != 0 || rem != 0))
867 div += unused_frac_mask + 1;
868 div &= ~unused_frac_mask;
Eric Anholt41691b82015-10-08 18:37:24 -0700869
Martin Sperl959ca922016-02-29 11:39:21 +0000870 /* different clamping limits apply for a mash clock */
871 if (data->is_mash_clock) {
872 /* clamp to min divider of 2 */
873 mindiv = 2 << CM_DIV_FRAC_BITS;
874 /* clamp to the highest possible integer divider */
875 maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
876 } else {
877 /* clamp to min divider of 1 */
878 mindiv = 1 << CM_DIV_FRAC_BITS;
879 /* clamp to the highest possible fractional divider */
880 maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
881 CM_DIV_FRAC_BITS - data->frac_bits);
882 }
883
884 /* apply the clamping limits */
885 div = max_t(u32, div, mindiv);
886 div = min_t(u32, div, maxdiv);
Eric Anholt41691b82015-10-08 18:37:24 -0700887
888 return div;
889}
890
891static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
892 unsigned long parent_rate,
893 u32 div)
894{
895 const struct bcm2835_clock_data *data = clock->data;
896 u64 temp;
897
898 /*
899 * The divisor is a 12.12 fixed point field, but only some of
900 * the bits are populated in any given clock.
901 */
902 div >>= CM_DIV_FRAC_BITS - data->frac_bits;
903 div &= (1 << (data->int_bits + data->frac_bits)) - 1;
904
905 if (div == 0)
906 return 0;
907
908 temp = (u64)parent_rate << data->frac_bits;
909
910 do_div(temp, div);
911
912 return temp;
913}
914
Eric Anholt41691b82015-10-08 18:37:24 -0700915static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
916 unsigned long parent_rate)
917{
918 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
919 struct bcm2835_cprman *cprman = clock->cprman;
920 const struct bcm2835_clock_data *data = clock->data;
921 u32 div = cprman_read(cprman, data->div_reg);
922
923 return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
924}
925
926static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
927{
928 struct bcm2835_cprman *cprman = clock->cprman;
929 const struct bcm2835_clock_data *data = clock->data;
930 ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
931
932 while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
933 if (ktime_after(ktime_get(), timeout)) {
934 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
935 clk_hw_get_name(&clock->hw));
936 return;
937 }
938 cpu_relax();
939 }
940}
941
942static void bcm2835_clock_off(struct clk_hw *hw)
943{
944 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
945 struct bcm2835_cprman *cprman = clock->cprman;
946 const struct bcm2835_clock_data *data = clock->data;
947
948 spin_lock(&cprman->regs_lock);
949 cprman_write(cprman, data->ctl_reg,
950 cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
951 spin_unlock(&cprman->regs_lock);
952
953 /* BUSY will remain high until the divider completes its cycle. */
954 bcm2835_clock_wait_busy(clock);
955}
956
957static int bcm2835_clock_on(struct clk_hw *hw)
958{
959 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
960 struct bcm2835_cprman *cprman = clock->cprman;
961 const struct bcm2835_clock_data *data = clock->data;
962
963 spin_lock(&cprman->regs_lock);
964 cprman_write(cprman, data->ctl_reg,
965 cprman_read(cprman, data->ctl_reg) |
966 CM_ENABLE |
967 CM_GATE);
968 spin_unlock(&cprman->regs_lock);
969
970 return 0;
971}
972
973static int bcm2835_clock_set_rate(struct clk_hw *hw,
974 unsigned long rate, unsigned long parent_rate)
975{
976 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
977 struct bcm2835_cprman *cprman = clock->cprman;
978 const struct bcm2835_clock_data *data = clock->data;
Remi Pommarel9c95b322015-12-06 17:22:46 +0100979 u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false);
Martin Sperl959ca922016-02-29 11:39:21 +0000980 u32 ctl;
981
982 spin_lock(&cprman->regs_lock);
983
984 /*
985 * Setting up frac support
986 *
987 * In principle it is recommended to stop/start the clock first,
988 * but as we set CLK_SET_RATE_GATE during registration of the
989 * clock this requirement should be take care of by the
990 * clk-framework.
991 */
992 ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
993 ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
994 cprman_write(cprman, data->ctl_reg, ctl);
Eric Anholt41691b82015-10-08 18:37:24 -0700995
996 cprman_write(cprman, data->div_reg, div);
997
Martin Sperl959ca922016-02-29 11:39:21 +0000998 spin_unlock(&cprman->regs_lock);
999
Eric Anholt41691b82015-10-08 18:37:24 -07001000 return 0;
1001}
1002
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001003static int bcm2835_clock_determine_rate(struct clk_hw *hw,
Martin Sperl6e1e60d2016-02-29 11:39:22 +00001004 struct clk_rate_request *req)
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001005{
1006 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1007 struct clk_hw *parent, *best_parent = NULL;
1008 unsigned long rate, best_rate = 0;
1009 unsigned long prate, best_prate = 0;
1010 size_t i;
1011 u32 div;
1012
1013 /*
1014 * Select parent clock that results in the closest but lower rate
1015 */
1016 for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
1017 parent = clk_hw_get_parent_by_index(hw, i);
1018 if (!parent)
1019 continue;
1020 prate = clk_hw_get_rate(parent);
1021 div = bcm2835_clock_choose_div(hw, req->rate, prate, true);
1022 rate = bcm2835_clock_rate_from_divisor(clock, prate, div);
1023 if (rate > best_rate && rate <= req->rate) {
1024 best_parent = parent;
1025 best_prate = prate;
1026 best_rate = rate;
1027 }
1028 }
1029
1030 if (!best_parent)
1031 return -EINVAL;
1032
1033 req->best_parent_hw = best_parent;
1034 req->best_parent_rate = best_prate;
1035
1036 req->rate = best_rate;
1037
1038 return 0;
1039}
1040
1041static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
1042{
1043 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1044 struct bcm2835_cprman *cprman = clock->cprman;
1045 const struct bcm2835_clock_data *data = clock->data;
1046 u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
1047
1048 cprman_write(cprman, data->ctl_reg, src);
1049 return 0;
1050}
1051
1052static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
1053{
1054 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1055 struct bcm2835_cprman *cprman = clock->cprman;
1056 const struct bcm2835_clock_data *data = clock->data;
1057 u32 src = cprman_read(cprman, data->ctl_reg);
1058
1059 return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
1060}
1061
Martin Sperl96bf9c62016-02-29 14:20:15 +00001062static struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = {
1063 {
1064 .name = "ctl",
1065 .offset = 0,
1066 },
1067 {
1068 .name = "div",
1069 .offset = 4,
1070 },
1071};
1072
1073static int bcm2835_clock_debug_init(struct clk_hw *hw,
1074 struct dentry *dentry)
1075{
1076 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1077 struct bcm2835_cprman *cprman = clock->cprman;
1078 const struct bcm2835_clock_data *data = clock->data;
1079
1080 return bcm2835_debugfs_regset(
1081 cprman, data->ctl_reg,
1082 bcm2835_debugfs_clock_reg32,
1083 ARRAY_SIZE(bcm2835_debugfs_clock_reg32),
1084 dentry);
1085}
1086
Eric Anholt41691b82015-10-08 18:37:24 -07001087static const struct clk_ops bcm2835_clock_clk_ops = {
1088 .is_prepared = bcm2835_clock_is_on,
1089 .prepare = bcm2835_clock_on,
1090 .unprepare = bcm2835_clock_off,
1091 .recalc_rate = bcm2835_clock_get_rate,
1092 .set_rate = bcm2835_clock_set_rate,
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001093 .determine_rate = bcm2835_clock_determine_rate,
1094 .set_parent = bcm2835_clock_set_parent,
1095 .get_parent = bcm2835_clock_get_parent,
Martin Sperl96bf9c62016-02-29 14:20:15 +00001096 .debug_init = bcm2835_clock_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -07001097};
1098
1099static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1100{
1101 return true;
1102}
1103
1104/*
1105 * The VPU clock can never be disabled (it doesn't have an ENABLE
1106 * bit), so it gets its own set of clock ops.
1107 */
1108static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1109 .is_prepared = bcm2835_vpu_clock_is_on,
1110 .recalc_rate = bcm2835_clock_get_rate,
1111 .set_rate = bcm2835_clock_set_rate,
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001112 .determine_rate = bcm2835_clock_determine_rate,
1113 .set_parent = bcm2835_clock_set_parent,
1114 .get_parent = bcm2835_clock_get_parent,
Martin Sperl96bf9c62016-02-29 14:20:15 +00001115 .debug_init = bcm2835_clock_debug_init,
Eric Anholt41691b82015-10-08 18:37:24 -07001116};
1117
1118static struct clk *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1119 const struct bcm2835_pll_data *data)
1120{
1121 struct bcm2835_pll *pll;
1122 struct clk_init_data init;
1123
1124 memset(&init, 0, sizeof(init));
1125
1126 /* All of the PLLs derive from the external oscillator. */
1127 init.parent_names = &cprman->osc_name;
1128 init.num_parents = 1;
1129 init.name = data->name;
1130 init.ops = &bcm2835_pll_clk_ops;
1131 init.flags = CLK_IGNORE_UNUSED;
1132
1133 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1134 if (!pll)
1135 return NULL;
1136
1137 pll->cprman = cprman;
1138 pll->data = data;
1139 pll->hw.init = &init;
1140
1141 return devm_clk_register(cprman->dev, &pll->hw);
1142}
1143
1144static struct clk *
1145bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1146 const struct bcm2835_pll_divider_data *data)
1147{
1148 struct bcm2835_pll_divider *divider;
1149 struct clk_init_data init;
1150 struct clk *clk;
1151 const char *divider_name;
1152
1153 if (data->fixed_divider != 1) {
1154 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1155 "%s_prediv", data->name);
1156 if (!divider_name)
1157 return NULL;
1158 } else {
1159 divider_name = data->name;
1160 }
1161
1162 memset(&init, 0, sizeof(init));
1163
Martin Sperl3b15afe2016-02-29 12:51:42 +00001164 init.parent_names = &data->source_pll;
Eric Anholt41691b82015-10-08 18:37:24 -07001165 init.num_parents = 1;
1166 init.name = divider_name;
1167 init.ops = &bcm2835_pll_divider_clk_ops;
1168 init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
1169
1170 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1171 if (!divider)
1172 return NULL;
1173
1174 divider->div.reg = cprman->regs + data->a2w_reg;
1175 divider->div.shift = A2W_PLL_DIV_SHIFT;
1176 divider->div.width = A2W_PLL_DIV_BITS;
Eric Anholt79c1e2f2016-02-15 19:03:58 -08001177 divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
Eric Anholt41691b82015-10-08 18:37:24 -07001178 divider->div.lock = &cprman->regs_lock;
1179 divider->div.hw.init = &init;
1180 divider->div.table = NULL;
1181
1182 divider->cprman = cprman;
1183 divider->data = data;
1184
1185 clk = devm_clk_register(cprman->dev, &divider->div.hw);
1186 if (IS_ERR(clk))
1187 return clk;
1188
1189 /*
1190 * PLLH's channels have a fixed divide by 10 afterwards, which
1191 * is what our consumers are actually using.
1192 */
1193 if (data->fixed_divider != 1) {
1194 return clk_register_fixed_factor(cprman->dev, data->name,
1195 divider_name,
1196 CLK_SET_RATE_PARENT,
1197 1,
1198 data->fixed_divider);
1199 }
1200
1201 return clk;
1202}
1203
1204static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman,
1205 const struct bcm2835_clock_data *data)
1206{
1207 struct bcm2835_clock *clock;
1208 struct clk_init_data init;
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001209 const char *parents[1 << CM_SRC_BITS];
1210 size_t i;
Eric Anholt41691b82015-10-08 18:37:24 -07001211
1212 /*
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001213 * Replace our "xosc" references with the oscillator's
1214 * actual name.
Eric Anholt41691b82015-10-08 18:37:24 -07001215 */
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001216 for (i = 0; i < data->num_mux_parents; i++) {
1217 if (strcmp(data->parents[i], "xosc") == 0)
1218 parents[i] = cprman->osc_name;
1219 else
1220 parents[i] = data->parents[i];
Eric Anholt41691b82015-10-08 18:37:24 -07001221 }
1222
1223 memset(&init, 0, sizeof(init));
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001224 init.parent_names = parents;
1225 init.num_parents = data->num_mux_parents;
Eric Anholt41691b82015-10-08 18:37:24 -07001226 init.name = data->name;
1227 init.flags = CLK_IGNORE_UNUSED;
1228
1229 if (data->is_vpu_clock) {
1230 init.ops = &bcm2835_vpu_clock_clk_ops;
1231 } else {
1232 init.ops = &bcm2835_clock_clk_ops;
1233 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
1234 }
1235
1236 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1237 if (!clock)
1238 return NULL;
1239
1240 clock->cprman = cprman;
1241 clock->data = data;
1242 clock->hw.init = &init;
1243
1244 return devm_clk_register(cprman->dev, &clock->hw);
1245}
1246
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001247static struct clk *bcm2835_register_gate(struct bcm2835_cprman *cprman,
1248 const struct bcm2835_gate_data *data)
1249{
1250 return clk_register_gate(cprman->dev, data->name, data->parent,
1251 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1252 cprman->regs + data->ctl_reg,
1253 CM_GATE_BIT, 0, &cprman->regs_lock);
1254}
1255
1256typedef struct clk *(*bcm2835_clk_register)(struct bcm2835_cprman *cprman,
1257 const void *data);
1258struct bcm2835_clk_desc {
1259 bcm2835_clk_register clk_register;
1260 const void *data;
1261};
1262
Martin Sperl3b15afe2016-02-29 12:51:42 +00001263/* assignment helper macros for different clock types */
1264#define _REGISTER(f, ...) { .clk_register = (bcm2835_clk_register)f, \
1265 .data = __VA_ARGS__ }
1266#define REGISTER_PLL(...) _REGISTER(&bcm2835_register_pll, \
1267 &(struct bcm2835_pll_data) \
1268 {__VA_ARGS__})
1269#define REGISTER_PLL_DIV(...) _REGISTER(&bcm2835_register_pll_divider, \
1270 &(struct bcm2835_pll_divider_data) \
1271 {__VA_ARGS__})
1272#define REGISTER_CLK(...) _REGISTER(&bcm2835_register_clock, \
1273 &(struct bcm2835_clock_data) \
1274 {__VA_ARGS__})
1275#define REGISTER_GATE(...) _REGISTER(&bcm2835_register_gate, \
1276 &(struct bcm2835_gate_data) \
1277 {__VA_ARGS__})
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001278
Martin Sperl3b15afe2016-02-29 12:51:42 +00001279/* parent mux arrays plus helper macros */
1280
1281/* main oscillator parent mux */
1282static const char *const bcm2835_clock_osc_parents[] = {
1283 "gnd",
1284 "xosc",
1285 "testdebug0",
1286 "testdebug1"
1287};
1288
1289#define REGISTER_OSC_CLK(...) REGISTER_CLK( \
1290 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), \
1291 .parents = bcm2835_clock_osc_parents, \
1292 __VA_ARGS__)
1293
1294/* main peripherial parent mux */
1295static const char *const bcm2835_clock_per_parents[] = {
1296 "gnd",
1297 "xosc",
1298 "testdebug0",
1299 "testdebug1",
1300 "plla_per",
1301 "pllc_per",
1302 "plld_per",
1303 "pllh_aux",
1304};
1305
1306#define REGISTER_PER_CLK(...) REGISTER_CLK( \
1307 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), \
1308 .parents = bcm2835_clock_per_parents, \
1309 __VA_ARGS__)
1310
1311/* main vpu parent mux */
1312static const char *const bcm2835_clock_vpu_parents[] = {
1313 "gnd",
1314 "xosc",
1315 "testdebug0",
1316 "testdebug1",
1317 "plla_core",
1318 "pllc_core0",
1319 "plld_core",
1320 "pllh_aux",
1321 "pllc_core1",
1322 "pllc_core2",
1323};
1324
1325#define REGISTER_VPU_CLK(...) REGISTER_CLK( \
1326 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), \
1327 .parents = bcm2835_clock_vpu_parents, \
1328 __VA_ARGS__)
1329
1330/*
1331 * the real definition of all the pll, pll_dividers and clocks
1332 * these make use of the above REGISTER_* macros
1333 */
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001334static const struct bcm2835_clk_desc clk_desc_array[] = {
Martin Sperl3b15afe2016-02-29 12:51:42 +00001335 /* the PLL + PLL dividers */
1336
1337 /*
1338 * PLLA is the auxiliary PLL, used to drive the CCP2
1339 * (Compact Camera Port 2) transmitter clock.
1340 *
1341 * It is in the PX LDO power domain, which is on when the
1342 * AUDIO domain is on.
1343 */
1344 [BCM2835_PLLA] = REGISTER_PLL(
1345 .name = "plla",
1346 .cm_ctrl_reg = CM_PLLA,
1347 .a2w_ctrl_reg = A2W_PLLA_CTRL,
1348 .frac_reg = A2W_PLLA_FRAC,
1349 .ana_reg_base = A2W_PLLA_ANA0,
1350 .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
1351 .lock_mask = CM_LOCK_FLOCKA,
1352
1353 .ana = &bcm2835_ana_default,
1354
1355 .min_rate = 600000000u,
1356 .max_rate = 2400000000u,
1357 .max_fb_rate = BCM2835_MAX_FB_RATE),
1358 [BCM2835_PLLA_CORE] = REGISTER_PLL_DIV(
1359 .name = "plla_core",
1360 .source_pll = "plla",
1361 .cm_reg = CM_PLLA,
1362 .a2w_reg = A2W_PLLA_CORE,
1363 .load_mask = CM_PLLA_LOADCORE,
1364 .hold_mask = CM_PLLA_HOLDCORE,
1365 .fixed_divider = 1),
1366 [BCM2835_PLLA_PER] = REGISTER_PLL_DIV(
1367 .name = "plla_per",
1368 .source_pll = "plla",
1369 .cm_reg = CM_PLLA,
1370 .a2w_reg = A2W_PLLA_PER,
1371 .load_mask = CM_PLLA_LOADPER,
1372 .hold_mask = CM_PLLA_HOLDPER,
1373 .fixed_divider = 1),
1374
1375 /* PLLB is used for the ARM's clock. */
1376 [BCM2835_PLLB] = REGISTER_PLL(
1377 .name = "pllb",
1378 .cm_ctrl_reg = CM_PLLB,
1379 .a2w_ctrl_reg = A2W_PLLB_CTRL,
1380 .frac_reg = A2W_PLLB_FRAC,
1381 .ana_reg_base = A2W_PLLB_ANA0,
1382 .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
1383 .lock_mask = CM_LOCK_FLOCKB,
1384
1385 .ana = &bcm2835_ana_default,
1386
1387 .min_rate = 600000000u,
1388 .max_rate = 3000000000u,
1389 .max_fb_rate = BCM2835_MAX_FB_RATE),
1390 [BCM2835_PLLB_ARM] = REGISTER_PLL_DIV(
1391 .name = "pllb_arm",
1392 .source_pll = "pllb",
1393 .cm_reg = CM_PLLB,
1394 .a2w_reg = A2W_PLLB_ARM,
1395 .load_mask = CM_PLLB_LOADARM,
1396 .hold_mask = CM_PLLB_HOLDARM,
1397 .fixed_divider = 1),
1398
1399 /*
1400 * PLLC is the core PLL, used to drive the core VPU clock.
1401 *
1402 * It is in the PX LDO power domain, which is on when the
1403 * AUDIO domain is on.
1404 */
1405 [BCM2835_PLLC] = REGISTER_PLL(
1406 .name = "pllc",
1407 .cm_ctrl_reg = CM_PLLC,
1408 .a2w_ctrl_reg = A2W_PLLC_CTRL,
1409 .frac_reg = A2W_PLLC_FRAC,
1410 .ana_reg_base = A2W_PLLC_ANA0,
1411 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1412 .lock_mask = CM_LOCK_FLOCKC,
1413
1414 .ana = &bcm2835_ana_default,
1415
1416 .min_rate = 600000000u,
1417 .max_rate = 3000000000u,
1418 .max_fb_rate = BCM2835_MAX_FB_RATE),
1419 [BCM2835_PLLC_CORE0] = REGISTER_PLL_DIV(
1420 .name = "pllc_core0",
1421 .source_pll = "pllc",
1422 .cm_reg = CM_PLLC,
1423 .a2w_reg = A2W_PLLC_CORE0,
1424 .load_mask = CM_PLLC_LOADCORE0,
1425 .hold_mask = CM_PLLC_HOLDCORE0,
1426 .fixed_divider = 1),
1427 [BCM2835_PLLC_CORE1] = REGISTER_PLL_DIV(
1428 .name = "pllc_core1",
1429 .source_pll = "pllc",
1430 .cm_reg = CM_PLLC,
1431 .a2w_reg = A2W_PLLC_CORE1,
1432 .load_mask = CM_PLLC_LOADCORE1,
1433 .hold_mask = CM_PLLC_HOLDCORE1,
1434 .fixed_divider = 1),
1435 [BCM2835_PLLC_CORE2] = REGISTER_PLL_DIV(
1436 .name = "pllc_core2",
1437 .source_pll = "pllc",
1438 .cm_reg = CM_PLLC,
1439 .a2w_reg = A2W_PLLC_CORE2,
1440 .load_mask = CM_PLLC_LOADCORE2,
1441 .hold_mask = CM_PLLC_HOLDCORE2,
1442 .fixed_divider = 1),
1443 [BCM2835_PLLC_PER] = REGISTER_PLL_DIV(
1444 .name = "pllc_per",
1445 .source_pll = "pllc",
1446 .cm_reg = CM_PLLC,
1447 .a2w_reg = A2W_PLLC_PER,
1448 .load_mask = CM_PLLC_LOADPER,
1449 .hold_mask = CM_PLLC_HOLDPER,
1450 .fixed_divider = 1),
1451
1452 /*
1453 * PLLD is the display PLL, used to drive DSI display panels.
1454 *
1455 * It is in the PX LDO power domain, which is on when the
1456 * AUDIO domain is on.
1457 */
1458 [BCM2835_PLLD] = REGISTER_PLL(
1459 .name = "plld",
1460 .cm_ctrl_reg = CM_PLLD,
1461 .a2w_ctrl_reg = A2W_PLLD_CTRL,
1462 .frac_reg = A2W_PLLD_FRAC,
1463 .ana_reg_base = A2W_PLLD_ANA0,
1464 .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
1465 .lock_mask = CM_LOCK_FLOCKD,
1466
1467 .ana = &bcm2835_ana_default,
1468
1469 .min_rate = 600000000u,
1470 .max_rate = 2400000000u,
1471 .max_fb_rate = BCM2835_MAX_FB_RATE),
1472 [BCM2835_PLLD_CORE] = REGISTER_PLL_DIV(
1473 .name = "plld_core",
1474 .source_pll = "plld",
1475 .cm_reg = CM_PLLD,
1476 .a2w_reg = A2W_PLLD_CORE,
1477 .load_mask = CM_PLLD_LOADCORE,
1478 .hold_mask = CM_PLLD_HOLDCORE,
1479 .fixed_divider = 1),
1480 [BCM2835_PLLD_PER] = REGISTER_PLL_DIV(
1481 .name = "plld_per",
1482 .source_pll = "plld",
1483 .cm_reg = CM_PLLD,
1484 .a2w_reg = A2W_PLLD_PER,
1485 .load_mask = CM_PLLD_LOADPER,
1486 .hold_mask = CM_PLLD_HOLDPER,
1487 .fixed_divider = 1),
1488
1489 /*
1490 * PLLH is used to supply the pixel clock or the AUX clock for the
1491 * TV encoder.
1492 *
1493 * It is in the HDMI power domain.
1494 */
1495 [BCM2835_PLLH] = REGISTER_PLL(
1496 "pllh",
1497 .cm_ctrl_reg = CM_PLLH,
1498 .a2w_ctrl_reg = A2W_PLLH_CTRL,
1499 .frac_reg = A2W_PLLH_FRAC,
1500 .ana_reg_base = A2W_PLLH_ANA0,
1501 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1502 .lock_mask = CM_LOCK_FLOCKH,
1503
1504 .ana = &bcm2835_ana_pllh,
1505
1506 .min_rate = 600000000u,
1507 .max_rate = 3000000000u,
1508 .max_fb_rate = BCM2835_MAX_FB_RATE),
1509 [BCM2835_PLLH_RCAL] = REGISTER_PLL_DIV(
1510 .name = "pllh_rcal",
1511 .source_pll = "pllh",
1512 .cm_reg = CM_PLLH,
1513 .a2w_reg = A2W_PLLH_RCAL,
1514 .load_mask = CM_PLLH_LOADRCAL,
1515 .hold_mask = 0,
1516 .fixed_divider = 10),
1517 [BCM2835_PLLH_AUX] = REGISTER_PLL_DIV(
1518 .name = "pllh_aux",
1519 .source_pll = "pllh",
1520 .cm_reg = CM_PLLH,
1521 .a2w_reg = A2W_PLLH_AUX,
1522 .load_mask = CM_PLLH_LOADAUX,
1523 .hold_mask = 0,
1524 .fixed_divider = 10),
1525 [BCM2835_PLLH_PIX] = REGISTER_PLL_DIV(
1526 .name = "pllh_pix",
1527 .source_pll = "pllh",
1528 .cm_reg = CM_PLLH,
1529 .a2w_reg = A2W_PLLH_PIX,
1530 .load_mask = CM_PLLH_LOADPIX,
1531 .hold_mask = 0,
1532 .fixed_divider = 10),
1533
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001534 /* the clocks */
Martin Sperl3b15afe2016-02-29 12:51:42 +00001535
1536 /* clocks with oscillator parent mux */
1537
1538 /* One Time Programmable Memory clock. Maximum 10Mhz. */
1539 [BCM2835_CLOCK_OTP] = REGISTER_OSC_CLK(
1540 .name = "otp",
1541 .ctl_reg = CM_OTPCTL,
1542 .div_reg = CM_OTPDIV,
1543 .int_bits = 4,
1544 .frac_bits = 0),
1545 /*
1546 * Used for a 1Mhz clock for the system clocksource, and also used
1547 * bythe watchdog timer and the camera pulse generator.
1548 */
1549 [BCM2835_CLOCK_TIMER] = REGISTER_OSC_CLK(
1550 .name = "timer",
1551 .ctl_reg = CM_TIMERCTL,
1552 .div_reg = CM_TIMERDIV,
1553 .int_bits = 6,
1554 .frac_bits = 12),
1555 /*
1556 * Clock for the temperature sensor.
1557 * Generally run at 2Mhz, max 5Mhz.
1558 */
1559 [BCM2835_CLOCK_TSENS] = REGISTER_OSC_CLK(
1560 .name = "tsens",
1561 .ctl_reg = CM_TSENSCTL,
1562 .div_reg = CM_TSENSDIV,
1563 .int_bits = 5,
1564 .frac_bits = 0),
1565
1566 /* clocks with vpu parent mux */
1567 [BCM2835_CLOCK_H264] = REGISTER_VPU_CLK(
1568 .name = "h264",
1569 .ctl_reg = CM_H264CTL,
1570 .div_reg = CM_H264DIV,
1571 .int_bits = 4,
1572 .frac_bits = 8),
1573 [BCM2835_CLOCK_ISP] = REGISTER_VPU_CLK(
1574 .name = "isp",
1575 .ctl_reg = CM_ISPCTL,
1576 .div_reg = CM_ISPDIV,
1577 .int_bits = 4,
1578 .frac_bits = 8),
1579 /*
1580 * Secondary SDRAM clock. Used for low-voltage modes when the PLL
1581 * in the SDRAM controller can't be used.
1582 */
1583 [BCM2835_CLOCK_SDRAM] = REGISTER_VPU_CLK(
1584 .name = "sdram",
1585 .ctl_reg = CM_SDCCTL,
1586 .div_reg = CM_SDCDIV,
1587 .int_bits = 6,
1588 .frac_bits = 0),
1589 [BCM2835_CLOCK_V3D] = REGISTER_VPU_CLK(
1590 .name = "v3d",
1591 .ctl_reg = CM_V3DCTL,
1592 .div_reg = CM_V3DDIV,
1593 .int_bits = 4,
1594 .frac_bits = 8),
1595 /*
1596 * VPU clock. This doesn't have an enable bit, since it drives
1597 * the bus for everything else, and is special so it doesn't need
1598 * to be gated for rate changes. It is also known as "clk_audio"
1599 * in various hardware documentation.
1600 */
1601 [BCM2835_CLOCK_VPU] = REGISTER_VPU_CLK(
1602 .name = "vpu",
1603 .ctl_reg = CM_VPUCTL,
1604 .div_reg = CM_VPUDIV,
1605 .int_bits = 12,
1606 .frac_bits = 8,
1607 .is_vpu_clock = true),
1608
1609 /* clocks with per parent mux */
1610
1611 /* Arasan EMMC clock */
1612 [BCM2835_CLOCK_EMMC] = REGISTER_PER_CLK(
1613 .name = "emmc",
1614 .ctl_reg = CM_EMMCCTL,
1615 .div_reg = CM_EMMCDIV,
1616 .int_bits = 4,
1617 .frac_bits = 8),
1618 /* HDMI state machine */
1619 [BCM2835_CLOCK_HSM] = REGISTER_PER_CLK(
1620 .name = "hsm",
1621 .ctl_reg = CM_HSMCTL,
1622 .div_reg = CM_HSMDIV,
1623 .int_bits = 4,
1624 .frac_bits = 8),
Martin Sperl33b68962016-02-29 12:51:43 +00001625 [BCM2835_CLOCK_PCM] = REGISTER_PER_CLK(
1626 .name = "pcm",
1627 .ctl_reg = CM_PCMCTL,
1628 .div_reg = CM_PCMDIV,
1629 .int_bits = 12,
1630 .frac_bits = 12,
1631 .is_mash_clock = true),
Martin Sperl3b15afe2016-02-29 12:51:42 +00001632 [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK(
1633 .name = "pwm",
1634 .ctl_reg = CM_PWMCTL,
1635 .div_reg = CM_PWMDIV,
1636 .int_bits = 12,
1637 .frac_bits = 12,
1638 .is_mash_clock = true),
1639 [BCM2835_CLOCK_UART] = REGISTER_PER_CLK(
1640 .name = "uart",
1641 .ctl_reg = CM_UARTCTL,
1642 .div_reg = CM_UARTDIV,
1643 .int_bits = 10,
1644 .frac_bits = 12),
1645 /* TV encoder clock. Only operating frequency is 108Mhz. */
1646 [BCM2835_CLOCK_VEC] = REGISTER_PER_CLK(
1647 .name = "vec",
1648 .ctl_reg = CM_VECCTL,
1649 .div_reg = CM_VECDIV,
1650 .int_bits = 4,
1651 .frac_bits = 0),
1652
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001653 /* the gates */
Martin Sperl3b15afe2016-02-29 12:51:42 +00001654
1655 /*
1656 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
1657 * you have the debug bit set in the power manager, which we
1658 * don't bother exposing) are individual gates off of the
1659 * non-stop vpu clock.
1660 */
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001661 [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE(
Martin Sperl3b15afe2016-02-29 12:51:42 +00001662 .name = "peri_image",
1663 .parent = "vpu",
1664 .ctl_reg = CM_PERIICTL),
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001665};
1666
Eric Anholt41691b82015-10-08 18:37:24 -07001667static int bcm2835_clk_probe(struct platform_device *pdev)
1668{
1669 struct device *dev = &pdev->dev;
1670 struct clk **clks;
1671 struct bcm2835_cprman *cprman;
1672 struct resource *res;
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001673 const struct bcm2835_clk_desc *desc;
1674 const size_t asize = ARRAY_SIZE(clk_desc_array);
1675 size_t i;
Eric Anholt41691b82015-10-08 18:37:24 -07001676
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001677 cprman = devm_kzalloc(dev,
1678 sizeof(*cprman) + asize * sizeof(*clks),
1679 GFP_KERNEL);
Eric Anholt41691b82015-10-08 18:37:24 -07001680 if (!cprman)
1681 return -ENOMEM;
1682
1683 spin_lock_init(&cprman->regs_lock);
1684 cprman->dev = dev;
1685 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1686 cprman->regs = devm_ioremap_resource(dev, res);
1687 if (IS_ERR(cprman->regs))
1688 return PTR_ERR(cprman->regs);
1689
1690 cprman->osc_name = of_clk_get_parent_name(dev->of_node, 0);
1691 if (!cprman->osc_name)
1692 return -ENODEV;
1693
1694 platform_set_drvdata(pdev, cprman);
1695
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001696 cprman->onecell.clk_num = asize;
Eric Anholt41691b82015-10-08 18:37:24 -07001697 cprman->onecell.clks = cprman->clks;
1698 clks = cprman->clks;
1699
Martin Sperl56eb3a2e2016-02-29 12:51:41 +00001700 for (i = 0; i < asize; i++) {
1701 desc = &clk_desc_array[i];
1702 if (desc->clk_register && desc->data)
1703 clks[i] = desc->clk_register(cprman, desc->data);
1704 }
Remi Pommarelcfbab8f2015-12-06 17:22:48 +01001705
Eric Anholt41691b82015-10-08 18:37:24 -07001706 return of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1707 &cprman->onecell);
1708}
1709
1710static const struct of_device_id bcm2835_clk_of_match[] = {
1711 { .compatible = "brcm,bcm2835-cprman", },
1712 {}
1713};
1714MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
1715
1716static struct platform_driver bcm2835_clk_driver = {
1717 .driver = {
1718 .name = "bcm2835-clk",
1719 .of_match_table = bcm2835_clk_of_match,
1720 },
1721 .probe = bcm2835_clk_probe,
1722};
1723
1724builtin_platform_driver(bcm2835_clk_driver);
1725
1726MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
1727MODULE_DESCRIPTION("BCM2835 clock driver");
1728MODULE_LICENSE("GPL v2");