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