1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the ICST307 VCO clock found in the ARM Reference designs.
4 * We wrap the custom interface from <asm/hardware/icst.h> into the generic
7 * Copyright (C) 2012-2015 Linus Walleij
9 * TODO: when all ARM reference designs are migrated to generic clocks, the
10 * ICST clock code from the ARM tree should probably be merged into this
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/err.h>
17 #include <linux/clk-provider.h>
19 #include <linux/regmap.h>
20 #include <linux/mfd/syscon.h>
25 /* Magic unlocking token used on all Versatile boards */
26 #define VERSATILE_LOCK_VAL 0xA05F
28 #define VERSATILE_AUX_OSC_BITS 0x7FFFF
29 #define INTEGRATOR_AP_CM_BITS 0xFF
30 #define INTEGRATOR_AP_SYS_BITS 0xFF
31 #define INTEGRATOR_CP_CM_CORE_BITS 0x7FF
32 #define INTEGRATOR_CP_CM_MEM_BITS 0x7FF000
34 #define INTEGRATOR_AP_PCI_25_33_MHZ BIT(8)
37 * enum icst_control_type - the type of ICST control register
39 enum icst_control_type
{
40 ICST_VERSATILE
, /* The standard type, all control bits available */
41 ICST_INTEGRATOR_AP_CM
, /* Only 8 bits of VDW available */
42 ICST_INTEGRATOR_AP_SYS
, /* Only 8 bits of VDW available */
43 ICST_INTEGRATOR_AP_PCI
, /* Odd bit pattern storage */
44 ICST_INTEGRATOR_CP_CM_CORE
, /* Only 8 bits of VDW and 3 bits of OD */
45 ICST_INTEGRATOR_CP_CM_MEM
, /* Only 8 bits of VDW and 3 bits of OD */
49 * struct clk_icst - ICST VCO clock wrapper
50 * @hw: corresponding clock hardware entry
51 * @vcoreg: VCO register address
52 * @lockreg: VCO lock register address
53 * @params: parameters for this ICST instance
55 * @ctype: the type of control register for the ICST
62 struct icst_params
*params
;
64 enum icst_control_type ctype
;
67 #define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
70 * vco_get() - get ICST VCO settings from a certain ICST
71 * @icst: the ICST clock to get
72 * @vco: the VCO struct to return the value in
74 static int vco_get(struct clk_icst
*icst
, struct icst_vco
*vco
)
79 ret
= regmap_read(icst
->map
, icst
->vcoreg_off
, &val
);
84 * The Integrator/AP core clock can only access the low eight
85 * bits of the v PLL divider. Bit 8 is tied low and always zero,
86 * r is hardwired to 22 and output divider s is hardwired to 1
87 * (divide by 2) according to the document
88 * "Integrator CM926EJ-S, CM946E-S, CM966E-S, CM1026EJ-S and
89 * CM1136JF-S User Guide" ARM DUI 0138E, page 3-13 thru 3-14.
91 if (icst
->ctype
== ICST_INTEGRATOR_AP_CM
) {
92 vco
->v
= val
& INTEGRATOR_AP_CM_BITS
;
99 * The Integrator/AP system clock on the base board can only
100 * access the low eight bits of the v PLL divider. Bit 8 is tied low
101 * and always zero, r is hardwired to 46, and the output divider is
102 * hardwired to 3 (divide by 4) according to the document
103 * "Integrator AP ASIC Development Motherboard" ARM DUI 0098B,
106 if (icst
->ctype
== ICST_INTEGRATOR_AP_SYS
) {
107 vco
->v
= val
& INTEGRATOR_AP_SYS_BITS
;
114 * The Integrator/AP PCI clock is using an odd pattern to create
115 * the child clock, basically a single bit called DIVX/Y is used
116 * to select between two different hardwired values: setting the
117 * bit to 0 yields v = 17, r = 22 and OD = 1, whereas setting the
118 * bit to 1 yields v = 14, r = 14 and OD = 1 giving the frequencies
119 * 33 or 25 MHz respectively.
121 if (icst
->ctype
== ICST_INTEGRATOR_AP_PCI
) {
122 bool divxy
= !!(val
& INTEGRATOR_AP_PCI_25_33_MHZ
);
124 vco
->v
= divxy
? 17 : 14;
125 vco
->r
= divxy
? 22 : 14;
131 * The Integrator/CP core clock can access the low eight bits
132 * of the v PLL divider. Bit 8 is tied low and always zero,
133 * r is hardwired to 22 and the output divider s is accessible
134 * in bits 8 thru 10 according to the document
135 * "Integrator/CM940T, CM920T, CM740T, and CM720T User Guide"
136 * ARM DUI 0157A, page 3-20 thru 3-23 and 4-10.
138 if (icst
->ctype
== ICST_INTEGRATOR_CP_CM_CORE
) {
141 vco
->s
= (val
>> 8) & 7;
145 if (icst
->ctype
== ICST_INTEGRATOR_CP_CM_MEM
) {
146 vco
->v
= (val
>> 12) & 0xFF;
148 vco
->s
= (val
>> 20) & 7;
152 vco
->v
= val
& 0x1ff;
153 vco
->r
= (val
>> 9) & 0x7f;
154 vco
->s
= (val
>> 16) & 03;
159 * vco_set() - commit changes to an ICST VCO
160 * @icst: the ICST clock to set
161 * @vco: the VCO struct to set the changes from
163 static int vco_set(struct clk_icst
*icst
, struct icst_vco vco
)
169 /* Mask the bits used by the VCO */
170 switch (icst
->ctype
) {
171 case ICST_INTEGRATOR_AP_CM
:
172 mask
= INTEGRATOR_AP_CM_BITS
;
175 pr_err("ICST error: tried to set bit 8 of VDW\n");
177 pr_err("ICST error: tried to use VOD != 1\n");
179 pr_err("ICST error: tried to use RDW != 22\n");
181 case ICST_INTEGRATOR_AP_SYS
:
182 mask
= INTEGRATOR_AP_SYS_BITS
;
185 pr_err("ICST error: tried to set bit 8 of VDW\n");
187 pr_err("ICST error: tried to use VOD != 1\n");
189 pr_err("ICST error: tried to use RDW != 22\n");
191 case ICST_INTEGRATOR_CP_CM_CORE
:
192 mask
= INTEGRATOR_CP_CM_CORE_BITS
; /* Uses 12 bits */
193 val
= (vco
.v
& 0xFF) | vco
.s
<< 8;
195 pr_err("ICST error: tried to set bit 8 of VDW\n");
197 pr_err("ICST error: tried to use RDW != 22\n");
199 case ICST_INTEGRATOR_CP_CM_MEM
:
200 mask
= INTEGRATOR_CP_CM_MEM_BITS
; /* Uses 12 bits */
201 val
= ((vco
.v
& 0xFF) << 12) | (vco
.s
<< 20);
203 pr_err("ICST error: tried to set bit 8 of VDW\n");
205 pr_err("ICST error: tried to use RDW != 22\n");
208 /* Regular auxilary oscillator */
209 mask
= VERSATILE_AUX_OSC_BITS
;
210 val
= vco
.v
| (vco
.r
<< 9) | (vco
.s
<< 16);
214 pr_debug("ICST: new val = 0x%08x\n", val
);
216 /* This magic unlocks the VCO so it can be controlled */
217 ret
= regmap_write(icst
->map
, icst
->lockreg_off
, VERSATILE_LOCK_VAL
);
220 ret
= regmap_update_bits(icst
->map
, icst
->vcoreg_off
, mask
, val
);
223 /* This locks the VCO again */
224 ret
= regmap_write(icst
->map
, icst
->lockreg_off
, 0);
230 static unsigned long icst_recalc_rate(struct clk_hw
*hw
,
231 unsigned long parent_rate
)
233 struct clk_icst
*icst
= to_icst(hw
);
238 icst
->params
->ref
= parent_rate
;
239 ret
= vco_get(icst
, &vco
);
241 pr_err("ICST: could not get VCO setting\n");
244 icst
->rate
= icst_hz(icst
->params
, vco
);
248 static long icst_round_rate(struct clk_hw
*hw
, unsigned long rate
,
249 unsigned long *prate
)
251 struct clk_icst
*icst
= to_icst(hw
);
254 if (icst
->ctype
== ICST_INTEGRATOR_AP_CM
||
255 icst
->ctype
== ICST_INTEGRATOR_CP_CM_CORE
) {
256 if (rate
<= 12000000)
258 if (rate
>= 160000000)
260 /* Slam to closest megahertz */
261 return DIV_ROUND_CLOSEST(rate
, 1000000) * 1000000;
264 if (icst
->ctype
== ICST_INTEGRATOR_CP_CM_MEM
) {
267 if (rate
>= 66000000)
269 /* Slam to closest 0.5 megahertz */
270 return DIV_ROUND_CLOSEST(rate
, 500000) * 500000;
273 if (icst
->ctype
== ICST_INTEGRATOR_AP_SYS
) {
274 /* Divides between 3 and 50 MHz in steps of 0.25 MHz */
277 if (rate
>= 50000000)
279 /* Slam to closest 0.25 MHz */
280 return DIV_ROUND_CLOSEST(rate
, 250000) * 250000;
283 if (icst
->ctype
== ICST_INTEGRATOR_AP_PCI
) {
285 * If we're below or less than halfway from 25 to 33 MHz
288 if (rate
<= 25000000 || rate
< 29000000)
290 /* Else just return the default frequency */
294 vco
= icst_hz_to_vco(icst
->params
, rate
);
295 return icst_hz(icst
->params
, vco
);
298 static int icst_set_rate(struct clk_hw
*hw
, unsigned long rate
,
299 unsigned long parent_rate
)
301 struct clk_icst
*icst
= to_icst(hw
);
304 if (icst
->ctype
== ICST_INTEGRATOR_AP_PCI
) {
305 /* This clock is especially primitive */
309 if (rate
== 25000000) {
311 } else if (rate
== 33000000) {
312 val
= INTEGRATOR_AP_PCI_25_33_MHZ
;
314 pr_err("ICST: cannot set PCI frequency %lu\n",
318 ret
= regmap_write(icst
->map
, icst
->lockreg_off
,
322 ret
= regmap_update_bits(icst
->map
, icst
->vcoreg_off
,
323 INTEGRATOR_AP_PCI_25_33_MHZ
,
327 /* This locks the VCO again */
328 ret
= regmap_write(icst
->map
, icst
->lockreg_off
, 0);
335 icst
->params
->ref
= parent_rate
;
336 vco
= icst_hz_to_vco(icst
->params
, rate
);
337 icst
->rate
= icst_hz(icst
->params
, vco
);
338 return vco_set(icst
, vco
);
341 static const struct clk_ops icst_ops
= {
342 .recalc_rate
= icst_recalc_rate
,
343 .round_rate
= icst_round_rate
,
344 .set_rate
= icst_set_rate
,
347 static struct clk
*icst_clk_setup(struct device
*dev
,
348 const struct clk_icst_desc
*desc
,
350 const char *parent_name
,
352 enum icst_control_type ctype
)
355 struct clk_icst
*icst
;
356 struct clk_init_data init
;
357 struct icst_params
*pclone
;
359 icst
= kzalloc(sizeof(*icst
), GFP_KERNEL
);
361 return ERR_PTR(-ENOMEM
);
363 pclone
= kmemdup(desc
->params
, sizeof(*pclone
), GFP_KERNEL
);
366 return ERR_PTR(-ENOMEM
);
370 init
.ops
= &icst_ops
;
372 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
373 init
.num_parents
= (parent_name
? 1 : 0);
375 icst
->hw
.init
= &init
;
376 icst
->params
= pclone
;
377 icst
->vcoreg_off
= desc
->vco_offset
;
378 icst
->lockreg_off
= desc
->lock_offset
;
381 clk
= clk_register(dev
, &icst
->hw
);
390 struct clk
*icst_clk_register(struct device
*dev
,
391 const struct clk_icst_desc
*desc
,
393 const char *parent_name
,
396 struct regmap_config icst_regmap_conf
= {
403 map
= regmap_init_mmio(dev
, base
, &icst_regmap_conf
);
405 pr_err("could not initialize ICST regmap\n");
406 return ERR_CAST(map
);
408 return icst_clk_setup(dev
, desc
, name
, parent_name
, map
,
411 EXPORT_SYMBOL_GPL(icst_clk_register
);
415 * In a device tree, an memory-mapped ICST clock appear as a child
416 * of a syscon node. Assume this and probe it only as a child of a
420 static const struct icst_params icst525_params
= {
421 .vco_max
= ICST525_VCO_MAX_5V
,
422 .vco_min
= ICST525_VCO_MIN
,
427 .s2div
= icst525_s2div
,
428 .idx2s
= icst525_idx2s
,
431 static const struct icst_params icst307_params
= {
432 .vco_max
= ICST307_VCO_MAX
,
433 .vco_min
= ICST307_VCO_MIN
,
438 .s2div
= icst307_s2div
,
439 .idx2s
= icst307_idx2s
,
443 * The core modules on the Integrator/AP and Integrator/CP have
444 * especially crippled ICST525 control.
446 static const struct icst_params icst525_apcp_cm_params
= {
447 .vco_max
= ICST525_VCO_MAX_5V
,
448 .vco_min
= ICST525_VCO_MIN
,
449 /* Minimum 12 MHz, VDW = 4 */
452 * Maximum 160 MHz, VDW = 152 for all core modules, but
453 * CM926EJ-S, CM1026EJ-S and CM1136JF-S can actually
454 * go to 200 MHz (max VDW = 192).
457 /* r is hardcoded to 22 and this is the actual divisor, +2 */
460 .s2div
= icst525_s2div
,
461 .idx2s
= icst525_idx2s
,
464 static const struct icst_params icst525_ap_sys_params
= {
465 .vco_max
= ICST525_VCO_MAX_5V
,
466 .vco_min
= ICST525_VCO_MIN
,
467 /* Minimum 3 MHz, VDW = 4 */
469 /* Maximum 50 MHz, VDW = 192 */
471 /* r is hardcoded to 46 and this is the actual divisor, +2 */
474 .s2div
= icst525_s2div
,
475 .idx2s
= icst525_idx2s
,
478 static const struct icst_params icst525_ap_pci_params
= {
479 .vco_max
= ICST525_VCO_MAX_5V
,
480 .vco_min
= ICST525_VCO_MIN
,
485 /* r is hardcoded to 14 or 22 and this is the actual divisors +2 */
488 .s2div
= icst525_s2div
,
489 .idx2s
= icst525_idx2s
,
492 static void __init
of_syscon_icst_setup(struct device_node
*np
)
494 struct device_node
*parent
;
496 struct clk_icst_desc icst_desc
;
497 const char *name
= np
->name
;
498 const char *parent_name
;
500 enum icst_control_type ctype
;
502 /* We do not release this reference, we are using it perpetually */
503 parent
= of_get_parent(np
);
505 pr_err("no parent node for syscon ICST clock\n");
508 map
= syscon_node_to_regmap(parent
);
510 pr_err("no regmap for syscon ICST clock parent\n");
514 if (of_property_read_u32(np
, "vco-offset", &icst_desc
.vco_offset
)) {
515 pr_err("no VCO register offset for ICST clock\n");
518 if (of_property_read_u32(np
, "lock-offset", &icst_desc
.lock_offset
)) {
519 pr_err("no lock register offset for ICST clock\n");
523 if (of_device_is_compatible(np
, "arm,syscon-icst525")) {
524 icst_desc
.params
= &icst525_params
;
525 ctype
= ICST_VERSATILE
;
526 } else if (of_device_is_compatible(np
, "arm,syscon-icst307")) {
527 icst_desc
.params
= &icst307_params
;
528 ctype
= ICST_VERSATILE
;
529 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorap-cm")) {
530 icst_desc
.params
= &icst525_apcp_cm_params
;
531 ctype
= ICST_INTEGRATOR_AP_CM
;
532 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorap-sys")) {
533 icst_desc
.params
= &icst525_ap_sys_params
;
534 ctype
= ICST_INTEGRATOR_AP_SYS
;
535 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorap-pci")) {
536 icst_desc
.params
= &icst525_ap_pci_params
;
537 ctype
= ICST_INTEGRATOR_AP_PCI
;
538 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorcp-cm-core")) {
539 icst_desc
.params
= &icst525_apcp_cm_params
;
540 ctype
= ICST_INTEGRATOR_CP_CM_CORE
;
541 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorcp-cm-mem")) {
542 icst_desc
.params
= &icst525_apcp_cm_params
;
543 ctype
= ICST_INTEGRATOR_CP_CM_MEM
;
545 pr_err("unknown ICST clock %s\n", name
);
549 /* Parent clock name is not the same as node parent */
550 parent_name
= of_clk_get_parent_name(np
, 0);
552 regclk
= icst_clk_setup(NULL
, &icst_desc
, name
, parent_name
, map
, ctype
);
553 if (IS_ERR(regclk
)) {
554 pr_err("error setting up syscon ICST clock %s\n", name
);
557 of_clk_add_provider(np
, of_clk_src_simple_get
, regclk
);
558 pr_debug("registered syscon ICST clock %s\n", name
);
561 CLK_OF_DECLARE(arm_syscon_icst525_clk
,
562 "arm,syscon-icst525", of_syscon_icst_setup
);
563 CLK_OF_DECLARE(arm_syscon_icst307_clk
,
564 "arm,syscon-icst307", of_syscon_icst_setup
);
565 CLK_OF_DECLARE(arm_syscon_integratorap_cm_clk
,
566 "arm,syscon-icst525-integratorap-cm", of_syscon_icst_setup
);
567 CLK_OF_DECLARE(arm_syscon_integratorap_sys_clk
,
568 "arm,syscon-icst525-integratorap-sys", of_syscon_icst_setup
);
569 CLK_OF_DECLARE(arm_syscon_integratorap_pci_clk
,
570 "arm,syscon-icst525-integratorap-pci", of_syscon_icst_setup
);
571 CLK_OF_DECLARE(arm_syscon_integratorcp_cm_core_clk
,
572 "arm,syscon-icst525-integratorcp-cm-core", of_syscon_icst_setup
);
573 CLK_OF_DECLARE(arm_syscon_integratorcp_cm_mem_clk
,
574 "arm,syscon-icst525-integratorcp-cm-mem", of_syscon_icst_setup
);