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