]>
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> | |
9e400c5c | 39 | #include <linux/clk.h> |
75fabc3f | 40 | #include <linux/clk/bcm2835.h> |
96bf9c69 | 41 | #include <linux/debugfs.h> |
1c97aa6c | 42 | #include <linux/delay.h> |
41691b88 | 43 | #include <linux/module.h> |
526d239c | 44 | #include <linux/of.h> |
41691b88 EA |
45 | #include <linux/platform_device.h> |
46 | #include <linux/slab.h> | |
47 | #include <dt-bindings/clock/bcm2835.h> | |
11262d0f | 48 | #include <soc/bcm2835/raspberrypi-firmware.h> |
41691b88 EA |
49 | |
50 | #define CM_PASSWORD 0x5a000000 | |
51 | ||
52 | #define CM_GNRICCTL 0x000 | |
53 | #define CM_GNRICDIV 0x004 | |
54 | # define CM_DIV_FRAC_BITS 12 | |
959ca92a | 55 | # define CM_DIV_FRAC_MASK GENMASK(CM_DIV_FRAC_BITS - 1, 0) |
41691b88 EA |
56 | |
57 | #define CM_VPUCTL 0x008 | |
58 | #define CM_VPUDIV 0x00c | |
59 | #define CM_SYSCTL 0x010 | |
60 | #define CM_SYSDIV 0x014 | |
61 | #define CM_PERIACTL 0x018 | |
62 | #define CM_PERIADIV 0x01c | |
63 | #define CM_PERIICTL 0x020 | |
64 | #define CM_PERIIDIV 0x024 | |
65 | #define CM_H264CTL 0x028 | |
66 | #define CM_H264DIV 0x02c | |
67 | #define CM_ISPCTL 0x030 | |
68 | #define CM_ISPDIV 0x034 | |
69 | #define CM_V3DCTL 0x038 | |
70 | #define CM_V3DDIV 0x03c | |
71 | #define CM_CAM0CTL 0x040 | |
72 | #define CM_CAM0DIV 0x044 | |
73 | #define CM_CAM1CTL 0x048 | |
74 | #define CM_CAM1DIV 0x04c | |
75 | #define CM_CCP2CTL 0x050 | |
76 | #define CM_CCP2DIV 0x054 | |
77 | #define CM_DSI0ECTL 0x058 | |
78 | #define CM_DSI0EDIV 0x05c | |
79 | #define CM_DSI0PCTL 0x060 | |
80 | #define CM_DSI0PDIV 0x064 | |
81 | #define CM_DPICTL 0x068 | |
82 | #define CM_DPIDIV 0x06c | |
83 | #define CM_GP0CTL 0x070 | |
84 | #define CM_GP0DIV 0x074 | |
85 | #define CM_GP1CTL 0x078 | |
86 | #define CM_GP1DIV 0x07c | |
87 | #define CM_GP2CTL 0x080 | |
88 | #define CM_GP2DIV 0x084 | |
89 | #define CM_HSMCTL 0x088 | |
90 | #define CM_HSMDIV 0x08c | |
91 | #define CM_OTPCTL 0x090 | |
92 | #define CM_OTPDIV 0x094 | |
2103a215 MS |
93 | #define CM_PCMCTL 0x098 |
94 | #define CM_PCMDIV 0x09c | |
41691b88 EA |
95 | #define CM_PWMCTL 0x0a0 |
96 | #define CM_PWMDIV 0x0a4 | |
2103a215 MS |
97 | #define CM_SLIMCTL 0x0a8 |
98 | #define CM_SLIMDIV 0x0ac | |
41691b88 EA |
99 | #define CM_SMICTL 0x0b0 |
100 | #define CM_SMIDIV 0x0b4 | |
2103a215 MS |
101 | /* no definition for 0x0b8 and 0x0bc */ |
102 | #define CM_TCNTCTL 0x0c0 | |
1c97aa6c EA |
103 | # define CM_TCNT_SRC1_SHIFT 12 |
104 | #define CM_TCNTCNT 0x0c4 | |
2103a215 MS |
105 | #define CM_TECCTL 0x0c8 |
106 | #define CM_TECDIV 0x0cc | |
107 | #define CM_TD0CTL 0x0d0 | |
108 | #define CM_TD0DIV 0x0d4 | |
109 | #define CM_TD1CTL 0x0d8 | |
110 | #define CM_TD1DIV 0x0dc | |
41691b88 EA |
111 | #define CM_TSENSCTL 0x0e0 |
112 | #define CM_TSENSDIV 0x0e4 | |
113 | #define CM_TIMERCTL 0x0e8 | |
114 | #define CM_TIMERDIV 0x0ec | |
115 | #define CM_UARTCTL 0x0f0 | |
116 | #define CM_UARTDIV 0x0f4 | |
117 | #define CM_VECCTL 0x0f8 | |
118 | #define CM_VECDIV 0x0fc | |
119 | #define CM_PULSECTL 0x190 | |
120 | #define CM_PULSEDIV 0x194 | |
121 | #define CM_SDCCTL 0x1a8 | |
122 | #define CM_SDCDIV 0x1ac | |
123 | #define CM_ARMCTL 0x1b0 | |
d3d6f15f MS |
124 | #define CM_AVEOCTL 0x1b8 |
125 | #define CM_AVEODIV 0x1bc | |
41691b88 EA |
126 | #define CM_EMMCCTL 0x1c0 |
127 | #define CM_EMMCDIV 0x1c4 | |
128 | ||
129 | /* General bits for the CM_*CTL regs */ | |
130 | # define CM_ENABLE BIT(4) | |
131 | # define CM_KILL BIT(5) | |
132 | # define CM_GATE_BIT 6 | |
133 | # define CM_GATE BIT(CM_GATE_BIT) | |
134 | # define CM_BUSY BIT(7) | |
135 | # define CM_BUSYD BIT(8) | |
959ca92a | 136 | # define CM_FRAC BIT(9) |
41691b88 EA |
137 | # define CM_SRC_SHIFT 0 |
138 | # define CM_SRC_BITS 4 | |
139 | # define CM_SRC_MASK 0xf | |
140 | # define CM_SRC_GND 0 | |
141 | # define CM_SRC_OSC 1 | |
142 | # define CM_SRC_TESTDEBUG0 2 | |
143 | # define CM_SRC_TESTDEBUG1 3 | |
144 | # define CM_SRC_PLLA_CORE 4 | |
145 | # define CM_SRC_PLLA_PER 4 | |
146 | # define CM_SRC_PLLC_CORE0 5 | |
147 | # define CM_SRC_PLLC_PER 5 | |
148 | # define CM_SRC_PLLC_CORE1 8 | |
149 | # define CM_SRC_PLLD_CORE 6 | |
150 | # define CM_SRC_PLLD_PER 6 | |
151 | # define CM_SRC_PLLH_AUX 7 | |
152 | # define CM_SRC_PLLC_CORE1 8 | |
153 | # define CM_SRC_PLLC_CORE2 9 | |
154 | ||
155 | #define CM_OSCCOUNT 0x100 | |
156 | ||
157 | #define CM_PLLA 0x104 | |
158 | # define CM_PLL_ANARST BIT(8) | |
159 | # define CM_PLLA_HOLDPER BIT(7) | |
160 | # define CM_PLLA_LOADPER BIT(6) | |
161 | # define CM_PLLA_HOLDCORE BIT(5) | |
162 | # define CM_PLLA_LOADCORE BIT(4) | |
163 | # define CM_PLLA_HOLDCCP2 BIT(3) | |
164 | # define CM_PLLA_LOADCCP2 BIT(2) | |
165 | # define CM_PLLA_HOLDDSI0 BIT(1) | |
166 | # define CM_PLLA_LOADDSI0 BIT(0) | |
167 | ||
168 | #define CM_PLLC 0x108 | |
169 | # define CM_PLLC_HOLDPER BIT(7) | |
170 | # define CM_PLLC_LOADPER BIT(6) | |
171 | # define CM_PLLC_HOLDCORE2 BIT(5) | |
172 | # define CM_PLLC_LOADCORE2 BIT(4) | |
173 | # define CM_PLLC_HOLDCORE1 BIT(3) | |
174 | # define CM_PLLC_LOADCORE1 BIT(2) | |
175 | # define CM_PLLC_HOLDCORE0 BIT(1) | |
176 | # define CM_PLLC_LOADCORE0 BIT(0) | |
177 | ||
178 | #define CM_PLLD 0x10c | |
179 | # define CM_PLLD_HOLDPER BIT(7) | |
180 | # define CM_PLLD_LOADPER BIT(6) | |
181 | # define CM_PLLD_HOLDCORE BIT(5) | |
182 | # define CM_PLLD_LOADCORE BIT(4) | |
183 | # define CM_PLLD_HOLDDSI1 BIT(3) | |
184 | # define CM_PLLD_LOADDSI1 BIT(2) | |
185 | # define CM_PLLD_HOLDDSI0 BIT(1) | |
186 | # define CM_PLLD_LOADDSI0 BIT(0) | |
187 | ||
188 | #define CM_PLLH 0x110 | |
189 | # define CM_PLLH_LOADRCAL BIT(2) | |
190 | # define CM_PLLH_LOADAUX BIT(1) | |
191 | # define CM_PLLH_LOADPIX BIT(0) | |
192 | ||
193 | #define CM_LOCK 0x114 | |
194 | # define CM_LOCK_FLOCKH BIT(12) | |
195 | # define CM_LOCK_FLOCKD BIT(11) | |
196 | # define CM_LOCK_FLOCKC BIT(10) | |
197 | # define CM_LOCK_FLOCKB BIT(9) | |
198 | # define CM_LOCK_FLOCKA BIT(8) | |
199 | ||
200 | #define CM_EVENT 0x118 | |
201 | #define CM_DSI1ECTL 0x158 | |
202 | #define CM_DSI1EDIV 0x15c | |
203 | #define CM_DSI1PCTL 0x160 | |
204 | #define CM_DSI1PDIV 0x164 | |
205 | #define CM_DFTCTL 0x168 | |
206 | #define CM_DFTDIV 0x16c | |
207 | ||
208 | #define CM_PLLB 0x170 | |
209 | # define CM_PLLB_HOLDARM BIT(1) | |
210 | # define CM_PLLB_LOADARM BIT(0) | |
211 | ||
212 | #define A2W_PLLA_CTRL 0x1100 | |
213 | #define A2W_PLLC_CTRL 0x1120 | |
214 | #define A2W_PLLD_CTRL 0x1140 | |
215 | #define A2W_PLLH_CTRL 0x1160 | |
216 | #define A2W_PLLB_CTRL 0x11e0 | |
217 | # define A2W_PLL_CTRL_PRST_DISABLE BIT(17) | |
218 | # define A2W_PLL_CTRL_PWRDN BIT(16) | |
219 | # define A2W_PLL_CTRL_PDIV_MASK 0x000007000 | |
220 | # define A2W_PLL_CTRL_PDIV_SHIFT 12 | |
221 | # define A2W_PLL_CTRL_NDIV_MASK 0x0000003ff | |
222 | # define A2W_PLL_CTRL_NDIV_SHIFT 0 | |
223 | ||
224 | #define A2W_PLLA_ANA0 0x1010 | |
225 | #define A2W_PLLC_ANA0 0x1030 | |
226 | #define A2W_PLLD_ANA0 0x1050 | |
227 | #define A2W_PLLH_ANA0 0x1070 | |
228 | #define A2W_PLLB_ANA0 0x10f0 | |
229 | ||
230 | #define A2W_PLL_KA_SHIFT 7 | |
231 | #define A2W_PLL_KA_MASK GENMASK(9, 7) | |
232 | #define A2W_PLL_KI_SHIFT 19 | |
233 | #define A2W_PLL_KI_MASK GENMASK(21, 19) | |
234 | #define A2W_PLL_KP_SHIFT 15 | |
235 | #define A2W_PLL_KP_MASK GENMASK(18, 15) | |
236 | ||
237 | #define A2W_PLLH_KA_SHIFT 19 | |
238 | #define A2W_PLLH_KA_MASK GENMASK(21, 19) | |
239 | #define A2W_PLLH_KI_LOW_SHIFT 22 | |
240 | #define A2W_PLLH_KI_LOW_MASK GENMASK(23, 22) | |
241 | #define A2W_PLLH_KI_HIGH_SHIFT 0 | |
242 | #define A2W_PLLH_KI_HIGH_MASK GENMASK(0, 0) | |
243 | #define A2W_PLLH_KP_SHIFT 1 | |
244 | #define A2W_PLLH_KP_MASK GENMASK(4, 1) | |
245 | ||
246 | #define A2W_XOSC_CTRL 0x1190 | |
247 | # define A2W_XOSC_CTRL_PLLB_ENABLE BIT(7) | |
248 | # define A2W_XOSC_CTRL_PLLA_ENABLE BIT(6) | |
249 | # define A2W_XOSC_CTRL_PLLD_ENABLE BIT(5) | |
250 | # define A2W_XOSC_CTRL_DDR_ENABLE BIT(4) | |
251 | # define A2W_XOSC_CTRL_CPR1_ENABLE BIT(3) | |
252 | # define A2W_XOSC_CTRL_USB_ENABLE BIT(2) | |
253 | # define A2W_XOSC_CTRL_HDMI_ENABLE BIT(1) | |
254 | # define A2W_XOSC_CTRL_PLLC_ENABLE BIT(0) | |
255 | ||
256 | #define A2W_PLLA_FRAC 0x1200 | |
257 | #define A2W_PLLC_FRAC 0x1220 | |
258 | #define A2W_PLLD_FRAC 0x1240 | |
259 | #define A2W_PLLH_FRAC 0x1260 | |
260 | #define A2W_PLLB_FRAC 0x12e0 | |
261 | # define A2W_PLL_FRAC_MASK ((1 << A2W_PLL_FRAC_BITS) - 1) | |
262 | # define A2W_PLL_FRAC_BITS 20 | |
263 | ||
264 | #define A2W_PLL_CHANNEL_DISABLE BIT(8) | |
265 | #define A2W_PLL_DIV_BITS 8 | |
266 | #define A2W_PLL_DIV_SHIFT 0 | |
267 | ||
268 | #define A2W_PLLA_DSI0 0x1300 | |
269 | #define A2W_PLLA_CORE 0x1400 | |
270 | #define A2W_PLLA_PER 0x1500 | |
271 | #define A2W_PLLA_CCP2 0x1600 | |
272 | ||
273 | #define A2W_PLLC_CORE2 0x1320 | |
274 | #define A2W_PLLC_CORE1 0x1420 | |
275 | #define A2W_PLLC_PER 0x1520 | |
276 | #define A2W_PLLC_CORE0 0x1620 | |
277 | ||
278 | #define A2W_PLLD_DSI0 0x1340 | |
279 | #define A2W_PLLD_CORE 0x1440 | |
280 | #define A2W_PLLD_PER 0x1540 | |
281 | #define A2W_PLLD_DSI1 0x1640 | |
282 | ||
283 | #define A2W_PLLH_AUX 0x1360 | |
284 | #define A2W_PLLH_RCAL 0x1460 | |
285 | #define A2W_PLLH_PIX 0x1560 | |
286 | #define A2W_PLLH_STS 0x1660 | |
287 | ||
288 | #define A2W_PLLH_CTRLR 0x1960 | |
289 | #define A2W_PLLH_FRACR 0x1a60 | |
290 | #define A2W_PLLH_AUXR 0x1b60 | |
291 | #define A2W_PLLH_RCALR 0x1c60 | |
292 | #define A2W_PLLH_PIXR 0x1d60 | |
293 | #define A2W_PLLH_STSR 0x1e60 | |
294 | ||
295 | #define A2W_PLLB_ARM 0x13e0 | |
296 | #define A2W_PLLB_SP0 0x14e0 | |
297 | #define A2W_PLLB_SP1 0x15e0 | |
298 | #define A2W_PLLB_SP2 0x16e0 | |
299 | ||
300 | #define LOCK_TIMEOUT_NS 100000000 | |
301 | #define BCM2835_MAX_FB_RATE 1750000000u | |
302 | ||
11262d0f PE |
303 | #define VCMSG_ID_CORE_CLOCK 4 |
304 | ||
78b200b9 EA |
305 | /* |
306 | * Names of clocks used within the driver that need to be replaced | |
307 | * with an external parent's name. This array is in the order that | |
308 | * the clocks node in the DT references external clocks. | |
309 | */ | |
310 | static const char *const cprman_parent_names[] = { | |
311 | "xosc", | |
312 | "dsi0_byte", | |
313 | "dsi0_ddr2", | |
314 | "dsi0_ddr", | |
315 | "dsi1_byte", | |
316 | "dsi1_ddr2", | |
317 | "dsi1_ddr", | |
318 | }; | |
319 | ||
41691b88 EA |
320 | struct bcm2835_cprman { |
321 | struct device *dev; | |
322 | void __iomem *regs; | |
11262d0f | 323 | struct rpi_firmware *fw; |
6e1e60da | 324 | spinlock_t regs_lock; /* spinlock for all clocks */ |
78b200b9 EA |
325 | |
326 | /* | |
327 | * Real names of cprman clock parents looked up through | |
328 | * of_clk_get_parent_name(), which will be used in the | |
329 | * parent_names[] arrays for clock registration. | |
330 | */ | |
331 | const char *real_parent_names[ARRAY_SIZE(cprman_parent_names)]; | |
41691b88 | 332 | |
b19f009d SB |
333 | /* Must be last */ |
334 | struct clk_hw_onecell_data onecell; | |
41691b88 EA |
335 | }; |
336 | ||
337 | static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val) | |
338 | { | |
339 | writel(CM_PASSWORD | val, cprman->regs + reg); | |
340 | } | |
341 | ||
342 | static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg) | |
343 | { | |
344 | return readl(cprman->regs + reg); | |
345 | } | |
526d239c | 346 | |
1c97aa6c EA |
347 | /* Does a cycle of measuring a clock through the TCNT clock, which may |
348 | * source from many other clocks in the system. | |
349 | */ | |
350 | static unsigned long bcm2835_measure_tcnt_mux(struct bcm2835_cprman *cprman, | |
351 | u32 tcnt_mux) | |
352 | { | |
353 | u32 osccount = 19200; /* 1ms */ | |
354 | u32 count; | |
355 | ktime_t timeout; | |
356 | ||
357 | spin_lock(&cprman->regs_lock); | |
358 | ||
359 | cprman_write(cprman, CM_TCNTCTL, CM_KILL); | |
360 | ||
361 | cprman_write(cprman, CM_TCNTCTL, | |
362 | (tcnt_mux & CM_SRC_MASK) | | |
363 | (tcnt_mux >> CM_SRC_BITS) << CM_TCNT_SRC1_SHIFT); | |
364 | ||
365 | cprman_write(cprman, CM_OSCCOUNT, osccount); | |
366 | ||
367 | /* do a kind delay at the start */ | |
368 | mdelay(1); | |
369 | ||
370 | /* Finish off whatever is left of OSCCOUNT */ | |
371 | timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); | |
372 | while (cprman_read(cprman, CM_OSCCOUNT)) { | |
373 | if (ktime_after(ktime_get(), timeout)) { | |
374 | dev_err(cprman->dev, "timeout waiting for OSCCOUNT\n"); | |
375 | count = 0; | |
376 | goto out; | |
377 | } | |
378 | cpu_relax(); | |
379 | } | |
380 | ||
381 | /* Wait for BUSY to clear. */ | |
382 | timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); | |
383 | while (cprman_read(cprman, CM_TCNTCTL) & CM_BUSY) { | |
384 | if (ktime_after(ktime_get(), timeout)) { | |
385 | dev_err(cprman->dev, "timeout waiting for !BUSY\n"); | |
386 | count = 0; | |
387 | goto out; | |
388 | } | |
389 | cpu_relax(); | |
390 | } | |
391 | ||
392 | count = cprman_read(cprman, CM_TCNTCNT); | |
393 | ||
394 | cprman_write(cprman, CM_TCNTCTL, 0); | |
395 | ||
396 | out: | |
397 | spin_unlock(&cprman->regs_lock); | |
398 | ||
399 | return count * 1000; | |
400 | } | |
401 | ||
96bf9c69 MS |
402 | static int bcm2835_debugfs_regset(struct bcm2835_cprman *cprman, u32 base, |
403 | struct debugfs_reg32 *regs, size_t nregs, | |
404 | struct dentry *dentry) | |
405 | { | |
406 | struct dentry *regdump; | |
407 | struct debugfs_regset32 *regset; | |
408 | ||
409 | regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL); | |
410 | if (!regset) | |
411 | return -ENOMEM; | |
412 | ||
413 | regset->regs = regs; | |
414 | regset->nregs = nregs; | |
415 | regset->base = cprman->regs + base; | |
416 | ||
417 | regdump = debugfs_create_regset32("regdump", S_IRUGO, dentry, | |
418 | regset); | |
419 | ||
420 | return regdump ? 0 : -ENOMEM; | |
421 | } | |
422 | ||
75fabc3f SA |
423 | /* |
424 | * These are fixed clocks. They're probably not all root clocks and it may | |
425 | * be possible to turn them on and off but until this is mapped out better | |
426 | * it's the only way they can be used. | |
427 | */ | |
428 | void __init bcm2835_init_clocks(void) | |
429 | { | |
b19f009d | 430 | struct clk_hw *hw; |
75fabc3f SA |
431 | int ret; |
432 | ||
b19f009d SB |
433 | hw = clk_hw_register_fixed_rate(NULL, "apb_pclk", NULL, 0, 126000000); |
434 | if (IS_ERR(hw)) | |
75fabc3f SA |
435 | pr_err("apb_pclk not registered\n"); |
436 | ||
b19f009d SB |
437 | hw = clk_hw_register_fixed_rate(NULL, "uart0_pclk", NULL, 0, 3000000); |
438 | if (IS_ERR(hw)) | |
75fabc3f | 439 | pr_err("uart0_pclk not registered\n"); |
b19f009d | 440 | ret = clk_hw_register_clkdev(hw, NULL, "20201000.uart"); |
75fabc3f SA |
441 | if (ret) |
442 | pr_err("uart0_pclk alias not registered\n"); | |
443 | ||
b19f009d SB |
444 | hw = clk_hw_register_fixed_rate(NULL, "uart1_pclk", NULL, 0, 125000000); |
445 | if (IS_ERR(hw)) | |
75fabc3f | 446 | pr_err("uart1_pclk not registered\n"); |
b19f009d | 447 | ret = clk_hw_register_clkdev(hw, NULL, "20215000.uart"); |
75fabc3f | 448 | if (ret) |
686ea585 | 449 | pr_err("uart1_pclk alias not registered\n"); |
75fabc3f | 450 | } |
41691b88 EA |
451 | |
452 | struct bcm2835_pll_data { | |
453 | const char *name; | |
454 | u32 cm_ctrl_reg; | |
455 | u32 a2w_ctrl_reg; | |
456 | u32 frac_reg; | |
457 | u32 ana_reg_base; | |
458 | u32 reference_enable_mask; | |
459 | /* Bit in CM_LOCK to indicate when the PLL has locked. */ | |
460 | u32 lock_mask; | |
461 | ||
462 | const struct bcm2835_pll_ana_bits *ana; | |
463 | ||
464 | unsigned long min_rate; | |
465 | unsigned long max_rate; | |
466 | /* | |
467 | * Highest rate for the VCO before we have to use the | |
468 | * pre-divide-by-2. | |
469 | */ | |
470 | unsigned long max_fb_rate; | |
471 | }; | |
472 | ||
473 | struct bcm2835_pll_ana_bits { | |
474 | u32 mask0; | |
475 | u32 set0; | |
476 | u32 mask1; | |
477 | u32 set1; | |
478 | u32 mask3; | |
479 | u32 set3; | |
480 | u32 fb_prediv_mask; | |
481 | }; | |
482 | ||
483 | static const struct bcm2835_pll_ana_bits bcm2835_ana_default = { | |
484 | .mask0 = 0, | |
485 | .set0 = 0, | |
286259ef | 486 | .mask1 = (u32)~(A2W_PLL_KI_MASK | A2W_PLL_KP_MASK), |
41691b88 | 487 | .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT), |
286259ef | 488 | .mask3 = (u32)~A2W_PLL_KA_MASK, |
41691b88 EA |
489 | .set3 = (2 << A2W_PLL_KA_SHIFT), |
490 | .fb_prediv_mask = BIT(14), | |
491 | }; | |
492 | ||
493 | static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = { | |
286259ef | 494 | .mask0 = (u32)~(A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK), |
41691b88 | 495 | .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT), |
286259ef | 496 | .mask1 = (u32)~(A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK), |
41691b88 EA |
497 | .set1 = (6 << A2W_PLLH_KP_SHIFT), |
498 | .mask3 = 0, | |
499 | .set3 = 0, | |
500 | .fb_prediv_mask = BIT(11), | |
501 | }; | |
502 | ||
41691b88 EA |
503 | struct bcm2835_pll_divider_data { |
504 | const char *name; | |
3b15afef MS |
505 | const char *source_pll; |
506 | ||
41691b88 EA |
507 | u32 cm_reg; |
508 | u32 a2w_reg; | |
509 | ||
510 | u32 load_mask; | |
511 | u32 hold_mask; | |
512 | u32 fixed_divider; | |
5678dd2d | 513 | u32 flags; |
41691b88 EA |
514 | }; |
515 | ||
41691b88 EA |
516 | struct bcm2835_clock_data { |
517 | const char *name; | |
518 | ||
519 | const char *const *parents; | |
520 | int num_mux_parents; | |
521 | ||
155e8b3b BB |
522 | /* Bitmap encoding which parents accept rate change propagation. */ |
523 | unsigned int set_rate_parent; | |
524 | ||
41691b88 EA |
525 | u32 ctl_reg; |
526 | u32 div_reg; | |
527 | ||
528 | /* Number of integer bits in the divider */ | |
529 | u32 int_bits; | |
530 | /* Number of fractional bits in the divider */ | |
531 | u32 frac_bits; | |
532 | ||
e69fdcca EA |
533 | u32 flags; |
534 | ||
41691b88 | 535 | bool is_vpu_clock; |
959ca92a | 536 | bool is_mash_clock; |
1c97aa6c EA |
537 | |
538 | u32 tcnt_mux; | |
41691b88 EA |
539 | }; |
540 | ||
56eb3a2e MS |
541 | struct bcm2835_gate_data { |
542 | const char *name; | |
543 | const char *parent; | |
544 | ||
545 | u32 ctl_reg; | |
546 | }; | |
547 | ||
41691b88 EA |
548 | struct bcm2835_pll { |
549 | struct clk_hw hw; | |
550 | struct bcm2835_cprman *cprman; | |
551 | const struct bcm2835_pll_data *data; | |
552 | }; | |
553 | ||
554 | static int bcm2835_pll_is_on(struct clk_hw *hw) | |
555 | { | |
556 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
557 | struct bcm2835_cprman *cprman = pll->cprman; | |
558 | const struct bcm2835_pll_data *data = pll->data; | |
559 | ||
560 | return cprman_read(cprman, data->a2w_ctrl_reg) & | |
561 | A2W_PLL_CTRL_PRST_DISABLE; | |
562 | } | |
563 | ||
564 | static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate, | |
565 | unsigned long parent_rate, | |
566 | u32 *ndiv, u32 *fdiv) | |
567 | { | |
568 | u64 div; | |
569 | ||
570 | div = (u64)rate << A2W_PLL_FRAC_BITS; | |
571 | do_div(div, parent_rate); | |
572 | ||
573 | *ndiv = div >> A2W_PLL_FRAC_BITS; | |
574 | *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1); | |
575 | } | |
576 | ||
577 | static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate, | |
578 | u32 ndiv, u32 fdiv, u32 pdiv) | |
579 | { | |
580 | u64 rate; | |
581 | ||
582 | if (pdiv == 0) | |
583 | return 0; | |
584 | ||
585 | rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv); | |
586 | do_div(rate, pdiv); | |
587 | return rate >> A2W_PLL_FRAC_BITS; | |
588 | } | |
589 | ||
590 | static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate, | |
591 | unsigned long *parent_rate) | |
592 | { | |
c4e634ce EA |
593 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); |
594 | const struct bcm2835_pll_data *data = pll->data; | |
41691b88 EA |
595 | u32 ndiv, fdiv; |
596 | ||
c4e634ce EA |
597 | rate = clamp(rate, data->min_rate, data->max_rate); |
598 | ||
41691b88 EA |
599 | bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv); |
600 | ||
601 | return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1); | |
602 | } | |
603 | ||
604 | static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw, | |
605 | unsigned long parent_rate) | |
606 | { | |
607 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
608 | struct bcm2835_cprman *cprman = pll->cprman; | |
609 | const struct bcm2835_pll_data *data = pll->data; | |
610 | u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg); | |
611 | u32 ndiv, pdiv, fdiv; | |
612 | bool using_prediv; | |
613 | ||
614 | if (parent_rate == 0) | |
615 | return 0; | |
616 | ||
617 | fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK; | |
618 | ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT; | |
619 | pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT; | |
620 | using_prediv = cprman_read(cprman, data->ana_reg_base + 4) & | |
621 | data->ana->fb_prediv_mask; | |
622 | ||
051e56db | 623 | if (using_prediv) { |
41691b88 | 624 | ndiv *= 2; |
051e56db PE |
625 | fdiv *= 2; |
626 | } | |
41691b88 EA |
627 | |
628 | return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv); | |
629 | } | |
630 | ||
631 | static void bcm2835_pll_off(struct clk_hw *hw) | |
632 | { | |
633 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
634 | struct bcm2835_cprman *cprman = pll->cprman; | |
635 | const struct bcm2835_pll_data *data = pll->data; | |
636 | ||
6727f086 MS |
637 | spin_lock(&cprman->regs_lock); |
638 | cprman_write(cprman, data->cm_ctrl_reg, | |
639 | cprman_read(cprman, data->cm_ctrl_reg) | | |
640 | CM_PLL_ANARST); | |
641 | cprman_write(cprman, data->a2w_ctrl_reg, | |
642 | cprman_read(cprman, data->a2w_ctrl_reg) | | |
643 | A2W_PLL_CTRL_PWRDN); | |
644 | spin_unlock(&cprman->regs_lock); | |
41691b88 EA |
645 | } |
646 | ||
647 | static int bcm2835_pll_on(struct clk_hw *hw) | |
648 | { | |
649 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
650 | struct bcm2835_cprman *cprman = pll->cprman; | |
651 | const struct bcm2835_pll_data *data = pll->data; | |
652 | ktime_t timeout; | |
653 | ||
e708b383 EA |
654 | cprman_write(cprman, data->a2w_ctrl_reg, |
655 | cprman_read(cprman, data->a2w_ctrl_reg) & | |
656 | ~A2W_PLL_CTRL_PWRDN); | |
657 | ||
41691b88 EA |
658 | /* Take the PLL out of reset. */ |
659 | cprman_write(cprman, data->cm_ctrl_reg, | |
660 | cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST); | |
661 | ||
662 | /* Wait for the PLL to lock. */ | |
663 | timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); | |
664 | while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) { | |
665 | if (ktime_after(ktime_get(), timeout)) { | |
666 | dev_err(cprman->dev, "%s: couldn't lock PLL\n", | |
667 | clk_hw_get_name(hw)); | |
668 | return -ETIMEDOUT; | |
669 | } | |
670 | ||
671 | cpu_relax(); | |
672 | } | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
677 | static void | |
678 | bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana) | |
679 | { | |
680 | int i; | |
681 | ||
682 | /* | |
683 | * ANA register setup is done as a series of writes to | |
684 | * ANA3-ANA0, in that order. This lets us write all 4 | |
685 | * registers as a single cycle of the serdes interface (taking | |
686 | * 100 xosc clocks), whereas if we were to update ana0, 1, and | |
687 | * 3 individually through their partial-write registers, each | |
688 | * would be their own serdes cycle. | |
689 | */ | |
690 | for (i = 3; i >= 0; i--) | |
691 | cprman_write(cprman, ana_reg_base + i * 4, ana[i]); | |
692 | } | |
693 | ||
694 | static int bcm2835_pll_set_rate(struct clk_hw *hw, | |
695 | unsigned long rate, unsigned long parent_rate) | |
696 | { | |
697 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
698 | struct bcm2835_cprman *cprman = pll->cprman; | |
699 | const struct bcm2835_pll_data *data = pll->data; | |
700 | bool was_using_prediv, use_fb_prediv, do_ana_setup_first; | |
701 | u32 ndiv, fdiv, a2w_ctl; | |
702 | u32 ana[4]; | |
703 | int i; | |
704 | ||
41691b88 EA |
705 | if (rate > data->max_fb_rate) { |
706 | use_fb_prediv = true; | |
707 | rate /= 2; | |
708 | } else { | |
709 | use_fb_prediv = false; | |
710 | } | |
711 | ||
712 | bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv); | |
713 | ||
714 | for (i = 3; i >= 0; i--) | |
715 | ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4); | |
716 | ||
717 | was_using_prediv = ana[1] & data->ana->fb_prediv_mask; | |
718 | ||
719 | ana[0] &= ~data->ana->mask0; | |
720 | ana[0] |= data->ana->set0; | |
721 | ana[1] &= ~data->ana->mask1; | |
722 | ana[1] |= data->ana->set1; | |
723 | ana[3] &= ~data->ana->mask3; | |
724 | ana[3] |= data->ana->set3; | |
725 | ||
726 | if (was_using_prediv && !use_fb_prediv) { | |
727 | ana[1] &= ~data->ana->fb_prediv_mask; | |
728 | do_ana_setup_first = true; | |
729 | } else if (!was_using_prediv && use_fb_prediv) { | |
730 | ana[1] |= data->ana->fb_prediv_mask; | |
731 | do_ana_setup_first = false; | |
732 | } else { | |
733 | do_ana_setup_first = true; | |
734 | } | |
735 | ||
736 | /* Unmask the reference clock from the oscillator. */ | |
737 | cprman_write(cprman, A2W_XOSC_CTRL, | |
738 | cprman_read(cprman, A2W_XOSC_CTRL) | | |
739 | data->reference_enable_mask); | |
740 | ||
741 | if (do_ana_setup_first) | |
742 | bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana); | |
743 | ||
744 | /* Set the PLL multiplier from the oscillator. */ | |
745 | cprman_write(cprman, data->frac_reg, fdiv); | |
746 | ||
747 | a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg); | |
748 | a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK; | |
749 | a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT; | |
750 | a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK; | |
751 | a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT; | |
752 | cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl); | |
753 | ||
754 | if (!do_ana_setup_first) | |
755 | bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana); | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
96bf9c69 MS |
760 | static int bcm2835_pll_debug_init(struct clk_hw *hw, |
761 | struct dentry *dentry) | |
762 | { | |
763 | struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); | |
764 | struct bcm2835_cprman *cprman = pll->cprman; | |
765 | const struct bcm2835_pll_data *data = pll->data; | |
766 | struct debugfs_reg32 *regs; | |
767 | ||
768 | regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL); | |
769 | if (!regs) | |
770 | return -ENOMEM; | |
771 | ||
772 | regs[0].name = "cm_ctrl"; | |
773 | regs[0].offset = data->cm_ctrl_reg; | |
774 | regs[1].name = "a2w_ctrl"; | |
775 | regs[1].offset = data->a2w_ctrl_reg; | |
776 | regs[2].name = "frac"; | |
777 | regs[2].offset = data->frac_reg; | |
778 | regs[3].name = "ana0"; | |
779 | regs[3].offset = data->ana_reg_base + 0 * 4; | |
780 | regs[4].name = "ana1"; | |
781 | regs[4].offset = data->ana_reg_base + 1 * 4; | |
782 | regs[5].name = "ana2"; | |
783 | regs[5].offset = data->ana_reg_base + 2 * 4; | |
784 | regs[6].name = "ana3"; | |
785 | regs[6].offset = data->ana_reg_base + 3 * 4; | |
786 | ||
787 | return bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry); | |
788 | } | |
789 | ||
41691b88 EA |
790 | static const struct clk_ops bcm2835_pll_clk_ops = { |
791 | .is_prepared = bcm2835_pll_is_on, | |
792 | .prepare = bcm2835_pll_on, | |
793 | .unprepare = bcm2835_pll_off, | |
794 | .recalc_rate = bcm2835_pll_get_rate, | |
795 | .set_rate = bcm2835_pll_set_rate, | |
796 | .round_rate = bcm2835_pll_round_rate, | |
96bf9c69 | 797 | .debug_init = bcm2835_pll_debug_init, |
41691b88 EA |
798 | }; |
799 | ||
800 | struct bcm2835_pll_divider { | |
801 | struct clk_divider div; | |
802 | struct bcm2835_cprman *cprman; | |
803 | const struct bcm2835_pll_divider_data *data; | |
804 | }; | |
805 | ||
806 | static struct bcm2835_pll_divider * | |
807 | bcm2835_pll_divider_from_hw(struct clk_hw *hw) | |
808 | { | |
809 | return container_of(hw, struct bcm2835_pll_divider, div.hw); | |
810 | } | |
811 | ||
812 | static int bcm2835_pll_divider_is_on(struct clk_hw *hw) | |
813 | { | |
814 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
815 | struct bcm2835_cprman *cprman = divider->cprman; | |
816 | const struct bcm2835_pll_divider_data *data = divider->data; | |
817 | ||
818 | return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE); | |
819 | } | |
820 | ||
821 | static long bcm2835_pll_divider_round_rate(struct clk_hw *hw, | |
822 | unsigned long rate, | |
823 | unsigned long *parent_rate) | |
824 | { | |
825 | return clk_divider_ops.round_rate(hw, rate, parent_rate); | |
826 | } | |
827 | ||
828 | static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw, | |
829 | unsigned long parent_rate) | |
830 | { | |
79c1e2fc | 831 | return clk_divider_ops.recalc_rate(hw, parent_rate); |
41691b88 EA |
832 | } |
833 | ||
834 | static void bcm2835_pll_divider_off(struct clk_hw *hw) | |
835 | { | |
836 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
837 | struct bcm2835_cprman *cprman = divider->cprman; | |
838 | const struct bcm2835_pll_divider_data *data = divider->data; | |
839 | ||
ec36a5c6 | 840 | spin_lock(&cprman->regs_lock); |
41691b88 EA |
841 | cprman_write(cprman, data->cm_reg, |
842 | (cprman_read(cprman, data->cm_reg) & | |
843 | ~data->load_mask) | data->hold_mask); | |
68af4fa8 BB |
844 | cprman_write(cprman, data->a2w_reg, |
845 | cprman_read(cprman, data->a2w_reg) | | |
846 | A2W_PLL_CHANNEL_DISABLE); | |
ec36a5c6 | 847 | spin_unlock(&cprman->regs_lock); |
41691b88 EA |
848 | } |
849 | ||
850 | static int bcm2835_pll_divider_on(struct clk_hw *hw) | |
851 | { | |
852 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
853 | struct bcm2835_cprman *cprman = divider->cprman; | |
854 | const struct bcm2835_pll_divider_data *data = divider->data; | |
855 | ||
ec36a5c6 | 856 | spin_lock(&cprman->regs_lock); |
41691b88 EA |
857 | cprman_write(cprman, data->a2w_reg, |
858 | cprman_read(cprman, data->a2w_reg) & | |
859 | ~A2W_PLL_CHANNEL_DISABLE); | |
860 | ||
861 | cprman_write(cprman, data->cm_reg, | |
862 | cprman_read(cprman, data->cm_reg) & ~data->hold_mask); | |
ec36a5c6 | 863 | spin_unlock(&cprman->regs_lock); |
41691b88 EA |
864 | |
865 | return 0; | |
866 | } | |
867 | ||
868 | static int bcm2835_pll_divider_set_rate(struct clk_hw *hw, | |
869 | unsigned long rate, | |
870 | unsigned long parent_rate) | |
871 | { | |
872 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
873 | struct bcm2835_cprman *cprman = divider->cprman; | |
874 | const struct bcm2835_pll_divider_data *data = divider->data; | |
773b3966 | 875 | u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS; |
41691b88 | 876 | |
773b3966 EA |
877 | div = DIV_ROUND_UP_ULL(parent_rate, rate); |
878 | ||
879 | div = min(div, max_div); | |
880 | if (div == max_div) | |
881 | div = 0; | |
41691b88 | 882 | |
773b3966 | 883 | cprman_write(cprman, data->a2w_reg, div); |
41691b88 EA |
884 | cm = cprman_read(cprman, data->cm_reg); |
885 | cprman_write(cprman, data->cm_reg, cm | data->load_mask); | |
886 | cprman_write(cprman, data->cm_reg, cm & ~data->load_mask); | |
887 | ||
888 | return 0; | |
889 | } | |
890 | ||
96bf9c69 MS |
891 | static int bcm2835_pll_divider_debug_init(struct clk_hw *hw, |
892 | struct dentry *dentry) | |
893 | { | |
894 | struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); | |
895 | struct bcm2835_cprman *cprman = divider->cprman; | |
896 | const struct bcm2835_pll_divider_data *data = divider->data; | |
897 | struct debugfs_reg32 *regs; | |
898 | ||
899 | regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL); | |
900 | if (!regs) | |
901 | return -ENOMEM; | |
902 | ||
903 | regs[0].name = "cm"; | |
904 | regs[0].offset = data->cm_reg; | |
905 | regs[1].name = "a2w"; | |
906 | regs[1].offset = data->a2w_reg; | |
907 | ||
908 | return bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry); | |
909 | } | |
910 | ||
41691b88 EA |
911 | static const struct clk_ops bcm2835_pll_divider_clk_ops = { |
912 | .is_prepared = bcm2835_pll_divider_is_on, | |
913 | .prepare = bcm2835_pll_divider_on, | |
914 | .unprepare = bcm2835_pll_divider_off, | |
915 | .recalc_rate = bcm2835_pll_divider_get_rate, | |
916 | .set_rate = bcm2835_pll_divider_set_rate, | |
917 | .round_rate = bcm2835_pll_divider_round_rate, | |
96bf9c69 | 918 | .debug_init = bcm2835_pll_divider_debug_init, |
41691b88 EA |
919 | }; |
920 | ||
921 | /* | |
922 | * The CM dividers do fixed-point division, so we can't use the | |
923 | * generic integer divider code like the PLL dividers do (and we can't | |
924 | * fake it by having some fixed shifts preceding it in the clock tree, | |
925 | * because we'd run out of bits in a 32-bit unsigned long). | |
926 | */ | |
927 | struct bcm2835_clock { | |
928 | struct clk_hw hw; | |
929 | struct bcm2835_cprman *cprman; | |
930 | const struct bcm2835_clock_data *data; | |
931 | }; | |
932 | ||
933 | static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw) | |
934 | { | |
935 | return container_of(hw, struct bcm2835_clock, hw); | |
936 | } | |
937 | ||
938 | static int bcm2835_clock_is_on(struct clk_hw *hw) | |
939 | { | |
940 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
941 | struct bcm2835_cprman *cprman = clock->cprman; | |
942 | const struct bcm2835_clock_data *data = clock->data; | |
943 | ||
944 | return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0; | |
945 | } | |
946 | ||
947 | static u32 bcm2835_clock_choose_div(struct clk_hw *hw, | |
948 | unsigned long rate, | |
9c95b32c RP |
949 | unsigned long parent_rate, |
950 | bool round_up) | |
41691b88 EA |
951 | { |
952 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
953 | const struct bcm2835_clock_data *data = clock->data; | |
9c95b32c RP |
954 | u32 unused_frac_mask = |
955 | GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1; | |
41691b88 | 956 | u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS; |
9c95b32c | 957 | u64 rem; |
959ca92a | 958 | u32 div, mindiv, maxdiv; |
41691b88 | 959 | |
9c95b32c | 960 | rem = do_div(temp, rate); |
41691b88 EA |
961 | div = temp; |
962 | ||
9c95b32c RP |
963 | /* Round up and mask off the unused bits */ |
964 | if (round_up && ((div & unused_frac_mask) != 0 || rem != 0)) | |
965 | div += unused_frac_mask + 1; | |
966 | div &= ~unused_frac_mask; | |
41691b88 | 967 | |
959ca92a MS |
968 | /* different clamping limits apply for a mash clock */ |
969 | if (data->is_mash_clock) { | |
970 | /* clamp to min divider of 2 */ | |
971 | mindiv = 2 << CM_DIV_FRAC_BITS; | |
972 | /* clamp to the highest possible integer divider */ | |
973 | maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS; | |
974 | } else { | |
975 | /* clamp to min divider of 1 */ | |
976 | mindiv = 1 << CM_DIV_FRAC_BITS; | |
977 | /* clamp to the highest possible fractional divider */ | |
978 | maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1, | |
979 | CM_DIV_FRAC_BITS - data->frac_bits); | |
980 | } | |
981 | ||
982 | /* apply the clamping limits */ | |
983 | div = max_t(u32, div, mindiv); | |
984 | div = min_t(u32, div, maxdiv); | |
41691b88 EA |
985 | |
986 | return div; | |
987 | } | |
988 | ||
989 | static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock, | |
990 | unsigned long parent_rate, | |
991 | u32 div) | |
992 | { | |
993 | const struct bcm2835_clock_data *data = clock->data; | |
994 | u64 temp; | |
995 | ||
78b200b9 EA |
996 | if (data->int_bits == 0 && data->frac_bits == 0) |
997 | return parent_rate; | |
998 | ||
41691b88 EA |
999 | /* |
1000 | * The divisor is a 12.12 fixed point field, but only some of | |
1001 | * the bits are populated in any given clock. | |
1002 | */ | |
1003 | div >>= CM_DIV_FRAC_BITS - data->frac_bits; | |
1004 | div &= (1 << (data->int_bits + data->frac_bits)) - 1; | |
1005 | ||
1006 | if (div == 0) | |
1007 | return 0; | |
1008 | ||
1009 | temp = (u64)parent_rate << data->frac_bits; | |
1010 | ||
1011 | do_div(temp, div); | |
1012 | ||
1013 | return temp; | |
1014 | } | |
1015 | ||
41691b88 EA |
1016 | static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw, |
1017 | unsigned long parent_rate) | |
1018 | { | |
1019 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1020 | struct bcm2835_cprman *cprman = clock->cprman; | |
1021 | const struct bcm2835_clock_data *data = clock->data; | |
78b200b9 EA |
1022 | u32 div; |
1023 | ||
1024 | if (data->int_bits == 0 && data->frac_bits == 0) | |
1025 | return parent_rate; | |
1026 | ||
1027 | div = cprman_read(cprman, data->div_reg); | |
41691b88 EA |
1028 | |
1029 | return bcm2835_clock_rate_from_divisor(clock, parent_rate, div); | |
1030 | } | |
1031 | ||
11262d0f PE |
1032 | static unsigned long bcm2835_clock_get_rate_vpu(struct clk_hw *hw, |
1033 | unsigned long parent_rate) | |
1034 | { | |
1035 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1036 | struct bcm2835_cprman *cprman = clock->cprman; | |
1037 | ||
1038 | if (cprman->fw) { | |
1039 | struct { | |
1040 | u32 id; | |
1041 | u32 val; | |
1042 | } packet; | |
1043 | ||
1044 | packet.id = VCMSG_ID_CORE_CLOCK; | |
1045 | packet.val = 0; | |
1046 | ||
1047 | if (!rpi_firmware_property(cprman->fw, | |
1048 | RPI_FIRMWARE_GET_MAX_CLOCK_RATE, | |
1049 | &packet, sizeof(packet))) | |
1050 | return packet.val; | |
1051 | } | |
1052 | ||
1053 | return bcm2835_clock_get_rate(hw, parent_rate); | |
1054 | } | |
1055 | ||
41691b88 EA |
1056 | static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock) |
1057 | { | |
1058 | struct bcm2835_cprman *cprman = clock->cprman; | |
1059 | const struct bcm2835_clock_data *data = clock->data; | |
1060 | ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); | |
1061 | ||
1062 | while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) { | |
1063 | if (ktime_after(ktime_get(), timeout)) { | |
1064 | dev_err(cprman->dev, "%s: couldn't lock PLL\n", | |
1065 | clk_hw_get_name(&clock->hw)); | |
1066 | return; | |
1067 | } | |
1068 | cpu_relax(); | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | static void bcm2835_clock_off(struct clk_hw *hw) | |
1073 | { | |
1074 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1075 | struct bcm2835_cprman *cprman = clock->cprman; | |
1076 | const struct bcm2835_clock_data *data = clock->data; | |
1077 | ||
1078 | spin_lock(&cprman->regs_lock); | |
1079 | cprman_write(cprman, data->ctl_reg, | |
1080 | cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE); | |
1081 | spin_unlock(&cprman->regs_lock); | |
1082 | ||
1083 | /* BUSY will remain high until the divider completes its cycle. */ | |
1084 | bcm2835_clock_wait_busy(clock); | |
1085 | } | |
1086 | ||
1087 | static int bcm2835_clock_on(struct clk_hw *hw) | |
1088 | { | |
1089 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1090 | struct bcm2835_cprman *cprman = clock->cprman; | |
1091 | const struct bcm2835_clock_data *data = clock->data; | |
1092 | ||
1093 | spin_lock(&cprman->regs_lock); | |
1094 | cprman_write(cprman, data->ctl_reg, | |
1095 | cprman_read(cprman, data->ctl_reg) | | |
1096 | CM_ENABLE | | |
1097 | CM_GATE); | |
1098 | spin_unlock(&cprman->regs_lock); | |
1099 | ||
1c97aa6c EA |
1100 | /* Debug code to measure the clock once it's turned on to see |
1101 | * if it's ticking at the rate we expect. | |
1102 | */ | |
1103 | if (data->tcnt_mux && false) { | |
1104 | dev_info(cprman->dev, | |
1105 | "clk %s: rate %ld, measure %ld\n", | |
1106 | data->name, | |
1107 | clk_hw_get_rate(hw), | |
1108 | bcm2835_measure_tcnt_mux(cprman, data->tcnt_mux)); | |
1109 | } | |
1110 | ||
41691b88 EA |
1111 | return 0; |
1112 | } | |
1113 | ||
1114 | static int bcm2835_clock_set_rate(struct clk_hw *hw, | |
1115 | unsigned long rate, unsigned long parent_rate) | |
1116 | { | |
1117 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1118 | struct bcm2835_cprman *cprman = clock->cprman; | |
1119 | const struct bcm2835_clock_data *data = clock->data; | |
9c95b32c | 1120 | u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false); |
959ca92a MS |
1121 | u32 ctl; |
1122 | ||
1123 | spin_lock(&cprman->regs_lock); | |
1124 | ||
1125 | /* | |
1126 | * Setting up frac support | |
1127 | * | |
1128 | * In principle it is recommended to stop/start the clock first, | |
1129 | * but as we set CLK_SET_RATE_GATE during registration of the | |
1130 | * clock this requirement should be take care of by the | |
1131 | * clk-framework. | |
1132 | */ | |
1133 | ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC; | |
1134 | ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0; | |
1135 | cprman_write(cprman, data->ctl_reg, ctl); | |
41691b88 EA |
1136 | |
1137 | cprman_write(cprman, data->div_reg, div); | |
1138 | ||
959ca92a MS |
1139 | spin_unlock(&cprman->regs_lock); |
1140 | ||
41691b88 EA |
1141 | return 0; |
1142 | } | |
1143 | ||
67615c58 EA |
1144 | static bool |
1145 | bcm2835_clk_is_pllc(struct clk_hw *hw) | |
1146 | { | |
1147 | if (!hw) | |
1148 | return false; | |
1149 | ||
1150 | return strncmp(clk_hw_get_name(hw), "pllc", 4) == 0; | |
1151 | } | |
1152 | ||
155e8b3b BB |
1153 | static unsigned long bcm2835_clock_choose_div_and_prate(struct clk_hw *hw, |
1154 | int parent_idx, | |
1155 | unsigned long rate, | |
1156 | u32 *div, | |
1157 | unsigned long *prate) | |
1158 | { | |
1159 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1160 | struct bcm2835_cprman *cprman = clock->cprman; | |
1161 | const struct bcm2835_clock_data *data = clock->data; | |
2aab7a20 | 1162 | unsigned long best_rate = 0; |
155e8b3b BB |
1163 | u32 curdiv, mindiv, maxdiv; |
1164 | struct clk_hw *parent; | |
1165 | ||
1166 | parent = clk_hw_get_parent_by_index(hw, parent_idx); | |
1167 | ||
1168 | if (!(BIT(parent_idx) & data->set_rate_parent)) { | |
1169 | *prate = clk_hw_get_rate(parent); | |
1170 | *div = bcm2835_clock_choose_div(hw, rate, *prate, true); | |
1171 | ||
1172 | return bcm2835_clock_rate_from_divisor(clock, *prate, | |
1173 | *div); | |
1174 | } | |
1175 | ||
1176 | if (data->frac_bits) | |
1177 | dev_warn(cprman->dev, | |
1178 | "frac bits are not used when propagating rate change"); | |
1179 | ||
1180 | /* clamp to min divider of 2 if we're dealing with a mash clock */ | |
1181 | mindiv = data->is_mash_clock ? 2 : 1; | |
1182 | maxdiv = BIT(data->int_bits) - 1; | |
1183 | ||
1184 | /* TODO: Be smart, and only test a subset of the available divisors. */ | |
1185 | for (curdiv = mindiv; curdiv <= maxdiv; curdiv++) { | |
1186 | unsigned long tmp_rate; | |
1187 | ||
1188 | tmp_rate = clk_hw_round_rate(parent, rate * curdiv); | |
1189 | tmp_rate /= curdiv; | |
1190 | if (curdiv == mindiv || | |
1191 | (tmp_rate > best_rate && tmp_rate <= rate)) | |
1192 | best_rate = tmp_rate; | |
1193 | ||
1194 | if (best_rate == rate) | |
1195 | break; | |
1196 | } | |
1197 | ||
1198 | *div = curdiv << CM_DIV_FRAC_BITS; | |
1199 | *prate = curdiv * best_rate; | |
1200 | ||
1201 | return best_rate; | |
1202 | } | |
1203 | ||
6d18b8ad | 1204 | static int bcm2835_clock_determine_rate(struct clk_hw *hw, |
6e1e60da | 1205 | struct clk_rate_request *req) |
6d18b8ad | 1206 | { |
6d18b8ad | 1207 | struct clk_hw *parent, *best_parent = NULL; |
67615c58 | 1208 | bool current_parent_is_pllc; |
6d18b8ad RP |
1209 | unsigned long rate, best_rate = 0; |
1210 | unsigned long prate, best_prate = 0; | |
1211 | size_t i; | |
1212 | u32 div; | |
1213 | ||
67615c58 EA |
1214 | current_parent_is_pllc = bcm2835_clk_is_pllc(clk_hw_get_parent(hw)); |
1215 | ||
6d18b8ad RP |
1216 | /* |
1217 | * Select parent clock that results in the closest but lower rate | |
1218 | */ | |
1219 | for (i = 0; i < clk_hw_get_num_parents(hw); ++i) { | |
1220 | parent = clk_hw_get_parent_by_index(hw, i); | |
1221 | if (!parent) | |
1222 | continue; | |
67615c58 EA |
1223 | |
1224 | /* | |
1225 | * Don't choose a PLLC-derived clock as our parent | |
1226 | * unless it had been manually set that way. PLLC's | |
1227 | * frequency gets adjusted by the firmware due to | |
1228 | * over-temp or under-voltage conditions, without | |
1229 | * prior notification to our clock consumer. | |
1230 | */ | |
1231 | if (bcm2835_clk_is_pllc(parent) && !current_parent_is_pllc) | |
1232 | continue; | |
1233 | ||
155e8b3b BB |
1234 | rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate, |
1235 | &div, &prate); | |
6d18b8ad RP |
1236 | if (rate > best_rate && rate <= req->rate) { |
1237 | best_parent = parent; | |
1238 | best_prate = prate; | |
1239 | best_rate = rate; | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | if (!best_parent) | |
1244 | return -EINVAL; | |
1245 | ||
1246 | req->best_parent_hw = best_parent; | |
1247 | req->best_parent_rate = best_prate; | |
1248 | ||
1249 | req->rate = best_rate; | |
1250 | ||
1251 | return 0; | |
1252 | } | |
1253 | ||
1254 | static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index) | |
1255 | { | |
1256 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1257 | struct bcm2835_cprman *cprman = clock->cprman; | |
1258 | const struct bcm2835_clock_data *data = clock->data; | |
1259 | u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK; | |
1260 | ||
1261 | cprman_write(cprman, data->ctl_reg, src); | |
1262 | return 0; | |
1263 | } | |
1264 | ||
1265 | static u8 bcm2835_clock_get_parent(struct clk_hw *hw) | |
1266 | { | |
1267 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1268 | struct bcm2835_cprman *cprman = clock->cprman; | |
1269 | const struct bcm2835_clock_data *data = clock->data; | |
1270 | u32 src = cprman_read(cprman, data->ctl_reg); | |
1271 | ||
1272 | return (src & CM_SRC_MASK) >> CM_SRC_SHIFT; | |
1273 | } | |
1274 | ||
96bf9c69 MS |
1275 | static struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = { |
1276 | { | |
1277 | .name = "ctl", | |
1278 | .offset = 0, | |
1279 | }, | |
1280 | { | |
1281 | .name = "div", | |
1282 | .offset = 4, | |
1283 | }, | |
1284 | }; | |
1285 | ||
1286 | static int bcm2835_clock_debug_init(struct clk_hw *hw, | |
1287 | struct dentry *dentry) | |
1288 | { | |
1289 | struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); | |
1290 | struct bcm2835_cprman *cprman = clock->cprman; | |
1291 | const struct bcm2835_clock_data *data = clock->data; | |
1292 | ||
1293 | return bcm2835_debugfs_regset( | |
1294 | cprman, data->ctl_reg, | |
1295 | bcm2835_debugfs_clock_reg32, | |
1296 | ARRAY_SIZE(bcm2835_debugfs_clock_reg32), | |
1297 | dentry); | |
1298 | } | |
1299 | ||
41691b88 EA |
1300 | static const struct clk_ops bcm2835_clock_clk_ops = { |
1301 | .is_prepared = bcm2835_clock_is_on, | |
1302 | .prepare = bcm2835_clock_on, | |
1303 | .unprepare = bcm2835_clock_off, | |
1304 | .recalc_rate = bcm2835_clock_get_rate, | |
1305 | .set_rate = bcm2835_clock_set_rate, | |
6d18b8ad RP |
1306 | .determine_rate = bcm2835_clock_determine_rate, |
1307 | .set_parent = bcm2835_clock_set_parent, | |
1308 | .get_parent = bcm2835_clock_get_parent, | |
96bf9c69 | 1309 | .debug_init = bcm2835_clock_debug_init, |
41691b88 EA |
1310 | }; |
1311 | ||
1312 | static int bcm2835_vpu_clock_is_on(struct clk_hw *hw) | |
1313 | { | |
1314 | return true; | |
1315 | } | |
1316 | ||
1317 | /* | |
1318 | * The VPU clock can never be disabled (it doesn't have an ENABLE | |
1319 | * bit), so it gets its own set of clock ops. | |
1320 | */ | |
1321 | static const struct clk_ops bcm2835_vpu_clock_clk_ops = { | |
1322 | .is_prepared = bcm2835_vpu_clock_is_on, | |
11262d0f | 1323 | .recalc_rate = bcm2835_clock_get_rate_vpu, |
41691b88 | 1324 | .set_rate = bcm2835_clock_set_rate, |
6d18b8ad RP |
1325 | .determine_rate = bcm2835_clock_determine_rate, |
1326 | .set_parent = bcm2835_clock_set_parent, | |
1327 | .get_parent = bcm2835_clock_get_parent, | |
96bf9c69 | 1328 | .debug_init = bcm2835_clock_debug_init, |
41691b88 EA |
1329 | }; |
1330 | ||
f1760ee6 PE |
1331 | static bool bcm2835_clk_is_claimed(const char *name); |
1332 | ||
b19f009d SB |
1333 | static struct clk_hw *bcm2835_register_pll(struct bcm2835_cprman *cprman, |
1334 | const struct bcm2835_pll_data *data) | |
41691b88 EA |
1335 | { |
1336 | struct bcm2835_pll *pll; | |
1337 | struct clk_init_data init; | |
b19f009d | 1338 | int ret; |
41691b88 EA |
1339 | |
1340 | memset(&init, 0, sizeof(init)); | |
1341 | ||
1342 | /* All of the PLLs derive from the external oscillator. */ | |
78b200b9 | 1343 | init.parent_names = &cprman->real_parent_names[0]; |
41691b88 EA |
1344 | init.num_parents = 1; |
1345 | init.name = data->name; | |
1346 | init.ops = &bcm2835_pll_clk_ops; | |
1347 | init.flags = CLK_IGNORE_UNUSED; | |
1348 | ||
f1760ee6 PE |
1349 | if (!bcm2835_clk_is_claimed(data->name)) |
1350 | init.flags |= CLK_IS_CRITICAL; | |
1351 | ||
41691b88 EA |
1352 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
1353 | if (!pll) | |
1354 | return NULL; | |
1355 | ||
1356 | pll->cprman = cprman; | |
1357 | pll->data = data; | |
1358 | pll->hw.init = &init; | |
1359 | ||
b19f009d SB |
1360 | ret = devm_clk_hw_register(cprman->dev, &pll->hw); |
1361 | if (ret) | |
1362 | return NULL; | |
1363 | return &pll->hw; | |
41691b88 EA |
1364 | } |
1365 | ||
b19f009d | 1366 | static struct clk_hw * |
41691b88 EA |
1367 | bcm2835_register_pll_divider(struct bcm2835_cprman *cprman, |
1368 | const struct bcm2835_pll_divider_data *data) | |
1369 | { | |
1370 | struct bcm2835_pll_divider *divider; | |
1371 | struct clk_init_data init; | |
41691b88 | 1372 | const char *divider_name; |
b19f009d | 1373 | int ret; |
41691b88 EA |
1374 | |
1375 | if (data->fixed_divider != 1) { | |
1376 | divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL, | |
1377 | "%s_prediv", data->name); | |
1378 | if (!divider_name) | |
1379 | return NULL; | |
1380 | } else { | |
1381 | divider_name = data->name; | |
1382 | } | |
1383 | ||
1384 | memset(&init, 0, sizeof(init)); | |
1385 | ||
3b15afef | 1386 | init.parent_names = &data->source_pll; |
41691b88 EA |
1387 | init.num_parents = 1; |
1388 | init.name = divider_name; | |
1389 | init.ops = &bcm2835_pll_divider_clk_ops; | |
5678dd2d | 1390 | init.flags = data->flags | CLK_IGNORE_UNUSED; |
41691b88 EA |
1391 | |
1392 | divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL); | |
1393 | if (!divider) | |
1394 | return NULL; | |
1395 | ||
1396 | divider->div.reg = cprman->regs + data->a2w_reg; | |
1397 | divider->div.shift = A2W_PLL_DIV_SHIFT; | |
1398 | divider->div.width = A2W_PLL_DIV_BITS; | |
79c1e2fc | 1399 | divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO; |
41691b88 EA |
1400 | divider->div.lock = &cprman->regs_lock; |
1401 | divider->div.hw.init = &init; | |
1402 | divider->div.table = NULL; | |
1403 | ||
e47eaeb2 | 1404 | if (!(cprman_read(cprman, data->cm_reg) & data->hold_mask)) { |
f1760ee6 PE |
1405 | if (!bcm2835_clk_is_claimed(data->source_pll)) |
1406 | init.flags |= CLK_IS_CRITICAL; | |
1407 | if (!bcm2835_clk_is_claimed(data->name)) | |
1408 | divider->div.flags |= CLK_IS_CRITICAL; | |
e47eaeb2 PE |
1409 | } |
1410 | ||
41691b88 EA |
1411 | divider->cprman = cprman; |
1412 | divider->data = data; | |
1413 | ||
b19f009d SB |
1414 | ret = devm_clk_hw_register(cprman->dev, ÷r->div.hw); |
1415 | if (ret) | |
1416 | return ERR_PTR(ret); | |
41691b88 EA |
1417 | |
1418 | /* | |
1419 | * PLLH's channels have a fixed divide by 10 afterwards, which | |
1420 | * is what our consumers are actually using. | |
1421 | */ | |
1422 | if (data->fixed_divider != 1) { | |
b19f009d SB |
1423 | return clk_hw_register_fixed_factor(cprman->dev, data->name, |
1424 | divider_name, | |
1425 | CLK_SET_RATE_PARENT, | |
1426 | 1, | |
1427 | data->fixed_divider); | |
41691b88 EA |
1428 | } |
1429 | ||
b19f009d | 1430 | return ÷r->div.hw; |
41691b88 EA |
1431 | } |
1432 | ||
b19f009d | 1433 | static struct clk_hw *bcm2835_register_clock(struct bcm2835_cprman *cprman, |
41691b88 EA |
1434 | const struct bcm2835_clock_data *data) |
1435 | { | |
1436 | struct bcm2835_clock *clock; | |
1437 | struct clk_init_data init; | |
6d18b8ad | 1438 | const char *parents[1 << CM_SRC_BITS]; |
78b200b9 | 1439 | size_t i, j; |
b19f009d | 1440 | int ret; |
41691b88 EA |
1441 | |
1442 | /* | |
78b200b9 EA |
1443 | * Replace our strings referencing parent clocks with the |
1444 | * actual clock-output-name of the parent. | |
41691b88 | 1445 | */ |
6d18b8ad | 1446 | for (i = 0; i < data->num_mux_parents; i++) { |
78b200b9 EA |
1447 | parents[i] = data->parents[i]; |
1448 | ||
1449 | for (j = 0; j < ARRAY_SIZE(cprman_parent_names); j++) { | |
1450 | if (strcmp(parents[i], cprman_parent_names[j]) == 0) { | |
1451 | parents[i] = cprman->real_parent_names[j]; | |
1452 | break; | |
1453 | } | |
1454 | } | |
41691b88 EA |
1455 | } |
1456 | ||
1457 | memset(&init, 0, sizeof(init)); | |
6d18b8ad RP |
1458 | init.parent_names = parents; |
1459 | init.num_parents = data->num_mux_parents; | |
41691b88 | 1460 | init.name = data->name; |
e69fdcca | 1461 | init.flags = data->flags | CLK_IGNORE_UNUSED; |
41691b88 | 1462 | |
3393a08d EA |
1463 | /* |
1464 | * Some GPIO clocks for ethernet/wifi PLLs are marked as | |
1465 | * critical (since some platforms use them), but if the | |
1466 | * firmware didn't have them turned on then they clearly | |
1467 | * aren't actually critical. | |
1468 | */ | |
1469 | if ((cprman_read(cprman, data->ctl_reg) & CM_ENABLE) == 0) | |
1470 | init.flags &= ~CLK_IS_CRITICAL; | |
1471 | ||
155e8b3b BB |
1472 | /* |
1473 | * Pass the CLK_SET_RATE_PARENT flag if we are allowed to propagate | |
1474 | * rate changes on at least of the parents. | |
1475 | */ | |
1476 | if (data->set_rate_parent) | |
1477 | init.flags |= CLK_SET_RATE_PARENT; | |
1478 | ||
41691b88 EA |
1479 | if (data->is_vpu_clock) { |
1480 | init.ops = &bcm2835_vpu_clock_clk_ops; | |
1481 | } else { | |
1482 | init.ops = &bcm2835_clock_clk_ops; | |
1483 | init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE; | |
eddcbe83 EA |
1484 | |
1485 | /* If the clock wasn't actually enabled at boot, it's not | |
1486 | * critical. | |
1487 | */ | |
1488 | if (!(cprman_read(cprman, data->ctl_reg) & CM_ENABLE)) | |
1489 | init.flags &= ~CLK_IS_CRITICAL; | |
41691b88 EA |
1490 | } |
1491 | ||
1492 | clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL); | |
1493 | if (!clock) | |
1494 | return NULL; | |
1495 | ||
1496 | clock->cprman = cprman; | |
1497 | clock->data = data; | |
1498 | clock->hw.init = &init; | |
1499 | ||
b19f009d SB |
1500 | ret = devm_clk_hw_register(cprman->dev, &clock->hw); |
1501 | if (ret) | |
1502 | return ERR_PTR(ret); | |
1503 | return &clock->hw; | |
41691b88 EA |
1504 | } |
1505 | ||
56eb3a2e MS |
1506 | static struct clk *bcm2835_register_gate(struct bcm2835_cprman *cprman, |
1507 | const struct bcm2835_gate_data *data) | |
1508 | { | |
1509 | return clk_register_gate(cprman->dev, data->name, data->parent, | |
1510 | CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE, | |
1511 | cprman->regs + data->ctl_reg, | |
1512 | CM_GATE_BIT, 0, &cprman->regs_lock); | |
1513 | } | |
1514 | ||
b19f009d SB |
1515 | typedef struct clk_hw *(*bcm2835_clk_register)(struct bcm2835_cprman *cprman, |
1516 | const void *data); | |
56eb3a2e MS |
1517 | struct bcm2835_clk_desc { |
1518 | bcm2835_clk_register clk_register; | |
1519 | const void *data; | |
1520 | }; | |
1521 | ||
3b15afef MS |
1522 | /* assignment helper macros for different clock types */ |
1523 | #define _REGISTER(f, ...) { .clk_register = (bcm2835_clk_register)f, \ | |
1524 | .data = __VA_ARGS__ } | |
1525 | #define REGISTER_PLL(...) _REGISTER(&bcm2835_register_pll, \ | |
1526 | &(struct bcm2835_pll_data) \ | |
1527 | {__VA_ARGS__}) | |
1528 | #define REGISTER_PLL_DIV(...) _REGISTER(&bcm2835_register_pll_divider, \ | |
1529 | &(struct bcm2835_pll_divider_data) \ | |
1530 | {__VA_ARGS__}) | |
1531 | #define REGISTER_CLK(...) _REGISTER(&bcm2835_register_clock, \ | |
1532 | &(struct bcm2835_clock_data) \ | |
1533 | {__VA_ARGS__}) | |
1534 | #define REGISTER_GATE(...) _REGISTER(&bcm2835_register_gate, \ | |
1535 | &(struct bcm2835_gate_data) \ | |
1536 | {__VA_ARGS__}) | |
1537 | ||
1538 | /* parent mux arrays plus helper macros */ | |
1539 | ||
1540 | /* main oscillator parent mux */ | |
1541 | static const char *const bcm2835_clock_osc_parents[] = { | |
1542 | "gnd", | |
1543 | "xosc", | |
1544 | "testdebug0", | |
1545 | "testdebug1" | |
1546 | }; | |
1547 | ||
1548 | #define REGISTER_OSC_CLK(...) REGISTER_CLK( \ | |
1549 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), \ | |
1550 | .parents = bcm2835_clock_osc_parents, \ | |
1551 | __VA_ARGS__) | |
1552 | ||
1553 | /* main peripherial parent mux */ | |
1554 | static const char *const bcm2835_clock_per_parents[] = { | |
1555 | "gnd", | |
1556 | "xosc", | |
1557 | "testdebug0", | |
1558 | "testdebug1", | |
1559 | "plla_per", | |
1560 | "pllc_per", | |
1561 | "plld_per", | |
1562 | "pllh_aux", | |
1563 | }; | |
1564 | ||
1565 | #define REGISTER_PER_CLK(...) REGISTER_CLK( \ | |
1566 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), \ | |
1567 | .parents = bcm2835_clock_per_parents, \ | |
1568 | __VA_ARGS__) | |
56eb3a2e | 1569 | |
3b15afef MS |
1570 | /* main vpu parent mux */ |
1571 | static const char *const bcm2835_clock_vpu_parents[] = { | |
1572 | "gnd", | |
1573 | "xosc", | |
1574 | "testdebug0", | |
1575 | "testdebug1", | |
1576 | "plla_core", | |
1577 | "pllc_core0", | |
1578 | "plld_core", | |
1579 | "pllh_aux", | |
1580 | "pllc_core1", | |
1581 | "pllc_core2", | |
1582 | }; | |
1583 | ||
1584 | #define REGISTER_VPU_CLK(...) REGISTER_CLK( \ | |
1585 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), \ | |
1586 | .parents = bcm2835_clock_vpu_parents, \ | |
1587 | __VA_ARGS__) | |
1588 | ||
78b200b9 EA |
1589 | /* |
1590 | * DSI parent clocks. The DSI byte/DDR/DDR2 clocks come from the DSI | |
1591 | * analog PHY. The _inv variants are generated internally to cprman, | |
1592 | * but we don't use them so they aren't hooked up. | |
1593 | */ | |
1594 | static const char *const bcm2835_clock_dsi0_parents[] = { | |
1595 | "gnd", | |
1596 | "xosc", | |
1597 | "testdebug0", | |
1598 | "testdebug1", | |
1599 | "dsi0_ddr", | |
1600 | "dsi0_ddr_inv", | |
1601 | "dsi0_ddr2", | |
1602 | "dsi0_ddr2_inv", | |
1603 | "dsi0_byte", | |
1604 | "dsi0_byte_inv", | |
1605 | }; | |
1606 | ||
1607 | static const char *const bcm2835_clock_dsi1_parents[] = { | |
1608 | "gnd", | |
1609 | "xosc", | |
1610 | "testdebug0", | |
1611 | "testdebug1", | |
1612 | "dsi1_ddr", | |
1613 | "dsi1_ddr_inv", | |
1614 | "dsi1_ddr2", | |
1615 | "dsi1_ddr2_inv", | |
1616 | "dsi1_byte", | |
1617 | "dsi1_byte_inv", | |
1618 | }; | |
1619 | ||
1620 | #define REGISTER_DSI0_CLK(...) REGISTER_CLK( \ | |
1621 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi0_parents), \ | |
1622 | .parents = bcm2835_clock_dsi0_parents, \ | |
1623 | __VA_ARGS__) | |
1624 | ||
1625 | #define REGISTER_DSI1_CLK(...) REGISTER_CLK( \ | |
1626 | .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi1_parents), \ | |
1627 | .parents = bcm2835_clock_dsi1_parents, \ | |
1628 | __VA_ARGS__) | |
1629 | ||
3b15afef MS |
1630 | /* |
1631 | * the real definition of all the pll, pll_dividers and clocks | |
1632 | * these make use of the above REGISTER_* macros | |
1633 | */ | |
56eb3a2e | 1634 | static const struct bcm2835_clk_desc clk_desc_array[] = { |
3b15afef MS |
1635 | /* the PLL + PLL dividers */ |
1636 | ||
1637 | /* | |
1638 | * PLLA is the auxiliary PLL, used to drive the CCP2 | |
1639 | * (Compact Camera Port 2) transmitter clock. | |
1640 | * | |
1641 | * It is in the PX LDO power domain, which is on when the | |
1642 | * AUDIO domain is on. | |
1643 | */ | |
1644 | [BCM2835_PLLA] = REGISTER_PLL( | |
1645 | .name = "plla", | |
1646 | .cm_ctrl_reg = CM_PLLA, | |
1647 | .a2w_ctrl_reg = A2W_PLLA_CTRL, | |
1648 | .frac_reg = A2W_PLLA_FRAC, | |
1649 | .ana_reg_base = A2W_PLLA_ANA0, | |
1650 | .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE, | |
1651 | .lock_mask = CM_LOCK_FLOCKA, | |
1652 | ||
1653 | .ana = &bcm2835_ana_default, | |
1654 | ||
1655 | .min_rate = 600000000u, | |
1656 | .max_rate = 2400000000u, | |
1657 | .max_fb_rate = BCM2835_MAX_FB_RATE), | |
1658 | [BCM2835_PLLA_CORE] = REGISTER_PLL_DIV( | |
1659 | .name = "plla_core", | |
1660 | .source_pll = "plla", | |
1661 | .cm_reg = CM_PLLA, | |
1662 | .a2w_reg = A2W_PLLA_CORE, | |
1663 | .load_mask = CM_PLLA_LOADCORE, | |
1664 | .hold_mask = CM_PLLA_HOLDCORE, | |
5678dd2d EA |
1665 | .fixed_divider = 1, |
1666 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1667 | [BCM2835_PLLA_PER] = REGISTER_PLL_DIV( |
1668 | .name = "plla_per", | |
1669 | .source_pll = "plla", | |
1670 | .cm_reg = CM_PLLA, | |
1671 | .a2w_reg = A2W_PLLA_PER, | |
1672 | .load_mask = CM_PLLA_LOADPER, | |
1673 | .hold_mask = CM_PLLA_HOLDPER, | |
5678dd2d EA |
1674 | .fixed_divider = 1, |
1675 | .flags = CLK_SET_RATE_PARENT), | |
72843695 MS |
1676 | [BCM2835_PLLA_DSI0] = REGISTER_PLL_DIV( |
1677 | .name = "plla_dsi0", | |
1678 | .source_pll = "plla", | |
1679 | .cm_reg = CM_PLLA, | |
1680 | .a2w_reg = A2W_PLLA_DSI0, | |
1681 | .load_mask = CM_PLLA_LOADDSI0, | |
1682 | .hold_mask = CM_PLLA_HOLDDSI0, | |
1683 | .fixed_divider = 1), | |
1684 | [BCM2835_PLLA_CCP2] = REGISTER_PLL_DIV( | |
1685 | .name = "plla_ccp2", | |
1686 | .source_pll = "plla", | |
1687 | .cm_reg = CM_PLLA, | |
1688 | .a2w_reg = A2W_PLLA_CCP2, | |
1689 | .load_mask = CM_PLLA_LOADCCP2, | |
1690 | .hold_mask = CM_PLLA_HOLDCCP2, | |
5678dd2d EA |
1691 | .fixed_divider = 1, |
1692 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1693 | |
1694 | /* PLLB is used for the ARM's clock. */ | |
1695 | [BCM2835_PLLB] = REGISTER_PLL( | |
1696 | .name = "pllb", | |
1697 | .cm_ctrl_reg = CM_PLLB, | |
1698 | .a2w_ctrl_reg = A2W_PLLB_CTRL, | |
1699 | .frac_reg = A2W_PLLB_FRAC, | |
1700 | .ana_reg_base = A2W_PLLB_ANA0, | |
1701 | .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE, | |
1702 | .lock_mask = CM_LOCK_FLOCKB, | |
1703 | ||
1704 | .ana = &bcm2835_ana_default, | |
1705 | ||
1706 | .min_rate = 600000000u, | |
1707 | .max_rate = 3000000000u, | |
1708 | .max_fb_rate = BCM2835_MAX_FB_RATE), | |
1709 | [BCM2835_PLLB_ARM] = REGISTER_PLL_DIV( | |
1710 | .name = "pllb_arm", | |
1711 | .source_pll = "pllb", | |
1712 | .cm_reg = CM_PLLB, | |
1713 | .a2w_reg = A2W_PLLB_ARM, | |
1714 | .load_mask = CM_PLLB_LOADARM, | |
1715 | .hold_mask = CM_PLLB_HOLDARM, | |
5678dd2d EA |
1716 | .fixed_divider = 1, |
1717 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1718 | |
1719 | /* | |
1720 | * PLLC is the core PLL, used to drive the core VPU clock. | |
1721 | * | |
1722 | * It is in the PX LDO power domain, which is on when the | |
1723 | * AUDIO domain is on. | |
1724 | */ | |
1725 | [BCM2835_PLLC] = REGISTER_PLL( | |
1726 | .name = "pllc", | |
1727 | .cm_ctrl_reg = CM_PLLC, | |
1728 | .a2w_ctrl_reg = A2W_PLLC_CTRL, | |
1729 | .frac_reg = A2W_PLLC_FRAC, | |
1730 | .ana_reg_base = A2W_PLLC_ANA0, | |
1731 | .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE, | |
1732 | .lock_mask = CM_LOCK_FLOCKC, | |
1733 | ||
1734 | .ana = &bcm2835_ana_default, | |
1735 | ||
1736 | .min_rate = 600000000u, | |
1737 | .max_rate = 3000000000u, | |
1738 | .max_fb_rate = BCM2835_MAX_FB_RATE), | |
1739 | [BCM2835_PLLC_CORE0] = REGISTER_PLL_DIV( | |
1740 | .name = "pllc_core0", | |
1741 | .source_pll = "pllc", | |
1742 | .cm_reg = CM_PLLC, | |
1743 | .a2w_reg = A2W_PLLC_CORE0, | |
1744 | .load_mask = CM_PLLC_LOADCORE0, | |
1745 | .hold_mask = CM_PLLC_HOLDCORE0, | |
5678dd2d EA |
1746 | .fixed_divider = 1, |
1747 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1748 | [BCM2835_PLLC_CORE1] = REGISTER_PLL_DIV( |
1749 | .name = "pllc_core1", | |
1750 | .source_pll = "pllc", | |
1751 | .cm_reg = CM_PLLC, | |
1752 | .a2w_reg = A2W_PLLC_CORE1, | |
1753 | .load_mask = CM_PLLC_LOADCORE1, | |
1754 | .hold_mask = CM_PLLC_HOLDCORE1, | |
5678dd2d EA |
1755 | .fixed_divider = 1, |
1756 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1757 | [BCM2835_PLLC_CORE2] = REGISTER_PLL_DIV( |
1758 | .name = "pllc_core2", | |
1759 | .source_pll = "pllc", | |
1760 | .cm_reg = CM_PLLC, | |
1761 | .a2w_reg = A2W_PLLC_CORE2, | |
1762 | .load_mask = CM_PLLC_LOADCORE2, | |
1763 | .hold_mask = CM_PLLC_HOLDCORE2, | |
5678dd2d EA |
1764 | .fixed_divider = 1, |
1765 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1766 | [BCM2835_PLLC_PER] = REGISTER_PLL_DIV( |
1767 | .name = "pllc_per", | |
1768 | .source_pll = "pllc", | |
1769 | .cm_reg = CM_PLLC, | |
1770 | .a2w_reg = A2W_PLLC_PER, | |
1771 | .load_mask = CM_PLLC_LOADPER, | |
1772 | .hold_mask = CM_PLLC_HOLDPER, | |
5678dd2d EA |
1773 | .fixed_divider = 1, |
1774 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1775 | |
1776 | /* | |
1777 | * PLLD is the display PLL, used to drive DSI display panels. | |
1778 | * | |
1779 | * It is in the PX LDO power domain, which is on when the | |
1780 | * AUDIO domain is on. | |
1781 | */ | |
1782 | [BCM2835_PLLD] = REGISTER_PLL( | |
1783 | .name = "plld", | |
1784 | .cm_ctrl_reg = CM_PLLD, | |
1785 | .a2w_ctrl_reg = A2W_PLLD_CTRL, | |
1786 | .frac_reg = A2W_PLLD_FRAC, | |
1787 | .ana_reg_base = A2W_PLLD_ANA0, | |
1788 | .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE, | |
1789 | .lock_mask = CM_LOCK_FLOCKD, | |
1790 | ||
1791 | .ana = &bcm2835_ana_default, | |
1792 | ||
1793 | .min_rate = 600000000u, | |
1794 | .max_rate = 2400000000u, | |
1795 | .max_fb_rate = BCM2835_MAX_FB_RATE), | |
1796 | [BCM2835_PLLD_CORE] = REGISTER_PLL_DIV( | |
1797 | .name = "plld_core", | |
1798 | .source_pll = "plld", | |
1799 | .cm_reg = CM_PLLD, | |
1800 | .a2w_reg = A2W_PLLD_CORE, | |
1801 | .load_mask = CM_PLLD_LOADCORE, | |
1802 | .hold_mask = CM_PLLD_HOLDCORE, | |
5678dd2d EA |
1803 | .fixed_divider = 1, |
1804 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1805 | [BCM2835_PLLD_PER] = REGISTER_PLL_DIV( |
1806 | .name = "plld_per", | |
1807 | .source_pll = "plld", | |
1808 | .cm_reg = CM_PLLD, | |
1809 | .a2w_reg = A2W_PLLD_PER, | |
1810 | .load_mask = CM_PLLD_LOADPER, | |
1811 | .hold_mask = CM_PLLD_HOLDPER, | |
5678dd2d EA |
1812 | .fixed_divider = 1, |
1813 | .flags = CLK_SET_RATE_PARENT), | |
72843695 MS |
1814 | [BCM2835_PLLD_DSI0] = REGISTER_PLL_DIV( |
1815 | .name = "plld_dsi0", | |
1816 | .source_pll = "plld", | |
1817 | .cm_reg = CM_PLLD, | |
1818 | .a2w_reg = A2W_PLLD_DSI0, | |
1819 | .load_mask = CM_PLLD_LOADDSI0, | |
1820 | .hold_mask = CM_PLLD_HOLDDSI0, | |
1821 | .fixed_divider = 1), | |
1822 | [BCM2835_PLLD_DSI1] = REGISTER_PLL_DIV( | |
1823 | .name = "plld_dsi1", | |
1824 | .source_pll = "plld", | |
1825 | .cm_reg = CM_PLLD, | |
1826 | .a2w_reg = A2W_PLLD_DSI1, | |
1827 | .load_mask = CM_PLLD_LOADDSI1, | |
1828 | .hold_mask = CM_PLLD_HOLDDSI1, | |
1829 | .fixed_divider = 1), | |
3b15afef MS |
1830 | |
1831 | /* | |
1832 | * PLLH is used to supply the pixel clock or the AUX clock for the | |
1833 | * TV encoder. | |
1834 | * | |
1835 | * It is in the HDMI power domain. | |
1836 | */ | |
1837 | [BCM2835_PLLH] = REGISTER_PLL( | |
1838 | "pllh", | |
1839 | .cm_ctrl_reg = CM_PLLH, | |
1840 | .a2w_ctrl_reg = A2W_PLLH_CTRL, | |
1841 | .frac_reg = A2W_PLLH_FRAC, | |
1842 | .ana_reg_base = A2W_PLLH_ANA0, | |
1843 | .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE, | |
1844 | .lock_mask = CM_LOCK_FLOCKH, | |
1845 | ||
1846 | .ana = &bcm2835_ana_pllh, | |
1847 | ||
1848 | .min_rate = 600000000u, | |
1849 | .max_rate = 3000000000u, | |
1850 | .max_fb_rate = BCM2835_MAX_FB_RATE), | |
1851 | [BCM2835_PLLH_RCAL] = REGISTER_PLL_DIV( | |
1852 | .name = "pllh_rcal", | |
1853 | .source_pll = "pllh", | |
1854 | .cm_reg = CM_PLLH, | |
1855 | .a2w_reg = A2W_PLLH_RCAL, | |
1856 | .load_mask = CM_PLLH_LOADRCAL, | |
1857 | .hold_mask = 0, | |
5678dd2d EA |
1858 | .fixed_divider = 10, |
1859 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1860 | [BCM2835_PLLH_AUX] = REGISTER_PLL_DIV( |
1861 | .name = "pllh_aux", | |
1862 | .source_pll = "pllh", | |
1863 | .cm_reg = CM_PLLH, | |
1864 | .a2w_reg = A2W_PLLH_AUX, | |
1865 | .load_mask = CM_PLLH_LOADAUX, | |
1866 | .hold_mask = 0, | |
5678dd2d EA |
1867 | .fixed_divider = 1, |
1868 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef MS |
1869 | [BCM2835_PLLH_PIX] = REGISTER_PLL_DIV( |
1870 | .name = "pllh_pix", | |
1871 | .source_pll = "pllh", | |
1872 | .cm_reg = CM_PLLH, | |
1873 | .a2w_reg = A2W_PLLH_PIX, | |
1874 | .load_mask = CM_PLLH_LOADPIX, | |
1875 | .hold_mask = 0, | |
5678dd2d EA |
1876 | .fixed_divider = 10, |
1877 | .flags = CLK_SET_RATE_PARENT), | |
3b15afef | 1878 | |
56eb3a2e | 1879 | /* the clocks */ |
3b15afef MS |
1880 | |
1881 | /* clocks with oscillator parent mux */ | |
1882 | ||
1883 | /* One Time Programmable Memory clock. Maximum 10Mhz. */ | |
1884 | [BCM2835_CLOCK_OTP] = REGISTER_OSC_CLK( | |
1885 | .name = "otp", | |
1886 | .ctl_reg = CM_OTPCTL, | |
1887 | .div_reg = CM_OTPDIV, | |
1888 | .int_bits = 4, | |
1c97aa6c EA |
1889 | .frac_bits = 0, |
1890 | .tcnt_mux = 6), | |
3b15afef MS |
1891 | /* |
1892 | * Used for a 1Mhz clock for the system clocksource, and also used | |
1893 | * bythe watchdog timer and the camera pulse generator. | |
1894 | */ | |
1895 | [BCM2835_CLOCK_TIMER] = REGISTER_OSC_CLK( | |
1896 | .name = "timer", | |
1897 | .ctl_reg = CM_TIMERCTL, | |
1898 | .div_reg = CM_TIMERDIV, | |
1899 | .int_bits = 6, | |
1900 | .frac_bits = 12), | |
1901 | /* | |
1902 | * Clock for the temperature sensor. | |
1903 | * Generally run at 2Mhz, max 5Mhz. | |
1904 | */ | |
1905 | [BCM2835_CLOCK_TSENS] = REGISTER_OSC_CLK( | |
1906 | .name = "tsens", | |
1907 | .ctl_reg = CM_TSENSCTL, | |
1908 | .div_reg = CM_TSENSDIV, | |
1909 | .int_bits = 5, | |
1910 | .frac_bits = 0), | |
d3d6f15f MS |
1911 | [BCM2835_CLOCK_TEC] = REGISTER_OSC_CLK( |
1912 | .name = "tec", | |
1913 | .ctl_reg = CM_TECCTL, | |
1914 | .div_reg = CM_TECDIV, | |
1915 | .int_bits = 6, | |
1916 | .frac_bits = 0), | |
3b15afef MS |
1917 | |
1918 | /* clocks with vpu parent mux */ | |
1919 | [BCM2835_CLOCK_H264] = REGISTER_VPU_CLK( | |
1920 | .name = "h264", | |
1921 | .ctl_reg = CM_H264CTL, | |
1922 | .div_reg = CM_H264DIV, | |
1923 | .int_bits = 4, | |
1c97aa6c EA |
1924 | .frac_bits = 8, |
1925 | .tcnt_mux = 1), | |
3b15afef MS |
1926 | [BCM2835_CLOCK_ISP] = REGISTER_VPU_CLK( |
1927 | .name = "isp", | |
1928 | .ctl_reg = CM_ISPCTL, | |
1929 | .div_reg = CM_ISPDIV, | |
1930 | .int_bits = 4, | |
1c97aa6c EA |
1931 | .frac_bits = 8, |
1932 | .tcnt_mux = 2), | |
d3d6f15f | 1933 | |
3b15afef MS |
1934 | /* |
1935 | * Secondary SDRAM clock. Used for low-voltage modes when the PLL | |
1936 | * in the SDRAM controller can't be used. | |
1937 | */ | |
1938 | [BCM2835_CLOCK_SDRAM] = REGISTER_VPU_CLK( | |
1939 | .name = "sdram", | |
1940 | .ctl_reg = CM_SDCCTL, | |
1941 | .div_reg = CM_SDCDIV, | |
1942 | .int_bits = 6, | |
1c97aa6c EA |
1943 | .frac_bits = 0, |
1944 | .tcnt_mux = 3), | |
3b15afef MS |
1945 | [BCM2835_CLOCK_V3D] = REGISTER_VPU_CLK( |
1946 | .name = "v3d", | |
1947 | .ctl_reg = CM_V3DCTL, | |
1948 | .div_reg = CM_V3DDIV, | |
1949 | .int_bits = 4, | |
1c97aa6c EA |
1950 | .frac_bits = 8, |
1951 | .tcnt_mux = 4), | |
3b15afef MS |
1952 | /* |
1953 | * VPU clock. This doesn't have an enable bit, since it drives | |
1954 | * the bus for everything else, and is special so it doesn't need | |
1955 | * to be gated for rate changes. It is also known as "clk_audio" | |
1956 | * in various hardware documentation. | |
1957 | */ | |
1958 | [BCM2835_CLOCK_VPU] = REGISTER_VPU_CLK( | |
1959 | .name = "vpu", | |
1960 | .ctl_reg = CM_VPUCTL, | |
1961 | .div_reg = CM_VPUDIV, | |
1962 | .int_bits = 12, | |
1963 | .frac_bits = 8, | |
e69fdcca | 1964 | .flags = CLK_IS_CRITICAL, |
1c97aa6c EA |
1965 | .is_vpu_clock = true, |
1966 | .tcnt_mux = 5), | |
3b15afef MS |
1967 | |
1968 | /* clocks with per parent mux */ | |
d3d6f15f MS |
1969 | [BCM2835_CLOCK_AVEO] = REGISTER_PER_CLK( |
1970 | .name = "aveo", | |
1971 | .ctl_reg = CM_AVEOCTL, | |
1972 | .div_reg = CM_AVEODIV, | |
1973 | .int_bits = 4, | |
1c97aa6c EA |
1974 | .frac_bits = 0, |
1975 | .tcnt_mux = 38), | |
d3d6f15f MS |
1976 | [BCM2835_CLOCK_CAM0] = REGISTER_PER_CLK( |
1977 | .name = "cam0", | |
1978 | .ctl_reg = CM_CAM0CTL, | |
1979 | .div_reg = CM_CAM0DIV, | |
1980 | .int_bits = 4, | |
1c97aa6c EA |
1981 | .frac_bits = 8, |
1982 | .tcnt_mux = 14), | |
d3d6f15f MS |
1983 | [BCM2835_CLOCK_CAM1] = REGISTER_PER_CLK( |
1984 | .name = "cam1", | |
1985 | .ctl_reg = CM_CAM1CTL, | |
1986 | .div_reg = CM_CAM1DIV, | |
1987 | .int_bits = 4, | |
1c97aa6c EA |
1988 | .frac_bits = 8, |
1989 | .tcnt_mux = 15), | |
d3d6f15f MS |
1990 | [BCM2835_CLOCK_DFT] = REGISTER_PER_CLK( |
1991 | .name = "dft", | |
1992 | .ctl_reg = CM_DFTCTL, | |
1993 | .div_reg = CM_DFTDIV, | |
1994 | .int_bits = 5, | |
1995 | .frac_bits = 0), | |
1996 | [BCM2835_CLOCK_DPI] = REGISTER_PER_CLK( | |
1997 | .name = "dpi", | |
1998 | .ctl_reg = CM_DPICTL, | |
1999 | .div_reg = CM_DPIDIV, | |
2000 | .int_bits = 4, | |
1c97aa6c EA |
2001 | .frac_bits = 8, |
2002 | .tcnt_mux = 17), | |
3b15afef MS |
2003 | |
2004 | /* Arasan EMMC clock */ | |
2005 | [BCM2835_CLOCK_EMMC] = REGISTER_PER_CLK( | |
2006 | .name = "emmc", | |
2007 | .ctl_reg = CM_EMMCCTL, | |
2008 | .div_reg = CM_EMMCDIV, | |
2009 | .int_bits = 4, | |
1c97aa6c EA |
2010 | .frac_bits = 8, |
2011 | .tcnt_mux = 39), | |
d3d6f15f MS |
2012 | |
2013 | /* General purpose (GPIO) clocks */ | |
2014 | [BCM2835_CLOCK_GP0] = REGISTER_PER_CLK( | |
2015 | .name = "gp0", | |
2016 | .ctl_reg = CM_GP0CTL, | |
2017 | .div_reg = CM_GP0DIV, | |
2018 | .int_bits = 12, | |
2019 | .frac_bits = 12, | |
1c97aa6c EA |
2020 | .is_mash_clock = true, |
2021 | .tcnt_mux = 20), | |
d3d6f15f MS |
2022 | [BCM2835_CLOCK_GP1] = REGISTER_PER_CLK( |
2023 | .name = "gp1", | |
2024 | .ctl_reg = CM_GP1CTL, | |
2025 | .div_reg = CM_GP1DIV, | |
2026 | .int_bits = 12, | |
2027 | .frac_bits = 12, | |
eddcbe83 | 2028 | .flags = CLK_IS_CRITICAL, |
1c97aa6c EA |
2029 | .is_mash_clock = true, |
2030 | .tcnt_mux = 21), | |
d3d6f15f MS |
2031 | [BCM2835_CLOCK_GP2] = REGISTER_PER_CLK( |
2032 | .name = "gp2", | |
2033 | .ctl_reg = CM_GP2CTL, | |
2034 | .div_reg = CM_GP2DIV, | |
2035 | .int_bits = 12, | |
eddcbe83 EA |
2036 | .frac_bits = 12, |
2037 | .flags = CLK_IS_CRITICAL), | |
d3d6f15f | 2038 | |
3b15afef MS |
2039 | /* HDMI state machine */ |
2040 | [BCM2835_CLOCK_HSM] = REGISTER_PER_CLK( | |
2041 | .name = "hsm", | |
2042 | .ctl_reg = CM_HSMCTL, | |
2043 | .div_reg = CM_HSMDIV, | |
2044 | .int_bits = 4, | |
1c97aa6c EA |
2045 | .frac_bits = 8, |
2046 | .tcnt_mux = 22), | |
33b68960 MS |
2047 | [BCM2835_CLOCK_PCM] = REGISTER_PER_CLK( |
2048 | .name = "pcm", | |
2049 | .ctl_reg = CM_PCMCTL, | |
2050 | .div_reg = CM_PCMDIV, | |
2051 | .int_bits = 12, | |
2052 | .frac_bits = 12, | |
1c97aa6c EA |
2053 | .is_mash_clock = true, |
2054 | .tcnt_mux = 23), | |
3b15afef MS |
2055 | [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK( |
2056 | .name = "pwm", | |
2057 | .ctl_reg = CM_PWMCTL, | |
2058 | .div_reg = CM_PWMDIV, | |
2059 | .int_bits = 12, | |
2060 | .frac_bits = 12, | |
1c97aa6c EA |
2061 | .is_mash_clock = true, |
2062 | .tcnt_mux = 24), | |
d3d6f15f MS |
2063 | [BCM2835_CLOCK_SLIM] = REGISTER_PER_CLK( |
2064 | .name = "slim", | |
2065 | .ctl_reg = CM_SLIMCTL, | |
2066 | .div_reg = CM_SLIMDIV, | |
2067 | .int_bits = 12, | |
2068 | .frac_bits = 12, | |
1c97aa6c EA |
2069 | .is_mash_clock = true, |
2070 | .tcnt_mux = 25), | |
d3d6f15f MS |
2071 | [BCM2835_CLOCK_SMI] = REGISTER_PER_CLK( |
2072 | .name = "smi", | |
2073 | .ctl_reg = CM_SMICTL, | |
2074 | .div_reg = CM_SMIDIV, | |
2075 | .int_bits = 4, | |
1c97aa6c EA |
2076 | .frac_bits = 8, |
2077 | .tcnt_mux = 27), | |
3b15afef MS |
2078 | [BCM2835_CLOCK_UART] = REGISTER_PER_CLK( |
2079 | .name = "uart", | |
2080 | .ctl_reg = CM_UARTCTL, | |
2081 | .div_reg = CM_UARTDIV, | |
2082 | .int_bits = 10, | |
1c97aa6c EA |
2083 | .frac_bits = 12, |
2084 | .tcnt_mux = 28), | |
d3d6f15f | 2085 | |
3b15afef MS |
2086 | /* TV encoder clock. Only operating frequency is 108Mhz. */ |
2087 | [BCM2835_CLOCK_VEC] = REGISTER_PER_CLK( | |
2088 | .name = "vec", | |
2089 | .ctl_reg = CM_VECCTL, | |
2090 | .div_reg = CM_VECDIV, | |
2091 | .int_bits = 4, | |
d86d46af BB |
2092 | .frac_bits = 0, |
2093 | /* | |
2094 | * Allow rate change propagation only on PLLH_AUX which is | |
2095 | * assigned index 7 in the parent array. | |
2096 | */ | |
1c97aa6c EA |
2097 | .set_rate_parent = BIT(7), |
2098 | .tcnt_mux = 29), | |
3b15afef | 2099 | |
d3d6f15f MS |
2100 | /* dsi clocks */ |
2101 | [BCM2835_CLOCK_DSI0E] = REGISTER_PER_CLK( | |
2102 | .name = "dsi0e", | |
2103 | .ctl_reg = CM_DSI0ECTL, | |
2104 | .div_reg = CM_DSI0EDIV, | |
2105 | .int_bits = 4, | |
1c97aa6c EA |
2106 | .frac_bits = 8, |
2107 | .tcnt_mux = 18), | |
d3d6f15f MS |
2108 | [BCM2835_CLOCK_DSI1E] = REGISTER_PER_CLK( |
2109 | .name = "dsi1e", | |
2110 | .ctl_reg = CM_DSI1ECTL, | |
2111 | .div_reg = CM_DSI1EDIV, | |
2112 | .int_bits = 4, | |
1c97aa6c EA |
2113 | .frac_bits = 8, |
2114 | .tcnt_mux = 19), | |
78b200b9 EA |
2115 | [BCM2835_CLOCK_DSI0P] = REGISTER_DSI0_CLK( |
2116 | .name = "dsi0p", | |
2117 | .ctl_reg = CM_DSI0PCTL, | |
2118 | .div_reg = CM_DSI0PDIV, | |
2119 | .int_bits = 0, | |
1c97aa6c EA |
2120 | .frac_bits = 0, |
2121 | .tcnt_mux = 12), | |
78b200b9 EA |
2122 | [BCM2835_CLOCK_DSI1P] = REGISTER_DSI1_CLK( |
2123 | .name = "dsi1p", | |
2124 | .ctl_reg = CM_DSI1PCTL, | |
2125 | .div_reg = CM_DSI1PDIV, | |
2126 | .int_bits = 0, | |
1c97aa6c EA |
2127 | .frac_bits = 0, |
2128 | .tcnt_mux = 13), | |
d3d6f15f | 2129 | |
56eb3a2e | 2130 | /* the gates */ |
3b15afef MS |
2131 | |
2132 | /* | |
2133 | * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if | |
2134 | * you have the debug bit set in the power manager, which we | |
2135 | * don't bother exposing) are individual gates off of the | |
2136 | * non-stop vpu clock. | |
2137 | */ | |
56eb3a2e | 2138 | [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE( |
3b15afef MS |
2139 | .name = "peri_image", |
2140 | .parent = "vpu", | |
2141 | .ctl_reg = CM_PERIICTL), | |
56eb3a2e MS |
2142 | }; |
2143 | ||
f1760ee6 PE |
2144 | static bool bcm2835_clk_claimed[ARRAY_SIZE(clk_desc_array)]; |
2145 | ||
9e400c5c EA |
2146 | /* |
2147 | * Permanently take a reference on the parent of the SDRAM clock. | |
2148 | * | |
2149 | * While the SDRAM is being driven by its dedicated PLL most of the | |
2150 | * time, there is a little loop running in the firmware that | |
2151 | * periodically switches the SDRAM to using our CM clock to do PVT | |
2152 | * recalibration, with the assumption that the previously configured | |
2153 | * SDRAM parent is still enabled and running. | |
2154 | */ | |
2155 | static int bcm2835_mark_sdc_parent_critical(struct clk *sdc) | |
2156 | { | |
2157 | struct clk *parent = clk_get_parent(sdc); | |
2158 | ||
2159 | if (IS_ERR(parent)) | |
2160 | return PTR_ERR(parent); | |
2161 | ||
2162 | return clk_prepare_enable(parent); | |
2163 | } | |
2164 | ||
f1760ee6 PE |
2165 | static bool bcm2835_clk_is_claimed(const char *name) |
2166 | { | |
2167 | int i; | |
2168 | ||
2169 | for (i = 0; i < ARRAY_SIZE(clk_desc_array); i++) { | |
2170 | const char *clk_name = *(const char **)(clk_desc_array[i].data); | |
2171 | if (!strcmp(name, clk_name)) | |
2172 | return bcm2835_clk_claimed[i]; | |
2173 | } | |
2174 | ||
2175 | return false; | |
2176 | } | |
2177 | ||
41691b88 EA |
2178 | static int bcm2835_clk_probe(struct platform_device *pdev) |
2179 | { | |
2180 | struct device *dev = &pdev->dev; | |
b19f009d | 2181 | struct clk_hw **hws; |
41691b88 EA |
2182 | struct bcm2835_cprman *cprman; |
2183 | struct resource *res; | |
56eb3a2e MS |
2184 | const struct bcm2835_clk_desc *desc; |
2185 | const size_t asize = ARRAY_SIZE(clk_desc_array); | |
11262d0f | 2186 | struct device_node *fw_node; |
56eb3a2e | 2187 | size_t i; |
f1760ee6 | 2188 | u32 clk_id; |
9e400c5c | 2189 | int ret; |
41691b88 | 2190 | |
b19f009d SB |
2191 | cprman = devm_kzalloc(dev, sizeof(*cprman) + |
2192 | sizeof(*cprman->onecell.hws) * asize, | |
56eb3a2e | 2193 | GFP_KERNEL); |
41691b88 EA |
2194 | if (!cprman) |
2195 | return -ENOMEM; | |
2196 | ||
2197 | spin_lock_init(&cprman->regs_lock); | |
2198 | cprman->dev = dev; | |
2199 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
2200 | cprman->regs = devm_ioremap_resource(dev, res); | |
2201 | if (IS_ERR(cprman->regs)) | |
2202 | return PTR_ERR(cprman->regs); | |
2203 | ||
11262d0f PE |
2204 | fw_node = of_parse_phandle(dev->of_node, "firmware", 0); |
2205 | if (fw_node) { | |
2206 | struct rpi_firmware *fw = rpi_firmware_get(NULL); | |
2207 | if (!fw) | |
2208 | return -EPROBE_DEFER; | |
2209 | cprman->fw = fw; | |
2210 | } | |
2211 | ||
f1760ee6 PE |
2212 | memset(bcm2835_clk_claimed, 0, sizeof(bcm2835_clk_claimed)); |
2213 | for (i = 0; | |
2214 | !of_property_read_u32_index(pdev->dev.of_node, "claim-clocks", | |
2215 | i, &clk_id); | |
2216 | i++) | |
2217 | bcm2835_clk_claimed[clk_id]= true; | |
2218 | ||
78b200b9 EA |
2219 | memcpy(cprman->real_parent_names, cprman_parent_names, |
2220 | sizeof(cprman_parent_names)); | |
2221 | of_clk_parent_fill(dev->of_node, cprman->real_parent_names, | |
2222 | ARRAY_SIZE(cprman_parent_names)); | |
2223 | ||
2224 | /* | |
2225 | * Make sure the external oscillator has been registered. | |
2226 | * | |
2227 | * The other (DSI) clocks are not present on older device | |
2228 | * trees, which we still need to support for backwards | |
2229 | * compatibility. | |
2230 | */ | |
2231 | if (!cprman->real_parent_names[0]) | |
41691b88 EA |
2232 | return -ENODEV; |
2233 | ||
2234 | platform_set_drvdata(pdev, cprman); | |
2235 | ||
b19f009d SB |
2236 | cprman->onecell.num = asize; |
2237 | hws = cprman->onecell.hws; | |
41691b88 | 2238 | |
56eb3a2e MS |
2239 | for (i = 0; i < asize; i++) { |
2240 | desc = &clk_desc_array[i]; | |
2241 | if (desc->clk_register && desc->data) | |
b19f009d | 2242 | hws[i] = desc->clk_register(cprman, desc->data); |
56eb3a2e | 2243 | } |
cfbab8fb | 2244 | |
b19f009d | 2245 | ret = bcm2835_mark_sdc_parent_critical(hws[BCM2835_CLOCK_SDRAM]->clk); |
9e400c5c EA |
2246 | if (ret) |
2247 | return ret; | |
2248 | ||
6d3fa5a5 | 2249 | ret = of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get, |
b19f009d | 2250 | &cprman->onecell); |
6d3fa5a5 MS |
2251 | if (ret) |
2252 | return ret; | |
2253 | ||
2254 | /* note that we have registered all the clocks */ | |
2255 | dev_dbg(dev, "registered %d clocks\n", asize); | |
2256 | ||
2257 | return 0; | |
41691b88 EA |
2258 | } |
2259 | ||
2260 | static const struct of_device_id bcm2835_clk_of_match[] = { | |
2261 | { .compatible = "brcm,bcm2835-cprman", }, | |
2262 | {} | |
2263 | }; | |
2264 | MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match); | |
2265 | ||
2266 | static struct platform_driver bcm2835_clk_driver = { | |
2267 | .driver = { | |
2268 | .name = "bcm2835-clk", | |
2269 | .of_match_table = bcm2835_clk_of_match, | |
2270 | }, | |
2271 | .probe = bcm2835_clk_probe, | |
2272 | }; | |
2273 | ||
6d3fa5a5 MS |
2274 | static int __init __bcm2835_clk_driver_init(void) |
2275 | { | |
2276 | return platform_driver_register(&bcm2835_clk_driver); | |
2277 | } | |
2278 | core_initcall(__bcm2835_clk_driver_init); | |
41691b88 EA |
2279 | |
2280 | MODULE_AUTHOR("Eric Anholt <eric@anholt.net>"); | |
2281 | MODULE_DESCRIPTION("BCM2835 clock driver"); | |
2282 | MODULE_LICENSE("GPL v2"); |