Commit | Line | Data |
---|---|---|
75fabc3f | 1 | /* |
41691b88 | 2 | * Copyright (C) 2010,2015 Broadcom |
75fabc3f SA |
3 | * Copyright (C) 2012 Stephen Warren |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
75fabc3f SA |
15 | */ |
16 | ||
41691b88 EA |
17 | /** |
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 | ||
75fabc3f SA |
37 | #include <linux/clk-provider.h> |
38 | #include <linux/clkdev.h> | |
39 | #include <linux/clk/bcm2835.h> | |
96bf9c69 | 40 | #include <linux/debugfs.h> |
41691b88 | 41 | #include <linux/module.h> |
526d239c | 42 | #include <linux/of.h> |
41691b88 EA |
43 | #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 | |
959ca92a | 52 | # define CM_DIV_FRAC_MASK GENMASK(CM_DIV_FRAC_BITS - 1, 0) |
41691b88 EA |
53 | |
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 | |
2103a215 MS |
90 | #define CM_PCMCTL 0x098 |
91 | #define CM_PCMDIV 0x09c | |
41691b88 EA |
92 | #define CM_PWMCTL 0x0a0 |
93 | #define CM_PWMDIV 0x0a4 | |
2103a215 MS |
94 | #define CM_SLIMCTL 0x0a8 |
95 | #define CM_SLIMDIV 0x0ac | |
41691b88 EA |
96 | #define CM_SMICTL 0x0b0 |
97 | #define CM_SMIDIV 0x0b4 | |
2103a215 MS |
98 | /* 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 | |
41691b88 EA |
107 | #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) | |
959ca92a | 130 | # define CM_FRAC BIT(9) |
41691b88 EA |
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 | ||
297 | struct bcm2835_cprman { | |
298 | struct device *dev; | |
299 | void __iomem *regs; | |
6e1e60da | 300 | spinlock_t regs_lock; /* spinlock for all clocks */ |
41691b88 EA |
301 | const char *osc_name; |
302 | ||
303 | struct clk_onecell_data onecell; | |
304 | struct clk *clks[BCM2835_CLOCK_COUNT]; | |
305 | }; | |
306 | ||
307 | static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val) | |
308 | { | |
309 | writel(CM_PASSWORD | val, cprman->regs + reg); | |
310 | } | |
311 | ||
312 | static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg) | |
313 | { | |
314 | return readl(cprman->regs + reg); | |
315 | } | |
526d239c | 316 | |
96bf9c69 MS |
317 | static 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 | ||
75fabc3f SA |
338 | /* |
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 | */ | |
343 | void __init bcm2835_init_clocks(void) | |
344 | { | |
345 | struct clk *clk; | |
346 | int ret; | |
347 | ||
bd41aa67 | 348 | clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, 0, 126000000); |
0de9f23a | 349 | if (IS_ERR(clk)) |
75fabc3f SA |
350 | pr_err("apb_pclk not registered\n"); |
351 | ||
bd41aa67 | 352 | clk = clk_register_fixed_rate(NULL, "uart0_pclk", NULL, 0, 3000000); |
0de9f23a | 353 | if (IS_ERR(clk)) |
75fabc3f SA |
354 | 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 | ||
bd41aa67 | 359 | clk = clk_register_fixed_rate(NULL, "uart1_pclk", NULL, 0, 125000000); |
0de9f23a | 360 | if (IS_ERR(clk)) |
75fabc3f SA |
361 | pr_err("uart1_pclk not registered\n"); |
362 | ret = clk_register_clkdev(clk, NULL, "20215000.uart"); | |
363 | if (ret) | |
686ea585 | 364 | pr_err("uart1_pclk alias not registered\n"); |
75fabc3f | 365 | } |
41691b88 EA |
366 | |
367 | struct 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 | ||
388 | struct 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 | ||
398 | static 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 | ||
408 | static 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 | ||
418 | /* | |
419 | * PLLA is the auxiliary PLL, used to drive the CCP2 (Compact Camera | |
420 | * Port 2) transmitter clock. | |
421 | * | |
422 | * It is in the PX LDO power domain, which is on when the AUDIO domain | |
423 | * is on. | |
424 | */ | |
425 | static const struct bcm2835_pll_data bcm2835_plla_data = { | |
426 | .name = "plla", | |
427 | .cm_ctrl_reg = CM_PLLA, | |
428 | .a2w_ctrl_reg = A2W_PLLA_CTRL, | |
429 | .frac_reg = A2W_PLLA_FRAC, | |
430 | .ana_reg_base = A2W_PLLA_ANA0, | |
431 | .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE, | |
432 | .lock_mask = CM_LOCK_FLOCKA, | |
433 | ||
434 | .ana = &bcm2835_ana_default, | |
435 | ||
436 | .min_rate = 600000000u, | |
437 | .max_rate = 2400000000u, | |
438 | .max_fb_rate = BCM2835_MAX_FB_RATE, | |
439 | }; | |
440 | ||
441 | /* PLLB is used for the ARM's clock. */ | |
442 | static const struct bcm2835_pll_data bcm2835_pllb_data = { | |
443 | .name = "pllb", | |
444 | .cm_ctrl_reg = CM_PLLB, | |
445 | .a2w_ctrl_reg = A2W_PLLB_CTRL, | |
446 | .frac_reg = A2W_PLLB_FRAC, | |
447 | .ana_reg_base = A2W_PLLB_ANA0, | |
448 | .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE, | |
449 | .lock_mask = CM_LOCK_FLOCKB, | |
450 | ||
451 | .ana = &bcm2835_ana_default, | |
452 | ||
453 | .min_rate = 600000000u, | |
454 | .max_rate = 3000000000u, | |
455 | .max_fb_rate = BCM2835_MAX_FB_RATE, | |
456 | }; | |
457 | ||
458 | /* | |
459 | * PLLC is the core PLL, used to drive the core VPU clock. | |
460 | * | |
461 | * It is in the PX LDO power domain, which is on when the AUDIO domain | |
462 | * is on. | |
463 | */ | |
464 | static const struct bcm2835_pll_data bcm2835_pllc_data = { | |
465 | .name = "pllc", | |
466 | .cm_ctrl_reg = CM_PLLC, | |
467 | .a2w_ctrl_reg = A2W_PLLC_CTRL, | |
468 | .frac_reg = A2W_PLLC_FRAC, | |
469 | .ana_reg_base = A2W_PLLC_ANA0, | |
470 | .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE, | |
471 | .lock_mask = CM_LOCK_FLOCKC, | |
472 | ||
473 | .ana = &bcm2835_ana_default, | |
474 | ||
475 | .min_rate = 600000000u, | |
476 | .max_rate = 3000000000u, | |
477 | .max_fb_rate = BCM2835_MAX_FB_RATE, | |
478 | }; | |
479 | ||
480 | /* | |
481 | * PLLD is the display PLL, used to drive DSI display panels. | |
482 | * | |
483 | * It is in the PX LDO power domain, which is on when the AUDIO domain | |
484 | * is on. | |
485 | */ | |
486 | static const struct bcm2835_pll_data bcm2835_plld_data = { | |
487 | .name = "plld", | |
488 | .cm_ctrl_reg = CM_PLLD, | |
489 | .a2w_ctrl_reg = A2W_PLLD_CTRL, | |
490 | .frac_reg = A2W_PLLD_FRAC, | |
491 | .ana_reg_base = A2W_PLLD_ANA0, | |
492 | .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE, | |
493 | .lock_mask = CM_LOCK_FLOCKD, | |
494 | ||
495 | .ana = &bcm2835_ana_default, | |
496 | ||
497 | .min_rate = 600000000u, | |
498 | .max_rate = 2400000000u, | |
499 | .max_fb_rate = BCM2835_MAX_FB_RATE, | |
500 | }; | |
501 | ||
502 | /* | |
503 | * PLLH is used to supply the pixel clock or the AUX clock for the TV | |
504 | * encoder. | |
505 | * | |
506 | * It is in the HDMI power domain. | |
507 | */ | |
508 | static const struct bcm2835_pll_data bcm2835_pllh_data = { | |
509 | "pllh", | |
510 | .cm_ctrl_reg = CM_PLLH, | |
511 | .a2w_ctrl_reg = A2W_PLLH_CTRL, | |
512 | .frac_reg = A2W_PLLH_FRAC, | |
513 | .ana_reg_base = A2W_PLLH_ANA0, | |
514 | .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE, | |
515 | .lock_mask = CM_LOCK_FLOCKH, | |
516 | ||
517 | .ana = &bcm2835_ana_pllh, | |
518 | ||
519 | .min_rate = 600000000u, | |
520 | .max_rate = 3000000000u, | |
521 | .max_fb_rate = BCM2835_MAX_FB_RATE, | |
522 | }; | |
523 | ||
524 | struct bcm2835_pll_divider_data { | |
525 | const char *name; | |
526 | const struct bcm2835_pll_data *source_pll; | |
527 | u32 cm_reg; | |
528 | u32 a2w_reg; | |
529 | ||
530 | u32 load_mask; | |
531 | u32 hold_mask; | |
532 | u32 fixed_divider; | |
533 | }; | |
534 | ||
535 | static const struct bcm2835_pll_divider_data bcm2835_plla_core_data = { | |
536 | .name = "plla_core", | |
537 | .source_pll = &bcm2835_plla_data, | |
538 | .cm_reg = CM_PLLA, | |
539 | .a2w_reg = A2W_PLLA_CORE, | |
540 | .load_mask = CM_PLLA_LOADCORE, | |
541 | .hold_mask = CM_PLLA_HOLDCORE, | |
542 | .fixed_divider = 1, | |
543 | }; | |
544 | ||
545 | static const struct bcm2835_pll_divider_data bcm2835_plla_per_data = { | |
546 | .name = "plla_per", | |
547 | .source_pll = &bcm2835_plla_data, | |
548 | .cm_reg = CM_PLLA, | |
549 | .a2w_reg = A2W_PLLA_PER, | |
550 | .load_mask = CM_PLLA_LOADPER, | |
551 | .hold_mask = CM_PLLA_HOLDPER, | |
552 | .fixed_divider = 1, | |
553 | }; | |
554 | ||
555 | static const struct bcm2835_pll_divider_data bcm2835_pllb_arm_data = { | |
556 | .name = "pllb_arm", | |
557 | .source_pll = &bcm2835_pllb_data, | |
558 | .cm_reg = CM_PLLB, | |
559 | .a2w_reg = A2W_PLLB_ARM, | |
560 | .load_mask = CM_PLLB_LOADARM, | |
561 | .hold_mask = CM_PLLB_HOLDARM, | |
562 | .fixed_divider = 1, | |
563 | }; | |
564 | ||
565 | static const struct bcm2835_pll_divider_data bcm2835_pllc_core0_data = { | |
566 | .name = "pllc_core0", | |
567 | .source_pll = &bcm2835_pllc_data, | |
568 | .cm_reg = CM_PLLC, | |
569 | .a2w_reg = A2W_PLLC_CORE0, | |
570 | .load_mask = CM_PLLC_LOADCORE0, | |
571 | .hold_mask = CM_PLLC_HOLDCORE0, | |
572 | .fixed_divider = 1, | |
573 | }; | |
574 | ||
575 | static const struct bcm2835_pll_divider_data bcm2835_pllc_core1_data = { | |
576 | .name = "pllc_core1", .source_pll = &bcm2835_pllc_data, | |
577 | .cm_reg = CM_PLLC, A2W_PLLC_CORE1, | |
578 | .load_mask = CM_PLLC_LOADCORE1, | |
579 | .hold_mask = CM_PLLC_HOLDCORE1, | |
580 | .fixed_divider = 1, | |
581 | }; | |
582 | ||
583 | static const struct bcm2835_pll_divider_data bcm2835_pllc_core2_data = { | |
584 | .name = "pllc_core2", | |
585 | .source_pll = &bcm2835_pllc_data, | |
586 | .cm_reg = CM_PLLC, | |
587 | .a2w_reg = A2W_PLLC_CORE2, | |
588 | .load_mask = CM_PLLC_LOADCORE2, | |
589 | .hold_mask = CM_PLLC_HOLDCORE2, | |
590 | .fixed_divider = 1, | |
591 | }; | |
592 | ||
593 | static const struct bcm2835_pll_divider_data bcm2835_pllc_per_data = { | |
594 | .name = "pllc_per", | |
595 | .source_pll = &bcm2835_pllc_data, | |
596 | .cm_reg = CM_PLLC, | |
597 | .a2w_reg = A2W_PLLC_PER, | |
598 | .load_mask = CM_PLLC_LOADPER, | |
599 | .hold_mask = CM_PLLC_HOLDPER, | |
600 | .fixed_divider = 1, | |
601 | }; | |
602 | ||
603 | static const struct bcm2835_pll_divider_data bcm2835_plld_core_data = { | |
604 | .name = "plld_core", | |
605 | .source_pll = &bcm2835_plld_data, | |
606 | .cm_reg = CM_PLLD, | |
607 | .a2w_reg = A2W_PLLD_CORE, | |
608 | .load_mask = CM_PLLD_LOADCORE, | |
609 | .hold_mask = CM_PLLD_HOLDCORE, | |
610 | .fixed_divider = 1, | |
611 | }; | |
612 | ||
613 | static const struct bcm2835_pll_divider_data bcm2835_plld_per_data = { | |
614 | .name = "plld_per", | |
615 | .source_pll = &bcm2835_plld_data, | |
616 | .cm_reg = CM_PLLD, | |
617 | .a2w_reg = A2W_PLLD_PER, | |
618 | .load_mask = CM_PLLD_LOADPER, | |
619 | .hold_mask = CM_PLLD_HOLDPER, | |
620 | .fixed_divider = 1, | |
621 | }; | |
622 | ||
623 | static const struct bcm2835_pll_divider_data bcm2835_pllh_rcal_data = { | |
624 | .name = "pllh_rcal", | |
625 | .source_pll = &bcm2835_pllh_data, | |
626 | .cm_reg = CM_PLLH, | |
627 | .a2w_reg = A2W_PLLH_RCAL, | |
628 | .load_mask = CM_PLLH_LOADRCAL, | |
629 | .hold_mask = 0, | |
630 | .fixed_divider = 10, | |
631 | }; | |
632 | ||
633 | static const struct bcm2835_pll_divider_data bcm2835_pllh_aux_data = { | |
634 | .name = "pllh_aux", | |
635 | .source_pll = &bcm2835_pllh_data, | |
636 | .cm_reg = CM_PLLH, | |
637 | .a2w_reg = A2W_PLLH_AUX, | |
638 | .load_mask = CM_PLLH_LOADAUX, | |
639 | .hold_mask = 0, | |
640 | .fixed_divider = 10, | |
641 | }; | |
642 | ||
643 | static const struct bcm2835_pll_divider_data bcm2835_pllh_pix_data = { | |
644 | .name = "pllh_pix", | |
645 | .source_pll = &bcm2835_pllh_data, | |
646 | .cm_reg = CM_PLLH, | |
647 | .a2w_reg = A2W_PLLH_PIX, | |
648 | .load_mask = CM_PLLH_LOADPIX, | |
649 | .hold_mask = 0, | |
650 | .fixed_divider = 10, | |
651 | }; | |
652 | ||
653 | struct bcm2835_clock_data { | |
654 | const char *name; | |
655 | ||
656 | const char *const *parents; | |
657 | int num_mux_parents; | |
658 | ||
659 | u32 ctl_reg; | |
660 | u32 div_reg; | |
661 | ||
662 | /* Number of integer bits in the divider */ | |
663 | u32 int_bits; | |
664 | /* Number of fractional bits in the divider */ | |
665 | u32 frac_bits; | |
666 | ||
667 | bool is_vpu_clock; | |
959ca92a | 668 | bool is_mash_clock; |
41691b88 EA |
669 | }; |
670 | ||
671 | static const char *const bcm2835_clock_per_parents[] = { | |
672 | "gnd", | |
673 | "xosc", | |
674 | "testdebug0", | |
675 | "testdebug1", | |
676 | "plla_per", | |
677 | "pllc_per", | |
678 | "plld_per", | |
679 | "pllh_aux", | |
680 | }; | |
681 | ||
682 | static const char *const bcm2835_clock_vpu_parents[] = { | |
683 | "gnd", | |
684 | "xosc", | |
685 | "testdebug0", | |
686 | "testdebug1", | |
687 | "plla_core", | |
688 | "pllc_core0", | |
689 | "plld_core", | |
690 | "pllh_aux", | |
691 | "pllc_core1", | |
692 | "pllc_core2", | |
693 | }; | |
694 | ||
695 | static const char *const bcm2835_clock_osc_parents[] = { | |
696 | "gnd", | |
697 | "xosc", | |
698 | "testdebug0", | |
699 | "testdebug1" | |
700 | }; | |
701 | ||
702 | /* | |
703 | * Used for a 1Mhz clock for the system clocksource, and also used by | |
704 | * the watchdog timer and the camera pulse generator. | |
705 | */ | |
706 | static const struct bcm2835_clock_data bcm2835_clock_timer_data = { | |
707 | .name = "timer", | |
708 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), | |
709 | .parents = bcm2835_clock_osc_parents, | |
710 | .ctl_reg = CM_TIMERCTL, | |
711 | .div_reg = CM_TIMERDIV, | |
712 | .int_bits = 6, | |
713 | .frac_bits = 12, | |
714 | }; | |
715 | ||
716 | /* One Time Programmable Memory clock. Maximum 10Mhz. */ | |
717 | static const struct bcm2835_clock_data bcm2835_clock_otp_data = { | |
718 | .name = "otp", | |
719 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), | |
720 | .parents = bcm2835_clock_osc_parents, | |
721 | .ctl_reg = CM_OTPCTL, | |
722 | .div_reg = CM_OTPDIV, | |
723 | .int_bits = 4, | |
724 | .frac_bits = 0, | |
725 | }; | |
726 | ||
727 | /* | |
728 | * VPU clock. This doesn't have an enable bit, since it drives the | |
729 | * bus for everything else, and is special so it doesn't need to be | |
730 | * gated for rate changes. It is also known as "clk_audio" in various | |
731 | * hardware documentation. | |
732 | */ | |
733 | static const struct bcm2835_clock_data bcm2835_clock_vpu_data = { | |
734 | .name = "vpu", | |
735 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), | |
736 | .parents = bcm2835_clock_vpu_parents, | |
737 | .ctl_reg = CM_VPUCTL, | |
738 | .div_reg = CM_VPUDIV, | |
739 | .int_bits = 12, | |
740 | .frac_bits = 8, | |
741 | .is_vpu_clock = true, | |
742 | }; | |
743 | ||
744 | static const struct bcm2835_clock_data bcm2835_clock_v3d_data = { | |
745 | .name = "v3d", | |
746 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), | |
747 | .parents = bcm2835_clock_vpu_parents, | |
748 | .ctl_reg = CM_V3DCTL, | |
749 | .div_reg = CM_V3DDIV, | |
750 | .int_bits = 4, | |
751 | .frac_bits = 8, | |
752 | }; | |
753 | ||
754 | static const struct bcm2835_clock_data bcm2835_clock_isp_data = { | |
755 | .name = "isp", | |
756 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), | |
757 | .parents = bcm2835_clock_vpu_parents, | |
758 | .ctl_reg = CM_ISPCTL, | |
759 | .div_reg = CM_ISPDIV, | |
760 | .int_bits = 4, | |
761 | .frac_bits = 8, | |
762 | }; | |
763 | ||
764 | static const struct bcm2835_clock_data bcm2835_clock_h264_data = { | |
765 | .name = "h264", | |
766 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), | |
767 | .parents = bcm2835_clock_vpu_parents, | |
768 | .ctl_reg = CM_H264CTL, | |
769 | .div_reg = CM_H264DIV, | |
770 | .int_bits = 4, | |
771 | .frac_bits = 8, | |
772 | }; | |
773 | ||
774 | /* TV encoder clock. Only operating frequency is 108Mhz. */ | |
775 | static const struct bcm2835_clock_data bcm2835_clock_vec_data = { | |
776 | .name = "vec", | |
777 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), | |
778 | .parents = bcm2835_clock_per_parents, | |
779 | .ctl_reg = CM_VECCTL, | |
780 | .div_reg = CM_VECDIV, | |
781 | .int_bits = 4, | |
782 | .frac_bits = 0, | |
783 | }; | |
784 | ||
785 | static const struct bcm2835_clock_data bcm2835_clock_uart_data = { | |
786 | .name = "uart", | |
787 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), | |
788 | .parents = bcm2835_clock_per_parents, | |
789 | .ctl_reg = CM_UARTCTL, | |
790 | .div_reg = CM_UARTDIV, | |
791 | .int_bits = 10, | |
792 | .frac_bits = 12, | |
793 | }; | |
794 | ||
795 | /* HDMI state machine */ | |
796 | static const struct bcm2835_clock_data bcm2835_clock_hsm_data = { | |
797 | .name = "hsm", | |
798 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), | |
799 | .parents = bcm2835_clock_per_parents, | |
800 | .ctl_reg = CM_HSMCTL, | |
801 | .div_reg = CM_HSMDIV, | |
802 | .int_bits = 4, | |
803 | .frac_bits = 8, | |
804 | }; | |
805 | ||
806 | /* | |
807 | * Secondary SDRAM clock. Used for low-voltage modes when the PLL in | |
808 | * the SDRAM controller can't be used. | |
809 | */ | |
810 | static const struct bcm2835_clock_data bcm2835_clock_sdram_data = { | |
811 | .name = "sdram", | |
812 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), | |
813 | .parents = bcm2835_clock_vpu_parents, | |
814 | .ctl_reg = CM_SDCCTL, | |
815 | .div_reg = CM_SDCDIV, | |
816 | .int_bits = 6, | |
817 | .frac_bits = 0, | |
818 | }; | |
819 | ||
820 | /* Clock for the temperature sensor. Generally run at 2Mhz, max 5Mhz. */ | |
821 | static const struct bcm2835_clock_data bcm2835_clock_tsens_data = { | |
822 | .name = "tsens", | |
823 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), | |
824 | .parents = bcm2835_clock_osc_parents, | |
825 | .ctl_reg = CM_TSENSCTL, | |
826 | .div_reg = CM_TSENSDIV, | |
827 | .int_bits = 5, | |
828 | .frac_bits = 0, | |
829 | }; | |
830 | ||
831 | /* Arasan EMMC clock */ | |
832 | static const struct bcm2835_clock_data bcm2835_clock_emmc_data = { | |
833 | .name = "emmc", | |
834 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), | |
835 | .parents = bcm2835_clock_per_parents, | |
836 | .ctl_reg = CM_EMMCCTL, | |
837 | .div_reg = CM_EMMCDIV, | |
838 | .int_bits = 4, | |
839 | .frac_bits = 8, | |
840 | }; | |
841 | ||
cfbab8fb RP |
842 | static const struct bcm2835_clock_data bcm2835_clock_pwm_data = { |
843 | .name = "pwm", | |
844 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), | |
845 | .parents = bcm2835_clock_per_parents, | |
846 | .ctl_reg = CM_PWMCTL, | |
847 | .div_reg = CM_PWMDIV, | |
848 | .int_bits = 12, | |
849 | .frac_bits = 12, | |
959ca92a | 850 | .is_mash_clock = true, |
cfbab8fb RP |
851 | }; |
852 | ||
41691b88 EA |
853 | struct bcm2835_pll { |
854 | struct clk_hw hw; | |
855 | struct bcm2835_cprman *cprman; | |
856 | const struct bcm2835_pll_data *data; | |
857 | }; | |
858 | ||
859 | static int bcm2835_pll_is_on(struct clk_hw *hw) | |
860 | { | |
861 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
862 | struct bcm2835_cprman *cprman = pll->cprman; | |
863 | const struct bcm2835_pll_data *data = pll->data; | |
864 | ||
865 | return cprman_read(cprman, data->a2w_ctrl_reg) & | |
866 | A2W_PLL_CTRL_PRST_DISABLE; | |
867 | } | |
868 | ||
869 | static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate, | |
870 | unsigned long parent_rate, | |
871 | u32 *ndiv, u32 *fdiv) | |
872 | { | |
873 | u64 div; | |
874 | ||
875 | div = (u64)rate << A2W_PLL_FRAC_BITS; | |
876 | do_div(div, parent_rate); | |
877 | ||
878 | *ndiv = div >> A2W_PLL_FRAC_BITS; | |
879 | *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1); | |
880 | } | |
881 | ||
882 | static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate, | |
883 | u32 ndiv, u32 fdiv, u32 pdiv) | |
884 | { | |
885 | u64 rate; | |
886 | ||
887 | if (pdiv == 0) | |
888 | return 0; | |
889 | ||
890 | rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv); | |
891 | do_div(rate, pdiv); | |
892 | return rate >> A2W_PLL_FRAC_BITS; | |
893 | } | |
894 | ||
895 | static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate, | |
896 | unsigned long *parent_rate) | |
897 | { | |
898 | u32 ndiv, fdiv; | |
899 | ||
900 | bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv); | |
901 | ||
902 | return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1); | |
903 | } | |
904 | ||
905 | static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw, | |
906 | unsigned long parent_rate) | |
907 | { | |
908 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
909 | struct bcm2835_cprman *cprman = pll->cprman; | |
910 | const struct bcm2835_pll_data *data = pll->data; | |
911 | u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg); | |
912 | u32 ndiv, pdiv, fdiv; | |
913 | bool using_prediv; | |
914 | ||
915 | if (parent_rate == 0) | |
916 | return 0; | |
917 | ||
918 | fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK; | |
919 | ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT; | |
920 | pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT; | |
921 | using_prediv = cprman_read(cprman, data->ana_reg_base + 4) & | |
922 | data->ana->fb_prediv_mask; | |
923 | ||
924 | if (using_prediv) | |
925 | ndiv *= 2; | |
926 | ||
927 | return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv); | |
928 | } | |
929 | ||
930 | static void bcm2835_pll_off(struct clk_hw *hw) | |
931 | { | |
932 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
933 | struct bcm2835_cprman *cprman = pll->cprman; | |
934 | const struct bcm2835_pll_data *data = pll->data; | |
935 | ||
6727f086 MS |
936 | spin_lock(&cprman->regs_lock); |
937 | cprman_write(cprman, data->cm_ctrl_reg, | |
938 | cprman_read(cprman, data->cm_ctrl_reg) | | |
939 | CM_PLL_ANARST); | |
940 | cprman_write(cprman, data->a2w_ctrl_reg, | |
941 | cprman_read(cprman, data->a2w_ctrl_reg) | | |
942 | A2W_PLL_CTRL_PWRDN); | |
943 | spin_unlock(&cprman->regs_lock); | |
41691b88 EA |
944 | } |
945 | ||
946 | static int bcm2835_pll_on(struct clk_hw *hw) | |
947 | { | |
948 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
949 | struct bcm2835_cprman *cprman = pll->cprman; | |
950 | const struct bcm2835_pll_data *data = pll->data; | |
951 | ktime_t timeout; | |
952 | ||
953 | /* Take the PLL out of reset. */ | |
954 | cprman_write(cprman, data->cm_ctrl_reg, | |
955 | cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST); | |
956 | ||
957 | /* Wait for the PLL to lock. */ | |
958 | timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); | |
959 | while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) { | |
960 | if (ktime_after(ktime_get(), timeout)) { | |
961 | dev_err(cprman->dev, "%s: couldn't lock PLL\n", | |
962 | clk_hw_get_name(hw)); | |
963 | return -ETIMEDOUT; | |
964 | } | |
965 | ||
966 | cpu_relax(); | |
967 | } | |
968 | ||
969 | return 0; | |
970 | } | |
971 | ||
972 | static void | |
973 | bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana) | |
974 | { | |
975 | int i; | |
976 | ||
977 | /* | |
978 | * ANA register setup is done as a series of writes to | |
979 | * ANA3-ANA0, in that order. This lets us write all 4 | |
980 | * registers as a single cycle of the serdes interface (taking | |
981 | * 100 xosc clocks), whereas if we were to update ana0, 1, and | |
982 | * 3 individually through their partial-write registers, each | |
983 | * would be their own serdes cycle. | |
984 | */ | |
985 | for (i = 3; i >= 0; i--) | |
986 | cprman_write(cprman, ana_reg_base + i * 4, ana[i]); | |
987 | } | |
988 | ||
989 | static int bcm2835_pll_set_rate(struct clk_hw *hw, | |
990 | unsigned long rate, unsigned long parent_rate) | |
991 | { | |
992 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
993 | struct bcm2835_cprman *cprman = pll->cprman; | |
994 | const struct bcm2835_pll_data *data = pll->data; | |
995 | bool was_using_prediv, use_fb_prediv, do_ana_setup_first; | |
996 | u32 ndiv, fdiv, a2w_ctl; | |
997 | u32 ana[4]; | |
998 | int i; | |
999 | ||
1000 | if (rate < data->min_rate || rate > data->max_rate) { | |
1001 | dev_err(cprman->dev, "%s: rate out of spec: %lu vs (%lu, %lu)\n", | |
1002 | clk_hw_get_name(hw), rate, | |
1003 | data->min_rate, data->max_rate); | |
1004 | return -EINVAL; | |
1005 | } | |
1006 | ||
1007 | if (rate > data->max_fb_rate) { | |
1008 | use_fb_prediv = true; | |
1009 | rate /= 2; | |
1010 | } else { | |
1011 | use_fb_prediv = false; | |
1012 | } | |
1013 | ||
1014 | bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv); | |
1015 | ||
1016 | for (i = 3; i >= 0; i--) | |
1017 | ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4); | |
1018 | ||
1019 | was_using_prediv = ana[1] & data->ana->fb_prediv_mask; | |
1020 | ||
1021 | ana[0] &= ~data->ana->mask0; | |
1022 | ana[0] |= data->ana->set0; | |
1023 | ana[1] &= ~data->ana->mask1; | |
1024 | ana[1] |= data->ana->set1; | |
1025 | ana[3] &= ~data->ana->mask3; | |
1026 | ana[3] |= data->ana->set3; | |
1027 | ||
1028 | if (was_using_prediv && !use_fb_prediv) { | |
1029 | ana[1] &= ~data->ana->fb_prediv_mask; | |
1030 | do_ana_setup_first = true; | |
1031 | } else if (!was_using_prediv && use_fb_prediv) { | |
1032 | ana[1] |= data->ana->fb_prediv_mask; | |
1033 | do_ana_setup_first = false; | |
1034 | } else { | |
1035 | do_ana_setup_first = true; | |
1036 | } | |
1037 | ||
1038 | /* Unmask the reference clock from the oscillator. */ | |
1039 | cprman_write(cprman, A2W_XOSC_CTRL, | |
1040 | cprman_read(cprman, A2W_XOSC_CTRL) | | |
1041 | data->reference_enable_mask); | |
1042 | ||
1043 | if (do_ana_setup_first) | |
1044 | bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana); | |
1045 | ||
1046 | /* Set the PLL multiplier from the oscillator. */ | |
1047 | cprman_write(cprman, data->frac_reg, fdiv); | |
1048 | ||
1049 | a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg); | |
1050 | a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK; | |
1051 | a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT; | |
1052 | a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK; | |
1053 | a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT; | |
1054 | cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl); | |
1055 | ||
1056 | if (!do_ana_setup_first) | |
1057 | bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana); | |
1058 | ||
1059 | return 0; | |
1060 | } | |
1061 | ||
96bf9c69 MS |
1062 | static int bcm2835_pll_debug_init(struct clk_hw *hw, |
1063 | struct dentry *dentry) | |
1064 | { | |
1065 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
1066 | struct bcm2835_cprman *cprman = pll->cprman; | |
1067 | const struct bcm2835_pll_data *data = pll->data; | |
1068 | struct debugfs_reg32 *regs; | |
1069 | ||
1070 | regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL); | |
1071 | if (!regs) | |
1072 | return -ENOMEM; | |
1073 | ||
1074 | regs[0].name = "cm_ctrl"; | |
1075 | regs[0].offset = data->cm_ctrl_reg; | |
1076 | regs[1].name = "a2w_ctrl"; | |
1077 | regs[1].offset = data->a2w_ctrl_reg; | |
1078 | regs[2].name = "frac"; | |
1079 | regs[2].offset = data->frac_reg; | |
1080 | regs[3].name = "ana0"; | |
1081 | regs[3].offset = data->ana_reg_base + 0 * 4; | |
1082 | regs[4].name = "ana1"; | |
1083 | regs[4].offset = data->ana_reg_base + 1 * 4; | |
1084 | regs[5].name = "ana2"; | |
1085 | regs[5].offset = data->ana_reg_base + 2 * 4; | |
1086 | regs[6].name = "ana3"; | |
1087 | regs[6].offset = data->ana_reg_base + 3 * 4; | |
1088 | ||
1089 | return bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry); | |
1090 | } | |
1091 | ||
41691b88 EA |
1092 | static const struct clk_ops bcm2835_pll_clk_ops = { |
1093 | .is_prepared = bcm2835_pll_is_on, | |
1094 | .prepare = bcm2835_pll_on, | |
1095 | .unprepare = bcm2835_pll_off, | |
1096 | .recalc_rate = bcm2835_pll_get_rate, | |
1097 | .set_rate = bcm2835_pll_set_rate, | |
1098 | .round_rate = bcm2835_pll_round_rate, | |
96bf9c69 | 1099 | .debug_init = bcm2835_pll_debug_init, |
41691b88 EA |
1100 | }; |
1101 | ||
1102 | struct bcm2835_pll_divider { | |
1103 | struct clk_divider div; | |
1104 | struct bcm2835_cprman *cprman; | |
1105 | const struct bcm2835_pll_divider_data *data; | |
1106 | }; | |
1107 | ||
1108 | static struct bcm2835_pll_divider * | |
1109 | bcm2835_pll_divider_from_hw(struct clk_hw *hw) | |
1110 | { | |
1111 | return container_of(hw, struct bcm2835_pll_divider, div.hw); | |
1112 | } | |
1113 | ||
1114 | static int bcm2835_pll_divider_is_on(struct clk_hw *hw) | |
1115 | { | |
1116 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
1117 | struct bcm2835_cprman *cprman = divider->cprman; | |
1118 | const struct bcm2835_pll_divider_data *data = divider->data; | |
1119 | ||
1120 | return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE); | |
1121 | } | |
1122 | ||
1123 | static long bcm2835_pll_divider_round_rate(struct clk_hw *hw, | |
1124 | unsigned long rate, | |
1125 | unsigned long *parent_rate) | |
1126 | { | |
1127 | return clk_divider_ops.round_rate(hw, rate, parent_rate); | |
1128 | } | |
1129 | ||
1130 | static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw, | |
1131 | unsigned long parent_rate) | |
1132 | { | |
79c1e2fc | 1133 | return clk_divider_ops.recalc_rate(hw, parent_rate); |
41691b88 EA |
1134 | } |
1135 | ||
1136 | static void bcm2835_pll_divider_off(struct clk_hw *hw) | |
1137 | { | |
1138 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
1139 | struct bcm2835_cprman *cprman = divider->cprman; | |
1140 | const struct bcm2835_pll_divider_data *data = divider->data; | |
1141 | ||
ec36a5c6 | 1142 | spin_lock(&cprman->regs_lock); |
41691b88 EA |
1143 | cprman_write(cprman, data->cm_reg, |
1144 | (cprman_read(cprman, data->cm_reg) & | |
1145 | ~data->load_mask) | data->hold_mask); | |
1146 | cprman_write(cprman, data->a2w_reg, A2W_PLL_CHANNEL_DISABLE); | |
ec36a5c6 | 1147 | spin_unlock(&cprman->regs_lock); |
41691b88 EA |
1148 | } |
1149 | ||
1150 | static int bcm2835_pll_divider_on(struct clk_hw *hw) | |
1151 | { | |
1152 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
1153 | struct bcm2835_cprman *cprman = divider->cprman; | |
1154 | const struct bcm2835_pll_divider_data *data = divider->data; | |
1155 | ||
ec36a5c6 | 1156 | spin_lock(&cprman->regs_lock); |
41691b88 EA |
1157 | cprman_write(cprman, data->a2w_reg, |
1158 | cprman_read(cprman, data->a2w_reg) & | |
1159 | ~A2W_PLL_CHANNEL_DISABLE); | |
1160 | ||
1161 | cprman_write(cprman, data->cm_reg, | |
1162 | cprman_read(cprman, data->cm_reg) & ~data->hold_mask); | |
ec36a5c6 | 1163 | spin_unlock(&cprman->regs_lock); |
41691b88 EA |
1164 | |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | static int bcm2835_pll_divider_set_rate(struct clk_hw *hw, | |
1169 | unsigned long rate, | |
1170 | unsigned long parent_rate) | |
1171 | { | |
1172 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
1173 | struct bcm2835_cprman *cprman = divider->cprman; | |
1174 | const struct bcm2835_pll_divider_data *data = divider->data; | |
773b3966 | 1175 | u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS; |
41691b88 | 1176 | |
773b3966 EA |
1177 | div = DIV_ROUND_UP_ULL(parent_rate, rate); |
1178 | ||
1179 | div = min(div, max_div); | |
1180 | if (div == max_div) | |
1181 | div = 0; | |
41691b88 | 1182 | |
773b3966 | 1183 | cprman_write(cprman, data->a2w_reg, div); |
41691b88 EA |
1184 | cm = cprman_read(cprman, data->cm_reg); |
1185 | cprman_write(cprman, data->cm_reg, cm | data->load_mask); | |
1186 | cprman_write(cprman, data->cm_reg, cm & ~data->load_mask); | |
1187 | ||
1188 | return 0; | |
1189 | } | |
1190 | ||
96bf9c69 MS |
1191 | static int bcm2835_pll_divider_debug_init(struct clk_hw *hw, |
1192 | struct dentry *dentry) | |
1193 | { | |
1194 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
1195 | struct bcm2835_cprman *cprman = divider->cprman; | |
1196 | const struct bcm2835_pll_divider_data *data = divider->data; | |
1197 | struct debugfs_reg32 *regs; | |
1198 | ||
1199 | regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL); | |
1200 | if (!regs) | |
1201 | return -ENOMEM; | |
1202 | ||
1203 | regs[0].name = "cm"; | |
1204 | regs[0].offset = data->cm_reg; | |
1205 | regs[1].name = "a2w"; | |
1206 | regs[1].offset = data->a2w_reg; | |
1207 | ||
1208 | return bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry); | |
1209 | } | |
1210 | ||
41691b88 EA |
1211 | static const struct clk_ops bcm2835_pll_divider_clk_ops = { |
1212 | .is_prepared = bcm2835_pll_divider_is_on, | |
1213 | .prepare = bcm2835_pll_divider_on, | |
1214 | .unprepare = bcm2835_pll_divider_off, | |
1215 | .recalc_rate = bcm2835_pll_divider_get_rate, | |
1216 | .set_rate = bcm2835_pll_divider_set_rate, | |
1217 | .round_rate = bcm2835_pll_divider_round_rate, | |
96bf9c69 | 1218 | .debug_init = bcm2835_pll_divider_debug_init, |
41691b88 EA |
1219 | }; |
1220 | ||
1221 | /* | |
1222 | * The CM dividers do fixed-point division, so we can't use the | |
1223 | * generic integer divider code like the PLL dividers do (and we can't | |
1224 | * fake it by having some fixed shifts preceding it in the clock tree, | |
1225 | * because we'd run out of bits in a 32-bit unsigned long). | |
1226 | */ | |
1227 | struct bcm2835_clock { | |
1228 | struct clk_hw hw; | |
1229 | struct bcm2835_cprman *cprman; | |
1230 | const struct bcm2835_clock_data *data; | |
1231 | }; | |
1232 | ||
1233 | static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw) | |
1234 | { | |
1235 | return container_of(hw, struct bcm2835_clock, hw); | |
1236 | } | |
1237 | ||
1238 | static int bcm2835_clock_is_on(struct clk_hw *hw) | |
1239 | { | |
1240 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1241 | struct bcm2835_cprman *cprman = clock->cprman; | |
1242 | const struct bcm2835_clock_data *data = clock->data; | |
1243 | ||
1244 | return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0; | |
1245 | } | |
1246 | ||
1247 | static u32 bcm2835_clock_choose_div(struct clk_hw *hw, | |
1248 | unsigned long rate, | |
9c95b32c RP |
1249 | unsigned long parent_rate, |
1250 | bool round_up) | |
41691b88 EA |
1251 | { |
1252 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1253 | const struct bcm2835_clock_data *data = clock->data; | |
9c95b32c RP |
1254 | u32 unused_frac_mask = |
1255 | GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1; | |
41691b88 | 1256 | u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS; |
9c95b32c | 1257 | u64 rem; |
959ca92a | 1258 | u32 div, mindiv, maxdiv; |
41691b88 | 1259 | |
9c95b32c | 1260 | rem = do_div(temp, rate); |
41691b88 EA |
1261 | div = temp; |
1262 | ||
9c95b32c RP |
1263 | /* Round up and mask off the unused bits */ |
1264 | if (round_up && ((div & unused_frac_mask) != 0 || rem != 0)) | |
1265 | div += unused_frac_mask + 1; | |
1266 | div &= ~unused_frac_mask; | |
41691b88 | 1267 | |
959ca92a MS |
1268 | /* different clamping limits apply for a mash clock */ |
1269 | if (data->is_mash_clock) { | |
1270 | /* clamp to min divider of 2 */ | |
1271 | mindiv = 2 << CM_DIV_FRAC_BITS; | |
1272 | /* clamp to the highest possible integer divider */ | |
1273 | maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS; | |
1274 | } else { | |
1275 | /* clamp to min divider of 1 */ | |
1276 | mindiv = 1 << CM_DIV_FRAC_BITS; | |
1277 | /* clamp to the highest possible fractional divider */ | |
1278 | maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1, | |
1279 | CM_DIV_FRAC_BITS - data->frac_bits); | |
1280 | } | |
1281 | ||
1282 | /* apply the clamping limits */ | |
1283 | div = max_t(u32, div, mindiv); | |
1284 | div = min_t(u32, div, maxdiv); | |
41691b88 EA |
1285 | |
1286 | return div; | |
1287 | } | |
1288 | ||
1289 | static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock, | |
1290 | unsigned long parent_rate, | |
1291 | u32 div) | |
1292 | { | |
1293 | const struct bcm2835_clock_data *data = clock->data; | |
1294 | u64 temp; | |
1295 | ||
1296 | /* | |
1297 | * The divisor is a 12.12 fixed point field, but only some of | |
1298 | * the bits are populated in any given clock. | |
1299 | */ | |
1300 | div >>= CM_DIV_FRAC_BITS - data->frac_bits; | |
1301 | div &= (1 << (data->int_bits + data->frac_bits)) - 1; | |
1302 | ||
1303 | if (div == 0) | |
1304 | return 0; | |
1305 | ||
1306 | temp = (u64)parent_rate << data->frac_bits; | |
1307 | ||
1308 | do_div(temp, div); | |
1309 | ||
1310 | return temp; | |
1311 | } | |
1312 | ||
41691b88 EA |
1313 | static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw, |
1314 | unsigned long parent_rate) | |
1315 | { | |
1316 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1317 | struct bcm2835_cprman *cprman = clock->cprman; | |
1318 | const struct bcm2835_clock_data *data = clock->data; | |
1319 | u32 div = cprman_read(cprman, data->div_reg); | |
1320 | ||
1321 | return bcm2835_clock_rate_from_divisor(clock, parent_rate, div); | |
1322 | } | |
1323 | ||
1324 | static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock) | |
1325 | { | |
1326 | struct bcm2835_cprman *cprman = clock->cprman; | |
1327 | const struct bcm2835_clock_data *data = clock->data; | |
1328 | ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); | |
1329 | ||
1330 | while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) { | |
1331 | if (ktime_after(ktime_get(), timeout)) { | |
1332 | dev_err(cprman->dev, "%s: couldn't lock PLL\n", | |
1333 | clk_hw_get_name(&clock->hw)); | |
1334 | return; | |
1335 | } | |
1336 | cpu_relax(); | |
1337 | } | |
1338 | } | |
1339 | ||
1340 | static void bcm2835_clock_off(struct clk_hw *hw) | |
1341 | { | |
1342 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1343 | struct bcm2835_cprman *cprman = clock->cprman; | |
1344 | const struct bcm2835_clock_data *data = clock->data; | |
1345 | ||
1346 | spin_lock(&cprman->regs_lock); | |
1347 | cprman_write(cprman, data->ctl_reg, | |
1348 | cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE); | |
1349 | spin_unlock(&cprman->regs_lock); | |
1350 | ||
1351 | /* BUSY will remain high until the divider completes its cycle. */ | |
1352 | bcm2835_clock_wait_busy(clock); | |
1353 | } | |
1354 | ||
1355 | static int bcm2835_clock_on(struct clk_hw *hw) | |
1356 | { | |
1357 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1358 | struct bcm2835_cprman *cprman = clock->cprman; | |
1359 | const struct bcm2835_clock_data *data = clock->data; | |
1360 | ||
1361 | spin_lock(&cprman->regs_lock); | |
1362 | cprman_write(cprman, data->ctl_reg, | |
1363 | cprman_read(cprman, data->ctl_reg) | | |
1364 | CM_ENABLE | | |
1365 | CM_GATE); | |
1366 | spin_unlock(&cprman->regs_lock); | |
1367 | ||
1368 | return 0; | |
1369 | } | |
1370 | ||
1371 | static int bcm2835_clock_set_rate(struct clk_hw *hw, | |
1372 | unsigned long rate, unsigned long parent_rate) | |
1373 | { | |
1374 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1375 | struct bcm2835_cprman *cprman = clock->cprman; | |
1376 | const struct bcm2835_clock_data *data = clock->data; | |
9c95b32c | 1377 | u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false); |
959ca92a MS |
1378 | u32 ctl; |
1379 | ||
1380 | spin_lock(&cprman->regs_lock); | |
1381 | ||
1382 | /* | |
1383 | * Setting up frac support | |
1384 | * | |
1385 | * In principle it is recommended to stop/start the clock first, | |
1386 | * but as we set CLK_SET_RATE_GATE during registration of the | |
1387 | * clock this requirement should be take care of by the | |
1388 | * clk-framework. | |
1389 | */ | |
1390 | ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC; | |
1391 | ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0; | |
1392 | cprman_write(cprman, data->ctl_reg, ctl); | |
41691b88 EA |
1393 | |
1394 | cprman_write(cprman, data->div_reg, div); | |
1395 | ||
959ca92a MS |
1396 | spin_unlock(&cprman->regs_lock); |
1397 | ||
41691b88 EA |
1398 | return 0; |
1399 | } | |
1400 | ||
6d18b8ad | 1401 | static int bcm2835_clock_determine_rate(struct clk_hw *hw, |
6e1e60da | 1402 | struct clk_rate_request *req) |
6d18b8ad RP |
1403 | { |
1404 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1405 | struct clk_hw *parent, *best_parent = NULL; | |
1406 | unsigned long rate, best_rate = 0; | |
1407 | unsigned long prate, best_prate = 0; | |
1408 | size_t i; | |
1409 | u32 div; | |
1410 | ||
1411 | /* | |
1412 | * Select parent clock that results in the closest but lower rate | |
1413 | */ | |
1414 | for (i = 0; i < clk_hw_get_num_parents(hw); ++i) { | |
1415 | parent = clk_hw_get_parent_by_index(hw, i); | |
1416 | if (!parent) | |
1417 | continue; | |
1418 | prate = clk_hw_get_rate(parent); | |
1419 | div = bcm2835_clock_choose_div(hw, req->rate, prate, true); | |
1420 | rate = bcm2835_clock_rate_from_divisor(clock, prate, div); | |
1421 | if (rate > best_rate && rate <= req->rate) { | |
1422 | best_parent = parent; | |
1423 | best_prate = prate; | |
1424 | best_rate = rate; | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | if (!best_parent) | |
1429 | return -EINVAL; | |
1430 | ||
1431 | req->best_parent_hw = best_parent; | |
1432 | req->best_parent_rate = best_prate; | |
1433 | ||
1434 | req->rate = best_rate; | |
1435 | ||
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index) | |
1440 | { | |
1441 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1442 | struct bcm2835_cprman *cprman = clock->cprman; | |
1443 | const struct bcm2835_clock_data *data = clock->data; | |
1444 | u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK; | |
1445 | ||
1446 | cprman_write(cprman, data->ctl_reg, src); | |
1447 | return 0; | |
1448 | } | |
1449 | ||
1450 | static u8 bcm2835_clock_get_parent(struct clk_hw *hw) | |
1451 | { | |
1452 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1453 | struct bcm2835_cprman *cprman = clock->cprman; | |
1454 | const struct bcm2835_clock_data *data = clock->data; | |
1455 | u32 src = cprman_read(cprman, data->ctl_reg); | |
1456 | ||
1457 | return (src & CM_SRC_MASK) >> CM_SRC_SHIFT; | |
1458 | } | |
1459 | ||
96bf9c69 MS |
1460 | static struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = { |
1461 | { | |
1462 | .name = "ctl", | |
1463 | .offset = 0, | |
1464 | }, | |
1465 | { | |
1466 | .name = "div", | |
1467 | .offset = 4, | |
1468 | }, | |
1469 | }; | |
1470 | ||
1471 | static int bcm2835_clock_debug_init(struct clk_hw *hw, | |
1472 | struct dentry *dentry) | |
1473 | { | |
1474 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1475 | struct bcm2835_cprman *cprman = clock->cprman; | |
1476 | const struct bcm2835_clock_data *data = clock->data; | |
1477 | ||
1478 | return bcm2835_debugfs_regset( | |
1479 | cprman, data->ctl_reg, | |
1480 | bcm2835_debugfs_clock_reg32, | |
1481 | ARRAY_SIZE(bcm2835_debugfs_clock_reg32), | |
1482 | dentry); | |
1483 | } | |
1484 | ||
41691b88 EA |
1485 | static const struct clk_ops bcm2835_clock_clk_ops = { |
1486 | .is_prepared = bcm2835_clock_is_on, | |
1487 | .prepare = bcm2835_clock_on, | |
1488 | .unprepare = bcm2835_clock_off, | |
1489 | .recalc_rate = bcm2835_clock_get_rate, | |
1490 | .set_rate = bcm2835_clock_set_rate, | |
6d18b8ad RP |
1491 | .determine_rate = bcm2835_clock_determine_rate, |
1492 | .set_parent = bcm2835_clock_set_parent, | |
1493 | .get_parent = bcm2835_clock_get_parent, | |
96bf9c69 | 1494 | .debug_init = bcm2835_clock_debug_init, |
41691b88 EA |
1495 | }; |
1496 | ||
1497 | static int bcm2835_vpu_clock_is_on(struct clk_hw *hw) | |
1498 | { | |
1499 | return true; | |
1500 | } | |
1501 | ||
1502 | /* | |
1503 | * The VPU clock can never be disabled (it doesn't have an ENABLE | |
1504 | * bit), so it gets its own set of clock ops. | |
1505 | */ | |
1506 | static const struct clk_ops bcm2835_vpu_clock_clk_ops = { | |
1507 | .is_prepared = bcm2835_vpu_clock_is_on, | |
1508 | .recalc_rate = bcm2835_clock_get_rate, | |
1509 | .set_rate = bcm2835_clock_set_rate, | |
6d18b8ad RP |
1510 | .determine_rate = bcm2835_clock_determine_rate, |
1511 | .set_parent = bcm2835_clock_set_parent, | |
1512 | .get_parent = bcm2835_clock_get_parent, | |
96bf9c69 | 1513 | .debug_init = bcm2835_clock_debug_init, |
41691b88 EA |
1514 | }; |
1515 | ||
1516 | static struct clk *bcm2835_register_pll(struct bcm2835_cprman *cprman, | |
1517 | const struct bcm2835_pll_data *data) | |
1518 | { | |
1519 | struct bcm2835_pll *pll; | |
1520 | struct clk_init_data init; | |
1521 | ||
1522 | memset(&init, 0, sizeof(init)); | |
1523 | ||
1524 | /* All of the PLLs derive from the external oscillator. */ | |
1525 | init.parent_names = &cprman->osc_name; | |
1526 | init.num_parents = 1; | |
1527 | init.name = data->name; | |
1528 | init.ops = &bcm2835_pll_clk_ops; | |
1529 | init.flags = CLK_IGNORE_UNUSED; | |
1530 | ||
1531 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); | |
1532 | if (!pll) | |
1533 | return NULL; | |
1534 | ||
1535 | pll->cprman = cprman; | |
1536 | pll->data = data; | |
1537 | pll->hw.init = &init; | |
1538 | ||
1539 | return devm_clk_register(cprman->dev, &pll->hw); | |
1540 | } | |
1541 | ||
1542 | static struct clk * | |
1543 | bcm2835_register_pll_divider(struct bcm2835_cprman *cprman, | |
1544 | const struct bcm2835_pll_divider_data *data) | |
1545 | { | |
1546 | struct bcm2835_pll_divider *divider; | |
1547 | struct clk_init_data init; | |
1548 | struct clk *clk; | |
1549 | const char *divider_name; | |
1550 | ||
1551 | if (data->fixed_divider != 1) { | |
1552 | divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL, | |
1553 | "%s_prediv", data->name); | |
1554 | if (!divider_name) | |
1555 | return NULL; | |
1556 | } else { | |
1557 | divider_name = data->name; | |
1558 | } | |
1559 | ||
1560 | memset(&init, 0, sizeof(init)); | |
1561 | ||
1562 | init.parent_names = &data->source_pll->name; | |
1563 | init.num_parents = 1; | |
1564 | init.name = divider_name; | |
1565 | init.ops = &bcm2835_pll_divider_clk_ops; | |
1566 | init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED; | |
1567 | ||
1568 | divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL); | |
1569 | if (!divider) | |
1570 | return NULL; | |
1571 | ||
1572 | divider->div.reg = cprman->regs + data->a2w_reg; | |
1573 | divider->div.shift = A2W_PLL_DIV_SHIFT; | |
1574 | divider->div.width = A2W_PLL_DIV_BITS; | |
79c1e2fc | 1575 | divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO; |
41691b88 EA |
1576 | divider->div.lock = &cprman->regs_lock; |
1577 | divider->div.hw.init = &init; | |
1578 | divider->div.table = NULL; | |
1579 | ||
1580 | divider->cprman = cprman; | |
1581 | divider->data = data; | |
1582 | ||
1583 | clk = devm_clk_register(cprman->dev, ÷r->div.hw); | |
1584 | if (IS_ERR(clk)) | |
1585 | return clk; | |
1586 | ||
1587 | /* | |
1588 | * PLLH's channels have a fixed divide by 10 afterwards, which | |
1589 | * is what our consumers are actually using. | |
1590 | */ | |
1591 | if (data->fixed_divider != 1) { | |
1592 | return clk_register_fixed_factor(cprman->dev, data->name, | |
1593 | divider_name, | |
1594 | CLK_SET_RATE_PARENT, | |
1595 | 1, | |
1596 | data->fixed_divider); | |
1597 | } | |
1598 | ||
1599 | return clk; | |
1600 | } | |
1601 | ||
1602 | static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman, | |
1603 | const struct bcm2835_clock_data *data) | |
1604 | { | |
1605 | struct bcm2835_clock *clock; | |
1606 | struct clk_init_data init; | |
6d18b8ad RP |
1607 | const char *parents[1 << CM_SRC_BITS]; |
1608 | size_t i; | |
41691b88 EA |
1609 | |
1610 | /* | |
6d18b8ad RP |
1611 | * Replace our "xosc" references with the oscillator's |
1612 | * actual name. | |
41691b88 | 1613 | */ |
6d18b8ad RP |
1614 | for (i = 0; i < data->num_mux_parents; i++) { |
1615 | if (strcmp(data->parents[i], "xosc") == 0) | |
1616 | parents[i] = cprman->osc_name; | |
1617 | else | |
1618 | parents[i] = data->parents[i]; | |
41691b88 EA |
1619 | } |
1620 | ||
1621 | memset(&init, 0, sizeof(init)); | |
6d18b8ad RP |
1622 | init.parent_names = parents; |
1623 | init.num_parents = data->num_mux_parents; | |
41691b88 EA |
1624 | init.name = data->name; |
1625 | init.flags = CLK_IGNORE_UNUSED; | |
1626 | ||
1627 | if (data->is_vpu_clock) { | |
1628 | init.ops = &bcm2835_vpu_clock_clk_ops; | |
1629 | } else { | |
1630 | init.ops = &bcm2835_clock_clk_ops; | |
1631 | init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE; | |
1632 | } | |
1633 | ||
1634 | clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL); | |
1635 | if (!clock) | |
1636 | return NULL; | |
1637 | ||
1638 | clock->cprman = cprman; | |
1639 | clock->data = data; | |
1640 | clock->hw.init = &init; | |
1641 | ||
1642 | return devm_clk_register(cprman->dev, &clock->hw); | |
1643 | } | |
1644 | ||
1645 | static int bcm2835_clk_probe(struct platform_device *pdev) | |
1646 | { | |
1647 | struct device *dev = &pdev->dev; | |
1648 | struct clk **clks; | |
1649 | struct bcm2835_cprman *cprman; | |
1650 | struct resource *res; | |
1651 | ||
1652 | cprman = devm_kzalloc(dev, sizeof(*cprman), GFP_KERNEL); | |
1653 | if (!cprman) | |
1654 | return -ENOMEM; | |
1655 | ||
1656 | spin_lock_init(&cprman->regs_lock); | |
1657 | cprman->dev = dev; | |
1658 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1659 | cprman->regs = devm_ioremap_resource(dev, res); | |
1660 | if (IS_ERR(cprman->regs)) | |
1661 | return PTR_ERR(cprman->regs); | |
1662 | ||
1663 | cprman->osc_name = of_clk_get_parent_name(dev->of_node, 0); | |
1664 | if (!cprman->osc_name) | |
1665 | return -ENODEV; | |
1666 | ||
1667 | platform_set_drvdata(pdev, cprman); | |
1668 | ||
1669 | cprman->onecell.clk_num = BCM2835_CLOCK_COUNT; | |
1670 | cprman->onecell.clks = cprman->clks; | |
1671 | clks = cprman->clks; | |
1672 | ||
1673 | clks[BCM2835_PLLA] = bcm2835_register_pll(cprman, &bcm2835_plla_data); | |
1674 | clks[BCM2835_PLLB] = bcm2835_register_pll(cprman, &bcm2835_pllb_data); | |
1675 | clks[BCM2835_PLLC] = bcm2835_register_pll(cprman, &bcm2835_pllc_data); | |
1676 | clks[BCM2835_PLLD] = bcm2835_register_pll(cprman, &bcm2835_plld_data); | |
1677 | clks[BCM2835_PLLH] = bcm2835_register_pll(cprman, &bcm2835_pllh_data); | |
1678 | ||
1679 | clks[BCM2835_PLLA_CORE] = | |
1680 | bcm2835_register_pll_divider(cprman, &bcm2835_plla_core_data); | |
1681 | clks[BCM2835_PLLA_PER] = | |
1682 | bcm2835_register_pll_divider(cprman, &bcm2835_plla_per_data); | |
1683 | clks[BCM2835_PLLC_CORE0] = | |
1684 | bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core0_data); | |
1685 | clks[BCM2835_PLLC_CORE1] = | |
1686 | bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core1_data); | |
1687 | clks[BCM2835_PLLC_CORE2] = | |
1688 | bcm2835_register_pll_divider(cprman, &bcm2835_pllc_core2_data); | |
1689 | clks[BCM2835_PLLC_PER] = | |
1690 | bcm2835_register_pll_divider(cprman, &bcm2835_pllc_per_data); | |
1691 | clks[BCM2835_PLLD_CORE] = | |
1692 | bcm2835_register_pll_divider(cprman, &bcm2835_plld_core_data); | |
1693 | clks[BCM2835_PLLD_PER] = | |
1694 | bcm2835_register_pll_divider(cprman, &bcm2835_plld_per_data); | |
1695 | clks[BCM2835_PLLH_RCAL] = | |
1696 | bcm2835_register_pll_divider(cprman, &bcm2835_pllh_rcal_data); | |
1697 | clks[BCM2835_PLLH_AUX] = | |
1698 | bcm2835_register_pll_divider(cprman, &bcm2835_pllh_aux_data); | |
1699 | clks[BCM2835_PLLH_PIX] = | |
1700 | bcm2835_register_pll_divider(cprman, &bcm2835_pllh_pix_data); | |
1701 | ||
1702 | clks[BCM2835_CLOCK_TIMER] = | |
1703 | bcm2835_register_clock(cprman, &bcm2835_clock_timer_data); | |
1704 | clks[BCM2835_CLOCK_OTP] = | |
1705 | bcm2835_register_clock(cprman, &bcm2835_clock_otp_data); | |
1706 | clks[BCM2835_CLOCK_TSENS] = | |
1707 | bcm2835_register_clock(cprman, &bcm2835_clock_tsens_data); | |
1708 | clks[BCM2835_CLOCK_VPU] = | |
1709 | bcm2835_register_clock(cprman, &bcm2835_clock_vpu_data); | |
1710 | clks[BCM2835_CLOCK_V3D] = | |
1711 | bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data); | |
1712 | clks[BCM2835_CLOCK_ISP] = | |
1713 | bcm2835_register_clock(cprman, &bcm2835_clock_isp_data); | |
1714 | clks[BCM2835_CLOCK_H264] = | |
1715 | bcm2835_register_clock(cprman, &bcm2835_clock_h264_data); | |
1716 | clks[BCM2835_CLOCK_V3D] = | |
1717 | bcm2835_register_clock(cprman, &bcm2835_clock_v3d_data); | |
1718 | clks[BCM2835_CLOCK_SDRAM] = | |
1719 | bcm2835_register_clock(cprman, &bcm2835_clock_sdram_data); | |
1720 | clks[BCM2835_CLOCK_UART] = | |
1721 | bcm2835_register_clock(cprman, &bcm2835_clock_uart_data); | |
1722 | clks[BCM2835_CLOCK_VEC] = | |
1723 | bcm2835_register_clock(cprman, &bcm2835_clock_vec_data); | |
1724 | clks[BCM2835_CLOCK_HSM] = | |
1725 | bcm2835_register_clock(cprman, &bcm2835_clock_hsm_data); | |
1726 | clks[BCM2835_CLOCK_EMMC] = | |
1727 | bcm2835_register_clock(cprman, &bcm2835_clock_emmc_data); | |
1728 | ||
1729 | /* | |
1730 | * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if | |
1731 | * you have the debug bit set in the power manager, which we | |
1732 | * don't bother exposing) are individual gates off of the | |
1733 | * non-stop vpu clock. | |
1734 | */ | |
1735 | clks[BCM2835_CLOCK_PERI_IMAGE] = | |
1736 | clk_register_gate(dev, "peri_image", "vpu", | |
1737 | CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE, | |
1738 | cprman->regs + CM_PERIICTL, CM_GATE_BIT, | |
1739 | 0, &cprman->regs_lock); | |
1740 | ||
cfbab8fb RP |
1741 | clks[BCM2835_CLOCK_PWM] = |
1742 | bcm2835_register_clock(cprman, &bcm2835_clock_pwm_data); | |
1743 | ||
41691b88 EA |
1744 | return of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, |
1745 | &cprman->onecell); | |
1746 | } | |
1747 | ||
1748 | static const struct of_device_id bcm2835_clk_of_match[] = { | |
1749 | { .compatible = "brcm,bcm2835-cprman", }, | |
1750 | {} | |
1751 | }; | |
1752 | MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match); | |
1753 | ||
1754 | static struct platform_driver bcm2835_clk_driver = { | |
1755 | .driver = { | |
1756 | .name = "bcm2835-clk", | |
1757 | .of_match_table = bcm2835_clk_of_match, | |
1758 | }, | |
1759 | .probe = bcm2835_clk_probe, | |
1760 | }; | |
1761 | ||
1762 | builtin_platform_driver(bcm2835_clk_driver); | |
1763 | ||
1764 | MODULE_AUTHOR("Eric Anholt <eric@anholt.net>"); | |
1765 | MODULE_DESCRIPTION("BCM2835 clock driver"); | |
1766 | MODULE_LICENSE("GPL v2"); |