2 * Synopsys HSDK SDP Generic PLL clock driver
4 * Copyright (C) 2017 Synopsys
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/clk-provider.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
22 #define CGU_PLL_CTRL 0x000 /* ARC PLL control register */
23 #define CGU_PLL_STATUS 0x004 /* ARC PLL status register */
24 #define CGU_PLL_FMEAS 0x008 /* ARC PLL frequency measurement register */
25 #define CGU_PLL_MON 0x00C /* ARC PLL monitor register */
27 #define CGU_PLL_CTRL_ODIV_SHIFT 2
28 #define CGU_PLL_CTRL_IDIV_SHIFT 4
29 #define CGU_PLL_CTRL_FBDIV_SHIFT 9
30 #define CGU_PLL_CTRL_BAND_SHIFT 20
32 #define CGU_PLL_CTRL_ODIV_MASK GENMASK(3, CGU_PLL_CTRL_ODIV_SHIFT)
33 #define CGU_PLL_CTRL_IDIV_MASK GENMASK(8, CGU_PLL_CTRL_IDIV_SHIFT)
34 #define CGU_PLL_CTRL_FBDIV_MASK GENMASK(15, CGU_PLL_CTRL_FBDIV_SHIFT)
36 #define CGU_PLL_CTRL_PD BIT(0)
37 #define CGU_PLL_CTRL_BYPASS BIT(1)
39 #define CGU_PLL_STATUS_LOCK BIT(0)
40 #define CGU_PLL_STATUS_ERR BIT(1)
42 #define HSDK_PLL_MAX_LOCK_TIME 100 /* 100 us */
44 #define CGU_PLL_SOURCE_MAX 1
46 #define CORE_IF_CLK_THRESHOLD_HZ 500000000
47 #define CREG_CORE_IF_CLK_DIV_1 0x0
48 #define CREG_CORE_IF_CLK_DIV_2 0x1
59 static const struct hsdk_pll_cfg asdt_pll_cfg
[] = {
60 { 100000000, 0, 11, 3, 0, 0 },
61 { 133000000, 0, 15, 3, 0, 0 },
62 { 200000000, 1, 47, 3, 0, 0 },
63 { 233000000, 1, 27, 2, 0, 0 },
64 { 300000000, 1, 35, 2, 0, 0 },
65 { 333000000, 1, 39, 2, 0, 0 },
66 { 400000000, 1, 47, 2, 0, 0 },
67 { 500000000, 0, 14, 1, 0, 0 },
68 { 600000000, 0, 17, 1, 0, 0 },
69 { 700000000, 0, 20, 1, 0, 0 },
70 { 800000000, 0, 23, 1, 0, 0 },
71 { 900000000, 1, 26, 0, 0, 0 },
72 { 1000000000, 1, 29, 0, 0, 0 },
73 { 1100000000, 1, 32, 0, 0, 0 },
74 { 1200000000, 1, 35, 0, 0, 0 },
75 { 1300000000, 1, 38, 0, 0, 0 },
76 { 1400000000, 1, 41, 0, 0, 0 },
77 { 1500000000, 1, 44, 0, 0, 0 },
78 { 1600000000, 1, 47, 0, 0, 0 },
82 static const struct hsdk_pll_cfg hdmi_pll_cfg
[] = {
83 { 27000000, 0, 0, 0, 0, 1 },
84 { 148500000, 0, 21, 3, 0, 0 },
85 { 297000000, 0, 21, 2, 0, 0 },
86 { 540000000, 0, 19, 1, 0, 0 },
87 { 594000000, 0, 21, 1, 0, 0 },
94 void __iomem
*spec_regs
;
95 const struct hsdk_pll_devdata
*pll_devdata
;
99 struct hsdk_pll_devdata
{
100 const struct hsdk_pll_cfg
*pll_cfg
;
101 int (*update_rate
)(struct hsdk_pll_clk
*clk
, unsigned long rate
,
102 const struct hsdk_pll_cfg
*cfg
);
105 static int hsdk_pll_core_update_rate(struct hsdk_pll_clk
*, unsigned long,
106 const struct hsdk_pll_cfg
*);
107 static int hsdk_pll_comm_update_rate(struct hsdk_pll_clk
*, unsigned long,
108 const struct hsdk_pll_cfg
*);
110 static const struct hsdk_pll_devdata core_pll_devdata
= {
111 .pll_cfg
= asdt_pll_cfg
,
112 .update_rate
= hsdk_pll_core_update_rate
,
115 static const struct hsdk_pll_devdata sdt_pll_devdata
= {
116 .pll_cfg
= asdt_pll_cfg
,
117 .update_rate
= hsdk_pll_comm_update_rate
,
120 static const struct hsdk_pll_devdata hdmi_pll_devdata
= {
121 .pll_cfg
= hdmi_pll_cfg
,
122 .update_rate
= hsdk_pll_comm_update_rate
,
125 static inline void hsdk_pll_write(struct hsdk_pll_clk
*clk
, u32 reg
, u32 val
)
127 iowrite32(val
, clk
->regs
+ reg
);
130 static inline u32
hsdk_pll_read(struct hsdk_pll_clk
*clk
, u32 reg
)
132 return ioread32(clk
->regs
+ reg
);
135 static inline void hsdk_pll_set_cfg(struct hsdk_pll_clk
*clk
,
136 const struct hsdk_pll_cfg
*cfg
)
141 val
= hsdk_pll_read(clk
, CGU_PLL_CTRL
);
142 val
|= CGU_PLL_CTRL_BYPASS
;
144 /* Powerdown and Bypass bits should be cleared */
145 val
|= cfg
->idiv
<< CGU_PLL_CTRL_IDIV_SHIFT
;
146 val
|= cfg
->fbdiv
<< CGU_PLL_CTRL_FBDIV_SHIFT
;
147 val
|= cfg
->odiv
<< CGU_PLL_CTRL_ODIV_SHIFT
;
148 val
|= cfg
->band
<< CGU_PLL_CTRL_BAND_SHIFT
;
151 dev_dbg(clk
->dev
, "write configuration: %#x\n", val
);
153 hsdk_pll_write(clk
, CGU_PLL_CTRL
, val
);
156 static inline bool hsdk_pll_is_locked(struct hsdk_pll_clk
*clk
)
158 return !!(hsdk_pll_read(clk
, CGU_PLL_STATUS
) & CGU_PLL_STATUS_LOCK
);
161 static inline bool hsdk_pll_is_err(struct hsdk_pll_clk
*clk
)
163 return !!(hsdk_pll_read(clk
, CGU_PLL_STATUS
) & CGU_PLL_STATUS_ERR
);
166 static inline struct hsdk_pll_clk
*to_hsdk_pll_clk(struct clk_hw
*hw
)
168 return container_of(hw
, struct hsdk_pll_clk
, hw
);
171 static unsigned long hsdk_pll_recalc_rate(struct clk_hw
*hw
,
172 unsigned long parent_rate
)
176 u32 idiv
, fbdiv
, odiv
;
177 struct hsdk_pll_clk
*clk
= to_hsdk_pll_clk(hw
);
179 val
= hsdk_pll_read(clk
, CGU_PLL_CTRL
);
181 dev_dbg(clk
->dev
, "current configuration: %#x\n", val
);
183 /* Check if PLL is bypassed */
184 if (val
& CGU_PLL_CTRL_BYPASS
)
187 /* Check if PLL is disabled */
188 if (val
& CGU_PLL_CTRL_PD
)
191 /* input divider = reg.idiv + 1 */
192 idiv
= 1 + ((val
& CGU_PLL_CTRL_IDIV_MASK
) >> CGU_PLL_CTRL_IDIV_SHIFT
);
193 /* fb divider = 2*(reg.fbdiv + 1) */
194 fbdiv
= 2 * (1 + ((val
& CGU_PLL_CTRL_FBDIV_MASK
) >> CGU_PLL_CTRL_FBDIV_SHIFT
));
195 /* output divider = 2^(reg.odiv) */
196 odiv
= 1 << ((val
& CGU_PLL_CTRL_ODIV_MASK
) >> CGU_PLL_CTRL_ODIV_SHIFT
);
198 rate
= (u64
)parent_rate
* fbdiv
;
199 do_div(rate
, idiv
* odiv
);
204 static long hsdk_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
205 unsigned long *prate
)
208 unsigned long best_rate
;
209 struct hsdk_pll_clk
*clk
= to_hsdk_pll_clk(hw
);
210 const struct hsdk_pll_cfg
*pll_cfg
= clk
->pll_devdata
->pll_cfg
;
212 if (pll_cfg
[0].rate
== 0)
215 best_rate
= pll_cfg
[0].rate
;
217 for (i
= 1; pll_cfg
[i
].rate
!= 0; i
++) {
218 if (abs(rate
- pll_cfg
[i
].rate
) < abs(rate
- best_rate
))
219 best_rate
= pll_cfg
[i
].rate
;
222 dev_dbg(clk
->dev
, "chosen best rate: %lu\n", best_rate
);
227 static int hsdk_pll_comm_update_rate(struct hsdk_pll_clk
*clk
,
229 const struct hsdk_pll_cfg
*cfg
)
231 hsdk_pll_set_cfg(clk
, cfg
);
234 * Wait until CGU relocks and check error status.
235 * If after timeout CGU is unlocked yet return error.
237 udelay(HSDK_PLL_MAX_LOCK_TIME
);
238 if (!hsdk_pll_is_locked(clk
))
241 if (hsdk_pll_is_err(clk
))
247 static int hsdk_pll_core_update_rate(struct hsdk_pll_clk
*clk
,
249 const struct hsdk_pll_cfg
*cfg
)
252 * When core clock exceeds 500MHz, the divider for the interface
253 * clock must be programmed to div-by-2.
255 if (rate
> CORE_IF_CLK_THRESHOLD_HZ
)
256 iowrite32(CREG_CORE_IF_CLK_DIV_2
, clk
->spec_regs
);
258 hsdk_pll_set_cfg(clk
, cfg
);
261 * Wait until CGU relocks and check error status.
262 * If after timeout CGU is unlocked yet return error.
264 udelay(HSDK_PLL_MAX_LOCK_TIME
);
265 if (!hsdk_pll_is_locked(clk
))
268 if (hsdk_pll_is_err(clk
))
272 * Program divider to div-by-1 if we succesfuly set core clock below
275 if (rate
<= CORE_IF_CLK_THRESHOLD_HZ
)
276 iowrite32(CREG_CORE_IF_CLK_DIV_1
, clk
->spec_regs
);
281 static int hsdk_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
282 unsigned long parent_rate
)
285 struct hsdk_pll_clk
*clk
= to_hsdk_pll_clk(hw
);
286 const struct hsdk_pll_cfg
*pll_cfg
= clk
->pll_devdata
->pll_cfg
;
288 for (i
= 0; pll_cfg
[i
].rate
!= 0; i
++) {
289 if (pll_cfg
[i
].rate
== rate
) {
290 return clk
->pll_devdata
->update_rate(clk
, rate
,
295 dev_err(clk
->dev
, "invalid rate=%ld, parent_rate=%ld\n", rate
,
301 static const struct clk_ops hsdk_pll_ops
= {
302 .recalc_rate
= hsdk_pll_recalc_rate
,
303 .round_rate
= hsdk_pll_round_rate
,
304 .set_rate
= hsdk_pll_set_rate
,
307 static int hsdk_pll_clk_probe(struct platform_device
*pdev
)
310 struct resource
*mem
;
311 const char *parent_name
;
312 unsigned int num_parents
;
313 struct hsdk_pll_clk
*pll_clk
;
314 struct clk_init_data init
= { };
315 struct device
*dev
= &pdev
->dev
;
317 pll_clk
= devm_kzalloc(dev
, sizeof(*pll_clk
), GFP_KERNEL
);
321 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
322 pll_clk
->regs
= devm_ioremap_resource(dev
, mem
);
323 if (IS_ERR(pll_clk
->regs
))
324 return PTR_ERR(pll_clk
->regs
);
326 init
.name
= dev
->of_node
->name
;
327 init
.ops
= &hsdk_pll_ops
;
328 parent_name
= of_clk_get_parent_name(dev
->of_node
, 0);
329 init
.parent_names
= &parent_name
;
330 num_parents
= of_clk_get_parent_count(dev
->of_node
);
331 if (num_parents
== 0 || num_parents
> CGU_PLL_SOURCE_MAX
) {
332 dev_err(dev
, "wrong clock parents number: %u\n", num_parents
);
335 init
.num_parents
= num_parents
;
337 pll_clk
->hw
.init
= &init
;
339 pll_clk
->pll_devdata
= of_device_get_match_data(dev
);
341 if (!pll_clk
->pll_devdata
) {
342 dev_err(dev
, "No OF match data provided\n");
346 ret
= devm_clk_hw_register(dev
, &pll_clk
->hw
);
348 dev_err(dev
, "failed to register %s clock\n", init
.name
);
352 return of_clk_add_hw_provider(dev
->of_node
, of_clk_hw_simple_get
,
356 static int hsdk_pll_clk_remove(struct platform_device
*pdev
)
358 of_clk_del_provider(pdev
->dev
.of_node
);
362 static void __init
of_hsdk_pll_clk_setup(struct device_node
*node
)
365 const char *parent_name
;
366 unsigned int num_parents
;
367 struct hsdk_pll_clk
*pll_clk
;
368 struct clk_init_data init
= { };
370 pll_clk
= kzalloc(sizeof(*pll_clk
), GFP_KERNEL
);
374 pll_clk
->regs
= of_iomap(node
, 0);
375 if (!pll_clk
->regs
) {
376 pr_err("failed to map pll registers\n");
377 goto err_free_pll_clk
;
380 pll_clk
->spec_regs
= of_iomap(node
, 1);
381 if (!pll_clk
->spec_regs
) {
382 pr_err("failed to map pll registers\n");
383 goto err_unmap_comm_regs
;
386 init
.name
= node
->name
;
387 init
.ops
= &hsdk_pll_ops
;
388 parent_name
= of_clk_get_parent_name(node
, 0);
389 init
.parent_names
= &parent_name
;
390 num_parents
= of_clk_get_parent_count(node
);
391 if (num_parents
> CGU_PLL_SOURCE_MAX
) {
392 pr_err("too much clock parents: %u\n", num_parents
);
393 goto err_unmap_spec_regs
;
395 init
.num_parents
= num_parents
;
397 pll_clk
->hw
.init
= &init
;
398 pll_clk
->pll_devdata
= &core_pll_devdata
;
400 ret
= clk_hw_register(NULL
, &pll_clk
->hw
);
402 pr_err("failed to register %pOFn clock\n", node
);
403 goto err_unmap_spec_regs
;
406 ret
= of_clk_add_hw_provider(node
, of_clk_hw_simple_get
, &pll_clk
->hw
);
408 pr_err("failed to add hw provider for %pOFn clock\n", node
);
409 goto err_unmap_spec_regs
;
415 iounmap(pll_clk
->spec_regs
);
417 iounmap(pll_clk
->regs
);
422 /* Core PLL needed early for ARC cpus timers */
423 CLK_OF_DECLARE(hsdk_pll_clock
, "snps,hsdk-core-pll-clock",
424 of_hsdk_pll_clk_setup
);
426 static const struct of_device_id hsdk_pll_clk_id
[] = {
427 { .compatible
= "snps,hsdk-gp-pll-clock", .data
= &sdt_pll_devdata
},
428 { .compatible
= "snps,hsdk-hdmi-pll-clock", .data
= &hdmi_pll_devdata
},
432 static struct platform_driver hsdk_pll_clk_driver
= {
434 .name
= "hsdk-gp-pll-clock",
435 .of_match_table
= hsdk_pll_clk_id
,
437 .probe
= hsdk_pll_clk_probe
,
438 .remove
= hsdk_pll_clk_remove
,
440 builtin_platform_driver(hsdk_pll_clk_driver
);