blob: 5747a9d17c740f15a348eaf2adf3692d1ce43e84 [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 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Eric Anholt41691b82015-10-08 18:37:24 -070020/**
21 * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
22 *
23 * The clock tree on the 2835 has several levels. There's a root
24 * oscillator running at 19.2Mhz. After the oscillator there are 5
25 * PLLs, roughly divided as "camera", "ARM", "core", "DSI displays",
26 * and "HDMI displays". Those 5 PLLs each can divide their output to
27 * produce up to 4 channels. Finally, there is the level of clocks to
28 * be consumed by other hardware components (like "H264" or "HDMI
29 * state machine"), which divide off of some subset of the PLL
30 * channels.
31 *
32 * All of the clocks in the tree are exposed in the DT, because the DT
33 * may want to make assignments of the final layer of clocks to the
34 * PLL channels, and some components of the hardware will actually
35 * skip layers of the tree (for example, the pixel clock comes
36 * directly from the PLLH PIX channel without using a CM_*CTL clock
37 * generator).
38 */
39
Simon Arlott75fabc32012-09-10 23:26:15 -060040#include <linux/clk-provider.h>
41#include <linux/clkdev.h>
42#include <linux/clk/bcm2835.h>
Eric Anholt41691b82015-10-08 18:37:24 -070043#include <linux/module.h>
Stephen Warren526d2392012-12-24 21:55:01 -070044#include <linux/of.h>
Eric Anholt41691b82015-10-08 18:37:24 -070045#include <linux/platform_device.h>
46#include <linux/slab.h>
47#include <dt-bindings/clock/bcm2835.h>
48
49#define CM_PASSWORD 0x5a000000
50
51#define CM_GNRICCTL 0x000
52#define CM_GNRICDIV 0x004
53# define CM_DIV_FRAC_BITS 12
54
55#define CM_VPUCTL 0x008
56#define CM_VPUDIV 0x00c
57#define CM_SYSCTL 0x010
58#define CM_SYSDIV 0x014
59#define CM_PERIACTL 0x018
60#define CM_PERIADIV 0x01c
61#define CM_PERIICTL 0x020
62#define CM_PERIIDIV 0x024
63#define CM_H264CTL 0x028
64#define CM_H264DIV 0x02c
65#define CM_ISPCTL 0x030
66#define CM_ISPDIV 0x034
67#define CM_V3DCTL 0x038
68#define CM_V3DDIV 0x03c
69#define CM_CAM0CTL 0x040
70#define CM_CAM0DIV 0x044
71#define CM_CAM1CTL 0x048
72#define CM_CAM1DIV 0x04c
73#define CM_CCP2CTL 0x050
74#define CM_CCP2DIV 0x054
75#define CM_DSI0ECTL 0x058
76#define CM_DSI0EDIV 0x05c
77#define CM_DSI0PCTL 0x060
78#define CM_DSI0PDIV 0x064
79#define CM_DPICTL 0x068
80#define CM_DPIDIV 0x06c
81#define CM_GP0CTL 0x070
82#define CM_GP0DIV 0x074
83#define CM_GP1CTL 0x078
84#define CM_GP1DIV 0x07c
85#define CM_GP2CTL 0x080
86#define CM_GP2DIV 0x084
87#define CM_HSMCTL 0x088
88#define CM_HSMDIV 0x08c
89#define CM_OTPCTL 0x090
90#define CM_OTPDIV 0x094
Martin Sperl2103a212015-12-22 20:13:08 +000091#define CM_PCMCTL 0x098
92#define CM_PCMDIV 0x09c
Eric Anholt41691b82015-10-08 18:37:24 -070093#define CM_PWMCTL 0x0a0
94#define CM_PWMDIV 0x0a4
Martin Sperl2103a212015-12-22 20:13:08 +000095#define CM_SLIMCTL 0x0a8
96#define CM_SLIMDIV 0x0ac
Eric Anholt41691b82015-10-08 18:37:24 -070097#define CM_SMICTL 0x0b0
98#define CM_SMIDIV 0x0b4
Martin Sperl2103a212015-12-22 20:13:08 +000099/* no definition for 0x0b8 and 0x0bc */
100#define CM_TCNTCTL 0x0c0
101#define CM_TCNTDIV 0x0c4
102#define CM_TECCTL 0x0c8
103#define CM_TECDIV 0x0cc
104#define CM_TD0CTL 0x0d0
105#define CM_TD0DIV 0x0d4
106#define CM_TD1CTL 0x0d8
107#define CM_TD1DIV 0x0dc
Eric Anholt41691b82015-10-08 18:37:24 -0700108#define CM_TSENSCTL 0x0e0
109#define CM_TSENSDIV 0x0e4
110#define CM_TIMERCTL 0x0e8
111#define CM_TIMERDIV 0x0ec
112#define CM_UARTCTL 0x0f0
113#define CM_UARTDIV 0x0f4
114#define CM_VECCTL 0x0f8
115#define CM_VECDIV 0x0fc
116#define CM_PULSECTL 0x190
117#define CM_PULSEDIV 0x194
118#define CM_SDCCTL 0x1a8
119#define CM_SDCDIV 0x1ac
120#define CM_ARMCTL 0x1b0
121#define CM_EMMCCTL 0x1c0
122#define CM_EMMCDIV 0x1c4
123
124/* General bits for the CM_*CTL regs */
125# define CM_ENABLE BIT(4)
126# define CM_KILL BIT(5)
127# define CM_GATE_BIT 6
128# define CM_GATE BIT(CM_GATE_BIT)
129# define CM_BUSY BIT(7)
130# define CM_BUSYD BIT(8)
131# 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;
300 spinlock_t regs_lock;
301 const char *osc_name;
302
303 struct clk_onecell_data onecell;
304 struct clk *clks[BCM2835_CLOCK_COUNT];
305};
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
Simon Arlott75fabc32012-09-10 23:26:15 -0600317/*
318 * These are fixed clocks. They're probably not all root clocks and it may
319 * be possible to turn them on and off but until this is mapped out better
320 * it's the only way they can be used.
321 */
322void __init bcm2835_init_clocks(void)
323{
324 struct clk *clk;
325 int ret;
326
Simon Arlott75fabc32012-09-10 23:26:15 -0600327 clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, CLK_IS_ROOT,
328 126000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800329 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600330 pr_err("apb_pclk not registered\n");
331
332 clk = clk_register_fixed_rate(NULL, "uart0_pclk", NULL, CLK_IS_ROOT,
333 3000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800334 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600335 pr_err("uart0_pclk not registered\n");
336 ret = clk_register_clkdev(clk, NULL, "20201000.uart");
337 if (ret)
338 pr_err("uart0_pclk alias not registered\n");
339
340 clk = clk_register_fixed_rate(NULL, "uart1_pclk", NULL, CLK_IS_ROOT,
341 125000000);
Wei Yongjun0de9f232012-10-09 10:46:00 +0800342 if (IS_ERR(clk))
Simon Arlott75fabc32012-09-10 23:26:15 -0600343 pr_err("uart1_pclk not registered\n");
344 ret = clk_register_clkdev(clk, NULL, "20215000.uart");
345 if (ret)
Domenico Andreoli686ea582012-10-20 03:35:28 +0200346 pr_err("uart1_pclk alias not registered\n");
Simon Arlott75fabc32012-09-10 23:26:15 -0600347}
Eric Anholt41691b82015-10-08 18:37:24 -0700348
349struct bcm2835_pll_data {
350 const char *name;
351 u32 cm_ctrl_reg;
352 u32 a2w_ctrl_reg;
353 u32 frac_reg;
354 u32 ana_reg_base;
355 u32 reference_enable_mask;
356 /* Bit in CM_LOCK to indicate when the PLL has locked. */
357 u32 lock_mask;
358
359 const struct bcm2835_pll_ana_bits *ana;
360
361 unsigned long min_rate;
362 unsigned long max_rate;
363 /*
364 * Highest rate for the VCO before we have to use the
365 * pre-divide-by-2.
366 */
367 unsigned long max_fb_rate;
368};
369
370struct bcm2835_pll_ana_bits {
371 u32 mask0;
372 u32 set0;
373 u32 mask1;
374 u32 set1;
375 u32 mask3;
376 u32 set3;
377 u32 fb_prediv_mask;
378};
379
380static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
381 .mask0 = 0,
382 .set0 = 0,
383 .mask1 = ~(A2W_PLL_KI_MASK | A2W_PLL_KP_MASK),
384 .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
385 .mask3 = ~A2W_PLL_KA_MASK,
386 .set3 = (2 << A2W_PLL_KA_SHIFT),
387 .fb_prediv_mask = BIT(14),
388};
389
390static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
391 .mask0 = ~(A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK),
392 .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
393 .mask1 = ~(A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK),
394 .set1 = (6 << A2W_PLLH_KP_SHIFT),
395 .mask3 = 0,
396 .set3 = 0,
397 .fb_prediv_mask = BIT(11),
398};
399
400/*
401 * PLLA is the auxiliary PLL, used to drive the CCP2 (Compact Camera
402 * Port 2) transmitter clock.
403 *
404 * It is in the PX LDO power domain, which is on when the AUDIO domain
405 * is on.
406 */
407static const struct bcm2835_pll_data bcm2835_plla_data = {
408 .name = "plla",
409 .cm_ctrl_reg = CM_PLLA,
410 .a2w_ctrl_reg = A2W_PLLA_CTRL,
411 .frac_reg = A2W_PLLA_FRAC,
412 .ana_reg_base = A2W_PLLA_ANA0,
413 .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
414 .lock_mask = CM_LOCK_FLOCKA,
415
416 .ana = &bcm2835_ana_default,
417
418 .min_rate = 600000000u,
419 .max_rate = 2400000000u,
420 .max_fb_rate = BCM2835_MAX_FB_RATE,
421};
422
423/* PLLB is used for the ARM's clock. */
424static const struct bcm2835_pll_data bcm2835_pllb_data = {
425 .name = "pllb",
426 .cm_ctrl_reg = CM_PLLB,
427 .a2w_ctrl_reg = A2W_PLLB_CTRL,
428 .frac_reg = A2W_PLLB_FRAC,
429 .ana_reg_base = A2W_PLLB_ANA0,
430 .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
431 .lock_mask = CM_LOCK_FLOCKB,
432
433 .ana = &bcm2835_ana_default,
434
435 .min_rate = 600000000u,
436 .max_rate = 3000000000u,
437 .max_fb_rate = BCM2835_MAX_FB_RATE,
438};
439
440/*
441 * PLLC is the core PLL, used to drive the core VPU clock.
442 *
443 * It is in the PX LDO power domain, which is on when the AUDIO domain
444 * is on.
445*/
446static const struct bcm2835_pll_data bcm2835_pllc_data = {
447 .name = "pllc",
448 .cm_ctrl_reg = CM_PLLC,
449 .a2w_ctrl_reg = A2W_PLLC_CTRL,
450 .frac_reg = A2W_PLLC_FRAC,
451 .ana_reg_base = A2W_PLLC_ANA0,
452 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
453 .lock_mask = CM_LOCK_FLOCKC,
454
455 .ana = &bcm2835_ana_default,
456
457 .min_rate = 600000000u,
458 .max_rate = 3000000000u,
459 .max_fb_rate = BCM2835_MAX_FB_RATE,
460};
461
462/*
463 * PLLD is the display PLL, used to drive DSI display panels.
464 *
465 * It is in the PX LDO power domain, which is on when the AUDIO domain
466 * is on.
467 */
468static const struct bcm2835_pll_data bcm2835_plld_data = {
469 .name = "plld",
470 .cm_ctrl_reg = CM_PLLD,
471 .a2w_ctrl_reg = A2W_PLLD_CTRL,
472 .frac_reg = A2W_PLLD_FRAC,
473 .ana_reg_base = A2W_PLLD_ANA0,
474 .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
475 .lock_mask = CM_LOCK_FLOCKD,
476
477 .ana = &bcm2835_ana_default,
478
479 .min_rate = 600000000u,
480 .max_rate = 2400000000u,
481 .max_fb_rate = BCM2835_MAX_FB_RATE,
482};
483
484/*
485 * PLLH is used to supply the pixel clock or the AUX clock for the TV
486 * encoder.
487 *
488 * It is in the HDMI power domain.
489 */
490static const struct bcm2835_pll_data bcm2835_pllh_data = {
491 "pllh",
492 .cm_ctrl_reg = CM_PLLH,
493 .a2w_ctrl_reg = A2W_PLLH_CTRL,
494 .frac_reg = A2W_PLLH_FRAC,
495 .ana_reg_base = A2W_PLLH_ANA0,
496 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
497 .lock_mask = CM_LOCK_FLOCKH,
498
499 .ana = &bcm2835_ana_pllh,
500
501 .min_rate = 600000000u,
502 .max_rate = 3000000000u,
503 .max_fb_rate = BCM2835_MAX_FB_RATE,
504};
505
506struct bcm2835_pll_divider_data {
507 const char *name;
508 const struct bcm2835_pll_data *source_pll;
509 u32 cm_reg;
510 u32 a2w_reg;
511
512 u32 load_mask;
513 u32 hold_mask;
514 u32 fixed_divider;
515};
516
517static const struct bcm2835_pll_divider_data bcm2835_plla_core_data = {
518 .name = "plla_core",
519 .source_pll = &bcm2835_plla_data,
520 .cm_reg = CM_PLLA,
521 .a2w_reg = A2W_PLLA_CORE,
522 .load_mask = CM_PLLA_LOADCORE,
523 .hold_mask = CM_PLLA_HOLDCORE,
524 .fixed_divider = 1,
525};
526
527static const struct bcm2835_pll_divider_data bcm2835_plla_per_data = {
528 .name = "plla_per",
529 .source_pll = &bcm2835_plla_data,
530 .cm_reg = CM_PLLA,
531 .a2w_reg = A2W_PLLA_PER,
532 .load_mask = CM_PLLA_LOADPER,
533 .hold_mask = CM_PLLA_HOLDPER,
534 .fixed_divider = 1,
535};
536
537static const struct bcm2835_pll_divider_data bcm2835_pllb_arm_data = {
538 .name = "pllb_arm",
539 .source_pll = &bcm2835_pllb_data,
540 .cm_reg = CM_PLLB,
541 .a2w_reg = A2W_PLLB_ARM,
542 .load_mask = CM_PLLB_LOADARM,
543 .hold_mask = CM_PLLB_HOLDARM,
544 .fixed_divider = 1,
545};
546
547static const struct bcm2835_pll_divider_data bcm2835_pllc_core0_data = {
548 .name = "pllc_core0",
549 .source_pll = &bcm2835_pllc_data,
550 .cm_reg = CM_PLLC,
551 .a2w_reg = A2W_PLLC_CORE0,
552 .load_mask = CM_PLLC_LOADCORE0,
553 .hold_mask = CM_PLLC_HOLDCORE0,
554 .fixed_divider = 1,
555};
556
557static const struct bcm2835_pll_divider_data bcm2835_pllc_core1_data = {
558 .name = "pllc_core1", .source_pll = &bcm2835_pllc_data,
559 .cm_reg = CM_PLLC, A2W_PLLC_CORE1,
560 .load_mask = CM_PLLC_LOADCORE1,
561 .hold_mask = CM_PLLC_HOLDCORE1,
562 .fixed_divider = 1,
563};
564
565static const struct bcm2835_pll_divider_data bcm2835_pllc_core2_data = {
566 .name = "pllc_core2",
567 .source_pll = &bcm2835_pllc_data,
568 .cm_reg = CM_PLLC,
569 .a2w_reg = A2W_PLLC_CORE2,
570 .load_mask = CM_PLLC_LOADCORE2,
571 .hold_mask = CM_PLLC_HOLDCORE2,
572 .fixed_divider = 1,
573};
574
575static const struct bcm2835_pll_divider_data bcm2835_pllc_per_data = {
576 .name = "pllc_per",
577 .source_pll = &bcm2835_pllc_data,
578 .cm_reg = CM_PLLC,
579 .a2w_reg = A2W_PLLC_PER,
580 .load_mask = CM_PLLC_LOADPER,
581 .hold_mask = CM_PLLC_HOLDPER,
582 .fixed_divider = 1,
583};
584
585static const struct bcm2835_pll_divider_data bcm2835_plld_core_data = {
586 .name = "plld_core",
587 .source_pll = &bcm2835_plld_data,
588 .cm_reg = CM_PLLD,
589 .a2w_reg = A2W_PLLD_CORE,
590 .load_mask = CM_PLLD_LOADCORE,
591 .hold_mask = CM_PLLD_HOLDCORE,
592 .fixed_divider = 1,
593};
594
595static const struct bcm2835_pll_divider_data bcm2835_plld_per_data = {
596 .name = "plld_per",
597 .source_pll = &bcm2835_plld_data,
598 .cm_reg = CM_PLLD,
599 .a2w_reg = A2W_PLLD_PER,
600 .load_mask = CM_PLLD_LOADPER,
601 .hold_mask = CM_PLLD_HOLDPER,
602 .fixed_divider = 1,
603};
604
605static const struct bcm2835_pll_divider_data bcm2835_pllh_rcal_data = {
606 .name = "pllh_rcal",
607 .source_pll = &bcm2835_pllh_data,
608 .cm_reg = CM_PLLH,
609 .a2w_reg = A2W_PLLH_RCAL,
610 .load_mask = CM_PLLH_LOADRCAL,
611 .hold_mask = 0,
612 .fixed_divider = 10,
613};
614
615static const struct bcm2835_pll_divider_data bcm2835_pllh_aux_data = {
616 .name = "pllh_aux",
617 .source_pll = &bcm2835_pllh_data,
618 .cm_reg = CM_PLLH,
619 .a2w_reg = A2W_PLLH_AUX,
620 .load_mask = CM_PLLH_LOADAUX,
621 .hold_mask = 0,
622 .fixed_divider = 10,
623};
624
625static const struct bcm2835_pll_divider_data bcm2835_pllh_pix_data = {
626 .name = "pllh_pix",
627 .source_pll = &bcm2835_pllh_data,
628 .cm_reg = CM_PLLH,
629 .a2w_reg = A2W_PLLH_PIX,
630 .load_mask = CM_PLLH_LOADPIX,
631 .hold_mask = 0,
632 .fixed_divider = 10,
633};
634
635struct bcm2835_clock_data {
636 const char *name;
637
638 const char *const *parents;
639 int num_mux_parents;
640
641 u32 ctl_reg;
642 u32 div_reg;
643
644 /* Number of integer bits in the divider */
645 u32 int_bits;
646 /* Number of fractional bits in the divider */
647 u32 frac_bits;
648
649 bool is_vpu_clock;
650};
651
652static const char *const bcm2835_clock_per_parents[] = {
653 "gnd",
654 "xosc",
655 "testdebug0",
656 "testdebug1",
657 "plla_per",
658 "pllc_per",
659 "plld_per",
660 "pllh_aux",
661};
662
663static const char *const bcm2835_clock_vpu_parents[] = {
664 "gnd",
665 "xosc",
666 "testdebug0",
667 "testdebug1",
668 "plla_core",
669 "pllc_core0",
670 "plld_core",
671 "pllh_aux",
672 "pllc_core1",
673 "pllc_core2",
674};
675
676static const char *const bcm2835_clock_osc_parents[] = {
677 "gnd",
678 "xosc",
679 "testdebug0",
680 "testdebug1"
681};
682
683/*
684 * Used for a 1Mhz clock for the system clocksource, and also used by
685 * the watchdog timer and the camera pulse generator.
686 */
687static const struct bcm2835_clock_data bcm2835_clock_timer_data = {
688 .name = "timer",
689 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
690 .parents = bcm2835_clock_osc_parents,
691 .ctl_reg = CM_TIMERCTL,
692 .div_reg = CM_TIMERDIV,
693 .int_bits = 6,
694 .frac_bits = 12,
695};
696
697/* One Time Programmable Memory clock. Maximum 10Mhz. */
698static const struct bcm2835_clock_data bcm2835_clock_otp_data = {
699 .name = "otp",
700 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
701 .parents = bcm2835_clock_osc_parents,
702 .ctl_reg = CM_OTPCTL,
703 .div_reg = CM_OTPDIV,
704 .int_bits = 4,
705 .frac_bits = 0,
706};
707
708/*
709 * VPU clock. This doesn't have an enable bit, since it drives the
710 * bus for everything else, and is special so it doesn't need to be
711 * gated for rate changes. It is also known as "clk_audio" in various
712 * hardware documentation.
713 */
714static const struct bcm2835_clock_data bcm2835_clock_vpu_data = {
715 .name = "vpu",
716 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
717 .parents = bcm2835_clock_vpu_parents,
718 .ctl_reg = CM_VPUCTL,
719 .div_reg = CM_VPUDIV,
720 .int_bits = 12,
721 .frac_bits = 8,
722 .is_vpu_clock = true,
723};
724
725static const struct bcm2835_clock_data bcm2835_clock_v3d_data = {
726 .name = "v3d",
727 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
728 .parents = bcm2835_clock_vpu_parents,
729 .ctl_reg = CM_V3DCTL,
730 .div_reg = CM_V3DDIV,
731 .int_bits = 4,
732 .frac_bits = 8,
733};
734
735static const struct bcm2835_clock_data bcm2835_clock_isp_data = {
736 .name = "isp",
737 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
738 .parents = bcm2835_clock_vpu_parents,
739 .ctl_reg = CM_ISPCTL,
740 .div_reg = CM_ISPDIV,
741 .int_bits = 4,
742 .frac_bits = 8,
743};
744
745static const struct bcm2835_clock_data bcm2835_clock_h264_data = {
746 .name = "h264",
747 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
748 .parents = bcm2835_clock_vpu_parents,
749 .ctl_reg = CM_H264CTL,
750 .div_reg = CM_H264DIV,
751 .int_bits = 4,
752 .frac_bits = 8,
753};
754
755/* TV encoder clock. Only operating frequency is 108Mhz. */
756static const struct bcm2835_clock_data bcm2835_clock_vec_data = {
757 .name = "vec",
758 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
759 .parents = bcm2835_clock_per_parents,
760 .ctl_reg = CM_VECCTL,
761 .div_reg = CM_VECDIV,
762 .int_bits = 4,
763 .frac_bits = 0,
764};
765
766static const struct bcm2835_clock_data bcm2835_clock_uart_data = {
767 .name = "uart",
768 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
769 .parents = bcm2835_clock_per_parents,
770 .ctl_reg = CM_UARTCTL,
771 .div_reg = CM_UARTDIV,
772 .int_bits = 10,
773 .frac_bits = 12,
774};
775
776/* HDMI state machine */
777static const struct bcm2835_clock_data bcm2835_clock_hsm_data = {
778 .name = "hsm",
779 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
780 .parents = bcm2835_clock_per_parents,
781 .ctl_reg = CM_HSMCTL,
782 .div_reg = CM_HSMDIV,
783 .int_bits = 4,
784 .frac_bits = 8,
785};
786
787/*
788 * Secondary SDRAM clock. Used for low-voltage modes when the PLL in
789 * the SDRAM controller can't be used.
790 */
791static const struct bcm2835_clock_data bcm2835_clock_sdram_data = {
792 .name = "sdram",
793 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),
794 .parents = bcm2835_clock_vpu_parents,
795 .ctl_reg = CM_SDCCTL,
796 .div_reg = CM_SDCDIV,
797 .int_bits = 6,
798 .frac_bits = 0,
799};
800
801/* Clock for the temperature sensor. Generally run at 2Mhz, max 5Mhz. */
802static const struct bcm2835_clock_data bcm2835_clock_tsens_data = {
803 .name = "tsens",
804 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),
805 .parents = bcm2835_clock_osc_parents,
806 .ctl_reg = CM_TSENSCTL,
807 .div_reg = CM_TSENSDIV,
808 .int_bits = 5,
809 .frac_bits = 0,
810};
811
812/* Arasan EMMC clock */
813static const struct bcm2835_clock_data bcm2835_clock_emmc_data = {
814 .name = "emmc",
815 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
816 .parents = bcm2835_clock_per_parents,
817 .ctl_reg = CM_EMMCCTL,
818 .div_reg = CM_EMMCDIV,
819 .int_bits = 4,
820 .frac_bits = 8,
821};
822
Remi Pommarelcfbab8f2015-12-06 17:22:48 +0100823static const struct bcm2835_clock_data bcm2835_clock_pwm_data = {
824 .name = "pwm",
825 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),
826 .parents = bcm2835_clock_per_parents,
827 .ctl_reg = CM_PWMCTL,
828 .div_reg = CM_PWMDIV,
829 .int_bits = 12,
830 .frac_bits = 12,
831};
832
Eric Anholt41691b82015-10-08 18:37:24 -0700833struct bcm2835_pll {
834 struct clk_hw hw;
835 struct bcm2835_cprman *cprman;
836 const struct bcm2835_pll_data *data;
837};
838
839static int bcm2835_pll_is_on(struct clk_hw *hw)
840{
841 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
842 struct bcm2835_cprman *cprman = pll->cprman;
843 const struct bcm2835_pll_data *data = pll->data;
844
845 return cprman_read(cprman, data->a2w_ctrl_reg) &
846 A2W_PLL_CTRL_PRST_DISABLE;
847}
848
849static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
850 unsigned long parent_rate,
851 u32 *ndiv, u32 *fdiv)
852{
853 u64 div;
854
855 div = (u64)rate << A2W_PLL_FRAC_BITS;
856 do_div(div, parent_rate);
857
858 *ndiv = div >> A2W_PLL_FRAC_BITS;
859 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
860}
861
862static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
863 u32 ndiv, u32 fdiv, u32 pdiv)
864{
865 u64 rate;
866
867 if (pdiv == 0)
868 return 0;
869
870 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
871 do_div(rate, pdiv);
872 return rate >> A2W_PLL_FRAC_BITS;
873}
874
875static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
876 unsigned long *parent_rate)
877{
878 u32 ndiv, fdiv;
879
880 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
881
882 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
883}
884
885static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
886 unsigned long parent_rate)
887{
888 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
889 struct bcm2835_cprman *cprman = pll->cprman;
890 const struct bcm2835_pll_data *data = pll->data;
891 u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
892 u32 ndiv, pdiv, fdiv;
893 bool using_prediv;
894
895 if (parent_rate == 0)
896 return 0;
897
898 fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
899 ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
900 pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
901 using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
902 data->ana->fb_prediv_mask;
903
904 if (using_prediv)
905 ndiv *= 2;
906
907 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
908}
909
910static void bcm2835_pll_off(struct clk_hw *hw)
911{
912 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
913 struct bcm2835_cprman *cprman = pll->cprman;
914 const struct bcm2835_pll_data *data = pll->data;
915
916 cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);
917 cprman_write(cprman, data->a2w_ctrl_reg, A2W_PLL_CTRL_PWRDN);
918}
919
920static int bcm2835_pll_on(struct clk_hw *hw)
921{
922 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
923 struct bcm2835_cprman *cprman = pll->cprman;
924 const struct bcm2835_pll_data *data = pll->data;
925 ktime_t timeout;
926
927 /* Take the PLL out of reset. */
928 cprman_write(cprman, data->cm_ctrl_reg,
929 cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
930
931 /* Wait for the PLL to lock. */
932 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
933 while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
934 if (ktime_after(ktime_get(), timeout)) {
935 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
936 clk_hw_get_name(hw));
937 return -ETIMEDOUT;
938 }
939
940 cpu_relax();
941 }
942
943 return 0;
944}
945
946static void
947bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
948{
949 int i;
950
951 /*
952 * ANA register setup is done as a series of writes to
953 * ANA3-ANA0, in that order. This lets us write all 4
954 * registers as a single cycle of the serdes interface (taking
955 * 100 xosc clocks), whereas if we were to update ana0, 1, and
956 * 3 individually through their partial-write registers, each
957 * would be their own serdes cycle.
958 */
959 for (i = 3; i >= 0; i--)
960 cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
961}
962
963static int bcm2835_pll_set_rate(struct clk_hw *hw,
964 unsigned long rate, unsigned long parent_rate)
965{
966 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
967 struct bcm2835_cprman *cprman = pll->cprman;
968 const struct bcm2835_pll_data *data = pll->data;
969 bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
970 u32 ndiv, fdiv, a2w_ctl;
971 u32 ana[4];
972 int i;
973
974 if (rate < data->min_rate || rate > data->max_rate) {
975 dev_err(cprman->dev, "%s: rate out of spec: %lu vs (%lu, %lu)\n",
976 clk_hw_get_name(hw), rate,
977 data->min_rate, data->max_rate);
978 return -EINVAL;
979 }
980
981 if (rate > data->max_fb_rate) {
982 use_fb_prediv = true;
983 rate /= 2;
984 } else {
985 use_fb_prediv = false;
986 }
987
988 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
989
990 for (i = 3; i >= 0; i--)
991 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
992
993 was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
994
995 ana[0] &= ~data->ana->mask0;
996 ana[0] |= data->ana->set0;
997 ana[1] &= ~data->ana->mask1;
998 ana[1] |= data->ana->set1;
999 ana[3] &= ~data->ana->mask3;
1000 ana[3] |= data->ana->set3;
1001
1002 if (was_using_prediv && !use_fb_prediv) {
1003 ana[1] &= ~data->ana->fb_prediv_mask;
1004 do_ana_setup_first = true;
1005 } else if (!was_using_prediv && use_fb_prediv) {
1006 ana[1] |= data->ana->fb_prediv_mask;
1007 do_ana_setup_first = false;
1008 } else {
1009 do_ana_setup_first = true;
1010 }
1011
1012 /* Unmask the reference clock from the oscillator. */
1013 cprman_write(cprman, A2W_XOSC_CTRL,
1014 cprman_read(cprman, A2W_XOSC_CTRL) |
1015 data->reference_enable_mask);
1016
1017 if (do_ana_setup_first)
1018 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
1019
1020 /* Set the PLL multiplier from the oscillator. */
1021 cprman_write(cprman, data->frac_reg, fdiv);
1022
1023 a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
1024 a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
1025 a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
1026 a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
1027 a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
1028 cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
1029
1030 if (!do_ana_setup_first)
1031 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
1032
1033 return 0;
1034}
1035
1036static const struct clk_ops bcm2835_pll_clk_ops = {
1037 .is_prepared = bcm2835_pll_is_on,
1038 .prepare = bcm2835_pll_on,
1039 .unprepare = bcm2835_pll_off,
1040 .recalc_rate = bcm2835_pll_get_rate,
1041 .set_rate = bcm2835_pll_set_rate,
1042 .round_rate = bcm2835_pll_round_rate,
1043};
1044
1045struct bcm2835_pll_divider {
1046 struct clk_divider div;
1047 struct bcm2835_cprman *cprman;
1048 const struct bcm2835_pll_divider_data *data;
1049};
1050
1051static struct bcm2835_pll_divider *
1052bcm2835_pll_divider_from_hw(struct clk_hw *hw)
1053{
1054 return container_of(hw, struct bcm2835_pll_divider, div.hw);
1055}
1056
1057static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
1058{
1059 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1060 struct bcm2835_cprman *cprman = divider->cprman;
1061 const struct bcm2835_pll_divider_data *data = divider->data;
1062
1063 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
1064}
1065
1066static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
1067 unsigned long rate,
1068 unsigned long *parent_rate)
1069{
1070 return clk_divider_ops.round_rate(hw, rate, parent_rate);
1071}
1072
1073static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
1074 unsigned long parent_rate)
1075{
Eric Anholt79c1e2f2016-02-15 19:03:58 -08001076 return clk_divider_ops.recalc_rate(hw, parent_rate);
Eric Anholt41691b82015-10-08 18:37:24 -07001077}
1078
1079static void bcm2835_pll_divider_off(struct clk_hw *hw)
1080{
1081 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1082 struct bcm2835_cprman *cprman = divider->cprman;
1083 const struct bcm2835_pll_divider_data *data = divider->data;
1084
1085 cprman_write(cprman, data->cm_reg,
1086 (cprman_read(cprman, data->cm_reg) &
1087 ~data->load_mask) | data->hold_mask);
1088 cprman_write(cprman, data->a2w_reg, A2W_PLL_CHANNEL_DISABLE);
1089}
1090
1091static int bcm2835_pll_divider_on(struct clk_hw *hw)
1092{
1093 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1094 struct bcm2835_cprman *cprman = divider->cprman;
1095 const struct bcm2835_pll_divider_data *data = divider->data;
1096
1097 cprman_write(cprman, data->a2w_reg,
1098 cprman_read(cprman, data->a2w_reg) &
1099 ~A2W_PLL_CHANNEL_DISABLE);
1100
1101 cprman_write(cprman, data->cm_reg,
1102 cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
1103
1104 return 0;
1105}
1106
1107static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
1108 unsigned long rate,
1109 unsigned long parent_rate)
1110{
1111 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
1112 struct bcm2835_cprman *cprman = divider->cprman;
1113 const struct bcm2835_pll_divider_data *data = divider->data;
Eric Anholt773b3962016-02-15 19:03:57 -08001114 u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
Eric Anholt41691b82015-10-08 18:37:24 -07001115
Eric Anholt773b3962016-02-15 19:03:57 -08001116 div = DIV_ROUND_UP_ULL(parent_rate, rate);
Eric Anholt41691b82015-10-08 18:37:24 -07001117
Eric Anholt773b3962016-02-15 19:03:57 -08001118 div = min(div, max_div);
1119 if (div == max_div)
1120 div = 0;
1121
1122 cprman_write(cprman, data->a2w_reg, div);
Eric Anholt41691b82015-10-08 18:37:24 -07001123 cm = cprman_read(cprman, data->cm_reg);
1124 cprman_write(cprman, data->cm_reg, cm | data->load_mask);
1125 cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
1126
1127 return 0;
1128}
1129
1130static const struct clk_ops bcm2835_pll_divider_clk_ops = {
1131 .is_prepared = bcm2835_pll_divider_is_on,
1132 .prepare = bcm2835_pll_divider_on,
1133 .unprepare = bcm2835_pll_divider_off,
1134 .recalc_rate = bcm2835_pll_divider_get_rate,
1135 .set_rate = bcm2835_pll_divider_set_rate,
1136 .round_rate = bcm2835_pll_divider_round_rate,
1137};
1138
1139/*
1140 * The CM dividers do fixed-point division, so we can't use the
1141 * generic integer divider code like the PLL dividers do (and we can't
1142 * fake it by having some fixed shifts preceding it in the clock tree,
1143 * because we'd run out of bits in a 32-bit unsigned long).
1144 */
1145struct bcm2835_clock {
1146 struct clk_hw hw;
1147 struct bcm2835_cprman *cprman;
1148 const struct bcm2835_clock_data *data;
1149};
1150
1151static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
1152{
1153 return container_of(hw, struct bcm2835_clock, hw);
1154}
1155
1156static int bcm2835_clock_is_on(struct clk_hw *hw)
1157{
1158 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1159 struct bcm2835_cprman *cprman = clock->cprman;
1160 const struct bcm2835_clock_data *data = clock->data;
1161
1162 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
1163}
1164
1165static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
1166 unsigned long rate,
Remi Pommarel9c95b322015-12-06 17:22:46 +01001167 unsigned long parent_rate,
1168 bool round_up)
Eric Anholt41691b82015-10-08 18:37:24 -07001169{
1170 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1171 const struct bcm2835_clock_data *data = clock->data;
Remi Pommarel9c95b322015-12-06 17:22:46 +01001172 u32 unused_frac_mask =
1173 GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
Eric Anholt41691b82015-10-08 18:37:24 -07001174 u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
Remi Pommarel9c95b322015-12-06 17:22:46 +01001175 u64 rem;
Eric Anholt41691b82015-10-08 18:37:24 -07001176 u32 div;
1177
Remi Pommarel9c95b322015-12-06 17:22:46 +01001178 rem = do_div(temp, rate);
Eric Anholt41691b82015-10-08 18:37:24 -07001179 div = temp;
1180
Remi Pommarel9c95b322015-12-06 17:22:46 +01001181 /* Round up and mask off the unused bits */
1182 if (round_up && ((div & unused_frac_mask) != 0 || rem != 0))
1183 div += unused_frac_mask + 1;
1184 div &= ~unused_frac_mask;
Eric Anholt41691b82015-10-08 18:37:24 -07001185
1186 /* Clamp to the limits. */
1187 div = max(div, unused_frac_mask + 1);
1188 div = min_t(u32, div, GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
1189 CM_DIV_FRAC_BITS - data->frac_bits));
1190
1191 return div;
1192}
1193
1194static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
1195 unsigned long parent_rate,
1196 u32 div)
1197{
1198 const struct bcm2835_clock_data *data = clock->data;
1199 u64 temp;
1200
1201 /*
1202 * The divisor is a 12.12 fixed point field, but only some of
1203 * the bits are populated in any given clock.
1204 */
1205 div >>= CM_DIV_FRAC_BITS - data->frac_bits;
1206 div &= (1 << (data->int_bits + data->frac_bits)) - 1;
1207
1208 if (div == 0)
1209 return 0;
1210
1211 temp = (u64)parent_rate << data->frac_bits;
1212
1213 do_div(temp, div);
1214
1215 return temp;
1216}
1217
Eric Anholt41691b82015-10-08 18:37:24 -07001218static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
1219 unsigned long parent_rate)
1220{
1221 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1222 struct bcm2835_cprman *cprman = clock->cprman;
1223 const struct bcm2835_clock_data *data = clock->data;
1224 u32 div = cprman_read(cprman, data->div_reg);
1225
1226 return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
1227}
1228
1229static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
1230{
1231 struct bcm2835_cprman *cprman = clock->cprman;
1232 const struct bcm2835_clock_data *data = clock->data;
1233 ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
1234
1235 while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
1236 if (ktime_after(ktime_get(), timeout)) {
1237 dev_err(cprman->dev, "%s: couldn't lock PLL\n",
1238 clk_hw_get_name(&clock->hw));
1239 return;
1240 }
1241 cpu_relax();
1242 }
1243}
1244
1245static void bcm2835_clock_off(struct clk_hw *hw)
1246{
1247 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1248 struct bcm2835_cprman *cprman = clock->cprman;
1249 const struct bcm2835_clock_data *data = clock->data;
1250
1251 spin_lock(&cprman->regs_lock);
1252 cprman_write(cprman, data->ctl_reg,
1253 cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
1254 spin_unlock(&cprman->regs_lock);
1255
1256 /* BUSY will remain high until the divider completes its cycle. */
1257 bcm2835_clock_wait_busy(clock);
1258}
1259
1260static int bcm2835_clock_on(struct clk_hw *hw)
1261{
1262 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1263 struct bcm2835_cprman *cprman = clock->cprman;
1264 const struct bcm2835_clock_data *data = clock->data;
1265
1266 spin_lock(&cprman->regs_lock);
1267 cprman_write(cprman, data->ctl_reg,
1268 cprman_read(cprman, data->ctl_reg) |
1269 CM_ENABLE |
1270 CM_GATE);
1271 spin_unlock(&cprman->regs_lock);
1272
1273 return 0;
1274}
1275
1276static int bcm2835_clock_set_rate(struct clk_hw *hw,
1277 unsigned long rate, unsigned long parent_rate)
1278{
1279 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1280 struct bcm2835_cprman *cprman = clock->cprman;
1281 const struct bcm2835_clock_data *data = clock->data;
Remi Pommarel9c95b322015-12-06 17:22:46 +01001282 u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false);
Eric Anholt41691b82015-10-08 18:37:24 -07001283
1284 cprman_write(cprman, data->div_reg, div);
1285
1286 return 0;
1287}
1288
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001289static int bcm2835_clock_determine_rate(struct clk_hw *hw,
1290 struct clk_rate_request *req)
1291{
1292 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1293 struct clk_hw *parent, *best_parent = NULL;
1294 unsigned long rate, best_rate = 0;
1295 unsigned long prate, best_prate = 0;
1296 size_t i;
1297 u32 div;
1298
1299 /*
1300 * Select parent clock that results in the closest but lower rate
1301 */
1302 for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
1303 parent = clk_hw_get_parent_by_index(hw, i);
1304 if (!parent)
1305 continue;
1306 prate = clk_hw_get_rate(parent);
1307 div = bcm2835_clock_choose_div(hw, req->rate, prate, true);
1308 rate = bcm2835_clock_rate_from_divisor(clock, prate, div);
1309 if (rate > best_rate && rate <= req->rate) {
1310 best_parent = parent;
1311 best_prate = prate;
1312 best_rate = rate;
1313 }
1314 }
1315
1316 if (!best_parent)
1317 return -EINVAL;
1318
1319 req->best_parent_hw = best_parent;
1320 req->best_parent_rate = best_prate;
1321
1322 req->rate = best_rate;
1323
1324 return 0;
1325}
1326
1327static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
1328{
1329 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1330 struct bcm2835_cprman *cprman = clock->cprman;
1331 const struct bcm2835_clock_data *data = clock->data;
1332 u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
1333
1334 cprman_write(cprman, data->ctl_reg, src);
1335 return 0;
1336}
1337
1338static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
1339{
1340 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1341 struct bcm2835_cprman *cprman = clock->cprman;
1342 const struct bcm2835_clock_data *data = clock->data;
1343 u32 src = cprman_read(cprman, data->ctl_reg);
1344
1345 return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
1346}
1347
1348
Eric Anholt41691b82015-10-08 18:37:24 -07001349static const struct clk_ops bcm2835_clock_clk_ops = {
1350 .is_prepared = bcm2835_clock_is_on,
1351 .prepare = bcm2835_clock_on,
1352 .unprepare = bcm2835_clock_off,
1353 .recalc_rate = bcm2835_clock_get_rate,
1354 .set_rate = bcm2835_clock_set_rate,
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001355 .determine_rate = bcm2835_clock_determine_rate,
1356 .set_parent = bcm2835_clock_set_parent,
1357 .get_parent = bcm2835_clock_get_parent,
Eric Anholt41691b82015-10-08 18:37:24 -07001358};
1359
1360static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1361{
1362 return true;
1363}
1364
1365/*
1366 * The VPU clock can never be disabled (it doesn't have an ENABLE
1367 * bit), so it gets its own set of clock ops.
1368 */
1369static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1370 .is_prepared = bcm2835_vpu_clock_is_on,
1371 .recalc_rate = bcm2835_clock_get_rate,
1372 .set_rate = bcm2835_clock_set_rate,
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001373 .determine_rate = bcm2835_clock_determine_rate,
1374 .set_parent = bcm2835_clock_set_parent,
1375 .get_parent = bcm2835_clock_get_parent,
Eric Anholt41691b82015-10-08 18:37:24 -07001376};
1377
1378static struct clk *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1379 const struct bcm2835_pll_data *data)
1380{
1381 struct bcm2835_pll *pll;
1382 struct clk_init_data init;
1383
1384 memset(&init, 0, sizeof(init));
1385
1386 /* All of the PLLs derive from the external oscillator. */
1387 init.parent_names = &cprman->osc_name;
1388 init.num_parents = 1;
1389 init.name = data->name;
1390 init.ops = &bcm2835_pll_clk_ops;
1391 init.flags = CLK_IGNORE_UNUSED;
1392
1393 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1394 if (!pll)
1395 return NULL;
1396
1397 pll->cprman = cprman;
1398 pll->data = data;
1399 pll->hw.init = &init;
1400
1401 return devm_clk_register(cprman->dev, &pll->hw);
1402}
1403
1404static struct clk *
1405bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1406 const struct bcm2835_pll_divider_data *data)
1407{
1408 struct bcm2835_pll_divider *divider;
1409 struct clk_init_data init;
1410 struct clk *clk;
1411 const char *divider_name;
1412
1413 if (data->fixed_divider != 1) {
1414 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1415 "%s_prediv", data->name);
1416 if (!divider_name)
1417 return NULL;
1418 } else {
1419 divider_name = data->name;
1420 }
1421
1422 memset(&init, 0, sizeof(init));
1423
1424 init.parent_names = &data->source_pll->name;
1425 init.num_parents = 1;
1426 init.name = divider_name;
1427 init.ops = &bcm2835_pll_divider_clk_ops;
1428 init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
1429
1430 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1431 if (!divider)
1432 return NULL;
1433
1434 divider->div.reg = cprman->regs + data->a2w_reg;
1435 divider->div.shift = A2W_PLL_DIV_SHIFT;
1436 divider->div.width = A2W_PLL_DIV_BITS;
Eric Anholt79c1e2f2016-02-15 19:03:58 -08001437 divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
Eric Anholt41691b82015-10-08 18:37:24 -07001438 divider->div.lock = &cprman->regs_lock;
1439 divider->div.hw.init = &init;
1440 divider->div.table = NULL;
1441
1442 divider->cprman = cprman;
1443 divider->data = data;
1444
1445 clk = devm_clk_register(cprman->dev, &divider->div.hw);
1446 if (IS_ERR(clk))
1447 return clk;
1448
1449 /*
1450 * PLLH's channels have a fixed divide by 10 afterwards, which
1451 * is what our consumers are actually using.
1452 */
1453 if (data->fixed_divider != 1) {
1454 return clk_register_fixed_factor(cprman->dev, data->name,
1455 divider_name,
1456 CLK_SET_RATE_PARENT,
1457 1,
1458 data->fixed_divider);
1459 }
1460
1461 return clk;
1462}
1463
1464static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman,
1465 const struct bcm2835_clock_data *data)
1466{
1467 struct bcm2835_clock *clock;
1468 struct clk_init_data init;
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001469 const char *parents[1 << CM_SRC_BITS];
1470 size_t i;
Eric Anholt41691b82015-10-08 18:37:24 -07001471
1472 /*
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001473 * Replace our "xosc" references with the oscillator's
1474 * actual name.
Eric Anholt41691b82015-10-08 18:37:24 -07001475 */
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001476 for (i = 0; i < data->num_mux_parents; i++) {
1477 if (strcmp(data->parents[i], "xosc") == 0)
1478 parents[i] = cprman->osc_name;
1479 else
1480 parents[i] = data->parents[i];
Eric Anholt41691b82015-10-08 18:37:24 -07001481 }
1482
1483 memset(&init, 0, sizeof(init));
Remi Pommarel6d18b8a2015-12-06 17:22:47 +01001484 init.parent_names = parents;
1485 init.num_parents = data->num_mux_parents;
Eric Anholt41691b82015-10-08 18:37:24 -07001486 init.name = data->name;
1487 init.flags = CLK_IGNORE_UNUSED;
1488
1489 if (data->is_vpu_clock) {
1490 init.ops = &bcm2835_vpu_clock_clk_ops;
1491 } else {
1492 init.ops = &bcm2835_clock_clk_ops;
1493 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
1494 }
1495
1496 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1497 if (!clock)
1498 return NULL;
1499
1500 clock->cprman = cprman;
1501 clock->data = data;
1502 clock->hw.init = &init;
1503
1504 return devm_clk_register(cprman->dev, &clock->hw);
1505}
1506
1507static int bcm2835_clk_probe(struct platform_device *pdev)
1508{
1509 struct device *dev = &pdev->dev;
1510 struct clk **clks;
1511 struct bcm2835_cprman *cprman;
1512 struct resource *res;
1513
1514 cprman = devm_kzalloc(dev, sizeof(*cprman), GFP_KERNEL);
1515 if (!cprman)
1516 return -ENOMEM;
1517
1518 spin_lock_init(&cprman->regs_lock);
1519 cprman->dev = dev;
1520 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1521 cprman->regs = devm_ioremap_resource(dev, res);
1522 if (IS_ERR(cprman->regs))
1523 return PTR_ERR(cprman->regs);
1524
1525 cprman->osc_name = of_clk_get_parent_name(dev->of_node, 0);
1526 if (!cprman->osc_name)
1527 return -ENODEV;
1528
1529 platform_set_drvdata(pdev, cprman);
1530
1531 cprman->onecell.clk_num = BCM2835_CLOCK_COUNT;
1532 cprman->onecell.clks = cprman->clks;
1533 clks = cprman->clks;
1534
1535 clks[BCM2835_PLLA] = bcm2835_register_pll(cprman, &bcm2835_plla_data);
1536 clks[BCM2835_PLLB] = bcm2835_register_pll(cprman, &bcm2835_pllb_data);
1537 clks[BCM2835_PLLC] = bcm2835_register_pll(cprman, &bcm2835_pllc_data);
1538 clks[BCM2835_PLLD] = bcm2835_register_pll(cprman, &bcm2835_plld_data);
1539 clks[BCM2835_PLLH] = bcm2835_register_pll(cprman, &bcm2835_pllh_data);
1540
1541 clks[BCM2835_PLLA_CORE] =
1542 bcm2835_register_pll_divider(cprman, &bcm2835_plla_core_data);
1543 clks[BCM2835_PLLA_PER] =
1544 bcm2835_register_pll_divider(cprman, &bcm2835_plla_per_data);
1545 clks[BCM2835_PLLC_CORE0] =
1546 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core0_data);
1547 clks[BCM2835_PLLC_CORE1] =
1548 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core1_data);
1549 clks[BCM2835_PLLC_CORE2] =
1550 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core2_data);
1551 clks[BCM2835_PLLC_PER] =
1552 bcm2835_register_pll_divider(cprman, &bcm2835_pllc_per_data);
1553 clks[BCM2835_PLLD_CORE] =
1554 bcm2835_register_pll_divider(cprman, &bcm2835_plld_core_data);
1555 clks[BCM2835_PLLD_PER] =
1556 bcm2835_register_pll_divider(cprman, &bcm2835_plld_per_data);
1557 clks[BCM2835_PLLH_RCAL] =
1558 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_rcal_data);
1559 clks[BCM2835_PLLH_AUX] =
1560 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_aux_data);
1561 clks[BCM2835_PLLH_PIX] =
1562 bcm2835_register_pll_divider(cprman, &bcm2835_pllh_pix_data);
1563
1564 clks[BCM2835_CLOCK_TIMER] =
1565 bcm2835_register_clock(cprman, &bcm2835_clock_timer_data);
1566 clks[BCM2835_CLOCK_OTP] =
1567 bcm2835_register_clock(cprman, &bcm2835_clock_otp_data);
1568 clks[BCM2835_CLOCK_TSENS] =
1569 bcm2835_register_clock(cprman, &bcm2835_clock_tsens_data);
1570 clks[BCM2835_CLOCK_VPU] =
1571 bcm2835_register_clock(cprman, &bcm2835_clock_vpu_data);
1572 clks[BCM2835_CLOCK_V3D] =
1573 bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data);
1574 clks[BCM2835_CLOCK_ISP] =
1575 bcm2835_register_clock(cprman, &bcm2835_clock_isp_data);
1576 clks[BCM2835_CLOCK_H264] =
1577 bcm2835_register_clock(cprman, &bcm2835_clock_h264_data);
1578 clks[BCM2835_CLOCK_V3D] =
1579 bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data);
1580 clks[BCM2835_CLOCK_SDRAM] =
1581 bcm2835_register_clock(cprman, &bcm2835_clock_sdram_data);
1582 clks[BCM2835_CLOCK_UART] =
1583 bcm2835_register_clock(cprman, &bcm2835_clock_uart_data);
1584 clks[BCM2835_CLOCK_VEC] =
1585 bcm2835_register_clock(cprman, &bcm2835_clock_vec_data);
1586 clks[BCM2835_CLOCK_HSM] =
1587 bcm2835_register_clock(cprman, &bcm2835_clock_hsm_data);
1588 clks[BCM2835_CLOCK_EMMC] =
1589 bcm2835_register_clock(cprman, &bcm2835_clock_emmc_data);
1590
1591 /*
1592 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
1593 * you have the debug bit set in the power manager, which we
1594 * don't bother exposing) are individual gates off of the
1595 * non-stop vpu clock.
1596 */
1597 clks[BCM2835_CLOCK_PERI_IMAGE] =
1598 clk_register_gate(dev, "peri_image", "vpu",
1599 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1600 cprman->regs + CM_PERIICTL, CM_GATE_BIT,
1601 0, &cprman->regs_lock);
1602
Remi Pommarelcfbab8f2015-12-06 17:22:48 +01001603 clks[BCM2835_CLOCK_PWM] =
1604 bcm2835_register_clock(cprman, &bcm2835_clock_pwm_data);
1605
Eric Anholt41691b82015-10-08 18:37:24 -07001606 return of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1607 &cprman->onecell);
1608}
1609
1610static const struct of_device_id bcm2835_clk_of_match[] = {
1611 { .compatible = "brcm,bcm2835-cprman", },
1612 {}
1613};
1614MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
1615
1616static struct platform_driver bcm2835_clk_driver = {
1617 .driver = {
1618 .name = "bcm2835-clk",
1619 .of_match_table = bcm2835_clk_of_match,
1620 },
1621 .probe = bcm2835_clk_probe,
1622};
1623
1624builtin_platform_driver(bcm2835_clk_driver);
1625
1626MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
1627MODULE_DESCRIPTION("BCM2835 clock driver");
1628MODULE_LICENSE("GPL v2");